blob: 195f19d7cf1ecb59aebee6a6b64cbc197eb8a630 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
16#ifdef HAVE_FCNTL_H
17# include <fcntl.h> /* for chdir() */
18#endif
19
20static int quitmore = 0;
21static int ex_pressedreturn = FALSE;
22#ifndef FEAT_PRINTER
23# define ex_hardcopy ex_ni
24#endif
25
26#ifdef FEAT_USR_CMDS
27typedef struct ucmd
28{
29 char_u *uc_name; /* The command name */
30 long_u uc_argt; /* The argument type */
31 char_u *uc_rep; /* The command's replacement string */
32 long uc_def; /* The default value for a range/count */
33 scid_T uc_scriptID; /* SID where the command was defined */
34 int uc_compl; /* completion type */
35# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
36 char_u *uc_compl_arg; /* completion argument if any */
37# endif
38} ucmd_T;
39
40#define UC_BUFFER 1 /* -buffer: local to current buffer */
41
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000042static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
44#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
45#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
46
47static void do_ucmd __ARGS((exarg_T *eap));
48static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000049static void ex_delcommand __ARGS((exarg_T *eap));
50# ifdef FEAT_CMDL_COMPL
51static char_u *get_user_command_name __ARGS((int idx));
52# endif
53
54#else
55# define ex_command ex_ni
56# define ex_comclear ex_ni
57# define ex_delcommand ex_ni
58#endif
59
60#ifdef FEAT_EVAL
61static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*getline)(int, void *, int), void *cookie));
62#else
63static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*getline)(int, void *, int), void *cookie));
64static int if_level = 0; /* depth in :if */
65#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static char_u *find_command __ARGS((exarg_T *eap, int *full));
67
68static void ex_abbreviate __ARGS((exarg_T *eap));
69static void ex_map __ARGS((exarg_T *eap));
70static void ex_unmap __ARGS((exarg_T *eap));
71static void ex_mapclear __ARGS((exarg_T *eap));
72static void ex_abclear __ARGS((exarg_T *eap));
73#ifndef FEAT_MENU
74# define ex_emenu ex_ni
75# define ex_menu ex_ni
76# define ex_menutranslate ex_ni
77#endif
78#ifdef FEAT_AUTOCMD
79static void ex_autocmd __ARGS((exarg_T *eap));
80static void ex_doautocmd __ARGS((exarg_T *eap));
81#else
82# define ex_autocmd ex_ni
83# define ex_doautocmd ex_ni
84# define ex_doautoall ex_ni
85#endif
86#ifdef FEAT_LISTCMDS
87static void ex_bunload __ARGS((exarg_T *eap));
88static void ex_buffer __ARGS((exarg_T *eap));
89static void ex_bmodified __ARGS((exarg_T *eap));
90static void ex_bnext __ARGS((exarg_T *eap));
91static void ex_bprevious __ARGS((exarg_T *eap));
92static void ex_brewind __ARGS((exarg_T *eap));
93static void ex_blast __ARGS((exarg_T *eap));
94#else
95# define ex_bunload ex_ni
96# define ex_buffer ex_ni
97# define ex_bmodified ex_ni
98# define ex_bnext ex_ni
99# define ex_bprevious ex_ni
100# define ex_brewind ex_ni
101# define ex_blast ex_ni
102# define buflist_list ex_ni
103# define ex_checktime ex_ni
104#endif
105#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
106# define ex_buffer_all ex_ni
107#endif
108static char_u *getargcmd __ARGS((char_u **));
109static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
110static int getargopt __ARGS((exarg_T *eap));
111#ifndef FEAT_QUICKFIX
112# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000113# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114# define ex_cc ex_ni
115# define ex_cnext ex_ni
116# define ex_cfile ex_ni
117# define qf_list ex_ni
118# define qf_age ex_ni
119# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000120# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#endif
122#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
123# define ex_cclose ex_ni
124# define ex_copen ex_ni
125# define ex_cwindow ex_ni
126#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000127#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
128# define ex_cexpr ex_ni
129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
131static int check_more __ARGS((int, int));
132static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000133static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000135 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136static void ex_script_ni __ARGS((exarg_T *eap));
137#endif
138static char_u *invalid_range __ARGS((exarg_T *eap));
139static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000140#ifdef FEAT_QUICKFIX
141static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
144static void ex_highlight __ARGS((exarg_T *eap));
145static void ex_colorscheme __ARGS((exarg_T *eap));
146static void ex_quit __ARGS((exarg_T *eap));
147static void ex_cquit __ARGS((exarg_T *eap));
148static void ex_quit_all __ARGS((exarg_T *eap));
149#ifdef FEAT_WINDOWS
150static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000151static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152static void ex_only __ARGS((exarg_T *eap));
153static void ex_all __ARGS((exarg_T *eap));
154static void ex_resize __ARGS((exarg_T *eap));
155static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000156static void ex_tabclose __ARGS((exarg_T *eap));
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 Moolenaarf740b292006-02-16 22:11:02 +00006147 ex_win_close(eap->forceit, curwin, NULL);
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 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006163 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006164 break;
6165 }
6166}
6167#endif
6168
Bram Moolenaarf740b292006-02-16 22:11:02 +00006169/*
6170 * Close window "win" and take care of handling closing the last window for a
6171 * modified buffer.
6172 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006173 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006174ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006175 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006177 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178{
6179 int need_hide;
6180 buf_T *buf = win->w_buffer;
6181
6182 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006183 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 {
6185#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6186 if ((p_confirm || cmdmod.confirm) && p_write)
6187 {
6188 dialog_changed(buf, FALSE);
6189 if (buf_valid(buf) && bufIsChanged(buf))
6190 return;
6191 need_hide = FALSE;
6192 }
6193 else
6194#endif
6195 {
6196 EMSG(_(e_nowrtmsg));
6197 return;
6198 }
6199 }
6200
6201#ifdef FEAT_GUI
6202 need_mouse_correct = TRUE;
6203#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006204
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006206 if (tp == NULL)
6207 win_close(win, !need_hide && !P_HID(buf));
6208 else
6209 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210}
6211
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006213 * ":tabclose": close current tab page, unless it is the last one.
6214 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 */
6216 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006217ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 exarg_T *eap;
6219{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006220 tabpage_T *tp;
6221
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006222# ifdef FEAT_CMDWIN
6223 if (cmdwin_type != 0)
6224 cmdwin_result = K_IGNORE;
6225 else
6226# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006227 if (first_tabpage->tp_next == NULL)
6228 EMSG(_("E999: Cannot close last tab page"));
6229 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006231 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006232 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006233 tp = find_tabpage((int)eap->line2);
6234 if (tp == NULL)
6235 {
6236 beep_flush();
6237 return;
6238 }
6239 if (tp->tp_topframe != topframe)
6240 {
6241 tabpage_close_other(tp, eap->forceit);
6242 return;
6243 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006244 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00006245 if (!text_locked())
6246 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 }
6248}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249
6250/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006251 * Close the current tab page.
6252 */
6253 void
6254tabpage_close(forceit)
6255 int forceit;
6256{
6257 /* First close all the windows but the current one. If that worked then
6258 * close the last window in this tab, that will close it. */
6259 close_others(TRUE, forceit);
6260 if (lastwin == firstwin)
6261 ex_win_close(forceit, curwin, NULL);
6262# ifdef FEAT_GUI
6263 need_mouse_correct = TRUE;
6264# endif
6265}
6266
6267/*
6268 * Close tab page "tp", which is not the current tab page.
6269 * Note that autocommands may make "tp" invalid.
6270 */
6271 void
6272tabpage_close_other(tp, forceit)
6273 tabpage_T *tp;
6274 int forceit;
6275{
6276 int done = 0;
6277
6278 /* Limit to 1000 windows, autocommands may add a window while we close
6279 * one. OK, so I'm paranoid... */
6280 while (++done < 1000)
6281 {
6282 ex_win_close(forceit, tp->tp_firstwin, tp);
6283
6284 /* Autocommands may delete the tab page under our fingers. */
6285 if (!valid_tabpage(tp))
6286 break;
6287 }
6288}
6289
6290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 * ":only".
6292 */
6293 static void
6294ex_only(eap)
6295 exarg_T *eap;
6296{
6297# ifdef FEAT_GUI
6298 need_mouse_correct = TRUE;
6299# endif
6300 close_others(TRUE, eap->forceit);
6301}
6302
6303/*
6304 * ":all" and ":sall".
6305 */
6306 static void
6307ex_all(eap)
6308 exarg_T *eap;
6309{
6310 if (eap->addr_count == 0)
6311 eap->line2 = 9999;
6312 do_arg_all((int)eap->line2, eap->forceit);
6313}
6314#endif /* FEAT_WINDOWS */
6315
6316 static void
6317ex_hide(eap)
6318 exarg_T *eap;
6319{
6320 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6321 eap->errmsg = e_invarg;
6322 else
6323 {
6324 /* ":hide" or ":hide | cmd": hide current window */
6325 eap->nextcmd = check_nextcmd(eap->arg);
6326#ifdef FEAT_WINDOWS
6327 if (!eap->skip)
6328 {
6329# ifdef FEAT_GUI
6330 need_mouse_correct = TRUE;
6331# endif
6332 win_close(curwin, FALSE); /* don't free buffer */
6333 }
6334#endif
6335 }
6336}
6337
6338/*
6339 * ":stop" and ":suspend": Suspend Vim.
6340 */
6341 static void
6342ex_stop(eap)
6343 exarg_T *eap;
6344{
6345 /*
6346 * Disallow suspending for "rvim".
6347 */
6348 if (!check_restricted()
6349#ifdef WIN3264
6350 /*
6351 * Check if external commands are allowed now.
6352 */
6353 && can_end_termcap_mode(TRUE)
6354#endif
6355 )
6356 {
6357 if (!eap->forceit)
6358 autowrite_all();
6359 windgoto((int)Rows - 1, 0);
6360 out_char('\n');
6361 out_flush();
6362 stoptermcap();
6363 out_flush(); /* needed for SUN to restore xterm buffer */
6364#ifdef FEAT_TITLE
6365 mch_restore_title(3); /* restore window titles */
6366#endif
6367 ui_suspend(); /* call machine specific function */
6368#ifdef FEAT_TITLE
6369 maketitle();
6370 resettitle(); /* force updating the title */
6371#endif
6372 starttermcap();
6373 scroll_start(); /* scroll screen before redrawing */
6374 redraw_later_clear();
6375 shell_resized(); /* may have resized window */
6376 }
6377}
6378
6379/*
6380 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6381 */
6382 static void
6383ex_exit(eap)
6384 exarg_T *eap;
6385{
6386#ifdef FEAT_CMDWIN
6387 if (cmdwin_type != 0)
6388 {
6389 cmdwin_result = Ctrl_C;
6390 return;
6391 }
6392#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006393 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006394 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006395 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006396 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006397 return;
6398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399
6400 /*
6401 * if more files or windows we won't exit
6402 */
6403 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6404 exiting = TRUE;
6405 if ( ((eap->cmdidx == CMD_wq
6406 || curbufIsChanged())
6407 && do_write(eap) == FAIL)
6408 || check_more(TRUE, eap->forceit) == FAIL
6409 || (only_one_window() && check_changed_any(eap->forceit)))
6410 {
6411 not_exiting();
6412 }
6413 else
6414 {
6415#ifdef FEAT_WINDOWS
6416 if (only_one_window()) /* quit last window, exit Vim */
6417#endif
6418 getout(0);
6419#ifdef FEAT_WINDOWS
6420# ifdef FEAT_GUI
6421 need_mouse_correct = TRUE;
6422# endif
6423 /* quit current window, may free buffer */
6424 win_close(curwin, !P_HID(curwin->w_buffer));
6425#endif
6426 }
6427}
6428
6429/*
6430 * ":print", ":list", ":number".
6431 */
6432 static void
6433ex_print(eap)
6434 exarg_T *eap;
6435{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006436 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6437 EMSG(_(e_emptybuf));
6438 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006440 for ( ;!got_int; ui_breakcheck())
6441 {
6442 print_line(eap->line1,
6443 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6444 || (eap->flags & EXFLAG_NR)),
6445 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6446 if (++eap->line1 > eap->line2)
6447 break;
6448 out_flush(); /* show one line at a time */
6449 }
6450 setpcmark();
6451 /* put cursor at last line */
6452 curwin->w_cursor.lnum = eap->line2;
6453 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 }
6455
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457}
6458
6459#ifdef FEAT_BYTEOFF
6460 static void
6461ex_goto(eap)
6462 exarg_T *eap;
6463{
6464 goto_byte(eap->line2);
6465}
6466#endif
6467
6468/*
6469 * ":shell".
6470 */
6471/*ARGSUSED*/
6472 static void
6473ex_shell(eap)
6474 exarg_T *eap;
6475{
6476 do_shell(NULL, 0);
6477}
6478
6479#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6480 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006481 || defined(FEAT_GUI_MSWIN) \
6482 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 || defined(PROTO)
6484
6485/*
6486 * Handle a file drop. The code is here because a drop is *nearly* like an
6487 * :args command, but not quite (we have a list of exact filenames, so we
6488 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6489 * code is very similar to :args and hence needs access to a lot of the static
6490 * functions in this file.
6491 *
6492 * The list should be allocated using alloc(), as should each item in the
6493 * list. This function takes over responsibility for freeing the list.
6494 *
6495 * XXX The list is made into the arggument list. This is freed using
6496 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6497 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6498 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6499 * file functionality is (currently) not in EMX this is not presently a
6500 * problem.
6501 */
6502 void
6503handle_drop(filec, filev, split)
6504 int filec; /* the number of files dropped */
6505 char_u **filev; /* the list of files dropped */
6506 int split; /* force splitting the window */
6507{
6508 exarg_T ea;
6509 int save_msg_scroll = msg_scroll;
6510
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006511 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006512 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514
6515 /* Check whether the current buffer is changed. If so, we will need
6516 * to split the current window or data could be lost.
6517 * We don't need to check if the 'hidden' option is set, as in this
6518 * case the buffer won't be lost.
6519 */
6520 if (!P_HID(curbuf) && !split)
6521 {
6522 ++emsg_off;
6523 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6524 --emsg_off;
6525 }
6526 if (split)
6527 {
6528# ifdef FEAT_WINDOWS
6529 if (win_split(0, 0) == FAIL)
6530 return;
6531# ifdef FEAT_SCROLLBIND
6532 curwin->w_p_scb = FALSE;
6533# endif
6534
6535 /* When splitting the window, create a new alist. Otherwise the
6536 * existing one is overwritten. */
6537 alist_unlink(curwin->w_alist);
6538 alist_new();
6539# else
6540 return; /* can't split, always fail */
6541# endif
6542 }
6543
6544 /*
6545 * Set up the new argument list.
6546 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006547 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
6549 /*
6550 * Move to the first file.
6551 */
6552 /* Fake up a minimal "next" command for do_argfile() */
6553 vim_memset(&ea, 0, sizeof(ea));
6554 ea.cmd = (char_u *)"next";
6555 do_argfile(&ea, 0);
6556
6557 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6558 * mode that is not needed here. */
6559 need_start_insertmode = FALSE;
6560
6561 /* Restore msg_scroll, otherwise a following command may cause scrolling
6562 * unexpectedly. The screen will be redrawn by the caller, thus
6563 * msg_scroll being set by displaying a message is irrelevant. */
6564 msg_scroll = save_msg_scroll;
6565}
6566#endif
6567
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568/*
6569 * Clear an argument list: free all file names and reset it to zero entries.
6570 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006571 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572alist_clear(al)
6573 alist_T *al;
6574{
6575 while (--al->al_ga.ga_len >= 0)
6576 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6577 ga_clear(&al->al_ga);
6578}
6579
6580/*
6581 * Init an argument list.
6582 */
6583 void
6584alist_init(al)
6585 alist_T *al;
6586{
6587 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6588}
6589
6590#if defined(FEAT_WINDOWS) || defined(PROTO)
6591
6592/*
6593 * Remove a reference from an argument list.
6594 * Ignored when the argument list is the global one.
6595 * If the argument list is no longer used by any window, free it.
6596 */
6597 void
6598alist_unlink(al)
6599 alist_T *al;
6600{
6601 if (al != &global_alist && --al->al_refcount <= 0)
6602 {
6603 alist_clear(al);
6604 vim_free(al);
6605 }
6606}
6607
6608# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6609/*
6610 * Create a new argument list and use it for the current window.
6611 */
6612 void
6613alist_new()
6614{
6615 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6616 if (curwin->w_alist == NULL)
6617 {
6618 curwin->w_alist = &global_alist;
6619 ++global_alist.al_refcount;
6620 }
6621 else
6622 {
6623 curwin->w_alist->al_refcount = 1;
6624 alist_init(curwin->w_alist);
6625 }
6626}
6627# endif
6628#endif
6629
6630#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6631/*
6632 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006633 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6634 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 */
6636 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006637alist_expand(fnum_list, fnum_len)
6638 int *fnum_list;
6639 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640{
6641 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006642 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 char_u **new_arg_files;
6644 int new_arg_file_count;
6645 char_u *save_p_su = p_su;
6646 int i;
6647
6648 /* Don't use 'suffixes' here. This should work like the shell did the
6649 * expansion. Also, the vimrc file isn't read yet, thus the user
6650 * can't set the options. */
6651 p_su = empty_option;
6652 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6653 if (old_arg_files != NULL)
6654 {
6655 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006656 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6657 old_arg_count = GARGCOUNT;
6658 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 &new_arg_file_count, &new_arg_files,
6660 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6661 && new_arg_file_count > 0)
6662 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006663 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6664 TRUE, fnum_list, fnum_len);
6665 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 }
6668 p_su = save_p_su;
6669}
6670#endif
6671
6672/*
6673 * Set the argument list for the current window.
6674 * Takes over the allocated files[] and the allocated fnames in it.
6675 */
6676 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006677alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 alist_T *al;
6679 int count;
6680 char_u **files;
6681 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006682 int *fnum_list;
6683 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684{
6685 int i;
6686
6687 alist_clear(al);
6688 if (ga_grow(&al->al_ga, count) == OK)
6689 {
6690 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006691 {
6692 if (got_int)
6693 {
6694 /* When adding many buffers this can take a long time. Allow
6695 * interrupting here. */
6696 while (i < count)
6697 vim_free(files[i++]);
6698 break;
6699 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006700
6701 /* May set buffer name of a buffer previously used for the
6702 * argument list, so that it's re-used by alist_add. */
6703 if (fnum_list != NULL && i < fnum_len)
6704 buf_set_name(fnum_list[i], files[i]);
6705
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006707 ui_breakcheck();
6708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 vim_free(files);
6710 }
6711 else
6712 FreeWild(count, files);
6713#ifdef FEAT_WINDOWS
6714 if (al == &global_alist)
6715#endif
6716 arg_had_last = FALSE;
6717}
6718
6719/*
6720 * Add file "fname" to argument list "al".
6721 * "fname" must have been allocated and "al" must have been checked for room.
6722 */
6723 void
6724alist_add(al, fname, set_fnum)
6725 alist_T *al;
6726 char_u *fname;
6727 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6728{
6729 if (fname == NULL) /* don't add NULL file names */
6730 return;
6731#ifdef BACKSLASH_IN_FILENAME
6732 slash_adjust(fname);
6733#endif
6734 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6735 if (set_fnum > 0)
6736 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6737 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6738 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739}
6740
6741#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6742/*
6743 * Adjust slashes in file names. Called after 'shellslash' was set.
6744 */
6745 void
6746alist_slash_adjust()
6747{
6748 int i;
6749# ifdef FEAT_WINDOWS
6750 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006751 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752# endif
6753
6754 for (i = 0; i < GARGCOUNT; ++i)
6755 if (GARGLIST[i].ae_fname != NULL)
6756 slash_adjust(GARGLIST[i].ae_fname);
6757# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006758 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 if (wp->w_alist != &global_alist)
6760 for (i = 0; i < WARGCOUNT(wp); ++i)
6761 if (WARGLIST(wp)[i].ae_fname != NULL)
6762 slash_adjust(WARGLIST(wp)[i].ae_fname);
6763# endif
6764}
6765#endif
6766
6767/*
6768 * ":preserve".
6769 */
6770/*ARGSUSED*/
6771 static void
6772ex_preserve(eap)
6773 exarg_T *eap;
6774{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006775 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 ml_preserve(curbuf, TRUE);
6777}
6778
6779/*
6780 * ":recover".
6781 */
6782 static void
6783ex_recover(eap)
6784 exarg_T *eap;
6785{
6786 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6787 recoverymode = TRUE;
6788 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6789 && (*eap->arg == NUL
6790 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6791 ml_recover();
6792 recoverymode = FALSE;
6793}
6794
6795/*
6796 * Command modifier used in a wrong way.
6797 */
6798 static void
6799ex_wrongmodifier(eap)
6800 exarg_T *eap;
6801{
6802 eap->errmsg = e_invcmd;
6803}
6804
6805#ifdef FEAT_WINDOWS
6806/*
6807 * :sview [+command] file split window with new file, read-only
6808 * :split [[+command] file] split window with current or new file
6809 * :vsplit [[+command] file] split window vertically with current or new file
6810 * :new [[+command] file] split window with no or new file
6811 * :vnew [[+command] file] split vertically window with no or new file
6812 * :sfind [+command] file split window with file in 'path'
6813 */
6814 void
6815ex_splitview(eap)
6816 exarg_T *eap;
6817{
6818 win_T *old_curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006819# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006821# endif
6822# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006824# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006826# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6828 {
6829 ex_ni(eap);
6830 return;
6831 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833
6834 old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006835# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006839# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 /* A ":split" in the quickfix window works like ":new". Don't want two
6841 * quickfix windows. */
6842 if (bt_quickfix(curbuf))
6843 {
6844 if (eap->cmdidx == CMD_split)
6845 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006846# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 if (eap->cmdidx == CMD_vsplit)
6848 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006853# ifdef FEAT_SEARCHPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 if (eap->cmdidx == CMD_sfind)
6855 {
6856 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6857 FNAME_MESS, TRUE, curbuf->b_ffname);
6858 if (fname == NULL)
6859 goto theend;
6860 eap->arg = fname;
6861 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006862# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006864# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006866# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006868# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 && eap->cmdidx != CMD_new)
6872 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006873 if (
6874# ifdef FEAT_GUI
6875 !gui.in_use &&
6876# endif
6877 au_has_group((char_u *)"FileExplorer"))
6878 {
6879 /* No browsing supported but we do have the file explorer:
6880 * Edit the directory. */
6881 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6882 eap->arg = (char_u *)".";
6883 }
6884 else
6885 {
6886 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006888 if (fname == NULL)
6889 goto theend;
6890 eap->arg = fname;
6891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 }
6893 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006894# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895
6896 if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
6897 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6898 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006899# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 /* Reset 'scrollbind' when editing another file, but keep it when
6901 * doing ":split" without arguments. */
6902 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006903# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 )
6907 curwin->w_p_scb = FALSE;
6908 else
6909 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006910# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 do_exedit(eap, old_curwin);
6912 }
6913
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006914# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006916# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006918# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919theend:
6920 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006921# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006923
6924/*
6925 * :tabedit [[+command] file] open new Tab page with empty window
6926 * :tabedit [[+command] file] open new Tab page and edit "file"
6927 * :tabfind [[+command] file] open new Tab page and find "file"
6928 */
6929 void
6930ex_tabedit(eap)
6931 exarg_T *eap;
6932{
6933# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6934 char_u *fname = NULL;
6935# endif
6936# ifdef FEAT_BROWSE
6937 int browse_flag = cmdmod.browse;
6938# endif
6939
6940# ifdef FEAT_GUI
6941 need_mouse_correct = TRUE;
6942# endif
6943
6944# ifdef FEAT_SEARCHPATH
6945 if (eap->cmdidx == CMD_tabfind)
6946 {
6947 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6948 FNAME_MESS, TRUE, curbuf->b_ffname);
6949 if (fname == NULL)
6950 goto theend;
6951 eap->arg = fname;
6952 }
6953# ifdef FEAT_BROWSE
6954 else
6955# endif
6956# endif
6957# ifdef FEAT_BROWSE
6958 if (cmdmod.browse)
6959 {
6960 if (
6961# ifdef FEAT_GUI
6962 !gui.in_use &&
6963# endif
6964 au_has_group((char_u *)"FileExplorer"))
6965 {
6966 /* No browsing supported but we do have the file explorer:
6967 * Edit the directory. */
6968 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6969 eap->arg = (char_u *)".";
6970 }
6971 else
6972 {
6973 fname = do_browse(0, (char_u *)_("Edit File in new tab page"),
6974 eap->arg, NULL, NULL, NULL, curbuf);
6975 if (fname == NULL)
6976 goto theend;
6977 eap->arg = fname;
6978 }
6979 }
6980 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
6981# endif
6982
6983 if (win_new_tabpage() != FAIL)
6984 {
6985# ifdef FEAT_SCROLLBIND
6986 curwin->w_p_scb = FALSE;
6987# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006988 if (*eap->arg != NUL)
6989 do_exedit(eap, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006990 }
6991
6992# ifdef FEAT_BROWSE
6993 cmdmod.browse = browse_flag;
6994# endif
6995
6996# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6997theend:
6998 vim_free(fname);
6999# endif
7000}
7001
7002/*
7003 * :tab command
7004 */
7005 void
7006ex_tab(eap)
7007 exarg_T *eap;
7008{
7009 goto_tabpage((int)eap->line2);
7010}
7011
7012/*
7013 * :tabs command: List tabs and their contents.
7014 */
7015/*ARGSUSED*/
7016 static void
7017ex_tabs(eap)
7018 exarg_T *eap;
7019{
7020 tabpage_T *tp;
7021 win_T *wp;
7022 int tabcount = 1;
7023
7024 msg_start();
7025 msg_scroll = TRUE;
7026 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7027 {
7028 msg_putchar('\n');
7029 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7030 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7031 out_flush(); /* output one line at a time */
7032 ui_breakcheck();
7033
7034 if (tp->tp_topframe == topframe)
7035 wp = firstwin;
7036 else
7037 wp = tp->tp_firstwin;
7038 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7039 {
7040 msg_puts((char_u *)"\n ");
7041 if (buf_spname(wp->w_buffer) != NULL)
7042 STRCPY(IObuff, buf_spname(wp->w_buffer));
7043 else
7044 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7045 IObuff, IOSIZE, TRUE);
7046 msg_outtrans(IObuff);
7047 out_flush(); /* output one line at a time */
7048 ui_breakcheck();
7049 }
7050 }
7051}
7052
7053#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054
7055/*
7056 * ":mode": Set screen mode.
7057 * If no argument given, just get the screen size and redraw.
7058 */
7059 static void
7060ex_mode(eap)
7061 exarg_T *eap;
7062{
7063 if (*eap->arg == NUL)
7064 shell_resized();
7065 else
7066 mch_screenmode(eap->arg);
7067}
7068
7069#ifdef FEAT_WINDOWS
7070/*
7071 * ":resize".
7072 * set, increment or decrement current window height
7073 */
7074 static void
7075ex_resize(eap)
7076 exarg_T *eap;
7077{
7078 int n;
7079 win_T *wp = curwin;
7080
7081 if (eap->addr_count > 0)
7082 {
7083 n = eap->line2;
7084 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7085 ;
7086 }
7087
7088#ifdef FEAT_GUI
7089 need_mouse_correct = TRUE;
7090#endif
7091 n = atol((char *)eap->arg);
7092#ifdef FEAT_VERTSPLIT
7093 if (cmdmod.split & WSP_VERT)
7094 {
7095 if (*eap->arg == '-' || *eap->arg == '+')
7096 n += W_WIDTH(curwin);
7097 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7098 n = 9999;
7099 win_setwidth_win((int)n, wp);
7100 }
7101 else
7102#endif
7103 {
7104 if (*eap->arg == '-' || *eap->arg == '+')
7105 n += curwin->w_height;
7106 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7107 n = 9999;
7108 win_setheight_win((int)n, wp);
7109 }
7110}
7111#endif
7112
7113/*
7114 * ":find [+command] <file>" command.
7115 */
7116 static void
7117ex_find(eap)
7118 exarg_T *eap;
7119{
7120#ifdef FEAT_SEARCHPATH
7121 char_u *fname;
7122 int count;
7123
7124 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7125 TRUE, curbuf->b_ffname);
7126 if (eap->addr_count > 0)
7127 {
7128 /* Repeat finding the file "count" times. This matters when it
7129 * appears several times in the path. */
7130 count = eap->line2;
7131 while (fname != NULL && --count > 0)
7132 {
7133 vim_free(fname);
7134 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7135 FALSE, curbuf->b_ffname);
7136 }
7137 }
7138
7139 if (fname != NULL)
7140 {
7141 eap->arg = fname;
7142#endif
7143 do_exedit(eap, NULL);
7144#ifdef FEAT_SEARCHPATH
7145 vim_free(fname);
7146 }
7147#endif
7148}
7149
7150/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007151 * ":open" simulation: for now just work like ":visual".
7152 */
7153 static void
7154ex_open(eap)
7155 exarg_T *eap;
7156{
7157 regmatch_T regmatch;
7158 char_u *p;
7159
7160 curwin->w_cursor.lnum = eap->line2;
7161 beginline(BL_SOL | BL_FIX);
7162 if (*eap->arg == '/')
7163 {
7164 /* ":open /pattern/": put cursor in column found with pattern */
7165 ++eap->arg;
7166 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7167 *p = NUL;
7168 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7169 if (regmatch.regprog != NULL)
7170 {
7171 regmatch.rm_ic = p_ic;
7172 p = ml_get_curline();
7173 if (vim_regexec(&regmatch, p, (colnr_T)0))
7174 curwin->w_cursor.col = regmatch.startp[0] - p;
7175 else
7176 EMSG(_(e_nomatch));
7177 vim_free(regmatch.regprog);
7178 }
7179 /* Move to the NUL, ignore any other arguments. */
7180 eap->arg += STRLEN(eap->arg);
7181 }
7182 check_cursor();
7183
7184 eap->cmdidx = CMD_visual;
7185 do_exedit(eap, NULL);
7186}
7187
7188/*
7189 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 */
7191 static void
7192ex_edit(eap)
7193 exarg_T *eap;
7194{
7195 do_exedit(eap, NULL);
7196}
7197
7198/*
7199 * ":edit <file>" command and alikes.
7200 */
7201/*ARGSUSED*/
7202 void
7203do_exedit(eap, old_curwin)
7204 exarg_T *eap;
7205 win_T *old_curwin; /* curwin before doing a split or NULL */
7206{
7207 int n;
7208#ifdef FEAT_WINDOWS
7209 int need_hide;
7210#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007211 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
7213 /*
7214 * ":vi" command ends Ex mode.
7215 */
7216 if (exmode_active && (eap->cmdidx == CMD_visual
7217 || eap->cmdidx == CMD_view))
7218 {
7219 exmode_active = FALSE;
7220 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007221 {
7222 /* Special case: ":global/pat/visual\NLvi-commands" */
7223 if (global_busy)
7224 {
7225 int rd = RedrawingDisabled;
7226 int nwr = no_wait_return;
7227 int ms = msg_scroll;
7228#ifdef FEAT_GUI
7229 int he = hold_gui_events;
7230#endif
7231
7232 if (eap->nextcmd != NULL)
7233 {
7234 stuffReadbuff(eap->nextcmd);
7235 eap->nextcmd = NULL;
7236 }
7237
7238 if (exmode_was != EXMODE_VIM)
7239 settmode(TMODE_RAW);
7240 RedrawingDisabled = 0;
7241 no_wait_return = 0;
7242 need_wait_return = FALSE;
7243 msg_scroll = 0;
7244#ifdef FEAT_GUI
7245 hold_gui_events = 0;
7246#endif
7247 must_redraw = CLEAR;
7248
7249 main_loop(FALSE, TRUE);
7250
7251 RedrawingDisabled = rd;
7252 no_wait_return = nwr;
7253 msg_scroll = ms;
7254#ifdef FEAT_GUI
7255 hold_gui_events = he;
7256#endif
7257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 }
7261
7262 if ((eap->cmdidx == CMD_new
7263#ifdef FEAT_VERTSPLIT
7264 || eap->cmdidx == CMD_vnew
7265#endif
7266 ) && *eap->arg == NUL)
7267 {
7268 /* ":new" without argument: edit an new empty buffer */
7269 setpcmark();
7270 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
7271 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
7272 }
7273 else if ((eap->cmdidx != CMD_split
7274#ifdef FEAT_VERTSPLIT
7275 && eap->cmdidx != CMD_vsplit
7276#endif
7277 )
7278 || *eap->arg != NUL
7279#ifdef FEAT_BROWSE
7280 || cmdmod.browse
7281#endif
7282 )
7283 {
7284 n = readonlymode;
7285 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7286 readonlymode = TRUE;
7287 else if (eap->cmdidx == CMD_enew)
7288 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7289 empty buffer */
7290 setpcmark();
7291 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7292 NULL, eap,
7293 /* ":edit" goes to first line if Vi compatible */
7294 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7295 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7296 ? ECMD_ONE : eap->do_ecmd_lnum,
7297 (P_HID(curbuf) ? ECMD_HIDE : 0)
7298 + (eap->forceit ? ECMD_FORCEIT : 0)
7299#ifdef FEAT_LISTCMDS
7300 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7301#endif
7302 ) == FAIL)
7303 {
7304 /* Editing the file failed. If the window was split, close it. */
7305#ifdef FEAT_WINDOWS
7306 if (old_curwin != NULL)
7307 {
7308 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7309 if (!need_hide || P_HID(curbuf))
7310 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007311# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7312 cleanup_T cs;
7313
7314 /* Reset the error/interrupt/exception state here so that
7315 * aborting() returns FALSE when closing a window. */
7316 enter_cleanup(&cs);
7317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318# ifdef FEAT_GUI
7319 need_mouse_correct = TRUE;
7320# endif
7321 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007322
7323# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7324 /* Restore the error/interrupt/exception state if not
7325 * discarded by a new aborting error, interrupt, or
7326 * uncaught exception. */
7327 leave_cleanup(&cs);
7328# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 }
7330 }
7331#endif
7332 }
7333 else if (readonlymode && curbuf->b_nwindows == 1)
7334 {
7335 /* When editing an already visited buffer, 'readonly' won't be set
7336 * but the previous value is kept. With ":view" and ":sview" we
7337 * want the file to be readonly, except when another window is
7338 * editing the same buffer. */
7339 curbuf->b_p_ro = TRUE;
7340 }
7341 readonlymode = n;
7342 }
7343 else
7344 {
7345 if (eap->do_ecmd_cmd != NULL)
7346 do_cmdline_cmd(eap->do_ecmd_cmd);
7347#ifdef FEAT_TITLE
7348 n = curwin->w_arg_idx_invalid;
7349#endif
7350 check_arg_idx(curwin);
7351#ifdef FEAT_TITLE
7352 if (n != curwin->w_arg_idx_invalid)
7353 maketitle();
7354#endif
7355 }
7356
7357#ifdef FEAT_WINDOWS
7358 /*
7359 * if ":split file" worked, set alternate file name in old window to new
7360 * file
7361 */
7362 if (old_curwin != NULL
7363 && *eap->arg != NUL
7364 && curwin != old_curwin
7365 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007366 && old_curwin->w_buffer != curbuf
7367 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 old_curwin->w_alt_fnum = curbuf->b_fnum;
7369#endif
7370
7371 ex_no_reprint = TRUE;
7372}
7373
7374#ifndef FEAT_GUI
7375/*
7376 * ":gui" and ":gvim" when there is no GUI.
7377 */
7378 static void
7379ex_nogui(eap)
7380 exarg_T *eap;
7381{
7382 eap->errmsg = e_nogvim;
7383}
7384#endif
7385
7386#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7387 static void
7388ex_tearoff(eap)
7389 exarg_T *eap;
7390{
7391 gui_make_tearoff(eap->arg);
7392}
7393#endif
7394
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007395#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 static void
7397ex_popup(eap)
7398 exarg_T *eap;
7399{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007400 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401}
7402#endif
7403
7404/*ARGSUSED*/
7405 static void
7406ex_swapname(eap)
7407 exarg_T *eap;
7408{
7409 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7410 MSG(_("No swap file"));
7411 else
7412 msg(curbuf->b_ml.ml_mfp->mf_fname);
7413}
7414
7415/*
7416 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7417 * offset.
7418 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7419 */
7420/*ARGSUSED*/
7421 static void
7422ex_syncbind(eap)
7423 exarg_T *eap;
7424{
7425#ifdef FEAT_SCROLLBIND
7426 win_T *wp;
7427 long topline;
7428 long y;
7429 linenr_T old_linenr = curwin->w_cursor.lnum;
7430
7431 setpcmark();
7432
7433 /*
7434 * determine max topline
7435 */
7436 if (curwin->w_p_scb)
7437 {
7438 topline = curwin->w_topline;
7439 for (wp = firstwin; wp; wp = wp->w_next)
7440 {
7441 if (wp->w_p_scb && wp->w_buffer)
7442 {
7443 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7444 if (topline > y)
7445 topline = y;
7446 }
7447 }
7448 if (topline < 1)
7449 topline = 1;
7450 }
7451 else
7452 {
7453 topline = 1;
7454 }
7455
7456
7457 /*
7458 * set all scrollbind windows to the same topline
7459 */
7460 wp = curwin;
7461 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7462 {
7463 if (curwin->w_p_scb)
7464 {
7465 y = topline - curwin->w_topline;
7466 if (y > 0)
7467 scrollup(y, TRUE);
7468 else
7469 scrolldown(-y, TRUE);
7470 curwin->w_scbind_pos = topline;
7471 redraw_later(VALID);
7472 cursor_correct();
7473#ifdef FEAT_WINDOWS
7474 curwin->w_redr_status = TRUE;
7475#endif
7476 }
7477 }
7478 curwin = wp;
7479 if (curwin->w_p_scb)
7480 {
7481 did_syncbind = TRUE;
7482 checkpcmark();
7483 if (old_linenr != curwin->w_cursor.lnum)
7484 {
7485 char_u ctrl_o[2];
7486
7487 ctrl_o[0] = Ctrl_O;
7488 ctrl_o[1] = 0;
7489 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7490 }
7491 }
7492#endif
7493}
7494
7495
7496 static void
7497ex_read(eap)
7498 exarg_T *eap;
7499{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007500 int i;
7501 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7502 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
7504 if (eap->usefilter) /* :r!cmd */
7505 do_bang(1, eap, FALSE, FALSE, TRUE);
7506 else
7507 {
7508 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7509 return;
7510
7511#ifdef FEAT_BROWSE
7512 if (cmdmod.browse)
7513 {
7514 char_u *browseFile;
7515
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007516 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 NULL, NULL, NULL, curbuf);
7518 if (browseFile != NULL)
7519 {
7520 i = readfile(browseFile, NULL,
7521 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7522 vim_free(browseFile);
7523 }
7524 else
7525 i = OK;
7526 }
7527 else
7528#endif
7529 if (*eap->arg == NUL)
7530 {
7531 if (check_fname() == FAIL) /* check for no file name */
7532 return;
7533 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7534 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7535 }
7536 else
7537 {
7538 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7539 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7540 i = readfile(eap->arg, NULL,
7541 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7542
7543 }
7544 if (i == FAIL)
7545 {
7546#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7547 if (!aborting())
7548#endif
7549 EMSG2(_(e_notopen), eap->arg);
7550 }
7551 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007552 {
7553 if (empty && exmode_active)
7554 {
7555 /* Delete the empty line that remains. Historically ex does
7556 * this but vi doesn't. */
7557 if (eap->line2 == 0)
7558 lnum = curbuf->b_ml.ml_line_count;
7559 else
7560 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007561 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007562 {
7563 ml_delete(lnum, FALSE);
7564 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007565 if (curwin->w_cursor.lnum > 1
7566 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007567 --curwin->w_cursor.lnum;
7568 }
7569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 }
7573}
7574
Bram Moolenaarea408852005-06-25 22:49:46 +00007575static char_u *prev_dir = NULL;
7576
7577#if defined(EXITFREE) || defined(PROTO)
7578 void
7579free_cd_dir()
7580{
7581 vim_free(prev_dir);
7582}
7583#endif
7584
7585
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586/*
7587 * ":cd", ":lcd", ":chdir" and ":lchdir".
7588 */
7589 static void
7590ex_cd(eap)
7591 exarg_T *eap;
7592{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 char_u *new_dir;
7594 char_u *tofree;
7595
7596 new_dir = eap->arg;
7597#if !defined(UNIX) && !defined(VMS)
7598 /* for non-UNIX ":cd" means: print current directory */
7599 if (*new_dir == NUL)
7600 ex_pwd(NULL);
7601 else
7602#endif
7603 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007604 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7605 && !eap->forceit)
7606 {
7607 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7608 return;
7609 }
7610
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 /* ":cd -": Change to previous directory */
7612 if (STRCMP(new_dir, "-") == 0)
7613 {
7614 if (prev_dir == NULL)
7615 {
7616 EMSG(_("E186: No previous directory"));
7617 return;
7618 }
7619 new_dir = prev_dir;
7620 }
7621
7622 /* Save current directory for next ":cd -" */
7623 tofree = prev_dir;
7624 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7625 prev_dir = vim_strsave(NameBuff);
7626 else
7627 prev_dir = NULL;
7628
7629#if defined(UNIX) || defined(VMS)
7630 /* for UNIX ":cd" means: go to home directory */
7631 if (*new_dir == NUL)
7632 {
7633 /* use NameBuff for home directory name */
7634# ifdef VMS
7635 char_u *p;
7636
7637 p = mch_getenv((char_u *)"SYS$LOGIN");
7638 if (p == NULL || *p == NUL) /* empty is the same as not set */
7639 NameBuff[0] = NUL;
7640 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007641 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642# else
7643 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7644# endif
7645 new_dir = NameBuff;
7646 }
7647#endif
7648 if (new_dir == NULL || vim_chdir(new_dir))
7649 EMSG(_(e_failed));
7650 else
7651 {
7652 vim_free(curwin->w_localdir);
7653 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7654 {
7655 /* If still in global directory, need to remember current
7656 * directory as global directory. */
7657 if (globaldir == NULL && prev_dir != NULL)
7658 globaldir = vim_strsave(prev_dir);
7659 /* Remember this local directory for the window. */
7660 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7661 curwin->w_localdir = vim_strsave(NameBuff);
7662 }
7663 else
7664 {
7665 /* We are now in the global directory, no need to remember its
7666 * name. */
7667 vim_free(globaldir);
7668 globaldir = NULL;
7669 curwin->w_localdir = NULL;
7670 }
7671
7672 shorten_fnames(TRUE);
7673
7674 /* Echo the new current directory if the command was typed. */
7675 if (KeyTyped)
7676 ex_pwd(eap);
7677 }
7678 vim_free(tofree);
7679 }
7680}
7681
7682/*
7683 * ":pwd".
7684 */
7685/*ARGSUSED*/
7686 static void
7687ex_pwd(eap)
7688 exarg_T *eap;
7689{
7690 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7691 {
7692#ifdef BACKSLASH_IN_FILENAME
7693 slash_adjust(NameBuff);
7694#endif
7695 msg(NameBuff);
7696 }
7697 else
7698 EMSG(_("E187: Unknown"));
7699}
7700
7701/*
7702 * ":=".
7703 */
7704 static void
7705ex_equal(eap)
7706 exarg_T *eap;
7707{
7708 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007709 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710}
7711
7712 static void
7713ex_sleep(eap)
7714 exarg_T *eap;
7715{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007716 int n;
7717 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718
7719 if (cursor_valid())
7720 {
7721 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7722 if (n >= 0)
7723 windgoto((int)n, curwin->w_wcol);
7724 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007725
7726 len = eap->line2;
7727 switch (*eap->arg)
7728 {
7729 case 'm': break;
7730 case NUL: len *= 1000L; break;
7731 default: EMSG2(_(e_invarg2), eap->arg); return;
7732 }
7733 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734}
7735
7736/*
7737 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7738 */
7739 void
7740do_sleep(msec)
7741 long msec;
7742{
7743 long done;
7744
7745 cursor_on();
7746 out_flush();
7747 for (done = 0; !got_int && done < msec; done += 1000L)
7748 {
7749 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7750 ui_breakcheck();
7751 }
7752}
7753
7754 static void
7755do_exmap(eap, isabbrev)
7756 exarg_T *eap;
7757 int isabbrev;
7758{
7759 int mode;
7760 char_u *cmdp;
7761
7762 cmdp = eap->cmd;
7763 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7764
7765 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7766 eap->arg, mode, isabbrev))
7767 {
7768 case 1: EMSG(_(e_invarg));
7769 break;
7770 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7771 break;
7772 }
7773}
7774
7775/*
7776 * ":winsize" command (obsolete).
7777 */
7778 static void
7779ex_winsize(eap)
7780 exarg_T *eap;
7781{
7782 int w, h;
7783 char_u *arg = eap->arg;
7784 char_u *p;
7785
7786 w = getdigits(&arg);
7787 arg = skipwhite(arg);
7788 p = arg;
7789 h = getdigits(&arg);
7790 if (*p != NUL && *arg == NUL)
7791 set_shellsize(w, h, TRUE);
7792 else
7793 EMSG(_("E465: :winsize requires two number arguments"));
7794}
7795
7796#ifdef FEAT_WINDOWS
7797 static void
7798ex_wincmd(eap)
7799 exarg_T *eap;
7800{
7801 int xchar = NUL;
7802 char_u *p;
7803
7804 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7805 {
7806 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7807 if (eap->arg[1] == NUL)
7808 {
7809 EMSG(_(e_invarg));
7810 return;
7811 }
7812 xchar = eap->arg[1];
7813 p = eap->arg + 2;
7814 }
7815 else
7816 p = eap->arg + 1;
7817
7818 eap->nextcmd = check_nextcmd(p);
7819 p = skipwhite(p);
7820 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7821 EMSG(_(e_invarg));
7822 else
7823 {
7824 /* Pass flags on for ":vertical wincmd ]". */
7825 postponed_split_flags = cmdmod.split;
7826 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7827 postponed_split_flags = 0;
7828 }
7829}
7830#endif
7831
Bram Moolenaar843ee412004-06-30 16:16:41 +00007832#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833/*
7834 * ":winpos".
7835 */
7836 static void
7837ex_winpos(eap)
7838 exarg_T *eap;
7839{
7840 int x, y;
7841 char_u *arg = eap->arg;
7842 char_u *p;
7843
7844 if (*arg == NUL)
7845 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007846# if defined(FEAT_GUI) || defined(MSWIN)
7847# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007849# else
7850 if (mch_get_winpos(&x, &y) != FAIL)
7851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {
7853 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7854 msg(IObuff);
7855 }
7856 else
7857# endif
7858 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7859 }
7860 else
7861 {
7862 x = getdigits(&arg);
7863 arg = skipwhite(arg);
7864 p = arg;
7865 y = getdigits(&arg);
7866 if (*p == NUL || *arg != NUL)
7867 {
7868 EMSG(_("E466: :winpos requires two number arguments"));
7869 return;
7870 }
7871# ifdef FEAT_GUI
7872 if (gui.in_use)
7873 gui_mch_set_winpos(x, y);
7874 else if (gui.starting)
7875 {
7876 /* Remember the coordinates for when the window is opened. */
7877 gui_win_x = x;
7878 gui_win_y = y;
7879 }
7880# ifdef HAVE_TGETENT
7881 else
7882# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007883# else
7884# ifdef MSWIN
7885 mch_set_winpos(x, y);
7886# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887# endif
7888# ifdef HAVE_TGETENT
7889 if (*T_CWP)
7890 term_set_winpos(x, y);
7891# endif
7892 }
7893}
7894#endif
7895
7896/*
7897 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7898 */
7899 static void
7900ex_operators(eap)
7901 exarg_T *eap;
7902{
7903 oparg_T oa;
7904
7905 clear_oparg(&oa);
7906 oa.regname = eap->regname;
7907 oa.start.lnum = eap->line1;
7908 oa.end.lnum = eap->line2;
7909 oa.line_count = eap->line2 - eap->line1 + 1;
7910 oa.motion_type = MLINE;
7911#ifdef FEAT_VIRTUALEDIT
7912 virtual_op = FALSE;
7913#endif
7914 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7915 {
7916 setpcmark();
7917 curwin->w_cursor.lnum = eap->line1;
7918 beginline(BL_SOL | BL_FIX);
7919 }
7920
7921 switch (eap->cmdidx)
7922 {
7923 case CMD_delete:
7924 oa.op_type = OP_DELETE;
7925 op_delete(&oa);
7926 break;
7927
7928 case CMD_yank:
7929 oa.op_type = OP_YANK;
7930 (void)op_yank(&oa, FALSE, TRUE);
7931 break;
7932
7933 default: /* CMD_rshift or CMD_lshift */
7934 if ((eap->cmdidx == CMD_rshift)
7935#ifdef FEAT_RIGHTLEFT
7936 ^ curwin->w_p_rl
7937#endif
7938 )
7939 oa.op_type = OP_RSHIFT;
7940 else
7941 oa.op_type = OP_LSHIFT;
7942 op_shift(&oa, FALSE, eap->amount);
7943 break;
7944 }
7945#ifdef FEAT_VIRTUALEDIT
7946 virtual_op = MAYBE;
7947#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007948 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949}
7950
7951/*
7952 * ":put".
7953 */
7954 static void
7955ex_put(eap)
7956 exarg_T *eap;
7957{
7958 /* ":0put" works like ":1put!". */
7959 if (eap->line2 == 0)
7960 {
7961 eap->line2 = 1;
7962 eap->forceit = TRUE;
7963 }
7964 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007965 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7966 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967}
7968
7969/*
7970 * Handle ":copy" and ":move".
7971 */
7972 static void
7973ex_copymove(eap)
7974 exarg_T *eap;
7975{
7976 long n;
7977
7978 n = get_address(&eap->arg, FALSE, FALSE);
7979 if (eap->arg == NULL) /* error detected */
7980 {
7981 eap->nextcmd = NULL;
7982 return;
7983 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007984 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985
7986 /*
7987 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7988 */
7989 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7990 {
7991 EMSG(_(e_invaddr));
7992 return;
7993 }
7994
7995 if (eap->cmdidx == CMD_move)
7996 {
7997 if (do_move(eap->line1, eap->line2, n) == FAIL)
7998 return;
7999 }
8000 else
8001 ex_copy(eap->line1, eap->line2, n);
8002 u_clearline();
8003 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008004 ex_may_print(eap);
8005}
8006
8007/*
8008 * Print the current line if flags were given to the Ex command.
8009 */
8010 static void
8011ex_may_print(eap)
8012 exarg_T *eap;
8013{
8014 if (eap->flags != 0)
8015 {
8016 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8017 (eap->flags & EXFLAG_LIST));
8018 ex_no_reprint = TRUE;
8019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020}
8021
8022/*
8023 * ":smagic" and ":snomagic".
8024 */
8025 static void
8026ex_submagic(eap)
8027 exarg_T *eap;
8028{
8029 int magic_save = p_magic;
8030
8031 p_magic = (eap->cmdidx == CMD_smagic);
8032 do_sub(eap);
8033 p_magic = magic_save;
8034}
8035
8036/*
8037 * ":join".
8038 */
8039 static void
8040ex_join(eap)
8041 exarg_T *eap;
8042{
8043 curwin->w_cursor.lnum = eap->line1;
8044 if (eap->line1 == eap->line2)
8045 {
8046 if (eap->addr_count >= 2) /* :2,2join does nothing */
8047 return;
8048 if (eap->line2 == curbuf->b_ml.ml_line_count)
8049 {
8050 beep_flush();
8051 return;
8052 }
8053 ++eap->line2;
8054 }
8055 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8056 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008057 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058}
8059
8060/*
8061 * ":[addr]@r" or ":[addr]*r": execute register
8062 */
8063 static void
8064ex_at(eap)
8065 exarg_T *eap;
8066{
8067 int c;
8068
8069 curwin->w_cursor.lnum = eap->line2;
8070
8071#ifdef USE_ON_FLY_SCROLL
8072 dont_scroll = TRUE; /* disallow scrolling here */
8073#endif
8074
8075 /* get the register name. No name means to use the previous one */
8076 c = *eap->arg;
8077 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8078 c = '@';
8079 /* put the register in mapbuf */
8080 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008081 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 else
8085 {
8086 int save_efr = exec_from_reg;
8087
8088 exec_from_reg = TRUE;
8089
8090 /*
8091 * Execute from the typeahead buffer.
8092 * Originally this didn't check for the typeahead buffer to be empty,
8093 * thus could read more Ex commands from stdin. It's not clear why,
8094 * it is certainly unexpected.
8095 */
8096 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8097 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8098
8099 exec_from_reg = save_efr;
8100 }
8101}
8102
8103/*
8104 * ":!".
8105 */
8106 static void
8107ex_bang(eap)
8108 exarg_T *eap;
8109{
8110 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8111}
8112
8113/*
8114 * ":undo".
8115 */
8116/*ARGSUSED*/
8117 static void
8118ex_undo(eap)
8119 exarg_T *eap;
8120{
8121 u_undo(1);
8122}
8123
8124/*
8125 * ":redo".
8126 */
8127/*ARGSUSED*/
8128 static void
8129ex_redo(eap)
8130 exarg_T *eap;
8131{
8132 u_redo(1);
8133}
8134
8135/*
8136 * ":redir": start/stop redirection.
8137 */
8138 static void
8139ex_redir(eap)
8140 exarg_T *eap;
8141{
8142 char *mode;
8143 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008144 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145
8146 if (STRICMP(eap->arg, "END") == 0)
8147 close_redir();
8148 else
8149 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008150 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008152 ++arg;
8153 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008155 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 mode = "a";
8157 }
8158 else
8159 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008160 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161
8162 close_redir();
8163
8164 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008165 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 if (fname == NULL)
8167 return;
8168#ifdef FEAT_BROWSE
8169 if (cmdmod.browse)
8170 {
8171 char_u *browseFile;
8172
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008173 browseFile = do_browse(BROWSE_SAVE,
8174 (char_u *)_("Save Redirection"),
8175 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 if (browseFile == NULL)
8177 return; /* operation cancelled */
8178 vim_free(fname);
8179 fname = browseFile;
8180 eap->forceit = TRUE; /* since dialog already asked */
8181 }
8182#endif
8183
8184 redir_fd = open_exfile(fname, eap->forceit, mode);
8185 vim_free(fname);
8186 }
8187#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008188 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {
8190 /* redirect to a register a-z (resp. A-Z for appending) */
8191 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008192 ++arg;
8193 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008195 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008197 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008199 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008200 if (*arg == '>' && arg[1] == '>')
8201 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008202 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
8203 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008205 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008207 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008209 if (*arg == '>')
8210 arg++;
8211
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 /* make register empty */
8213 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
8214 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008215 }
8216 if (*arg != NUL)
8217 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008218 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008219 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008220 }
8221 }
8222 else if (*arg == '=' && arg[1] == '>')
8223 {
8224 int append;
8225
8226 /* redirect to a variable */
8227 close_redir();
8228 arg += 2;
8229
8230 if (*arg == '>')
8231 {
8232 ++arg;
8233 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 }
8235 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008236 append = FALSE;
8237
8238 if (var_redir_start(skipwhite(arg), append) == OK)
8239 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 }
8241#endif
8242
8243 /* TODO: redirect to a buffer */
8244
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008246 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 }
8248}
8249
8250/*
8251 * ":redraw": force redraw
8252 */
8253 static void
8254ex_redraw(eap)
8255 exarg_T *eap;
8256{
8257 int r = RedrawingDisabled;
8258 int p = p_lz;
8259
8260 RedrawingDisabled = 0;
8261 p_lz = FALSE;
8262 update_topline();
8263 update_screen(eap->forceit ? CLEAR :
8264#ifdef FEAT_VISUAL
8265 VIsual_active ? INVERTED :
8266#endif
8267 0);
8268#ifdef FEAT_TITLE
8269 if (need_maketitle)
8270 maketitle();
8271#endif
8272 RedrawingDisabled = r;
8273 p_lz = p;
8274
8275 /* Reset msg_didout, so that a message that's there is overwritten. */
8276 msg_didout = FALSE;
8277 msg_col = 0;
8278
8279 out_flush();
8280}
8281
8282/*
8283 * ":redrawstatus": force redraw of status line(s)
8284 */
8285/*ARGSUSED*/
8286 static void
8287ex_redrawstatus(eap)
8288 exarg_T *eap;
8289{
8290#if defined(FEAT_WINDOWS)
8291 int r = RedrawingDisabled;
8292 int p = p_lz;
8293
8294 RedrawingDisabled = 0;
8295 p_lz = FALSE;
8296 if (eap->forceit)
8297 status_redraw_all();
8298 else
8299 status_redraw_curbuf();
8300 update_screen(
8301# ifdef FEAT_VISUAL
8302 VIsual_active ? INVERTED :
8303# endif
8304 0);
8305 RedrawingDisabled = r;
8306 p_lz = p;
8307 out_flush();
8308#endif
8309}
8310
8311 static void
8312close_redir()
8313{
8314 if (redir_fd != NULL)
8315 {
8316 fclose(redir_fd);
8317 redir_fd = NULL;
8318 }
8319#ifdef FEAT_EVAL
8320 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008321 if (redir_vname)
8322 {
8323 var_redir_stop();
8324 redir_vname = 0;
8325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326#endif
8327}
8328
8329#if defined(FEAT_SESSION) && defined(USE_CRNL)
8330# define MKSESSION_NL
8331static int mksession_nl = FALSE; /* use NL only in put_eol() */
8332#endif
8333
8334/*
8335 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8336 */
8337 static void
8338ex_mkrc(eap)
8339 exarg_T *eap;
8340{
8341 FILE *fd;
8342 int failed = FALSE;
8343 char_u *fname;
8344#ifdef FEAT_BROWSE
8345 char_u *browseFile = NULL;
8346#endif
8347#ifdef FEAT_SESSION
8348 int view_session = FALSE;
8349 int using_vdir = FALSE; /* using 'viewdir'? */
8350 char_u *viewFile = NULL;
8351 unsigned *flagp;
8352#endif
8353
8354 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8355 {
8356#ifdef FEAT_SESSION
8357 view_session = TRUE;
8358#else
8359 ex_ni(eap);
8360 return;
8361#endif
8362 }
8363
8364#ifdef FEAT_SESSION
8365 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8366 if (eap->cmdidx == CMD_mkview
8367 && (*eap->arg == NUL
8368 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8369 {
8370 eap->forceit = TRUE;
8371 fname = get_view_file(*eap->arg);
8372 if (fname == NULL)
8373 return;
8374 viewFile = fname;
8375 using_vdir = TRUE;
8376 }
8377 else
8378#endif
8379 if (*eap->arg != NUL)
8380 fname = eap->arg;
8381 else if (eap->cmdidx == CMD_mkvimrc)
8382 fname = (char_u *)VIMRC_FILE;
8383#ifdef FEAT_SESSION
8384 else if (eap->cmdidx == CMD_mksession)
8385 fname = (char_u *)SESSION_FILE;
8386#endif
8387 else
8388 fname = (char_u *)EXRC_FILE;
8389
8390#ifdef FEAT_BROWSE
8391 if (cmdmod.browse)
8392 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008393 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394# ifdef FEAT_SESSION
8395 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8396 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8397# endif
8398 (char_u *)_("Save Setup"),
8399 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8400 if (browseFile == NULL)
8401 goto theend;
8402 fname = browseFile;
8403 eap->forceit = TRUE; /* since dialog already asked */
8404 }
8405#endif
8406
8407#if defined(FEAT_SESSION) && defined(vim_mkdir)
8408 /* When using 'viewdir' may have to create the directory. */
8409 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008410 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411#endif
8412
8413 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8414 if (fd != NULL)
8415 {
8416#ifdef FEAT_SESSION
8417 if (eap->cmdidx == CMD_mkview)
8418 flagp = &vop_flags;
8419 else
8420 flagp = &ssop_flags;
8421#endif
8422
8423#ifdef MKSESSION_NL
8424 /* "unix" in 'sessionoptions': use NL line separator */
8425 if (view_session && (*flagp & SSOP_UNIX))
8426 mksession_nl = TRUE;
8427#endif
8428
8429 /* Write the version command for :mkvimrc */
8430 if (eap->cmdidx == CMD_mkvimrc)
8431 (void)put_line(fd, "version 6.0");
8432
8433#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008434 if (eap->cmdidx == CMD_mksession)
8435 {
8436 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8437 failed = TRUE;
8438 }
8439
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 if (eap->cmdidx != CMD_mkview)
8441#endif
8442 {
8443 /* Write setting 'compatible' first, because it has side effects.
8444 * For that same reason only do it when needed. */
8445 if (p_cp)
8446 (void)put_line(fd, "if !&cp | set cp | endif");
8447 else
8448 (void)put_line(fd, "if &cp | set nocp | endif");
8449 }
8450
8451#ifdef FEAT_SESSION
8452 if (!view_session
8453 || (eap->cmdidx == CMD_mksession
8454 && (*flagp & SSOP_OPTIONS)))
8455#endif
8456 failed |= (makemap(fd, NULL) == FAIL
8457 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8458
8459#ifdef FEAT_SESSION
8460 if (!failed && view_session)
8461 {
8462 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8463 failed = TRUE;
8464 if (eap->cmdidx == CMD_mksession)
8465 {
8466 char_u dirnow[MAXPATHL]; /* current directory */
8467
8468 /*
8469 * Change to session file's dir.
8470 */
8471 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8472 || mch_chdir((char *)dirnow) != 0)
8473 *dirnow = NUL;
8474 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8475 {
8476 if (vim_chdirfile(fname) == OK)
8477 shorten_fnames(TRUE);
8478 }
8479 else if (*dirnow != NUL
8480 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8481 {
8482 (void)mch_chdir((char *)globaldir);
8483 shorten_fnames(TRUE);
8484 }
8485
8486 failed |= (makeopens(fd, dirnow) == FAIL);
8487
8488 /* restore original dir */
8489 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8490 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8491 {
8492 if (mch_chdir((char *)dirnow) != 0)
8493 EMSG(_(e_prev_dir));
8494 shorten_fnames(TRUE);
8495 }
8496 }
8497 else
8498 {
8499 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8500 }
8501 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8502 == FAIL)
8503 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008504 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8505 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008506 if (eap->cmdidx == CMD_mksession)
8507 {
8508 if (put_line(fd, "unlet SessionLoad") == FAIL)
8509 failed = TRUE;
8510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 }
8512#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008513 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8514 failed = TRUE;
8515
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 failed |= fclose(fd);
8517
8518 if (failed)
8519 EMSG(_(e_write));
8520#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8521 else if (eap->cmdidx == CMD_mksession)
8522 {
8523 /* successful session write - set this_session var */
8524 char_u tbuf[MAXPATHL];
8525
8526 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8527 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8528 }
8529#endif
8530#ifdef MKSESSION_NL
8531 mksession_nl = FALSE;
8532#endif
8533 }
8534
8535#ifdef FEAT_BROWSE
8536theend:
8537 vim_free(browseFile);
8538#endif
8539#ifdef FEAT_SESSION
8540 vim_free(viewFile);
8541#endif
8542}
8543
Bram Moolenaardf177f62005-02-22 08:39:57 +00008544#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8545 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008546/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008547 int
8548vim_mkdir_emsg(name, prot)
8549 char_u *name;
8550 int prot;
8551{
8552 if (vim_mkdir(name, prot) != 0)
8553 {
8554 EMSG2(_("E739: Cannot create directory: %s"), name);
8555 return FAIL;
8556 }
8557 return OK;
8558}
8559#endif
8560
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561/*
8562 * Open a file for writing for an Ex command, with some checks.
8563 * Return file descriptor, or NULL on failure.
8564 */
8565 FILE *
8566open_exfile(fname, forceit, mode)
8567 char_u *fname;
8568 int forceit;
8569 char *mode; /* "w" for create new file or "a" for append */
8570{
8571 FILE *fd;
8572
8573#ifdef UNIX
8574 /* with Unix it is possible to open a directory */
8575 if (mch_isdir(fname))
8576 {
8577 EMSG2(_(e_isadir2), fname);
8578 return NULL;
8579 }
8580#endif
8581 if (!forceit && *mode != 'a' && vim_fexists(fname))
8582 {
8583 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8584 return NULL;
8585 }
8586
8587 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8588 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8589
8590 return fd;
8591}
8592
8593/*
8594 * ":mark" and ":k".
8595 */
8596 static void
8597ex_mark(eap)
8598 exarg_T *eap;
8599{
8600 pos_T pos;
8601
8602 if (*eap->arg == NUL) /* No argument? */
8603 EMSG(_(e_argreq));
8604 else if (eap->arg[1] != NUL) /* more than one character? */
8605 EMSG(_(e_trailing));
8606 else
8607 {
8608 pos = curwin->w_cursor; /* save curwin->w_cursor */
8609 curwin->w_cursor.lnum = eap->line2;
8610 beginline(BL_WHITE | BL_FIX);
8611 if (setmark(*eap->arg) == FAIL) /* set mark */
8612 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8613 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8614 }
8615}
8616
8617/*
8618 * Update w_topline, w_leftcol and the cursor position.
8619 */
8620 void
8621update_topline_cursor()
8622{
8623 check_cursor(); /* put cursor on valid line */
8624 update_topline();
8625 if (!curwin->w_p_wrap)
8626 validate_cursor();
8627 update_curswant();
8628}
8629
8630#ifdef FEAT_EX_EXTRA
8631/*
8632 * ":normal[!] {commands}": Execute normal mode commands.
8633 */
8634 static void
8635ex_normal(eap)
8636 exarg_T *eap;
8637{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 int save_msg_scroll = msg_scroll;
8639 int save_restart_edit = restart_edit;
8640 int save_msg_didout = msg_didout;
8641 int save_State = State;
8642 tasave_T tabuf;
8643 int save_insertmode = p_im;
8644 int save_finish_op = finish_op;
8645#ifdef FEAT_MBYTE
8646 char_u *arg = NULL;
8647 int l;
8648 char_u *p;
8649#endif
8650
8651 if (ex_normal_busy >= p_mmd)
8652 {
8653 EMSG(_("E192: Recursive use of :normal too deep"));
8654 return;
8655 }
8656 ++ex_normal_busy;
8657
8658 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8659 restart_edit = 0; /* don't go to Insert mode */
8660 p_im = FALSE; /* don't use 'insertmode' */
8661
8662#ifdef FEAT_MBYTE
8663 /*
8664 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8665 * this for the K_SPECIAL leading byte, otherwise special keys will not
8666 * work.
8667 */
8668 if (has_mbyte)
8669 {
8670 int len = 0;
8671
8672 /* Count the number of characters to be escaped. */
8673 for (p = eap->arg; *p != NUL; ++p)
8674 {
8675# ifdef FEAT_GUI
8676 if (*p == CSI) /* leadbyte CSI */
8677 len += 2;
8678# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008679 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8681# ifdef FEAT_GUI
8682 || *p == CSI
8683# endif
8684 )
8685 len += 2;
8686 }
8687 if (len > 0)
8688 {
8689 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8690 if (arg != NULL)
8691 {
8692 len = 0;
8693 for (p = eap->arg; *p != NUL; ++p)
8694 {
8695 arg[len++] = *p;
8696# ifdef FEAT_GUI
8697 if (*p == CSI)
8698 {
8699 arg[len++] = KS_EXTRA;
8700 arg[len++] = (int)KE_CSI;
8701 }
8702# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008703 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 {
8705 arg[len++] = *++p;
8706 if (*p == K_SPECIAL)
8707 {
8708 arg[len++] = KS_SPECIAL;
8709 arg[len++] = KE_FILLER;
8710 }
8711# ifdef FEAT_GUI
8712 else if (*p == CSI)
8713 {
8714 arg[len++] = KS_EXTRA;
8715 arg[len++] = (int)KE_CSI;
8716 }
8717# endif
8718 }
8719 arg[len] = NUL;
8720 }
8721 }
8722 }
8723 }
8724#endif
8725
8726 /*
8727 * Save the current typeahead. This is required to allow using ":normal"
8728 * from an event handler and makes sure we don't hang when the argument
8729 * ends with half a command.
8730 */
8731 save_typeahead(&tabuf);
8732 if (tabuf.typebuf_valid)
8733 {
8734 /*
8735 * Repeat the :normal command for each line in the range. When no
8736 * range given, execute it just once, without positioning the cursor
8737 * first.
8738 */
8739 do
8740 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 if (eap->addr_count != 0)
8742 {
8743 curwin->w_cursor.lnum = eap->line1++;
8744 curwin->w_cursor.col = 0;
8745 }
8746
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008747 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748#ifdef FEAT_MBYTE
8749 arg != NULL ? arg :
8750#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008751 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 }
8753 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8754 }
8755
8756 /* Might not return to the main loop when in an event handler. */
8757 update_topline_cursor();
8758
8759 /* Restore the previous typeahead. */
8760 restore_typeahead(&tabuf);
8761
8762 --ex_normal_busy;
8763 msg_scroll = save_msg_scroll;
8764 restart_edit = save_restart_edit;
8765 p_im = save_insertmode;
8766 finish_op = save_finish_op;
8767 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8768
8769 /* Restore the state (needed when called from a function executed for
8770 * 'indentexpr'). */
8771 State = save_State;
8772#ifdef FEAT_MBYTE
8773 vim_free(arg);
8774#endif
8775}
8776
8777/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008778 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 */
8780 static void
8781ex_startinsert(eap)
8782 exarg_T *eap;
8783{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008784 if (eap->forceit)
8785 {
8786 coladvance((colnr_T)MAXCOL);
8787 curwin->w_curswant = MAXCOL;
8788 curwin->w_set_curswant = FALSE;
8789 }
8790
Bram Moolenaara40c5002005-01-09 21:16:21 +00008791 /* Ignore the command when already in Insert mode. Inserting an
8792 * expression register that invokes a function can do this. */
8793 if (State & INSERT)
8794 return;
8795
Bram Moolenaar64969662005-12-14 21:59:55 +00008796 if (eap->cmdidx == CMD_startinsert)
8797 restart_edit = 'a';
8798 else if (eap->cmdidx == CMD_startreplace)
8799 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008801 restart_edit = 'V';
8802
8803 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008805 if (eap->cmdidx == CMD_startinsert)
8806 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 curwin->w_curswant = 0; /* avoid MAXCOL */
8808 }
8809}
8810
8811/*
8812 * ":stopinsert"
8813 */
8814/*ARGSUSED*/
8815 static void
8816ex_stopinsert(eap)
8817 exarg_T *eap;
8818{
8819 restart_edit = 0;
8820 stop_insert_mode = TRUE;
8821}
8822#endif
8823
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008824#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8825/*
8826 * Execute normal mode command "cmd".
8827 * "remap" can be REMAP_NONE or REMAP_YES.
8828 */
8829 void
8830exec_normal_cmd(cmd, remap, silent)
8831 char_u *cmd;
8832 int remap;
8833 int silent;
8834{
8835 oparg_T oa;
8836
8837 /*
8838 * Stuff the argument into the typeahead buffer.
8839 * Execute normal_cmd() until there is no typeahead left.
8840 */
8841 clear_oparg(&oa);
8842 finish_op = FALSE;
8843 ins_typebuf(cmd, remap, 0, TRUE, silent);
8844 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8845 && !got_int)
8846 {
8847 update_topline_cursor();
8848 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8849 }
8850}
8851#endif
8852
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853#ifdef FEAT_FIND_ID
8854 static void
8855ex_checkpath(eap)
8856 exarg_T *eap;
8857{
8858 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8859 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8860 (linenr_T)1, (linenr_T)MAXLNUM);
8861}
8862
8863#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8864/*
8865 * ":psearch"
8866 */
8867 static void
8868ex_psearch(eap)
8869 exarg_T *eap;
8870{
8871 g_do_tagpreview = p_pvh;
8872 ex_findpat(eap);
8873 g_do_tagpreview = 0;
8874}
8875#endif
8876
8877 static void
8878ex_findpat(eap)
8879 exarg_T *eap;
8880{
8881 int whole = TRUE;
8882 long n;
8883 char_u *p;
8884 int action;
8885
8886 switch (cmdnames[eap->cmdidx].cmd_name[2])
8887 {
8888 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8889 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8890 action = ACTION_GOTO;
8891 else
8892 action = ACTION_SHOW;
8893 break;
8894 case 'i': /* ":ilist" and ":dlist" */
8895 action = ACTION_SHOW_ALL;
8896 break;
8897 case 'u': /* ":ijump" and ":djump" */
8898 action = ACTION_GOTO;
8899 break;
8900 default: /* ":isplit" and ":dsplit" */
8901 action = ACTION_SPLIT;
8902 break;
8903 }
8904
8905 n = 1;
8906 if (vim_isdigit(*eap->arg)) /* get count */
8907 {
8908 n = getdigits(&eap->arg);
8909 eap->arg = skipwhite(eap->arg);
8910 }
8911 if (*eap->arg == '/') /* Match regexp, not just whole words */
8912 {
8913 whole = FALSE;
8914 ++eap->arg;
8915 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8916 if (*p)
8917 {
8918 *p++ = NUL;
8919 p = skipwhite(p);
8920
8921 /* Check for trailing illegal characters */
8922 if (!ends_excmd(*p))
8923 eap->errmsg = e_trailing;
8924 else
8925 eap->nextcmd = check_nextcmd(p);
8926 }
8927 }
8928 if (!eap->skip)
8929 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8930 whole, !eap->forceit,
8931 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8932 n, action, eap->line1, eap->line2);
8933}
8934#endif
8935
8936#ifdef FEAT_WINDOWS
8937
8938# ifdef FEAT_QUICKFIX
8939/*
8940 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8941 */
8942 static void
8943ex_ptag(eap)
8944 exarg_T *eap;
8945{
8946 g_do_tagpreview = p_pvh;
8947 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8948}
8949
8950/*
8951 * ":pedit"
8952 */
8953 static void
8954ex_pedit(eap)
8955 exarg_T *eap;
8956{
8957 win_T *curwin_save = curwin;
8958
8959 g_do_tagpreview = p_pvh;
8960 prepare_tagpreview();
8961 keep_help_flag = curwin_save->w_buffer->b_help;
8962 do_exedit(eap, NULL);
8963 keep_help_flag = FALSE;
8964 if (curwin != curwin_save && win_valid(curwin_save))
8965 {
8966 /* Return cursor to where we were */
8967 validate_cursor();
8968 redraw_later(VALID);
8969 win_enter(curwin_save, TRUE);
8970 }
8971 g_do_tagpreview = 0;
8972}
8973# endif
8974
8975/*
8976 * ":stag", ":stselect" and ":stjump".
8977 */
8978 static void
8979ex_stag(eap)
8980 exarg_T *eap;
8981{
8982 postponed_split = -1;
8983 postponed_split_flags = cmdmod.split;
8984 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8985 postponed_split_flags = 0;
8986}
8987#endif
8988
8989/*
8990 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8991 */
8992 static void
8993ex_tag(eap)
8994 exarg_T *eap;
8995{
8996 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8997}
8998
8999 static void
9000ex_tag_cmd(eap, name)
9001 exarg_T *eap;
9002 char_u *name;
9003{
9004 int cmd;
9005
9006 switch (name[1])
9007 {
9008 case 'j': cmd = DT_JUMP; /* ":tjump" */
9009 break;
9010 case 's': cmd = DT_SELECT; /* ":tselect" */
9011 break;
9012 case 'p': cmd = DT_PREV; /* ":tprevious" */
9013 break;
9014 case 'N': cmd = DT_PREV; /* ":tNext" */
9015 break;
9016 case 'n': cmd = DT_NEXT; /* ":tnext" */
9017 break;
9018 case 'o': cmd = DT_POP; /* ":pop" */
9019 break;
9020 case 'f': /* ":tfirst" */
9021 case 'r': cmd = DT_FIRST; /* ":trewind" */
9022 break;
9023 case 'l': cmd = DT_LAST; /* ":tlast" */
9024 break;
9025 default: /* ":tag" */
9026#ifdef FEAT_CSCOPE
9027 if (p_cst)
9028 {
9029 do_cstag(eap);
9030 return;
9031 }
9032#endif
9033 cmd = DT_TAG;
9034 break;
9035 }
9036
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009037 if (name[0] == 'l')
9038 {
9039#ifndef FEAT_QUICKFIX
9040 ex_ni(eap);
9041 return;
9042#else
9043 cmd = DT_LTAG;
9044#endif
9045 }
9046
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9048 eap->forceit, TRUE);
9049}
9050
9051/*
9052 * Evaluate cmdline variables.
9053 *
9054 * change '%' to curbuf->b_ffname
9055 * '#' to curwin->w_altfile
9056 * '<cword>' to word under the cursor
9057 * '<cWORD>' to WORD under the cursor
9058 * '<cfile>' to path name under the cursor
9059 * '<sfile>' to sourced file name
9060 * '<afile>' to file name for autocommand
9061 * '<abuf>' to buffer number for autocommand
9062 * '<amatch>' to matching name for autocommand
9063 *
9064 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9065 * "" for error without a message) and NULL is returned.
9066 * Returns an allocated string if a valid match was found.
9067 * Returns NULL if no match was found. "usedlen" then still contains the
9068 * number of characters to skip.
9069 */
9070 char_u *
9071eval_vars(src, usedlen, lnump, errormsg, srcstart)
9072 char_u *src; /* pointer into commandline */
9073 int *usedlen; /* characters after src that are used */
9074 linenr_T *lnump; /* line number for :e command, or NULL */
9075 char_u **errormsg; /* pointer to error message */
9076 char_u *srcstart; /* beginning of valid memory for src */
9077{
9078 int i;
9079 char_u *s;
9080 char_u *result;
9081 char_u *resultbuf = NULL;
9082 int resultlen;
9083 buf_T *buf;
9084 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9085 int spec_idx;
9086#ifdef FEAT_MODIFY_FNAME
9087 int skip_mod = FALSE;
9088#endif
9089 static char *(spec_str[]) =
9090 {
9091 "%",
9092#define SPEC_PERC 0
9093 "#",
9094#define SPEC_HASH 1
9095 "<cword>", /* cursor word */
9096#define SPEC_CWORD 2
9097 "<cWORD>", /* cursor WORD */
9098#define SPEC_CCWORD 3
9099 "<cfile>", /* cursor path name */
9100#define SPEC_CFILE 4
9101 "<sfile>", /* ":so" file name */
9102#define SPEC_SFILE 5
9103#ifdef FEAT_AUTOCMD
9104 "<afile>", /* autocommand file name */
9105# define SPEC_AFILE 6
9106 "<abuf>", /* autocommand buffer number */
9107# define SPEC_ABUF 7
9108 "<amatch>", /* autocommand match name */
9109# define SPEC_AMATCH 8
9110#endif
9111#ifdef FEAT_CLIENTSERVER
9112 "<client>"
9113# define SPEC_CLIENT 9
9114#endif
9115 };
9116#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9117
9118#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9119 char_u strbuf[30];
9120#endif
9121
9122 *errormsg = NULL;
9123
9124 /*
9125 * Check if there is something to do.
9126 */
9127 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
9128 {
9129 *usedlen = (int)STRLEN(spec_str[spec_idx]);
9130 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
9131 break;
9132 }
9133 if (spec_idx == SPEC_COUNT) /* no match */
9134 {
9135 *usedlen = 1;
9136 return NULL;
9137 }
9138
9139 /*
9140 * Skip when preceded with a backslash "\%" and "\#".
9141 * Note: In "\\%" the % is also not recognized!
9142 */
9143 if (src > srcstart && src[-1] == '\\')
9144 {
9145 *usedlen = 0;
9146 STRCPY(src - 1, src); /* remove backslash */
9147 return NULL;
9148 }
9149
9150 /*
9151 * word or WORD under cursor
9152 */
9153 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9154 {
9155 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9156 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9157 if (resultlen == 0)
9158 {
9159 *errormsg = (char_u *)"";
9160 return NULL;
9161 }
9162 }
9163
9164 /*
9165 * '#': Alternate file name
9166 * '%': Current file name
9167 * File name under the cursor
9168 * File name for autocommand
9169 * and following modifiers
9170 */
9171 else
9172 {
9173 switch (spec_idx)
9174 {
9175 case SPEC_PERC: /* '%': current file */
9176 if (curbuf->b_fname == NULL)
9177 {
9178 result = (char_u *)"";
9179 valid = 0; /* Must have ":p:h" to be valid */
9180 }
9181 else
9182#ifdef RISCOS
9183 /* Always use the full path for RISC OS if possible. */
9184 result = curbuf->b_ffname;
9185 if (result == NULL)
9186 result = curbuf->b_fname;
9187#else
9188 result = curbuf->b_fname;
9189#endif
9190 break;
9191
9192 case SPEC_HASH: /* '#' or "#99": alternate file */
9193 if (src[1] == '#') /* "##": the argument list */
9194 {
9195 result = arg_all();
9196 resultbuf = result;
9197 *usedlen = 2;
9198#ifdef FEAT_MODIFY_FNAME
9199 skip_mod = TRUE;
9200#endif
9201 break;
9202 }
9203 s = src + 1;
9204 i = (int)getdigits(&s);
9205 *usedlen = (int)(s - src); /* length of what we expand */
9206
9207 buf = buflist_findnr(i);
9208 if (buf == NULL)
9209 {
9210 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9211 return NULL;
9212 }
9213 if (lnump != NULL)
9214 *lnump = ECMD_LAST;
9215 if (buf->b_fname == NULL)
9216 {
9217 result = (char_u *)"";
9218 valid = 0; /* Must have ":p:h" to be valid */
9219 }
9220 else
9221 result = buf->b_fname;
9222 break;
9223
9224#ifdef FEAT_SEARCHPATH
9225 case SPEC_CFILE: /* file name under cursor */
9226 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L);
9227 if (result == NULL)
9228 {
9229 *errormsg = (char_u *)"";
9230 return NULL;
9231 }
9232 resultbuf = result; /* remember allocated string */
9233 break;
9234#endif
9235
9236#ifdef FEAT_AUTOCMD
9237 case SPEC_AFILE: /* file name for autocommand */
9238 result = autocmd_fname;
9239 if (result == NULL)
9240 {
9241 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9242 return NULL;
9243 }
9244 break;
9245
9246 case SPEC_ABUF: /* buffer number for autocommand */
9247 if (autocmd_bufnr <= 0)
9248 {
9249 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9250 return NULL;
9251 }
9252 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9253 result = strbuf;
9254 break;
9255
9256 case SPEC_AMATCH: /* match name for autocommand */
9257 result = autocmd_match;
9258 if (result == NULL)
9259 {
9260 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9261 return NULL;
9262 }
9263 break;
9264
9265#endif
9266 case SPEC_SFILE: /* file name for ":so" command */
9267 result = sourcing_name;
9268 if (result == NULL)
9269 {
9270 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9271 return NULL;
9272 }
9273 break;
9274#if defined(FEAT_CLIENTSERVER)
9275 case SPEC_CLIENT: /* Source of last submitted input */
9276 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
9277 result = strbuf;
9278 break;
9279#endif
9280 }
9281
9282 resultlen = (int)STRLEN(result); /* length of new string */
9283 if (src[*usedlen] == '<') /* remove the file name extension */
9284 {
9285 ++*usedlen;
9286#ifdef RISCOS
9287 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9288#else
9289 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9290#endif
9291 resultlen = (int)(s - result);
9292 }
9293#ifdef FEAT_MODIFY_FNAME
9294 else if (!skip_mod)
9295 {
9296 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9297 &resultlen);
9298 if (result == NULL)
9299 {
9300 *errormsg = (char_u *)"";
9301 return NULL;
9302 }
9303 }
9304#endif
9305 }
9306
9307 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9308 {
9309 if (valid != VALID_HEAD + VALID_PATH)
9310 /* xgettext:no-c-format */
9311 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9312 else
9313 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9314 result = NULL;
9315 }
9316 else
9317 result = vim_strnsave(result, resultlen);
9318 vim_free(resultbuf);
9319 return result;
9320}
9321
9322/*
9323 * Concatenate all files in the argument list, separated by spaces, and return
9324 * it in one allocated string.
9325 * Spaces and backslashes in the file names are escaped with a backslash.
9326 * Returns NULL when out of memory.
9327 */
9328 static char_u *
9329arg_all()
9330{
9331 int len;
9332 int idx;
9333 char_u *retval = NULL;
9334 char_u *p;
9335
9336 /*
9337 * Do this loop two times:
9338 * first time: compute the total length
9339 * second time: concatenate the names
9340 */
9341 for (;;)
9342 {
9343 len = 0;
9344 for (idx = 0; idx < ARGCOUNT; ++idx)
9345 {
9346 p = alist_name(&ARGLIST[idx]);
9347 if (p != NULL)
9348 {
9349 if (len > 0)
9350 {
9351 /* insert a space in between names */
9352 if (retval != NULL)
9353 retval[len] = ' ';
9354 ++len;
9355 }
9356 for ( ; *p != NUL; ++p)
9357 {
9358 if (*p == ' ' || *p == '\\')
9359 {
9360 /* insert a backslash */
9361 if (retval != NULL)
9362 retval[len] = '\\';
9363 ++len;
9364 }
9365 if (retval != NULL)
9366 retval[len] = *p;
9367 ++len;
9368 }
9369 }
9370 }
9371
9372 /* second time: break here */
9373 if (retval != NULL)
9374 {
9375 retval[len] = NUL;
9376 break;
9377 }
9378
9379 /* allocate memory */
9380 retval = alloc(len + 1);
9381 if (retval == NULL)
9382 break;
9383 }
9384
9385 return retval;
9386}
9387
9388#if defined(FEAT_AUTOCMD) || defined(PROTO)
9389/*
9390 * Expand the <sfile> string in "arg".
9391 *
9392 * Returns an allocated string, or NULL for any error.
9393 */
9394 char_u *
9395expand_sfile(arg)
9396 char_u *arg;
9397{
9398 char_u *errormsg;
9399 int len;
9400 char_u *result;
9401 char_u *newres;
9402 char_u *repl;
9403 int srclen;
9404 char_u *p;
9405
9406 result = vim_strsave(arg);
9407 if (result == NULL)
9408 return NULL;
9409
9410 for (p = result; *p; )
9411 {
9412 if (STRNCMP(p, "<sfile>", 7) != 0)
9413 ++p;
9414 else
9415 {
9416 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9417 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9418 if (errormsg != NULL)
9419 {
9420 if (*errormsg)
9421 emsg(errormsg);
9422 vim_free(result);
9423 return NULL;
9424 }
9425 if (repl == NULL) /* no match (cannot happen) */
9426 {
9427 p += srclen;
9428 continue;
9429 }
9430 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9431 newres = alloc(len);
9432 if (newres == NULL)
9433 {
9434 vim_free(repl);
9435 vim_free(result);
9436 return NULL;
9437 }
9438 mch_memmove(newres, result, (size_t)(p - result));
9439 STRCPY(newres + (p - result), repl);
9440 len = (int)STRLEN(newres);
9441 STRCAT(newres, p + srclen);
9442 vim_free(repl);
9443 vim_free(result);
9444 result = newres;
9445 p = newres + len; /* continue after the match */
9446 }
9447 }
9448
9449 return result;
9450}
9451#endif
9452
9453#ifdef FEAT_SESSION
9454static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9455static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9456static frame_T *ses_skipframe __ARGS((frame_T *fr));
9457static int ses_do_frame __ARGS((frame_T *fr));
9458static int ses_do_win __ARGS((win_T *wp));
9459static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9460static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9461static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9462
9463/*
9464 * Write openfile commands for the current buffers to an .exrc file.
9465 * Return FAIL on error, OK otherwise.
9466 */
9467 static int
9468makeopens(fd, dirnow)
9469 FILE *fd;
9470 char_u *dirnow; /* Current directory name */
9471{
9472 buf_T *buf;
9473 int only_save_windows = TRUE;
9474 int nr;
9475 int cnr = 1;
9476 int restore_size = TRUE;
9477 win_T *wp;
9478 char_u *sname;
9479 win_T *edited_win = NULL;
9480
9481 if (ssop_flags & SSOP_BUFFERS)
9482 only_save_windows = FALSE; /* Save ALL buffers */
9483
9484 /*
9485 * Begin by setting the this_session variable, and then other
9486 * sessionable variables.
9487 */
9488#ifdef FEAT_EVAL
9489 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9490 return FAIL;
9491 if (ssop_flags & SSOP_GLOBALS)
9492 if (store_session_globals(fd) == FAIL)
9493 return FAIL;
9494#endif
9495
9496 /*
9497 * Close all windows but one.
9498 */
9499 if (put_line(fd, "silent only") == FAIL)
9500 return FAIL;
9501
9502 /*
9503 * Now a :cd command to the session directory or the current directory
9504 */
9505 if (ssop_flags & SSOP_SESDIR)
9506 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009507 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9508 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 return FAIL;
9510 }
9511 else if (ssop_flags & SSOP_CURDIR)
9512 {
9513 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9514 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009515 || fputs("cd ", fd) < 0
9516 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9517 || put_eol(fd) == FAIL)
9518 {
9519 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 vim_free(sname);
9523 }
9524
9525 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009526 * If there is an empty, unnamed buffer we will wipe it out later.
9527 * Remember the buffer number.
9528 */
9529 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9530 return FAIL;
9531 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9532 return FAIL;
9533 if (put_line(fd, "endif") == FAIL)
9534 return FAIL;
9535
9536 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 * Now save the current files, current buffer first.
9538 */
9539 if (put_line(fd, "set shortmess=aoO") == FAIL)
9540 return FAIL;
9541
9542 /* Now put the other buffers into the buffer list */
9543 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9544 {
9545 if (!(only_save_windows && buf->b_nwindows == 0)
9546 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9547 && buf->b_fname != NULL
9548 && buf->b_p_bl)
9549 {
9550 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9551 : buf->b_wininfo->wi_fpos.lnum) < 0
9552 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9553 return FAIL;
9554 }
9555 }
9556
9557 /* the global argument list */
9558 if (ses_arglist(fd, "args", &global_alist.al_ga,
9559 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9560 return FAIL;
9561
9562 if (ssop_flags & SSOP_RESIZE)
9563 {
9564 /* Note: after the restore we still check it worked!*/
9565 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9566 || put_eol(fd) == FAIL)
9567 return FAIL;
9568 }
9569
9570#ifdef FEAT_GUI
9571 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9572 {
9573 int x, y;
9574
9575 if (gui_mch_get_winpos(&x, &y) == OK)
9576 {
9577 /* Note: after the restore we still check it worked!*/
9578 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9579 return FAIL;
9580 }
9581 }
9582#endif
9583
9584 /*
9585 * Before creating the window layout, try loading one file. If this is
9586 * aborted we don't end up with a number of useless windows.
9587 * This may have side effects! (e.g., compressed or network file).
9588 */
9589 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9590 {
9591 if (ses_do_win(wp)
9592 && wp->w_buffer->b_ffname != NULL
9593 && !wp->w_buffer->b_help
9594#ifdef FEAT_QUICKFIX
9595 && !bt_nofile(wp->w_buffer)
9596#endif
9597 )
9598 {
9599 if (fputs("edit ", fd) < 0
9600 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9601 return FAIL;
9602 if (!wp->w_arg_idx_invalid)
9603 edited_win = wp;
9604 break;
9605 }
9606 }
9607
9608 /*
9609 * Save current window layout.
9610 */
9611 if (put_line(fd, "set splitbelow splitright") == FAIL)
9612 return FAIL;
9613 if (ses_win_rec(fd, topframe) == FAIL)
9614 return FAIL;
9615 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9616 return FAIL;
9617 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9618 return FAIL;
9619
9620 /*
9621 * Check if window sizes can be restored (no windows omitted).
9622 * Remember the window number of the current window after restoring.
9623 */
9624 nr = 0;
9625 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9626 {
9627 if (ses_do_win(wp))
9628 ++nr;
9629 else
9630 restore_size = FALSE;
9631 if (curwin == wp)
9632 cnr = nr;
9633 }
9634
9635 /* Go to the first window. */
9636 if (put_line(fd, "wincmd t") == FAIL)
9637 return FAIL;
9638
9639 /*
9640 * If more than one window, see if sizes can be restored.
9641 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9642 * resized when moving between windows.
9643 * Do this before restoring the view, so that the topline and the cursor
9644 * can be set. This is done again below.
9645 */
9646 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9647 return FAIL;
9648 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9649 return FAIL;
9650
9651 /*
9652 * Restore the view of the window (options, file, cursor, etc.).
9653 */
9654 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9655 {
9656 if (!ses_do_win(wp))
9657 continue;
9658 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9659 return FAIL;
9660 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9661 return FAIL;
9662 }
9663
9664 /*
9665 * Restore cursor to the current window if it's not the first one.
9666 */
9667 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9668 return FAIL;
9669
9670 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009671 * Wipe out an empty unnamed buffer we started in.
9672 */
9673 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9674 return FAIL;
9675 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9676 return FAIL;
9677 if (put_line(fd, "endif") == FAIL)
9678 return FAIL;
9679 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9680 return FAIL;
9681
9682 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 * Restore window sizes again after jumping around in windows, because the
9684 * current window has a minimum size while others may not.
9685 */
9686 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9687 return FAIL;
9688
9689 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9690 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9691 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9692 return FAIL;
9693
9694 /*
9695 * Lastly, execute the x.vim file if it exists.
9696 */
9697 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9698 || put_line(fd, "if file_readable(s:sx)") == FAIL
9699 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9700 || put_line(fd, "endif") == FAIL)
9701 return FAIL;
9702
9703 return OK;
9704}
9705
9706 static int
9707ses_winsizes(fd, restore_size)
9708 FILE *fd;
9709 int restore_size;
9710{
9711 int n = 0;
9712 win_T *wp;
9713
9714 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9715 {
9716 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9717 {
9718 if (!ses_do_win(wp))
9719 continue;
9720 ++n;
9721
9722 /* restore height when not full height */
9723 if (wp->w_height + wp->w_status_height < topframe->fr_height
9724 && (fprintf(fd,
9725 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9726 n, (long)wp->w_height, Rows / 2, Rows) < 0
9727 || put_eol(fd) == FAIL))
9728 return FAIL;
9729
9730 /* restore width when not full width */
9731 if (wp->w_width < Columns && (fprintf(fd,
9732 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9733 n, (long)wp->w_width, Columns / 2, Columns) < 0
9734 || put_eol(fd) == FAIL))
9735 return FAIL;
9736 }
9737 }
9738 else
9739 {
9740 /* Just equalise window sizes */
9741 if (put_line(fd, "wincmd =") == FAIL)
9742 return FAIL;
9743 }
9744 return OK;
9745}
9746
9747/*
9748 * Write commands to "fd" to recursively create windows for frame "fr",
9749 * horizontally and vertically split.
9750 * After the commands the last window in the frame is the current window.
9751 * Returns FAIL when writing the commands to "fd" fails.
9752 */
9753 static int
9754ses_win_rec(fd, fr)
9755 FILE *fd;
9756 frame_T *fr;
9757{
9758 frame_T *frc;
9759 int count = 0;
9760
9761 if (fr->fr_layout != FR_LEAF)
9762 {
9763 /* Find first frame that's not skipped and then create a window for
9764 * each following one (first frame is already there). */
9765 frc = ses_skipframe(fr->fr_child);
9766 if (frc != NULL)
9767 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9768 {
9769 /* Make window as big as possible so that we have lots of room
9770 * to split. */
9771 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9772 || put_line(fd, fr->fr_layout == FR_COL
9773 ? "split" : "vsplit") == FAIL)
9774 return FAIL;
9775 ++count;
9776 }
9777
9778 /* Go back to the first window. */
9779 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9780 ? "%dwincmd k" : "%dwincmd h", count) < 0
9781 || put_eol(fd) == FAIL))
9782 return FAIL;
9783
9784 /* Recursively create frames/windows in each window of this column or
9785 * row. */
9786 frc = ses_skipframe(fr->fr_child);
9787 while (frc != NULL)
9788 {
9789 ses_win_rec(fd, frc);
9790 frc = ses_skipframe(frc->fr_next);
9791 /* Go to next window. */
9792 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9793 return FAIL;
9794 }
9795 }
9796 return OK;
9797}
9798
9799/*
9800 * Skip frames that don't contain windows we want to save in the Session.
9801 * Returns NULL when there none.
9802 */
9803 static frame_T *
9804ses_skipframe(fr)
9805 frame_T *fr;
9806{
9807 frame_T *frc;
9808
9809 for (frc = fr; frc != NULL; frc = frc->fr_next)
9810 if (ses_do_frame(frc))
9811 break;
9812 return frc;
9813}
9814
9815/*
9816 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9817 * the Session.
9818 */
9819 static int
9820ses_do_frame(fr)
9821 frame_T *fr;
9822{
9823 frame_T *frc;
9824
9825 if (fr->fr_layout == FR_LEAF)
9826 return ses_do_win(fr->fr_win);
9827 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9828 if (ses_do_frame(frc))
9829 return TRUE;
9830 return FALSE;
9831}
9832
9833/*
9834 * Return non-zero if window "wp" is to be stored in the Session.
9835 */
9836 static int
9837ses_do_win(wp)
9838 win_T *wp;
9839{
9840 if (wp->w_buffer->b_fname == NULL
9841#ifdef FEAT_QUICKFIX
9842 /* When 'buftype' is "nofile" can't restore the window contents. */
9843 || bt_nofile(wp->w_buffer)
9844#endif
9845 )
9846 return (ssop_flags & SSOP_BLANK);
9847 if (wp->w_buffer->b_help)
9848 return (ssop_flags & SSOP_HELP);
9849 return TRUE;
9850}
9851
9852/*
9853 * Write commands to "fd" to restore the view of a window.
9854 * Caller must make sure 'scrolloff' is zero.
9855 */
9856 static int
9857put_view(fd, wp, add_edit, flagp)
9858 FILE *fd;
9859 win_T *wp;
9860 int add_edit; /* add ":edit" command to view */
9861 unsigned *flagp; /* vop_flags or ssop_flags */
9862{
9863 win_T *save_curwin;
9864 int f;
9865 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009866 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867
9868 /* Always restore cursor position for ":mksession". For ":mkview" only
9869 * when 'viewoptions' contains "cursor". */
9870 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9871
9872 /*
9873 * Local argument list.
9874 */
9875 if (wp->w_alist == &global_alist)
9876 {
9877 if (put_line(fd, "argglobal") == FAIL)
9878 return FAIL;
9879 }
9880 else
9881 {
9882 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9883 flagp == &vop_flags
9884 || !(*flagp & SSOP_CURDIR)
9885 || wp->w_localdir != NULL, flagp) == FAIL)
9886 return FAIL;
9887 }
9888
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009889 /* Only when part of a session: restore the argument index. Some
9890 * arguments may have been deleted, check if the index is valid. */
9891 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
9892 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 {
9894 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9895 || put_eol(fd) == FAIL)
9896 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009897 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 }
9899
9900 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009901 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 {
9903 /*
9904 * Load the file.
9905 */
9906 if (wp->w_buffer->b_ffname != NULL
9907#ifdef FEAT_QUICKFIX
9908 && !bt_nofile(wp->w_buffer)
9909#endif
9910 )
9911 {
9912 /*
9913 * Editing a file in this buffer: use ":edit file".
9914 * This may have side effects! (e.g., compressed or network file).
9915 */
9916 if (fputs("edit ", fd) < 0
9917 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9918 return FAIL;
9919 }
9920 else
9921 {
9922 /* No file in this buffer, just make it empty. */
9923 if (put_line(fd, "enew") == FAIL)
9924 return FAIL;
9925#ifdef FEAT_QUICKFIX
9926 if (wp->w_buffer->b_ffname != NULL)
9927 {
9928 /* The buffer does have a name, but it's not a file name. */
9929 if (fputs("file ", fd) < 0
9930 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9931 return FAIL;
9932 }
9933#endif
9934 do_cursor = FALSE;
9935 }
9936 }
9937
9938 /*
9939 * Local mappings and abbreviations.
9940 */
9941 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9942 && makemap(fd, wp->w_buffer) == FAIL)
9943 return FAIL;
9944
9945 /*
9946 * Local options. Need to go to the window temporarily.
9947 * Store only local values when using ":mkview" and when ":mksession" is
9948 * used and 'sessionoptions' doesn't include "options".
9949 * Some folding options are always stored when "folds" is included,
9950 * otherwise the folds would not be restored correctly.
9951 */
9952 save_curwin = curwin;
9953 curwin = wp;
9954 curbuf = curwin->w_buffer;
9955 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9956 f = makeset(fd, OPT_LOCAL,
9957 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
9958#ifdef FEAT_FOLDING
9959 else if (*flagp & SSOP_FOLDS)
9960 f = makefoldset(fd);
9961#endif
9962 else
9963 f = OK;
9964 curwin = save_curwin;
9965 curbuf = curwin->w_buffer;
9966 if (f == FAIL)
9967 return FAIL;
9968
9969#ifdef FEAT_FOLDING
9970 /*
9971 * Save Folds when 'buftype' is empty and for help files.
9972 */
9973 if ((*flagp & SSOP_FOLDS)
9974 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +00009975# ifdef FEAT_QUICKFIX
9976 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
9977# endif
9978 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 {
9980 if (put_folds(fd, wp) == FAIL)
9981 return FAIL;
9982 }
9983#endif
9984
9985 /*
9986 * Set the cursor after creating folds, since that moves the cursor.
9987 */
9988 if (do_cursor)
9989 {
9990
9991 /* Restore the cursor line in the file and relatively in the
9992 * window. Don't use "G", it changes the jumplist. */
9993 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
9994 (long)wp->w_cursor.lnum,
9995 (long)(wp->w_cursor.lnum - wp->w_topline),
9996 (long)wp->w_height / 2, (long)wp->w_height) < 0
9997 || put_eol(fd) == FAIL
9998 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
9999 || put_line(fd, "exe s:l") == FAIL
10000 || put_line(fd, "normal! zt") == FAIL
10001 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10002 || put_eol(fd) == FAIL)
10003 return FAIL;
10004 /* Restore the cursor column and left offset when not wrapping. */
10005 if (wp->w_cursor.col == 0)
10006 {
10007 if (put_line(fd, "normal! 0") == FAIL)
10008 return FAIL;
10009 }
10010 else
10011 {
10012 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10013 {
10014 if (fprintf(fd,
10015 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10016 (long)wp->w_cursor.col,
10017 (long)(wp->w_cursor.col - wp->w_leftcol),
10018 (long)wp->w_width / 2, (long)wp->w_width) < 0
10019 || put_eol(fd) == FAIL
10020 || put_line(fd, "if s:c > 0") == FAIL
10021 || fprintf(fd,
10022 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10023 (long)wp->w_cursor.col) < 0
10024 || put_eol(fd) == FAIL
10025 || put_line(fd, "else") == FAIL
10026 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10027 || put_eol(fd) == FAIL
10028 || put_line(fd, "endif") == FAIL)
10029 return FAIL;
10030 }
10031 else
10032 {
10033 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10034 || put_eol(fd) == FAIL)
10035 return FAIL;
10036 }
10037 }
10038 }
10039
10040 /*
10041 * Local directory.
10042 */
10043 if (wp->w_localdir != NULL)
10044 {
10045 if (fputs("lcd ", fd) < 0
10046 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10047 || put_eol(fd) == FAIL)
10048 return FAIL;
10049 }
10050
10051 return OK;
10052}
10053
10054/*
10055 * Write an argument list to the session file.
10056 * Returns FAIL if writing fails.
10057 */
10058 static int
10059ses_arglist(fd, cmd, gap, fullname, flagp)
10060 FILE *fd;
10061 char *cmd;
10062 garray_T *gap;
10063 int fullname; /* TRUE: use full path name */
10064 unsigned *flagp;
10065{
10066 int i;
10067 char_u buf[MAXPATHL];
10068 char_u *s;
10069
10070 if (gap->ga_len == 0)
10071 return put_line(fd, "silent! argdel *");
10072 if (fputs(cmd, fd) < 0)
10073 return FAIL;
10074 for (i = 0; i < gap->ga_len; ++i)
10075 {
10076 /* NULL file names are skipped (only happens when out of memory). */
10077 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10078 if (s != NULL)
10079 {
10080 if (fullname)
10081 {
10082 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10083 s = buf;
10084 }
10085 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10086 return FAIL;
10087 }
10088 }
10089 return put_eol(fd);
10090}
10091
10092/*
10093 * Write a buffer name to the session file.
10094 * Also ends the line.
10095 * Returns FAIL if writing fails.
10096 */
10097 static int
10098ses_fname(fd, buf, flagp)
10099 FILE *fd;
10100 buf_T *buf;
10101 unsigned *flagp;
10102{
10103 char_u *name;
10104
10105 /* Use the short file name if the current directory is known at the time
10106 * the session file will be sourced. Don't do this for ":mkview", we
10107 * don't know the current directory. */
10108 if (buf->b_sfname != NULL
10109 && flagp == &ssop_flags
10110 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
10111 name = buf->b_sfname;
10112 else
10113 name = buf->b_ffname;
10114 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10115 return FAIL;
10116 return OK;
10117}
10118
10119/*
10120 * Write a file name to the session file.
10121 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10122 * characters.
10123 * Returns FAIL if writing fails.
10124 */
10125 static int
10126ses_put_fname(fd, name, flagp)
10127 FILE *fd;
10128 char_u *name;
10129 unsigned *flagp;
10130{
10131 char_u *sname;
10132 int retval = OK;
10133 int c;
10134
10135 sname = home_replace_save(NULL, name);
10136 if (sname != NULL)
10137 name = sname;
10138 while (*name != NUL)
10139 {
10140#ifdef FEAT_MBYTE
10141 {
10142 int l;
10143
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010144 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 {
10146 /* copy a multibyte char */
10147 while (--l >= 0)
10148 {
10149 if (putc(*name, fd) != *name)
10150 retval = FAIL;
10151 ++name;
10152 }
10153 continue;
10154 }
10155 }
10156#endif
10157 c = *name++;
10158 if (c == '\\' && (*flagp & SSOP_SLASH))
10159 /* change a backslash to a forward slash */
10160 c = '/';
10161 else if ((vim_strchr(escape_chars, c) != NULL
10162#ifdef BACKSLASH_IN_FILENAME
10163 && c != '\\'
10164#endif
10165 ) || c == '#' || c == '%')
10166 {
10167 /* escape a special character with a backslash */
10168 if (putc('\\', fd) != '\\')
10169 retval = FAIL;
10170 }
10171 if (putc(c, fd) != c)
10172 retval = FAIL;
10173 }
10174 vim_free(sname);
10175 return retval;
10176}
10177
10178/*
10179 * ":loadview [nr]"
10180 */
10181 static void
10182ex_loadview(eap)
10183 exarg_T *eap;
10184{
10185 char_u *fname;
10186
10187 fname = get_view_file(*eap->arg);
10188 if (fname != NULL)
10189 {
10190 do_source(fname, FALSE, FALSE);
10191 vim_free(fname);
10192 }
10193}
10194
10195/*
10196 * Get the name of the view file for the current buffer.
10197 */
10198 static char_u *
10199get_view_file(c)
10200 int c;
10201{
10202 int len = 0;
10203 char_u *p, *s;
10204 char_u *retval;
10205 char_u *sname;
10206
10207 if (curbuf->b_ffname == NULL)
10208 {
10209 EMSG(_(e_noname));
10210 return NULL;
10211 }
10212 sname = home_replace_save(NULL, curbuf->b_ffname);
10213 if (sname == NULL)
10214 return NULL;
10215
10216 /*
10217 * We want a file name without separators, because we're not going to make
10218 * a directory.
10219 * "normal" path separator -> "=+"
10220 * "=" -> "=="
10221 * ":" path separator -> "=-"
10222 */
10223 for (p = sname; *p; ++p)
10224 if (*p == '=' || vim_ispathsep(*p))
10225 ++len;
10226 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10227 if (retval != NULL)
10228 {
10229 STRCPY(retval, p_vdir);
10230 add_pathsep(retval);
10231 s = retval + STRLEN(retval);
10232 for (p = sname; *p; ++p)
10233 {
10234 if (*p == '=')
10235 {
10236 *s++ = '=';
10237 *s++ = '=';
10238 }
10239 else if (vim_ispathsep(*p))
10240 {
10241 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010242#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 || defined(VMS)
10244 if (*p == ':')
10245 *s++ = '-';
10246 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010248 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 }
10250 else
10251 *s++ = *p;
10252 }
10253 *s++ = '=';
10254 *s++ = c;
10255 STRCPY(s, ".vim");
10256 }
10257
10258 vim_free(sname);
10259 return retval;
10260}
10261
10262#endif /* FEAT_SESSION */
10263
10264/*
10265 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10266 * Return FAIL for a write error.
10267 */
10268 int
10269put_eol(fd)
10270 FILE *fd;
10271{
10272 if (
10273#ifdef USE_CRNL
10274 (
10275# ifdef MKSESSION_NL
10276 !mksession_nl &&
10277# endif
10278 (putc('\r', fd) < 0)) ||
10279#endif
10280 (putc('\n', fd) < 0))
10281 return FAIL;
10282 return OK;
10283}
10284
10285/*
10286 * Write a line to "fd".
10287 * Return FAIL for a write error.
10288 */
10289 int
10290put_line(fd, s)
10291 FILE *fd;
10292 char *s;
10293{
10294 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10295 return FAIL;
10296 return OK;
10297}
10298
10299#ifdef FEAT_VIMINFO
10300/*
10301 * ":rviminfo" and ":wviminfo".
10302 */
10303 static void
10304ex_viminfo(eap)
10305 exarg_T *eap;
10306{
10307 char_u *save_viminfo;
10308
10309 save_viminfo = p_viminfo;
10310 if (*p_viminfo == NUL)
10311 p_viminfo = (char_u *)"'100";
10312 if (eap->cmdidx == CMD_rviminfo)
10313 {
10314 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
10315 EMSG(_("E195: Cannot open viminfo file for reading"));
10316 }
10317 else
10318 write_viminfo(eap->arg, eap->forceit);
10319 p_viminfo = save_viminfo;
10320}
10321#endif
10322
10323#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010324/*
10325 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010326 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 void
10329dialog_msg(buff, format, fname)
10330 char_u *buff;
10331 char *format;
10332 char_u *fname;
10333{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 if (fname == NULL)
10335 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010336 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337}
10338#endif
10339
10340/*
10341 * ":behave {mswin,xterm}"
10342 */
10343 static void
10344ex_behave(eap)
10345 exarg_T *eap;
10346{
10347 if (STRCMP(eap->arg, "mswin") == 0)
10348 {
10349 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10350 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10351 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10352 set_option_value((char_u *)"keymodel", 0L,
10353 (char_u *)"startsel,stopsel", 0);
10354 }
10355 else if (STRCMP(eap->arg, "xterm") == 0)
10356 {
10357 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10358 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10359 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10360 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10361 }
10362 else
10363 EMSG2(_(e_invarg2), eap->arg);
10364}
10365
10366#ifdef FEAT_AUTOCMD
10367static int filetype_detect = FALSE;
10368static int filetype_plugin = FALSE;
10369static int filetype_indent = FALSE;
10370
10371/*
10372 * ":filetype [plugin] [indent] {on,off,detect}"
10373 * on: Load the filetype.vim file to install autocommands for file types.
10374 * off: Load the ftoff.vim file to remove all autocommands for file types.
10375 * plugin on: load filetype.vim and ftplugin.vim
10376 * plugin off: load ftplugof.vim
10377 * indent on: load filetype.vim and indent.vim
10378 * indent off: load indoff.vim
10379 */
10380 static void
10381ex_filetype(eap)
10382 exarg_T *eap;
10383{
10384 char_u *arg = eap->arg;
10385 int plugin = FALSE;
10386 int indent = FALSE;
10387
10388 if (*eap->arg == NUL)
10389 {
10390 /* Print current status. */
10391 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10392 filetype_detect ? "ON" : "OFF",
10393 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10394 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10395 return;
10396 }
10397
10398 /* Accept "plugin" and "indent" in any order. */
10399 for (;;)
10400 {
10401 if (STRNCMP(arg, "plugin", 6) == 0)
10402 {
10403 plugin = TRUE;
10404 arg = skipwhite(arg + 6);
10405 continue;
10406 }
10407 if (STRNCMP(arg, "indent", 6) == 0)
10408 {
10409 indent = TRUE;
10410 arg = skipwhite(arg + 6);
10411 continue;
10412 }
10413 break;
10414 }
10415 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10416 {
10417 if (*arg == 'o' || !filetype_detect)
10418 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010419 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 filetype_detect = TRUE;
10421 if (plugin)
10422 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010423 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 filetype_plugin = TRUE;
10425 }
10426 if (indent)
10427 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010428 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 filetype_indent = TRUE;
10430 }
10431 }
10432 if (*arg == 'd')
10433 {
10434 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +000010435 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 }
10437 }
10438 else if (STRCMP(arg, "off") == 0)
10439 {
10440 if (plugin || indent)
10441 {
10442 if (plugin)
10443 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010444 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 filetype_plugin = FALSE;
10446 }
10447 if (indent)
10448 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010449 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 filetype_indent = FALSE;
10451 }
10452 }
10453 else
10454 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010455 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 filetype_detect = FALSE;
10457 }
10458 }
10459 else
10460 EMSG2(_(e_invarg2), arg);
10461}
10462
10463/*
10464 * ":setfiletype {name}"
10465 */
10466 static void
10467ex_setfiletype(eap)
10468 exarg_T *eap;
10469{
10470 if (!did_filetype)
10471 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10472}
10473#endif
10474
10475/*ARGSUSED*/
10476 static void
10477ex_digraphs(eap)
10478 exarg_T *eap;
10479{
10480#ifdef FEAT_DIGRAPHS
10481 if (*eap->arg != NUL)
10482 putdigraph(eap->arg);
10483 else
10484 listdigraphs();
10485#else
10486 EMSG(_("E196: No digraphs in this version"));
10487#endif
10488}
10489
10490 static void
10491ex_set(eap)
10492 exarg_T *eap;
10493{
10494 int flags = 0;
10495
10496 if (eap->cmdidx == CMD_setlocal)
10497 flags = OPT_LOCAL;
10498 else if (eap->cmdidx == CMD_setglobal)
10499 flags = OPT_GLOBAL;
10500#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10501 if (cmdmod.browse && flags == 0)
10502 ex_options(eap);
10503 else
10504#endif
10505 (void)do_set(eap->arg, flags);
10506}
10507
10508#ifdef FEAT_SEARCH_EXTRA
10509/*
10510 * ":nohlsearch"
10511 */
10512/*ARGSUSED*/
10513 static void
10514ex_nohlsearch(eap)
10515 exarg_T *eap;
10516{
10517 no_hlsearch = TRUE;
10518 redraw_all_later(NOT_VALID);
10519}
10520
10521/*
10522 * ":match {group} {pattern}"
10523 * Sets nextcmd to the start of the next command, if any. Also called when
10524 * skipping commands to find the next command.
10525 */
10526 static void
10527ex_match(eap)
10528 exarg_T *eap;
10529{
10530 char_u *p;
10531 char_u *end;
10532 int c;
10533
10534 /* First clear any old pattern. */
10535 if (!eap->skip)
10536 {
10537 vim_free(curwin->w_match.regprog);
10538 curwin->w_match.regprog = NULL;
10539 redraw_later(NOT_VALID); /* always need a redraw */
10540 }
10541
10542 if (ends_excmd(*eap->arg))
10543 end = eap->arg;
10544 else if ((STRNICMP(eap->arg, "none", 4) == 0
10545 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10546 end = eap->arg + 4;
10547 else
10548 {
10549 p = skiptowhite(eap->arg);
10550 if (!eap->skip)
10551 {
10552 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg));
10553 if (curwin->w_match_id == 0)
10554 {
10555 EMSG2(_(e_nogroup), eap->arg);
10556 return;
10557 }
10558 }
10559 p = skipwhite(p);
10560 if (*p == NUL)
10561 {
10562 /* There must be two arguments. */
10563 EMSG2(_(e_invarg2), eap->arg);
10564 return;
10565 }
10566 end = skip_regexp(p + 1, *p, TRUE, NULL);
10567 if (!eap->skip)
10568 {
10569 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10570 {
10571 eap->errmsg = e_trailing;
10572 return;
10573 }
10574
10575 c = *end;
10576 *end = NUL;
10577 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC);
10578 *end = c;
10579 if (curwin->w_match.regprog == NULL)
10580 {
10581 EMSG2(_(e_invarg2), p);
10582 return;
10583 }
10584 }
10585 }
10586 eap->nextcmd = find_nextcmd(end);
10587}
10588#endif
10589
10590#ifdef FEAT_CRYPT
10591/*
10592 * ":X": Get crypt key
10593 */
10594/*ARGSUSED*/
10595 static void
10596ex_X(eap)
10597 exarg_T *eap;
10598{
10599 (void)get_crypt_key(TRUE, TRUE);
10600}
10601#endif
10602
10603#ifdef FEAT_FOLDING
10604 static void
10605ex_fold(eap)
10606 exarg_T *eap;
10607{
10608 if (foldManualAllowed(TRUE))
10609 foldCreate(eap->line1, eap->line2);
10610}
10611
10612 static void
10613ex_foldopen(eap)
10614 exarg_T *eap;
10615{
10616 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10617 eap->forceit, FALSE);
10618}
10619
10620 static void
10621ex_folddo(eap)
10622 exarg_T *eap;
10623{
10624 linenr_T lnum;
10625
10626 /* First set the marks for all lines closed/open. */
10627 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10628 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10629 ml_setmarked(lnum);
10630
10631 /* Execute the command on the marked lines. */
10632 global_exe(eap->arg);
10633 ml_clearmarked(); /* clear rest of the marks */
10634}
10635#endif