blob: e87bb6f6f27ee540065ab1071137cbb4efee3503 [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 Moolenaar1d2ba7f2006-02-14 22:29:30 +0000151static void ex_win_close __ARGS((int forceit, win_T *win));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152static void ex_only __ARGS((exarg_T *eap));
153static void ex_all __ARGS((exarg_T *eap));
154static void ex_resize __ARGS((exarg_T *eap));
155static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000156static void ex_tabclose __ARGS((exarg_T *eap));
157static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158#else
159# define ex_close ex_ni
160# define ex_only ex_ni
161# define ex_all ex_ni
162# define ex_resize ex_ni
163# define ex_splitview ex_ni
164# define ex_stag ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000165# define ex_tabedit ex_ni
166# define ex_tab ex_ni
167# define ex_tabs ex_ni
168# define ex_tabclose ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif
170#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
171static void ex_pclose __ARGS((exarg_T *eap));
172static void ex_ptag __ARGS((exarg_T *eap));
173static void ex_pedit __ARGS((exarg_T *eap));
174#else
175# define ex_pclose ex_ni
176# define ex_ptag ex_ni
177# define ex_pedit ex_ni
178#endif
179static void ex_hide __ARGS((exarg_T *eap));
180static void ex_stop __ARGS((exarg_T *eap));
181static void ex_exit __ARGS((exarg_T *eap));
182static void ex_print __ARGS((exarg_T *eap));
183#ifdef FEAT_BYTEOFF
184static void ex_goto __ARGS((exarg_T *eap));
185#else
186# define ex_goto ex_ni
187#endif
188static void ex_shell __ARGS((exarg_T *eap));
189static void ex_preserve __ARGS((exarg_T *eap));
190static void ex_recover __ARGS((exarg_T *eap));
191#ifndef FEAT_LISTCMDS
192# define ex_argedit ex_ni
193# define ex_argadd ex_ni
194# define ex_argdelete ex_ni
195# define ex_listdo ex_ni
196#endif
197static void ex_mode __ARGS((exarg_T *eap));
198static void ex_wrongmodifier __ARGS((exarg_T *eap));
199static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000200static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201static void ex_edit __ARGS((exarg_T *eap));
202#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
203# define ex_drop ex_ni
204#endif
205#ifndef FEAT_GUI
206# define ex_gui ex_nogui
207static void ex_nogui __ARGS((exarg_T *eap));
208#endif
209#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
210static void ex_tearoff __ARGS((exarg_T *eap));
211#else
212# define ex_tearoff ex_ni
213#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000214#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215static void ex_popup __ARGS((exarg_T *eap));
216#else
217# define ex_popup ex_ni
218#endif
219#ifndef FEAT_GUI_MSWIN
220# define ex_simalt ex_ni
221#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000222#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223# define gui_mch_find_dialog ex_ni
224# define gui_mch_replace_dialog ex_ni
225#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000226#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227# define ex_helpfind ex_ni
228#endif
229#ifndef FEAT_CSCOPE
230# define do_cscope ex_ni
231# define do_scscope ex_ni
232# define do_cstag ex_ni
233#endif
234#ifndef FEAT_SYN_HL
235# define ex_syntax ex_ni
Bram Moolenaarb765d632005-06-07 21:00:02 +0000236# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000237# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000238# define ex_spelldump ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000239# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000240#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000241#ifndef FEAT_MZSCHEME
242# define ex_mzscheme ex_script_ni
243# define ex_mzfile ex_ni
244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245#ifndef FEAT_PERL
246# define ex_perl ex_script_ni
247# define ex_perldo ex_ni
248#endif
249#ifndef FEAT_PYTHON
250# define ex_python ex_script_ni
251# define ex_pyfile ex_ni
252#endif
253#ifndef FEAT_TCL
254# define ex_tcl ex_script_ni
255# define ex_tcldo ex_ni
256# define ex_tclfile ex_ni
257#endif
258#ifndef FEAT_RUBY
259# define ex_ruby ex_script_ni
260# define ex_rubydo ex_ni
261# define ex_rubyfile ex_ni
262#endif
263#ifndef FEAT_SNIFF
264# define ex_sniff ex_ni
265#endif
266#ifndef FEAT_KEYMAP
267# define ex_loadkeymap ex_ni
268#endif
269static void ex_swapname __ARGS((exarg_T *eap));
270static void ex_syncbind __ARGS((exarg_T *eap));
271static void ex_read __ARGS((exarg_T *eap));
272static void ex_cd __ARGS((exarg_T *eap));
273static void ex_pwd __ARGS((exarg_T *eap));
274static void ex_equal __ARGS((exarg_T *eap));
275static void ex_sleep __ARGS((exarg_T *eap));
276static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
277static void ex_winsize __ARGS((exarg_T *eap));
278#ifdef FEAT_WINDOWS
279static void ex_wincmd __ARGS((exarg_T *eap));
280#else
281# define ex_wincmd ex_ni
282#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000283#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284static void ex_winpos __ARGS((exarg_T *eap));
285#else
286# define ex_winpos ex_ni
287#endif
288static void ex_operators __ARGS((exarg_T *eap));
289static void ex_put __ARGS((exarg_T *eap));
290static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000291static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292static void ex_submagic __ARGS((exarg_T *eap));
293static void ex_join __ARGS((exarg_T *eap));
294static void ex_at __ARGS((exarg_T *eap));
295static void ex_bang __ARGS((exarg_T *eap));
296static void ex_undo __ARGS((exarg_T *eap));
297static void ex_redo __ARGS((exarg_T *eap));
298static void ex_redir __ARGS((exarg_T *eap));
299static void ex_redraw __ARGS((exarg_T *eap));
300static void ex_redrawstatus __ARGS((exarg_T *eap));
301static void close_redir __ARGS((void));
302static void ex_mkrc __ARGS((exarg_T *eap));
303static void ex_mark __ARGS((exarg_T *eap));
304#ifdef FEAT_USR_CMDS
305static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000306static 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 +0000307#endif
308#ifdef FEAT_EX_EXTRA
309static void ex_normal __ARGS((exarg_T *eap));
310static void ex_startinsert __ARGS((exarg_T *eap));
311static void ex_stopinsert __ARGS((exarg_T *eap));
312#else
313# define ex_normal ex_ni
314# define ex_align ex_ni
315# define ex_retab ex_ni
316# define ex_startinsert ex_ni
317# define ex_stopinsert ex_ni
318# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000319# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#endif
321#ifdef FEAT_FIND_ID
322static void ex_checkpath __ARGS((exarg_T *eap));
323static void ex_findpat __ARGS((exarg_T *eap));
324#else
325# define ex_findpat ex_ni
326# define ex_checkpath ex_ni
327#endif
328#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
329static void ex_psearch __ARGS((exarg_T *eap));
330#else
331# define ex_psearch ex_ni
332#endif
333static void ex_tag __ARGS((exarg_T *eap));
334static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
335#ifndef FEAT_EVAL
336# define ex_scriptnames ex_ni
337# define ex_finish ex_ni
338# define ex_echo ex_ni
339# define ex_echohl ex_ni
340# define ex_execute ex_ni
341# define ex_call ex_ni
342# define ex_if ex_ni
343# define ex_endif ex_ni
344# define ex_else ex_ni
345# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000346# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_continue ex_ni
348# define ex_break ex_ni
349# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000350# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351# define ex_throw ex_ni
352# define ex_try ex_ni
353# define ex_catch ex_ni
354# define ex_finally ex_ni
355# define ex_endtry ex_ni
356# define ex_endfunction ex_ni
357# define ex_let ex_ni
358# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000359# define ex_lockvar ex_ni
360# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361# define ex_function ex_ni
362# define ex_delfunction ex_ni
363# define ex_return ex_ni
364#endif
365static char_u *arg_all __ARGS((void));
366#ifdef FEAT_SESSION
367static int makeopens __ARGS((FILE *fd, char_u *dirnow));
368static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp));
369static void ex_loadview __ARGS((exarg_T *eap));
370static char_u *get_view_file __ARGS((int c));
371#else
372# define ex_loadview ex_ni
373#endif
374#ifndef FEAT_EVAL
375# define ex_compiler ex_ni
376#endif
377#ifdef FEAT_VIMINFO
378static void ex_viminfo __ARGS((exarg_T *eap));
379#else
380# define ex_viminfo ex_ni
381#endif
382static void ex_behave __ARGS((exarg_T *eap));
383#ifdef FEAT_AUTOCMD
384static void ex_filetype __ARGS((exarg_T *eap));
385static void ex_setfiletype __ARGS((exarg_T *eap));
386#else
387# define ex_filetype ex_ni
388# define ex_setfiletype ex_ni
389#endif
390#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000391# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392# define ex_diffpatch ex_ni
393# define ex_diffgetput ex_ni
394# define ex_diffsplit ex_ni
395# define ex_diffthis ex_ni
396# define ex_diffupdate ex_ni
397#endif
398static void ex_digraphs __ARGS((exarg_T *eap));
399static void ex_set __ARGS((exarg_T *eap));
400#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
401# define ex_options ex_ni
402#endif
403#ifdef FEAT_SEARCH_EXTRA
404static void ex_nohlsearch __ARGS((exarg_T *eap));
405static void ex_match __ARGS((exarg_T *eap));
406#else
407# define ex_nohlsearch ex_ni
408# define ex_match ex_ni
409#endif
410#ifdef FEAT_CRYPT
411static void ex_X __ARGS((exarg_T *eap));
412#else
413# define ex_X ex_ni
414#endif
415#ifdef FEAT_FOLDING
416static void ex_fold __ARGS((exarg_T *eap));
417static void ex_foldopen __ARGS((exarg_T *eap));
418static void ex_folddo __ARGS((exarg_T *eap));
419#else
420# define ex_fold ex_ni
421# define ex_foldopen ex_ni
422# define ex_folddo ex_ni
423#endif
424#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
425 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
426# define ex_language ex_ni
427#endif
428#ifndef FEAT_SIGNS
429# define ex_sign ex_ni
430#endif
431#ifndef FEAT_SUN_WORKSHOP
432# define ex_wsverb ex_ni
433#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000434#ifndef FEAT_NETBEANS_INTG
435# define ex_nbkey ex_ni
436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437
438#ifndef FEAT_EVAL
439# define ex_debug ex_ni
440# define ex_breakadd ex_ni
441# define ex_debuggreedy ex_ni
442# define ex_breakdel ex_ni
443# define ex_breaklist ex_ni
444#endif
445
446#ifndef FEAT_CMDHIST
447# define ex_history ex_ni
448#endif
449#ifndef FEAT_JUMPLIST
450# define ex_jumps ex_ni
451# define ex_changes ex_ni
452#endif
453
Bram Moolenaar05159a02005-02-26 23:04:13 +0000454#ifndef FEAT_PROFILE
455# define ex_profile ex_ni
456#endif
457
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458/*
459 * Declare cmdnames[].
460 */
461#define DO_DECLARE_EXCMD
462#include "ex_cmds.h"
463
464/*
465 * Table used to quickly search for a command, based on its first character.
466 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000467static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468{
469 CMD_append,
470 CMD_buffer,
471 CMD_change,
472 CMD_delete,
473 CMD_edit,
474 CMD_file,
475 CMD_global,
476 CMD_help,
477 CMD_insert,
478 CMD_join,
479 CMD_k,
480 CMD_list,
481 CMD_move,
482 CMD_next,
483 CMD_open,
484 CMD_print,
485 CMD_quit,
486 CMD_read,
487 CMD_substitute,
488 CMD_t,
489 CMD_undo,
490 CMD_vglobal,
491 CMD_write,
492 CMD_xit,
493 CMD_yank,
494 CMD_z,
495 CMD_bang
496};
497
498static char_u dollar_command[2] = {'$', 0};
499
500
501#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000502/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503typedef struct
504{
505 char_u *line; /* command line */
506 linenr_T lnum; /* sourcing_lnum of the line */
507} wcmd_T;
508
509/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000510 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000512 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000514struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515{
516 garray_T *lines_gap; /* growarray with line info */
517 int current_line; /* last read line from growarray */
518 int repeating; /* TRUE when looping a second time */
519 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
520 char_u *(*getline) __ARGS((int, void *, int));
521 void *cookie;
522};
523
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000524static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
525static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000527
528/* Struct to save a few things while debugging. Used in do_cmdline() only. */
529struct dbg_stuff
530{
531 int trylevel;
532 int force_abort;
533 except_T *caught_stack;
534 char_u *vv_exception;
535 char_u *vv_throwpoint;
536 int did_emsg;
537 int got_int;
538 int did_throw;
539 int need_rethrow;
540 int check_cstack;
541 except_T *current_exception;
542};
543
544static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
545static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
546
547 static void
548save_dbg_stuff(dsp)
549 struct dbg_stuff *dsp;
550{
551 dsp->trylevel = trylevel; trylevel = 0;
552 dsp->force_abort = force_abort; force_abort = FALSE;
553 dsp->caught_stack = caught_stack; caught_stack = NULL;
554 dsp->vv_exception = v_exception(NULL);
555 dsp->vv_throwpoint = v_throwpoint(NULL);
556
557 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
558 dsp->did_emsg = did_emsg; did_emsg = FALSE;
559 dsp->got_int = got_int; got_int = FALSE;
560 dsp->did_throw = did_throw; did_throw = FALSE;
561 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
562 dsp->check_cstack = check_cstack; check_cstack = FALSE;
563 dsp->current_exception = current_exception; current_exception = NULL;
564}
565
566 static void
567restore_dbg_stuff(dsp)
568 struct dbg_stuff *dsp;
569{
570 suppress_errthrow = FALSE;
571 trylevel = dsp->trylevel;
572 force_abort = dsp->force_abort;
573 caught_stack = dsp->caught_stack;
574 (void)v_exception(dsp->vv_exception);
575 (void)v_throwpoint(dsp->vv_throwpoint);
576 did_emsg = dsp->did_emsg;
577 got_int = dsp->got_int;
578 did_throw = dsp->did_throw;
579 need_rethrow = dsp->need_rethrow;
580 check_cstack = dsp->check_cstack;
581 current_exception = dsp->current_exception;
582}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583#endif
584
585
586/*
587 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
588 * command is given.
589 */
590 void
591do_exmode(improved)
592 int improved; /* TRUE for "improved Ex" mode */
593{
594 int save_msg_scroll;
595 int prev_msg_row;
596 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000597 int changedtick;
598
599 if (improved)
600 exmode_active = EXMODE_VIM;
601 else
602 exmode_active = EXMODE_NORMAL;
603 State = NORMAL;
604
605 /* When using ":global /pat/ visual" and then "Q" we return to continue
606 * the :global command. */
607 if (global_busy)
608 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
610 save_msg_scroll = msg_scroll;
611 ++RedrawingDisabled; /* don't redisplay the window */
612 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613#ifdef FEAT_GUI
614 /* Ignore scrollbar and mouse events in Ex mode */
615 ++hold_gui_events;
616#endif
617#ifdef FEAT_SNIFF
618 want_sniff_request = 0; /* No K_SNIFF wanted */
619#endif
620
621 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
622 while (exmode_active)
623 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000624#ifdef FEAT_EX_EXTRA
625 /* Check for a ":normal" command and no more characters left. */
626 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
627 {
628 exmode_active = FALSE;
629 break;
630 }
631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 msg_scroll = TRUE;
633 need_wait_return = FALSE;
634 ex_pressedreturn = FALSE;
635 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000636 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 prev_msg_row = msg_row;
638 prev_line = curwin->w_cursor.lnum;
639#ifdef FEAT_SNIFF
640 ProcessSniffRequests();
641#endif
642 if (improved)
643 {
644 cmdline_row = msg_row;
645 do_cmdline(NULL, getexline, NULL, 0);
646 }
647 else
648 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
649 lines_left = Rows - 1;
650
Bram Moolenaardf177f62005-02-22 08:39:57 +0000651 if ((prev_line != curwin->w_cursor.lnum
652 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000654 if (curbuf->b_ml.ml_flags & ML_EMPTY)
655 EMSG(_(e_emptybuf));
656 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000658 if (ex_pressedreturn)
659 {
660 /* go up one line, to overwrite the ":<CR>" line, so the
661 * output doensn't contain empty lines. */
662 msg_row = prev_msg_row;
663 if (prev_msg_row == Rows - 1)
664 msg_row--;
665 }
666 msg_col = 0;
667 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
668 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000671 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
672 {
673 if (curbuf->b_ml.ml_flags & ML_EMPTY)
674 EMSG(_(e_emptybuf));
675 else
676 EMSG(_("E501: At end-of-file"));
677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 }
679
680#ifdef FEAT_GUI
681 --hold_gui_events;
682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 --RedrawingDisabled;
684 --no_wait_return;
685 update_screen(CLEAR);
686 need_wait_return = FALSE;
687 msg_scroll = save_msg_scroll;
688}
689
690/*
691 * Execute a simple command line. Used for translated commands like "*".
692 */
693 int
694do_cmdline_cmd(cmd)
695 char_u *cmd;
696{
697 return do_cmdline(cmd, NULL, NULL,
698 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
699}
700
701/*
702 * do_cmdline(): execute one Ex command line
703 *
704 * 1. Execute "cmdline" when it is not NULL.
705 * If "cmdline" is NULL, or more lines are needed, getline() is used.
706 * 2. Split up in parts separated with '|'.
707 *
708 * This function can be called recursively!
709 *
710 * flags:
711 * DOCMD_VERBOSE - The command will be included in the error message.
712 * DOCMD_NOWAIT - Don't call wait_return() and friends.
713 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
714 * DOCMD_KEYTYPED - Don't reset KeyTyped.
715 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
716 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
717 *
718 * return FAIL if cmdline could not be executed, OK otherwise
719 */
720 int
721do_cmdline(cmdline, getline, cookie, flags)
722 char_u *cmdline;
723 char_u *(*getline) __ARGS((int, void *, int));
724 void *cookie; /* argument for getline() */
725 int flags;
726{
727 char_u *next_cmdline; /* next cmd to execute */
728 char_u *cmdline_copy = NULL; /* copy of cmd line */
729 int used_getline = FALSE; /* used "getline" to obtain command */
730 static int recursive = 0; /* recursive depth */
731 int msg_didout_before_start = 0;
732 int count = 0; /* line number count */
733 int did_inc = FALSE; /* incremented RedrawingDisabled */
734 int retval = OK;
735#ifdef FEAT_EVAL
736 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000737 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 int current_line = 0; /* active line in lines_ga */
739 char_u *fname = NULL; /* function or script name */
740 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
741 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000742 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 int initial_trylevel;
744 struct msglist **saved_msg_list = NULL;
745 struct msglist *private_msg_list;
746
747 /* "getline" and "cookie" passed to do_one_cmd() */
748 char_u *(*cmd_getline) __ARGS((int, void *, int));
749 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000750 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000752 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753#else
754# define cmd_getline getline
755# define cmd_cookie cookie
756#endif
757 static int call_depth = 0; /* recursiveness */
758
759#ifdef FEAT_EVAL
760 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
761 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000762 * This ensures that the do_errthrow() call in do_one_cmd() does not
763 * combine the messages stored by an earlier invocation of do_one_cmd()
764 * with the command name of the later one. This would happen when
765 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 saved_msg_list = msg_list;
767 msg_list = &private_msg_list;
768 private_msg_list = NULL;
769#endif
770
771 /* It's possible to create an endless loop with ":execute", catch that
772 * here. The value of 200 allows nested function calls, ":source", etc. */
773 if (call_depth == 200)
774 {
775 EMSG(_("E169: Command too recursive"));
776#ifdef FEAT_EVAL
777 /* When converting to an exception, we do not include the command name
778 * since this is not an error of the specific command. */
779 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
780 msg_list = saved_msg_list;
781#endif
782 return FAIL;
783 }
784 ++call_depth;
785
786#ifdef FEAT_EVAL
787 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000788 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 cstack.cs_trylevel = 0;
790 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000791 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
793
794 real_cookie = getline_cookie(getline, cookie);
795
796 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797 getline_is_func = getline_equal(getline, cookie, get_func_line);
798 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 ++ex_nesting_level;
800
801 /* Get the function or script name and the address where the next breakpoint
802 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {
805 fname = func_name(real_cookie);
806 breakpoint = func_breakpoint(real_cookie);
807 dbg_tick = func_dbg_tick(real_cookie);
808 }
809 else if (getline_equal(getline, cookie, getsourceline))
810 {
811 fname = sourcing_name;
812 breakpoint = source_breakpoint(real_cookie);
813 dbg_tick = source_dbg_tick(real_cookie);
814 }
815
816 /*
817 * Initialize "force_abort" and "suppress_errthrow" at the top level.
818 */
819 if (!recursive)
820 {
821 force_abort = FALSE;
822 suppress_errthrow = FALSE;
823 }
824
825 /*
826 * If requested, store and reset the global values controlling the
827 * exception handling (used when debugging).
828 */
829 else if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000830 save_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
832 initial_trylevel = trylevel;
833
834 /*
835 * "did_throw" will be set to TRUE when an exception is being thrown.
836 */
837 did_throw = FALSE;
838#endif
839 /*
840 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000841 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 * If force_abort is set, we cancel everything.
843 */
844 did_emsg = FALSE;
845
846 /*
847 * KeyTyped is only set when calling vgetc(). Reset it here when not
848 * calling vgetc() (sourced command lines).
849 */
850 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
851 KeyTyped = FALSE;
852
853 /*
854 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000855 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 * - for multiple commands on one line, separated with '|'
857 * - when repeating until there are no more lines (for ":source")
858 */
859 next_cmdline = cmdline;
860 do
861 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862#ifdef FEAT_EVAL
863 getline_is_func = getline_equal(getline, cookie, get_func_line);
864#endif
865
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000866 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if (next_cmdline == NULL
868#ifdef FEAT_EVAL
869 && !force_abort
870 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000871 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872#endif
873 )
874 did_emsg = FALSE;
875
876 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000877 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 * 2. If no line given: Get an allocated line with getline().
879 * 3. If a line is given: Make a copy, so we can mess with it.
880 */
881
882#ifdef FEAT_EVAL
883 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000884 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
886 /* Each '|' separated command is stored separately in lines_ga, to
887 * be able to jump to it. Don't use next_cmdline now. */
888 vim_free(cmdline_copy);
889 cmdline_copy = NULL;
890
891 /* Check if a function has returned or, unless it has an unclosed
892 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000893 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000895# ifdef FEAT_PROFILE
896 if (do_profiling)
897 func_line_end(real_cookie);
898# endif
899 if (func_has_ended(real_cookie))
900 {
901 retval = FAIL;
902 break;
903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000905#ifdef FEAT_PROFILE
906 else if (do_profiling
907 && getline_equal(getline, cookie, getsourceline))
908 script_line_end();
909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910
911 /* Check if a sourced file hit a ":finish" command. */
912 if (source_finished(getline, cookie))
913 {
914 retval = FAIL;
915 break;
916 }
917
918 /* If breakpoints have been added/deleted need to check for it. */
919 if (breakpoint != NULL && dbg_tick != NULL
920 && *dbg_tick != debug_tick)
921 {
922 *breakpoint = dbg_find_breakpoint(
923 getline_equal(getline, cookie, getsourceline),
924 fname, sourcing_lnum);
925 *dbg_tick = debug_tick;
926 }
927
928 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
929 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
930
931 /* Did we encounter a breakpoint? */
932 if (breakpoint != NULL && *breakpoint != 0
933 && *breakpoint <= sourcing_lnum)
934 {
935 dbg_breakpoint(fname, sourcing_lnum);
936 /* Find next breakpoint. */
937 *breakpoint = dbg_find_breakpoint(
938 getline_equal(getline, cookie, getsourceline),
939 fname, sourcing_lnum);
940 *dbg_tick = debug_tick;
941 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000942# ifdef FEAT_PROFILE
943 if (do_profiling)
944 {
945 if (getline_is_func)
946 func_line_start(real_cookie);
947 else if (getline_equal(getline, cookie, getsourceline))
948 script_line_start();
949 }
950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 }
952
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000953 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000955 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 * again. Pass a different "getline" function to do_one_cmd()
957 * below, so that it stores lines in or reads them from
958 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000959 * while/for loop. */
960 cmd_getline = get_loop_line;
961 cmd_cookie = (void *)&cmd_loop_cookie;
962 cmd_loop_cookie.lines_gap = &lines_ga;
963 cmd_loop_cookie.current_line = current_line;
964 cmd_loop_cookie.getline = getline;
965 cmd_loop_cookie.cookie = cookie;
966 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968 else
969 {
970 cmd_getline = getline;
971 cmd_cookie = cookie;
972 }
973#endif
974
975 /* 2. If no line given, get an allocated line with getline(). */
976 if (next_cmdline == NULL)
977 {
978 /*
979 * Need to set msg_didout for the first line after an ":if",
980 * otherwise the ":if" will be overwritten.
981 */
982 if (count == 1 && getline_equal(getline, cookie, getexline))
983 msg_didout = TRUE;
984 if (getline == NULL || (next_cmdline = getline(':', cookie,
985#ifdef FEAT_EVAL
986 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
987#else
988 0
989#endif
990 )) == NULL)
991 {
992 /* Don't call wait_return for aborted command line. The NULL
993 * returned for the end of a sourced file or executed function
994 * doesn't do this. */
995 if (KeyTyped && !(flags & DOCMD_REPEAT))
996 need_wait_return = FALSE;
997 retval = FAIL;
998 break;
999 }
1000 used_getline = TRUE;
1001
1002 /*
1003 * Keep the first typed line. Clear it when more lines are typed.
1004 */
1005 if (flags & DOCMD_KEEPLINE)
1006 {
1007 vim_free(repeat_cmdline);
1008 if (count == 0)
1009 repeat_cmdline = vim_strsave(next_cmdline);
1010 else
1011 repeat_cmdline = NULL;
1012 }
1013 }
1014
1015 /* 3. Make a copy of the command so we can mess with it. */
1016 else if (cmdline_copy == NULL)
1017 {
1018 next_cmdline = vim_strsave(next_cmdline);
1019 if (next_cmdline == NULL)
1020 {
1021 EMSG(_(e_outofmem));
1022 retval = FAIL;
1023 break;
1024 }
1025 }
1026 cmdline_copy = next_cmdline;
1027
1028#ifdef FEAT_EVAL
1029 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001030 * Save the current line when inside a ":while" or ":for", and when
1031 * the command looks like a ":while" or ":for", because we may need it
1032 * later. When there is a '|' and another command, it is stored
1033 * separately, because we need to be able to jump back to it from an
1034 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001036 if (current_line == lines_ga.ga_len
1037 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
1041 retval = FAIL;
1042 break;
1043 }
1044 }
1045 did_endif = FALSE;
1046#endif
1047
1048 if (count++ == 0)
1049 {
1050 /*
1051 * All output from the commands is put below each other, without
1052 * waiting for a return. Don't do this when executing commands
1053 * from a script or when being called recursive (e.g. for ":e
1054 * +command file").
1055 */
1056 if (!(flags & DOCMD_NOWAIT) && !recursive)
1057 {
1058 msg_didout_before_start = msg_didout;
1059 msg_didany = FALSE; /* no output yet */
1060 msg_start();
1061 msg_scroll = TRUE; /* put messages below each other */
1062 ++no_wait_return; /* dont wait for return until finished */
1063 ++RedrawingDisabled;
1064 did_inc = TRUE;
1065 }
1066 }
1067
1068 if (p_verbose >= 15 && sourcing_name != NULL)
1069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001071 verbose_enter_scroll();
1072
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 smsg((char_u *)_("line %ld: %s"),
1074 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001075 if (msg_silent == 0)
1076 msg_puts((char_u *)"\n"); /* don't overwrite this */
1077
1078 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 --no_wait_return;
1080 }
1081
1082 /*
1083 * 2. Execute one '|' separated command.
1084 * do_one_cmd() will return NULL if there is no trailing '|'.
1085 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1086 */
1087 ++recursive;
1088 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1089#ifdef FEAT_EVAL
1090 &cstack,
1091#endif
1092 cmd_getline, cmd_cookie);
1093 --recursive;
1094
1095#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001096 if (cmd_cookie == (void *)&cmd_loop_cookie)
1097 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001099 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100#endif
1101
1102 if (next_cmdline == NULL)
1103 {
1104 vim_free(cmdline_copy);
1105 cmdline_copy = NULL;
1106#ifdef FEAT_CMDHIST
1107 /*
1108 * If the command was typed, remember it for the ':' register.
1109 * Do this AFTER executing the command to make :@: work.
1110 */
1111 if (getline_equal(getline, cookie, getexline)
1112 && new_last_cmdline != NULL)
1113 {
1114 vim_free(last_cmdline);
1115 last_cmdline = new_last_cmdline;
1116 new_last_cmdline = NULL;
1117 }
1118#endif
1119 }
1120 else
1121 {
1122 /* need to copy the command after the '|' to cmdline_copy, for the
1123 * next do_one_cmd() */
1124 mch_memmove(cmdline_copy, next_cmdline, STRLEN(next_cmdline) + 1);
1125 next_cmdline = cmdline_copy;
1126 }
1127
1128
1129#ifdef FEAT_EVAL
1130 /* reset did_emsg for a function that is not aborted by an error */
1131 if (did_emsg && !force_abort
1132 && getline_equal(getline, cookie, get_func_line)
1133 && !func_has_abort(real_cookie))
1134 did_emsg = FALSE;
1135
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001136 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
1138 ++current_line;
1139
1140 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001141 * An ":endwhile", ":endfor" and ":continue" is handled here.
1142 * If we were executing commands, jump back to the ":while" or
1143 * ":for".
1144 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001146 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001148 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001150 /* Jump back to the matching ":while" or ":for". Be careful
1151 * not to use a cs_line[] from an entry that isn't a ":while"
1152 * or ":for": It would make "current_line" invalid and can
1153 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 if (!did_emsg && !got_int && !did_throw
1155 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001156 && (cstack.cs_flags[cstack.cs_idx]
1157 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 && cstack.cs_line[cstack.cs_idx] >= 0
1159 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1160 {
1161 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 /* remember we jumped there */
1163 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 line_breakcheck(); /* check if CTRL-C typed */
1165
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001166 /* Check for the next breakpoint at or after the ":while"
1167 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 if (breakpoint != NULL)
1169 {
1170 *breakpoint = dbg_find_breakpoint(
1171 getline_equal(getline, cookie, getsourceline),
1172 fname,
1173 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1174 *dbg_tick = debug_tick;
1175 }
1176 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001177 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001179 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001181 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1182 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
1184 }
1185
1186 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001187 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001189 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001191 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1193 }
1194 }
1195
1196 /*
1197 * When not inside any ":while" loop, clear remembered lines.
1198 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001199 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {
1201 if (lines_ga.ga_len > 0)
1202 {
1203 sourcing_lnum =
1204 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1205 free_cmdlines(&lines_ga);
1206 }
1207 current_line = 0;
1208 }
1209
1210 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1212 * being restored at the ":endtry". Reset them here and set the
1213 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1214 * This includes the case where a missing ":endif", ":endwhile" or
1215 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001217 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001219 cstack.cs_lflags &= ~CSL_HAD_FINA;
1220 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1221 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 did_throw ? (void *)current_exception : NULL);
1223 did_emsg = got_int = did_throw = FALSE;
1224 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1225 }
1226
1227 /* Update global "trylevel" for recursive calls to do_cmdline() from
1228 * within this loop. */
1229 trylevel = initial_trylevel + cstack.cs_trylevel;
1230
1231 /*
1232 * If the outermost try conditional (accross function calls and sourced
1233 * files) is aborted because of an error, an interrupt, or an uncaught
1234 * exception, cancel everything. If it is left normally, reset
1235 * force_abort to get the non-EH compatible abortion behavior for
1236 * the rest of the script.
1237 */
1238 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1239 force_abort = FALSE;
1240
1241 /* Convert an interrupt to an exception if appropriate. */
1242 (void)do_intthrow(&cstack);
1243#endif /* FEAT_EVAL */
1244
1245 }
1246 /*
1247 * Continue executing command lines when:
1248 * - no CTRL-C typed, no aborting error, no exception thrown or try
1249 * conditionals need to be checked for executing finally clauses or
1250 * catching an interrupt exception
1251 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001252 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 * looping for ":source" command or function call.
1254 */
1255 while (!((got_int
1256#ifdef FEAT_EVAL
1257 || (did_emsg && force_abort) || did_throw
1258#endif
1259 )
1260#ifdef FEAT_EVAL
1261 && cstack.cs_trylevel == 0
1262#endif
1263 )
1264 && !(did_emsg && used_getline
1265 && (getline_equal(getline, cookie, getexmodeline)
1266 || getline_equal(getline, cookie, getexline)))
1267 && (next_cmdline != NULL
1268#ifdef FEAT_EVAL
1269 || cstack.cs_idx >= 0
1270#endif
1271 || (flags & DOCMD_REPEAT)));
1272
1273 vim_free(cmdline_copy);
1274#ifdef FEAT_EVAL
1275 free_cmdlines(&lines_ga);
1276 ga_clear(&lines_ga);
1277
1278 if (cstack.cs_idx >= 0)
1279 {
1280 /*
1281 * If a sourced file or executed function ran to its end, report the
1282 * unclosed conditional.
1283 */
1284 if (!got_int && !did_throw
1285 && ((getline_equal(getline, cookie, getsourceline)
1286 && !source_finished(getline, cookie))
1287 || (getline_equal(getline, cookie, get_func_line)
1288 && !func_has_ended(real_cookie))))
1289 {
1290 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1291 EMSG(_(e_endtry));
1292 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1293 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001294 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1295 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 else
1297 EMSG(_(e_endif));
1298 }
1299
1300 /*
1301 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1302 * ":endtry" in a sourced file or executed function. If the try
1303 * conditional is in its finally clause, ignore anything pending.
1304 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001305 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 */
1307 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001308 {
1309 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1310
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001311 if (idx >= 0)
1312 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001313 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1314 &cstack.cs_looplevel);
1315 }
1316 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 trylevel = initial_trylevel;
1318 }
1319
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001320 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1321 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 * exception, do this now after rewinding the cstack. */
1323 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1324 ? (char_u *)"endfunction" : (char_u *)NULL);
1325
1326 if (trylevel == 0)
1327 {
1328 /*
1329 * When an exception is being thrown out of the outermost try
1330 * conditional, discard the uncaught exception, disable the conversion
1331 * of interrupts or errors to exceptions, and ensure that no more
1332 * commands are executed.
1333 */
1334 if (did_throw)
1335 {
1336 void *p = NULL;
1337 char_u *saved_sourcing_name;
1338 int saved_sourcing_lnum;
1339 struct msglist *messages = NULL, *next;
1340
1341 /*
1342 * If the uncaught exception is a user exception, report it as an
1343 * error. If it is an error exception, display the saved error
1344 * message now. For an interrupt exception, do nothing; the
1345 * interrupt message is given elsewhere.
1346 */
1347 switch (current_exception->type)
1348 {
1349 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001350 vim_snprintf((char *)IObuff, IOSIZE,
1351 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 current_exception->value);
1353 p = vim_strsave(IObuff);
1354 break;
1355 case ET_ERROR:
1356 messages = current_exception->messages;
1357 current_exception->messages = NULL;
1358 break;
1359 case ET_INTERRUPT:
1360 break;
1361 default:
1362 p = vim_strsave((char_u *)_(e_internal));
1363 }
1364
1365 saved_sourcing_name = sourcing_name;
1366 saved_sourcing_lnum = sourcing_lnum;
1367 sourcing_name = current_exception->throw_name;
1368 sourcing_lnum = current_exception->throw_lnum;
1369 current_exception->throw_name = NULL;
1370
1371 discard_current_exception(); /* uses IObuff if 'verbose' */
1372 suppress_errthrow = TRUE;
1373 force_abort = TRUE;
1374
1375 if (messages != NULL)
1376 {
1377 do
1378 {
1379 next = messages->next;
1380 emsg(messages->msg);
1381 vim_free(messages->msg);
1382 vim_free(messages);
1383 messages = next;
1384 }
1385 while (messages != NULL);
1386 }
1387 else if (p != NULL)
1388 {
1389 emsg(p);
1390 vim_free(p);
1391 }
1392 vim_free(sourcing_name);
1393 sourcing_name = saved_sourcing_name;
1394 sourcing_lnum = saved_sourcing_lnum;
1395 }
1396
1397 /*
1398 * On an interrupt or an aborting error not converted to an exception,
1399 * disable the conversion of errors to exceptions. (Interrupts are not
1400 * converted any more, here.) This enables also the interrupt message
1401 * when force_abort is set and did_emsg unset in case of an interrupt
1402 * from a finally clause after an error.
1403 */
1404 else if (got_int || (did_emsg && force_abort))
1405 suppress_errthrow = TRUE;
1406 }
1407
1408 /*
1409 * The current cstack will be freed when do_cmdline() returns. An uncaught
1410 * exception will have to be rethrown in the previous cstack. If a function
1411 * has just returned or a script file was just finished and the previous
1412 * cstack belongs to the same function or, respectively, script file, it
1413 * will have to be checked for finally clauses to be executed due to the
1414 * ":return" or ":finish". This is done in do_one_cmd().
1415 */
1416 if (did_throw)
1417 need_rethrow = TRUE;
1418 if ((getline_equal(getline, cookie, getsourceline)
1419 && ex_nesting_level > source_level(real_cookie))
1420 || (getline_equal(getline, cookie, get_func_line)
1421 && ex_nesting_level > func_level(real_cookie) + 1))
1422 {
1423 if (!did_throw)
1424 check_cstack = TRUE;
1425 }
1426 else
1427 {
1428 /* When leaving a function, reduce nesting level. */
1429 if (getline_equal(getline, cookie, get_func_line))
1430 --ex_nesting_level;
1431 /*
1432 * Go to debug mode when returning from a function in which we are
1433 * single-stepping.
1434 */
1435 if ((getline_equal(getline, cookie, getsourceline)
1436 || getline_equal(getline, cookie, get_func_line))
1437 && ex_nesting_level + 1 <= debug_break_level)
1438 do_debug(getline_equal(getline, cookie, getsourceline)
1439 ? (char_u *)_("End of sourced file")
1440 : (char_u *)_("End of function"));
1441 }
1442
1443 /*
1444 * Restore the exception environment (done after returning from the
1445 * debugger).
1446 */
1447 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001448 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449
1450 msg_list = saved_msg_list;
1451#endif /* FEAT_EVAL */
1452
1453 /*
1454 * If there was too much output to fit on the command line, ask the user to
1455 * hit return before redrawing the screen. With the ":global" command we do
1456 * this only once after the command is finished.
1457 */
1458 if (did_inc)
1459 {
1460 --RedrawingDisabled;
1461 --no_wait_return;
1462 msg_scroll = FALSE;
1463
1464 /*
1465 * When just finished an ":if"-":else" which was typed, no need to
1466 * wait for hit-return. Also for an error situation.
1467 */
1468 if (retval == FAIL
1469#ifdef FEAT_EVAL
1470 || (did_endif && KeyTyped && !did_emsg)
1471#endif
1472 )
1473 {
1474 need_wait_return = FALSE;
1475 msg_didany = FALSE; /* don't wait when restarting edit */
1476 }
1477 else if (need_wait_return)
1478 {
1479 /*
1480 * The msg_start() above clears msg_didout. The wait_return we do
1481 * here should not overwrite the command that may be shown before
1482 * doing that.
1483 */
1484 msg_didout |= msg_didout_before_start;
1485 wait_return(FALSE);
1486 }
1487 }
1488
1489#ifndef FEAT_EVAL
1490 /*
1491 * Reset if_level, in case a sourced script file contains more ":if" than
1492 * ":endif" (could be ":if x | foo | endif").
1493 */
1494 if_level = 0;
1495#endif
1496
1497 --call_depth;
1498 return retval;
1499}
1500
1501#ifdef FEAT_EVAL
1502/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001503 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 */
1505 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001506get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 int c;
1508 void *cookie;
1509 int indent;
1510{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001511 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 wcmd_T *wp;
1513 char_u *line;
1514
1515 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1516 {
1517 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001518 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001520 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 if (cp->getline == NULL)
1522 line = getcmdline(c, 0L, indent);
1523 else
1524 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001525 if (store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 ++cp->current_line;
1527
1528 return line;
1529 }
1530
1531 KeyTyped = FALSE;
1532 ++cp->current_line;
1533 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1534 sourcing_lnum = wp->lnum;
1535 return vim_strsave(wp->line);
1536}
1537
1538/*
1539 * Store a line in "gap" so that a ":while" loop can execute it again.
1540 */
1541 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001542store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 garray_T *gap;
1544 char_u *line;
1545{
1546 if (ga_grow(gap, 1) == FAIL)
1547 return FAIL;
1548 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1549 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1550 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 return OK;
1552}
1553
1554/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 */
1557 static void
1558free_cmdlines(gap)
1559 garray_T *gap;
1560{
1561 while (gap->ga_len > 0)
1562 {
1563 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1564 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 }
1566}
1567#endif
1568
1569/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001570 * If "getline" is get_loop_line(), return TRUE if the getline it uses equals
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 * "func". * Otherwise return TRUE when "getline" equals "func".
1572 */
1573/*ARGSUSED*/
1574 int
1575getline_equal(getline, cookie, func)
1576 char_u *(*getline) __ARGS((int, void *, int));
1577 void *cookie; /* argument for getline() */
1578 char_u *(*func) __ARGS((int, void *, int));
1579{
1580#ifdef FEAT_EVAL
1581 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001582 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001584 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 * function that's orignally used to obtain the lines. This may be nested
1586 * several levels. */
1587 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001588 cp = (struct loop_cookie *)cookie;
1589 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 {
1591 gp = cp->getline;
1592 cp = cp->cookie;
1593 }
1594 return gp == func;
1595#else
1596 return getline == func;
1597#endif
1598}
1599
1600#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1601/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001602 * If "getline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 * getline function. Otherwise return "cookie".
1604 */
1605/*ARGSUSED*/
1606 void *
1607getline_cookie(getline, cookie)
1608 char_u *(*getline) __ARGS((int, void *, int));
1609 void *cookie; /* argument for getline() */
1610{
1611# ifdef FEAT_EVAL
1612 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001613 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001615 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 * cookie that's orignally used to obtain the lines. This may be nested
1617 * several levels. */
1618 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001619 cp = (struct loop_cookie *)cookie;
1620 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
1622 gp = cp->getline;
1623 cp = cp->cookie;
1624 }
1625 return cp;
1626# else
1627 return cookie;
1628# endif
1629}
1630#endif
1631
1632/*
1633 * Execute one Ex command.
1634 *
1635 * If 'sourcing' is TRUE, the command will be included in the error message.
1636 *
1637 * 1. skip comment lines and leading space
1638 * 2. handle command modifiers
1639 * 3. parse range
1640 * 4. parse command
1641 * 5. parse arguments
1642 * 6. switch on command name
1643 *
1644 * Note: "getline" can be NULL.
1645 *
1646 * This function may be called recursively!
1647 */
1648#if (_MSC_VER == 1200)
1649/*
Bram Moolenaared203462004-06-16 11:19:22 +00001650 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001652 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653#endif
1654 static char_u *
1655do_one_cmd(cmdlinep, sourcing,
1656#ifdef FEAT_EVAL
1657 cstack,
1658#endif
1659 getline, cookie)
1660 char_u **cmdlinep;
1661 int sourcing;
1662#ifdef FEAT_EVAL
1663 struct condstack *cstack;
1664#endif
1665 char_u *(*getline) __ARGS((int, void *, int));
1666 void *cookie; /* argument for getline() */
1667{
1668 char_u *p;
1669 linenr_T lnum;
1670 long n;
1671 char_u *errormsg = NULL; /* error message */
1672 exarg_T ea; /* Ex command arguments */
1673 long verbose_save = -1;
1674 int save_msg_scroll = 0;
1675 int did_silent = 0;
1676 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001677#ifdef HAVE_SANDBOX
1678 int did_sandbox = FALSE;
1679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 cmdmod_T save_cmdmod;
1681 int ni; /* set when Not Implemented */
1682
1683 vim_memset(&ea, 0, sizeof(ea));
1684 ea.line1 = 1;
1685 ea.line2 = 1;
1686#ifdef FEAT_EVAL
1687 ++ex_nesting_level;
1688#endif
1689
1690 /* when not editing the last file :q has to be typed twice */
1691 if (quitmore
1692#ifdef FEAT_EVAL
1693 /* avoid that a function call in 'statusline' does this */
1694 && !getline_equal(getline, cookie, get_func_line)
1695#endif
1696 )
1697 --quitmore;
1698
1699 /*
1700 * Reset browse, confirm, etc.. They are restored when returning, for
1701 * recursive calls.
1702 */
1703 save_cmdmod = cmdmod;
1704 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1705
1706 /*
1707 * Repeat until no more command modifiers are found.
1708 */
1709 ea.cmd = *cmdlinep;
1710 for (;;)
1711 {
1712/*
1713 * 1. skip comment lines and leading white space and colons
1714 */
1715 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1716 ++ea.cmd;
1717
1718 /* in ex mode, an empty line works like :+ */
1719 if (*ea.cmd == NUL && exmode_active
1720 && (getline_equal(getline, cookie, getexmodeline)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001721 || getline_equal(getline, cookie, getexline))
1722 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 {
1724 ea.cmd = (char_u *)"+";
1725 ex_pressedreturn = TRUE;
1726 }
1727
1728 /* ignore comment and empty lines */
1729 if (*ea.cmd == '"' || *ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001730 {
1731 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734
1735/*
1736 * 2. handle command modifiers.
1737 */
1738 p = ea.cmd;
1739 if (VIM_ISDIGIT(*ea.cmd))
1740 p = skipwhite(skipdigits(ea.cmd));
1741 switch (*p)
1742 {
1743 /* When adding an entry, also modify cmd_exists(). */
1744 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1745 break;
1746#ifdef FEAT_WINDOWS
1747 cmdmod.split |= WSP_ABOVE;
1748#endif
1749 continue;
1750
1751 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1752 {
1753#ifdef FEAT_WINDOWS
1754 cmdmod.split |= WSP_BELOW;
1755#endif
1756 continue;
1757 }
1758 if (checkforcmd(&ea.cmd, "browse", 3))
1759 {
1760#ifdef FEAT_BROWSE
1761 cmdmod.browse = TRUE;
1762#endif
1763 continue;
1764 }
1765 if (!checkforcmd(&ea.cmd, "botright", 2))
1766 break;
1767#ifdef FEAT_WINDOWS
1768 cmdmod.split |= WSP_BOT;
1769#endif
1770 continue;
1771
1772 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1773 break;
1774#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1775 cmdmod.confirm = TRUE;
1776#endif
1777 continue;
1778
1779 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1780 {
1781 cmdmod.keepmarks = TRUE;
1782 continue;
1783 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001784 if (checkforcmd(&ea.cmd, "keepalt", 5))
1785 {
1786 cmdmod.keepalt = TRUE;
1787 continue;
1788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1790 break;
1791 cmdmod.keepjumps = TRUE;
1792 continue;
1793
1794 /* ":hide" and ":hide | cmd" are not modifiers */
1795 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1796 || *p == NUL || ends_excmd(*p))
1797 break;
1798 ea.cmd = p;
1799 cmdmod.hide = TRUE;
1800 continue;
1801
1802 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1803 {
1804 cmdmod.lockmarks = TRUE;
1805 continue;
1806 }
1807
1808 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1809 break;
1810#ifdef FEAT_WINDOWS
1811 cmdmod.split |= WSP_ABOVE;
1812#endif
1813 continue;
1814
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001815 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1816 break;
1817#ifdef FEAT_AUTOCMD
1818 if (cmdmod.save_ei == NULL)
1819 {
1820 /* Set 'eventignore' to "all". Don't free the
1821 * existing option value, we restore it later. */
1822 cmdmod.save_ei = vim_strsave(p_ei);
1823 set_string_option_direct((char_u *)"ei", -1,
1824 (char_u *)"all", OPT_FREE);
1825 }
1826#endif
1827 continue;
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1830 break;
1831#ifdef FEAT_WINDOWS
1832 cmdmod.split |= WSP_BELOW;
1833#endif
1834 continue;
1835
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001836 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1837 {
1838#ifdef HAVE_SANDBOX
1839 if (!did_sandbox)
1840 ++sandbox;
1841 did_sandbox = TRUE;
1842#endif
1843 continue;
1844 }
1845 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 break;
1847 ++did_silent;
1848 ++msg_silent;
1849 save_msg_scroll = msg_scroll;
1850 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1851 {
1852 /* ":silent!", but not "silent !cmd" */
1853 ea.cmd = skipwhite(ea.cmd + 1);
1854 ++emsg_silent;
1855 ++did_esilent;
1856 }
1857 continue;
1858
1859 case 't': if (!checkforcmd(&ea.cmd, "topleft", 2))
1860 break;
1861#ifdef FEAT_WINDOWS
1862 cmdmod.split |= WSP_TOP;
1863#endif
1864 continue;
1865
1866 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1867 {
1868#ifdef FEAT_VERTSPLIT
1869 cmdmod.split |= WSP_VERT;
1870#endif
1871 continue;
1872 }
1873 if (!checkforcmd(&p, "verbose", 4))
1874 break;
1875 if (verbose_save < 0)
1876 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001877 if (vim_isdigit(*ea.cmd))
1878 p_verbose = atoi((char *)ea.cmd);
1879 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 p_verbose = 1;
1881 ea.cmd = p;
1882 continue;
1883 }
1884 break;
1885 }
1886
1887#ifdef FEAT_EVAL
1888 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1889 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1890#else
1891 ea.skip = (if_level > 0);
1892#endif
1893
1894#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001895# ifdef FEAT_PROFILE
1896 /* Count this line for profiling if ea.skip is FALSE. */
1897 if (do_profiling && !ea.skip)
1898 {
1899 if (getline_equal(getline, cookie, get_func_line))
1900 func_line_exec(getline_cookie(getline, cookie));
1901 else if (getline_equal(getline, cookie, getsourceline))
1902 script_line_exec();
1903 }
1904#endif
1905
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 /* May go to debug mode. If this happens and the ">quit" debug command is
1907 * used, throw an interrupt exception and skip the next command. */
1908 dbg_check_breakpoint(&ea);
1909 if (!ea.skip && got_int)
1910 {
1911 ea.skip = TRUE;
1912 (void)do_intthrow(cstack);
1913 }
1914#endif
1915
1916/*
1917 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1918 *
1919 * where 'addr' is:
1920 *
1921 * % (entire file)
1922 * $ [+-NUM]
1923 * 'x [+-NUM] (where x denotes a currently defined mark)
1924 * . [+-NUM]
1925 * [+-NUM]..
1926 * NUM
1927 *
1928 * The ea.cmd pointer is updated to point to the first character following the
1929 * range spec. If an initial address is found, but no second, the upper bound
1930 * is equal to the lower.
1931 */
1932
1933 /* repeat for all ',' or ';' separated addresses */
1934 for (;;)
1935 {
1936 ea.line1 = ea.line2;
1937 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1938 ea.cmd = skipwhite(ea.cmd);
1939 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1940 if (ea.cmd == NULL) /* error detected */
1941 goto doend;
1942 if (lnum == MAXLNUM)
1943 {
1944 if (*ea.cmd == '%') /* '%' - all lines */
1945 {
1946 ++ea.cmd;
1947 ea.line1 = 1;
1948 ea.line2 = curbuf->b_ml.ml_line_count;
1949 ++ea.addr_count;
1950 }
1951 /* '*' - visual area */
1952 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1953 {
1954 pos_T *fp;
1955
1956 ++ea.cmd;
1957 if (!ea.skip)
1958 {
1959 fp = getmark('<', FALSE);
1960 if (check_mark(fp) == FAIL)
1961 goto doend;
1962 ea.line1 = fp->lnum;
1963 fp = getmark('>', FALSE);
1964 if (check_mark(fp) == FAIL)
1965 goto doend;
1966 ea.line2 = fp->lnum;
1967 ++ea.addr_count;
1968 }
1969 }
1970 }
1971 else
1972 ea.line2 = lnum;
1973 ea.addr_count++;
1974
1975 if (*ea.cmd == ';')
1976 {
1977 if (!ea.skip)
1978 curwin->w_cursor.lnum = ea.line2;
1979 }
1980 else if (*ea.cmd != ',')
1981 break;
1982 ++ea.cmd;
1983 }
1984
1985 /* One address given: set start and end lines */
1986 if (ea.addr_count == 1)
1987 {
1988 ea.line1 = ea.line2;
1989 /* ... but only implicit: really no address given */
1990 if (lnum == MAXLNUM)
1991 ea.addr_count = 0;
1992 }
1993
1994 /* Don't leave the cursor on an illegal line (caused by ';') */
1995 check_cursor_lnum();
1996
1997/*
1998 * 4. parse command
1999 */
2000
2001 /*
2002 * Skip ':' and any white space
2003 */
2004 ea.cmd = skipwhite(ea.cmd);
2005 while (*ea.cmd == ':')
2006 ea.cmd = skipwhite(ea.cmd + 1);
2007
2008 /*
2009 * If we got a line, but no command, then go to the line.
2010 * If we find a '|' or '\n' we set ea.nextcmd.
2011 */
2012 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2013 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2014 {
2015 /*
2016 * strange vi behaviour:
2017 * ":3" jumps to line 3
2018 * ":3|..." prints line 3
2019 * ":|" prints current line
2020 */
2021 if (ea.skip) /* skip this if inside :if */
2022 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002023 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 {
2025 ea.cmdidx = CMD_print;
2026 ea.argt = RANGE+COUNT+TRLBAR;
2027 if ((errormsg = invalid_range(&ea)) == NULL)
2028 {
2029 correct_range(&ea);
2030 ex_print(&ea);
2031 }
2032 }
2033 else if (ea.addr_count != 0)
2034 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002035 if (ea.line2 > curbuf->b_ml.ml_line_count)
2036 {
2037 /* With '-' in 'cpoptions' a line number past the file is an
2038 * error, otherwise put it at the end of the file. */
2039 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2040 ea.line2 = -1;
2041 else
2042 ea.line2 = curbuf->b_ml.ml_line_count;
2043 }
2044
2045 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002046 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 else
2048 {
2049 if (ea.line2 == 0)
2050 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 else
2052 curwin->w_cursor.lnum = ea.line2;
2053 beginline(BL_SOL | BL_FIX);
2054 }
2055 }
2056 goto doend;
2057 }
2058
2059 /* Find the command and let "p" point to after it. */
2060 p = find_command(&ea, NULL);
2061
2062#ifdef FEAT_USR_CMDS
2063 if (p == NULL)
2064 {
2065 if (!ea.skip)
2066 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2067 goto doend;
2068 }
2069 /* Check for wrong commands. */
2070 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2071 {
2072 errormsg = uc_fun_cmd();
2073 goto doend;
2074 }
2075#endif
2076 if (ea.cmdidx == CMD_SIZE)
2077 {
2078 if (!ea.skip)
2079 {
2080 STRCPY(IObuff, _("E492: Not an editor command"));
2081 if (!sourcing)
2082 {
2083 STRCAT(IObuff, ": ");
2084 STRNCAT(IObuff, *cmdlinep, 40);
2085 }
2086 errormsg = IObuff;
2087 }
2088 goto doend;
2089 }
2090
2091 ni = (
2092#ifdef FEAT_USR_CMDS
2093 !USER_CMDIDX(ea.cmdidx) &&
2094#endif
2095 cmdnames[ea.cmdidx].cmd_func == ex_ni);
2096
2097#ifndef FEAT_EVAL
2098 /*
2099 * When the expression evaluation is disabled, recognize the ":if" and
2100 * ":endif" commands and ignore everything in between it.
2101 */
2102 if (ea.cmdidx == CMD_if)
2103 ++if_level;
2104 if (if_level)
2105 {
2106 if (ea.cmdidx == CMD_endif)
2107 --if_level;
2108 goto doend;
2109 }
2110
2111#endif
2112
2113 if (*p == '!' && ea.cmdidx != CMD_substitute) /* forced commands */
2114 {
2115 ++p;
2116 ea.forceit = TRUE;
2117 }
2118 else
2119 ea.forceit = FALSE;
2120
2121/*
2122 * 5. parse arguments
2123 */
2124#ifdef FEAT_USR_CMDS
2125 if (!USER_CMDIDX(ea.cmdidx))
2126#endif
2127 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
2128
2129 if (!ea.skip)
2130 {
2131#ifdef HAVE_SANDBOX
2132 if (sandbox != 0 && !(ea.argt & SBOXOK))
2133 {
2134 /* Command not allowed in sandbox. */
2135 errormsg = (char_u *)_(e_sandbox);
2136 goto doend;
2137 }
2138#endif
2139 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2140 {
2141 /* Command not allowed in non-'modifiable' buffer */
2142 errormsg = (char_u *)_(e_modifiable);
2143 goto doend;
2144 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002145
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002146 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147# ifdef FEAT_USR_CMDS
2148 && !USER_CMDIDX(ea.cmdidx)
2149# endif
2150 )
2151 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002152 /* Command not allowed when editing the command line. */
2153#ifdef FEAT_CMDWIN
2154 if (cmdwin_type != 0)
2155 errormsg = (char_u *)_(e_cmdwin);
2156 else
2157#endif
2158 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 goto doend;
2160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161
2162 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2163 {
2164 /* no range allowed */
2165 errormsg = (char_u *)_(e_norange);
2166 goto doend;
2167 }
2168 }
2169
2170 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2171 {
2172 errormsg = (char_u *)_(e_nobang);
2173 goto doend;
2174 }
2175
2176 /*
2177 * Don't complain about the range if it is not used
2178 * (could happen if line_count is accidentally set to 0).
2179 */
2180 if (!ea.skip && !ni)
2181 {
2182 /*
2183 * If the range is backwards, ask for confirmation and, if given, swap
2184 * ea.line1 & ea.line2 so it's forwards again.
2185 * When global command is busy, don't ask, will fail below.
2186 */
2187 if (!global_busy && ea.line1 > ea.line2)
2188 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002189 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002191 if (sourcing || exmode_active)
2192 {
2193 errormsg = (char_u *)_("E493: Backwards range given");
2194 goto doend;
2195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 if (ask_yesno((char_u *)
2197 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002198 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 }
2200 lnum = ea.line1;
2201 ea.line1 = ea.line2;
2202 ea.line2 = lnum;
2203 }
2204 if ((errormsg = invalid_range(&ea)) != NULL)
2205 goto doend;
2206 }
2207
2208 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2209 ea.line2 = 1;
2210
2211 correct_range(&ea);
2212
2213#ifdef FEAT_FOLDING
2214 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2215 {
2216 /* Put the first line at the start of a closed fold, put the last line
2217 * at the end of a closed fold. */
2218 (void)hasFolding(ea.line1, &ea.line1, NULL);
2219 (void)hasFolding(ea.line2, NULL, &ea.line2);
2220 }
2221#endif
2222
2223#ifdef FEAT_QUICKFIX
2224 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002225 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 * option here, so things like % get expanded.
2227 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002228 p = replace_makeprg(&ea, p, cmdlinep);
2229 if (p == NULL)
2230 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231#endif
2232
2233 /*
2234 * Skip to start of argument.
2235 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2236 */
2237 if (ea.cmdidx == CMD_bang)
2238 ea.arg = p;
2239 else
2240 ea.arg = skipwhite(p);
2241
2242 /*
2243 * Check for "++opt=val" argument.
2244 * Must be first, allow ":w ++enc=utf8 !cmd"
2245 */
2246 if (ea.argt & ARGOPT)
2247 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2248 if (getargopt(&ea) == FAIL && !ni)
2249 {
2250 errormsg = (char_u *)_(e_invarg);
2251 goto doend;
2252 }
2253
2254 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2255 {
2256 if (*ea.arg == '>') /* append */
2257 {
2258 if (*++ea.arg != '>') /* typed wrong */
2259 {
2260 errormsg = (char_u *)_("E494: Use w or w>>");
2261 goto doend;
2262 }
2263 ea.arg = skipwhite(ea.arg + 1);
2264 ea.append = TRUE;
2265 }
2266 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2267 {
2268 ++ea.arg;
2269 ea.usefilter = TRUE;
2270 }
2271 }
2272
2273 if (ea.cmdidx == CMD_read)
2274 {
2275 if (ea.forceit)
2276 {
2277 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2278 ea.forceit = FALSE;
2279 }
2280 else if (*ea.arg == '!') /* :r !filter */
2281 {
2282 ++ea.arg;
2283 ea.usefilter = TRUE;
2284 }
2285 }
2286
2287 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2288 {
2289 ea.amount = 1;
2290 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2291 {
2292 ++ea.arg;
2293 ++ea.amount;
2294 }
2295 ea.arg = skipwhite(ea.arg);
2296 }
2297
2298 /*
2299 * Check for "+command" argument, before checking for next command.
2300 * Don't do this for ":read !cmd" and ":write !cmd".
2301 */
2302 if ((ea.argt & EDITCMD) && !ea.usefilter)
2303 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2304
2305 /*
2306 * Check for '|' to separate commands and '"' to start comments.
2307 * Don't do this for ":read !cmd" and ":write !cmd".
2308 */
2309 if ((ea.argt & TRLBAR) && !ea.usefilter)
2310 separate_nextcmd(&ea);
2311
2312 /*
2313 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002314 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2315 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002317 else if (ea.cmdidx == CMD_bang
2318 || ea.cmdidx == CMD_global
2319 || ea.cmdidx == CMD_vglobal
2320 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
2322 for (p = ea.arg; *p; ++p)
2323 {
2324 /* Remove one backslash before a newline, so that it's possible to
2325 * pass a newline to the shell and also a newline that is preceded
2326 * with a backslash. This makes it impossible to end a shell
2327 * command in a backslash, but that doesn't appear useful.
2328 * Halving the number of backslashes is incompatible with previous
2329 * versions. */
2330 if (*p == '\\' && p[1] == '\n')
2331 mch_memmove(p, p + 1, STRLEN(p));
2332 else if (*p == '\n')
2333 {
2334 ea.nextcmd = p + 1;
2335 *p = NUL;
2336 break;
2337 }
2338 }
2339 }
2340
2341 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2342 {
2343 ea.line1 = 1;
2344 ea.line2 = curbuf->b_ml.ml_line_count;
2345 }
2346
2347 /* accept numbered register only when no count allowed (:put) */
2348 if ( (ea.argt & REGSTR)
2349 && *ea.arg != NUL
2350#ifdef FEAT_USR_CMDS
2351 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2352 && USER_CMDIDX(ea.cmdidx)))
2353 /* Do not allow register = for user commands */
2354 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2355#else
2356 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2357#endif
2358 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2359 {
2360 ea.regname = *ea.arg++;
2361#ifdef FEAT_EVAL
2362 /* for '=' register: accept the rest of the line as an expression */
2363 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2364 {
2365 set_expr_line(vim_strsave(ea.arg));
2366 ea.arg += STRLEN(ea.arg);
2367 }
2368#endif
2369 ea.arg = skipwhite(ea.arg);
2370 }
2371
2372 /*
2373 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2374 * count, it's a buffer name.
2375 */
2376 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2377 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2378 || vim_iswhite(*p)))
2379 {
2380 n = getdigits(&ea.arg);
2381 ea.arg = skipwhite(ea.arg);
2382 if (n <= 0 && !ni)
2383 {
2384 errormsg = (char_u *)_(e_zerocount);
2385 goto doend;
2386 }
2387 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2388 {
2389 ea.line2 = n;
2390 if (ea.addr_count == 0)
2391 ea.addr_count = 1;
2392 }
2393 else
2394 {
2395 ea.line1 = ea.line2;
2396 ea.line2 += n - 1;
2397 ++ea.addr_count;
2398 /*
2399 * Be vi compatible: no error message for out of range.
2400 */
2401 if (ea.line2 > curbuf->b_ml.ml_line_count)
2402 ea.line2 = curbuf->b_ml.ml_line_count;
2403 }
2404 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002405
2406 /*
2407 * Check for flags: 'l', 'p' and '#'.
2408 */
2409 if (ea.argt & EXFLAGS)
2410 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002412 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
2413 && vim_strchr((char_u *)"|\"", *ea.arg) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
2415 errormsg = (char_u *)_(e_trailing);
2416 goto doend;
2417 }
2418
2419 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2420 {
2421 errormsg = (char_u *)_(e_argreq);
2422 goto doend;
2423 }
2424
2425#ifdef FEAT_EVAL
2426 /*
2427 * Skip the command when it's not going to be executed.
2428 * The commands like :if, :endif, etc. always need to be executed.
2429 * Also make an exception for commands that handle a trailing command
2430 * themselves.
2431 */
2432 if (ea.skip)
2433 {
2434 switch (ea.cmdidx)
2435 {
2436 /* commands that need evaluation */
2437 case CMD_while:
2438 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002439 case CMD_for:
2440 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 case CMD_if:
2442 case CMD_elseif:
2443 case CMD_else:
2444 case CMD_endif:
2445 case CMD_try:
2446 case CMD_catch:
2447 case CMD_finally:
2448 case CMD_endtry:
2449 case CMD_function:
2450 break;
2451
2452 /* Commands that handle '|' themselves. Check: A command should
2453 * either have the TRLBAR flag, appear in this list or appear in
2454 * the list at ":help :bar". */
2455 case CMD_aboveleft:
2456 case CMD_and:
2457 case CMD_belowright:
2458 case CMD_botright:
2459 case CMD_browse:
2460 case CMD_call:
2461 case CMD_confirm:
2462 case CMD_delfunction:
2463 case CMD_djump:
2464 case CMD_dlist:
2465 case CMD_dsearch:
2466 case CMD_dsplit:
2467 case CMD_echo:
2468 case CMD_echoerr:
2469 case CMD_echomsg:
2470 case CMD_echon:
2471 case CMD_execute:
2472 case CMD_help:
2473 case CMD_hide:
2474 case CMD_ijump:
2475 case CMD_ilist:
2476 case CMD_isearch:
2477 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002478 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 case CMD_keepjumps:
2480 case CMD_keepmarks:
2481 case CMD_leftabove:
2482 case CMD_let:
2483 case CMD_lockmarks:
2484 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002485 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 case CMD_perl:
2487 case CMD_psearch:
2488 case CMD_python:
2489 case CMD_return:
2490 case CMD_rightbelow:
2491 case CMD_ruby:
2492 case CMD_silent:
2493 case CMD_smagic:
2494 case CMD_snomagic:
2495 case CMD_substitute:
2496 case CMD_syntax:
2497 case CMD_tcl:
2498 case CMD_throw:
2499 case CMD_tilde:
2500 case CMD_topleft:
2501 case CMD_unlet:
2502 case CMD_verbose:
2503 case CMD_vertical:
2504 break;
2505
2506 default: goto doend;
2507 }
2508 }
2509#endif
2510
2511 if (ea.argt & XFILE)
2512 {
2513 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2514 goto doend;
2515 }
2516
2517#ifdef FEAT_LISTCMDS
2518 /*
2519 * Accept buffer name. Cannot be used at the same time with a buffer
2520 * number. Don't do this for a user command.
2521 */
2522 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2523# ifdef FEAT_USR_CMDS
2524 && !USER_CMDIDX(ea.cmdidx)
2525# endif
2526 )
2527 {
2528 /*
2529 * :bdelete, :bwipeout and :bunload take several arguments, separated
2530 * by spaces: find next space (skipping over escaped characters).
2531 * The others take one argument: ignore trailing spaces.
2532 */
2533 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2534 || ea.cmdidx == CMD_bunload)
2535 p = skiptowhite_esc(ea.arg);
2536 else
2537 {
2538 p = ea.arg + STRLEN(ea.arg);
2539 while (p > ea.arg && vim_iswhite(p[-1]))
2540 --p;
2541 }
2542 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2543 if (ea.line2 < 0) /* failed */
2544 goto doend;
2545 ea.addr_count = 1;
2546 ea.arg = skipwhite(p);
2547 }
2548#endif
2549
2550/*
2551 * 6. switch on command name
2552 *
2553 * The "ea" structure holds the arguments that can be used.
2554 */
2555 ea.cmdlinep = cmdlinep;
2556 ea.getline = getline;
2557 ea.cookie = cookie;
2558#ifdef FEAT_EVAL
2559 ea.cstack = cstack;
2560#endif
2561
2562#ifdef FEAT_USR_CMDS
2563 if (USER_CMDIDX(ea.cmdidx))
2564 {
2565 /*
2566 * Execute a user-defined command.
2567 */
2568 do_ucmd(&ea);
2569 }
2570 else
2571#endif
2572 {
2573 /*
2574 * Call the function to execute the command.
2575 */
2576 ea.errmsg = NULL;
2577 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2578 if (ea.errmsg != NULL)
2579 errormsg = (char_u *)_(ea.errmsg);
2580 }
2581
2582#ifdef FEAT_EVAL
2583 /*
2584 * If the command just executed called do_cmdline(), any throw or ":return"
2585 * or ":finish" encountered there must also check the cstack of the still
2586 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2587 * exception, or reanimate a returned function or finished script file and
2588 * return or finish it again.
2589 */
2590 if (need_rethrow)
2591 do_throw(cstack);
2592 else if (check_cstack)
2593 {
2594 if (source_finished(getline, cookie))
2595 do_finish(&ea, TRUE);
2596 else if (getline_equal(getline, cookie, get_func_line)
2597 && current_func_returned())
2598 do_return(&ea, TRUE, FALSE, NULL);
2599 }
2600 need_rethrow = check_cstack = FALSE;
2601#endif
2602
2603doend:
2604 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2605 curwin->w_cursor.lnum = 1;
2606
2607 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2608 {
2609 if (sourcing)
2610 {
2611 if (errormsg != IObuff)
2612 {
2613 STRCPY(IObuff, errormsg);
2614 errormsg = IObuff;
2615 }
2616 STRCAT(errormsg, ": ");
2617 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff));
2618 }
2619 emsg(errormsg);
2620 }
2621#ifdef FEAT_EVAL
2622 do_errthrow(cstack,
2623 (ea.cmdidx != CMD_SIZE
2624# ifdef FEAT_USR_CMDS
2625 && !USER_CMDIDX(ea.cmdidx)
2626# endif
2627 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2628#endif
2629
2630 if (verbose_save >= 0)
2631 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002632#ifdef FEAT_AUTOCMD
2633 if (cmdmod.save_ei != NULL)
2634 {
2635 /* Restore 'eventignore' to the value before ":noautocmd". */
2636 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei, OPT_FREE);
2637 free_string_option(cmdmod.save_ei);
2638 }
2639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
2641 cmdmod = save_cmdmod;
2642
2643 if (did_silent > 0)
2644 {
2645 /* messages could be enabled for a serious error, need to check if the
2646 * counters don't become negative */
2647 msg_silent -= did_silent;
2648 if (msg_silent < 0)
2649 msg_silent = 0;
2650 emsg_silent -= did_esilent;
2651 if (emsg_silent < 0)
2652 emsg_silent = 0;
2653 /* Restore msg_scroll, it's set by file I/O commands, even when no
2654 * message is actually displayed. */
2655 msg_scroll = save_msg_scroll;
2656 }
2657
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002658#ifdef HAVE_SANDBOX
2659 if (did_sandbox)
2660 --sandbox;
2661#endif
2662
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2664 ea.nextcmd = NULL;
2665
2666#ifdef FEAT_EVAL
2667 --ex_nesting_level;
2668#endif
2669
2670 return ea.nextcmd;
2671}
2672#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002673 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674#endif
2675
2676/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002677 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 * If there is a match advance "pp" to the argument and return TRUE.
2679 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002680 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002682 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 char *cmd; /* name of command */
2684 int len; /* required length */
2685{
2686 int i;
2687
2688 for (i = 0; cmd[i] != NUL; ++i)
2689 if (cmd[i] != (*pp)[i])
2690 break;
2691 if (i >= len && !isalpha((*pp)[i]))
2692 {
2693 *pp = skipwhite(*pp + i);
2694 return TRUE;
2695 }
2696 return FALSE;
2697}
2698
2699/*
2700 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002701 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002703 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 * Returns NULL for an ambiguous user command.
2705 */
2706/*ARGSUSED*/
2707 static char_u *
2708find_command(eap, full)
2709 exarg_T *eap;
2710 int *full;
2711{
2712 int len;
2713 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002714 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
2716 /*
2717 * Isolate the command and search for it in the command table.
2718 * Exeptions:
2719 * - the 'k' command can directly be followed by any character.
2720 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2721 * but :sre[wind] is another command, as are :scrip[tnames],
2722 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002723 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 */
2725 p = eap->cmd;
2726 if (*p == 'k')
2727 {
2728 eap->cmdidx = CMD_k;
2729 ++p;
2730 }
2731 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002732 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2733 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 || p[1] == 'g'
2735 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2736 || p[1] == 'I'
2737 || (p[1] == 'r' && p[2] != 'e')))
2738 {
2739 eap->cmdidx = CMD_substitute;
2740 ++p;
2741 }
2742 else
2743 {
2744 while (ASCII_ISALPHA(*p))
2745 ++p;
2746 /* check for non-alpha command */
2747 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2748 ++p;
2749 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002750 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2751 {
2752 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2753 * :delete with the 'l' flag. Same for 'p'. */
2754 for (i = 0; i < len; ++i)
2755 if (eap->cmd[i] != "delete"[i])
2756 break;
2757 if (i == len - 1)
2758 {
2759 --len;
2760 if (p[-1] == 'l')
2761 eap->flags |= EXFLAG_LIST;
2762 else
2763 eap->flags |= EXFLAG_PRINT;
2764 }
2765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766
2767 if (ASCII_ISLOWER(*eap->cmd))
2768 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2769 else
2770 eap->cmdidx = cmdidxs[26];
2771
2772 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2773 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2774 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2775 (size_t)len) == 0)
2776 {
2777#ifdef FEAT_EVAL
2778 if (full != NULL
2779 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2780 *full = TRUE;
2781#endif
2782 break;
2783 }
2784
2785#ifdef FEAT_USR_CMDS
2786 /* Look for a user defined command as a last resort */
2787 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2788 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002789 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 while (ASCII_ISALNUM(*p))
2791 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002792 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
2794#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002795 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 eap->cmdidx = CMD_SIZE;
2797 }
2798
2799 return p;
2800}
2801
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002802#ifdef FEAT_USR_CMDS
2803/*
2804 * Search for a user command that matches "eap->cmd".
2805 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2806 * Return a pointer to just after the command.
2807 * Return NULL if there is no matching command.
2808 */
2809 static char_u *
2810find_ucmd(eap, p, full, xp, compl)
2811 exarg_T *eap;
2812 char_u *p; /* end of the command (possibly including count) */
2813 int *full; /* set to TRUE for a full match */
2814 expand_T *xp; /* used for completion, NULL otherwise */
2815 int *compl; /* completion flags or NULL */
2816{
2817 int len = (int)(p - eap->cmd);
2818 int j, k, matchlen = 0;
2819 ucmd_T *uc;
2820 int found = FALSE;
2821 int possible = FALSE;
2822 char_u *cp, *np; /* Point into typed cmd and test name */
2823 garray_T *gap;
2824 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2825 only full match global is accepted. */
2826
2827 /*
2828 * Look for buffer-local user commands first, then global ones.
2829 */
2830 gap = &curbuf->b_ucmds;
2831 for (;;)
2832 {
2833 for (j = 0; j < gap->ga_len; ++j)
2834 {
2835 uc = USER_CMD_GA(gap, j);
2836 cp = eap->cmd;
2837 np = uc->uc_name;
2838 k = 0;
2839 while (k < len && *np != NUL && *cp++ == *np++)
2840 k++;
2841 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2842 {
2843 /* If finding a second match, the command is ambiguous. But
2844 * not if a buffer-local command wasn't a full match and a
2845 * global command is a full match. */
2846 if (k == len && found && *np != NUL)
2847 {
2848 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002849 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002850 amb_local = TRUE;
2851 }
2852
2853 if (!found || (k == len && *np == NUL))
2854 {
2855 /* If we matched up to a digit, then there could
2856 * be another command including the digit that we
2857 * should use instead.
2858 */
2859 if (k == len)
2860 found = TRUE;
2861 else
2862 possible = TRUE;
2863
2864 if (gap == &ucmds)
2865 eap->cmdidx = CMD_USER;
2866 else
2867 eap->cmdidx = CMD_USER_BUF;
2868 eap->argt = uc->uc_argt;
2869 eap->useridx = j;
2870
2871# ifdef FEAT_CMDL_COMPL
2872 if (compl != NULL)
2873 *compl = uc->uc_compl;
2874# ifdef FEAT_EVAL
2875 if (xp != NULL)
2876 {
2877 xp->xp_arg = uc->uc_compl_arg;
2878 xp->xp_scriptID = uc->uc_scriptID;
2879 }
2880# endif
2881# endif
2882 /* Do not search for further abbreviations
2883 * if this is an exact match. */
2884 matchlen = k;
2885 if (k == len && *np == NUL)
2886 {
2887 if (full != NULL)
2888 *full = TRUE;
2889 amb_local = FALSE;
2890 break;
2891 }
2892 }
2893 }
2894 }
2895
2896 /* Stop if we found a full match or searched all. */
2897 if (j < gap->ga_len || gap == &ucmds)
2898 break;
2899 gap = &ucmds;
2900 }
2901
2902 /* Only found ambiguous matches. */
2903 if (amb_local)
2904 {
2905 if (xp != NULL)
2906 xp->xp_context = EXPAND_UNSUCCESSFUL;
2907 return NULL;
2908 }
2909
2910 /* The match we found may be followed immediately by a number. Move "p"
2911 * back to point to it. */
2912 if (found || possible)
2913 return p + (matchlen - len);
2914 return p;
2915}
2916#endif
2917
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918#if defined(FEAT_EVAL) || defined(PROTO)
2919/*
2920 * Return > 0 if an Ex command "name" exists.
2921 * Return 2 if there is an exact match.
2922 * Return 3 if there is an ambiguous match.
2923 */
2924 int
2925cmd_exists(name)
2926 char_u *name;
2927{
2928 exarg_T ea;
2929 int full = FALSE;
2930 int i;
2931 int j;
2932 static struct cmdmod
2933 {
2934 char *name;
2935 int minlen;
2936 } cmdmods[] = {
2937 {"aboveleft", 3},
2938 {"belowright", 3},
2939 {"botright", 2},
2940 {"browse", 3},
2941 {"confirm", 4},
2942 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002943 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 {"keepjumps", 5},
2945 {"keepmarks", 3},
2946 {"leftabove", 5},
2947 {"lockmarks", 3},
2948 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002949 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 {"silent", 3},
2951 {"topleft", 2},
2952 {"verbose", 4},
2953 {"vertical", 4},
2954 };
2955
2956 /* Check command modifiers. */
2957 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2958 {
2959 for (j = 0; name[j] != NUL; ++j)
2960 if (name[j] != cmdmods[i].name[j])
2961 break;
2962 if (name[j] == NUL && j >= cmdmods[i].minlen)
2963 return (cmdmods[i].name[j] == NUL ? 2 : 1);
2964 }
2965
2966 /* Check built-in commands and user defined commands. */
2967 ea.cmd = name;
2968 ea.cmdidx = (cmdidx_T)0;
2969 if (find_command(&ea, &full) == NULL)
2970 return 3;
2971 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
2972}
2973#endif
2974
2975/*
2976 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2977 * we don't need/want deleted. Maybe this could be done better if we didn't
2978 * repeat all this stuff. The only problem is that they may not stay
2979 * perfectly compatible with each other, but then the command line syntax
2980 * probably won't change that much -- webb.
2981 */
2982 char_u *
2983set_one_cmd_context(xp, buff)
2984 expand_T *xp;
2985 char_u *buff; /* buffer for command string */
2986{
2987 char_u *p;
2988 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002989 int len = 0;
2990 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
2992 int compl = EXPAND_NOTHING;
2993#endif
2994#ifdef FEAT_CMDL_COMPL
2995 int delim;
2996#endif
2997 int forceit = FALSE;
2998 int usefilter = FALSE; /* filter instead of file name */
2999
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003000 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 xp->xp_pattern = buff;
3002 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003003 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
3005/*
3006 * 2. skip comment lines and leading space, colons or bars
3007 */
3008 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3009 ;
3010 xp->xp_pattern = cmd;
3011
3012 if (*cmd == NUL)
3013 return NULL;
3014 if (*cmd == '"') /* ignore comment lines */
3015 {
3016 xp->xp_context = EXPAND_NOTHING;
3017 return NULL;
3018 }
3019
3020/*
3021 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3022 */
3023 cmd = skip_range(cmd, &xp->xp_context);
3024
3025/*
3026 * 4. parse command
3027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 xp->xp_pattern = cmd;
3029 if (*cmd == NUL)
3030 return NULL;
3031 if (*cmd == '"')
3032 {
3033 xp->xp_context = EXPAND_NOTHING;
3034 return NULL;
3035 }
3036
3037 if (*cmd == '|' || *cmd == '\n')
3038 return cmd + 1; /* There's another command */
3039
3040 /*
3041 * Isolate the command and search for it in the command table.
3042 * Exceptions:
3043 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003044 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3046 */
3047 if (*cmd == 'k' && cmd[1] != 'e')
3048 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003049 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 p = cmd + 1;
3051 }
3052 else
3053 {
3054 p = cmd;
3055 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3056 ++p;
3057 /* check for non-alpha command */
3058 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3059 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003060 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003062 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 {
3064 xp->xp_context = EXPAND_UNSUCCESSFUL;
3065 return NULL;
3066 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003067 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3068 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3069 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 break;
3071
3072#ifdef FEAT_USR_CMDS
3073 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3074 {
3075 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3076 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003077 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 }
3079#endif
3080 }
3081
3082 /*
3083 * If the cursor is touching the command, and it ends in an alpha-numeric
3084 * character, complete the command name.
3085 */
3086 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3087 return NULL;
3088
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003089 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 {
3091 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3092 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003093 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 p = cmd + 1;
3095 }
3096#ifdef FEAT_USR_CMDS
3097 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3098 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003099 ea.cmd = cmd;
3100 p = find_ucmd(&ea, p, NULL, xp,
3101# if defined(FEAT_CMDL_COMPL)
3102 &compl
3103# else
3104 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003106 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003107 if (p == NULL)
3108 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 }
3110#endif
3111 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003112 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 {
3114 /* Not still touching the command and it was an illegal one */
3115 xp->xp_context = EXPAND_UNSUCCESSFUL;
3116 return NULL;
3117 }
3118
3119 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3120
3121 if (*p == '!') /* forced commands */
3122 {
3123 forceit = TRUE;
3124 ++p;
3125 }
3126
3127/*
3128 * 5. parse arguments
3129 */
3130#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003131 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003133 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
3135 arg = skipwhite(p);
3136
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003137 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
3139 if (*arg == '>') /* append */
3140 {
3141 if (*++arg == '>')
3142 ++arg;
3143 arg = skipwhite(arg);
3144 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003145 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 {
3147 ++arg;
3148 usefilter = TRUE;
3149 }
3150 }
3151
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003152 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 {
3154 usefilter = forceit; /* :r! filter if forced */
3155 if (*arg == '!') /* :r !filter */
3156 {
3157 ++arg;
3158 usefilter = TRUE;
3159 }
3160 }
3161
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003162 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 {
3164 while (*arg == *cmd) /* allow any number of '>' or '<' */
3165 ++arg;
3166 arg = skipwhite(arg);
3167 }
3168
3169 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003170 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 {
3172 /* Check if we're in the +command */
3173 p = arg + 1;
3174 arg = skip_cmd_arg(arg, FALSE);
3175
3176 /* Still touching the command after '+'? */
3177 if (*arg == NUL)
3178 return p;
3179
3180 /* Skip space(s) after +command to get to the real argument */
3181 arg = skipwhite(arg);
3182 }
3183
3184 /*
3185 * Check for '|' to separate commands and '"' to start comments.
3186 * Don't do this for ":read !cmd" and ":write !cmd".
3187 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003188 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
3190 p = arg;
3191 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003192 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 p += 2;
3194 while (*p)
3195 {
3196 if (*p == Ctrl_V)
3197 {
3198 if (p[1] != NUL)
3199 ++p;
3200 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003201 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 || *p == '|' || *p == '\n')
3203 {
3204 if (*(p - 1) != '\\')
3205 {
3206 if (*p == '|' || *p == '\n')
3207 return p + 1;
3208 return NULL; /* It's a comment */
3209 }
3210 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003211 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 }
3213 }
3214
3215 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003216 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 vim_strchr((char_u *)"|\"", *arg) == NULL)
3218 return NULL;
3219
3220 /* Find start of last argument (argument just before cursor): */
3221 p = buff + STRLEN(buff);
3222 while (p != arg && *p != ' ' && *p != TAB)
3223 p--;
3224 if (*p == ' ' || *p == TAB)
3225 p++;
3226 xp->xp_pattern = p;
3227
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003228 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 {
3230 int in_quote = FALSE;
3231 char_u *bow = NULL; /* Beginning of word */
3232
3233 /*
3234 * Allow spaces within back-quotes to count as part of the argument
3235 * being expanded.
3236 */
3237 xp->xp_pattern = skipwhite(arg);
3238 for (p = xp->xp_pattern; *p; )
3239 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003240 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 ++p;
3242#ifdef SPACE_IN_FILENAME
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003243 else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244#else
3245 else if (vim_iswhite(*p))
3246#endif
3247 {
3248 p = skipwhite(p);
3249 if (in_quote)
3250 bow = p;
3251 else
3252 xp->xp_pattern = p;
3253 --p;
3254 }
3255 else if (*p == '`')
3256 {
3257 if (!in_quote)
3258 {
3259 xp->xp_pattern = p;
3260 bow = p + 1;
3261 }
3262 in_quote = !in_quote;
3263 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003264 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 }
3266
3267 /*
3268 * If we are still inside the quotes, and we passed a space, just
3269 * expand from there.
3270 */
3271 if (bow != NULL && in_quote)
3272 xp->xp_pattern = bow;
3273 xp->xp_context = EXPAND_FILES;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003274#ifndef BACKSLASH_IN_FILENAME
3275 /* For a shell command more chars need to be escaped. */
3276 if (usefilter || ea.cmdidx == CMD_bang)
3277 xp->xp_shell = TRUE;
3278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
3280 /* Check for environment variable */
3281 if (*xp->xp_pattern == '$'
3282#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3283 || *xp->xp_pattern == '%'
3284#endif
3285 )
3286 {
3287 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3288 if (!vim_isIDc(*p))
3289 break;
3290 if (*p == NUL)
3291 {
3292 xp->xp_context = EXPAND_ENV_VARS;
3293 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003294#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3295 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003296 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003297 compl = EXPAND_ENV_VARS;
3298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 }
3300 }
3301 }
3302
3303/*
3304 * 6. switch on command name
3305 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003306 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 {
3308 case CMD_cd:
3309 case CMD_chdir:
3310 case CMD_lcd:
3311 case CMD_lchdir:
3312 if (xp->xp_context == EXPAND_FILES)
3313 xp->xp_context = EXPAND_DIRECTORIES;
3314 break;
3315 case CMD_help:
3316 xp->xp_context = EXPAND_HELP;
3317 xp->xp_pattern = arg;
3318 break;
3319
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003320 /* Command modifiers: return the argument.
3321 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003323 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 case CMD_belowright:
3325 case CMD_botright:
3326 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003327 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003329 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 case CMD_folddoclosed:
3331 case CMD_folddoopen:
3332 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003333 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 case CMD_keepjumps:
3335 case CMD_keepmarks:
3336 case CMD_leftabove:
3337 case CMD_lockmarks:
3338 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003339 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 case CMD_silent:
3341 case CMD_topleft:
3342 case CMD_verbose:
3343 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003344 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 return arg;
3346
3347#ifdef FEAT_SEARCH_EXTRA
3348 case CMD_match:
3349 if (*arg == NUL || !ends_excmd(*arg))
3350 {
3351 /* Dummy call to clear variables. */
3352 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3353 xp->xp_context = EXPAND_HIGHLIGHT;
3354 xp->xp_pattern = arg;
3355 arg = skipwhite(skiptowhite(arg));
3356 if (*arg != NUL)
3357 {
3358 xp->xp_context = EXPAND_NOTHING;
3359 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3360 }
3361 }
3362 return find_nextcmd(arg);
3363#endif
3364
3365#ifdef FEAT_CMDL_COMPL
3366/*
3367 * All completion for the +cmdline_compl feature goes here.
3368 */
3369
3370# ifdef FEAT_USR_CMDS
3371 case CMD_command:
3372 /* Check for attributes */
3373 while (*arg == '-')
3374 {
3375 arg++; /* Skip "-" */
3376 p = skiptowhite(arg);
3377 if (*p == NUL)
3378 {
3379 /* Cursor is still in the attribute */
3380 p = vim_strchr(arg, '=');
3381 if (p == NULL)
3382 {
3383 /* No "=", so complete attribute names */
3384 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3385 xp->xp_pattern = arg;
3386 return NULL;
3387 }
3388
3389 /* For the -complete and -nargs attributes, we complete
3390 * their arguments as well.
3391 */
3392 if (STRNICMP(arg, "complete", p - arg) == 0)
3393 {
3394 xp->xp_context = EXPAND_USER_COMPLETE;
3395 xp->xp_pattern = p + 1;
3396 return NULL;
3397 }
3398 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3399 {
3400 xp->xp_context = EXPAND_USER_NARGS;
3401 xp->xp_pattern = p + 1;
3402 return NULL;
3403 }
3404 return NULL;
3405 }
3406 arg = skipwhite(p);
3407 }
3408
3409 /* After the attributes comes the new command name */
3410 p = skiptowhite(arg);
3411 if (*p == NUL)
3412 {
3413 xp->xp_context = EXPAND_USER_COMMANDS;
3414 xp->xp_pattern = arg;
3415 break;
3416 }
3417
3418 /* And finally comes a normal command */
3419 return skipwhite(p);
3420
3421 case CMD_delcommand:
3422 xp->xp_context = EXPAND_USER_COMMANDS;
3423 xp->xp_pattern = arg;
3424 break;
3425# endif
3426
3427 case CMD_global:
3428 case CMD_vglobal:
3429 delim = *arg; /* get the delimiter */
3430 if (delim)
3431 ++arg; /* skip delimiter if there is one */
3432
3433 while (arg[0] != NUL && arg[0] != delim)
3434 {
3435 if (arg[0] == '\\' && arg[1] != NUL)
3436 ++arg;
3437 ++arg;
3438 }
3439 if (arg[0] != NUL)
3440 return arg + 1;
3441 break;
3442 case CMD_and:
3443 case CMD_substitute:
3444 delim = *arg;
3445 if (delim)
3446 {
3447 /* skip "from" part */
3448 ++arg;
3449 arg = skip_regexp(arg, delim, p_magic, NULL);
3450 }
3451 /* skip "to" part */
3452 while (arg[0] != NUL && arg[0] != delim)
3453 {
3454 if (arg[0] == '\\' && arg[1] != NUL)
3455 ++arg;
3456 ++arg;
3457 }
3458 if (arg[0] != NUL) /* skip delimiter */
3459 ++arg;
3460 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3461 ++arg;
3462 if (arg[0] != NUL)
3463 return arg;
3464 break;
3465 case CMD_isearch:
3466 case CMD_dsearch:
3467 case CMD_ilist:
3468 case CMD_dlist:
3469 case CMD_ijump:
3470 case CMD_psearch:
3471 case CMD_djump:
3472 case CMD_isplit:
3473 case CMD_dsplit:
3474 arg = skipwhite(skipdigits(arg)); /* skip count */
3475 if (*arg == '/') /* Match regexp, not just whole words */
3476 {
3477 for (++arg; *arg && *arg != '/'; arg++)
3478 if (*arg == '\\' && arg[1] != NUL)
3479 arg++;
3480 if (*arg)
3481 {
3482 arg = skipwhite(arg + 1);
3483
3484 /* Check for trailing illegal characters */
3485 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3486 xp->xp_context = EXPAND_NOTHING;
3487 else
3488 return arg;
3489 }
3490 }
3491 break;
3492#ifdef FEAT_AUTOCMD
3493 case CMD_autocmd:
3494 return set_context_in_autocmd(xp, arg, FALSE);
3495
3496 case CMD_doautocmd:
3497 return set_context_in_autocmd(xp, arg, TRUE);
3498#endif
3499 case CMD_set:
3500 set_context_in_set_cmd(xp, arg, 0);
3501 break;
3502 case CMD_setglobal:
3503 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3504 break;
3505 case CMD_setlocal:
3506 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3507 break;
3508 case CMD_tag:
3509 case CMD_stag:
3510 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003511 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 case CMD_tselect:
3513 case CMD_stselect:
3514 case CMD_ptselect:
3515 case CMD_tjump:
3516 case CMD_stjump:
3517 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003518 if (*p_wop != NUL)
3519 xp->xp_context = EXPAND_TAGS_LISTFILES;
3520 else
3521 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 xp->xp_pattern = arg;
3523 break;
3524 case CMD_augroup:
3525 xp->xp_context = EXPAND_AUGROUP;
3526 xp->xp_pattern = arg;
3527 break;
3528#ifdef FEAT_SYN_HL
3529 case CMD_syntax:
3530 set_context_in_syntax_cmd(xp, arg);
3531 break;
3532#endif
3533#ifdef FEAT_EVAL
3534 case CMD_let:
3535 case CMD_if:
3536 case CMD_elseif:
3537 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003538 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 case CMD_echo:
3540 case CMD_echon:
3541 case CMD_execute:
3542 case CMD_echomsg:
3543 case CMD_echoerr:
3544 case CMD_call:
3545 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003546 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 break;
3548
3549 case CMD_unlet:
3550 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3551 arg = xp->xp_pattern + 1;
3552 xp->xp_context = EXPAND_USER_VARS;
3553 xp->xp_pattern = arg;
3554 break;
3555
3556 case CMD_function:
3557 case CMD_delfunction:
3558 xp->xp_context = EXPAND_USER_FUNC;
3559 xp->xp_pattern = arg;
3560 break;
3561
3562 case CMD_echohl:
3563 xp->xp_context = EXPAND_HIGHLIGHT;
3564 xp->xp_pattern = arg;
3565 break;
3566#endif
3567 case CMD_highlight:
3568 set_context_in_highlight_cmd(xp, arg);
3569 break;
3570#ifdef FEAT_LISTCMDS
3571 case CMD_bdelete:
3572 case CMD_bwipeout:
3573 case CMD_bunload:
3574 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3575 arg = xp->xp_pattern + 1;
3576 /*FALLTHROUGH*/
3577 case CMD_buffer:
3578 case CMD_sbuffer:
3579 case CMD_checktime:
3580 xp->xp_context = EXPAND_BUFFERS;
3581 xp->xp_pattern = arg;
3582 break;
3583#endif
3584#ifdef FEAT_USR_CMDS
3585 case CMD_USER:
3586 case CMD_USER_BUF:
3587 if (compl != EXPAND_NOTHING)
3588 {
3589 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003590 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
3592# ifdef FEAT_MENU
3593 if (compl == EXPAND_MENUS)
3594 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3595# endif
3596 if (compl == EXPAND_COMMANDS)
3597 return arg;
3598 if (compl == EXPAND_MAPPINGS)
3599 return set_context_in_map_cmd(xp, (char_u *)"map",
3600 arg, forceit, FALSE, FALSE, CMD_map);
3601 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3602 arg = xp->xp_pattern + 1;
3603 xp->xp_pattern = arg;
3604 }
3605 xp->xp_context = compl;
3606 }
3607 break;
3608#endif
3609 case CMD_map: case CMD_noremap:
3610 case CMD_nmap: case CMD_nnoremap:
3611 case CMD_vmap: case CMD_vnoremap:
3612 case CMD_omap: case CMD_onoremap:
3613 case CMD_imap: case CMD_inoremap:
3614 case CMD_cmap: case CMD_cnoremap:
3615 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003616 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 case CMD_unmap:
3618 case CMD_nunmap:
3619 case CMD_vunmap:
3620 case CMD_ounmap:
3621 case CMD_iunmap:
3622 case CMD_cunmap:
3623 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003624 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 case CMD_abbreviate: case CMD_noreabbrev:
3626 case CMD_cabbrev: case CMD_cnoreabbrev:
3627 case CMD_iabbrev: case CMD_inoreabbrev:
3628 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003629 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 case CMD_unabbreviate:
3631 case CMD_cunabbrev:
3632 case CMD_iunabbrev:
3633 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003634 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635#ifdef FEAT_MENU
3636 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3637 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3638 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3639 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3640 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3641 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3642 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3643 case CMD_tmenu: case CMD_tunmenu:
3644 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3645 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3646#endif
3647
3648 case CMD_colorscheme:
3649 xp->xp_context = EXPAND_COLORS;
3650 xp->xp_pattern = arg;
3651 break;
3652
3653 case CMD_compiler:
3654 xp->xp_context = EXPAND_COMPILER;
3655 xp->xp_pattern = arg;
3656 break;
3657
3658#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3659 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3660 case CMD_language:
3661 if (*skiptowhite(arg) == NUL)
3662 {
3663 xp->xp_context = EXPAND_LANGUAGE;
3664 xp->xp_pattern = arg;
3665 }
3666 else
3667 xp->xp_context = EXPAND_NOTHING;
3668 break;
3669#endif
3670
3671#endif /* FEAT_CMDL_COMPL */
3672
3673 default:
3674 break;
3675 }
3676 return NULL;
3677}
3678
3679/*
3680 * skip a range specifier of the form: addr [,addr] [;addr] ..
3681 *
3682 * Backslashed delimiters after / or ? will be skipped, and commands will
3683 * not be expanded between /'s and ?'s or after "'".
3684 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003685 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 * Returns the "cmd" pointer advanced to beyond the range.
3687 */
3688 char_u *
3689skip_range(cmd, ctx)
3690 char_u *cmd;
3691 int *ctx; /* pointer to xp_context or NULL */
3692{
3693 int delim;
3694
Bram Moolenaardf177f62005-02-22 08:39:57 +00003695 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 {
3697 if (*cmd == '\'')
3698 {
3699 if (*++cmd == NUL && ctx != NULL)
3700 *ctx = EXPAND_NOTHING;
3701 }
3702 else if (*cmd == '/' || *cmd == '?')
3703 {
3704 delim = *cmd++;
3705 while (*cmd != NUL && *cmd != delim)
3706 if (*cmd++ == '\\' && *cmd != NUL)
3707 ++cmd;
3708 if (*cmd == NUL && ctx != NULL)
3709 *ctx = EXPAND_NOTHING;
3710 }
3711 if (*cmd != NUL)
3712 ++cmd;
3713 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003714
3715 /* Skip ":" and white space. */
3716 while (*cmd == ':')
3717 cmd = skipwhite(cmd + 1);
3718
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 return cmd;
3720}
3721
3722/*
3723 * get a single EX address
3724 *
3725 * Set ptr to the next character after the part that was interpreted.
3726 * Set ptr to NULL when an error is encountered.
3727 *
3728 * Return MAXLNUM when no Ex address was found.
3729 */
3730 static linenr_T
3731get_address(ptr, skip, to_other_file)
3732 char_u **ptr;
3733 int skip; /* only skip the address, don't use it */
3734 int to_other_file; /* flag: may jump to other file */
3735{
3736 int c;
3737 int i;
3738 long n;
3739 char_u *cmd;
3740 pos_T pos;
3741 pos_T *fp;
3742 linenr_T lnum;
3743
3744 cmd = skipwhite(*ptr);
3745 lnum = MAXLNUM;
3746 do
3747 {
3748 switch (*cmd)
3749 {
3750 case '.': /* '.' - Cursor position */
3751 ++cmd;
3752 lnum = curwin->w_cursor.lnum;
3753 break;
3754
3755 case '$': /* '$' - last line */
3756 ++cmd;
3757 lnum = curbuf->b_ml.ml_line_count;
3758 break;
3759
3760 case '\'': /* ''' - mark */
3761 if (*++cmd == NUL)
3762 {
3763 cmd = NULL;
3764 goto error;
3765 }
3766 if (skip)
3767 ++cmd;
3768 else
3769 {
3770 /* Only accept a mark in another file when it is
3771 * used by itself: ":'M". */
3772 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3773 ++cmd;
3774 if (fp == (pos_T *)-1)
3775 /* Jumped to another file. */
3776 lnum = curwin->w_cursor.lnum;
3777 else
3778 {
3779 if (check_mark(fp) == FAIL)
3780 {
3781 cmd = NULL;
3782 goto error;
3783 }
3784 lnum = fp->lnum;
3785 }
3786 }
3787 break;
3788
3789 case '/':
3790 case '?': /* '/' or '?' - search */
3791 c = *cmd++;
3792 if (skip) /* skip "/pat/" */
3793 {
3794 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3795 if (*cmd == c)
3796 ++cmd;
3797 }
3798 else
3799 {
3800 pos = curwin->w_cursor; /* save curwin->w_cursor */
3801 /*
3802 * When '/' or '?' follows another address, start
3803 * from there.
3804 */
3805 if (lnum != MAXLNUM)
3806 curwin->w_cursor.lnum = lnum;
3807 /*
3808 * Start a forward search at the end of the line.
3809 * Start a backward search at the start of the line.
3810 * This makes sure we never match in the current
3811 * line, and can match anywhere in the
3812 * next/previous line.
3813 */
3814 if (c == '/')
3815 curwin->w_cursor.col = MAXCOL;
3816 else
3817 curwin->w_cursor.col = 0;
3818 searchcmdlen = 0;
3819 if (!do_search(NULL, c, cmd, 1L,
3820 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3821 {
3822 curwin->w_cursor = pos;
3823 cmd = NULL;
3824 goto error;
3825 }
3826 lnum = curwin->w_cursor.lnum;
3827 curwin->w_cursor = pos;
3828 /* adjust command string pointer */
3829 cmd += searchcmdlen;
3830 }
3831 break;
3832
3833 case '\\': /* "\?", "\/" or "\&", repeat search */
3834 ++cmd;
3835 if (*cmd == '&')
3836 i = RE_SUBST;
3837 else if (*cmd == '?' || *cmd == '/')
3838 i = RE_SEARCH;
3839 else
3840 {
3841 EMSG(_(e_backslash));
3842 cmd = NULL;
3843 goto error;
3844 }
3845
3846 if (!skip)
3847 {
3848 /*
3849 * When search follows another address, start from
3850 * there.
3851 */
3852 if (lnum != MAXLNUM)
3853 pos.lnum = lnum;
3854 else
3855 pos.lnum = curwin->w_cursor.lnum;
3856
3857 /*
3858 * Start the search just like for the above
3859 * do_search().
3860 */
3861 if (*cmd != '?')
3862 pos.col = MAXCOL;
3863 else
3864 pos.col = 0;
3865 if (searchit(curwin, curbuf, &pos,
3866 *cmd == '?' ? BACKWARD : FORWARD,
3867 (char_u *)"", 1L,
3868 SEARCH_MSG + SEARCH_START, i) != FAIL)
3869 lnum = pos.lnum;
3870 else
3871 {
3872 cmd = NULL;
3873 goto error;
3874 }
3875 }
3876 ++cmd;
3877 break;
3878
3879 default:
3880 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3881 lnum = getdigits(&cmd);
3882 }
3883
3884 for (;;)
3885 {
3886 cmd = skipwhite(cmd);
3887 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3888 break;
3889
3890 if (lnum == MAXLNUM)
3891 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3892 if (VIM_ISDIGIT(*cmd))
3893 i = '+'; /* "number" is same as "+number" */
3894 else
3895 i = *cmd++;
3896 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3897 n = 1;
3898 else
3899 n = getdigits(&cmd);
3900 if (i == '-')
3901 lnum -= n;
3902 else
3903 lnum += n;
3904 }
3905 } while (*cmd == '/' || *cmd == '?');
3906
3907error:
3908 *ptr = cmd;
3909 return lnum;
3910}
3911
3912/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003913 * Get flags from an Ex command argument.
3914 */
3915 static void
3916get_flags(eap)
3917 exarg_T *eap;
3918{
3919 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3920 {
3921 if (*eap->arg == 'l')
3922 eap->flags |= EXFLAG_LIST;
3923 else if (*eap->arg == 'p')
3924 eap->flags |= EXFLAG_PRINT;
3925 else
3926 eap->flags |= EXFLAG_NR;
3927 eap->arg = skipwhite(eap->arg + 1);
3928 }
3929}
3930
3931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 * Function called for command which is Not Implemented. NI!
3933 */
3934 void
3935ex_ni(eap)
3936 exarg_T *eap;
3937{
3938 if (!eap->skip)
3939 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3940}
3941
3942#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003943 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944/*
3945 * Function called for script command which is Not Implemented. NI!
3946 * Skips over ":perl <<EOF" constructs.
3947 */
3948 static void
3949ex_script_ni(eap)
3950 exarg_T *eap;
3951{
3952 if (!eap->skip)
3953 ex_ni(eap);
3954 else
3955 vim_free(script_get(eap, eap->arg));
3956}
3957#endif
3958
3959/*
3960 * Check range in Ex command for validity.
3961 * Return NULL when valid, error message when invalid.
3962 */
3963 static char_u *
3964invalid_range(eap)
3965 exarg_T *eap;
3966{
3967 if ( eap->line1 < 0
3968 || eap->line2 < 0
3969 || eap->line1 > eap->line2
3970 || ((eap->argt & RANGE)
3971 && !(eap->argt & NOTADR)
3972 && eap->line2 > curbuf->b_ml.ml_line_count
3973#ifdef FEAT_DIFF
3974 + (eap->cmdidx == CMD_diffget)
3975#endif
3976 ))
3977 return (char_u *)_(e_invrange);
3978 return NULL;
3979}
3980
3981/*
3982 * Correct the range for zero line number, if required.
3983 */
3984 static void
3985correct_range(eap)
3986 exarg_T *eap;
3987{
3988 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
3989 {
3990 if (eap->line1 == 0)
3991 eap->line1 = 1;
3992 if (eap->line2 == 0)
3993 eap->line2 = 1;
3994 }
3995}
3996
Bram Moolenaar748bf032005-02-02 23:04:36 +00003997#ifdef FEAT_QUICKFIX
3998static char_u *skip_grep_pat __ARGS((exarg_T *eap));
3999
4000/*
4001 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4002 * pattern. Otherwise return eap->arg.
4003 */
4004 static char_u *
4005skip_grep_pat(eap)
4006 exarg_T *eap;
4007{
4008 char_u *p = eap->arg;
4009
Bram Moolenaara37420f2006-02-04 22:37:47 +00004010 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4011 || eap->cmdidx == CMD_vimgrepadd
4012 || eap->cmdidx == CMD_lvimgrepadd
4013 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004014 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004015 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004016 if (p == NULL)
4017 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004018 }
4019 return p;
4020}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004021
4022/*
4023 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4024 * in the command line, so that things like % get expanded.
4025 */
4026 static char_u *
4027replace_makeprg(eap, p, cmdlinep)
4028 exarg_T *eap;
4029 char_u *p;
4030 char_u **cmdlinep;
4031{
4032 char_u *new_cmdline;
4033 char_u *program;
4034 char_u *pos;
4035 char_u *ptr;
4036 int len;
4037 int i;
4038
4039 /*
4040 * Don't do it when ":vimgrep" is used for ":grep".
4041 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004042 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4043 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4044 || eap->cmdidx == CMD_grepadd
4045 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004046 && !grep_internal(eap->cmdidx))
4047 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004048 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4049 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004050 {
4051 if (*curbuf->b_p_gp == NUL)
4052 program = p_gp;
4053 else
4054 program = curbuf->b_p_gp;
4055 }
4056 else
4057 {
4058 if (*curbuf->b_p_mp == NUL)
4059 program = p_mp;
4060 else
4061 program = curbuf->b_p_mp;
4062 }
4063
4064 p = skipwhite(p);
4065
4066 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4067 {
4068 /* replace $* by given arguments */
4069 i = 1;
4070 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4071 ++i;
4072 len = (int)STRLEN(p);
4073 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4074 if (new_cmdline == NULL)
4075 return NULL; /* out of memory */
4076 ptr = new_cmdline;
4077 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4078 {
4079 i = (int)(pos - program);
4080 STRNCPY(ptr, program, i);
4081 STRCPY(ptr += i, p);
4082 ptr += len;
4083 program = pos + 2;
4084 }
4085 STRCPY(ptr, program);
4086 }
4087 else
4088 {
4089 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4090 if (new_cmdline == NULL)
4091 return NULL; /* out of memory */
4092 STRCPY(new_cmdline, program);
4093 STRCAT(new_cmdline, " ");
4094 STRCAT(new_cmdline, p);
4095 }
4096 msg_make(p);
4097
4098 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4099 vim_free(*cmdlinep);
4100 *cmdlinep = new_cmdline;
4101 p = new_cmdline;
4102 }
4103 return p;
4104}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004105#endif
4106
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107/*
4108 * Expand file name in Ex command argument.
4109 * Return FAIL for failure, OK otherwise.
4110 */
4111 int
4112expand_filename(eap, cmdlinep, errormsgp)
4113 exarg_T *eap;
4114 char_u **cmdlinep;
4115 char_u **errormsgp;
4116{
4117 int has_wildcards; /* need to expand wildcards */
4118 char_u *repl;
4119 int srclen;
4120 char_u *p;
4121 int n;
4122
Bram Moolenaar748bf032005-02-02 23:04:36 +00004123#ifdef FEAT_QUICKFIX
4124 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4125 p = skip_grep_pat(eap);
4126#else
4127 p = eap->arg;
4128#endif
4129
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 /*
4131 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4132 * the file name contains a wildcard it should not cause expanding.
4133 * (it will be expanded anyway if there is a wildcard before replacing).
4134 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004135 has_wildcards = mch_has_wildcard(p);
4136 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004138#ifdef FEAT_EVAL
4139 /* Skip over `=expr`, wildcards in it are not expanded. */
4140 if (p[0] == '`' && p[1] == '=')
4141 {
4142 p += 2;
4143 (void)skip_expr(&p);
4144 if (*p == '`')
4145 ++p;
4146 continue;
4147 }
4148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 /*
4150 * Quick check if this cannot be the start of a special string.
4151 * Also removes backslash before '%', '#' and '<'.
4152 */
4153 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4154 {
4155 ++p;
4156 continue;
4157 }
4158
4159 /*
4160 * Try to find a match at this position.
4161 */
4162 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4163 if (*errormsgp != NULL) /* error detected */
4164 return FAIL;
4165 if (repl == NULL) /* no match found */
4166 {
4167 p += srclen;
4168 continue;
4169 }
4170
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004171 /* Wildcards won't be expanded below, the replacement is taken
4172 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4173 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4174 {
4175 char_u *l = repl;
4176
4177 repl = expand_env_save(repl);
4178 vim_free(l);
4179 }
4180
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 /* Need to escape white space et al. with a backslash. Don't do this
4182 * for shell commands (may have to use quotes instead). Don't do this
4183 * for non-unix systems when there is a single argument (spaces don't
4184 * separate arguments then). */
4185 if (!eap->usefilter
4186 && eap->cmdidx != CMD_bang
4187 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004188 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004190 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004192 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193#ifndef UNIX
4194 && !(eap->argt & NOSPC)
4195#endif
4196 )
4197 {
4198 char_u *l;
4199#ifdef BACKSLASH_IN_FILENAME
4200 /* Don't escape a backslash here, because rem_backslash() doesn't
4201 * remove it later. */
4202 static char_u *nobslash = (char_u *)" \t\"|";
4203# define ESCAPE_CHARS nobslash
4204#else
4205# define ESCAPE_CHARS escape_chars
4206#endif
4207
4208 for (l = repl; *l; ++l)
4209 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4210 {
4211 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4212 if (l != NULL)
4213 {
4214 vim_free(repl);
4215 repl = l;
4216 }
4217 break;
4218 }
4219 }
4220
4221 /* For a shell command a '!' must be escaped. */
4222 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004223 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
4225 char_u *l;
4226
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004227 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 if (l != NULL)
4229 {
4230 vim_free(repl);
4231 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004232 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 if (strstr((char *)p_sh, "sh") != NULL)
4234 {
4235 l = vim_strsave_escaped(repl, (char_u *)"!");
4236 if (l != NULL)
4237 {
4238 vim_free(repl);
4239 repl = l;
4240 }
4241 }
4242 }
4243 }
4244
4245 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4246 vim_free(repl);
4247 if (p == NULL)
4248 return FAIL;
4249 }
4250
4251 /*
4252 * One file argument: Expand wildcards.
4253 * Don't do this with ":r !command" or ":w !command".
4254 */
4255 if ((eap->argt & NOSPC) && !eap->usefilter)
4256 {
4257 /*
4258 * May do this twice:
4259 * 1. Replace environment variables.
4260 * 2. Replace any other wildcards, remove backslashes.
4261 */
4262 for (n = 1; n <= 2; ++n)
4263 {
4264 if (n == 2)
4265 {
4266#ifdef UNIX
4267 /*
4268 * Only for Unix we check for more than one file name.
4269 * For other systems spaces are considered to be part
4270 * of the file name.
4271 * Only check here if there is no wildcard, otherwise
4272 * ExpandOne() will check for errors. This allows
4273 * ":e `ls ve*.c`" on Unix.
4274 */
4275 if (!has_wildcards)
4276 for (p = eap->arg; *p; ++p)
4277 {
4278 /* skip escaped characters */
4279 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4280 ++p;
4281 else if (vim_iswhite(*p))
4282 {
4283 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4284 return FAIL;
4285 }
4286 }
4287#endif
4288
4289 /*
4290 * Halve the number of backslashes (this is Vi compatible).
4291 * For Unix and OS/2, when wildcards are expanded, this is
4292 * done by ExpandOne() below.
4293 */
4294#if defined(UNIX) || defined(OS2)
4295 if (!has_wildcards)
4296#endif
4297 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299
4300 if (has_wildcards)
4301 {
4302 if (n == 1)
4303 {
4304 /*
4305 * First loop: May expand environment variables. This
4306 * can be done much faster with expand_env() than with
4307 * something else (e.g., calling a shell).
4308 * After expanding environment variables, check again
4309 * if there are still wildcards present.
4310 */
4311 if (vim_strchr(eap->arg, '$') != NULL
4312 || vim_strchr(eap->arg, '~') != NULL)
4313 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004314 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4315 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 has_wildcards = mch_has_wildcard(NameBuff);
4317 p = NameBuff;
4318 }
4319 else
4320 p = NULL;
4321 }
4322 else /* n == 2 */
4323 {
4324 expand_T xpc;
4325
4326 ExpandInit(&xpc);
4327 xpc.xp_context = EXPAND_FILES;
4328 p = ExpandOne(&xpc, eap->arg, NULL,
4329 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4330 WILD_EXPAND_FREE);
4331 ExpandCleanup(&xpc);
4332 if (p == NULL)
4333 return FAIL;
4334 }
4335 if (p != NULL)
4336 {
4337 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4338 p, cmdlinep);
4339 if (n == 2) /* p came from ExpandOne() */
4340 vim_free(p);
4341 }
4342 }
4343 }
4344 }
4345 return OK;
4346}
4347
4348/*
4349 * Replace part of the command line, keeping eap->cmd, eap->arg and
4350 * eap->nextcmd correct.
4351 * "src" points to the part that is to be replaced, of length "srclen".
4352 * "repl" is the replacement string.
4353 * Returns a pointer to the character after the replaced string.
4354 * Returns NULL for failure.
4355 */
4356 static char_u *
4357repl_cmdline(eap, src, srclen, repl, cmdlinep)
4358 exarg_T *eap;
4359 char_u *src;
4360 int srclen;
4361 char_u *repl;
4362 char_u **cmdlinep;
4363{
4364 int len;
4365 int i;
4366 char_u *new_cmdline;
4367
4368 /*
4369 * The new command line is build in new_cmdline[].
4370 * First allocate it.
4371 * Careful: a "+cmd" argument may have been NUL terminated.
4372 */
4373 len = (int)STRLEN(repl);
4374 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
4375 if (eap->nextcmd)
4376 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4377 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4378 return NULL; /* out of memory! */
4379
4380 /*
4381 * Copy the stuff before the expanded part.
4382 * Copy the expanded stuff.
4383 * Copy what came after the expanded part.
4384 * Copy the next commands, if there are any.
4385 */
4386 i = (int)(src - *cmdlinep); /* length of part before match */
4387 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004388
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 mch_memmove(new_cmdline + i, repl, (size_t)len);
4390 i += len; /* remember the end of the string */
4391 STRCPY(new_cmdline + i, src + srclen);
4392 src = new_cmdline + i; /* remember where to continue */
4393
4394 if (eap->nextcmd) /* append next command */
4395 {
4396 i = (int)STRLEN(new_cmdline) + 1;
4397 STRCPY(new_cmdline + i, eap->nextcmd);
4398 eap->nextcmd = new_cmdline + i;
4399 }
4400 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4401 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4402 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4403 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4404 vim_free(*cmdlinep);
4405 *cmdlinep = new_cmdline;
4406
4407 return src;
4408}
4409
4410/*
4411 * Check for '|' to separate commands and '"' to start comments.
4412 */
4413 void
4414separate_nextcmd(eap)
4415 exarg_T *eap;
4416{
4417 char_u *p;
4418
Bram Moolenaar86b68352004-12-27 21:59:20 +00004419#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004420 p = skip_grep_pat(eap);
4421#else
4422 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004423#endif
4424
4425 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 {
4427 if (*p == Ctrl_V)
4428 {
4429 if (eap->argt & (USECTRLV | XFILE))
4430 ++p; /* skip CTRL-V and next char */
4431 else
4432 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4433 if (*p == NUL) /* stop at NUL after CTRL-V */
4434 break;
4435 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004436
4437#ifdef FEAT_EVAL
4438 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004439 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004440 {
4441 p += 2;
4442 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004443 }
4444#endif
4445
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 /* Check for '"': start of comment or '|': next command */
4447 /* :@" and :*" do not start a comment!
4448 * :redir @" doesn't either. */
4449 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4450 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4451 || p != eap->arg)
4452 && (eap->cmdidx != CMD_redir
4453 || p != eap->arg + 1 || p[-1] != '@'))
4454 || *p == '|' || *p == '\n')
4455 {
4456 /*
4457 * We remove the '\' before the '|', unless USECTRLV is used
4458 * AND 'b' is present in 'cpoptions'.
4459 */
4460 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4461 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4462 {
4463 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4464 --p;
4465 }
4466 else
4467 {
4468 eap->nextcmd = check_nextcmd(p);
4469 *p = NUL;
4470 break;
4471 }
4472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004474
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4476 del_trailing_spaces(eap->arg);
4477}
4478
4479/*
4480 * get + command from ex argument
4481 */
4482 static char_u *
4483getargcmd(argp)
4484 char_u **argp;
4485{
4486 char_u *arg = *argp;
4487 char_u *command = NULL;
4488
4489 if (*arg == '+') /* +[command] */
4490 {
4491 ++arg;
4492 if (vim_isspace(*arg))
4493 command = dollar_command;
4494 else
4495 {
4496 command = arg;
4497 arg = skip_cmd_arg(command, TRUE);
4498 if (*arg != NUL)
4499 *arg++ = NUL; /* terminate command with NUL */
4500 }
4501
4502 arg = skipwhite(arg); /* skip over spaces */
4503 *argp = arg;
4504 }
4505 return command;
4506}
4507
4508/*
4509 * Find end of "+command" argument. Skip over "\ " and "\\".
4510 */
4511 static char_u *
4512skip_cmd_arg(p, rembs)
4513 char_u *p;
4514 int rembs; /* TRUE to halve the number of backslashes */
4515{
4516 while (*p && !vim_isspace(*p))
4517 {
4518 if (*p == '\\' && p[1] != NUL)
4519 {
4520 if (rembs)
4521 mch_memmove(p, p + 1, STRLEN(p));
4522 else
4523 ++p;
4524 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004525 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527 return p;
4528}
4529
4530/*
4531 * Get "++opt=arg" argument.
4532 * Return FAIL or OK.
4533 */
4534 static int
4535getargopt(eap)
4536 exarg_T *eap;
4537{
4538 char_u *arg = eap->arg + 2;
4539 int *pp = NULL;
4540#ifdef FEAT_MBYTE
4541 char_u *p;
4542#endif
4543
4544 /* ":edit ++[no]bin[ary] file" */
4545 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4546 {
4547 if (*arg == 'n')
4548 {
4549 arg += 2;
4550 eap->force_bin = FORCE_NOBIN;
4551 }
4552 else
4553 eap->force_bin = FORCE_BIN;
4554 if (!checkforcmd(&arg, "binary", 3))
4555 return FAIL;
4556 eap->arg = skipwhite(arg);
4557 return OK;
4558 }
4559
4560 if (STRNCMP(arg, "ff", 2) == 0)
4561 {
4562 arg += 2;
4563 pp = &eap->force_ff;
4564 }
4565 else if (STRNCMP(arg, "fileformat", 10) == 0)
4566 {
4567 arg += 10;
4568 pp = &eap->force_ff;
4569 }
4570#ifdef FEAT_MBYTE
4571 else if (STRNCMP(arg, "enc", 3) == 0)
4572 {
4573 arg += 3;
4574 pp = &eap->force_enc;
4575 }
4576 else if (STRNCMP(arg, "encoding", 8) == 0)
4577 {
4578 arg += 8;
4579 pp = &eap->force_enc;
4580 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004581 else if (STRNCMP(arg, "bad", 3) == 0)
4582 {
4583 arg += 3;
4584 pp = &eap->bad_char;
4585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586#endif
4587
4588 if (pp == NULL || *arg != '=')
4589 return FAIL;
4590
4591 ++arg;
4592 *pp = (int)(arg - eap->cmd);
4593 arg = skip_cmd_arg(arg, FALSE);
4594 eap->arg = skipwhite(arg);
4595 *arg = NUL;
4596
4597#ifdef FEAT_MBYTE
4598 if (pp == &eap->force_ff)
4599 {
4600#endif
4601 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4602 return FAIL;
4603#ifdef FEAT_MBYTE
4604 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004605 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {
4607 /* Make 'fileencoding' lower case. */
4608 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4609 *p = TOLOWER_ASC(*p);
4610 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004611 else
4612 {
4613 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4614 * "drop". */
4615 p = eap->cmd + eap->bad_char;
4616 if (STRICMP(p, "keep") == 0)
4617 eap->bad_char = BAD_KEEP;
4618 else if (STRICMP(p, "drop") == 0)
4619 eap->bad_char = BAD_DROP;
4620 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4621 eap->bad_char = *p;
4622 else
4623 return FAIL;
4624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625#endif
4626
4627 return OK;
4628}
4629
4630/*
4631 * ":abbreviate" and friends.
4632 */
4633 static void
4634ex_abbreviate(eap)
4635 exarg_T *eap;
4636{
4637 do_exmap(eap, TRUE); /* almost the same as mapping */
4638}
4639
4640/*
4641 * ":map" and friends.
4642 */
4643 static void
4644ex_map(eap)
4645 exarg_T *eap;
4646{
4647 /*
4648 * If we are sourcing .exrc or .vimrc in current directory we
4649 * print the mappings for security reasons.
4650 */
4651 if (secure)
4652 {
4653 secure = 2;
4654 msg_outtrans(eap->cmd);
4655 msg_putchar('\n');
4656 }
4657 do_exmap(eap, FALSE);
4658}
4659
4660/*
4661 * ":unmap" and friends.
4662 */
4663 static void
4664ex_unmap(eap)
4665 exarg_T *eap;
4666{
4667 do_exmap(eap, FALSE);
4668}
4669
4670/*
4671 * ":mapclear" and friends.
4672 */
4673 static void
4674ex_mapclear(eap)
4675 exarg_T *eap;
4676{
4677 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4678}
4679
4680/*
4681 * ":abclear" and friends.
4682 */
4683 static void
4684ex_abclear(eap)
4685 exarg_T *eap;
4686{
4687 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4688}
4689
4690#ifdef FEAT_AUTOCMD
4691 static void
4692ex_autocmd(eap)
4693 exarg_T *eap;
4694{
4695 /*
4696 * Disallow auto commands from .exrc and .vimrc in current
4697 * directory for security reasons.
4698 */
4699 if (secure)
4700 {
4701 secure = 2;
4702 eap->errmsg = e_curdir;
4703 }
4704 else if (eap->cmdidx == CMD_autocmd)
4705 do_autocmd(eap->arg, eap->forceit);
4706 else
4707 do_augroup(eap->arg, eap->forceit);
4708}
4709
4710/*
4711 * ":doautocmd": Apply the automatic commands to the current buffer.
4712 */
4713 static void
4714ex_doautocmd(eap)
4715 exarg_T *eap;
4716{
4717 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004718 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719}
4720#endif
4721
4722#ifdef FEAT_LISTCMDS
4723/*
4724 * :[N]bunload[!] [N] [bufname] unload buffer
4725 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4726 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4727 */
4728 static void
4729ex_bunload(eap)
4730 exarg_T *eap;
4731{
4732 eap->errmsg = do_bufdel(
4733 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4734 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4735 : DOBUF_UNLOAD, eap->arg,
4736 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4737}
4738
4739/*
4740 * :[N]buffer [N] to buffer N
4741 * :[N]sbuffer [N] to buffer N
4742 */
4743 static void
4744ex_buffer(eap)
4745 exarg_T *eap;
4746{
4747 if (*eap->arg)
4748 eap->errmsg = e_trailing;
4749 else
4750 {
4751 if (eap->addr_count == 0) /* default is current buffer */
4752 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4753 else
4754 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4755 }
4756}
4757
4758/*
4759 * :[N]bmodified [N] to next mod. buffer
4760 * :[N]sbmodified [N] to next mod. buffer
4761 */
4762 static void
4763ex_bmodified(eap)
4764 exarg_T *eap;
4765{
4766 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4767}
4768
4769/*
4770 * :[N]bnext [N] to next buffer
4771 * :[N]sbnext [N] split and to next buffer
4772 */
4773 static void
4774ex_bnext(eap)
4775 exarg_T *eap;
4776{
4777 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4778}
4779
4780/*
4781 * :[N]bNext [N] to previous buffer
4782 * :[N]bprevious [N] to previous buffer
4783 * :[N]sbNext [N] split and to previous buffer
4784 * :[N]sbprevious [N] split and to previous buffer
4785 */
4786 static void
4787ex_bprevious(eap)
4788 exarg_T *eap;
4789{
4790 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4791}
4792
4793/*
4794 * :brewind to first buffer
4795 * :bfirst to first buffer
4796 * :sbrewind split and to first buffer
4797 * :sbfirst split and to first buffer
4798 */
4799 static void
4800ex_brewind(eap)
4801 exarg_T *eap;
4802{
4803 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4804}
4805
4806/*
4807 * :blast to last buffer
4808 * :sblast split and to last buffer
4809 */
4810 static void
4811ex_blast(eap)
4812 exarg_T *eap;
4813{
4814 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4815}
4816#endif
4817
4818 int
4819ends_excmd(c)
4820 int c;
4821{
4822 return (c == NUL || c == '|' || c == '"' || c == '\n');
4823}
4824
4825#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4826 || defined(PROTO)
4827/*
4828 * Return the next command, after the first '|' or '\n'.
4829 * Return NULL if not found.
4830 */
4831 char_u *
4832find_nextcmd(p)
4833 char_u *p;
4834{
4835 while (*p != '|' && *p != '\n')
4836 {
4837 if (*p == NUL)
4838 return NULL;
4839 ++p;
4840 }
4841 return (p + 1);
4842}
4843#endif
4844
4845/*
4846 * Check if *p is a separator between Ex commands.
4847 * Return NULL if it isn't, (p + 1) if it is.
4848 */
4849 char_u *
4850check_nextcmd(p)
4851 char_u *p;
4852{
4853 p = skipwhite(p);
4854 if (*p == '|' || *p == '\n')
4855 return (p + 1);
4856 else
4857 return NULL;
4858}
4859
4860/*
4861 * - if there are more files to edit
4862 * - and this is the last window
4863 * - and forceit not used
4864 * - and not repeated twice on a row
4865 * return FAIL and give error message if 'message' TRUE
4866 * return OK otherwise
4867 */
4868 static int
4869check_more(message, forceit)
4870 int message; /* when FALSE check only, no messages */
4871 int forceit;
4872{
4873 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4874
4875 if (!forceit && only_one_window() && ARGCOUNT > 1 && !arg_had_last
4876 && n >= 0 && quitmore == 0)
4877 {
4878 if (message)
4879 {
4880#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4881 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4882 {
4883 char_u buff[IOSIZE];
4884
4885 if (n == 1)
4886 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4887 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004888 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 _("%d more files to edit. Quit anyway?"), n);
4890 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4891 return OK;
4892 return FAIL;
4893 }
4894#endif
4895 if (n == 1)
4896 EMSG(_("E173: 1 more file to edit"));
4897 else
4898 EMSGN(_("E173: %ld more files to edit"), n);
4899 quitmore = 2; /* next try to quit is allowed */
4900 }
4901 return FAIL;
4902 }
4903 return OK;
4904}
4905
4906#ifdef FEAT_CMDL_COMPL
4907/*
4908 * Function given to ExpandGeneric() to obtain the list of command names.
4909 */
4910/*ARGSUSED*/
4911 char_u *
4912get_command_name(xp, idx)
4913 expand_T *xp;
4914 int idx;
4915{
4916 if (idx >= (int)CMD_SIZE)
4917# ifdef FEAT_USR_CMDS
4918 return get_user_command_name(idx);
4919# else
4920 return NULL;
4921# endif
4922 return cmdnames[idx].cmd_name;
4923}
4924#endif
4925
4926#if defined(FEAT_USR_CMDS) || defined(PROTO)
4927static 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));
4928static void uc_list __ARGS((char_u *name, size_t name_len));
4929static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4930static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4931static 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));
4932
4933 static int
4934uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4935 char_u *name;
4936 size_t name_len;
4937 char_u *rep;
4938 long argt;
4939 long def;
4940 int flags;
4941 int compl;
4942 char_u *compl_arg;
4943 int force;
4944{
4945 ucmd_T *cmd = NULL;
4946 char_u *p;
4947 int i;
4948 int cmp = 1;
4949 char_u *rep_buf = NULL;
4950 garray_T *gap;
4951
4952 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4953 if (rep_buf == NULL)
4954 {
4955 /* Can't replace termcodes - try using the string as is */
4956 rep_buf = vim_strsave(rep);
4957
4958 /* Give up if out of memory */
4959 if (rep_buf == NULL)
4960 return FAIL;
4961 }
4962
4963 /* get address of growarray: global or in curbuf */
4964 if (flags & UC_BUFFER)
4965 {
4966 gap = &curbuf->b_ucmds;
4967 if (gap->ga_itemsize == 0)
4968 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4969 }
4970 else
4971 gap = &ucmds;
4972
4973 /* Search for the command in the already defined commands. */
4974 for (i = 0; i < gap->ga_len; ++i)
4975 {
4976 size_t len;
4977
4978 cmd = USER_CMD_GA(gap, i);
4979 len = STRLEN(cmd->uc_name);
4980 cmp = STRNCMP(name, cmd->uc_name, name_len);
4981 if (cmp == 0)
4982 {
4983 if (name_len < len)
4984 cmp = -1;
4985 else if (name_len > len)
4986 cmp = 1;
4987 }
4988
4989 if (cmp == 0)
4990 {
4991 if (!force)
4992 {
4993 EMSG(_("E174: Command already exists: add ! to replace it"));
4994 goto fail;
4995 }
4996
4997 vim_free(cmd->uc_rep);
4998 cmd->uc_rep = 0;
4999 break;
5000 }
5001
5002 /* Stop as soon as we pass the name to add */
5003 if (cmp < 0)
5004 break;
5005 }
5006
5007 /* Extend the array unless we're replacing an existing command */
5008 if (cmp != 0)
5009 {
5010 if (ga_grow(gap, 1) != OK)
5011 goto fail;
5012 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5013 goto fail;
5014
5015 cmd = USER_CMD_GA(gap, i);
5016 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5017
5018 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019
5020 cmd->uc_name = p;
5021 }
5022
5023 cmd->uc_rep = rep_buf;
5024 cmd->uc_argt = argt;
5025 cmd->uc_def = def;
5026 cmd->uc_compl = compl;
5027#ifdef FEAT_EVAL
5028 cmd->uc_scriptID = current_SID;
5029# ifdef FEAT_CMDL_COMPL
5030 cmd->uc_compl_arg = compl_arg;
5031# endif
5032#endif
5033
5034 return OK;
5035
5036fail:
5037 vim_free(rep_buf);
5038#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5039 vim_free(compl_arg);
5040#endif
5041 return FAIL;
5042}
5043
5044/*
5045 * List of names for completion for ":command" with the EXPAND_ flag.
5046 * Must be alphabetical for completion.
5047 */
5048static struct
5049{
5050 int expand;
5051 char *name;
5052} command_complete[] =
5053{
5054 {EXPAND_AUGROUP, "augroup"},
5055 {EXPAND_BUFFERS, "buffer"},
5056 {EXPAND_COMMANDS, "command"},
5057#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5058 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005059 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060#endif
5061 {EXPAND_DIRECTORIES, "dir"},
5062 {EXPAND_ENV_VARS, "environment"},
5063 {EXPAND_EVENTS, "event"},
5064 {EXPAND_EXPRESSION, "expression"},
5065 {EXPAND_FILES, "file"},
5066 {EXPAND_FUNCTIONS, "function"},
5067 {EXPAND_HELP, "help"},
5068 {EXPAND_HIGHLIGHT, "highlight"},
5069 {EXPAND_MAPPINGS, "mapping"},
5070 {EXPAND_MENUS, "menu"},
5071 {EXPAND_SETTINGS, "option"},
5072 {EXPAND_TAGS, "tag"},
5073 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5074 {EXPAND_USER_VARS, "var"},
5075 {0, NULL}
5076};
5077
5078 static void
5079uc_list(name, name_len)
5080 char_u *name;
5081 size_t name_len;
5082{
5083 int i, j;
5084 int found = FALSE;
5085 ucmd_T *cmd;
5086 int len;
5087 long a;
5088 garray_T *gap;
5089
5090 gap = &curbuf->b_ucmds;
5091 for (;;)
5092 {
5093 for (i = 0; i < gap->ga_len; ++i)
5094 {
5095 cmd = USER_CMD_GA(gap, i);
5096 a = cmd->uc_argt;
5097
5098 /* Skip commands which don't match the requested prefix */
5099 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5100 continue;
5101
5102 /* Put out the title first time */
5103 if (!found)
5104 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5105 found = TRUE;
5106 msg_putchar('\n');
5107 if (got_int)
5108 break;
5109
5110 /* Special cases */
5111 msg_putchar(a & BANG ? '!' : ' ');
5112 msg_putchar(a & REGSTR ? '"' : ' ');
5113 msg_putchar(gap != &ucmds ? 'b' : ' ');
5114 msg_putchar(' ');
5115
5116 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5117 len = (int)STRLEN(cmd->uc_name) + 4;
5118
5119 do {
5120 msg_putchar(' ');
5121 ++len;
5122 } while (len < 16);
5123
5124 len = 0;
5125
5126 /* Arguments */
5127 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5128 {
5129 case 0: IObuff[len++] = '0'; break;
5130 case (EXTRA): IObuff[len++] = '*'; break;
5131 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5132 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5133 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5134 }
5135
5136 do {
5137 IObuff[len++] = ' ';
5138 } while (len < 5);
5139
5140 /* Range */
5141 if (a & (RANGE|COUNT))
5142 {
5143 if (a & COUNT)
5144 {
5145 /* -count=N */
5146 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5147 len += (int)STRLEN(IObuff + len);
5148 }
5149 else if (a & DFLALL)
5150 IObuff[len++] = '%';
5151 else if (cmd->uc_def >= 0)
5152 {
5153 /* -range=N */
5154 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5155 len += (int)STRLEN(IObuff + len);
5156 }
5157 else
5158 IObuff[len++] = '.';
5159 }
5160
5161 do {
5162 IObuff[len++] = ' ';
5163 } while (len < 11);
5164
5165 /* Completion */
5166 for (j = 0; command_complete[j].expand != 0; ++j)
5167 if (command_complete[j].expand == cmd->uc_compl)
5168 {
5169 STRCPY(IObuff + len, command_complete[j].name);
5170 len += (int)STRLEN(IObuff + len);
5171 break;
5172 }
5173
5174 do {
5175 IObuff[len++] = ' ';
5176 } while (len < 21);
5177
5178 IObuff[len] = '\0';
5179 msg_outtrans(IObuff);
5180
5181 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005182#ifdef FEAT_EVAL
5183 if (p_verbose > 0)
5184 last_set_msg(cmd->uc_scriptID);
5185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 out_flush();
5187 ui_breakcheck();
5188 if (got_int)
5189 break;
5190 }
5191 if (gap == &ucmds || i < gap->ga_len)
5192 break;
5193 gap = &ucmds;
5194 }
5195
5196 if (!found)
5197 MSG(_("No user-defined commands found"));
5198}
5199
5200 static char_u *
5201uc_fun_cmd()
5202{
5203 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5204 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5205 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5206 0xb9, 0x7f, 0};
5207 int i;
5208
5209 for (i = 0; fcmd[i]; ++i)
5210 IObuff[i] = fcmd[i] - 0x40;
5211 IObuff[i] = 0;
5212 return IObuff;
5213}
5214
5215 static int
5216uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5217 char_u *attr;
5218 size_t len;
5219 long *argt;
5220 long *def;
5221 int *flags;
5222 int *compl;
5223 char_u **compl_arg;
5224{
5225 char_u *p;
5226
5227 if (len == 0)
5228 {
5229 EMSG(_("E175: No attribute specified"));
5230 return FAIL;
5231 }
5232
5233 /* First, try the simple attributes (no arguments) */
5234 if (STRNICMP(attr, "bang", len) == 0)
5235 *argt |= BANG;
5236 else if (STRNICMP(attr, "buffer", len) == 0)
5237 *flags |= UC_BUFFER;
5238 else if (STRNICMP(attr, "register", len) == 0)
5239 *argt |= REGSTR;
5240 else if (STRNICMP(attr, "bar", len) == 0)
5241 *argt |= TRLBAR;
5242 else
5243 {
5244 int i;
5245 char_u *val = NULL;
5246 size_t vallen = 0;
5247 size_t attrlen = len;
5248
5249 /* Look for the attribute name - which is the part before any '=' */
5250 for (i = 0; i < (int)len; ++i)
5251 {
5252 if (attr[i] == '=')
5253 {
5254 val = &attr[i + 1];
5255 vallen = len - i - 1;
5256 attrlen = i;
5257 break;
5258 }
5259 }
5260
5261 if (STRNICMP(attr, "nargs", attrlen) == 0)
5262 {
5263 if (vallen == 1)
5264 {
5265 if (*val == '0')
5266 /* Do nothing - this is the default */;
5267 else if (*val == '1')
5268 *argt |= (EXTRA | NOSPC | NEEDARG);
5269 else if (*val == '*')
5270 *argt |= EXTRA;
5271 else if (*val == '?')
5272 *argt |= (EXTRA | NOSPC);
5273 else if (*val == '+')
5274 *argt |= (EXTRA | NEEDARG);
5275 else
5276 goto wrong_nargs;
5277 }
5278 else
5279 {
5280wrong_nargs:
5281 EMSG(_("E176: Invalid number of arguments"));
5282 return FAIL;
5283 }
5284 }
5285 else if (STRNICMP(attr, "range", attrlen) == 0)
5286 {
5287 *argt |= RANGE;
5288 if (vallen == 1 && *val == '%')
5289 *argt |= DFLALL;
5290 else if (val != NULL)
5291 {
5292 p = val;
5293 if (*def >= 0)
5294 {
5295two_count:
5296 EMSG(_("E177: Count cannot be specified twice"));
5297 return FAIL;
5298 }
5299
5300 *def = getdigits(&p);
5301 *argt |= (ZEROR | NOTADR);
5302
5303 if (p != val + vallen || vallen == 0)
5304 {
5305invalid_count:
5306 EMSG(_("E178: Invalid default value for count"));
5307 return FAIL;
5308 }
5309 }
5310 }
5311 else if (STRNICMP(attr, "count", attrlen) == 0)
5312 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005313 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314
5315 if (val != NULL)
5316 {
5317 p = val;
5318 if (*def >= 0)
5319 goto two_count;
5320
5321 *def = getdigits(&p);
5322
5323 if (p != val + vallen)
5324 goto invalid_count;
5325 }
5326
5327 if (*def < 0)
5328 *def = 0;
5329 }
5330 else if (STRNICMP(attr, "complete", attrlen) == 0)
5331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 if (val == NULL)
5333 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005334 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 return FAIL;
5336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005338 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5339 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342 else
5343 {
5344 char_u ch = attr[len];
5345 attr[len] = '\0';
5346 EMSG2(_("E181: Invalid attribute: %s"), attr);
5347 attr[len] = ch;
5348 return FAIL;
5349 }
5350 }
5351
5352 return OK;
5353}
5354
5355 static void
5356ex_command(eap)
5357 exarg_T *eap;
5358{
5359 char_u *name;
5360 char_u *end;
5361 char_u *p;
5362 long argt = 0;
5363 long def = -1;
5364 int flags = 0;
5365 int compl = EXPAND_NOTHING;
5366 char_u *compl_arg = NULL;
5367 int has_attr = (eap->arg[0] == '-');
5368
5369 p = eap->arg;
5370
5371 /* Check for attributes */
5372 while (*p == '-')
5373 {
5374 ++p;
5375 end = skiptowhite(p);
5376 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5377 == FAIL)
5378 return;
5379 p = skipwhite(end);
5380 }
5381
5382 /* Get the name (if any) and skip to the following argument */
5383 name = p;
5384 if (ASCII_ISALPHA(*p))
5385 while (ASCII_ISALNUM(*p))
5386 ++p;
5387 if (!ends_excmd(*p) && !vim_iswhite(*p))
5388 {
5389 EMSG(_("E182: Invalid command name"));
5390 return;
5391 }
5392 end = p;
5393
5394 /* If there is nothing after the name, and no attributes were specified,
5395 * we are listing commands
5396 */
5397 p = skipwhite(end);
5398 if (!has_attr && ends_excmd(*p))
5399 {
5400 uc_list(name, end - name);
5401 }
5402 else if (!ASCII_ISUPPER(*name))
5403 {
5404 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5405 return;
5406 }
5407 else
5408 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5409 eap->forceit);
5410}
5411
5412/*
5413 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 * Clear all user commands, global and for current buffer.
5415 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005416/*ARGSUSED*/
5417 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418ex_comclear(eap)
5419 exarg_T *eap;
5420{
5421 uc_clear(&ucmds);
5422 uc_clear(&curbuf->b_ucmds);
5423}
5424
5425/*
5426 * Clear all user commands for "gap".
5427 */
5428 void
5429uc_clear(gap)
5430 garray_T *gap;
5431{
5432 int i;
5433 ucmd_T *cmd;
5434
5435 for (i = 0; i < gap->ga_len; ++i)
5436 {
5437 cmd = USER_CMD_GA(gap, i);
5438 vim_free(cmd->uc_name);
5439 vim_free(cmd->uc_rep);
5440# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5441 vim_free(cmd->uc_compl_arg);
5442# endif
5443 }
5444 ga_clear(gap);
5445}
5446
5447 static void
5448ex_delcommand(eap)
5449 exarg_T *eap;
5450{
5451 int i = 0;
5452 ucmd_T *cmd = NULL;
5453 int cmp = -1;
5454 garray_T *gap;
5455
5456 gap = &curbuf->b_ucmds;
5457 for (;;)
5458 {
5459 for (i = 0; i < gap->ga_len; ++i)
5460 {
5461 cmd = USER_CMD_GA(gap, i);
5462 cmp = STRCMP(eap->arg, cmd->uc_name);
5463 if (cmp <= 0)
5464 break;
5465 }
5466 if (gap == &ucmds || cmp == 0)
5467 break;
5468 gap = &ucmds;
5469 }
5470
5471 if (cmp != 0)
5472 {
5473 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5474 return;
5475 }
5476
5477 vim_free(cmd->uc_name);
5478 vim_free(cmd->uc_rep);
5479# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5480 vim_free(cmd->uc_compl_arg);
5481# endif
5482
5483 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
5485 if (i < gap->ga_len)
5486 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5487}
5488
5489 static char_u *
5490uc_split_args(arg, lenp)
5491 char_u *arg;
5492 size_t *lenp;
5493{
5494 char_u *buf;
5495 char_u *p;
5496 char_u *q;
5497 int len;
5498
5499 /* Precalculate length */
5500 p = arg;
5501 len = 2; /* Initial and final quotes */
5502
5503 while (*p)
5504 {
5505 if (p[0] == '\\' && vim_iswhite(p[1]))
5506 {
5507 len += 1;
5508 p += 2;
5509 }
5510 else if (*p == '\\' || *p == '"')
5511 {
5512 len += 2;
5513 p += 1;
5514 }
5515 else if (vim_iswhite(*p))
5516 {
5517 p = skipwhite(p);
5518 if (*p == NUL)
5519 break;
5520 len += 3; /* "," */
5521 }
5522 else
5523 {
5524 ++len;
5525 ++p;
5526 }
5527 }
5528
5529 buf = alloc(len + 1);
5530 if (buf == NULL)
5531 {
5532 *lenp = 0;
5533 return buf;
5534 }
5535
5536 p = arg;
5537 q = buf;
5538 *q++ = '"';
5539 while (*p)
5540 {
5541 if (p[0] == '\\' && vim_iswhite(p[1]))
5542 {
5543 *q++ = p[1];
5544 p += 2;
5545 }
5546 else if (*p == '\\' || *p == '"')
5547 {
5548 *q++ = '\\';
5549 *q++ = *p++;
5550 }
5551 else if (vim_iswhite(*p))
5552 {
5553 p = skipwhite(p);
5554 if (*p == NUL)
5555 break;
5556 *q++ = '"';
5557 *q++ = ',';
5558 *q++ = '"';
5559 }
5560 else
5561 {
5562 *q++ = *p++;
5563 }
5564 }
5565 *q++ = '"';
5566 *q = 0;
5567
5568 *lenp = len;
5569 return buf;
5570}
5571
5572/*
5573 * Check for a <> code in a user command.
5574 * "code" points to the '<'. "len" the length of the <> (inclusive).
5575 * "buf" is where the result is to be added.
5576 * "split_buf" points to a buffer used for splitting, caller should free it.
5577 * "split_len" is the length of what "split_buf" contains.
5578 * Returns the length of the replacement, which has been added to "buf".
5579 * Returns -1 if there was no match, and only the "<" has been copied.
5580 */
5581 static size_t
5582uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5583 char_u *code;
5584 size_t len;
5585 char_u *buf;
5586 ucmd_T *cmd; /* the user command we're expanding */
5587 exarg_T *eap; /* ex arguments */
5588 char_u **split_buf;
5589 size_t *split_len;
5590{
5591 size_t result = 0;
5592 char_u *p = code + 1;
5593 size_t l = len - 2;
5594 int quote = 0;
5595 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5596 ct_LT, ct_NONE } type = ct_NONE;
5597
5598 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5599 {
5600 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5601 p += 2;
5602 l -= 2;
5603 }
5604
5605 if (l < 1)
5606 type = ct_NONE;
5607 else if (STRNICMP(p, "args", l) == 0)
5608 type = ct_ARGS;
5609 else if (STRNICMP(p, "bang", l) == 0)
5610 type = ct_BANG;
5611 else if (STRNICMP(p, "count", l) == 0)
5612 type = ct_COUNT;
5613 else if (STRNICMP(p, "line1", l) == 0)
5614 type = ct_LINE1;
5615 else if (STRNICMP(p, "line2", l) == 0)
5616 type = ct_LINE2;
5617 else if (STRNICMP(p, "lt", l) == 0)
5618 type = ct_LT;
5619 else if (STRNICMP(p, "register", l) == 0)
5620 type = ct_REGISTER;
5621
5622 switch (type)
5623 {
5624 case ct_ARGS:
5625 /* Simple case first */
5626 if (*eap->arg == NUL)
5627 {
5628 if (quote == 1)
5629 {
5630 result = 2;
5631 if (buf != NULL)
5632 STRCPY(buf, "''");
5633 }
5634 else
5635 result = 0;
5636 break;
5637 }
5638
5639 /* When specified there is a single argument don't split it.
5640 * Works for ":Cmd %" when % is "a b c". */
5641 if ((eap->argt & NOSPC) && quote == 2)
5642 quote = 1;
5643
5644 switch (quote)
5645 {
5646 case 0: /* No quoting, no splitting */
5647 result = STRLEN(eap->arg);
5648 if (buf != NULL)
5649 STRCPY(buf, eap->arg);
5650 break;
5651 case 1: /* Quote, but don't split */
5652 result = STRLEN(eap->arg) + 2;
5653 for (p = eap->arg; *p; ++p)
5654 {
5655 if (*p == '\\' || *p == '"')
5656 ++result;
5657 }
5658
5659 if (buf != NULL)
5660 {
5661 *buf++ = '"';
5662 for (p = eap->arg; *p; ++p)
5663 {
5664 if (*p == '\\' || *p == '"')
5665 *buf++ = '\\';
5666 *buf++ = *p;
5667 }
5668 *buf = '"';
5669 }
5670
5671 break;
5672 case 2: /* Quote and split */
5673 /* This is hard, so only do it once, and cache the result */
5674 if (*split_buf == NULL)
5675 *split_buf = uc_split_args(eap->arg, split_len);
5676
5677 result = *split_len;
5678 if (buf != NULL && result != 0)
5679 STRCPY(buf, *split_buf);
5680
5681 break;
5682 }
5683 break;
5684
5685 case ct_BANG:
5686 result = eap->forceit ? 1 : 0;
5687 if (quote)
5688 result += 2;
5689 if (buf != NULL)
5690 {
5691 if (quote)
5692 *buf++ = '"';
5693 if (eap->forceit)
5694 *buf++ = '!';
5695 if (quote)
5696 *buf = '"';
5697 }
5698 break;
5699
5700 case ct_LINE1:
5701 case ct_LINE2:
5702 case ct_COUNT:
5703 {
5704 char num_buf[20];
5705 long num = (type == ct_LINE1) ? eap->line1 :
5706 (type == ct_LINE2) ? eap->line2 :
5707 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5708 size_t num_len;
5709
5710 sprintf(num_buf, "%ld", num);
5711 num_len = STRLEN(num_buf);
5712 result = num_len;
5713
5714 if (quote)
5715 result += 2;
5716
5717 if (buf != NULL)
5718 {
5719 if (quote)
5720 *buf++ = '"';
5721 STRCPY(buf, num_buf);
5722 buf += num_len;
5723 if (quote)
5724 *buf = '"';
5725 }
5726
5727 break;
5728 }
5729
5730 case ct_REGISTER:
5731 result = eap->regname ? 1 : 0;
5732 if (quote)
5733 result += 2;
5734 if (buf != NULL)
5735 {
5736 if (quote)
5737 *buf++ = '\'';
5738 if (eap->regname)
5739 *buf++ = eap->regname;
5740 if (quote)
5741 *buf = '\'';
5742 }
5743 break;
5744
5745 case ct_LT:
5746 result = 1;
5747 if (buf != NULL)
5748 *buf = '<';
5749 break;
5750
5751 default:
5752 /* Not recognized: just copy the '<' and return -1. */
5753 result = (size_t)-1;
5754 if (buf != NULL)
5755 *buf = '<';
5756 break;
5757 }
5758
5759 return result;
5760}
5761
5762 static void
5763do_ucmd(eap)
5764 exarg_T *eap;
5765{
5766 char_u *buf;
5767 char_u *p;
5768 char_u *q;
5769
5770 char_u *start;
5771 char_u *end;
5772 size_t len, totlen;
5773
5774 size_t split_len = 0;
5775 char_u *split_buf = NULL;
5776 ucmd_T *cmd;
5777#ifdef FEAT_EVAL
5778 scid_T save_current_SID = current_SID;
5779#endif
5780
5781 if (eap->cmdidx == CMD_USER)
5782 cmd = USER_CMD(eap->useridx);
5783 else
5784 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5785
5786 /*
5787 * Replace <> in the command by the arguments.
5788 */
5789 buf = NULL;
5790 for (;;)
5791 {
5792 p = cmd->uc_rep;
5793 q = buf;
5794 totlen = 0;
5795 while ((start = vim_strchr(p, '<')) != NULL
5796 && (end = vim_strchr(start + 1, '>')) != NULL)
5797 {
5798 /* Include the '>' */
5799 ++end;
5800
5801 /* Take everything up to the '<' */
5802 len = start - p;
5803 if (buf == NULL)
5804 totlen += len;
5805 else
5806 {
5807 mch_memmove(q, p, len);
5808 q += len;
5809 }
5810
5811 len = uc_check_code(start, end - start, q, cmd, eap,
5812 &split_buf, &split_len);
5813 if (len == (size_t)-1)
5814 {
5815 /* no match, continue after '<' */
5816 p = start + 1;
5817 len = 1;
5818 }
5819 else
5820 p = end;
5821 if (buf == NULL)
5822 totlen += len;
5823 else
5824 q += len;
5825 }
5826 if (buf != NULL) /* second time here, finished */
5827 {
5828 STRCPY(q, p);
5829 break;
5830 }
5831
5832 totlen += STRLEN(p); /* Add on the trailing characters */
5833 buf = alloc((unsigned)(totlen + 1));
5834 if (buf == NULL)
5835 {
5836 vim_free(split_buf);
5837 return;
5838 }
5839 }
5840
5841#ifdef FEAT_EVAL
5842 current_SID = cmd->uc_scriptID;
5843#endif
5844 (void)do_cmdline(buf, eap->getline, eap->cookie,
5845 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5846#ifdef FEAT_EVAL
5847 current_SID = save_current_SID;
5848#endif
5849 vim_free(buf);
5850 vim_free(split_buf);
5851}
5852
5853# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5854 static char_u *
5855get_user_command_name(idx)
5856 int idx;
5857{
5858 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5859}
5860
5861/*
5862 * Function given to ExpandGeneric() to obtain the list of user command names.
5863 */
5864/*ARGSUSED*/
5865 char_u *
5866get_user_commands(xp, idx)
5867 expand_T *xp;
5868 int idx;
5869{
5870 if (idx < curbuf->b_ucmds.ga_len)
5871 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5872 idx -= curbuf->b_ucmds.ga_len;
5873 if (idx < ucmds.ga_len)
5874 return USER_CMD(idx)->uc_name;
5875 return NULL;
5876}
5877
5878/*
5879 * Function given to ExpandGeneric() to obtain the list of user command
5880 * attributes.
5881 */
5882/*ARGSUSED*/
5883 char_u *
5884get_user_cmd_flags(xp, idx)
5885 expand_T *xp;
5886 int idx;
5887{
5888 static char *user_cmd_flags[] =
5889 {"bang", "bar", "buffer", "complete", "count",
5890 "nargs", "range", "register"};
5891
5892 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5893 return NULL;
5894 return (char_u *)user_cmd_flags[idx];
5895}
5896
5897/*
5898 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5899 */
5900/*ARGSUSED*/
5901 char_u *
5902get_user_cmd_nargs(xp, idx)
5903 expand_T *xp;
5904 int idx;
5905{
5906 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5907
5908 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5909 return NULL;
5910 return (char_u *)user_cmd_nargs[idx];
5911}
5912
5913/*
5914 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5915 */
5916/*ARGSUSED*/
5917 char_u *
5918get_user_cmd_complete(xp, idx)
5919 expand_T *xp;
5920 int idx;
5921{
5922 return (char_u *)command_complete[idx].name;
5923}
5924# endif /* FEAT_CMDL_COMPL */
5925
5926#endif /* FEAT_USR_CMDS */
5927
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005928#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
5929/*
5930 * Parse a completion argument "value[vallen]".
5931 * The detected completion goes in "*complp", argument type in "*argt".
5932 * When there is an argument, for function and user defined completion, it's
5933 * copied to allocated memory and stored in "*compl_arg".
5934 * Returns FAIL if something is wrong.
5935 */
5936 int
5937parse_compl_arg(value, vallen, complp, argt, compl_arg)
5938 char_u *value;
5939 int vallen;
5940 int *complp;
5941 long *argt;
5942 char_u **compl_arg;
5943{
5944 char_u *arg = NULL;
5945 size_t arglen = 0;
5946 int i;
5947 int valend = vallen;
5948
5949 /* Look for any argument part - which is the part after any ',' */
5950 for (i = 0; i < vallen; ++i)
5951 {
5952 if (value[i] == ',')
5953 {
5954 arg = &value[i + 1];
5955 arglen = vallen - i - 1;
5956 valend = i;
5957 break;
5958 }
5959 }
5960
5961 for (i = 0; command_complete[i].expand != 0; ++i)
5962 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005963 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005964 && STRNCMP(value, command_complete[i].name, valend) == 0)
5965 {
5966 *complp = command_complete[i].expand;
5967 if (command_complete[i].expand == EXPAND_BUFFERS)
5968 *argt |= BUFNAME;
5969 else if (command_complete[i].expand == EXPAND_DIRECTORIES
5970 || command_complete[i].expand == EXPAND_FILES)
5971 *argt |= XFILE;
5972 break;
5973 }
5974 }
5975
5976 if (command_complete[i].expand == 0)
5977 {
5978 EMSG2(_("E180: Invalid complete value: %s"), value);
5979 return FAIL;
5980 }
5981
5982# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5983 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
5984 && arg != NULL)
5985# else
5986 if (arg != NULL)
5987# endif
5988 {
5989 EMSG(_("E468: Completion argument only allowed for custom completion"));
5990 return FAIL;
5991 }
5992
5993# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5994 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
5995 && arg == NULL)
5996 {
5997 EMSG(_("E467: Custom completion requires a function argument"));
5998 return FAIL;
5999 }
6000
6001 if (arg != NULL)
6002 *compl_arg = vim_strnsave(arg, (int)arglen);
6003# endif
6004 return OK;
6005}
6006#endif
6007
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 static void
6009ex_colorscheme(eap)
6010 exarg_T *eap;
6011{
6012 if (load_colors(eap->arg) == FAIL)
6013 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6014}
6015
6016 static void
6017ex_highlight(eap)
6018 exarg_T *eap;
6019{
6020 if (*eap->arg == NUL && eap->cmd[2] == '!')
6021 MSG(_("Greetings, Vim user!"));
6022 do_highlight(eap->arg, eap->forceit, FALSE);
6023}
6024
6025
6026/*
6027 * Call this function if we thought we were going to exit, but we won't
6028 * (because of an error). May need to restore the terminal mode.
6029 */
6030 void
6031not_exiting()
6032{
6033 exiting = FALSE;
6034 settmode(TMODE_RAW);
6035}
6036
6037/*
6038 * ":quit": quit current window, quit Vim if closed the last window.
6039 */
6040 static void
6041ex_quit(eap)
6042 exarg_T *eap;
6043{
6044#ifdef FEAT_CMDWIN
6045 if (cmdwin_type != 0)
6046 {
6047 cmdwin_result = Ctrl_C;
6048 return;
6049 }
6050#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006051 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006052 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006053 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006054 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006055 return;
6056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057
6058#ifdef FEAT_NETBEANS_INTG
6059 netbeansForcedQuit = eap->forceit;
6060#endif
6061
6062 /*
6063 * If there are more files or windows we won't exit.
6064 */
6065 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6066 exiting = TRUE;
6067 if ((!P_HID(curbuf)
6068 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6069 || check_more(TRUE, eap->forceit) == FAIL
6070 || (only_one_window() && check_changed_any(eap->forceit)))
6071 {
6072 not_exiting();
6073 }
6074 else
6075 {
6076#ifdef FEAT_WINDOWS
6077 if (only_one_window()) /* quit last window */
6078#endif
6079 getout(0);
6080#ifdef FEAT_WINDOWS
6081# ifdef FEAT_GUI
6082 need_mouse_correct = TRUE;
6083# endif
6084 /* close window; may free buffer */
6085 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6086#endif
6087 }
6088}
6089
6090/*
6091 * ":cquit".
6092 */
6093/*ARGSUSED*/
6094 static void
6095ex_cquit(eap)
6096 exarg_T *eap;
6097{
6098 getout(1); /* this does not always pass on the exit code to the Manx
6099 compiler. why? */
6100}
6101
6102/*
6103 * ":qall": try to quit all windows
6104 */
6105 static void
6106ex_quit_all(eap)
6107 exarg_T *eap;
6108{
6109# ifdef FEAT_CMDWIN
6110 if (cmdwin_type != 0)
6111 {
6112 if (eap->forceit)
6113 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6114 else
6115 cmdwin_result = K_XF2;
6116 return;
6117 }
6118# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006119
6120 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006121 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006122 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006123 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006124 return;
6125 }
6126
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 exiting = TRUE;
6128 if (eap->forceit || !check_changed_any(FALSE))
6129 getout(0);
6130 not_exiting();
6131}
6132
6133#ifdef FEAT_WINDOWS
6134/*
6135 * ":close": close current window, unless it is the last one
6136 */
6137 static void
6138ex_close(eap)
6139 exarg_T *eap;
6140{
6141# ifdef FEAT_CMDWIN
6142 if (cmdwin_type != 0)
6143 cmdwin_result = K_IGNORE;
6144 else
6145# endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006146 if (!text_locked())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006147 ex_win_close(eap->forceit, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148}
6149
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006150#ifdef FEAT_QUICKFIX
6151/*
6152 * ":pclose": Close any preview window.
6153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006155ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006157{
6158 win_T *win;
6159
6160 for (win = firstwin; win != NULL; win = win->w_next)
6161 if (win->w_p_pvw)
6162 {
6163 ex_win_close(eap->forceit, win);
6164 break;
6165 }
6166}
6167#endif
6168
6169 static void
6170ex_win_close(forceit, win)
6171 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 win_T *win;
6173{
6174 int need_hide;
6175 buf_T *buf = win->w_buffer;
6176
6177 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006178 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 {
6180#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6181 if ((p_confirm || cmdmod.confirm) && p_write)
6182 {
6183 dialog_changed(buf, FALSE);
6184 if (buf_valid(buf) && bufIsChanged(buf))
6185 return;
6186 need_hide = FALSE;
6187 }
6188 else
6189#endif
6190 {
6191 EMSG(_(e_nowrtmsg));
6192 return;
6193 }
6194 }
6195
6196#ifdef FEAT_GUI
6197 need_mouse_correct = TRUE;
6198#endif
6199 /* free buffer when not hiding it or when it's a scratch buffer */
6200 win_close(win, !need_hide && !P_HID(buf));
6201}
6202
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006204 * ":tabclose": close current tab page, unless it is the last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 */
6206 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006207ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 exarg_T *eap;
6209{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006210# ifdef FEAT_CMDWIN
6211 if (cmdwin_type != 0)
6212 cmdwin_result = K_IGNORE;
6213 else
6214# endif
6215 if (!text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006217 if (first_tabpage->tp_next == NULL)
6218 EMSG(_("E999: Cannot close last tab page"));
6219 else
6220 {
6221 /* First close all the windows but the current one. If that
6222 * worked then close the last window in this tab, that will
6223 * close it. */
6224 ex_only(eap);
6225 if (lastwin == firstwin)
6226 ex_win_close(eap->forceit, curwin);
6227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 }
6229}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230
6231/*
6232 * ":only".
6233 */
6234 static void
6235ex_only(eap)
6236 exarg_T *eap;
6237{
6238# ifdef FEAT_GUI
6239 need_mouse_correct = TRUE;
6240# endif
6241 close_others(TRUE, eap->forceit);
6242}
6243
6244/*
6245 * ":all" and ":sall".
6246 */
6247 static void
6248ex_all(eap)
6249 exarg_T *eap;
6250{
6251 if (eap->addr_count == 0)
6252 eap->line2 = 9999;
6253 do_arg_all((int)eap->line2, eap->forceit);
6254}
6255#endif /* FEAT_WINDOWS */
6256
6257 static void
6258ex_hide(eap)
6259 exarg_T *eap;
6260{
6261 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6262 eap->errmsg = e_invarg;
6263 else
6264 {
6265 /* ":hide" or ":hide | cmd": hide current window */
6266 eap->nextcmd = check_nextcmd(eap->arg);
6267#ifdef FEAT_WINDOWS
6268 if (!eap->skip)
6269 {
6270# ifdef FEAT_GUI
6271 need_mouse_correct = TRUE;
6272# endif
6273 win_close(curwin, FALSE); /* don't free buffer */
6274 }
6275#endif
6276 }
6277}
6278
6279/*
6280 * ":stop" and ":suspend": Suspend Vim.
6281 */
6282 static void
6283ex_stop(eap)
6284 exarg_T *eap;
6285{
6286 /*
6287 * Disallow suspending for "rvim".
6288 */
6289 if (!check_restricted()
6290#ifdef WIN3264
6291 /*
6292 * Check if external commands are allowed now.
6293 */
6294 && can_end_termcap_mode(TRUE)
6295#endif
6296 )
6297 {
6298 if (!eap->forceit)
6299 autowrite_all();
6300 windgoto((int)Rows - 1, 0);
6301 out_char('\n');
6302 out_flush();
6303 stoptermcap();
6304 out_flush(); /* needed for SUN to restore xterm buffer */
6305#ifdef FEAT_TITLE
6306 mch_restore_title(3); /* restore window titles */
6307#endif
6308 ui_suspend(); /* call machine specific function */
6309#ifdef FEAT_TITLE
6310 maketitle();
6311 resettitle(); /* force updating the title */
6312#endif
6313 starttermcap();
6314 scroll_start(); /* scroll screen before redrawing */
6315 redraw_later_clear();
6316 shell_resized(); /* may have resized window */
6317 }
6318}
6319
6320/*
6321 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6322 */
6323 static void
6324ex_exit(eap)
6325 exarg_T *eap;
6326{
6327#ifdef FEAT_CMDWIN
6328 if (cmdwin_type != 0)
6329 {
6330 cmdwin_result = Ctrl_C;
6331 return;
6332 }
6333#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006334 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006335 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006336 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006337 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006338 return;
6339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340
6341 /*
6342 * if more files or windows we won't exit
6343 */
6344 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6345 exiting = TRUE;
6346 if ( ((eap->cmdidx == CMD_wq
6347 || curbufIsChanged())
6348 && do_write(eap) == FAIL)
6349 || check_more(TRUE, eap->forceit) == FAIL
6350 || (only_one_window() && check_changed_any(eap->forceit)))
6351 {
6352 not_exiting();
6353 }
6354 else
6355 {
6356#ifdef FEAT_WINDOWS
6357 if (only_one_window()) /* quit last window, exit Vim */
6358#endif
6359 getout(0);
6360#ifdef FEAT_WINDOWS
6361# ifdef FEAT_GUI
6362 need_mouse_correct = TRUE;
6363# endif
6364 /* quit current window, may free buffer */
6365 win_close(curwin, !P_HID(curwin->w_buffer));
6366#endif
6367 }
6368}
6369
6370/*
6371 * ":print", ":list", ":number".
6372 */
6373 static void
6374ex_print(eap)
6375 exarg_T *eap;
6376{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006377 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6378 EMSG(_(e_emptybuf));
6379 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006381 for ( ;!got_int; ui_breakcheck())
6382 {
6383 print_line(eap->line1,
6384 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6385 || (eap->flags & EXFLAG_NR)),
6386 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6387 if (++eap->line1 > eap->line2)
6388 break;
6389 out_flush(); /* show one line at a time */
6390 }
6391 setpcmark();
6392 /* put cursor at last line */
6393 curwin->w_cursor.lnum = eap->line2;
6394 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 }
6396
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398}
6399
6400#ifdef FEAT_BYTEOFF
6401 static void
6402ex_goto(eap)
6403 exarg_T *eap;
6404{
6405 goto_byte(eap->line2);
6406}
6407#endif
6408
6409/*
6410 * ":shell".
6411 */
6412/*ARGSUSED*/
6413 static void
6414ex_shell(eap)
6415 exarg_T *eap;
6416{
6417 do_shell(NULL, 0);
6418}
6419
6420#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6421 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006422 || defined(FEAT_GUI_MSWIN) \
6423 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 || defined(PROTO)
6425
6426/*
6427 * Handle a file drop. The code is here because a drop is *nearly* like an
6428 * :args command, but not quite (we have a list of exact filenames, so we
6429 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6430 * code is very similar to :args and hence needs access to a lot of the static
6431 * functions in this file.
6432 *
6433 * The list should be allocated using alloc(), as should each item in the
6434 * list. This function takes over responsibility for freeing the list.
6435 *
6436 * XXX The list is made into the arggument list. This is freed using
6437 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6438 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6439 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6440 * file functionality is (currently) not in EMX this is not presently a
6441 * problem.
6442 */
6443 void
6444handle_drop(filec, filev, split)
6445 int filec; /* the number of files dropped */
6446 char_u **filev; /* the list of files dropped */
6447 int split; /* force splitting the window */
6448{
6449 exarg_T ea;
6450 int save_msg_scroll = msg_scroll;
6451
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006452 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006453 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455
6456 /* Check whether the current buffer is changed. If so, we will need
6457 * to split the current window or data could be lost.
6458 * We don't need to check if the 'hidden' option is set, as in this
6459 * case the buffer won't be lost.
6460 */
6461 if (!P_HID(curbuf) && !split)
6462 {
6463 ++emsg_off;
6464 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6465 --emsg_off;
6466 }
6467 if (split)
6468 {
6469# ifdef FEAT_WINDOWS
6470 if (win_split(0, 0) == FAIL)
6471 return;
6472# ifdef FEAT_SCROLLBIND
6473 curwin->w_p_scb = FALSE;
6474# endif
6475
6476 /* When splitting the window, create a new alist. Otherwise the
6477 * existing one is overwritten. */
6478 alist_unlink(curwin->w_alist);
6479 alist_new();
6480# else
6481 return; /* can't split, always fail */
6482# endif
6483 }
6484
6485 /*
6486 * Set up the new argument list.
6487 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006488 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489
6490 /*
6491 * Move to the first file.
6492 */
6493 /* Fake up a minimal "next" command for do_argfile() */
6494 vim_memset(&ea, 0, sizeof(ea));
6495 ea.cmd = (char_u *)"next";
6496 do_argfile(&ea, 0);
6497
6498 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6499 * mode that is not needed here. */
6500 need_start_insertmode = FALSE;
6501
6502 /* Restore msg_scroll, otherwise a following command may cause scrolling
6503 * unexpectedly. The screen will be redrawn by the caller, thus
6504 * msg_scroll being set by displaying a message is irrelevant. */
6505 msg_scroll = save_msg_scroll;
6506}
6507#endif
6508
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509/*
6510 * Clear an argument list: free all file names and reset it to zero entries.
6511 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006512 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513alist_clear(al)
6514 alist_T *al;
6515{
6516 while (--al->al_ga.ga_len >= 0)
6517 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6518 ga_clear(&al->al_ga);
6519}
6520
6521/*
6522 * Init an argument list.
6523 */
6524 void
6525alist_init(al)
6526 alist_T *al;
6527{
6528 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6529}
6530
6531#if defined(FEAT_WINDOWS) || defined(PROTO)
6532
6533/*
6534 * Remove a reference from an argument list.
6535 * Ignored when the argument list is the global one.
6536 * If the argument list is no longer used by any window, free it.
6537 */
6538 void
6539alist_unlink(al)
6540 alist_T *al;
6541{
6542 if (al != &global_alist && --al->al_refcount <= 0)
6543 {
6544 alist_clear(al);
6545 vim_free(al);
6546 }
6547}
6548
6549# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6550/*
6551 * Create a new argument list and use it for the current window.
6552 */
6553 void
6554alist_new()
6555{
6556 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6557 if (curwin->w_alist == NULL)
6558 {
6559 curwin->w_alist = &global_alist;
6560 ++global_alist.al_refcount;
6561 }
6562 else
6563 {
6564 curwin->w_alist->al_refcount = 1;
6565 alist_init(curwin->w_alist);
6566 }
6567}
6568# endif
6569#endif
6570
6571#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6572/*
6573 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006574 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6575 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 */
6577 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006578alist_expand(fnum_list, fnum_len)
6579 int *fnum_list;
6580 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581{
6582 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006583 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 char_u **new_arg_files;
6585 int new_arg_file_count;
6586 char_u *save_p_su = p_su;
6587 int i;
6588
6589 /* Don't use 'suffixes' here. This should work like the shell did the
6590 * expansion. Also, the vimrc file isn't read yet, thus the user
6591 * can't set the options. */
6592 p_su = empty_option;
6593 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6594 if (old_arg_files != NULL)
6595 {
6596 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006597 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6598 old_arg_count = GARGCOUNT;
6599 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 &new_arg_file_count, &new_arg_files,
6601 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6602 && new_arg_file_count > 0)
6603 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006604 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6605 TRUE, fnum_list, fnum_len);
6606 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 }
6609 p_su = save_p_su;
6610}
6611#endif
6612
6613/*
6614 * Set the argument list for the current window.
6615 * Takes over the allocated files[] and the allocated fnames in it.
6616 */
6617 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006618alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 alist_T *al;
6620 int count;
6621 char_u **files;
6622 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006623 int *fnum_list;
6624 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 int i;
6627
6628 alist_clear(al);
6629 if (ga_grow(&al->al_ga, count) == OK)
6630 {
6631 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006632 {
6633 if (got_int)
6634 {
6635 /* When adding many buffers this can take a long time. Allow
6636 * interrupting here. */
6637 while (i < count)
6638 vim_free(files[i++]);
6639 break;
6640 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006641
6642 /* May set buffer name of a buffer previously used for the
6643 * argument list, so that it's re-used by alist_add. */
6644 if (fnum_list != NULL && i < fnum_len)
6645 buf_set_name(fnum_list[i], files[i]);
6646
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006648 ui_breakcheck();
6649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 vim_free(files);
6651 }
6652 else
6653 FreeWild(count, files);
6654#ifdef FEAT_WINDOWS
6655 if (al == &global_alist)
6656#endif
6657 arg_had_last = FALSE;
6658}
6659
6660/*
6661 * Add file "fname" to argument list "al".
6662 * "fname" must have been allocated and "al" must have been checked for room.
6663 */
6664 void
6665alist_add(al, fname, set_fnum)
6666 alist_T *al;
6667 char_u *fname;
6668 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6669{
6670 if (fname == NULL) /* don't add NULL file names */
6671 return;
6672#ifdef BACKSLASH_IN_FILENAME
6673 slash_adjust(fname);
6674#endif
6675 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6676 if (set_fnum > 0)
6677 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6678 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6679 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680}
6681
6682#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6683/*
6684 * Adjust slashes in file names. Called after 'shellslash' was set.
6685 */
6686 void
6687alist_slash_adjust()
6688{
6689 int i;
6690# ifdef FEAT_WINDOWS
6691 win_T *wp;
6692# endif
6693
6694 for (i = 0; i < GARGCOUNT; ++i)
6695 if (GARGLIST[i].ae_fname != NULL)
6696 slash_adjust(GARGLIST[i].ae_fname);
6697# ifdef FEAT_WINDOWS
6698 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6699 if (wp->w_alist != &global_alist)
6700 for (i = 0; i < WARGCOUNT(wp); ++i)
6701 if (WARGLIST(wp)[i].ae_fname != NULL)
6702 slash_adjust(WARGLIST(wp)[i].ae_fname);
6703# endif
6704}
6705#endif
6706
6707/*
6708 * ":preserve".
6709 */
6710/*ARGSUSED*/
6711 static void
6712ex_preserve(eap)
6713 exarg_T *eap;
6714{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006715 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 ml_preserve(curbuf, TRUE);
6717}
6718
6719/*
6720 * ":recover".
6721 */
6722 static void
6723ex_recover(eap)
6724 exarg_T *eap;
6725{
6726 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6727 recoverymode = TRUE;
6728 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6729 && (*eap->arg == NUL
6730 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6731 ml_recover();
6732 recoverymode = FALSE;
6733}
6734
6735/*
6736 * Command modifier used in a wrong way.
6737 */
6738 static void
6739ex_wrongmodifier(eap)
6740 exarg_T *eap;
6741{
6742 eap->errmsg = e_invcmd;
6743}
6744
6745#ifdef FEAT_WINDOWS
6746/*
6747 * :sview [+command] file split window with new file, read-only
6748 * :split [[+command] file] split window with current or new file
6749 * :vsplit [[+command] file] split window vertically with current or new file
6750 * :new [[+command] file] split window with no or new file
6751 * :vnew [[+command] file] split vertically window with no or new file
6752 * :sfind [+command] file split window with file in 'path'
6753 */
6754 void
6755ex_splitview(eap)
6756 exarg_T *eap;
6757{
6758 win_T *old_curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006759# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006761# endif
6762# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006764# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006766# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6768 {
6769 ex_ni(eap);
6770 return;
6771 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006772# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773
6774 old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006775# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006779# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 /* A ":split" in the quickfix window works like ":new". Don't want two
6781 * quickfix windows. */
6782 if (bt_quickfix(curbuf))
6783 {
6784 if (eap->cmdidx == CMD_split)
6785 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006786# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 if (eap->cmdidx == CMD_vsplit)
6788 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006789# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006791# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006793# ifdef FEAT_SEARCHPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 if (eap->cmdidx == CMD_sfind)
6795 {
6796 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6797 FNAME_MESS, TRUE, curbuf->b_ffname);
6798 if (fname == NULL)
6799 goto theend;
6800 eap->arg = fname;
6801 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006802# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006806# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006808# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006810# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 && eap->cmdidx != CMD_new)
6812 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006813 if (
6814# ifdef FEAT_GUI
6815 !gui.in_use &&
6816# endif
6817 au_has_group((char_u *)"FileExplorer"))
6818 {
6819 /* No browsing supported but we do have the file explorer:
6820 * Edit the directory. */
6821 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6822 eap->arg = (char_u *)".";
6823 }
6824 else
6825 {
6826 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006828 if (fname == NULL)
6829 goto theend;
6830 eap->arg = fname;
6831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 }
6833 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006834# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835
6836 if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
6837 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6838 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006839# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 /* Reset 'scrollbind' when editing another file, but keep it when
6841 * doing ":split" without arguments. */
6842 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006843# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006845# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 )
6847 curwin->w_p_scb = FALSE;
6848 else
6849 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006850# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 do_exedit(eap, old_curwin);
6852 }
6853
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006854# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006858# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859theend:
6860 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006861# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006863
6864/*
6865 * :tabedit [[+command] file] open new Tab page with empty window
6866 * :tabedit [[+command] file] open new Tab page and edit "file"
6867 * :tabfind [[+command] file] open new Tab page and find "file"
6868 */
6869 void
6870ex_tabedit(eap)
6871 exarg_T *eap;
6872{
6873# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6874 char_u *fname = NULL;
6875# endif
6876# ifdef FEAT_BROWSE
6877 int browse_flag = cmdmod.browse;
6878# endif
6879
6880# ifdef FEAT_GUI
6881 need_mouse_correct = TRUE;
6882# endif
6883
6884# ifdef FEAT_SEARCHPATH
6885 if (eap->cmdidx == CMD_tabfind)
6886 {
6887 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6888 FNAME_MESS, TRUE, curbuf->b_ffname);
6889 if (fname == NULL)
6890 goto theend;
6891 eap->arg = fname;
6892 }
6893# ifdef FEAT_BROWSE
6894 else
6895# endif
6896# endif
6897# ifdef FEAT_BROWSE
6898 if (cmdmod.browse)
6899 {
6900 if (
6901# ifdef FEAT_GUI
6902 !gui.in_use &&
6903# endif
6904 au_has_group((char_u *)"FileExplorer"))
6905 {
6906 /* No browsing supported but we do have the file explorer:
6907 * Edit the directory. */
6908 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6909 eap->arg = (char_u *)".";
6910 }
6911 else
6912 {
6913 fname = do_browse(0, (char_u *)_("Edit File in new tab page"),
6914 eap->arg, NULL, NULL, NULL, curbuf);
6915 if (fname == NULL)
6916 goto theend;
6917 eap->arg = fname;
6918 }
6919 }
6920 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
6921# endif
6922
6923 if (win_new_tabpage() != FAIL)
6924 {
6925# ifdef FEAT_SCROLLBIND
6926 curwin->w_p_scb = FALSE;
6927# endif
6928 do_exedit(eap, NULL);
6929 }
6930
6931# ifdef FEAT_BROWSE
6932 cmdmod.browse = browse_flag;
6933# endif
6934
6935# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6936theend:
6937 vim_free(fname);
6938# endif
6939}
6940
6941/*
6942 * :tab command
6943 */
6944 void
6945ex_tab(eap)
6946 exarg_T *eap;
6947{
6948 goto_tabpage((int)eap->line2);
6949}
6950
6951/*
6952 * :tabs command: List tabs and their contents.
6953 */
6954/*ARGSUSED*/
6955 static void
6956ex_tabs(eap)
6957 exarg_T *eap;
6958{
6959 tabpage_T *tp;
6960 win_T *wp;
6961 int tabcount = 1;
6962
6963 msg_start();
6964 msg_scroll = TRUE;
6965 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6966 {
6967 msg_putchar('\n');
6968 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
6969 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
6970 out_flush(); /* output one line at a time */
6971 ui_breakcheck();
6972
6973 if (tp->tp_topframe == topframe)
6974 wp = firstwin;
6975 else
6976 wp = tp->tp_firstwin;
6977 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6978 {
6979 msg_puts((char_u *)"\n ");
6980 if (buf_spname(wp->w_buffer) != NULL)
6981 STRCPY(IObuff, buf_spname(wp->w_buffer));
6982 else
6983 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6984 IObuff, IOSIZE, TRUE);
6985 msg_outtrans(IObuff);
6986 out_flush(); /* output one line at a time */
6987 ui_breakcheck();
6988 }
6989 }
6990}
6991
6992#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993
6994/*
6995 * ":mode": Set screen mode.
6996 * If no argument given, just get the screen size and redraw.
6997 */
6998 static void
6999ex_mode(eap)
7000 exarg_T *eap;
7001{
7002 if (*eap->arg == NUL)
7003 shell_resized();
7004 else
7005 mch_screenmode(eap->arg);
7006}
7007
7008#ifdef FEAT_WINDOWS
7009/*
7010 * ":resize".
7011 * set, increment or decrement current window height
7012 */
7013 static void
7014ex_resize(eap)
7015 exarg_T *eap;
7016{
7017 int n;
7018 win_T *wp = curwin;
7019
7020 if (eap->addr_count > 0)
7021 {
7022 n = eap->line2;
7023 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7024 ;
7025 }
7026
7027#ifdef FEAT_GUI
7028 need_mouse_correct = TRUE;
7029#endif
7030 n = atol((char *)eap->arg);
7031#ifdef FEAT_VERTSPLIT
7032 if (cmdmod.split & WSP_VERT)
7033 {
7034 if (*eap->arg == '-' || *eap->arg == '+')
7035 n += W_WIDTH(curwin);
7036 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7037 n = 9999;
7038 win_setwidth_win((int)n, wp);
7039 }
7040 else
7041#endif
7042 {
7043 if (*eap->arg == '-' || *eap->arg == '+')
7044 n += curwin->w_height;
7045 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7046 n = 9999;
7047 win_setheight_win((int)n, wp);
7048 }
7049}
7050#endif
7051
7052/*
7053 * ":find [+command] <file>" command.
7054 */
7055 static void
7056ex_find(eap)
7057 exarg_T *eap;
7058{
7059#ifdef FEAT_SEARCHPATH
7060 char_u *fname;
7061 int count;
7062
7063 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7064 TRUE, curbuf->b_ffname);
7065 if (eap->addr_count > 0)
7066 {
7067 /* Repeat finding the file "count" times. This matters when it
7068 * appears several times in the path. */
7069 count = eap->line2;
7070 while (fname != NULL && --count > 0)
7071 {
7072 vim_free(fname);
7073 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7074 FALSE, curbuf->b_ffname);
7075 }
7076 }
7077
7078 if (fname != NULL)
7079 {
7080 eap->arg = fname;
7081#endif
7082 do_exedit(eap, NULL);
7083#ifdef FEAT_SEARCHPATH
7084 vim_free(fname);
7085 }
7086#endif
7087}
7088
7089/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007090 * ":open" simulation: for now just work like ":visual".
7091 */
7092 static void
7093ex_open(eap)
7094 exarg_T *eap;
7095{
7096 regmatch_T regmatch;
7097 char_u *p;
7098
7099 curwin->w_cursor.lnum = eap->line2;
7100 beginline(BL_SOL | BL_FIX);
7101 if (*eap->arg == '/')
7102 {
7103 /* ":open /pattern/": put cursor in column found with pattern */
7104 ++eap->arg;
7105 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7106 *p = NUL;
7107 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7108 if (regmatch.regprog != NULL)
7109 {
7110 regmatch.rm_ic = p_ic;
7111 p = ml_get_curline();
7112 if (vim_regexec(&regmatch, p, (colnr_T)0))
7113 curwin->w_cursor.col = regmatch.startp[0] - p;
7114 else
7115 EMSG(_(e_nomatch));
7116 vim_free(regmatch.regprog);
7117 }
7118 /* Move to the NUL, ignore any other arguments. */
7119 eap->arg += STRLEN(eap->arg);
7120 }
7121 check_cursor();
7122
7123 eap->cmdidx = CMD_visual;
7124 do_exedit(eap, NULL);
7125}
7126
7127/*
7128 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 */
7130 static void
7131ex_edit(eap)
7132 exarg_T *eap;
7133{
7134 do_exedit(eap, NULL);
7135}
7136
7137/*
7138 * ":edit <file>" command and alikes.
7139 */
7140/*ARGSUSED*/
7141 void
7142do_exedit(eap, old_curwin)
7143 exarg_T *eap;
7144 win_T *old_curwin; /* curwin before doing a split or NULL */
7145{
7146 int n;
7147#ifdef FEAT_WINDOWS
7148 int need_hide;
7149#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007150 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151
7152 /*
7153 * ":vi" command ends Ex mode.
7154 */
7155 if (exmode_active && (eap->cmdidx == CMD_visual
7156 || eap->cmdidx == CMD_view))
7157 {
7158 exmode_active = FALSE;
7159 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007160 {
7161 /* Special case: ":global/pat/visual\NLvi-commands" */
7162 if (global_busy)
7163 {
7164 int rd = RedrawingDisabled;
7165 int nwr = no_wait_return;
7166 int ms = msg_scroll;
7167#ifdef FEAT_GUI
7168 int he = hold_gui_events;
7169#endif
7170
7171 if (eap->nextcmd != NULL)
7172 {
7173 stuffReadbuff(eap->nextcmd);
7174 eap->nextcmd = NULL;
7175 }
7176
7177 if (exmode_was != EXMODE_VIM)
7178 settmode(TMODE_RAW);
7179 RedrawingDisabled = 0;
7180 no_wait_return = 0;
7181 need_wait_return = FALSE;
7182 msg_scroll = 0;
7183#ifdef FEAT_GUI
7184 hold_gui_events = 0;
7185#endif
7186 must_redraw = CLEAR;
7187
7188 main_loop(FALSE, TRUE);
7189
7190 RedrawingDisabled = rd;
7191 no_wait_return = nwr;
7192 msg_scroll = ms;
7193#ifdef FEAT_GUI
7194 hold_gui_events = he;
7195#endif
7196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 }
7200
7201 if ((eap->cmdidx == CMD_new
7202#ifdef FEAT_VERTSPLIT
7203 || eap->cmdidx == CMD_vnew
7204#endif
7205 ) && *eap->arg == NUL)
7206 {
7207 /* ":new" without argument: edit an new empty buffer */
7208 setpcmark();
7209 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
7210 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
7211 }
7212 else if ((eap->cmdidx != CMD_split
7213#ifdef FEAT_VERTSPLIT
7214 && eap->cmdidx != CMD_vsplit
7215#endif
7216 )
7217 || *eap->arg != NUL
7218#ifdef FEAT_BROWSE
7219 || cmdmod.browse
7220#endif
7221 )
7222 {
7223 n = readonlymode;
7224 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7225 readonlymode = TRUE;
7226 else if (eap->cmdidx == CMD_enew)
7227 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7228 empty buffer */
7229 setpcmark();
7230 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7231 NULL, eap,
7232 /* ":edit" goes to first line if Vi compatible */
7233 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7234 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7235 ? ECMD_ONE : eap->do_ecmd_lnum,
7236 (P_HID(curbuf) ? ECMD_HIDE : 0)
7237 + (eap->forceit ? ECMD_FORCEIT : 0)
7238#ifdef FEAT_LISTCMDS
7239 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7240#endif
7241 ) == FAIL)
7242 {
7243 /* Editing the file failed. If the window was split, close it. */
7244#ifdef FEAT_WINDOWS
7245 if (old_curwin != NULL)
7246 {
7247 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7248 if (!need_hide || P_HID(curbuf))
7249 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007250# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7251 cleanup_T cs;
7252
7253 /* Reset the error/interrupt/exception state here so that
7254 * aborting() returns FALSE when closing a window. */
7255 enter_cleanup(&cs);
7256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257# ifdef FEAT_GUI
7258 need_mouse_correct = TRUE;
7259# endif
7260 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007261
7262# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7263 /* Restore the error/interrupt/exception state if not
7264 * discarded by a new aborting error, interrupt, or
7265 * uncaught exception. */
7266 leave_cleanup(&cs);
7267# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 }
7269 }
7270#endif
7271 }
7272 else if (readonlymode && curbuf->b_nwindows == 1)
7273 {
7274 /* When editing an already visited buffer, 'readonly' won't be set
7275 * but the previous value is kept. With ":view" and ":sview" we
7276 * want the file to be readonly, except when another window is
7277 * editing the same buffer. */
7278 curbuf->b_p_ro = TRUE;
7279 }
7280 readonlymode = n;
7281 }
7282 else
7283 {
7284 if (eap->do_ecmd_cmd != NULL)
7285 do_cmdline_cmd(eap->do_ecmd_cmd);
7286#ifdef FEAT_TITLE
7287 n = curwin->w_arg_idx_invalid;
7288#endif
7289 check_arg_idx(curwin);
7290#ifdef FEAT_TITLE
7291 if (n != curwin->w_arg_idx_invalid)
7292 maketitle();
7293#endif
7294 }
7295
7296#ifdef FEAT_WINDOWS
7297 /*
7298 * if ":split file" worked, set alternate file name in old window to new
7299 * file
7300 */
7301 if (old_curwin != NULL
7302 && *eap->arg != NUL
7303 && curwin != old_curwin
7304 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007305 && old_curwin->w_buffer != curbuf
7306 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 old_curwin->w_alt_fnum = curbuf->b_fnum;
7308#endif
7309
7310 ex_no_reprint = TRUE;
7311}
7312
7313#ifndef FEAT_GUI
7314/*
7315 * ":gui" and ":gvim" when there is no GUI.
7316 */
7317 static void
7318ex_nogui(eap)
7319 exarg_T *eap;
7320{
7321 eap->errmsg = e_nogvim;
7322}
7323#endif
7324
7325#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7326 static void
7327ex_tearoff(eap)
7328 exarg_T *eap;
7329{
7330 gui_make_tearoff(eap->arg);
7331}
7332#endif
7333
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007334#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 static void
7336ex_popup(eap)
7337 exarg_T *eap;
7338{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007339 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340}
7341#endif
7342
7343/*ARGSUSED*/
7344 static void
7345ex_swapname(eap)
7346 exarg_T *eap;
7347{
7348 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7349 MSG(_("No swap file"));
7350 else
7351 msg(curbuf->b_ml.ml_mfp->mf_fname);
7352}
7353
7354/*
7355 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7356 * offset.
7357 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7358 */
7359/*ARGSUSED*/
7360 static void
7361ex_syncbind(eap)
7362 exarg_T *eap;
7363{
7364#ifdef FEAT_SCROLLBIND
7365 win_T *wp;
7366 long topline;
7367 long y;
7368 linenr_T old_linenr = curwin->w_cursor.lnum;
7369
7370 setpcmark();
7371
7372 /*
7373 * determine max topline
7374 */
7375 if (curwin->w_p_scb)
7376 {
7377 topline = curwin->w_topline;
7378 for (wp = firstwin; wp; wp = wp->w_next)
7379 {
7380 if (wp->w_p_scb && wp->w_buffer)
7381 {
7382 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7383 if (topline > y)
7384 topline = y;
7385 }
7386 }
7387 if (topline < 1)
7388 topline = 1;
7389 }
7390 else
7391 {
7392 topline = 1;
7393 }
7394
7395
7396 /*
7397 * set all scrollbind windows to the same topline
7398 */
7399 wp = curwin;
7400 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7401 {
7402 if (curwin->w_p_scb)
7403 {
7404 y = topline - curwin->w_topline;
7405 if (y > 0)
7406 scrollup(y, TRUE);
7407 else
7408 scrolldown(-y, TRUE);
7409 curwin->w_scbind_pos = topline;
7410 redraw_later(VALID);
7411 cursor_correct();
7412#ifdef FEAT_WINDOWS
7413 curwin->w_redr_status = TRUE;
7414#endif
7415 }
7416 }
7417 curwin = wp;
7418 if (curwin->w_p_scb)
7419 {
7420 did_syncbind = TRUE;
7421 checkpcmark();
7422 if (old_linenr != curwin->w_cursor.lnum)
7423 {
7424 char_u ctrl_o[2];
7425
7426 ctrl_o[0] = Ctrl_O;
7427 ctrl_o[1] = 0;
7428 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7429 }
7430 }
7431#endif
7432}
7433
7434
7435 static void
7436ex_read(eap)
7437 exarg_T *eap;
7438{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007439 int i;
7440 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7441 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442
7443 if (eap->usefilter) /* :r!cmd */
7444 do_bang(1, eap, FALSE, FALSE, TRUE);
7445 else
7446 {
7447 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7448 return;
7449
7450#ifdef FEAT_BROWSE
7451 if (cmdmod.browse)
7452 {
7453 char_u *browseFile;
7454
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007455 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 NULL, NULL, NULL, curbuf);
7457 if (browseFile != NULL)
7458 {
7459 i = readfile(browseFile, NULL,
7460 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7461 vim_free(browseFile);
7462 }
7463 else
7464 i = OK;
7465 }
7466 else
7467#endif
7468 if (*eap->arg == NUL)
7469 {
7470 if (check_fname() == FAIL) /* check for no file name */
7471 return;
7472 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7473 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7474 }
7475 else
7476 {
7477 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7478 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7479 i = readfile(eap->arg, NULL,
7480 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7481
7482 }
7483 if (i == FAIL)
7484 {
7485#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7486 if (!aborting())
7487#endif
7488 EMSG2(_(e_notopen), eap->arg);
7489 }
7490 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007491 {
7492 if (empty && exmode_active)
7493 {
7494 /* Delete the empty line that remains. Historically ex does
7495 * this but vi doesn't. */
7496 if (eap->line2 == 0)
7497 lnum = curbuf->b_ml.ml_line_count;
7498 else
7499 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007500 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007501 {
7502 ml_delete(lnum, FALSE);
7503 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007504 if (curwin->w_cursor.lnum > 1
7505 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007506 --curwin->w_cursor.lnum;
7507 }
7508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 }
7512}
7513
Bram Moolenaarea408852005-06-25 22:49:46 +00007514static char_u *prev_dir = NULL;
7515
7516#if defined(EXITFREE) || defined(PROTO)
7517 void
7518free_cd_dir()
7519{
7520 vim_free(prev_dir);
7521}
7522#endif
7523
7524
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525/*
7526 * ":cd", ":lcd", ":chdir" and ":lchdir".
7527 */
7528 static void
7529ex_cd(eap)
7530 exarg_T *eap;
7531{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 char_u *new_dir;
7533 char_u *tofree;
7534
7535 new_dir = eap->arg;
7536#if !defined(UNIX) && !defined(VMS)
7537 /* for non-UNIX ":cd" means: print current directory */
7538 if (*new_dir == NUL)
7539 ex_pwd(NULL);
7540 else
7541#endif
7542 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007543 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7544 && !eap->forceit)
7545 {
7546 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7547 return;
7548 }
7549
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 /* ":cd -": Change to previous directory */
7551 if (STRCMP(new_dir, "-") == 0)
7552 {
7553 if (prev_dir == NULL)
7554 {
7555 EMSG(_("E186: No previous directory"));
7556 return;
7557 }
7558 new_dir = prev_dir;
7559 }
7560
7561 /* Save current directory for next ":cd -" */
7562 tofree = prev_dir;
7563 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7564 prev_dir = vim_strsave(NameBuff);
7565 else
7566 prev_dir = NULL;
7567
7568#if defined(UNIX) || defined(VMS)
7569 /* for UNIX ":cd" means: go to home directory */
7570 if (*new_dir == NUL)
7571 {
7572 /* use NameBuff for home directory name */
7573# ifdef VMS
7574 char_u *p;
7575
7576 p = mch_getenv((char_u *)"SYS$LOGIN");
7577 if (p == NULL || *p == NUL) /* empty is the same as not set */
7578 NameBuff[0] = NUL;
7579 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007580 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581# else
7582 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7583# endif
7584 new_dir = NameBuff;
7585 }
7586#endif
7587 if (new_dir == NULL || vim_chdir(new_dir))
7588 EMSG(_(e_failed));
7589 else
7590 {
7591 vim_free(curwin->w_localdir);
7592 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7593 {
7594 /* If still in global directory, need to remember current
7595 * directory as global directory. */
7596 if (globaldir == NULL && prev_dir != NULL)
7597 globaldir = vim_strsave(prev_dir);
7598 /* Remember this local directory for the window. */
7599 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7600 curwin->w_localdir = vim_strsave(NameBuff);
7601 }
7602 else
7603 {
7604 /* We are now in the global directory, no need to remember its
7605 * name. */
7606 vim_free(globaldir);
7607 globaldir = NULL;
7608 curwin->w_localdir = NULL;
7609 }
7610
7611 shorten_fnames(TRUE);
7612
7613 /* Echo the new current directory if the command was typed. */
7614 if (KeyTyped)
7615 ex_pwd(eap);
7616 }
7617 vim_free(tofree);
7618 }
7619}
7620
7621/*
7622 * ":pwd".
7623 */
7624/*ARGSUSED*/
7625 static void
7626ex_pwd(eap)
7627 exarg_T *eap;
7628{
7629 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7630 {
7631#ifdef BACKSLASH_IN_FILENAME
7632 slash_adjust(NameBuff);
7633#endif
7634 msg(NameBuff);
7635 }
7636 else
7637 EMSG(_("E187: Unknown"));
7638}
7639
7640/*
7641 * ":=".
7642 */
7643 static void
7644ex_equal(eap)
7645 exarg_T *eap;
7646{
7647 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007648 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649}
7650
7651 static void
7652ex_sleep(eap)
7653 exarg_T *eap;
7654{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007655 int n;
7656 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657
7658 if (cursor_valid())
7659 {
7660 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7661 if (n >= 0)
7662 windgoto((int)n, curwin->w_wcol);
7663 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007664
7665 len = eap->line2;
7666 switch (*eap->arg)
7667 {
7668 case 'm': break;
7669 case NUL: len *= 1000L; break;
7670 default: EMSG2(_(e_invarg2), eap->arg); return;
7671 }
7672 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673}
7674
7675/*
7676 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7677 */
7678 void
7679do_sleep(msec)
7680 long msec;
7681{
7682 long done;
7683
7684 cursor_on();
7685 out_flush();
7686 for (done = 0; !got_int && done < msec; done += 1000L)
7687 {
7688 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7689 ui_breakcheck();
7690 }
7691}
7692
7693 static void
7694do_exmap(eap, isabbrev)
7695 exarg_T *eap;
7696 int isabbrev;
7697{
7698 int mode;
7699 char_u *cmdp;
7700
7701 cmdp = eap->cmd;
7702 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7703
7704 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7705 eap->arg, mode, isabbrev))
7706 {
7707 case 1: EMSG(_(e_invarg));
7708 break;
7709 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7710 break;
7711 }
7712}
7713
7714/*
7715 * ":winsize" command (obsolete).
7716 */
7717 static void
7718ex_winsize(eap)
7719 exarg_T *eap;
7720{
7721 int w, h;
7722 char_u *arg = eap->arg;
7723 char_u *p;
7724
7725 w = getdigits(&arg);
7726 arg = skipwhite(arg);
7727 p = arg;
7728 h = getdigits(&arg);
7729 if (*p != NUL && *arg == NUL)
7730 set_shellsize(w, h, TRUE);
7731 else
7732 EMSG(_("E465: :winsize requires two number arguments"));
7733}
7734
7735#ifdef FEAT_WINDOWS
7736 static void
7737ex_wincmd(eap)
7738 exarg_T *eap;
7739{
7740 int xchar = NUL;
7741 char_u *p;
7742
7743 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7744 {
7745 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7746 if (eap->arg[1] == NUL)
7747 {
7748 EMSG(_(e_invarg));
7749 return;
7750 }
7751 xchar = eap->arg[1];
7752 p = eap->arg + 2;
7753 }
7754 else
7755 p = eap->arg + 1;
7756
7757 eap->nextcmd = check_nextcmd(p);
7758 p = skipwhite(p);
7759 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7760 EMSG(_(e_invarg));
7761 else
7762 {
7763 /* Pass flags on for ":vertical wincmd ]". */
7764 postponed_split_flags = cmdmod.split;
7765 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7766 postponed_split_flags = 0;
7767 }
7768}
7769#endif
7770
Bram Moolenaar843ee412004-06-30 16:16:41 +00007771#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772/*
7773 * ":winpos".
7774 */
7775 static void
7776ex_winpos(eap)
7777 exarg_T *eap;
7778{
7779 int x, y;
7780 char_u *arg = eap->arg;
7781 char_u *p;
7782
7783 if (*arg == NUL)
7784 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007785# if defined(FEAT_GUI) || defined(MSWIN)
7786# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007788# else
7789 if (mch_get_winpos(&x, &y) != FAIL)
7790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 {
7792 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7793 msg(IObuff);
7794 }
7795 else
7796# endif
7797 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7798 }
7799 else
7800 {
7801 x = getdigits(&arg);
7802 arg = skipwhite(arg);
7803 p = arg;
7804 y = getdigits(&arg);
7805 if (*p == NUL || *arg != NUL)
7806 {
7807 EMSG(_("E466: :winpos requires two number arguments"));
7808 return;
7809 }
7810# ifdef FEAT_GUI
7811 if (gui.in_use)
7812 gui_mch_set_winpos(x, y);
7813 else if (gui.starting)
7814 {
7815 /* Remember the coordinates for when the window is opened. */
7816 gui_win_x = x;
7817 gui_win_y = y;
7818 }
7819# ifdef HAVE_TGETENT
7820 else
7821# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007822# else
7823# ifdef MSWIN
7824 mch_set_winpos(x, y);
7825# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826# endif
7827# ifdef HAVE_TGETENT
7828 if (*T_CWP)
7829 term_set_winpos(x, y);
7830# endif
7831 }
7832}
7833#endif
7834
7835/*
7836 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7837 */
7838 static void
7839ex_operators(eap)
7840 exarg_T *eap;
7841{
7842 oparg_T oa;
7843
7844 clear_oparg(&oa);
7845 oa.regname = eap->regname;
7846 oa.start.lnum = eap->line1;
7847 oa.end.lnum = eap->line2;
7848 oa.line_count = eap->line2 - eap->line1 + 1;
7849 oa.motion_type = MLINE;
7850#ifdef FEAT_VIRTUALEDIT
7851 virtual_op = FALSE;
7852#endif
7853 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7854 {
7855 setpcmark();
7856 curwin->w_cursor.lnum = eap->line1;
7857 beginline(BL_SOL | BL_FIX);
7858 }
7859
7860 switch (eap->cmdidx)
7861 {
7862 case CMD_delete:
7863 oa.op_type = OP_DELETE;
7864 op_delete(&oa);
7865 break;
7866
7867 case CMD_yank:
7868 oa.op_type = OP_YANK;
7869 (void)op_yank(&oa, FALSE, TRUE);
7870 break;
7871
7872 default: /* CMD_rshift or CMD_lshift */
7873 if ((eap->cmdidx == CMD_rshift)
7874#ifdef FEAT_RIGHTLEFT
7875 ^ curwin->w_p_rl
7876#endif
7877 )
7878 oa.op_type = OP_RSHIFT;
7879 else
7880 oa.op_type = OP_LSHIFT;
7881 op_shift(&oa, FALSE, eap->amount);
7882 break;
7883 }
7884#ifdef FEAT_VIRTUALEDIT
7885 virtual_op = MAYBE;
7886#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007887 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888}
7889
7890/*
7891 * ":put".
7892 */
7893 static void
7894ex_put(eap)
7895 exarg_T *eap;
7896{
7897 /* ":0put" works like ":1put!". */
7898 if (eap->line2 == 0)
7899 {
7900 eap->line2 = 1;
7901 eap->forceit = TRUE;
7902 }
7903 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007904 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7905 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906}
7907
7908/*
7909 * Handle ":copy" and ":move".
7910 */
7911 static void
7912ex_copymove(eap)
7913 exarg_T *eap;
7914{
7915 long n;
7916
7917 n = get_address(&eap->arg, FALSE, FALSE);
7918 if (eap->arg == NULL) /* error detected */
7919 {
7920 eap->nextcmd = NULL;
7921 return;
7922 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007923 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924
7925 /*
7926 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7927 */
7928 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7929 {
7930 EMSG(_(e_invaddr));
7931 return;
7932 }
7933
7934 if (eap->cmdidx == CMD_move)
7935 {
7936 if (do_move(eap->line1, eap->line2, n) == FAIL)
7937 return;
7938 }
7939 else
7940 ex_copy(eap->line1, eap->line2, n);
7941 u_clearline();
7942 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007943 ex_may_print(eap);
7944}
7945
7946/*
7947 * Print the current line if flags were given to the Ex command.
7948 */
7949 static void
7950ex_may_print(eap)
7951 exarg_T *eap;
7952{
7953 if (eap->flags != 0)
7954 {
7955 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7956 (eap->flags & EXFLAG_LIST));
7957 ex_no_reprint = TRUE;
7958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959}
7960
7961/*
7962 * ":smagic" and ":snomagic".
7963 */
7964 static void
7965ex_submagic(eap)
7966 exarg_T *eap;
7967{
7968 int magic_save = p_magic;
7969
7970 p_magic = (eap->cmdidx == CMD_smagic);
7971 do_sub(eap);
7972 p_magic = magic_save;
7973}
7974
7975/*
7976 * ":join".
7977 */
7978 static void
7979ex_join(eap)
7980 exarg_T *eap;
7981{
7982 curwin->w_cursor.lnum = eap->line1;
7983 if (eap->line1 == eap->line2)
7984 {
7985 if (eap->addr_count >= 2) /* :2,2join does nothing */
7986 return;
7987 if (eap->line2 == curbuf->b_ml.ml_line_count)
7988 {
7989 beep_flush();
7990 return;
7991 }
7992 ++eap->line2;
7993 }
7994 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
7995 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007996 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997}
7998
7999/*
8000 * ":[addr]@r" or ":[addr]*r": execute register
8001 */
8002 static void
8003ex_at(eap)
8004 exarg_T *eap;
8005{
8006 int c;
8007
8008 curwin->w_cursor.lnum = eap->line2;
8009
8010#ifdef USE_ON_FLY_SCROLL
8011 dont_scroll = TRUE; /* disallow scrolling here */
8012#endif
8013
8014 /* get the register name. No name means to use the previous one */
8015 c = *eap->arg;
8016 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8017 c = '@';
8018 /* put the register in mapbuf */
8019 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008020 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 else
8024 {
8025 int save_efr = exec_from_reg;
8026
8027 exec_from_reg = TRUE;
8028
8029 /*
8030 * Execute from the typeahead buffer.
8031 * Originally this didn't check for the typeahead buffer to be empty,
8032 * thus could read more Ex commands from stdin. It's not clear why,
8033 * it is certainly unexpected.
8034 */
8035 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8036 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8037
8038 exec_from_reg = save_efr;
8039 }
8040}
8041
8042/*
8043 * ":!".
8044 */
8045 static void
8046ex_bang(eap)
8047 exarg_T *eap;
8048{
8049 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8050}
8051
8052/*
8053 * ":undo".
8054 */
8055/*ARGSUSED*/
8056 static void
8057ex_undo(eap)
8058 exarg_T *eap;
8059{
8060 u_undo(1);
8061}
8062
8063/*
8064 * ":redo".
8065 */
8066/*ARGSUSED*/
8067 static void
8068ex_redo(eap)
8069 exarg_T *eap;
8070{
8071 u_redo(1);
8072}
8073
8074/*
8075 * ":redir": start/stop redirection.
8076 */
8077 static void
8078ex_redir(eap)
8079 exarg_T *eap;
8080{
8081 char *mode;
8082 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008083 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084
8085 if (STRICMP(eap->arg, "END") == 0)
8086 close_redir();
8087 else
8088 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008089 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008091 ++arg;
8092 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008094 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 mode = "a";
8096 }
8097 else
8098 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008099 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100
8101 close_redir();
8102
8103 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008104 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 if (fname == NULL)
8106 return;
8107#ifdef FEAT_BROWSE
8108 if (cmdmod.browse)
8109 {
8110 char_u *browseFile;
8111
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008112 browseFile = do_browse(BROWSE_SAVE,
8113 (char_u *)_("Save Redirection"),
8114 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 if (browseFile == NULL)
8116 return; /* operation cancelled */
8117 vim_free(fname);
8118 fname = browseFile;
8119 eap->forceit = TRUE; /* since dialog already asked */
8120 }
8121#endif
8122
8123 redir_fd = open_exfile(fname, eap->forceit, mode);
8124 vim_free(fname);
8125 }
8126#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008127 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {
8129 /* redirect to a register a-z (resp. A-Z for appending) */
8130 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008131 ++arg;
8132 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008134 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008136 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008138 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008139 if (*arg == '>' && arg[1] == '>')
8140 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008141 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
8142 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008144 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008146 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008148 if (*arg == '>')
8149 arg++;
8150
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 /* make register empty */
8152 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
8153 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008154 }
8155 if (*arg != NUL)
8156 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008157 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008158 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008159 }
8160 }
8161 else if (*arg == '=' && arg[1] == '>')
8162 {
8163 int append;
8164
8165 /* redirect to a variable */
8166 close_redir();
8167 arg += 2;
8168
8169 if (*arg == '>')
8170 {
8171 ++arg;
8172 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 }
8174 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008175 append = FALSE;
8176
8177 if (var_redir_start(skipwhite(arg), append) == OK)
8178 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 }
8180#endif
8181
8182 /* TODO: redirect to a buffer */
8183
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008185 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 }
8187}
8188
8189/*
8190 * ":redraw": force redraw
8191 */
8192 static void
8193ex_redraw(eap)
8194 exarg_T *eap;
8195{
8196 int r = RedrawingDisabled;
8197 int p = p_lz;
8198
8199 RedrawingDisabled = 0;
8200 p_lz = FALSE;
8201 update_topline();
8202 update_screen(eap->forceit ? CLEAR :
8203#ifdef FEAT_VISUAL
8204 VIsual_active ? INVERTED :
8205#endif
8206 0);
8207#ifdef FEAT_TITLE
8208 if (need_maketitle)
8209 maketitle();
8210#endif
8211 RedrawingDisabled = r;
8212 p_lz = p;
8213
8214 /* Reset msg_didout, so that a message that's there is overwritten. */
8215 msg_didout = FALSE;
8216 msg_col = 0;
8217
8218 out_flush();
8219}
8220
8221/*
8222 * ":redrawstatus": force redraw of status line(s)
8223 */
8224/*ARGSUSED*/
8225 static void
8226ex_redrawstatus(eap)
8227 exarg_T *eap;
8228{
8229#if defined(FEAT_WINDOWS)
8230 int r = RedrawingDisabled;
8231 int p = p_lz;
8232
8233 RedrawingDisabled = 0;
8234 p_lz = FALSE;
8235 if (eap->forceit)
8236 status_redraw_all();
8237 else
8238 status_redraw_curbuf();
8239 update_screen(
8240# ifdef FEAT_VISUAL
8241 VIsual_active ? INVERTED :
8242# endif
8243 0);
8244 RedrawingDisabled = r;
8245 p_lz = p;
8246 out_flush();
8247#endif
8248}
8249
8250 static void
8251close_redir()
8252{
8253 if (redir_fd != NULL)
8254 {
8255 fclose(redir_fd);
8256 redir_fd = NULL;
8257 }
8258#ifdef FEAT_EVAL
8259 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008260 if (redir_vname)
8261 {
8262 var_redir_stop();
8263 redir_vname = 0;
8264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265#endif
8266}
8267
8268#if defined(FEAT_SESSION) && defined(USE_CRNL)
8269# define MKSESSION_NL
8270static int mksession_nl = FALSE; /* use NL only in put_eol() */
8271#endif
8272
8273/*
8274 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8275 */
8276 static void
8277ex_mkrc(eap)
8278 exarg_T *eap;
8279{
8280 FILE *fd;
8281 int failed = FALSE;
8282 char_u *fname;
8283#ifdef FEAT_BROWSE
8284 char_u *browseFile = NULL;
8285#endif
8286#ifdef FEAT_SESSION
8287 int view_session = FALSE;
8288 int using_vdir = FALSE; /* using 'viewdir'? */
8289 char_u *viewFile = NULL;
8290 unsigned *flagp;
8291#endif
8292
8293 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8294 {
8295#ifdef FEAT_SESSION
8296 view_session = TRUE;
8297#else
8298 ex_ni(eap);
8299 return;
8300#endif
8301 }
8302
8303#ifdef FEAT_SESSION
8304 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8305 if (eap->cmdidx == CMD_mkview
8306 && (*eap->arg == NUL
8307 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8308 {
8309 eap->forceit = TRUE;
8310 fname = get_view_file(*eap->arg);
8311 if (fname == NULL)
8312 return;
8313 viewFile = fname;
8314 using_vdir = TRUE;
8315 }
8316 else
8317#endif
8318 if (*eap->arg != NUL)
8319 fname = eap->arg;
8320 else if (eap->cmdidx == CMD_mkvimrc)
8321 fname = (char_u *)VIMRC_FILE;
8322#ifdef FEAT_SESSION
8323 else if (eap->cmdidx == CMD_mksession)
8324 fname = (char_u *)SESSION_FILE;
8325#endif
8326 else
8327 fname = (char_u *)EXRC_FILE;
8328
8329#ifdef FEAT_BROWSE
8330 if (cmdmod.browse)
8331 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008332 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333# ifdef FEAT_SESSION
8334 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8335 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8336# endif
8337 (char_u *)_("Save Setup"),
8338 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8339 if (browseFile == NULL)
8340 goto theend;
8341 fname = browseFile;
8342 eap->forceit = TRUE; /* since dialog already asked */
8343 }
8344#endif
8345
8346#if defined(FEAT_SESSION) && defined(vim_mkdir)
8347 /* When using 'viewdir' may have to create the directory. */
8348 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008349 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350#endif
8351
8352 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8353 if (fd != NULL)
8354 {
8355#ifdef FEAT_SESSION
8356 if (eap->cmdidx == CMD_mkview)
8357 flagp = &vop_flags;
8358 else
8359 flagp = &ssop_flags;
8360#endif
8361
8362#ifdef MKSESSION_NL
8363 /* "unix" in 'sessionoptions': use NL line separator */
8364 if (view_session && (*flagp & SSOP_UNIX))
8365 mksession_nl = TRUE;
8366#endif
8367
8368 /* Write the version command for :mkvimrc */
8369 if (eap->cmdidx == CMD_mkvimrc)
8370 (void)put_line(fd, "version 6.0");
8371
8372#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008373 if (eap->cmdidx == CMD_mksession)
8374 {
8375 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8376 failed = TRUE;
8377 }
8378
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 if (eap->cmdidx != CMD_mkview)
8380#endif
8381 {
8382 /* Write setting 'compatible' first, because it has side effects.
8383 * For that same reason only do it when needed. */
8384 if (p_cp)
8385 (void)put_line(fd, "if !&cp | set cp | endif");
8386 else
8387 (void)put_line(fd, "if &cp | set nocp | endif");
8388 }
8389
8390#ifdef FEAT_SESSION
8391 if (!view_session
8392 || (eap->cmdidx == CMD_mksession
8393 && (*flagp & SSOP_OPTIONS)))
8394#endif
8395 failed |= (makemap(fd, NULL) == FAIL
8396 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8397
8398#ifdef FEAT_SESSION
8399 if (!failed && view_session)
8400 {
8401 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8402 failed = TRUE;
8403 if (eap->cmdidx == CMD_mksession)
8404 {
8405 char_u dirnow[MAXPATHL]; /* current directory */
8406
8407 /*
8408 * Change to session file's dir.
8409 */
8410 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8411 || mch_chdir((char *)dirnow) != 0)
8412 *dirnow = NUL;
8413 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8414 {
8415 if (vim_chdirfile(fname) == OK)
8416 shorten_fnames(TRUE);
8417 }
8418 else if (*dirnow != NUL
8419 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8420 {
8421 (void)mch_chdir((char *)globaldir);
8422 shorten_fnames(TRUE);
8423 }
8424
8425 failed |= (makeopens(fd, dirnow) == FAIL);
8426
8427 /* restore original dir */
8428 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8429 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8430 {
8431 if (mch_chdir((char *)dirnow) != 0)
8432 EMSG(_(e_prev_dir));
8433 shorten_fnames(TRUE);
8434 }
8435 }
8436 else
8437 {
8438 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8439 }
8440 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8441 == FAIL)
8442 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008443 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8444 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008445 if (eap->cmdidx == CMD_mksession)
8446 {
8447 if (put_line(fd, "unlet SessionLoad") == FAIL)
8448 failed = TRUE;
8449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 }
8451#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008452 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8453 failed = TRUE;
8454
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 failed |= fclose(fd);
8456
8457 if (failed)
8458 EMSG(_(e_write));
8459#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8460 else if (eap->cmdidx == CMD_mksession)
8461 {
8462 /* successful session write - set this_session var */
8463 char_u tbuf[MAXPATHL];
8464
8465 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8466 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8467 }
8468#endif
8469#ifdef MKSESSION_NL
8470 mksession_nl = FALSE;
8471#endif
8472 }
8473
8474#ifdef FEAT_BROWSE
8475theend:
8476 vim_free(browseFile);
8477#endif
8478#ifdef FEAT_SESSION
8479 vim_free(viewFile);
8480#endif
8481}
8482
Bram Moolenaardf177f62005-02-22 08:39:57 +00008483#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8484 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008485/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008486 int
8487vim_mkdir_emsg(name, prot)
8488 char_u *name;
8489 int prot;
8490{
8491 if (vim_mkdir(name, prot) != 0)
8492 {
8493 EMSG2(_("E739: Cannot create directory: %s"), name);
8494 return FAIL;
8495 }
8496 return OK;
8497}
8498#endif
8499
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500/*
8501 * Open a file for writing for an Ex command, with some checks.
8502 * Return file descriptor, or NULL on failure.
8503 */
8504 FILE *
8505open_exfile(fname, forceit, mode)
8506 char_u *fname;
8507 int forceit;
8508 char *mode; /* "w" for create new file or "a" for append */
8509{
8510 FILE *fd;
8511
8512#ifdef UNIX
8513 /* with Unix it is possible to open a directory */
8514 if (mch_isdir(fname))
8515 {
8516 EMSG2(_(e_isadir2), fname);
8517 return NULL;
8518 }
8519#endif
8520 if (!forceit && *mode != 'a' && vim_fexists(fname))
8521 {
8522 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8523 return NULL;
8524 }
8525
8526 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8527 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8528
8529 return fd;
8530}
8531
8532/*
8533 * ":mark" and ":k".
8534 */
8535 static void
8536ex_mark(eap)
8537 exarg_T *eap;
8538{
8539 pos_T pos;
8540
8541 if (*eap->arg == NUL) /* No argument? */
8542 EMSG(_(e_argreq));
8543 else if (eap->arg[1] != NUL) /* more than one character? */
8544 EMSG(_(e_trailing));
8545 else
8546 {
8547 pos = curwin->w_cursor; /* save curwin->w_cursor */
8548 curwin->w_cursor.lnum = eap->line2;
8549 beginline(BL_WHITE | BL_FIX);
8550 if (setmark(*eap->arg) == FAIL) /* set mark */
8551 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8552 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8553 }
8554}
8555
8556/*
8557 * Update w_topline, w_leftcol and the cursor position.
8558 */
8559 void
8560update_topline_cursor()
8561{
8562 check_cursor(); /* put cursor on valid line */
8563 update_topline();
8564 if (!curwin->w_p_wrap)
8565 validate_cursor();
8566 update_curswant();
8567}
8568
8569#ifdef FEAT_EX_EXTRA
8570/*
8571 * ":normal[!] {commands}": Execute normal mode commands.
8572 */
8573 static void
8574ex_normal(eap)
8575 exarg_T *eap;
8576{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 int save_msg_scroll = msg_scroll;
8578 int save_restart_edit = restart_edit;
8579 int save_msg_didout = msg_didout;
8580 int save_State = State;
8581 tasave_T tabuf;
8582 int save_insertmode = p_im;
8583 int save_finish_op = finish_op;
8584#ifdef FEAT_MBYTE
8585 char_u *arg = NULL;
8586 int l;
8587 char_u *p;
8588#endif
8589
8590 if (ex_normal_busy >= p_mmd)
8591 {
8592 EMSG(_("E192: Recursive use of :normal too deep"));
8593 return;
8594 }
8595 ++ex_normal_busy;
8596
8597 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8598 restart_edit = 0; /* don't go to Insert mode */
8599 p_im = FALSE; /* don't use 'insertmode' */
8600
8601#ifdef FEAT_MBYTE
8602 /*
8603 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8604 * this for the K_SPECIAL leading byte, otherwise special keys will not
8605 * work.
8606 */
8607 if (has_mbyte)
8608 {
8609 int len = 0;
8610
8611 /* Count the number of characters to be escaped. */
8612 for (p = eap->arg; *p != NUL; ++p)
8613 {
8614# ifdef FEAT_GUI
8615 if (*p == CSI) /* leadbyte CSI */
8616 len += 2;
8617# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008618 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8620# ifdef FEAT_GUI
8621 || *p == CSI
8622# endif
8623 )
8624 len += 2;
8625 }
8626 if (len > 0)
8627 {
8628 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8629 if (arg != NULL)
8630 {
8631 len = 0;
8632 for (p = eap->arg; *p != NUL; ++p)
8633 {
8634 arg[len++] = *p;
8635# ifdef FEAT_GUI
8636 if (*p == CSI)
8637 {
8638 arg[len++] = KS_EXTRA;
8639 arg[len++] = (int)KE_CSI;
8640 }
8641# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008642 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 {
8644 arg[len++] = *++p;
8645 if (*p == K_SPECIAL)
8646 {
8647 arg[len++] = KS_SPECIAL;
8648 arg[len++] = KE_FILLER;
8649 }
8650# ifdef FEAT_GUI
8651 else if (*p == CSI)
8652 {
8653 arg[len++] = KS_EXTRA;
8654 arg[len++] = (int)KE_CSI;
8655 }
8656# endif
8657 }
8658 arg[len] = NUL;
8659 }
8660 }
8661 }
8662 }
8663#endif
8664
8665 /*
8666 * Save the current typeahead. This is required to allow using ":normal"
8667 * from an event handler and makes sure we don't hang when the argument
8668 * ends with half a command.
8669 */
8670 save_typeahead(&tabuf);
8671 if (tabuf.typebuf_valid)
8672 {
8673 /*
8674 * Repeat the :normal command for each line in the range. When no
8675 * range given, execute it just once, without positioning the cursor
8676 * first.
8677 */
8678 do
8679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 if (eap->addr_count != 0)
8681 {
8682 curwin->w_cursor.lnum = eap->line1++;
8683 curwin->w_cursor.col = 0;
8684 }
8685
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008686 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687#ifdef FEAT_MBYTE
8688 arg != NULL ? arg :
8689#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008690 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 }
8692 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8693 }
8694
8695 /* Might not return to the main loop when in an event handler. */
8696 update_topline_cursor();
8697
8698 /* Restore the previous typeahead. */
8699 restore_typeahead(&tabuf);
8700
8701 --ex_normal_busy;
8702 msg_scroll = save_msg_scroll;
8703 restart_edit = save_restart_edit;
8704 p_im = save_insertmode;
8705 finish_op = save_finish_op;
8706 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8707
8708 /* Restore the state (needed when called from a function executed for
8709 * 'indentexpr'). */
8710 State = save_State;
8711#ifdef FEAT_MBYTE
8712 vim_free(arg);
8713#endif
8714}
8715
8716/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008717 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 */
8719 static void
8720ex_startinsert(eap)
8721 exarg_T *eap;
8722{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008723 if (eap->forceit)
8724 {
8725 coladvance((colnr_T)MAXCOL);
8726 curwin->w_curswant = MAXCOL;
8727 curwin->w_set_curswant = FALSE;
8728 }
8729
Bram Moolenaara40c5002005-01-09 21:16:21 +00008730 /* Ignore the command when already in Insert mode. Inserting an
8731 * expression register that invokes a function can do this. */
8732 if (State & INSERT)
8733 return;
8734
Bram Moolenaar64969662005-12-14 21:59:55 +00008735 if (eap->cmdidx == CMD_startinsert)
8736 restart_edit = 'a';
8737 else if (eap->cmdidx == CMD_startreplace)
8738 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008740 restart_edit = 'V';
8741
8742 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008744 if (eap->cmdidx == CMD_startinsert)
8745 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 curwin->w_curswant = 0; /* avoid MAXCOL */
8747 }
8748}
8749
8750/*
8751 * ":stopinsert"
8752 */
8753/*ARGSUSED*/
8754 static void
8755ex_stopinsert(eap)
8756 exarg_T *eap;
8757{
8758 restart_edit = 0;
8759 stop_insert_mode = TRUE;
8760}
8761#endif
8762
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008763#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8764/*
8765 * Execute normal mode command "cmd".
8766 * "remap" can be REMAP_NONE or REMAP_YES.
8767 */
8768 void
8769exec_normal_cmd(cmd, remap, silent)
8770 char_u *cmd;
8771 int remap;
8772 int silent;
8773{
8774 oparg_T oa;
8775
8776 /*
8777 * Stuff the argument into the typeahead buffer.
8778 * Execute normal_cmd() until there is no typeahead left.
8779 */
8780 clear_oparg(&oa);
8781 finish_op = FALSE;
8782 ins_typebuf(cmd, remap, 0, TRUE, silent);
8783 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8784 && !got_int)
8785 {
8786 update_topline_cursor();
8787 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8788 }
8789}
8790#endif
8791
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792#ifdef FEAT_FIND_ID
8793 static void
8794ex_checkpath(eap)
8795 exarg_T *eap;
8796{
8797 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8798 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8799 (linenr_T)1, (linenr_T)MAXLNUM);
8800}
8801
8802#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8803/*
8804 * ":psearch"
8805 */
8806 static void
8807ex_psearch(eap)
8808 exarg_T *eap;
8809{
8810 g_do_tagpreview = p_pvh;
8811 ex_findpat(eap);
8812 g_do_tagpreview = 0;
8813}
8814#endif
8815
8816 static void
8817ex_findpat(eap)
8818 exarg_T *eap;
8819{
8820 int whole = TRUE;
8821 long n;
8822 char_u *p;
8823 int action;
8824
8825 switch (cmdnames[eap->cmdidx].cmd_name[2])
8826 {
8827 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8828 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8829 action = ACTION_GOTO;
8830 else
8831 action = ACTION_SHOW;
8832 break;
8833 case 'i': /* ":ilist" and ":dlist" */
8834 action = ACTION_SHOW_ALL;
8835 break;
8836 case 'u': /* ":ijump" and ":djump" */
8837 action = ACTION_GOTO;
8838 break;
8839 default: /* ":isplit" and ":dsplit" */
8840 action = ACTION_SPLIT;
8841 break;
8842 }
8843
8844 n = 1;
8845 if (vim_isdigit(*eap->arg)) /* get count */
8846 {
8847 n = getdigits(&eap->arg);
8848 eap->arg = skipwhite(eap->arg);
8849 }
8850 if (*eap->arg == '/') /* Match regexp, not just whole words */
8851 {
8852 whole = FALSE;
8853 ++eap->arg;
8854 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8855 if (*p)
8856 {
8857 *p++ = NUL;
8858 p = skipwhite(p);
8859
8860 /* Check for trailing illegal characters */
8861 if (!ends_excmd(*p))
8862 eap->errmsg = e_trailing;
8863 else
8864 eap->nextcmd = check_nextcmd(p);
8865 }
8866 }
8867 if (!eap->skip)
8868 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8869 whole, !eap->forceit,
8870 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8871 n, action, eap->line1, eap->line2);
8872}
8873#endif
8874
8875#ifdef FEAT_WINDOWS
8876
8877# ifdef FEAT_QUICKFIX
8878/*
8879 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8880 */
8881 static void
8882ex_ptag(eap)
8883 exarg_T *eap;
8884{
8885 g_do_tagpreview = p_pvh;
8886 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8887}
8888
8889/*
8890 * ":pedit"
8891 */
8892 static void
8893ex_pedit(eap)
8894 exarg_T *eap;
8895{
8896 win_T *curwin_save = curwin;
8897
8898 g_do_tagpreview = p_pvh;
8899 prepare_tagpreview();
8900 keep_help_flag = curwin_save->w_buffer->b_help;
8901 do_exedit(eap, NULL);
8902 keep_help_flag = FALSE;
8903 if (curwin != curwin_save && win_valid(curwin_save))
8904 {
8905 /* Return cursor to where we were */
8906 validate_cursor();
8907 redraw_later(VALID);
8908 win_enter(curwin_save, TRUE);
8909 }
8910 g_do_tagpreview = 0;
8911}
8912# endif
8913
8914/*
8915 * ":stag", ":stselect" and ":stjump".
8916 */
8917 static void
8918ex_stag(eap)
8919 exarg_T *eap;
8920{
8921 postponed_split = -1;
8922 postponed_split_flags = cmdmod.split;
8923 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8924 postponed_split_flags = 0;
8925}
8926#endif
8927
8928/*
8929 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8930 */
8931 static void
8932ex_tag(eap)
8933 exarg_T *eap;
8934{
8935 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8936}
8937
8938 static void
8939ex_tag_cmd(eap, name)
8940 exarg_T *eap;
8941 char_u *name;
8942{
8943 int cmd;
8944
8945 switch (name[1])
8946 {
8947 case 'j': cmd = DT_JUMP; /* ":tjump" */
8948 break;
8949 case 's': cmd = DT_SELECT; /* ":tselect" */
8950 break;
8951 case 'p': cmd = DT_PREV; /* ":tprevious" */
8952 break;
8953 case 'N': cmd = DT_PREV; /* ":tNext" */
8954 break;
8955 case 'n': cmd = DT_NEXT; /* ":tnext" */
8956 break;
8957 case 'o': cmd = DT_POP; /* ":pop" */
8958 break;
8959 case 'f': /* ":tfirst" */
8960 case 'r': cmd = DT_FIRST; /* ":trewind" */
8961 break;
8962 case 'l': cmd = DT_LAST; /* ":tlast" */
8963 break;
8964 default: /* ":tag" */
8965#ifdef FEAT_CSCOPE
8966 if (p_cst)
8967 {
8968 do_cstag(eap);
8969 return;
8970 }
8971#endif
8972 cmd = DT_TAG;
8973 break;
8974 }
8975
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008976 if (name[0] == 'l')
8977 {
8978#ifndef FEAT_QUICKFIX
8979 ex_ni(eap);
8980 return;
8981#else
8982 cmd = DT_LTAG;
8983#endif
8984 }
8985
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8987 eap->forceit, TRUE);
8988}
8989
8990/*
8991 * Evaluate cmdline variables.
8992 *
8993 * change '%' to curbuf->b_ffname
8994 * '#' to curwin->w_altfile
8995 * '<cword>' to word under the cursor
8996 * '<cWORD>' to WORD under the cursor
8997 * '<cfile>' to path name under the cursor
8998 * '<sfile>' to sourced file name
8999 * '<afile>' to file name for autocommand
9000 * '<abuf>' to buffer number for autocommand
9001 * '<amatch>' to matching name for autocommand
9002 *
9003 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9004 * "" for error without a message) and NULL is returned.
9005 * Returns an allocated string if a valid match was found.
9006 * Returns NULL if no match was found. "usedlen" then still contains the
9007 * number of characters to skip.
9008 */
9009 char_u *
9010eval_vars(src, usedlen, lnump, errormsg, srcstart)
9011 char_u *src; /* pointer into commandline */
9012 int *usedlen; /* characters after src that are used */
9013 linenr_T *lnump; /* line number for :e command, or NULL */
9014 char_u **errormsg; /* pointer to error message */
9015 char_u *srcstart; /* beginning of valid memory for src */
9016{
9017 int i;
9018 char_u *s;
9019 char_u *result;
9020 char_u *resultbuf = NULL;
9021 int resultlen;
9022 buf_T *buf;
9023 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9024 int spec_idx;
9025#ifdef FEAT_MODIFY_FNAME
9026 int skip_mod = FALSE;
9027#endif
9028 static char *(spec_str[]) =
9029 {
9030 "%",
9031#define SPEC_PERC 0
9032 "#",
9033#define SPEC_HASH 1
9034 "<cword>", /* cursor word */
9035#define SPEC_CWORD 2
9036 "<cWORD>", /* cursor WORD */
9037#define SPEC_CCWORD 3
9038 "<cfile>", /* cursor path name */
9039#define SPEC_CFILE 4
9040 "<sfile>", /* ":so" file name */
9041#define SPEC_SFILE 5
9042#ifdef FEAT_AUTOCMD
9043 "<afile>", /* autocommand file name */
9044# define SPEC_AFILE 6
9045 "<abuf>", /* autocommand buffer number */
9046# define SPEC_ABUF 7
9047 "<amatch>", /* autocommand match name */
9048# define SPEC_AMATCH 8
9049#endif
9050#ifdef FEAT_CLIENTSERVER
9051 "<client>"
9052# define SPEC_CLIENT 9
9053#endif
9054 };
9055#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9056
9057#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9058 char_u strbuf[30];
9059#endif
9060
9061 *errormsg = NULL;
9062
9063 /*
9064 * Check if there is something to do.
9065 */
9066 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
9067 {
9068 *usedlen = (int)STRLEN(spec_str[spec_idx]);
9069 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
9070 break;
9071 }
9072 if (spec_idx == SPEC_COUNT) /* no match */
9073 {
9074 *usedlen = 1;
9075 return NULL;
9076 }
9077
9078 /*
9079 * Skip when preceded with a backslash "\%" and "\#".
9080 * Note: In "\\%" the % is also not recognized!
9081 */
9082 if (src > srcstart && src[-1] == '\\')
9083 {
9084 *usedlen = 0;
9085 STRCPY(src - 1, src); /* remove backslash */
9086 return NULL;
9087 }
9088
9089 /*
9090 * word or WORD under cursor
9091 */
9092 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9093 {
9094 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9095 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9096 if (resultlen == 0)
9097 {
9098 *errormsg = (char_u *)"";
9099 return NULL;
9100 }
9101 }
9102
9103 /*
9104 * '#': Alternate file name
9105 * '%': Current file name
9106 * File name under the cursor
9107 * File name for autocommand
9108 * and following modifiers
9109 */
9110 else
9111 {
9112 switch (spec_idx)
9113 {
9114 case SPEC_PERC: /* '%': current file */
9115 if (curbuf->b_fname == NULL)
9116 {
9117 result = (char_u *)"";
9118 valid = 0; /* Must have ":p:h" to be valid */
9119 }
9120 else
9121#ifdef RISCOS
9122 /* Always use the full path for RISC OS if possible. */
9123 result = curbuf->b_ffname;
9124 if (result == NULL)
9125 result = curbuf->b_fname;
9126#else
9127 result = curbuf->b_fname;
9128#endif
9129 break;
9130
9131 case SPEC_HASH: /* '#' or "#99": alternate file */
9132 if (src[1] == '#') /* "##": the argument list */
9133 {
9134 result = arg_all();
9135 resultbuf = result;
9136 *usedlen = 2;
9137#ifdef FEAT_MODIFY_FNAME
9138 skip_mod = TRUE;
9139#endif
9140 break;
9141 }
9142 s = src + 1;
9143 i = (int)getdigits(&s);
9144 *usedlen = (int)(s - src); /* length of what we expand */
9145
9146 buf = buflist_findnr(i);
9147 if (buf == NULL)
9148 {
9149 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9150 return NULL;
9151 }
9152 if (lnump != NULL)
9153 *lnump = ECMD_LAST;
9154 if (buf->b_fname == NULL)
9155 {
9156 result = (char_u *)"";
9157 valid = 0; /* Must have ":p:h" to be valid */
9158 }
9159 else
9160 result = buf->b_fname;
9161 break;
9162
9163#ifdef FEAT_SEARCHPATH
9164 case SPEC_CFILE: /* file name under cursor */
9165 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L);
9166 if (result == NULL)
9167 {
9168 *errormsg = (char_u *)"";
9169 return NULL;
9170 }
9171 resultbuf = result; /* remember allocated string */
9172 break;
9173#endif
9174
9175#ifdef FEAT_AUTOCMD
9176 case SPEC_AFILE: /* file name for autocommand */
9177 result = autocmd_fname;
9178 if (result == NULL)
9179 {
9180 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9181 return NULL;
9182 }
9183 break;
9184
9185 case SPEC_ABUF: /* buffer number for autocommand */
9186 if (autocmd_bufnr <= 0)
9187 {
9188 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9189 return NULL;
9190 }
9191 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9192 result = strbuf;
9193 break;
9194
9195 case SPEC_AMATCH: /* match name for autocommand */
9196 result = autocmd_match;
9197 if (result == NULL)
9198 {
9199 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9200 return NULL;
9201 }
9202 break;
9203
9204#endif
9205 case SPEC_SFILE: /* file name for ":so" command */
9206 result = sourcing_name;
9207 if (result == NULL)
9208 {
9209 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9210 return NULL;
9211 }
9212 break;
9213#if defined(FEAT_CLIENTSERVER)
9214 case SPEC_CLIENT: /* Source of last submitted input */
9215 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
9216 result = strbuf;
9217 break;
9218#endif
9219 }
9220
9221 resultlen = (int)STRLEN(result); /* length of new string */
9222 if (src[*usedlen] == '<') /* remove the file name extension */
9223 {
9224 ++*usedlen;
9225#ifdef RISCOS
9226 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9227#else
9228 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9229#endif
9230 resultlen = (int)(s - result);
9231 }
9232#ifdef FEAT_MODIFY_FNAME
9233 else if (!skip_mod)
9234 {
9235 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9236 &resultlen);
9237 if (result == NULL)
9238 {
9239 *errormsg = (char_u *)"";
9240 return NULL;
9241 }
9242 }
9243#endif
9244 }
9245
9246 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9247 {
9248 if (valid != VALID_HEAD + VALID_PATH)
9249 /* xgettext:no-c-format */
9250 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9251 else
9252 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9253 result = NULL;
9254 }
9255 else
9256 result = vim_strnsave(result, resultlen);
9257 vim_free(resultbuf);
9258 return result;
9259}
9260
9261/*
9262 * Concatenate all files in the argument list, separated by spaces, and return
9263 * it in one allocated string.
9264 * Spaces and backslashes in the file names are escaped with a backslash.
9265 * Returns NULL when out of memory.
9266 */
9267 static char_u *
9268arg_all()
9269{
9270 int len;
9271 int idx;
9272 char_u *retval = NULL;
9273 char_u *p;
9274
9275 /*
9276 * Do this loop two times:
9277 * first time: compute the total length
9278 * second time: concatenate the names
9279 */
9280 for (;;)
9281 {
9282 len = 0;
9283 for (idx = 0; idx < ARGCOUNT; ++idx)
9284 {
9285 p = alist_name(&ARGLIST[idx]);
9286 if (p != NULL)
9287 {
9288 if (len > 0)
9289 {
9290 /* insert a space in between names */
9291 if (retval != NULL)
9292 retval[len] = ' ';
9293 ++len;
9294 }
9295 for ( ; *p != NUL; ++p)
9296 {
9297 if (*p == ' ' || *p == '\\')
9298 {
9299 /* insert a backslash */
9300 if (retval != NULL)
9301 retval[len] = '\\';
9302 ++len;
9303 }
9304 if (retval != NULL)
9305 retval[len] = *p;
9306 ++len;
9307 }
9308 }
9309 }
9310
9311 /* second time: break here */
9312 if (retval != NULL)
9313 {
9314 retval[len] = NUL;
9315 break;
9316 }
9317
9318 /* allocate memory */
9319 retval = alloc(len + 1);
9320 if (retval == NULL)
9321 break;
9322 }
9323
9324 return retval;
9325}
9326
9327#if defined(FEAT_AUTOCMD) || defined(PROTO)
9328/*
9329 * Expand the <sfile> string in "arg".
9330 *
9331 * Returns an allocated string, or NULL for any error.
9332 */
9333 char_u *
9334expand_sfile(arg)
9335 char_u *arg;
9336{
9337 char_u *errormsg;
9338 int len;
9339 char_u *result;
9340 char_u *newres;
9341 char_u *repl;
9342 int srclen;
9343 char_u *p;
9344
9345 result = vim_strsave(arg);
9346 if (result == NULL)
9347 return NULL;
9348
9349 for (p = result; *p; )
9350 {
9351 if (STRNCMP(p, "<sfile>", 7) != 0)
9352 ++p;
9353 else
9354 {
9355 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9356 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9357 if (errormsg != NULL)
9358 {
9359 if (*errormsg)
9360 emsg(errormsg);
9361 vim_free(result);
9362 return NULL;
9363 }
9364 if (repl == NULL) /* no match (cannot happen) */
9365 {
9366 p += srclen;
9367 continue;
9368 }
9369 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9370 newres = alloc(len);
9371 if (newres == NULL)
9372 {
9373 vim_free(repl);
9374 vim_free(result);
9375 return NULL;
9376 }
9377 mch_memmove(newres, result, (size_t)(p - result));
9378 STRCPY(newres + (p - result), repl);
9379 len = (int)STRLEN(newres);
9380 STRCAT(newres, p + srclen);
9381 vim_free(repl);
9382 vim_free(result);
9383 result = newres;
9384 p = newres + len; /* continue after the match */
9385 }
9386 }
9387
9388 return result;
9389}
9390#endif
9391
9392#ifdef FEAT_SESSION
9393static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9394static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9395static frame_T *ses_skipframe __ARGS((frame_T *fr));
9396static int ses_do_frame __ARGS((frame_T *fr));
9397static int ses_do_win __ARGS((win_T *wp));
9398static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9399static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9400static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9401
9402/*
9403 * Write openfile commands for the current buffers to an .exrc file.
9404 * Return FAIL on error, OK otherwise.
9405 */
9406 static int
9407makeopens(fd, dirnow)
9408 FILE *fd;
9409 char_u *dirnow; /* Current directory name */
9410{
9411 buf_T *buf;
9412 int only_save_windows = TRUE;
9413 int nr;
9414 int cnr = 1;
9415 int restore_size = TRUE;
9416 win_T *wp;
9417 char_u *sname;
9418 win_T *edited_win = NULL;
9419
9420 if (ssop_flags & SSOP_BUFFERS)
9421 only_save_windows = FALSE; /* Save ALL buffers */
9422
9423 /*
9424 * Begin by setting the this_session variable, and then other
9425 * sessionable variables.
9426 */
9427#ifdef FEAT_EVAL
9428 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9429 return FAIL;
9430 if (ssop_flags & SSOP_GLOBALS)
9431 if (store_session_globals(fd) == FAIL)
9432 return FAIL;
9433#endif
9434
9435 /*
9436 * Close all windows but one.
9437 */
9438 if (put_line(fd, "silent only") == FAIL)
9439 return FAIL;
9440
9441 /*
9442 * Now a :cd command to the session directory or the current directory
9443 */
9444 if (ssop_flags & SSOP_SESDIR)
9445 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009446 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9447 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 return FAIL;
9449 }
9450 else if (ssop_flags & SSOP_CURDIR)
9451 {
9452 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9453 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009454 || fputs("cd ", fd) < 0
9455 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9456 || put_eol(fd) == FAIL)
9457 {
9458 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 vim_free(sname);
9462 }
9463
9464 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009465 * If there is an empty, unnamed buffer we will wipe it out later.
9466 * Remember the buffer number.
9467 */
9468 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9469 return FAIL;
9470 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9471 return FAIL;
9472 if (put_line(fd, "endif") == FAIL)
9473 return FAIL;
9474
9475 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 * Now save the current files, current buffer first.
9477 */
9478 if (put_line(fd, "set shortmess=aoO") == FAIL)
9479 return FAIL;
9480
9481 /* Now put the other buffers into the buffer list */
9482 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9483 {
9484 if (!(only_save_windows && buf->b_nwindows == 0)
9485 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9486 && buf->b_fname != NULL
9487 && buf->b_p_bl)
9488 {
9489 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9490 : buf->b_wininfo->wi_fpos.lnum) < 0
9491 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9492 return FAIL;
9493 }
9494 }
9495
9496 /* the global argument list */
9497 if (ses_arglist(fd, "args", &global_alist.al_ga,
9498 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9499 return FAIL;
9500
9501 if (ssop_flags & SSOP_RESIZE)
9502 {
9503 /* Note: after the restore we still check it worked!*/
9504 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9505 || put_eol(fd) == FAIL)
9506 return FAIL;
9507 }
9508
9509#ifdef FEAT_GUI
9510 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9511 {
9512 int x, y;
9513
9514 if (gui_mch_get_winpos(&x, &y) == OK)
9515 {
9516 /* Note: after the restore we still check it worked!*/
9517 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9518 return FAIL;
9519 }
9520 }
9521#endif
9522
9523 /*
9524 * Before creating the window layout, try loading one file. If this is
9525 * aborted we don't end up with a number of useless windows.
9526 * This may have side effects! (e.g., compressed or network file).
9527 */
9528 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9529 {
9530 if (ses_do_win(wp)
9531 && wp->w_buffer->b_ffname != NULL
9532 && !wp->w_buffer->b_help
9533#ifdef FEAT_QUICKFIX
9534 && !bt_nofile(wp->w_buffer)
9535#endif
9536 )
9537 {
9538 if (fputs("edit ", fd) < 0
9539 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9540 return FAIL;
9541 if (!wp->w_arg_idx_invalid)
9542 edited_win = wp;
9543 break;
9544 }
9545 }
9546
9547 /*
9548 * Save current window layout.
9549 */
9550 if (put_line(fd, "set splitbelow splitright") == FAIL)
9551 return FAIL;
9552 if (ses_win_rec(fd, topframe) == FAIL)
9553 return FAIL;
9554 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9555 return FAIL;
9556 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9557 return FAIL;
9558
9559 /*
9560 * Check if window sizes can be restored (no windows omitted).
9561 * Remember the window number of the current window after restoring.
9562 */
9563 nr = 0;
9564 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9565 {
9566 if (ses_do_win(wp))
9567 ++nr;
9568 else
9569 restore_size = FALSE;
9570 if (curwin == wp)
9571 cnr = nr;
9572 }
9573
9574 /* Go to the first window. */
9575 if (put_line(fd, "wincmd t") == FAIL)
9576 return FAIL;
9577
9578 /*
9579 * If more than one window, see if sizes can be restored.
9580 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9581 * resized when moving between windows.
9582 * Do this before restoring the view, so that the topline and the cursor
9583 * can be set. This is done again below.
9584 */
9585 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9586 return FAIL;
9587 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9588 return FAIL;
9589
9590 /*
9591 * Restore the view of the window (options, file, cursor, etc.).
9592 */
9593 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9594 {
9595 if (!ses_do_win(wp))
9596 continue;
9597 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9598 return FAIL;
9599 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9600 return FAIL;
9601 }
9602
9603 /*
9604 * Restore cursor to the current window if it's not the first one.
9605 */
9606 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9607 return FAIL;
9608
9609 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009610 * Wipe out an empty unnamed buffer we started in.
9611 */
9612 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9613 return FAIL;
9614 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9615 return FAIL;
9616 if (put_line(fd, "endif") == FAIL)
9617 return FAIL;
9618 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9619 return FAIL;
9620
9621 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 * Restore window sizes again after jumping around in windows, because the
9623 * current window has a minimum size while others may not.
9624 */
9625 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9626 return FAIL;
9627
9628 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9629 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9630 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9631 return FAIL;
9632
9633 /*
9634 * Lastly, execute the x.vim file if it exists.
9635 */
9636 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9637 || put_line(fd, "if file_readable(s:sx)") == FAIL
9638 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9639 || put_line(fd, "endif") == FAIL)
9640 return FAIL;
9641
9642 return OK;
9643}
9644
9645 static int
9646ses_winsizes(fd, restore_size)
9647 FILE *fd;
9648 int restore_size;
9649{
9650 int n = 0;
9651 win_T *wp;
9652
9653 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9654 {
9655 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9656 {
9657 if (!ses_do_win(wp))
9658 continue;
9659 ++n;
9660
9661 /* restore height when not full height */
9662 if (wp->w_height + wp->w_status_height < topframe->fr_height
9663 && (fprintf(fd,
9664 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9665 n, (long)wp->w_height, Rows / 2, Rows) < 0
9666 || put_eol(fd) == FAIL))
9667 return FAIL;
9668
9669 /* restore width when not full width */
9670 if (wp->w_width < Columns && (fprintf(fd,
9671 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9672 n, (long)wp->w_width, Columns / 2, Columns) < 0
9673 || put_eol(fd) == FAIL))
9674 return FAIL;
9675 }
9676 }
9677 else
9678 {
9679 /* Just equalise window sizes */
9680 if (put_line(fd, "wincmd =") == FAIL)
9681 return FAIL;
9682 }
9683 return OK;
9684}
9685
9686/*
9687 * Write commands to "fd" to recursively create windows for frame "fr",
9688 * horizontally and vertically split.
9689 * After the commands the last window in the frame is the current window.
9690 * Returns FAIL when writing the commands to "fd" fails.
9691 */
9692 static int
9693ses_win_rec(fd, fr)
9694 FILE *fd;
9695 frame_T *fr;
9696{
9697 frame_T *frc;
9698 int count = 0;
9699
9700 if (fr->fr_layout != FR_LEAF)
9701 {
9702 /* Find first frame that's not skipped and then create a window for
9703 * each following one (first frame is already there). */
9704 frc = ses_skipframe(fr->fr_child);
9705 if (frc != NULL)
9706 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9707 {
9708 /* Make window as big as possible so that we have lots of room
9709 * to split. */
9710 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9711 || put_line(fd, fr->fr_layout == FR_COL
9712 ? "split" : "vsplit") == FAIL)
9713 return FAIL;
9714 ++count;
9715 }
9716
9717 /* Go back to the first window. */
9718 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9719 ? "%dwincmd k" : "%dwincmd h", count) < 0
9720 || put_eol(fd) == FAIL))
9721 return FAIL;
9722
9723 /* Recursively create frames/windows in each window of this column or
9724 * row. */
9725 frc = ses_skipframe(fr->fr_child);
9726 while (frc != NULL)
9727 {
9728 ses_win_rec(fd, frc);
9729 frc = ses_skipframe(frc->fr_next);
9730 /* Go to next window. */
9731 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9732 return FAIL;
9733 }
9734 }
9735 return OK;
9736}
9737
9738/*
9739 * Skip frames that don't contain windows we want to save in the Session.
9740 * Returns NULL when there none.
9741 */
9742 static frame_T *
9743ses_skipframe(fr)
9744 frame_T *fr;
9745{
9746 frame_T *frc;
9747
9748 for (frc = fr; frc != NULL; frc = frc->fr_next)
9749 if (ses_do_frame(frc))
9750 break;
9751 return frc;
9752}
9753
9754/*
9755 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9756 * the Session.
9757 */
9758 static int
9759ses_do_frame(fr)
9760 frame_T *fr;
9761{
9762 frame_T *frc;
9763
9764 if (fr->fr_layout == FR_LEAF)
9765 return ses_do_win(fr->fr_win);
9766 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9767 if (ses_do_frame(frc))
9768 return TRUE;
9769 return FALSE;
9770}
9771
9772/*
9773 * Return non-zero if window "wp" is to be stored in the Session.
9774 */
9775 static int
9776ses_do_win(wp)
9777 win_T *wp;
9778{
9779 if (wp->w_buffer->b_fname == NULL
9780#ifdef FEAT_QUICKFIX
9781 /* When 'buftype' is "nofile" can't restore the window contents. */
9782 || bt_nofile(wp->w_buffer)
9783#endif
9784 )
9785 return (ssop_flags & SSOP_BLANK);
9786 if (wp->w_buffer->b_help)
9787 return (ssop_flags & SSOP_HELP);
9788 return TRUE;
9789}
9790
9791/*
9792 * Write commands to "fd" to restore the view of a window.
9793 * Caller must make sure 'scrolloff' is zero.
9794 */
9795 static int
9796put_view(fd, wp, add_edit, flagp)
9797 FILE *fd;
9798 win_T *wp;
9799 int add_edit; /* add ":edit" command to view */
9800 unsigned *flagp; /* vop_flags or ssop_flags */
9801{
9802 win_T *save_curwin;
9803 int f;
9804 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009805 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806
9807 /* Always restore cursor position for ":mksession". For ":mkview" only
9808 * when 'viewoptions' contains "cursor". */
9809 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9810
9811 /*
9812 * Local argument list.
9813 */
9814 if (wp->w_alist == &global_alist)
9815 {
9816 if (put_line(fd, "argglobal") == FAIL)
9817 return FAIL;
9818 }
9819 else
9820 {
9821 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9822 flagp == &vop_flags
9823 || !(*flagp & SSOP_CURDIR)
9824 || wp->w_localdir != NULL, flagp) == FAIL)
9825 return FAIL;
9826 }
9827
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009828 /* Only when part of a session: restore the argument index. Some
9829 * arguments may have been deleted, check if the index is valid. */
9830 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
9831 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 {
9833 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9834 || put_eol(fd) == FAIL)
9835 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009836 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 }
9838
9839 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009840 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 {
9842 /*
9843 * Load the file.
9844 */
9845 if (wp->w_buffer->b_ffname != NULL
9846#ifdef FEAT_QUICKFIX
9847 && !bt_nofile(wp->w_buffer)
9848#endif
9849 )
9850 {
9851 /*
9852 * Editing a file in this buffer: use ":edit file".
9853 * This may have side effects! (e.g., compressed or network file).
9854 */
9855 if (fputs("edit ", fd) < 0
9856 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9857 return FAIL;
9858 }
9859 else
9860 {
9861 /* No file in this buffer, just make it empty. */
9862 if (put_line(fd, "enew") == FAIL)
9863 return FAIL;
9864#ifdef FEAT_QUICKFIX
9865 if (wp->w_buffer->b_ffname != NULL)
9866 {
9867 /* The buffer does have a name, but it's not a file name. */
9868 if (fputs("file ", fd) < 0
9869 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9870 return FAIL;
9871 }
9872#endif
9873 do_cursor = FALSE;
9874 }
9875 }
9876
9877 /*
9878 * Local mappings and abbreviations.
9879 */
9880 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9881 && makemap(fd, wp->w_buffer) == FAIL)
9882 return FAIL;
9883
9884 /*
9885 * Local options. Need to go to the window temporarily.
9886 * Store only local values when using ":mkview" and when ":mksession" is
9887 * used and 'sessionoptions' doesn't include "options".
9888 * Some folding options are always stored when "folds" is included,
9889 * otherwise the folds would not be restored correctly.
9890 */
9891 save_curwin = curwin;
9892 curwin = wp;
9893 curbuf = curwin->w_buffer;
9894 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9895 f = makeset(fd, OPT_LOCAL,
9896 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
9897#ifdef FEAT_FOLDING
9898 else if (*flagp & SSOP_FOLDS)
9899 f = makefoldset(fd);
9900#endif
9901 else
9902 f = OK;
9903 curwin = save_curwin;
9904 curbuf = curwin->w_buffer;
9905 if (f == FAIL)
9906 return FAIL;
9907
9908#ifdef FEAT_FOLDING
9909 /*
9910 * Save Folds when 'buftype' is empty and for help files.
9911 */
9912 if ((*flagp & SSOP_FOLDS)
9913 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +00009914# ifdef FEAT_QUICKFIX
9915 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
9916# endif
9917 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 {
9919 if (put_folds(fd, wp) == FAIL)
9920 return FAIL;
9921 }
9922#endif
9923
9924 /*
9925 * Set the cursor after creating folds, since that moves the cursor.
9926 */
9927 if (do_cursor)
9928 {
9929
9930 /* Restore the cursor line in the file and relatively in the
9931 * window. Don't use "G", it changes the jumplist. */
9932 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
9933 (long)wp->w_cursor.lnum,
9934 (long)(wp->w_cursor.lnum - wp->w_topline),
9935 (long)wp->w_height / 2, (long)wp->w_height) < 0
9936 || put_eol(fd) == FAIL
9937 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
9938 || put_line(fd, "exe s:l") == FAIL
9939 || put_line(fd, "normal! zt") == FAIL
9940 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
9941 || put_eol(fd) == FAIL)
9942 return FAIL;
9943 /* Restore the cursor column and left offset when not wrapping. */
9944 if (wp->w_cursor.col == 0)
9945 {
9946 if (put_line(fd, "normal! 0") == FAIL)
9947 return FAIL;
9948 }
9949 else
9950 {
9951 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
9952 {
9953 if (fprintf(fd,
9954 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
9955 (long)wp->w_cursor.col,
9956 (long)(wp->w_cursor.col - wp->w_leftcol),
9957 (long)wp->w_width / 2, (long)wp->w_width) < 0
9958 || put_eol(fd) == FAIL
9959 || put_line(fd, "if s:c > 0") == FAIL
9960 || fprintf(fd,
9961 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
9962 (long)wp->w_cursor.col) < 0
9963 || put_eol(fd) == FAIL
9964 || put_line(fd, "else") == FAIL
9965 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
9966 || put_eol(fd) == FAIL
9967 || put_line(fd, "endif") == FAIL)
9968 return FAIL;
9969 }
9970 else
9971 {
9972 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
9973 || put_eol(fd) == FAIL)
9974 return FAIL;
9975 }
9976 }
9977 }
9978
9979 /*
9980 * Local directory.
9981 */
9982 if (wp->w_localdir != NULL)
9983 {
9984 if (fputs("lcd ", fd) < 0
9985 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
9986 || put_eol(fd) == FAIL)
9987 return FAIL;
9988 }
9989
9990 return OK;
9991}
9992
9993/*
9994 * Write an argument list to the session file.
9995 * Returns FAIL if writing fails.
9996 */
9997 static int
9998ses_arglist(fd, cmd, gap, fullname, flagp)
9999 FILE *fd;
10000 char *cmd;
10001 garray_T *gap;
10002 int fullname; /* TRUE: use full path name */
10003 unsigned *flagp;
10004{
10005 int i;
10006 char_u buf[MAXPATHL];
10007 char_u *s;
10008
10009 if (gap->ga_len == 0)
10010 return put_line(fd, "silent! argdel *");
10011 if (fputs(cmd, fd) < 0)
10012 return FAIL;
10013 for (i = 0; i < gap->ga_len; ++i)
10014 {
10015 /* NULL file names are skipped (only happens when out of memory). */
10016 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10017 if (s != NULL)
10018 {
10019 if (fullname)
10020 {
10021 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10022 s = buf;
10023 }
10024 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10025 return FAIL;
10026 }
10027 }
10028 return put_eol(fd);
10029}
10030
10031/*
10032 * Write a buffer name to the session file.
10033 * Also ends the line.
10034 * Returns FAIL if writing fails.
10035 */
10036 static int
10037ses_fname(fd, buf, flagp)
10038 FILE *fd;
10039 buf_T *buf;
10040 unsigned *flagp;
10041{
10042 char_u *name;
10043
10044 /* Use the short file name if the current directory is known at the time
10045 * the session file will be sourced. Don't do this for ":mkview", we
10046 * don't know the current directory. */
10047 if (buf->b_sfname != NULL
10048 && flagp == &ssop_flags
10049 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
10050 name = buf->b_sfname;
10051 else
10052 name = buf->b_ffname;
10053 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10054 return FAIL;
10055 return OK;
10056}
10057
10058/*
10059 * Write a file name to the session file.
10060 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10061 * characters.
10062 * Returns FAIL if writing fails.
10063 */
10064 static int
10065ses_put_fname(fd, name, flagp)
10066 FILE *fd;
10067 char_u *name;
10068 unsigned *flagp;
10069{
10070 char_u *sname;
10071 int retval = OK;
10072 int c;
10073
10074 sname = home_replace_save(NULL, name);
10075 if (sname != NULL)
10076 name = sname;
10077 while (*name != NUL)
10078 {
10079#ifdef FEAT_MBYTE
10080 {
10081 int l;
10082
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010083 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 {
10085 /* copy a multibyte char */
10086 while (--l >= 0)
10087 {
10088 if (putc(*name, fd) != *name)
10089 retval = FAIL;
10090 ++name;
10091 }
10092 continue;
10093 }
10094 }
10095#endif
10096 c = *name++;
10097 if (c == '\\' && (*flagp & SSOP_SLASH))
10098 /* change a backslash to a forward slash */
10099 c = '/';
10100 else if ((vim_strchr(escape_chars, c) != NULL
10101#ifdef BACKSLASH_IN_FILENAME
10102 && c != '\\'
10103#endif
10104 ) || c == '#' || c == '%')
10105 {
10106 /* escape a special character with a backslash */
10107 if (putc('\\', fd) != '\\')
10108 retval = FAIL;
10109 }
10110 if (putc(c, fd) != c)
10111 retval = FAIL;
10112 }
10113 vim_free(sname);
10114 return retval;
10115}
10116
10117/*
10118 * ":loadview [nr]"
10119 */
10120 static void
10121ex_loadview(eap)
10122 exarg_T *eap;
10123{
10124 char_u *fname;
10125
10126 fname = get_view_file(*eap->arg);
10127 if (fname != NULL)
10128 {
10129 do_source(fname, FALSE, FALSE);
10130 vim_free(fname);
10131 }
10132}
10133
10134/*
10135 * Get the name of the view file for the current buffer.
10136 */
10137 static char_u *
10138get_view_file(c)
10139 int c;
10140{
10141 int len = 0;
10142 char_u *p, *s;
10143 char_u *retval;
10144 char_u *sname;
10145
10146 if (curbuf->b_ffname == NULL)
10147 {
10148 EMSG(_(e_noname));
10149 return NULL;
10150 }
10151 sname = home_replace_save(NULL, curbuf->b_ffname);
10152 if (sname == NULL)
10153 return NULL;
10154
10155 /*
10156 * We want a file name without separators, because we're not going to make
10157 * a directory.
10158 * "normal" path separator -> "=+"
10159 * "=" -> "=="
10160 * ":" path separator -> "=-"
10161 */
10162 for (p = sname; *p; ++p)
10163 if (*p == '=' || vim_ispathsep(*p))
10164 ++len;
10165 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10166 if (retval != NULL)
10167 {
10168 STRCPY(retval, p_vdir);
10169 add_pathsep(retval);
10170 s = retval + STRLEN(retval);
10171 for (p = sname; *p; ++p)
10172 {
10173 if (*p == '=')
10174 {
10175 *s++ = '=';
10176 *s++ = '=';
10177 }
10178 else if (vim_ispathsep(*p))
10179 {
10180 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010181#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 || defined(VMS)
10183 if (*p == ':')
10184 *s++ = '-';
10185 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010187 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 }
10189 else
10190 *s++ = *p;
10191 }
10192 *s++ = '=';
10193 *s++ = c;
10194 STRCPY(s, ".vim");
10195 }
10196
10197 vim_free(sname);
10198 return retval;
10199}
10200
10201#endif /* FEAT_SESSION */
10202
10203/*
10204 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10205 * Return FAIL for a write error.
10206 */
10207 int
10208put_eol(fd)
10209 FILE *fd;
10210{
10211 if (
10212#ifdef USE_CRNL
10213 (
10214# ifdef MKSESSION_NL
10215 !mksession_nl &&
10216# endif
10217 (putc('\r', fd) < 0)) ||
10218#endif
10219 (putc('\n', fd) < 0))
10220 return FAIL;
10221 return OK;
10222}
10223
10224/*
10225 * Write a line to "fd".
10226 * Return FAIL for a write error.
10227 */
10228 int
10229put_line(fd, s)
10230 FILE *fd;
10231 char *s;
10232{
10233 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10234 return FAIL;
10235 return OK;
10236}
10237
10238#ifdef FEAT_VIMINFO
10239/*
10240 * ":rviminfo" and ":wviminfo".
10241 */
10242 static void
10243ex_viminfo(eap)
10244 exarg_T *eap;
10245{
10246 char_u *save_viminfo;
10247
10248 save_viminfo = p_viminfo;
10249 if (*p_viminfo == NUL)
10250 p_viminfo = (char_u *)"'100";
10251 if (eap->cmdidx == CMD_rviminfo)
10252 {
10253 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
10254 EMSG(_("E195: Cannot open viminfo file for reading"));
10255 }
10256 else
10257 write_viminfo(eap->arg, eap->forceit);
10258 p_viminfo = save_viminfo;
10259}
10260#endif
10261
10262#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010263/*
10264 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010265 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 void
10268dialog_msg(buff, format, fname)
10269 char_u *buff;
10270 char *format;
10271 char_u *fname;
10272{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 if (fname == NULL)
10274 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010275 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276}
10277#endif
10278
10279/*
10280 * ":behave {mswin,xterm}"
10281 */
10282 static void
10283ex_behave(eap)
10284 exarg_T *eap;
10285{
10286 if (STRCMP(eap->arg, "mswin") == 0)
10287 {
10288 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10289 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10290 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10291 set_option_value((char_u *)"keymodel", 0L,
10292 (char_u *)"startsel,stopsel", 0);
10293 }
10294 else if (STRCMP(eap->arg, "xterm") == 0)
10295 {
10296 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10297 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10298 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10299 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10300 }
10301 else
10302 EMSG2(_(e_invarg2), eap->arg);
10303}
10304
10305#ifdef FEAT_AUTOCMD
10306static int filetype_detect = FALSE;
10307static int filetype_plugin = FALSE;
10308static int filetype_indent = FALSE;
10309
10310/*
10311 * ":filetype [plugin] [indent] {on,off,detect}"
10312 * on: Load the filetype.vim file to install autocommands for file types.
10313 * off: Load the ftoff.vim file to remove all autocommands for file types.
10314 * plugin on: load filetype.vim and ftplugin.vim
10315 * plugin off: load ftplugof.vim
10316 * indent on: load filetype.vim and indent.vim
10317 * indent off: load indoff.vim
10318 */
10319 static void
10320ex_filetype(eap)
10321 exarg_T *eap;
10322{
10323 char_u *arg = eap->arg;
10324 int plugin = FALSE;
10325 int indent = FALSE;
10326
10327 if (*eap->arg == NUL)
10328 {
10329 /* Print current status. */
10330 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10331 filetype_detect ? "ON" : "OFF",
10332 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10333 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10334 return;
10335 }
10336
10337 /* Accept "plugin" and "indent" in any order. */
10338 for (;;)
10339 {
10340 if (STRNCMP(arg, "plugin", 6) == 0)
10341 {
10342 plugin = TRUE;
10343 arg = skipwhite(arg + 6);
10344 continue;
10345 }
10346 if (STRNCMP(arg, "indent", 6) == 0)
10347 {
10348 indent = TRUE;
10349 arg = skipwhite(arg + 6);
10350 continue;
10351 }
10352 break;
10353 }
10354 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10355 {
10356 if (*arg == 'o' || !filetype_detect)
10357 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010358 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 filetype_detect = TRUE;
10360 if (plugin)
10361 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010362 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 filetype_plugin = TRUE;
10364 }
10365 if (indent)
10366 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010367 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 filetype_indent = TRUE;
10369 }
10370 }
10371 if (*arg == 'd')
10372 {
10373 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +000010374 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 }
10376 }
10377 else if (STRCMP(arg, "off") == 0)
10378 {
10379 if (plugin || indent)
10380 {
10381 if (plugin)
10382 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010383 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 filetype_plugin = FALSE;
10385 }
10386 if (indent)
10387 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010388 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 filetype_indent = FALSE;
10390 }
10391 }
10392 else
10393 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010394 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 filetype_detect = FALSE;
10396 }
10397 }
10398 else
10399 EMSG2(_(e_invarg2), arg);
10400}
10401
10402/*
10403 * ":setfiletype {name}"
10404 */
10405 static void
10406ex_setfiletype(eap)
10407 exarg_T *eap;
10408{
10409 if (!did_filetype)
10410 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10411}
10412#endif
10413
10414/*ARGSUSED*/
10415 static void
10416ex_digraphs(eap)
10417 exarg_T *eap;
10418{
10419#ifdef FEAT_DIGRAPHS
10420 if (*eap->arg != NUL)
10421 putdigraph(eap->arg);
10422 else
10423 listdigraphs();
10424#else
10425 EMSG(_("E196: No digraphs in this version"));
10426#endif
10427}
10428
10429 static void
10430ex_set(eap)
10431 exarg_T *eap;
10432{
10433 int flags = 0;
10434
10435 if (eap->cmdidx == CMD_setlocal)
10436 flags = OPT_LOCAL;
10437 else if (eap->cmdidx == CMD_setglobal)
10438 flags = OPT_GLOBAL;
10439#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10440 if (cmdmod.browse && flags == 0)
10441 ex_options(eap);
10442 else
10443#endif
10444 (void)do_set(eap->arg, flags);
10445}
10446
10447#ifdef FEAT_SEARCH_EXTRA
10448/*
10449 * ":nohlsearch"
10450 */
10451/*ARGSUSED*/
10452 static void
10453ex_nohlsearch(eap)
10454 exarg_T *eap;
10455{
10456 no_hlsearch = TRUE;
10457 redraw_all_later(NOT_VALID);
10458}
10459
10460/*
10461 * ":match {group} {pattern}"
10462 * Sets nextcmd to the start of the next command, if any. Also called when
10463 * skipping commands to find the next command.
10464 */
10465 static void
10466ex_match(eap)
10467 exarg_T *eap;
10468{
10469 char_u *p;
10470 char_u *end;
10471 int c;
10472
10473 /* First clear any old pattern. */
10474 if (!eap->skip)
10475 {
10476 vim_free(curwin->w_match.regprog);
10477 curwin->w_match.regprog = NULL;
10478 redraw_later(NOT_VALID); /* always need a redraw */
10479 }
10480
10481 if (ends_excmd(*eap->arg))
10482 end = eap->arg;
10483 else if ((STRNICMP(eap->arg, "none", 4) == 0
10484 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10485 end = eap->arg + 4;
10486 else
10487 {
10488 p = skiptowhite(eap->arg);
10489 if (!eap->skip)
10490 {
10491 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg));
10492 if (curwin->w_match_id == 0)
10493 {
10494 EMSG2(_(e_nogroup), eap->arg);
10495 return;
10496 }
10497 }
10498 p = skipwhite(p);
10499 if (*p == NUL)
10500 {
10501 /* There must be two arguments. */
10502 EMSG2(_(e_invarg2), eap->arg);
10503 return;
10504 }
10505 end = skip_regexp(p + 1, *p, TRUE, NULL);
10506 if (!eap->skip)
10507 {
10508 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10509 {
10510 eap->errmsg = e_trailing;
10511 return;
10512 }
10513
10514 c = *end;
10515 *end = NUL;
10516 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC);
10517 *end = c;
10518 if (curwin->w_match.regprog == NULL)
10519 {
10520 EMSG2(_(e_invarg2), p);
10521 return;
10522 }
10523 }
10524 }
10525 eap->nextcmd = find_nextcmd(end);
10526}
10527#endif
10528
10529#ifdef FEAT_CRYPT
10530/*
10531 * ":X": Get crypt key
10532 */
10533/*ARGSUSED*/
10534 static void
10535ex_X(eap)
10536 exarg_T *eap;
10537{
10538 (void)get_crypt_key(TRUE, TRUE);
10539}
10540#endif
10541
10542#ifdef FEAT_FOLDING
10543 static void
10544ex_fold(eap)
10545 exarg_T *eap;
10546{
10547 if (foldManualAllowed(TRUE))
10548 foldCreate(eap->line1, eap->line2);
10549}
10550
10551 static void
10552ex_foldopen(eap)
10553 exarg_T *eap;
10554{
10555 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10556 eap->forceit, FALSE);
10557}
10558
10559 static void
10560ex_folddo(eap)
10561 exarg_T *eap;
10562{
10563 linenr_T lnum;
10564
10565 /* First set the marks for all lines closed/open. */
10566 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10567 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10568 ml_setmarked(lnum);
10569
10570 /* Execute the command on the marked lines. */
10571 global_exe(eap->arg);
10572 ml_clearmarked(); /* clear rest of the marks */
10573}
10574#endif