blob: 5b07cf613d9aab9a0cc49b8e6f49baa4f37e9eab [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
127
128static int check_more __ARGS((int, int));
129static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000130static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000132 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133static void ex_script_ni __ARGS((exarg_T *eap));
134#endif
135static char_u *invalid_range __ARGS((exarg_T *eap));
136static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000137#ifdef FEAT_QUICKFIX
138static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
141static void ex_highlight __ARGS((exarg_T *eap));
142static void ex_colorscheme __ARGS((exarg_T *eap));
143static void ex_quit __ARGS((exarg_T *eap));
144static void ex_cquit __ARGS((exarg_T *eap));
145static void ex_quit_all __ARGS((exarg_T *eap));
146#ifdef FEAT_WINDOWS
147static void ex_close __ARGS((exarg_T *eap));
148static void ex_win_close __ARGS((exarg_T *eap, win_T *win));
149static void ex_only __ARGS((exarg_T *eap));
150static void ex_all __ARGS((exarg_T *eap));
151static void ex_resize __ARGS((exarg_T *eap));
152static void ex_stag __ARGS((exarg_T *eap));
153#else
154# define ex_close ex_ni
155# define ex_only ex_ni
156# define ex_all ex_ni
157# define ex_resize ex_ni
158# define ex_splitview ex_ni
159# define ex_stag ex_ni
160#endif
161#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
162static void ex_pclose __ARGS((exarg_T *eap));
163static void ex_ptag __ARGS((exarg_T *eap));
164static void ex_pedit __ARGS((exarg_T *eap));
165#else
166# define ex_pclose ex_ni
167# define ex_ptag ex_ni
168# define ex_pedit ex_ni
169#endif
170static void ex_hide __ARGS((exarg_T *eap));
171static void ex_stop __ARGS((exarg_T *eap));
172static void ex_exit __ARGS((exarg_T *eap));
173static void ex_print __ARGS((exarg_T *eap));
174#ifdef FEAT_BYTEOFF
175static void ex_goto __ARGS((exarg_T *eap));
176#else
177# define ex_goto ex_ni
178#endif
179static void ex_shell __ARGS((exarg_T *eap));
180static void ex_preserve __ARGS((exarg_T *eap));
181static void ex_recover __ARGS((exarg_T *eap));
182#ifndef FEAT_LISTCMDS
183# define ex_argedit ex_ni
184# define ex_argadd ex_ni
185# define ex_argdelete ex_ni
186# define ex_listdo ex_ni
187#endif
188static void ex_mode __ARGS((exarg_T *eap));
189static void ex_wrongmodifier __ARGS((exarg_T *eap));
190static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000191static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192static void ex_edit __ARGS((exarg_T *eap));
193#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
194# define ex_drop ex_ni
195#endif
196#ifndef FEAT_GUI
197# define ex_gui ex_nogui
198static void ex_nogui __ARGS((exarg_T *eap));
199#endif
200#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
201static void ex_tearoff __ARGS((exarg_T *eap));
202#else
203# define ex_tearoff ex_ni
204#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000205#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_KDE) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206static void ex_popup __ARGS((exarg_T *eap));
207#else
208# define ex_popup ex_ni
209#endif
210#ifndef FEAT_GUI_MSWIN
211# define ex_simalt ex_ni
212#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000213#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_KDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214# define gui_mch_find_dialog ex_ni
215# define gui_mch_replace_dialog ex_ni
216#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000217#if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_KDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218# define ex_helpfind ex_ni
219#endif
220#ifndef FEAT_CSCOPE
221# define do_cscope ex_ni
222# define do_scscope ex_ni
223# define do_cstag ex_ni
224#endif
225#ifndef FEAT_SYN_HL
226# define ex_syntax ex_ni
Bram Moolenaarb765d632005-06-07 21:00:02 +0000227# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000228# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000229# define ex_spelldump ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000230# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000231#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000232#ifndef FEAT_MZSCHEME
233# define ex_mzscheme ex_script_ni
234# define ex_mzfile ex_ni
235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236#ifndef FEAT_PERL
237# define ex_perl ex_script_ni
238# define ex_perldo ex_ni
239#endif
240#ifndef FEAT_PYTHON
241# define ex_python ex_script_ni
242# define ex_pyfile ex_ni
243#endif
244#ifndef FEAT_TCL
245# define ex_tcl ex_script_ni
246# define ex_tcldo ex_ni
247# define ex_tclfile ex_ni
248#endif
249#ifndef FEAT_RUBY
250# define ex_ruby ex_script_ni
251# define ex_rubydo ex_ni
252# define ex_rubyfile ex_ni
253#endif
254#ifndef FEAT_SNIFF
255# define ex_sniff ex_ni
256#endif
257#ifndef FEAT_KEYMAP
258# define ex_loadkeymap ex_ni
259#endif
260static void ex_swapname __ARGS((exarg_T *eap));
261static void ex_syncbind __ARGS((exarg_T *eap));
262static void ex_read __ARGS((exarg_T *eap));
263static void ex_cd __ARGS((exarg_T *eap));
264static void ex_pwd __ARGS((exarg_T *eap));
265static void ex_equal __ARGS((exarg_T *eap));
266static void ex_sleep __ARGS((exarg_T *eap));
267static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
268static void ex_winsize __ARGS((exarg_T *eap));
269#ifdef FEAT_WINDOWS
270static void ex_wincmd __ARGS((exarg_T *eap));
271#else
272# define ex_wincmd ex_ni
273#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000274#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275static void ex_winpos __ARGS((exarg_T *eap));
276#else
277# define ex_winpos ex_ni
278#endif
279static void ex_operators __ARGS((exarg_T *eap));
280static void ex_put __ARGS((exarg_T *eap));
281static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000282static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283static void ex_submagic __ARGS((exarg_T *eap));
284static void ex_join __ARGS((exarg_T *eap));
285static void ex_at __ARGS((exarg_T *eap));
286static void ex_bang __ARGS((exarg_T *eap));
287static void ex_undo __ARGS((exarg_T *eap));
288static void ex_redo __ARGS((exarg_T *eap));
289static void ex_redir __ARGS((exarg_T *eap));
290static void ex_redraw __ARGS((exarg_T *eap));
291static void ex_redrawstatus __ARGS((exarg_T *eap));
292static void close_redir __ARGS((void));
293static void ex_mkrc __ARGS((exarg_T *eap));
294static void ex_mark __ARGS((exarg_T *eap));
295#ifdef FEAT_USR_CMDS
296static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000297static 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 +0000298#endif
299#ifdef FEAT_EX_EXTRA
300static void ex_normal __ARGS((exarg_T *eap));
301static void ex_startinsert __ARGS((exarg_T *eap));
302static void ex_stopinsert __ARGS((exarg_T *eap));
303#else
304# define ex_normal ex_ni
305# define ex_align ex_ni
306# define ex_retab ex_ni
307# define ex_startinsert ex_ni
308# define ex_stopinsert ex_ni
309# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000310# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
312#ifdef FEAT_FIND_ID
313static void ex_checkpath __ARGS((exarg_T *eap));
314static void ex_findpat __ARGS((exarg_T *eap));
315#else
316# define ex_findpat ex_ni
317# define ex_checkpath ex_ni
318#endif
319#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
320static void ex_psearch __ARGS((exarg_T *eap));
321#else
322# define ex_psearch ex_ni
323#endif
324static void ex_tag __ARGS((exarg_T *eap));
325static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
326#ifndef FEAT_EVAL
327# define ex_scriptnames ex_ni
328# define ex_finish ex_ni
329# define ex_echo ex_ni
330# define ex_echohl ex_ni
331# define ex_execute ex_ni
332# define ex_call ex_ni
333# define ex_if ex_ni
334# define ex_endif ex_ni
335# define ex_else ex_ni
336# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000337# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338# define ex_continue ex_ni
339# define ex_break ex_ni
340# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000341# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342# define ex_throw ex_ni
343# define ex_try ex_ni
344# define ex_catch ex_ni
345# define ex_finally ex_ni
346# define ex_endtry ex_ni
347# define ex_endfunction ex_ni
348# define ex_let ex_ni
349# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000350# define ex_lockvar ex_ni
351# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352# define ex_function ex_ni
353# define ex_delfunction ex_ni
354# define ex_return ex_ni
355#endif
356static char_u *arg_all __ARGS((void));
357#ifdef FEAT_SESSION
358static int makeopens __ARGS((FILE *fd, char_u *dirnow));
359static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp));
360static void ex_loadview __ARGS((exarg_T *eap));
361static char_u *get_view_file __ARGS((int c));
362#else
363# define ex_loadview ex_ni
364#endif
365#ifndef FEAT_EVAL
366# define ex_compiler ex_ni
367#endif
368#ifdef FEAT_VIMINFO
369static void ex_viminfo __ARGS((exarg_T *eap));
370#else
371# define ex_viminfo ex_ni
372#endif
373static void ex_behave __ARGS((exarg_T *eap));
374#ifdef FEAT_AUTOCMD
375static void ex_filetype __ARGS((exarg_T *eap));
376static void ex_setfiletype __ARGS((exarg_T *eap));
377#else
378# define ex_filetype ex_ni
379# define ex_setfiletype ex_ni
380#endif
381#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000382# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383# define ex_diffpatch ex_ni
384# define ex_diffgetput ex_ni
385# define ex_diffsplit ex_ni
386# define ex_diffthis ex_ni
387# define ex_diffupdate ex_ni
388#endif
389static void ex_digraphs __ARGS((exarg_T *eap));
390static void ex_set __ARGS((exarg_T *eap));
391#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
392# define ex_options ex_ni
393#endif
394#ifdef FEAT_SEARCH_EXTRA
395static void ex_nohlsearch __ARGS((exarg_T *eap));
396static void ex_match __ARGS((exarg_T *eap));
397#else
398# define ex_nohlsearch ex_ni
399# define ex_match ex_ni
400#endif
401#ifdef FEAT_CRYPT
402static void ex_X __ARGS((exarg_T *eap));
403#else
404# define ex_X ex_ni
405#endif
406#ifdef FEAT_FOLDING
407static void ex_fold __ARGS((exarg_T *eap));
408static void ex_foldopen __ARGS((exarg_T *eap));
409static void ex_folddo __ARGS((exarg_T *eap));
410#else
411# define ex_fold ex_ni
412# define ex_foldopen ex_ni
413# define ex_folddo ex_ni
414#endif
415#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
416 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
417# define ex_language ex_ni
418#endif
419#ifndef FEAT_SIGNS
420# define ex_sign ex_ni
421#endif
422#ifndef FEAT_SUN_WORKSHOP
423# define ex_wsverb ex_ni
424#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000425#ifndef FEAT_NETBEANS_INTG
426# define ex_nbkey ex_ni
427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428
429#ifndef FEAT_EVAL
430# define ex_debug ex_ni
431# define ex_breakadd ex_ni
432# define ex_debuggreedy ex_ni
433# define ex_breakdel ex_ni
434# define ex_breaklist ex_ni
435#endif
436
437#ifndef FEAT_CMDHIST
438# define ex_history ex_ni
439#endif
440#ifndef FEAT_JUMPLIST
441# define ex_jumps ex_ni
442# define ex_changes ex_ni
443#endif
444
Bram Moolenaar05159a02005-02-26 23:04:13 +0000445#ifndef FEAT_PROFILE
446# define ex_profile ex_ni
447#endif
448
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449/*
450 * Declare cmdnames[].
451 */
452#define DO_DECLARE_EXCMD
453#include "ex_cmds.h"
454
455/*
456 * Table used to quickly search for a command, based on its first character.
457 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000458static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459{
460 CMD_append,
461 CMD_buffer,
462 CMD_change,
463 CMD_delete,
464 CMD_edit,
465 CMD_file,
466 CMD_global,
467 CMD_help,
468 CMD_insert,
469 CMD_join,
470 CMD_k,
471 CMD_list,
472 CMD_move,
473 CMD_next,
474 CMD_open,
475 CMD_print,
476 CMD_quit,
477 CMD_read,
478 CMD_substitute,
479 CMD_t,
480 CMD_undo,
481 CMD_vglobal,
482 CMD_write,
483 CMD_xit,
484 CMD_yank,
485 CMD_z,
486 CMD_bang
487};
488
489static char_u dollar_command[2] = {'$', 0};
490
491
492#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000493/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494typedef struct
495{
496 char_u *line; /* command line */
497 linenr_T lnum; /* sourcing_lnum of the line */
498} wcmd_T;
499
500/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000501 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000503 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000505struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506{
507 garray_T *lines_gap; /* growarray with line info */
508 int current_line; /* last read line from growarray */
509 int repeating; /* TRUE when looping a second time */
510 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
511 char_u *(*getline) __ARGS((int, void *, int));
512 void *cookie;
513};
514
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000515static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
516static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000518
519/* Struct to save a few things while debugging. Used in do_cmdline() only. */
520struct dbg_stuff
521{
522 int trylevel;
523 int force_abort;
524 except_T *caught_stack;
525 char_u *vv_exception;
526 char_u *vv_throwpoint;
527 int did_emsg;
528 int got_int;
529 int did_throw;
530 int need_rethrow;
531 int check_cstack;
532 except_T *current_exception;
533};
534
535static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
536static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
537
538 static void
539save_dbg_stuff(dsp)
540 struct dbg_stuff *dsp;
541{
542 dsp->trylevel = trylevel; trylevel = 0;
543 dsp->force_abort = force_abort; force_abort = FALSE;
544 dsp->caught_stack = caught_stack; caught_stack = NULL;
545 dsp->vv_exception = v_exception(NULL);
546 dsp->vv_throwpoint = v_throwpoint(NULL);
547
548 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
549 dsp->did_emsg = did_emsg; did_emsg = FALSE;
550 dsp->got_int = got_int; got_int = FALSE;
551 dsp->did_throw = did_throw; did_throw = FALSE;
552 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
553 dsp->check_cstack = check_cstack; check_cstack = FALSE;
554 dsp->current_exception = current_exception; current_exception = NULL;
555}
556
557 static void
558restore_dbg_stuff(dsp)
559 struct dbg_stuff *dsp;
560{
561 suppress_errthrow = FALSE;
562 trylevel = dsp->trylevel;
563 force_abort = dsp->force_abort;
564 caught_stack = dsp->caught_stack;
565 (void)v_exception(dsp->vv_exception);
566 (void)v_throwpoint(dsp->vv_throwpoint);
567 did_emsg = dsp->did_emsg;
568 got_int = dsp->got_int;
569 did_throw = dsp->did_throw;
570 need_rethrow = dsp->need_rethrow;
571 check_cstack = dsp->check_cstack;
572 current_exception = dsp->current_exception;
573}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574#endif
575
576
577/*
578 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
579 * command is given.
580 */
581 void
582do_exmode(improved)
583 int improved; /* TRUE for "improved Ex" mode */
584{
585 int save_msg_scroll;
586 int prev_msg_row;
587 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000588 int changedtick;
589
590 if (improved)
591 exmode_active = EXMODE_VIM;
592 else
593 exmode_active = EXMODE_NORMAL;
594 State = NORMAL;
595
596 /* When using ":global /pat/ visual" and then "Q" we return to continue
597 * the :global command. */
598 if (global_busy)
599 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600
601 save_msg_scroll = msg_scroll;
602 ++RedrawingDisabled; /* don't redisplay the window */
603 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604#ifdef FEAT_GUI
605 /* Ignore scrollbar and mouse events in Ex mode */
606 ++hold_gui_events;
607#endif
608#ifdef FEAT_SNIFF
609 want_sniff_request = 0; /* No K_SNIFF wanted */
610#endif
611
612 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
613 while (exmode_active)
614 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000615#ifdef FEAT_EX_EXTRA
616 /* Check for a ":normal" command and no more characters left. */
617 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
618 {
619 exmode_active = FALSE;
620 break;
621 }
622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 msg_scroll = TRUE;
624 need_wait_return = FALSE;
625 ex_pressedreturn = FALSE;
626 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000627 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 prev_msg_row = msg_row;
629 prev_line = curwin->w_cursor.lnum;
630#ifdef FEAT_SNIFF
631 ProcessSniffRequests();
632#endif
633 if (improved)
634 {
635 cmdline_row = msg_row;
636 do_cmdline(NULL, getexline, NULL, 0);
637 }
638 else
639 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
640 lines_left = Rows - 1;
641
Bram Moolenaardf177f62005-02-22 08:39:57 +0000642 if ((prev_line != curwin->w_cursor.lnum
643 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000645 if (curbuf->b_ml.ml_flags & ML_EMPTY)
646 EMSG(_(e_emptybuf));
647 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000649 if (ex_pressedreturn)
650 {
651 /* go up one line, to overwrite the ":<CR>" line, so the
652 * output doensn't contain empty lines. */
653 msg_row = prev_msg_row;
654 if (prev_msg_row == Rows - 1)
655 msg_row--;
656 }
657 msg_col = 0;
658 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
659 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000662 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
663 {
664 if (curbuf->b_ml.ml_flags & ML_EMPTY)
665 EMSG(_(e_emptybuf));
666 else
667 EMSG(_("E501: At end-of-file"));
668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 }
670
671#ifdef FEAT_GUI
672 --hold_gui_events;
673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 --RedrawingDisabled;
675 --no_wait_return;
676 update_screen(CLEAR);
677 need_wait_return = FALSE;
678 msg_scroll = save_msg_scroll;
679}
680
681/*
682 * Execute a simple command line. Used for translated commands like "*".
683 */
684 int
685do_cmdline_cmd(cmd)
686 char_u *cmd;
687{
688 return do_cmdline(cmd, NULL, NULL,
689 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
690}
691
692/*
693 * do_cmdline(): execute one Ex command line
694 *
695 * 1. Execute "cmdline" when it is not NULL.
696 * If "cmdline" is NULL, or more lines are needed, getline() is used.
697 * 2. Split up in parts separated with '|'.
698 *
699 * This function can be called recursively!
700 *
701 * flags:
702 * DOCMD_VERBOSE - The command will be included in the error message.
703 * DOCMD_NOWAIT - Don't call wait_return() and friends.
704 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
705 * DOCMD_KEYTYPED - Don't reset KeyTyped.
706 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
707 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
708 *
709 * return FAIL if cmdline could not be executed, OK otherwise
710 */
711 int
712do_cmdline(cmdline, getline, cookie, flags)
713 char_u *cmdline;
714 char_u *(*getline) __ARGS((int, void *, int));
715 void *cookie; /* argument for getline() */
716 int flags;
717{
718 char_u *next_cmdline; /* next cmd to execute */
719 char_u *cmdline_copy = NULL; /* copy of cmd line */
720 int used_getline = FALSE; /* used "getline" to obtain command */
721 static int recursive = 0; /* recursive depth */
722 int msg_didout_before_start = 0;
723 int count = 0; /* line number count */
724 int did_inc = FALSE; /* incremented RedrawingDisabled */
725 int retval = OK;
726#ifdef FEAT_EVAL
727 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000728 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 int current_line = 0; /* active line in lines_ga */
730 char_u *fname = NULL; /* function or script name */
731 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
732 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000733 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 int initial_trylevel;
735 struct msglist **saved_msg_list = NULL;
736 struct msglist *private_msg_list;
737
738 /* "getline" and "cookie" passed to do_one_cmd() */
739 char_u *(*cmd_getline) __ARGS((int, void *, int));
740 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000741 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000743 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744#else
745# define cmd_getline getline
746# define cmd_cookie cookie
747#endif
748 static int call_depth = 0; /* recursiveness */
749
750#ifdef FEAT_EVAL
751 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
752 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000753 * This ensures that the do_errthrow() call in do_one_cmd() does not
754 * combine the messages stored by an earlier invocation of do_one_cmd()
755 * with the command name of the later one. This would happen when
756 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 saved_msg_list = msg_list;
758 msg_list = &private_msg_list;
759 private_msg_list = NULL;
760#endif
761
762 /* It's possible to create an endless loop with ":execute", catch that
763 * here. The value of 200 allows nested function calls, ":source", etc. */
764 if (call_depth == 200)
765 {
766 EMSG(_("E169: Command too recursive"));
767#ifdef FEAT_EVAL
768 /* When converting to an exception, we do not include the command name
769 * since this is not an error of the specific command. */
770 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
771 msg_list = saved_msg_list;
772#endif
773 return FAIL;
774 }
775 ++call_depth;
776
777#ifdef FEAT_EVAL
778 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000779 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 cstack.cs_trylevel = 0;
781 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000782 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
784
785 real_cookie = getline_cookie(getline, cookie);
786
787 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000788 getline_is_func = getline_equal(getline, cookie, get_func_line);
789 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 ++ex_nesting_level;
791
792 /* Get the function or script name and the address where the next breakpoint
793 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000794 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 {
796 fname = func_name(real_cookie);
797 breakpoint = func_breakpoint(real_cookie);
798 dbg_tick = func_dbg_tick(real_cookie);
799 }
800 else if (getline_equal(getline, cookie, getsourceline))
801 {
802 fname = sourcing_name;
803 breakpoint = source_breakpoint(real_cookie);
804 dbg_tick = source_dbg_tick(real_cookie);
805 }
806
807 /*
808 * Initialize "force_abort" and "suppress_errthrow" at the top level.
809 */
810 if (!recursive)
811 {
812 force_abort = FALSE;
813 suppress_errthrow = FALSE;
814 }
815
816 /*
817 * If requested, store and reset the global values controlling the
818 * exception handling (used when debugging).
819 */
820 else if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000821 save_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
823 initial_trylevel = trylevel;
824
825 /*
826 * "did_throw" will be set to TRUE when an exception is being thrown.
827 */
828 did_throw = FALSE;
829#endif
830 /*
831 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000832 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 * If force_abort is set, we cancel everything.
834 */
835 did_emsg = FALSE;
836
837 /*
838 * KeyTyped is only set when calling vgetc(). Reset it here when not
839 * calling vgetc() (sourced command lines).
840 */
841 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
842 KeyTyped = FALSE;
843
844 /*
845 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000846 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 * - for multiple commands on one line, separated with '|'
848 * - when repeating until there are no more lines (for ":source")
849 */
850 next_cmdline = cmdline;
851 do
852 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000853#ifdef FEAT_EVAL
854 getline_is_func = getline_equal(getline, cookie, get_func_line);
855#endif
856
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000857 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 if (next_cmdline == NULL
859#ifdef FEAT_EVAL
860 && !force_abort
861 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863#endif
864 )
865 did_emsg = FALSE;
866
867 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000868 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 * 2. If no line given: Get an allocated line with getline().
870 * 3. If a line is given: Make a copy, so we can mess with it.
871 */
872
873#ifdef FEAT_EVAL
874 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000875 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 {
877 /* Each '|' separated command is stored separately in lines_ga, to
878 * be able to jump to it. Don't use next_cmdline now. */
879 vim_free(cmdline_copy);
880 cmdline_copy = NULL;
881
882 /* Check if a function has returned or, unless it has an unclosed
883 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000884 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000886# ifdef FEAT_PROFILE
887 if (do_profiling)
888 func_line_end(real_cookie);
889# endif
890 if (func_has_ended(real_cookie))
891 {
892 retval = FAIL;
893 break;
894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896#ifdef FEAT_PROFILE
897 else if (do_profiling
898 && getline_equal(getline, cookie, getsourceline))
899 script_line_end();
900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
902 /* Check if a sourced file hit a ":finish" command. */
903 if (source_finished(getline, cookie))
904 {
905 retval = FAIL;
906 break;
907 }
908
909 /* If breakpoints have been added/deleted need to check for it. */
910 if (breakpoint != NULL && dbg_tick != NULL
911 && *dbg_tick != debug_tick)
912 {
913 *breakpoint = dbg_find_breakpoint(
914 getline_equal(getline, cookie, getsourceline),
915 fname, sourcing_lnum);
916 *dbg_tick = debug_tick;
917 }
918
919 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
920 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
921
922 /* Did we encounter a breakpoint? */
923 if (breakpoint != NULL && *breakpoint != 0
924 && *breakpoint <= sourcing_lnum)
925 {
926 dbg_breakpoint(fname, sourcing_lnum);
927 /* Find next breakpoint. */
928 *breakpoint = dbg_find_breakpoint(
929 getline_equal(getline, cookie, getsourceline),
930 fname, sourcing_lnum);
931 *dbg_tick = debug_tick;
932 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000933# ifdef FEAT_PROFILE
934 if (do_profiling)
935 {
936 if (getline_is_func)
937 func_line_start(real_cookie);
938 else if (getline_equal(getline, cookie, getsourceline))
939 script_line_start();
940 }
941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
943
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000944 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000946 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 * again. Pass a different "getline" function to do_one_cmd()
948 * below, so that it stores lines in or reads them from
949 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000950 * while/for loop. */
951 cmd_getline = get_loop_line;
952 cmd_cookie = (void *)&cmd_loop_cookie;
953 cmd_loop_cookie.lines_gap = &lines_ga;
954 cmd_loop_cookie.current_line = current_line;
955 cmd_loop_cookie.getline = getline;
956 cmd_loop_cookie.cookie = cookie;
957 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959 else
960 {
961 cmd_getline = getline;
962 cmd_cookie = cookie;
963 }
964#endif
965
966 /* 2. If no line given, get an allocated line with getline(). */
967 if (next_cmdline == NULL)
968 {
969 /*
970 * Need to set msg_didout for the first line after an ":if",
971 * otherwise the ":if" will be overwritten.
972 */
973 if (count == 1 && getline_equal(getline, cookie, getexline))
974 msg_didout = TRUE;
975 if (getline == NULL || (next_cmdline = getline(':', cookie,
976#ifdef FEAT_EVAL
977 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
978#else
979 0
980#endif
981 )) == NULL)
982 {
983 /* Don't call wait_return for aborted command line. The NULL
984 * returned for the end of a sourced file or executed function
985 * doesn't do this. */
986 if (KeyTyped && !(flags & DOCMD_REPEAT))
987 need_wait_return = FALSE;
988 retval = FAIL;
989 break;
990 }
991 used_getline = TRUE;
992
993 /*
994 * Keep the first typed line. Clear it when more lines are typed.
995 */
996 if (flags & DOCMD_KEEPLINE)
997 {
998 vim_free(repeat_cmdline);
999 if (count == 0)
1000 repeat_cmdline = vim_strsave(next_cmdline);
1001 else
1002 repeat_cmdline = NULL;
1003 }
1004 }
1005
1006 /* 3. Make a copy of the command so we can mess with it. */
1007 else if (cmdline_copy == NULL)
1008 {
1009 next_cmdline = vim_strsave(next_cmdline);
1010 if (next_cmdline == NULL)
1011 {
1012 EMSG(_(e_outofmem));
1013 retval = FAIL;
1014 break;
1015 }
1016 }
1017 cmdline_copy = next_cmdline;
1018
1019#ifdef FEAT_EVAL
1020 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001021 * Save the current line when inside a ":while" or ":for", and when
1022 * the command looks like a ":while" or ":for", because we may need it
1023 * later. When there is a '|' and another command, it is stored
1024 * separately, because we need to be able to jump back to it from an
1025 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001027 if (current_line == lines_ga.ga_len
1028 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001030 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 {
1032 retval = FAIL;
1033 break;
1034 }
1035 }
1036 did_endif = FALSE;
1037#endif
1038
1039 if (count++ == 0)
1040 {
1041 /*
1042 * All output from the commands is put below each other, without
1043 * waiting for a return. Don't do this when executing commands
1044 * from a script or when being called recursive (e.g. for ":e
1045 * +command file").
1046 */
1047 if (!(flags & DOCMD_NOWAIT) && !recursive)
1048 {
1049 msg_didout_before_start = msg_didout;
1050 msg_didany = FALSE; /* no output yet */
1051 msg_start();
1052 msg_scroll = TRUE; /* put messages below each other */
1053 ++no_wait_return; /* dont wait for return until finished */
1054 ++RedrawingDisabled;
1055 did_inc = TRUE;
1056 }
1057 }
1058
1059 if (p_verbose >= 15 && sourcing_name != NULL)
1060 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001062 verbose_enter_scroll();
1063
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 smsg((char_u *)_("line %ld: %s"),
1065 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001066 if (msg_silent == 0)
1067 msg_puts((char_u *)"\n"); /* don't overwrite this */
1068
1069 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 --no_wait_return;
1071 }
1072
1073 /*
1074 * 2. Execute one '|' separated command.
1075 * do_one_cmd() will return NULL if there is no trailing '|'.
1076 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1077 */
1078 ++recursive;
1079 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1080#ifdef FEAT_EVAL
1081 &cstack,
1082#endif
1083 cmd_getline, cmd_cookie);
1084 --recursive;
1085
1086#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001087 if (cmd_cookie == (void *)&cmd_loop_cookie)
1088 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091#endif
1092
1093 if (next_cmdline == NULL)
1094 {
1095 vim_free(cmdline_copy);
1096 cmdline_copy = NULL;
1097#ifdef FEAT_CMDHIST
1098 /*
1099 * If the command was typed, remember it for the ':' register.
1100 * Do this AFTER executing the command to make :@: work.
1101 */
1102 if (getline_equal(getline, cookie, getexline)
1103 && new_last_cmdline != NULL)
1104 {
1105 vim_free(last_cmdline);
1106 last_cmdline = new_last_cmdline;
1107 new_last_cmdline = NULL;
1108 }
1109#endif
1110 }
1111 else
1112 {
1113 /* need to copy the command after the '|' to cmdline_copy, for the
1114 * next do_one_cmd() */
1115 mch_memmove(cmdline_copy, next_cmdline, STRLEN(next_cmdline) + 1);
1116 next_cmdline = cmdline_copy;
1117 }
1118
1119
1120#ifdef FEAT_EVAL
1121 /* reset did_emsg for a function that is not aborted by an error */
1122 if (did_emsg && !force_abort
1123 && getline_equal(getline, cookie, get_func_line)
1124 && !func_has_abort(real_cookie))
1125 did_emsg = FALSE;
1126
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001127 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
1129 ++current_line;
1130
1131 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 * An ":endwhile", ":endfor" and ":continue" is handled here.
1133 * If we were executing commands, jump back to the ":while" or
1134 * ":for".
1135 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001141 /* Jump back to the matching ":while" or ":for". Be careful
1142 * not to use a cs_line[] from an entry that isn't a ":while"
1143 * or ":for": It would make "current_line" invalid and can
1144 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 if (!did_emsg && !got_int && !did_throw
1146 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001147 && (cstack.cs_flags[cstack.cs_idx]
1148 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 && cstack.cs_line[cstack.cs_idx] >= 0
1150 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1151 {
1152 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001153 /* remember we jumped there */
1154 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 line_breakcheck(); /* check if CTRL-C typed */
1156
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 /* Check for the next breakpoint at or after the ":while"
1158 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (breakpoint != NULL)
1160 {
1161 *breakpoint = dbg_find_breakpoint(
1162 getline_equal(getline, cookie, getsourceline),
1163 fname,
1164 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1165 *dbg_tick = debug_tick;
1166 }
1167 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001172 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1173 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 }
1175 }
1176
1177 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1184 }
1185 }
1186
1187 /*
1188 * When not inside any ":while" loop, clear remembered lines.
1189 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
1192 if (lines_ga.ga_len > 0)
1193 {
1194 sourcing_lnum =
1195 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1196 free_cmdlines(&lines_ga);
1197 }
1198 current_line = 0;
1199 }
1200
1201 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001202 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1203 * being restored at the ":endtry". Reset them here and set the
1204 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1205 * This includes the case where a missing ":endif", ":endwhile" or
1206 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001208 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001210 cstack.cs_lflags &= ~CSL_HAD_FINA;
1211 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1212 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 did_throw ? (void *)current_exception : NULL);
1214 did_emsg = got_int = did_throw = FALSE;
1215 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1216 }
1217
1218 /* Update global "trylevel" for recursive calls to do_cmdline() from
1219 * within this loop. */
1220 trylevel = initial_trylevel + cstack.cs_trylevel;
1221
1222 /*
1223 * If the outermost try conditional (accross function calls and sourced
1224 * files) is aborted because of an error, an interrupt, or an uncaught
1225 * exception, cancel everything. If it is left normally, reset
1226 * force_abort to get the non-EH compatible abortion behavior for
1227 * the rest of the script.
1228 */
1229 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1230 force_abort = FALSE;
1231
1232 /* Convert an interrupt to an exception if appropriate. */
1233 (void)do_intthrow(&cstack);
1234#endif /* FEAT_EVAL */
1235
1236 }
1237 /*
1238 * Continue executing command lines when:
1239 * - no CTRL-C typed, no aborting error, no exception thrown or try
1240 * conditionals need to be checked for executing finally clauses or
1241 * catching an interrupt exception
1242 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001243 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 * looping for ":source" command or function call.
1245 */
1246 while (!((got_int
1247#ifdef FEAT_EVAL
1248 || (did_emsg && force_abort) || did_throw
1249#endif
1250 )
1251#ifdef FEAT_EVAL
1252 && cstack.cs_trylevel == 0
1253#endif
1254 )
1255 && !(did_emsg && used_getline
1256 && (getline_equal(getline, cookie, getexmodeline)
1257 || getline_equal(getline, cookie, getexline)))
1258 && (next_cmdline != NULL
1259#ifdef FEAT_EVAL
1260 || cstack.cs_idx >= 0
1261#endif
1262 || (flags & DOCMD_REPEAT)));
1263
1264 vim_free(cmdline_copy);
1265#ifdef FEAT_EVAL
1266 free_cmdlines(&lines_ga);
1267 ga_clear(&lines_ga);
1268
1269 if (cstack.cs_idx >= 0)
1270 {
1271 /*
1272 * If a sourced file or executed function ran to its end, report the
1273 * unclosed conditional.
1274 */
1275 if (!got_int && !did_throw
1276 && ((getline_equal(getline, cookie, getsourceline)
1277 && !source_finished(getline, cookie))
1278 || (getline_equal(getline, cookie, get_func_line)
1279 && !func_has_ended(real_cookie))))
1280 {
1281 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1282 EMSG(_(e_endtry));
1283 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1284 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001285 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1286 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 else
1288 EMSG(_(e_endif));
1289 }
1290
1291 /*
1292 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1293 * ":endtry" in a sourced file or executed function. If the try
1294 * conditional is in its finally clause, ignore anything pending.
1295 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001296 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 */
1298 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001299 {
1300 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1301
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001302 if (idx >= 0)
1303 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001304 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1305 &cstack.cs_looplevel);
1306 }
1307 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 trylevel = initial_trylevel;
1309 }
1310
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001311 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1312 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 * exception, do this now after rewinding the cstack. */
1314 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1315 ? (char_u *)"endfunction" : (char_u *)NULL);
1316
1317 if (trylevel == 0)
1318 {
1319 /*
1320 * When an exception is being thrown out of the outermost try
1321 * conditional, discard the uncaught exception, disable the conversion
1322 * of interrupts or errors to exceptions, and ensure that no more
1323 * commands are executed.
1324 */
1325 if (did_throw)
1326 {
1327 void *p = NULL;
1328 char_u *saved_sourcing_name;
1329 int saved_sourcing_lnum;
1330 struct msglist *messages = NULL, *next;
1331
1332 /*
1333 * If the uncaught exception is a user exception, report it as an
1334 * error. If it is an error exception, display the saved error
1335 * message now. For an interrupt exception, do nothing; the
1336 * interrupt message is given elsewhere.
1337 */
1338 switch (current_exception->type)
1339 {
1340 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001341 vim_snprintf((char *)IObuff, IOSIZE,
1342 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 current_exception->value);
1344 p = vim_strsave(IObuff);
1345 break;
1346 case ET_ERROR:
1347 messages = current_exception->messages;
1348 current_exception->messages = NULL;
1349 break;
1350 case ET_INTERRUPT:
1351 break;
1352 default:
1353 p = vim_strsave((char_u *)_(e_internal));
1354 }
1355
1356 saved_sourcing_name = sourcing_name;
1357 saved_sourcing_lnum = sourcing_lnum;
1358 sourcing_name = current_exception->throw_name;
1359 sourcing_lnum = current_exception->throw_lnum;
1360 current_exception->throw_name = NULL;
1361
1362 discard_current_exception(); /* uses IObuff if 'verbose' */
1363 suppress_errthrow = TRUE;
1364 force_abort = TRUE;
1365
1366 if (messages != NULL)
1367 {
1368 do
1369 {
1370 next = messages->next;
1371 emsg(messages->msg);
1372 vim_free(messages->msg);
1373 vim_free(messages);
1374 messages = next;
1375 }
1376 while (messages != NULL);
1377 }
1378 else if (p != NULL)
1379 {
1380 emsg(p);
1381 vim_free(p);
1382 }
1383 vim_free(sourcing_name);
1384 sourcing_name = saved_sourcing_name;
1385 sourcing_lnum = saved_sourcing_lnum;
1386 }
1387
1388 /*
1389 * On an interrupt or an aborting error not converted to an exception,
1390 * disable the conversion of errors to exceptions. (Interrupts are not
1391 * converted any more, here.) This enables also the interrupt message
1392 * when force_abort is set and did_emsg unset in case of an interrupt
1393 * from a finally clause after an error.
1394 */
1395 else if (got_int || (did_emsg && force_abort))
1396 suppress_errthrow = TRUE;
1397 }
1398
1399 /*
1400 * The current cstack will be freed when do_cmdline() returns. An uncaught
1401 * exception will have to be rethrown in the previous cstack. If a function
1402 * has just returned or a script file was just finished and the previous
1403 * cstack belongs to the same function or, respectively, script file, it
1404 * will have to be checked for finally clauses to be executed due to the
1405 * ":return" or ":finish". This is done in do_one_cmd().
1406 */
1407 if (did_throw)
1408 need_rethrow = TRUE;
1409 if ((getline_equal(getline, cookie, getsourceline)
1410 && ex_nesting_level > source_level(real_cookie))
1411 || (getline_equal(getline, cookie, get_func_line)
1412 && ex_nesting_level > func_level(real_cookie) + 1))
1413 {
1414 if (!did_throw)
1415 check_cstack = TRUE;
1416 }
1417 else
1418 {
1419 /* When leaving a function, reduce nesting level. */
1420 if (getline_equal(getline, cookie, get_func_line))
1421 --ex_nesting_level;
1422 /*
1423 * Go to debug mode when returning from a function in which we are
1424 * single-stepping.
1425 */
1426 if ((getline_equal(getline, cookie, getsourceline)
1427 || getline_equal(getline, cookie, get_func_line))
1428 && ex_nesting_level + 1 <= debug_break_level)
1429 do_debug(getline_equal(getline, cookie, getsourceline)
1430 ? (char_u *)_("End of sourced file")
1431 : (char_u *)_("End of function"));
1432 }
1433
1434 /*
1435 * Restore the exception environment (done after returning from the
1436 * debugger).
1437 */
1438 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001439 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
1441 msg_list = saved_msg_list;
1442#endif /* FEAT_EVAL */
1443
1444 /*
1445 * If there was too much output to fit on the command line, ask the user to
1446 * hit return before redrawing the screen. With the ":global" command we do
1447 * this only once after the command is finished.
1448 */
1449 if (did_inc)
1450 {
1451 --RedrawingDisabled;
1452 --no_wait_return;
1453 msg_scroll = FALSE;
1454
1455 /*
1456 * When just finished an ":if"-":else" which was typed, no need to
1457 * wait for hit-return. Also for an error situation.
1458 */
1459 if (retval == FAIL
1460#ifdef FEAT_EVAL
1461 || (did_endif && KeyTyped && !did_emsg)
1462#endif
1463 )
1464 {
1465 need_wait_return = FALSE;
1466 msg_didany = FALSE; /* don't wait when restarting edit */
1467 }
1468 else if (need_wait_return)
1469 {
1470 /*
1471 * The msg_start() above clears msg_didout. The wait_return we do
1472 * here should not overwrite the command that may be shown before
1473 * doing that.
1474 */
1475 msg_didout |= msg_didout_before_start;
1476 wait_return(FALSE);
1477 }
1478 }
1479
1480#ifndef FEAT_EVAL
1481 /*
1482 * Reset if_level, in case a sourced script file contains more ":if" than
1483 * ":endif" (could be ":if x | foo | endif").
1484 */
1485 if_level = 0;
1486#endif
1487
1488 --call_depth;
1489 return retval;
1490}
1491
1492#ifdef FEAT_EVAL
1493/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001494 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 */
1496 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001497get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 int c;
1499 void *cookie;
1500 int indent;
1501{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001502 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 wcmd_T *wp;
1504 char_u *line;
1505
1506 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1507 {
1508 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001509 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001511 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 if (cp->getline == NULL)
1513 line = getcmdline(c, 0L, indent);
1514 else
1515 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001516 if (store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 ++cp->current_line;
1518
1519 return line;
1520 }
1521
1522 KeyTyped = FALSE;
1523 ++cp->current_line;
1524 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1525 sourcing_lnum = wp->lnum;
1526 return vim_strsave(wp->line);
1527}
1528
1529/*
1530 * Store a line in "gap" so that a ":while" loop can execute it again.
1531 */
1532 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001533store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 garray_T *gap;
1535 char_u *line;
1536{
1537 if (ga_grow(gap, 1) == FAIL)
1538 return FAIL;
1539 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1540 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1541 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 return OK;
1543}
1544
1545/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001546 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 */
1548 static void
1549free_cmdlines(gap)
1550 garray_T *gap;
1551{
1552 while (gap->ga_len > 0)
1553 {
1554 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1555 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 }
1557}
1558#endif
1559
1560/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001561 * If "getline" is get_loop_line(), return TRUE if the getline it uses equals
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 * "func". * Otherwise return TRUE when "getline" equals "func".
1563 */
1564/*ARGSUSED*/
1565 int
1566getline_equal(getline, cookie, func)
1567 char_u *(*getline) __ARGS((int, void *, int));
1568 void *cookie; /* argument for getline() */
1569 char_u *(*func) __ARGS((int, void *, int));
1570{
1571#ifdef FEAT_EVAL
1572 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001573 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001575 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 * function that's orignally used to obtain the lines. This may be nested
1577 * several levels. */
1578 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001579 cp = (struct loop_cookie *)cookie;
1580 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 {
1582 gp = cp->getline;
1583 cp = cp->cookie;
1584 }
1585 return gp == func;
1586#else
1587 return getline == func;
1588#endif
1589}
1590
1591#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1592/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001593 * If "getline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 * getline function. Otherwise return "cookie".
1595 */
1596/*ARGSUSED*/
1597 void *
1598getline_cookie(getline, cookie)
1599 char_u *(*getline) __ARGS((int, void *, int));
1600 void *cookie; /* argument for getline() */
1601{
1602# ifdef FEAT_EVAL
1603 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001604 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001606 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 * cookie that's orignally used to obtain the lines. This may be nested
1608 * several levels. */
1609 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001610 cp = (struct loop_cookie *)cookie;
1611 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
1613 gp = cp->getline;
1614 cp = cp->cookie;
1615 }
1616 return cp;
1617# else
1618 return cookie;
1619# endif
1620}
1621#endif
1622
1623/*
1624 * Execute one Ex command.
1625 *
1626 * If 'sourcing' is TRUE, the command will be included in the error message.
1627 *
1628 * 1. skip comment lines and leading space
1629 * 2. handle command modifiers
1630 * 3. parse range
1631 * 4. parse command
1632 * 5. parse arguments
1633 * 6. switch on command name
1634 *
1635 * Note: "getline" can be NULL.
1636 *
1637 * This function may be called recursively!
1638 */
1639#if (_MSC_VER == 1200)
1640/*
Bram Moolenaared203462004-06-16 11:19:22 +00001641 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001643 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644#endif
1645 static char_u *
1646do_one_cmd(cmdlinep, sourcing,
1647#ifdef FEAT_EVAL
1648 cstack,
1649#endif
1650 getline, cookie)
1651 char_u **cmdlinep;
1652 int sourcing;
1653#ifdef FEAT_EVAL
1654 struct condstack *cstack;
1655#endif
1656 char_u *(*getline) __ARGS((int, void *, int));
1657 void *cookie; /* argument for getline() */
1658{
1659 char_u *p;
1660 linenr_T lnum;
1661 long n;
1662 char_u *errormsg = NULL; /* error message */
1663 exarg_T ea; /* Ex command arguments */
1664 long verbose_save = -1;
1665 int save_msg_scroll = 0;
1666 int did_silent = 0;
1667 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001668#ifdef HAVE_SANDBOX
1669 int did_sandbox = FALSE;
1670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 cmdmod_T save_cmdmod;
1672 int ni; /* set when Not Implemented */
1673
1674 vim_memset(&ea, 0, sizeof(ea));
1675 ea.line1 = 1;
1676 ea.line2 = 1;
1677#ifdef FEAT_EVAL
1678 ++ex_nesting_level;
1679#endif
1680
1681 /* when not editing the last file :q has to be typed twice */
1682 if (quitmore
1683#ifdef FEAT_EVAL
1684 /* avoid that a function call in 'statusline' does this */
1685 && !getline_equal(getline, cookie, get_func_line)
1686#endif
1687 )
1688 --quitmore;
1689
1690 /*
1691 * Reset browse, confirm, etc.. They are restored when returning, for
1692 * recursive calls.
1693 */
1694 save_cmdmod = cmdmod;
1695 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1696
1697 /*
1698 * Repeat until no more command modifiers are found.
1699 */
1700 ea.cmd = *cmdlinep;
1701 for (;;)
1702 {
1703/*
1704 * 1. skip comment lines and leading white space and colons
1705 */
1706 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1707 ++ea.cmd;
1708
1709 /* in ex mode, an empty line works like :+ */
1710 if (*ea.cmd == NUL && exmode_active
1711 && (getline_equal(getline, cookie, getexmodeline)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001712 || getline_equal(getline, cookie, getexline))
1713 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 {
1715 ea.cmd = (char_u *)"+";
1716 ex_pressedreturn = TRUE;
1717 }
1718
1719 /* ignore comment and empty lines */
1720 if (*ea.cmd == '"' || *ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001721 {
1722 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
1726/*
1727 * 2. handle command modifiers.
1728 */
1729 p = ea.cmd;
1730 if (VIM_ISDIGIT(*ea.cmd))
1731 p = skipwhite(skipdigits(ea.cmd));
1732 switch (*p)
1733 {
1734 /* When adding an entry, also modify cmd_exists(). */
1735 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1736 break;
1737#ifdef FEAT_WINDOWS
1738 cmdmod.split |= WSP_ABOVE;
1739#endif
1740 continue;
1741
1742 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1743 {
1744#ifdef FEAT_WINDOWS
1745 cmdmod.split |= WSP_BELOW;
1746#endif
1747 continue;
1748 }
1749 if (checkforcmd(&ea.cmd, "browse", 3))
1750 {
1751#ifdef FEAT_BROWSE
1752 cmdmod.browse = TRUE;
1753#endif
1754 continue;
1755 }
1756 if (!checkforcmd(&ea.cmd, "botright", 2))
1757 break;
1758#ifdef FEAT_WINDOWS
1759 cmdmod.split |= WSP_BOT;
1760#endif
1761 continue;
1762
1763 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1764 break;
1765#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1766 cmdmod.confirm = TRUE;
1767#endif
1768 continue;
1769
1770 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1771 {
1772 cmdmod.keepmarks = TRUE;
1773 continue;
1774 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001775 if (checkforcmd(&ea.cmd, "keepalt", 5))
1776 {
1777 cmdmod.keepalt = TRUE;
1778 continue;
1779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1781 break;
1782 cmdmod.keepjumps = TRUE;
1783 continue;
1784
1785 /* ":hide" and ":hide | cmd" are not modifiers */
1786 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1787 || *p == NUL || ends_excmd(*p))
1788 break;
1789 ea.cmd = p;
1790 cmdmod.hide = TRUE;
1791 continue;
1792
1793 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1794 {
1795 cmdmod.lockmarks = TRUE;
1796 continue;
1797 }
1798
1799 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1800 break;
1801#ifdef FEAT_WINDOWS
1802 cmdmod.split |= WSP_ABOVE;
1803#endif
1804 continue;
1805
1806 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1807 break;
1808#ifdef FEAT_WINDOWS
1809 cmdmod.split |= WSP_BELOW;
1810#endif
1811 continue;
1812
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001813 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1814 {
1815#ifdef HAVE_SANDBOX
1816 if (!did_sandbox)
1817 ++sandbox;
1818 did_sandbox = TRUE;
1819#endif
1820 continue;
1821 }
1822 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 break;
1824 ++did_silent;
1825 ++msg_silent;
1826 save_msg_scroll = msg_scroll;
1827 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1828 {
1829 /* ":silent!", but not "silent !cmd" */
1830 ea.cmd = skipwhite(ea.cmd + 1);
1831 ++emsg_silent;
1832 ++did_esilent;
1833 }
1834 continue;
1835
1836 case 't': if (!checkforcmd(&ea.cmd, "topleft", 2))
1837 break;
1838#ifdef FEAT_WINDOWS
1839 cmdmod.split |= WSP_TOP;
1840#endif
1841 continue;
1842
1843 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1844 {
1845#ifdef FEAT_VERTSPLIT
1846 cmdmod.split |= WSP_VERT;
1847#endif
1848 continue;
1849 }
1850 if (!checkforcmd(&p, "verbose", 4))
1851 break;
1852 if (verbose_save < 0)
1853 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001854 if (vim_isdigit(*ea.cmd))
1855 p_verbose = atoi((char *)ea.cmd);
1856 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 p_verbose = 1;
1858 ea.cmd = p;
1859 continue;
1860 }
1861 break;
1862 }
1863
1864#ifdef FEAT_EVAL
1865 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1866 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1867#else
1868 ea.skip = (if_level > 0);
1869#endif
1870
1871#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001872# ifdef FEAT_PROFILE
1873 /* Count this line for profiling if ea.skip is FALSE. */
1874 if (do_profiling && !ea.skip)
1875 {
1876 if (getline_equal(getline, cookie, get_func_line))
1877 func_line_exec(getline_cookie(getline, cookie));
1878 else if (getline_equal(getline, cookie, getsourceline))
1879 script_line_exec();
1880 }
1881#endif
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 /* May go to debug mode. If this happens and the ">quit" debug command is
1884 * used, throw an interrupt exception and skip the next command. */
1885 dbg_check_breakpoint(&ea);
1886 if (!ea.skip && got_int)
1887 {
1888 ea.skip = TRUE;
1889 (void)do_intthrow(cstack);
1890 }
1891#endif
1892
1893/*
1894 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1895 *
1896 * where 'addr' is:
1897 *
1898 * % (entire file)
1899 * $ [+-NUM]
1900 * 'x [+-NUM] (where x denotes a currently defined mark)
1901 * . [+-NUM]
1902 * [+-NUM]..
1903 * NUM
1904 *
1905 * The ea.cmd pointer is updated to point to the first character following the
1906 * range spec. If an initial address is found, but no second, the upper bound
1907 * is equal to the lower.
1908 */
1909
1910 /* repeat for all ',' or ';' separated addresses */
1911 for (;;)
1912 {
1913 ea.line1 = ea.line2;
1914 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1915 ea.cmd = skipwhite(ea.cmd);
1916 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1917 if (ea.cmd == NULL) /* error detected */
1918 goto doend;
1919 if (lnum == MAXLNUM)
1920 {
1921 if (*ea.cmd == '%') /* '%' - all lines */
1922 {
1923 ++ea.cmd;
1924 ea.line1 = 1;
1925 ea.line2 = curbuf->b_ml.ml_line_count;
1926 ++ea.addr_count;
1927 }
1928 /* '*' - visual area */
1929 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1930 {
1931 pos_T *fp;
1932
1933 ++ea.cmd;
1934 if (!ea.skip)
1935 {
1936 fp = getmark('<', FALSE);
1937 if (check_mark(fp) == FAIL)
1938 goto doend;
1939 ea.line1 = fp->lnum;
1940 fp = getmark('>', FALSE);
1941 if (check_mark(fp) == FAIL)
1942 goto doend;
1943 ea.line2 = fp->lnum;
1944 ++ea.addr_count;
1945 }
1946 }
1947 }
1948 else
1949 ea.line2 = lnum;
1950 ea.addr_count++;
1951
1952 if (*ea.cmd == ';')
1953 {
1954 if (!ea.skip)
1955 curwin->w_cursor.lnum = ea.line2;
1956 }
1957 else if (*ea.cmd != ',')
1958 break;
1959 ++ea.cmd;
1960 }
1961
1962 /* One address given: set start and end lines */
1963 if (ea.addr_count == 1)
1964 {
1965 ea.line1 = ea.line2;
1966 /* ... but only implicit: really no address given */
1967 if (lnum == MAXLNUM)
1968 ea.addr_count = 0;
1969 }
1970
1971 /* Don't leave the cursor on an illegal line (caused by ';') */
1972 check_cursor_lnum();
1973
1974/*
1975 * 4. parse command
1976 */
1977
1978 /*
1979 * Skip ':' and any white space
1980 */
1981 ea.cmd = skipwhite(ea.cmd);
1982 while (*ea.cmd == ':')
1983 ea.cmd = skipwhite(ea.cmd + 1);
1984
1985 /*
1986 * If we got a line, but no command, then go to the line.
1987 * If we find a '|' or '\n' we set ea.nextcmd.
1988 */
1989 if (*ea.cmd == NUL || *ea.cmd == '"' ||
1990 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
1991 {
1992 /*
1993 * strange vi behaviour:
1994 * ":3" jumps to line 3
1995 * ":3|..." prints line 3
1996 * ":|" prints current line
1997 */
1998 if (ea.skip) /* skip this if inside :if */
1999 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002000 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 {
2002 ea.cmdidx = CMD_print;
2003 ea.argt = RANGE+COUNT+TRLBAR;
2004 if ((errormsg = invalid_range(&ea)) == NULL)
2005 {
2006 correct_range(&ea);
2007 ex_print(&ea);
2008 }
2009 }
2010 else if (ea.addr_count != 0)
2011 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002012 if (ea.line2 < 0 || ea.line2 > curbuf->b_ml.ml_line_count)
2013 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 else
2015 {
2016 if (ea.line2 == 0)
2017 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 else
2019 curwin->w_cursor.lnum = ea.line2;
2020 beginline(BL_SOL | BL_FIX);
2021 }
2022 }
2023 goto doend;
2024 }
2025
2026 /* Find the command and let "p" point to after it. */
2027 p = find_command(&ea, NULL);
2028
2029#ifdef FEAT_USR_CMDS
2030 if (p == NULL)
2031 {
2032 if (!ea.skip)
2033 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2034 goto doend;
2035 }
2036 /* Check for wrong commands. */
2037 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2038 {
2039 errormsg = uc_fun_cmd();
2040 goto doend;
2041 }
2042#endif
2043 if (ea.cmdidx == CMD_SIZE)
2044 {
2045 if (!ea.skip)
2046 {
2047 STRCPY(IObuff, _("E492: Not an editor command"));
2048 if (!sourcing)
2049 {
2050 STRCAT(IObuff, ": ");
2051 STRNCAT(IObuff, *cmdlinep, 40);
2052 }
2053 errormsg = IObuff;
2054 }
2055 goto doend;
2056 }
2057
2058 ni = (
2059#ifdef FEAT_USR_CMDS
2060 !USER_CMDIDX(ea.cmdidx) &&
2061#endif
2062 cmdnames[ea.cmdidx].cmd_func == ex_ni);
2063
2064#ifndef FEAT_EVAL
2065 /*
2066 * When the expression evaluation is disabled, recognize the ":if" and
2067 * ":endif" commands and ignore everything in between it.
2068 */
2069 if (ea.cmdidx == CMD_if)
2070 ++if_level;
2071 if (if_level)
2072 {
2073 if (ea.cmdidx == CMD_endif)
2074 --if_level;
2075 goto doend;
2076 }
2077
2078#endif
2079
2080 if (*p == '!' && ea.cmdidx != CMD_substitute) /* forced commands */
2081 {
2082 ++p;
2083 ea.forceit = TRUE;
2084 }
2085 else
2086 ea.forceit = FALSE;
2087
2088/*
2089 * 5. parse arguments
2090 */
2091#ifdef FEAT_USR_CMDS
2092 if (!USER_CMDIDX(ea.cmdidx))
2093#endif
2094 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
2095
2096 if (!ea.skip)
2097 {
2098#ifdef HAVE_SANDBOX
2099 if (sandbox != 0 && !(ea.argt & SBOXOK))
2100 {
2101 /* Command not allowed in sandbox. */
2102 errormsg = (char_u *)_(e_sandbox);
2103 goto doend;
2104 }
2105#endif
2106 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2107 {
2108 /* Command not allowed in non-'modifiable' buffer */
2109 errormsg = (char_u *)_(e_modifiable);
2110 goto doend;
2111 }
2112#ifdef FEAT_CMDWIN
2113 if (cmdwin_type != 0 && !(ea.argt & CMDWIN)
2114# ifdef FEAT_USR_CMDS
2115 && !USER_CMDIDX(ea.cmdidx)
2116# endif
2117 )
2118 {
2119 /* Command not allowed in cmdline window. */
2120 errormsg = (char_u *)_(e_cmdwin);
2121 goto doend;
2122 }
2123#endif
2124
2125 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2126 {
2127 /* no range allowed */
2128 errormsg = (char_u *)_(e_norange);
2129 goto doend;
2130 }
2131 }
2132
2133 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2134 {
2135 errormsg = (char_u *)_(e_nobang);
2136 goto doend;
2137 }
2138
2139 /*
2140 * Don't complain about the range if it is not used
2141 * (could happen if line_count is accidentally set to 0).
2142 */
2143 if (!ea.skip && !ni)
2144 {
2145 /*
2146 * If the range is backwards, ask for confirmation and, if given, swap
2147 * ea.line1 & ea.line2 so it's forwards again.
2148 * When global command is busy, don't ask, will fail below.
2149 */
2150 if (!global_busy && ea.line1 > ea.line2)
2151 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002152 if (sourcing || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
2154 errormsg = (char_u *)_("E493: Backwards range given");
2155 goto doend;
2156 }
2157 else
2158 {
2159 int msg_silent_save = msg_silent;
2160
2161 msg_silent = 0;
2162 if (ask_yesno((char_u *)
2163 _("Backwards range given, OK to swap"), FALSE) != 'y')
2164 goto doend;
2165 msg_silent = msg_silent_save;
2166 }
2167 lnum = ea.line1;
2168 ea.line1 = ea.line2;
2169 ea.line2 = lnum;
2170 }
2171 if ((errormsg = invalid_range(&ea)) != NULL)
2172 goto doend;
2173 }
2174
2175 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2176 ea.line2 = 1;
2177
2178 correct_range(&ea);
2179
2180#ifdef FEAT_FOLDING
2181 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2182 {
2183 /* Put the first line at the start of a closed fold, put the last line
2184 * at the end of a closed fold. */
2185 (void)hasFolding(ea.line1, &ea.line1, NULL);
2186 (void)hasFolding(ea.line2, NULL, &ea.line2);
2187 }
2188#endif
2189
2190#ifdef FEAT_QUICKFIX
2191 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002192 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 * option here, so things like % get expanded.
2194 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002195 p = replace_makeprg(&ea, p, cmdlinep);
2196 if (p == NULL)
2197 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#endif
2199
2200 /*
2201 * Skip to start of argument.
2202 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2203 */
2204 if (ea.cmdidx == CMD_bang)
2205 ea.arg = p;
2206 else
2207 ea.arg = skipwhite(p);
2208
2209 /*
2210 * Check for "++opt=val" argument.
2211 * Must be first, allow ":w ++enc=utf8 !cmd"
2212 */
2213 if (ea.argt & ARGOPT)
2214 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2215 if (getargopt(&ea) == FAIL && !ni)
2216 {
2217 errormsg = (char_u *)_(e_invarg);
2218 goto doend;
2219 }
2220
2221 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2222 {
2223 if (*ea.arg == '>') /* append */
2224 {
2225 if (*++ea.arg != '>') /* typed wrong */
2226 {
2227 errormsg = (char_u *)_("E494: Use w or w>>");
2228 goto doend;
2229 }
2230 ea.arg = skipwhite(ea.arg + 1);
2231 ea.append = TRUE;
2232 }
2233 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2234 {
2235 ++ea.arg;
2236 ea.usefilter = TRUE;
2237 }
2238 }
2239
2240 if (ea.cmdidx == CMD_read)
2241 {
2242 if (ea.forceit)
2243 {
2244 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2245 ea.forceit = FALSE;
2246 }
2247 else if (*ea.arg == '!') /* :r !filter */
2248 {
2249 ++ea.arg;
2250 ea.usefilter = TRUE;
2251 }
2252 }
2253
2254 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2255 {
2256 ea.amount = 1;
2257 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2258 {
2259 ++ea.arg;
2260 ++ea.amount;
2261 }
2262 ea.arg = skipwhite(ea.arg);
2263 }
2264
2265 /*
2266 * Check for "+command" argument, before checking for next command.
2267 * Don't do this for ":read !cmd" and ":write !cmd".
2268 */
2269 if ((ea.argt & EDITCMD) && !ea.usefilter)
2270 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2271
2272 /*
2273 * Check for '|' to separate commands and '"' to start comments.
2274 * Don't do this for ":read !cmd" and ":write !cmd".
2275 */
2276 if ((ea.argt & TRLBAR) && !ea.usefilter)
2277 separate_nextcmd(&ea);
2278
2279 /*
2280 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002281 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2282 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002284 else if (ea.cmdidx == CMD_bang
2285 || ea.cmdidx == CMD_global
2286 || ea.cmdidx == CMD_vglobal
2287 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
2289 for (p = ea.arg; *p; ++p)
2290 {
2291 /* Remove one backslash before a newline, so that it's possible to
2292 * pass a newline to the shell and also a newline that is preceded
2293 * with a backslash. This makes it impossible to end a shell
2294 * command in a backslash, but that doesn't appear useful.
2295 * Halving the number of backslashes is incompatible with previous
2296 * versions. */
2297 if (*p == '\\' && p[1] == '\n')
2298 mch_memmove(p, p + 1, STRLEN(p));
2299 else if (*p == '\n')
2300 {
2301 ea.nextcmd = p + 1;
2302 *p = NUL;
2303 break;
2304 }
2305 }
2306 }
2307
2308 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2309 {
2310 ea.line1 = 1;
2311 ea.line2 = curbuf->b_ml.ml_line_count;
2312 }
2313
2314 /* accept numbered register only when no count allowed (:put) */
2315 if ( (ea.argt & REGSTR)
2316 && *ea.arg != NUL
2317#ifdef FEAT_USR_CMDS
2318 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2319 && USER_CMDIDX(ea.cmdidx)))
2320 /* Do not allow register = for user commands */
2321 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2322#else
2323 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2324#endif
2325 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2326 {
2327 ea.regname = *ea.arg++;
2328#ifdef FEAT_EVAL
2329 /* for '=' register: accept the rest of the line as an expression */
2330 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2331 {
2332 set_expr_line(vim_strsave(ea.arg));
2333 ea.arg += STRLEN(ea.arg);
2334 }
2335#endif
2336 ea.arg = skipwhite(ea.arg);
2337 }
2338
2339 /*
2340 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2341 * count, it's a buffer name.
2342 */
2343 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2344 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2345 || vim_iswhite(*p)))
2346 {
2347 n = getdigits(&ea.arg);
2348 ea.arg = skipwhite(ea.arg);
2349 if (n <= 0 && !ni)
2350 {
2351 errormsg = (char_u *)_(e_zerocount);
2352 goto doend;
2353 }
2354 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2355 {
2356 ea.line2 = n;
2357 if (ea.addr_count == 0)
2358 ea.addr_count = 1;
2359 }
2360 else
2361 {
2362 ea.line1 = ea.line2;
2363 ea.line2 += n - 1;
2364 ++ea.addr_count;
2365 /*
2366 * Be vi compatible: no error message for out of range.
2367 */
2368 if (ea.line2 > curbuf->b_ml.ml_line_count)
2369 ea.line2 = curbuf->b_ml.ml_line_count;
2370 }
2371 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002372
2373 /*
2374 * Check for flags: 'l', 'p' and '#'.
2375 */
2376 if (ea.argt & EXFLAGS)
2377 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002379 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
2380 && vim_strchr((char_u *)"|\"", *ea.arg) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 {
2382 errormsg = (char_u *)_(e_trailing);
2383 goto doend;
2384 }
2385
2386 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2387 {
2388 errormsg = (char_u *)_(e_argreq);
2389 goto doend;
2390 }
2391
2392#ifdef FEAT_EVAL
2393 /*
2394 * Skip the command when it's not going to be executed.
2395 * The commands like :if, :endif, etc. always need to be executed.
2396 * Also make an exception for commands that handle a trailing command
2397 * themselves.
2398 */
2399 if (ea.skip)
2400 {
2401 switch (ea.cmdidx)
2402 {
2403 /* commands that need evaluation */
2404 case CMD_while:
2405 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002406 case CMD_for:
2407 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 case CMD_if:
2409 case CMD_elseif:
2410 case CMD_else:
2411 case CMD_endif:
2412 case CMD_try:
2413 case CMD_catch:
2414 case CMD_finally:
2415 case CMD_endtry:
2416 case CMD_function:
2417 break;
2418
2419 /* Commands that handle '|' themselves. Check: A command should
2420 * either have the TRLBAR flag, appear in this list or appear in
2421 * the list at ":help :bar". */
2422 case CMD_aboveleft:
2423 case CMD_and:
2424 case CMD_belowright:
2425 case CMD_botright:
2426 case CMD_browse:
2427 case CMD_call:
2428 case CMD_confirm:
2429 case CMD_delfunction:
2430 case CMD_djump:
2431 case CMD_dlist:
2432 case CMD_dsearch:
2433 case CMD_dsplit:
2434 case CMD_echo:
2435 case CMD_echoerr:
2436 case CMD_echomsg:
2437 case CMD_echon:
2438 case CMD_execute:
2439 case CMD_help:
2440 case CMD_hide:
2441 case CMD_ijump:
2442 case CMD_ilist:
2443 case CMD_isearch:
2444 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002445 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 case CMD_keepjumps:
2447 case CMD_keepmarks:
2448 case CMD_leftabove:
2449 case CMD_let:
2450 case CMD_lockmarks:
2451 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002452 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 case CMD_perl:
2454 case CMD_psearch:
2455 case CMD_python:
2456 case CMD_return:
2457 case CMD_rightbelow:
2458 case CMD_ruby:
2459 case CMD_silent:
2460 case CMD_smagic:
2461 case CMD_snomagic:
2462 case CMD_substitute:
2463 case CMD_syntax:
2464 case CMD_tcl:
2465 case CMD_throw:
2466 case CMD_tilde:
2467 case CMD_topleft:
2468 case CMD_unlet:
2469 case CMD_verbose:
2470 case CMD_vertical:
2471 break;
2472
2473 default: goto doend;
2474 }
2475 }
2476#endif
2477
2478 if (ea.argt & XFILE)
2479 {
2480 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2481 goto doend;
2482 }
2483
2484#ifdef FEAT_LISTCMDS
2485 /*
2486 * Accept buffer name. Cannot be used at the same time with a buffer
2487 * number. Don't do this for a user command.
2488 */
2489 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2490# ifdef FEAT_USR_CMDS
2491 && !USER_CMDIDX(ea.cmdidx)
2492# endif
2493 )
2494 {
2495 /*
2496 * :bdelete, :bwipeout and :bunload take several arguments, separated
2497 * by spaces: find next space (skipping over escaped characters).
2498 * The others take one argument: ignore trailing spaces.
2499 */
2500 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2501 || ea.cmdidx == CMD_bunload)
2502 p = skiptowhite_esc(ea.arg);
2503 else
2504 {
2505 p = ea.arg + STRLEN(ea.arg);
2506 while (p > ea.arg && vim_iswhite(p[-1]))
2507 --p;
2508 }
2509 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2510 if (ea.line2 < 0) /* failed */
2511 goto doend;
2512 ea.addr_count = 1;
2513 ea.arg = skipwhite(p);
2514 }
2515#endif
2516
2517/*
2518 * 6. switch on command name
2519 *
2520 * The "ea" structure holds the arguments that can be used.
2521 */
2522 ea.cmdlinep = cmdlinep;
2523 ea.getline = getline;
2524 ea.cookie = cookie;
2525#ifdef FEAT_EVAL
2526 ea.cstack = cstack;
2527#endif
2528
2529#ifdef FEAT_USR_CMDS
2530 if (USER_CMDIDX(ea.cmdidx))
2531 {
2532 /*
2533 * Execute a user-defined command.
2534 */
2535 do_ucmd(&ea);
2536 }
2537 else
2538#endif
2539 {
2540 /*
2541 * Call the function to execute the command.
2542 */
2543 ea.errmsg = NULL;
2544 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2545 if (ea.errmsg != NULL)
2546 errormsg = (char_u *)_(ea.errmsg);
2547 }
2548
2549#ifdef FEAT_EVAL
2550 /*
2551 * If the command just executed called do_cmdline(), any throw or ":return"
2552 * or ":finish" encountered there must also check the cstack of the still
2553 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2554 * exception, or reanimate a returned function or finished script file and
2555 * return or finish it again.
2556 */
2557 if (need_rethrow)
2558 do_throw(cstack);
2559 else if (check_cstack)
2560 {
2561 if (source_finished(getline, cookie))
2562 do_finish(&ea, TRUE);
2563 else if (getline_equal(getline, cookie, get_func_line)
2564 && current_func_returned())
2565 do_return(&ea, TRUE, FALSE, NULL);
2566 }
2567 need_rethrow = check_cstack = FALSE;
2568#endif
2569
2570doend:
2571 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2572 curwin->w_cursor.lnum = 1;
2573
2574 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2575 {
2576 if (sourcing)
2577 {
2578 if (errormsg != IObuff)
2579 {
2580 STRCPY(IObuff, errormsg);
2581 errormsg = IObuff;
2582 }
2583 STRCAT(errormsg, ": ");
2584 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff));
2585 }
2586 emsg(errormsg);
2587 }
2588#ifdef FEAT_EVAL
2589 do_errthrow(cstack,
2590 (ea.cmdidx != CMD_SIZE
2591# ifdef FEAT_USR_CMDS
2592 && !USER_CMDIDX(ea.cmdidx)
2593# endif
2594 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2595#endif
2596
2597 if (verbose_save >= 0)
2598 p_verbose = verbose_save;
2599
2600 cmdmod = save_cmdmod;
2601
2602 if (did_silent > 0)
2603 {
2604 /* messages could be enabled for a serious error, need to check if the
2605 * counters don't become negative */
2606 msg_silent -= did_silent;
2607 if (msg_silent < 0)
2608 msg_silent = 0;
2609 emsg_silent -= did_esilent;
2610 if (emsg_silent < 0)
2611 emsg_silent = 0;
2612 /* Restore msg_scroll, it's set by file I/O commands, even when no
2613 * message is actually displayed. */
2614 msg_scroll = save_msg_scroll;
2615 }
2616
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002617#ifdef HAVE_SANDBOX
2618 if (did_sandbox)
2619 --sandbox;
2620#endif
2621
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2623 ea.nextcmd = NULL;
2624
2625#ifdef FEAT_EVAL
2626 --ex_nesting_level;
2627#endif
2628
2629 return ea.nextcmd;
2630}
2631#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002632 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633#endif
2634
2635/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002636 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 * If there is a match advance "pp" to the argument and return TRUE.
2638 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002639 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002641 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 char *cmd; /* name of command */
2643 int len; /* required length */
2644{
2645 int i;
2646
2647 for (i = 0; cmd[i] != NUL; ++i)
2648 if (cmd[i] != (*pp)[i])
2649 break;
2650 if (i >= len && !isalpha((*pp)[i]))
2651 {
2652 *pp = skipwhite(*pp + i);
2653 return TRUE;
2654 }
2655 return FALSE;
2656}
2657
2658/*
2659 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002660 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002662 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 * Returns NULL for an ambiguous user command.
2664 */
2665/*ARGSUSED*/
2666 static char_u *
2667find_command(eap, full)
2668 exarg_T *eap;
2669 int *full;
2670{
2671 int len;
2672 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002673 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
2675 /*
2676 * Isolate the command and search for it in the command table.
2677 * Exeptions:
2678 * - the 'k' command can directly be followed by any character.
2679 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2680 * but :sre[wind] is another command, as are :scrip[tnames],
2681 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002682 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 */
2684 p = eap->cmd;
2685 if (*p == 'k')
2686 {
2687 eap->cmdidx = CMD_k;
2688 ++p;
2689 }
2690 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002691 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2692 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 || p[1] == 'g'
2694 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2695 || p[1] == 'I'
2696 || (p[1] == 'r' && p[2] != 'e')))
2697 {
2698 eap->cmdidx = CMD_substitute;
2699 ++p;
2700 }
2701 else
2702 {
2703 while (ASCII_ISALPHA(*p))
2704 ++p;
2705 /* check for non-alpha command */
2706 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2707 ++p;
2708 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002709 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2710 {
2711 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2712 * :delete with the 'l' flag. Same for 'p'. */
2713 for (i = 0; i < len; ++i)
2714 if (eap->cmd[i] != "delete"[i])
2715 break;
2716 if (i == len - 1)
2717 {
2718 --len;
2719 if (p[-1] == 'l')
2720 eap->flags |= EXFLAG_LIST;
2721 else
2722 eap->flags |= EXFLAG_PRINT;
2723 }
2724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
2726 if (ASCII_ISLOWER(*eap->cmd))
2727 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2728 else
2729 eap->cmdidx = cmdidxs[26];
2730
2731 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2732 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2733 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2734 (size_t)len) == 0)
2735 {
2736#ifdef FEAT_EVAL
2737 if (full != NULL
2738 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2739 *full = TRUE;
2740#endif
2741 break;
2742 }
2743
2744#ifdef FEAT_USR_CMDS
2745 /* Look for a user defined command as a last resort */
2746 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2747 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002748 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 while (ASCII_ISALNUM(*p))
2750 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002751 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 }
2753#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002754 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 eap->cmdidx = CMD_SIZE;
2756 }
2757
2758 return p;
2759}
2760
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002761#ifdef FEAT_USR_CMDS
2762/*
2763 * Search for a user command that matches "eap->cmd".
2764 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2765 * Return a pointer to just after the command.
2766 * Return NULL if there is no matching command.
2767 */
2768 static char_u *
2769find_ucmd(eap, p, full, xp, compl)
2770 exarg_T *eap;
2771 char_u *p; /* end of the command (possibly including count) */
2772 int *full; /* set to TRUE for a full match */
2773 expand_T *xp; /* used for completion, NULL otherwise */
2774 int *compl; /* completion flags or NULL */
2775{
2776 int len = (int)(p - eap->cmd);
2777 int j, k, matchlen = 0;
2778 ucmd_T *uc;
2779 int found = FALSE;
2780 int possible = FALSE;
2781 char_u *cp, *np; /* Point into typed cmd and test name */
2782 garray_T *gap;
2783 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2784 only full match global is accepted. */
2785
2786 /*
2787 * Look for buffer-local user commands first, then global ones.
2788 */
2789 gap = &curbuf->b_ucmds;
2790 for (;;)
2791 {
2792 for (j = 0; j < gap->ga_len; ++j)
2793 {
2794 uc = USER_CMD_GA(gap, j);
2795 cp = eap->cmd;
2796 np = uc->uc_name;
2797 k = 0;
2798 while (k < len && *np != NUL && *cp++ == *np++)
2799 k++;
2800 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2801 {
2802 /* If finding a second match, the command is ambiguous. But
2803 * not if a buffer-local command wasn't a full match and a
2804 * global command is a full match. */
2805 if (k == len && found && *np != NUL)
2806 {
2807 if (gap == &ucmds)
2808 {
2809 if (xp != NULL)
2810 xp->xp_context = EXPAND_UNSUCCESSFUL;
2811 return NULL;
2812 }
2813 amb_local = TRUE;
2814 }
2815
2816 if (!found || (k == len && *np == NUL))
2817 {
2818 /* If we matched up to a digit, then there could
2819 * be another command including the digit that we
2820 * should use instead.
2821 */
2822 if (k == len)
2823 found = TRUE;
2824 else
2825 possible = TRUE;
2826
2827 if (gap == &ucmds)
2828 eap->cmdidx = CMD_USER;
2829 else
2830 eap->cmdidx = CMD_USER_BUF;
2831 eap->argt = uc->uc_argt;
2832 eap->useridx = j;
2833
2834# ifdef FEAT_CMDL_COMPL
2835 if (compl != NULL)
2836 *compl = uc->uc_compl;
2837# ifdef FEAT_EVAL
2838 if (xp != NULL)
2839 {
2840 xp->xp_arg = uc->uc_compl_arg;
2841 xp->xp_scriptID = uc->uc_scriptID;
2842 }
2843# endif
2844# endif
2845 /* Do not search for further abbreviations
2846 * if this is an exact match. */
2847 matchlen = k;
2848 if (k == len && *np == NUL)
2849 {
2850 if (full != NULL)
2851 *full = TRUE;
2852 amb_local = FALSE;
2853 break;
2854 }
2855 }
2856 }
2857 }
2858
2859 /* Stop if we found a full match or searched all. */
2860 if (j < gap->ga_len || gap == &ucmds)
2861 break;
2862 gap = &ucmds;
2863 }
2864
2865 /* Only found ambiguous matches. */
2866 if (amb_local)
2867 {
2868 if (xp != NULL)
2869 xp->xp_context = EXPAND_UNSUCCESSFUL;
2870 return NULL;
2871 }
2872
2873 /* The match we found may be followed immediately by a number. Move "p"
2874 * back to point to it. */
2875 if (found || possible)
2876 return p + (matchlen - len);
2877 return p;
2878}
2879#endif
2880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881#if defined(FEAT_EVAL) || defined(PROTO)
2882/*
2883 * Return > 0 if an Ex command "name" exists.
2884 * Return 2 if there is an exact match.
2885 * Return 3 if there is an ambiguous match.
2886 */
2887 int
2888cmd_exists(name)
2889 char_u *name;
2890{
2891 exarg_T ea;
2892 int full = FALSE;
2893 int i;
2894 int j;
2895 static struct cmdmod
2896 {
2897 char *name;
2898 int minlen;
2899 } cmdmods[] = {
2900 {"aboveleft", 3},
2901 {"belowright", 3},
2902 {"botright", 2},
2903 {"browse", 3},
2904 {"confirm", 4},
2905 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002906 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {"keepjumps", 5},
2908 {"keepmarks", 3},
2909 {"leftabove", 5},
2910 {"lockmarks", 3},
2911 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002912 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 {"silent", 3},
2914 {"topleft", 2},
2915 {"verbose", 4},
2916 {"vertical", 4},
2917 };
2918
2919 /* Check command modifiers. */
2920 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2921 {
2922 for (j = 0; name[j] != NUL; ++j)
2923 if (name[j] != cmdmods[i].name[j])
2924 break;
2925 if (name[j] == NUL && j >= cmdmods[i].minlen)
2926 return (cmdmods[i].name[j] == NUL ? 2 : 1);
2927 }
2928
2929 /* Check built-in commands and user defined commands. */
2930 ea.cmd = name;
2931 ea.cmdidx = (cmdidx_T)0;
2932 if (find_command(&ea, &full) == NULL)
2933 return 3;
2934 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
2935}
2936#endif
2937
2938/*
2939 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2940 * we don't need/want deleted. Maybe this could be done better if we didn't
2941 * repeat all this stuff. The only problem is that they may not stay
2942 * perfectly compatible with each other, but then the command line syntax
2943 * probably won't change that much -- webb.
2944 */
2945 char_u *
2946set_one_cmd_context(xp, buff)
2947 expand_T *xp;
2948 char_u *buff; /* buffer for command string */
2949{
2950 char_u *p;
2951 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002952 int len = 0;
2953 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
2955 int compl = EXPAND_NOTHING;
2956#endif
2957#ifdef FEAT_CMDL_COMPL
2958 int delim;
2959#endif
2960 int forceit = FALSE;
2961 int usefilter = FALSE; /* filter instead of file name */
2962
2963 xp->xp_pattern = buff;
2964 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
2965 xp->xp_backslash = XP_BS_NONE;
2966#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
2967 xp->xp_arg = NULL;
2968#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002969 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
2971/*
2972 * 2. skip comment lines and leading space, colons or bars
2973 */
2974 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2975 ;
2976 xp->xp_pattern = cmd;
2977
2978 if (*cmd == NUL)
2979 return NULL;
2980 if (*cmd == '"') /* ignore comment lines */
2981 {
2982 xp->xp_context = EXPAND_NOTHING;
2983 return NULL;
2984 }
2985
2986/*
2987 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
2988 */
2989 cmd = skip_range(cmd, &xp->xp_context);
2990
2991/*
2992 * 4. parse command
2993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 xp->xp_pattern = cmd;
2995 if (*cmd == NUL)
2996 return NULL;
2997 if (*cmd == '"')
2998 {
2999 xp->xp_context = EXPAND_NOTHING;
3000 return NULL;
3001 }
3002
3003 if (*cmd == '|' || *cmd == '\n')
3004 return cmd + 1; /* There's another command */
3005
3006 /*
3007 * Isolate the command and search for it in the command table.
3008 * Exceptions:
3009 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003010 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3012 */
3013 if (*cmd == 'k' && cmd[1] != 'e')
3014 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003015 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 p = cmd + 1;
3017 }
3018 else
3019 {
3020 p = cmd;
3021 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3022 ++p;
3023 /* check for non-alpha command */
3024 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3025 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003026 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003028 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 {
3030 xp->xp_context = EXPAND_UNSUCCESSFUL;
3031 return NULL;
3032 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003033 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3034 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3035 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 break;
3037
3038#ifdef FEAT_USR_CMDS
3039 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3040 {
3041 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3042 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003043 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 }
3045#endif
3046 }
3047
3048 /*
3049 * If the cursor is touching the command, and it ends in an alpha-numeric
3050 * character, complete the command name.
3051 */
3052 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3053 return NULL;
3054
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003055 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 {
3057 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3058 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003059 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 p = cmd + 1;
3061 }
3062#ifdef FEAT_USR_CMDS
3063 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3064 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003065 ea.cmd = cmd;
3066 p = find_ucmd(&ea, p, NULL, xp,
3067# if defined(FEAT_CMDL_COMPL)
3068 &compl
3069# else
3070 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003072 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 }
3074#endif
3075 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003076 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 {
3078 /* Not still touching the command and it was an illegal one */
3079 xp->xp_context = EXPAND_UNSUCCESSFUL;
3080 return NULL;
3081 }
3082
3083 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3084
3085 if (*p == '!') /* forced commands */
3086 {
3087 forceit = TRUE;
3088 ++p;
3089 }
3090
3091/*
3092 * 5. parse arguments
3093 */
3094#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003095 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003097 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
3099 arg = skipwhite(p);
3100
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003101 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
3103 if (*arg == '>') /* append */
3104 {
3105 if (*++arg == '>')
3106 ++arg;
3107 arg = skipwhite(arg);
3108 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003109 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 {
3111 ++arg;
3112 usefilter = TRUE;
3113 }
3114 }
3115
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003116 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 {
3118 usefilter = forceit; /* :r! filter if forced */
3119 if (*arg == '!') /* :r !filter */
3120 {
3121 ++arg;
3122 usefilter = TRUE;
3123 }
3124 }
3125
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003126 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 {
3128 while (*arg == *cmd) /* allow any number of '>' or '<' */
3129 ++arg;
3130 arg = skipwhite(arg);
3131 }
3132
3133 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003134 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 {
3136 /* Check if we're in the +command */
3137 p = arg + 1;
3138 arg = skip_cmd_arg(arg, FALSE);
3139
3140 /* Still touching the command after '+'? */
3141 if (*arg == NUL)
3142 return p;
3143
3144 /* Skip space(s) after +command to get to the real argument */
3145 arg = skipwhite(arg);
3146 }
3147
3148 /*
3149 * Check for '|' to separate commands and '"' to start comments.
3150 * Don't do this for ":read !cmd" and ":write !cmd".
3151 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003152 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 {
3154 p = arg;
3155 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003156 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 p += 2;
3158 while (*p)
3159 {
3160 if (*p == Ctrl_V)
3161 {
3162 if (p[1] != NUL)
3163 ++p;
3164 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003165 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 || *p == '|' || *p == '\n')
3167 {
3168 if (*(p - 1) != '\\')
3169 {
3170 if (*p == '|' || *p == '\n')
3171 return p + 1;
3172 return NULL; /* It's a comment */
3173 }
3174 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003175 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 }
3177 }
3178
3179 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003180 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 vim_strchr((char_u *)"|\"", *arg) == NULL)
3182 return NULL;
3183
3184 /* Find start of last argument (argument just before cursor): */
3185 p = buff + STRLEN(buff);
3186 while (p != arg && *p != ' ' && *p != TAB)
3187 p--;
3188 if (*p == ' ' || *p == TAB)
3189 p++;
3190 xp->xp_pattern = p;
3191
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003192 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 {
3194 int in_quote = FALSE;
3195 char_u *bow = NULL; /* Beginning of word */
3196
3197 /*
3198 * Allow spaces within back-quotes to count as part of the argument
3199 * being expanded.
3200 */
3201 xp->xp_pattern = skipwhite(arg);
3202 for (p = xp->xp_pattern; *p; )
3203 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003204 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 ++p;
3206#ifdef SPACE_IN_FILENAME
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003207 else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208#else
3209 else if (vim_iswhite(*p))
3210#endif
3211 {
3212 p = skipwhite(p);
3213 if (in_quote)
3214 bow = p;
3215 else
3216 xp->xp_pattern = p;
3217 --p;
3218 }
3219 else if (*p == '`')
3220 {
3221 if (!in_quote)
3222 {
3223 xp->xp_pattern = p;
3224 bow = p + 1;
3225 }
3226 in_quote = !in_quote;
3227 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003228 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 }
3230
3231 /*
3232 * If we are still inside the quotes, and we passed a space, just
3233 * expand from there.
3234 */
3235 if (bow != NULL && in_quote)
3236 xp->xp_pattern = bow;
3237 xp->xp_context = EXPAND_FILES;
3238
3239 /* Check for environment variable */
3240 if (*xp->xp_pattern == '$'
3241#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3242 || *xp->xp_pattern == '%'
3243#endif
3244 )
3245 {
3246 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3247 if (!vim_isIDc(*p))
3248 break;
3249 if (*p == NUL)
3250 {
3251 xp->xp_context = EXPAND_ENV_VARS;
3252 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003253#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3254 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003255 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003256 compl = EXPAND_ENV_VARS;
3257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 }
3259 }
3260 }
3261
3262/*
3263 * 6. switch on command name
3264 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003265 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 {
3267 case CMD_cd:
3268 case CMD_chdir:
3269 case CMD_lcd:
3270 case CMD_lchdir:
3271 if (xp->xp_context == EXPAND_FILES)
3272 xp->xp_context = EXPAND_DIRECTORIES;
3273 break;
3274 case CMD_help:
3275 xp->xp_context = EXPAND_HELP;
3276 xp->xp_pattern = arg;
3277 break;
3278
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003279 /* Command modifiers: return the argument.
3280 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003282 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 case CMD_belowright:
3284 case CMD_botright:
3285 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003286 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003288 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 case CMD_folddoclosed:
3290 case CMD_folddoopen:
3291 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003292 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 case CMD_keepjumps:
3294 case CMD_keepmarks:
3295 case CMD_leftabove:
3296 case CMD_lockmarks:
3297 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003298 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 case CMD_silent:
3300 case CMD_topleft:
3301 case CMD_verbose:
3302 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003303 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 return arg;
3305
3306#ifdef FEAT_SEARCH_EXTRA
3307 case CMD_match:
3308 if (*arg == NUL || !ends_excmd(*arg))
3309 {
3310 /* Dummy call to clear variables. */
3311 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3312 xp->xp_context = EXPAND_HIGHLIGHT;
3313 xp->xp_pattern = arg;
3314 arg = skipwhite(skiptowhite(arg));
3315 if (*arg != NUL)
3316 {
3317 xp->xp_context = EXPAND_NOTHING;
3318 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3319 }
3320 }
3321 return find_nextcmd(arg);
3322#endif
3323
3324#ifdef FEAT_CMDL_COMPL
3325/*
3326 * All completion for the +cmdline_compl feature goes here.
3327 */
3328
3329# ifdef FEAT_USR_CMDS
3330 case CMD_command:
3331 /* Check for attributes */
3332 while (*arg == '-')
3333 {
3334 arg++; /* Skip "-" */
3335 p = skiptowhite(arg);
3336 if (*p == NUL)
3337 {
3338 /* Cursor is still in the attribute */
3339 p = vim_strchr(arg, '=');
3340 if (p == NULL)
3341 {
3342 /* No "=", so complete attribute names */
3343 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3344 xp->xp_pattern = arg;
3345 return NULL;
3346 }
3347
3348 /* For the -complete and -nargs attributes, we complete
3349 * their arguments as well.
3350 */
3351 if (STRNICMP(arg, "complete", p - arg) == 0)
3352 {
3353 xp->xp_context = EXPAND_USER_COMPLETE;
3354 xp->xp_pattern = p + 1;
3355 return NULL;
3356 }
3357 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3358 {
3359 xp->xp_context = EXPAND_USER_NARGS;
3360 xp->xp_pattern = p + 1;
3361 return NULL;
3362 }
3363 return NULL;
3364 }
3365 arg = skipwhite(p);
3366 }
3367
3368 /* After the attributes comes the new command name */
3369 p = skiptowhite(arg);
3370 if (*p == NUL)
3371 {
3372 xp->xp_context = EXPAND_USER_COMMANDS;
3373 xp->xp_pattern = arg;
3374 break;
3375 }
3376
3377 /* And finally comes a normal command */
3378 return skipwhite(p);
3379
3380 case CMD_delcommand:
3381 xp->xp_context = EXPAND_USER_COMMANDS;
3382 xp->xp_pattern = arg;
3383 break;
3384# endif
3385
3386 case CMD_global:
3387 case CMD_vglobal:
3388 delim = *arg; /* get the delimiter */
3389 if (delim)
3390 ++arg; /* skip delimiter if there is one */
3391
3392 while (arg[0] != NUL && arg[0] != delim)
3393 {
3394 if (arg[0] == '\\' && arg[1] != NUL)
3395 ++arg;
3396 ++arg;
3397 }
3398 if (arg[0] != NUL)
3399 return arg + 1;
3400 break;
3401 case CMD_and:
3402 case CMD_substitute:
3403 delim = *arg;
3404 if (delim)
3405 {
3406 /* skip "from" part */
3407 ++arg;
3408 arg = skip_regexp(arg, delim, p_magic, NULL);
3409 }
3410 /* skip "to" part */
3411 while (arg[0] != NUL && arg[0] != delim)
3412 {
3413 if (arg[0] == '\\' && arg[1] != NUL)
3414 ++arg;
3415 ++arg;
3416 }
3417 if (arg[0] != NUL) /* skip delimiter */
3418 ++arg;
3419 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3420 ++arg;
3421 if (arg[0] != NUL)
3422 return arg;
3423 break;
3424 case CMD_isearch:
3425 case CMD_dsearch:
3426 case CMD_ilist:
3427 case CMD_dlist:
3428 case CMD_ijump:
3429 case CMD_psearch:
3430 case CMD_djump:
3431 case CMD_isplit:
3432 case CMD_dsplit:
3433 arg = skipwhite(skipdigits(arg)); /* skip count */
3434 if (*arg == '/') /* Match regexp, not just whole words */
3435 {
3436 for (++arg; *arg && *arg != '/'; arg++)
3437 if (*arg == '\\' && arg[1] != NUL)
3438 arg++;
3439 if (*arg)
3440 {
3441 arg = skipwhite(arg + 1);
3442
3443 /* Check for trailing illegal characters */
3444 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3445 xp->xp_context = EXPAND_NOTHING;
3446 else
3447 return arg;
3448 }
3449 }
3450 break;
3451#ifdef FEAT_AUTOCMD
3452 case CMD_autocmd:
3453 return set_context_in_autocmd(xp, arg, FALSE);
3454
3455 case CMD_doautocmd:
3456 return set_context_in_autocmd(xp, arg, TRUE);
3457#endif
3458 case CMD_set:
3459 set_context_in_set_cmd(xp, arg, 0);
3460 break;
3461 case CMD_setglobal:
3462 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3463 break;
3464 case CMD_setlocal:
3465 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3466 break;
3467 case CMD_tag:
3468 case CMD_stag:
3469 case CMD_ptag:
3470 case CMD_tselect:
3471 case CMD_stselect:
3472 case CMD_ptselect:
3473 case CMD_tjump:
3474 case CMD_stjump:
3475 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003476 if (*p_wop != NUL)
3477 xp->xp_context = EXPAND_TAGS_LISTFILES;
3478 else
3479 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 xp->xp_pattern = arg;
3481 break;
3482 case CMD_augroup:
3483 xp->xp_context = EXPAND_AUGROUP;
3484 xp->xp_pattern = arg;
3485 break;
3486#ifdef FEAT_SYN_HL
3487 case CMD_syntax:
3488 set_context_in_syntax_cmd(xp, arg);
3489 break;
3490#endif
3491#ifdef FEAT_EVAL
3492 case CMD_let:
3493 case CMD_if:
3494 case CMD_elseif:
3495 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003496 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 case CMD_echo:
3498 case CMD_echon:
3499 case CMD_execute:
3500 case CMD_echomsg:
3501 case CMD_echoerr:
3502 case CMD_call:
3503 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003504 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 break;
3506
3507 case CMD_unlet:
3508 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3509 arg = xp->xp_pattern + 1;
3510 xp->xp_context = EXPAND_USER_VARS;
3511 xp->xp_pattern = arg;
3512 break;
3513
3514 case CMD_function:
3515 case CMD_delfunction:
3516 xp->xp_context = EXPAND_USER_FUNC;
3517 xp->xp_pattern = arg;
3518 break;
3519
3520 case CMD_echohl:
3521 xp->xp_context = EXPAND_HIGHLIGHT;
3522 xp->xp_pattern = arg;
3523 break;
3524#endif
3525 case CMD_highlight:
3526 set_context_in_highlight_cmd(xp, arg);
3527 break;
3528#ifdef FEAT_LISTCMDS
3529 case CMD_bdelete:
3530 case CMD_bwipeout:
3531 case CMD_bunload:
3532 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3533 arg = xp->xp_pattern + 1;
3534 /*FALLTHROUGH*/
3535 case CMD_buffer:
3536 case CMD_sbuffer:
3537 case CMD_checktime:
3538 xp->xp_context = EXPAND_BUFFERS;
3539 xp->xp_pattern = arg;
3540 break;
3541#endif
3542#ifdef FEAT_USR_CMDS
3543 case CMD_USER:
3544 case CMD_USER_BUF:
3545 if (compl != EXPAND_NOTHING)
3546 {
3547 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003548 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
3550# ifdef FEAT_MENU
3551 if (compl == EXPAND_MENUS)
3552 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3553# endif
3554 if (compl == EXPAND_COMMANDS)
3555 return arg;
3556 if (compl == EXPAND_MAPPINGS)
3557 return set_context_in_map_cmd(xp, (char_u *)"map",
3558 arg, forceit, FALSE, FALSE, CMD_map);
3559 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3560 arg = xp->xp_pattern + 1;
3561 xp->xp_pattern = arg;
3562 }
3563 xp->xp_context = compl;
3564 }
3565 break;
3566#endif
3567 case CMD_map: case CMD_noremap:
3568 case CMD_nmap: case CMD_nnoremap:
3569 case CMD_vmap: case CMD_vnoremap:
3570 case CMD_omap: case CMD_onoremap:
3571 case CMD_imap: case CMD_inoremap:
3572 case CMD_cmap: case CMD_cnoremap:
3573 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003574 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 case CMD_unmap:
3576 case CMD_nunmap:
3577 case CMD_vunmap:
3578 case CMD_ounmap:
3579 case CMD_iunmap:
3580 case CMD_cunmap:
3581 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003582 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 case CMD_abbreviate: case CMD_noreabbrev:
3584 case CMD_cabbrev: case CMD_cnoreabbrev:
3585 case CMD_iabbrev: case CMD_inoreabbrev:
3586 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003587 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 case CMD_unabbreviate:
3589 case CMD_cunabbrev:
3590 case CMD_iunabbrev:
3591 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003592 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593#ifdef FEAT_MENU
3594 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3595 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3596 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3597 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3598 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3599 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3600 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3601 case CMD_tmenu: case CMD_tunmenu:
3602 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3603 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3604#endif
3605
3606 case CMD_colorscheme:
3607 xp->xp_context = EXPAND_COLORS;
3608 xp->xp_pattern = arg;
3609 break;
3610
3611 case CMD_compiler:
3612 xp->xp_context = EXPAND_COMPILER;
3613 xp->xp_pattern = arg;
3614 break;
3615
3616#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3617 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3618 case CMD_language:
3619 if (*skiptowhite(arg) == NUL)
3620 {
3621 xp->xp_context = EXPAND_LANGUAGE;
3622 xp->xp_pattern = arg;
3623 }
3624 else
3625 xp->xp_context = EXPAND_NOTHING;
3626 break;
3627#endif
3628
3629#endif /* FEAT_CMDL_COMPL */
3630
3631 default:
3632 break;
3633 }
3634 return NULL;
3635}
3636
3637/*
3638 * skip a range specifier of the form: addr [,addr] [;addr] ..
3639 *
3640 * Backslashed delimiters after / or ? will be skipped, and commands will
3641 * not be expanded between /'s and ?'s or after "'".
3642 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003643 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 * Returns the "cmd" pointer advanced to beyond the range.
3645 */
3646 char_u *
3647skip_range(cmd, ctx)
3648 char_u *cmd;
3649 int *ctx; /* pointer to xp_context or NULL */
3650{
3651 int delim;
3652
Bram Moolenaardf177f62005-02-22 08:39:57 +00003653 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
3655 if (*cmd == '\'')
3656 {
3657 if (*++cmd == NUL && ctx != NULL)
3658 *ctx = EXPAND_NOTHING;
3659 }
3660 else if (*cmd == '/' || *cmd == '?')
3661 {
3662 delim = *cmd++;
3663 while (*cmd != NUL && *cmd != delim)
3664 if (*cmd++ == '\\' && *cmd != NUL)
3665 ++cmd;
3666 if (*cmd == NUL && ctx != NULL)
3667 *ctx = EXPAND_NOTHING;
3668 }
3669 if (*cmd != NUL)
3670 ++cmd;
3671 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003672
3673 /* Skip ":" and white space. */
3674 while (*cmd == ':')
3675 cmd = skipwhite(cmd + 1);
3676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 return cmd;
3678}
3679
3680/*
3681 * get a single EX address
3682 *
3683 * Set ptr to the next character after the part that was interpreted.
3684 * Set ptr to NULL when an error is encountered.
3685 *
3686 * Return MAXLNUM when no Ex address was found.
3687 */
3688 static linenr_T
3689get_address(ptr, skip, to_other_file)
3690 char_u **ptr;
3691 int skip; /* only skip the address, don't use it */
3692 int to_other_file; /* flag: may jump to other file */
3693{
3694 int c;
3695 int i;
3696 long n;
3697 char_u *cmd;
3698 pos_T pos;
3699 pos_T *fp;
3700 linenr_T lnum;
3701
3702 cmd = skipwhite(*ptr);
3703 lnum = MAXLNUM;
3704 do
3705 {
3706 switch (*cmd)
3707 {
3708 case '.': /* '.' - Cursor position */
3709 ++cmd;
3710 lnum = curwin->w_cursor.lnum;
3711 break;
3712
3713 case '$': /* '$' - last line */
3714 ++cmd;
3715 lnum = curbuf->b_ml.ml_line_count;
3716 break;
3717
3718 case '\'': /* ''' - mark */
3719 if (*++cmd == NUL)
3720 {
3721 cmd = NULL;
3722 goto error;
3723 }
3724 if (skip)
3725 ++cmd;
3726 else
3727 {
3728 /* Only accept a mark in another file when it is
3729 * used by itself: ":'M". */
3730 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3731 ++cmd;
3732 if (fp == (pos_T *)-1)
3733 /* Jumped to another file. */
3734 lnum = curwin->w_cursor.lnum;
3735 else
3736 {
3737 if (check_mark(fp) == FAIL)
3738 {
3739 cmd = NULL;
3740 goto error;
3741 }
3742 lnum = fp->lnum;
3743 }
3744 }
3745 break;
3746
3747 case '/':
3748 case '?': /* '/' or '?' - search */
3749 c = *cmd++;
3750 if (skip) /* skip "/pat/" */
3751 {
3752 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3753 if (*cmd == c)
3754 ++cmd;
3755 }
3756 else
3757 {
3758 pos = curwin->w_cursor; /* save curwin->w_cursor */
3759 /*
3760 * When '/' or '?' follows another address, start
3761 * from there.
3762 */
3763 if (lnum != MAXLNUM)
3764 curwin->w_cursor.lnum = lnum;
3765 /*
3766 * Start a forward search at the end of the line.
3767 * Start a backward search at the start of the line.
3768 * This makes sure we never match in the current
3769 * line, and can match anywhere in the
3770 * next/previous line.
3771 */
3772 if (c == '/')
3773 curwin->w_cursor.col = MAXCOL;
3774 else
3775 curwin->w_cursor.col = 0;
3776 searchcmdlen = 0;
3777 if (!do_search(NULL, c, cmd, 1L,
3778 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3779 {
3780 curwin->w_cursor = pos;
3781 cmd = NULL;
3782 goto error;
3783 }
3784 lnum = curwin->w_cursor.lnum;
3785 curwin->w_cursor = pos;
3786 /* adjust command string pointer */
3787 cmd += searchcmdlen;
3788 }
3789 break;
3790
3791 case '\\': /* "\?", "\/" or "\&", repeat search */
3792 ++cmd;
3793 if (*cmd == '&')
3794 i = RE_SUBST;
3795 else if (*cmd == '?' || *cmd == '/')
3796 i = RE_SEARCH;
3797 else
3798 {
3799 EMSG(_(e_backslash));
3800 cmd = NULL;
3801 goto error;
3802 }
3803
3804 if (!skip)
3805 {
3806 /*
3807 * When search follows another address, start from
3808 * there.
3809 */
3810 if (lnum != MAXLNUM)
3811 pos.lnum = lnum;
3812 else
3813 pos.lnum = curwin->w_cursor.lnum;
3814
3815 /*
3816 * Start the search just like for the above
3817 * do_search().
3818 */
3819 if (*cmd != '?')
3820 pos.col = MAXCOL;
3821 else
3822 pos.col = 0;
3823 if (searchit(curwin, curbuf, &pos,
3824 *cmd == '?' ? BACKWARD : FORWARD,
3825 (char_u *)"", 1L,
3826 SEARCH_MSG + SEARCH_START, i) != FAIL)
3827 lnum = pos.lnum;
3828 else
3829 {
3830 cmd = NULL;
3831 goto error;
3832 }
3833 }
3834 ++cmd;
3835 break;
3836
3837 default:
3838 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3839 lnum = getdigits(&cmd);
3840 }
3841
3842 for (;;)
3843 {
3844 cmd = skipwhite(cmd);
3845 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3846 break;
3847
3848 if (lnum == MAXLNUM)
3849 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3850 if (VIM_ISDIGIT(*cmd))
3851 i = '+'; /* "number" is same as "+number" */
3852 else
3853 i = *cmd++;
3854 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3855 n = 1;
3856 else
3857 n = getdigits(&cmd);
3858 if (i == '-')
3859 lnum -= n;
3860 else
3861 lnum += n;
3862 }
3863 } while (*cmd == '/' || *cmd == '?');
3864
3865error:
3866 *ptr = cmd;
3867 return lnum;
3868}
3869
3870/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003871 * Get flags from an Ex command argument.
3872 */
3873 static void
3874get_flags(eap)
3875 exarg_T *eap;
3876{
3877 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3878 {
3879 if (*eap->arg == 'l')
3880 eap->flags |= EXFLAG_LIST;
3881 else if (*eap->arg == 'p')
3882 eap->flags |= EXFLAG_PRINT;
3883 else
3884 eap->flags |= EXFLAG_NR;
3885 eap->arg = skipwhite(eap->arg + 1);
3886 }
3887}
3888
3889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 * Function called for command which is Not Implemented. NI!
3891 */
3892 void
3893ex_ni(eap)
3894 exarg_T *eap;
3895{
3896 if (!eap->skip)
3897 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3898}
3899
3900#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003901 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902/*
3903 * Function called for script command which is Not Implemented. NI!
3904 * Skips over ":perl <<EOF" constructs.
3905 */
3906 static void
3907ex_script_ni(eap)
3908 exarg_T *eap;
3909{
3910 if (!eap->skip)
3911 ex_ni(eap);
3912 else
3913 vim_free(script_get(eap, eap->arg));
3914}
3915#endif
3916
3917/*
3918 * Check range in Ex command for validity.
3919 * Return NULL when valid, error message when invalid.
3920 */
3921 static char_u *
3922invalid_range(eap)
3923 exarg_T *eap;
3924{
3925 if ( eap->line1 < 0
3926 || eap->line2 < 0
3927 || eap->line1 > eap->line2
3928 || ((eap->argt & RANGE)
3929 && !(eap->argt & NOTADR)
3930 && eap->line2 > curbuf->b_ml.ml_line_count
3931#ifdef FEAT_DIFF
3932 + (eap->cmdidx == CMD_diffget)
3933#endif
3934 ))
3935 return (char_u *)_(e_invrange);
3936 return NULL;
3937}
3938
3939/*
3940 * Correct the range for zero line number, if required.
3941 */
3942 static void
3943correct_range(eap)
3944 exarg_T *eap;
3945{
3946 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
3947 {
3948 if (eap->line1 == 0)
3949 eap->line1 = 1;
3950 if (eap->line2 == 0)
3951 eap->line2 = 1;
3952 }
3953}
3954
Bram Moolenaar748bf032005-02-02 23:04:36 +00003955#ifdef FEAT_QUICKFIX
3956static char_u *skip_grep_pat __ARGS((exarg_T *eap));
3957
3958/*
3959 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
3960 * pattern. Otherwise return eap->arg.
3961 */
3962 static char_u *
3963skip_grep_pat(eap)
3964 exarg_T *eap;
3965{
3966 char_u *p = eap->arg;
3967
3968 if (*p != NUL && (eap->cmdidx == CMD_vimgrep
3969 || eap->cmdidx == CMD_vimgrepadd || grep_internal(eap->cmdidx)))
3970 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003971 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003972 if (p == NULL)
3973 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003974 }
3975 return p;
3976}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003977
3978/*
3979 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
3980 * in the command line, so that things like % get expanded.
3981 */
3982 static char_u *
3983replace_makeprg(eap, p, cmdlinep)
3984 exarg_T *eap;
3985 char_u *p;
3986 char_u **cmdlinep;
3987{
3988 char_u *new_cmdline;
3989 char_u *program;
3990 char_u *pos;
3991 char_u *ptr;
3992 int len;
3993 int i;
3994
3995 /*
3996 * Don't do it when ":vimgrep" is used for ":grep".
3997 */
3998 if ((eap->cmdidx == CMD_make
3999 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_grepadd)
4000 && !grep_internal(eap->cmdidx))
4001 {
4002 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_grepadd)
4003 {
4004 if (*curbuf->b_p_gp == NUL)
4005 program = p_gp;
4006 else
4007 program = curbuf->b_p_gp;
4008 }
4009 else
4010 {
4011 if (*curbuf->b_p_mp == NUL)
4012 program = p_mp;
4013 else
4014 program = curbuf->b_p_mp;
4015 }
4016
4017 p = skipwhite(p);
4018
4019 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4020 {
4021 /* replace $* by given arguments */
4022 i = 1;
4023 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4024 ++i;
4025 len = (int)STRLEN(p);
4026 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4027 if (new_cmdline == NULL)
4028 return NULL; /* out of memory */
4029 ptr = new_cmdline;
4030 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4031 {
4032 i = (int)(pos - program);
4033 STRNCPY(ptr, program, i);
4034 STRCPY(ptr += i, p);
4035 ptr += len;
4036 program = pos + 2;
4037 }
4038 STRCPY(ptr, program);
4039 }
4040 else
4041 {
4042 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4043 if (new_cmdline == NULL)
4044 return NULL; /* out of memory */
4045 STRCPY(new_cmdline, program);
4046 STRCAT(new_cmdline, " ");
4047 STRCAT(new_cmdline, p);
4048 }
4049 msg_make(p);
4050
4051 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4052 vim_free(*cmdlinep);
4053 *cmdlinep = new_cmdline;
4054 p = new_cmdline;
4055 }
4056 return p;
4057}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004058#endif
4059
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060/*
4061 * Expand file name in Ex command argument.
4062 * Return FAIL for failure, OK otherwise.
4063 */
4064 int
4065expand_filename(eap, cmdlinep, errormsgp)
4066 exarg_T *eap;
4067 char_u **cmdlinep;
4068 char_u **errormsgp;
4069{
4070 int has_wildcards; /* need to expand wildcards */
4071 char_u *repl;
4072 int srclen;
4073 char_u *p;
4074 int n;
4075
Bram Moolenaar748bf032005-02-02 23:04:36 +00004076#ifdef FEAT_QUICKFIX
4077 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4078 p = skip_grep_pat(eap);
4079#else
4080 p = eap->arg;
4081#endif
4082
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 /*
4084 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4085 * the file name contains a wildcard it should not cause expanding.
4086 * (it will be expanded anyway if there is a wildcard before replacing).
4087 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004088 has_wildcards = mch_has_wildcard(p);
4089 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004091#ifdef FEAT_EVAL
4092 /* Skip over `=expr`, wildcards in it are not expanded. */
4093 if (p[0] == '`' && p[1] == '=')
4094 {
4095 p += 2;
4096 (void)skip_expr(&p);
4097 if (*p == '`')
4098 ++p;
4099 continue;
4100 }
4101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 /*
4103 * Quick check if this cannot be the start of a special string.
4104 * Also removes backslash before '%', '#' and '<'.
4105 */
4106 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4107 {
4108 ++p;
4109 continue;
4110 }
4111
4112 /*
4113 * Try to find a match at this position.
4114 */
4115 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4116 if (*errormsgp != NULL) /* error detected */
4117 return FAIL;
4118 if (repl == NULL) /* no match found */
4119 {
4120 p += srclen;
4121 continue;
4122 }
4123
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004124 /* Wildcards won't be expanded below, the replacement is taken
4125 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4126 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4127 {
4128 char_u *l = repl;
4129
4130 repl = expand_env_save(repl);
4131 vim_free(l);
4132 }
4133
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 /* Need to escape white space et al. with a backslash. Don't do this
4135 * for shell commands (may have to use quotes instead). Don't do this
4136 * for non-unix systems when there is a single argument (spaces don't
4137 * separate arguments then). */
4138 if (!eap->usefilter
4139 && eap->cmdidx != CMD_bang
4140 && eap->cmdidx != CMD_make
4141 && eap->cmdidx != CMD_grep
4142 && eap->cmdidx != CMD_grepadd
4143#ifndef UNIX
4144 && !(eap->argt & NOSPC)
4145#endif
4146 )
4147 {
4148 char_u *l;
4149#ifdef BACKSLASH_IN_FILENAME
4150 /* Don't escape a backslash here, because rem_backslash() doesn't
4151 * remove it later. */
4152 static char_u *nobslash = (char_u *)" \t\"|";
4153# define ESCAPE_CHARS nobslash
4154#else
4155# define ESCAPE_CHARS escape_chars
4156#endif
4157
4158 for (l = repl; *l; ++l)
4159 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4160 {
4161 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4162 if (l != NULL)
4163 {
4164 vim_free(repl);
4165 repl = l;
4166 }
4167 break;
4168 }
4169 }
4170
4171 /* For a shell command a '!' must be escaped. */
4172 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaarea424162005-06-16 21:51:00 +00004173 && vim_strpbrk(repl, (char_u *)"!&;()") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {
4175 char_u *l;
4176
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004177 l = vim_strsave_escaped(repl, (char_u *)"!&;()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 if (l != NULL)
4179 {
4180 vim_free(repl);
4181 repl = l;
4182 /* For a sh-like shell escape it another time. */
4183 if (strstr((char *)p_sh, "sh") != NULL)
4184 {
4185 l = vim_strsave_escaped(repl, (char_u *)"!");
4186 if (l != NULL)
4187 {
4188 vim_free(repl);
4189 repl = l;
4190 }
4191 }
4192 }
4193 }
4194
4195 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4196 vim_free(repl);
4197 if (p == NULL)
4198 return FAIL;
4199 }
4200
4201 /*
4202 * One file argument: Expand wildcards.
4203 * Don't do this with ":r !command" or ":w !command".
4204 */
4205 if ((eap->argt & NOSPC) && !eap->usefilter)
4206 {
4207 /*
4208 * May do this twice:
4209 * 1. Replace environment variables.
4210 * 2. Replace any other wildcards, remove backslashes.
4211 */
4212 for (n = 1; n <= 2; ++n)
4213 {
4214 if (n == 2)
4215 {
4216#ifdef UNIX
4217 /*
4218 * Only for Unix we check for more than one file name.
4219 * For other systems spaces are considered to be part
4220 * of the file name.
4221 * Only check here if there is no wildcard, otherwise
4222 * ExpandOne() will check for errors. This allows
4223 * ":e `ls ve*.c`" on Unix.
4224 */
4225 if (!has_wildcards)
4226 for (p = eap->arg; *p; ++p)
4227 {
4228 /* skip escaped characters */
4229 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4230 ++p;
4231 else if (vim_iswhite(*p))
4232 {
4233 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4234 return FAIL;
4235 }
4236 }
4237#endif
4238
4239 /*
4240 * Halve the number of backslashes (this is Vi compatible).
4241 * For Unix and OS/2, when wildcards are expanded, this is
4242 * done by ExpandOne() below.
4243 */
4244#if defined(UNIX) || defined(OS2)
4245 if (!has_wildcards)
4246#endif
4247 backslash_halve(eap->arg);
4248#ifdef MACOS_CLASSIC
4249 /*
4250 * translate unix-like path components
4251 */
4252 slash_n_colon_adjust(eap->arg);
4253#endif
4254 }
4255
4256 if (has_wildcards)
4257 {
4258 if (n == 1)
4259 {
4260 /*
4261 * First loop: May expand environment variables. This
4262 * can be done much faster with expand_env() than with
4263 * something else (e.g., calling a shell).
4264 * After expanding environment variables, check again
4265 * if there are still wildcards present.
4266 */
4267 if (vim_strchr(eap->arg, '$') != NULL
4268 || vim_strchr(eap->arg, '~') != NULL)
4269 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004270 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4271 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 has_wildcards = mch_has_wildcard(NameBuff);
4273 p = NameBuff;
4274 }
4275 else
4276 p = NULL;
4277 }
4278 else /* n == 2 */
4279 {
4280 expand_T xpc;
4281
4282 ExpandInit(&xpc);
4283 xpc.xp_context = EXPAND_FILES;
4284 p = ExpandOne(&xpc, eap->arg, NULL,
4285 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4286 WILD_EXPAND_FREE);
4287 ExpandCleanup(&xpc);
4288 if (p == NULL)
4289 return FAIL;
4290 }
4291 if (p != NULL)
4292 {
4293 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4294 p, cmdlinep);
4295 if (n == 2) /* p came from ExpandOne() */
4296 vim_free(p);
4297 }
4298 }
4299 }
4300 }
4301 return OK;
4302}
4303
4304/*
4305 * Replace part of the command line, keeping eap->cmd, eap->arg and
4306 * eap->nextcmd correct.
4307 * "src" points to the part that is to be replaced, of length "srclen".
4308 * "repl" is the replacement string.
4309 * Returns a pointer to the character after the replaced string.
4310 * Returns NULL for failure.
4311 */
4312 static char_u *
4313repl_cmdline(eap, src, srclen, repl, cmdlinep)
4314 exarg_T *eap;
4315 char_u *src;
4316 int srclen;
4317 char_u *repl;
4318 char_u **cmdlinep;
4319{
4320 int len;
4321 int i;
4322 char_u *new_cmdline;
4323
4324 /*
4325 * The new command line is build in new_cmdline[].
4326 * First allocate it.
4327 * Careful: a "+cmd" argument may have been NUL terminated.
4328 */
4329 len = (int)STRLEN(repl);
4330 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
4331 if (eap->nextcmd)
4332 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4333 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4334 return NULL; /* out of memory! */
4335
4336 /*
4337 * Copy the stuff before the expanded part.
4338 * Copy the expanded stuff.
4339 * Copy what came after the expanded part.
4340 * Copy the next commands, if there are any.
4341 */
4342 i = (int)(src - *cmdlinep); /* length of part before match */
4343 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004344
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 mch_memmove(new_cmdline + i, repl, (size_t)len);
4346 i += len; /* remember the end of the string */
4347 STRCPY(new_cmdline + i, src + srclen);
4348 src = new_cmdline + i; /* remember where to continue */
4349
4350 if (eap->nextcmd) /* append next command */
4351 {
4352 i = (int)STRLEN(new_cmdline) + 1;
4353 STRCPY(new_cmdline + i, eap->nextcmd);
4354 eap->nextcmd = new_cmdline + i;
4355 }
4356 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4357 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4358 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4359 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4360 vim_free(*cmdlinep);
4361 *cmdlinep = new_cmdline;
4362
4363 return src;
4364}
4365
4366/*
4367 * Check for '|' to separate commands and '"' to start comments.
4368 */
4369 void
4370separate_nextcmd(eap)
4371 exarg_T *eap;
4372{
4373 char_u *p;
4374
Bram Moolenaar86b68352004-12-27 21:59:20 +00004375#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004376 p = skip_grep_pat(eap);
4377#else
4378 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004379#endif
4380
4381 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 {
4383 if (*p == Ctrl_V)
4384 {
4385 if (eap->argt & (USECTRLV | XFILE))
4386 ++p; /* skip CTRL-V and next char */
4387 else
4388 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4389 if (*p == NUL) /* stop at NUL after CTRL-V */
4390 break;
4391 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004392
4393#ifdef FEAT_EVAL
4394 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004395 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004396 {
4397 p += 2;
4398 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004399 }
4400#endif
4401
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 /* Check for '"': start of comment or '|': next command */
4403 /* :@" and :*" do not start a comment!
4404 * :redir @" doesn't either. */
4405 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4406 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4407 || p != eap->arg)
4408 && (eap->cmdidx != CMD_redir
4409 || p != eap->arg + 1 || p[-1] != '@'))
4410 || *p == '|' || *p == '\n')
4411 {
4412 /*
4413 * We remove the '\' before the '|', unless USECTRLV is used
4414 * AND 'b' is present in 'cpoptions'.
4415 */
4416 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4417 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4418 {
4419 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4420 --p;
4421 }
4422 else
4423 {
4424 eap->nextcmd = check_nextcmd(p);
4425 *p = NUL;
4426 break;
4427 }
4428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004430
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4432 del_trailing_spaces(eap->arg);
4433}
4434
4435/*
4436 * get + command from ex argument
4437 */
4438 static char_u *
4439getargcmd(argp)
4440 char_u **argp;
4441{
4442 char_u *arg = *argp;
4443 char_u *command = NULL;
4444
4445 if (*arg == '+') /* +[command] */
4446 {
4447 ++arg;
4448 if (vim_isspace(*arg))
4449 command = dollar_command;
4450 else
4451 {
4452 command = arg;
4453 arg = skip_cmd_arg(command, TRUE);
4454 if (*arg != NUL)
4455 *arg++ = NUL; /* terminate command with NUL */
4456 }
4457
4458 arg = skipwhite(arg); /* skip over spaces */
4459 *argp = arg;
4460 }
4461 return command;
4462}
4463
4464/*
4465 * Find end of "+command" argument. Skip over "\ " and "\\".
4466 */
4467 static char_u *
4468skip_cmd_arg(p, rembs)
4469 char_u *p;
4470 int rembs; /* TRUE to halve the number of backslashes */
4471{
4472 while (*p && !vim_isspace(*p))
4473 {
4474 if (*p == '\\' && p[1] != NUL)
4475 {
4476 if (rembs)
4477 mch_memmove(p, p + 1, STRLEN(p));
4478 else
4479 ++p;
4480 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004481 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483 return p;
4484}
4485
4486/*
4487 * Get "++opt=arg" argument.
4488 * Return FAIL or OK.
4489 */
4490 static int
4491getargopt(eap)
4492 exarg_T *eap;
4493{
4494 char_u *arg = eap->arg + 2;
4495 int *pp = NULL;
4496#ifdef FEAT_MBYTE
4497 char_u *p;
4498#endif
4499
4500 /* ":edit ++[no]bin[ary] file" */
4501 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4502 {
4503 if (*arg == 'n')
4504 {
4505 arg += 2;
4506 eap->force_bin = FORCE_NOBIN;
4507 }
4508 else
4509 eap->force_bin = FORCE_BIN;
4510 if (!checkforcmd(&arg, "binary", 3))
4511 return FAIL;
4512 eap->arg = skipwhite(arg);
4513 return OK;
4514 }
4515
4516 if (STRNCMP(arg, "ff", 2) == 0)
4517 {
4518 arg += 2;
4519 pp = &eap->force_ff;
4520 }
4521 else if (STRNCMP(arg, "fileformat", 10) == 0)
4522 {
4523 arg += 10;
4524 pp = &eap->force_ff;
4525 }
4526#ifdef FEAT_MBYTE
4527 else if (STRNCMP(arg, "enc", 3) == 0)
4528 {
4529 arg += 3;
4530 pp = &eap->force_enc;
4531 }
4532 else if (STRNCMP(arg, "encoding", 8) == 0)
4533 {
4534 arg += 8;
4535 pp = &eap->force_enc;
4536 }
4537#endif
4538
4539 if (pp == NULL || *arg != '=')
4540 return FAIL;
4541
4542 ++arg;
4543 *pp = (int)(arg - eap->cmd);
4544 arg = skip_cmd_arg(arg, FALSE);
4545 eap->arg = skipwhite(arg);
4546 *arg = NUL;
4547
4548#ifdef FEAT_MBYTE
4549 if (pp == &eap->force_ff)
4550 {
4551#endif
4552 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4553 return FAIL;
4554#ifdef FEAT_MBYTE
4555 }
4556 else
4557 {
4558 /* Make 'fileencoding' lower case. */
4559 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4560 *p = TOLOWER_ASC(*p);
4561 }
4562#endif
4563
4564 return OK;
4565}
4566
4567/*
4568 * ":abbreviate" and friends.
4569 */
4570 static void
4571ex_abbreviate(eap)
4572 exarg_T *eap;
4573{
4574 do_exmap(eap, TRUE); /* almost the same as mapping */
4575}
4576
4577/*
4578 * ":map" and friends.
4579 */
4580 static void
4581ex_map(eap)
4582 exarg_T *eap;
4583{
4584 /*
4585 * If we are sourcing .exrc or .vimrc in current directory we
4586 * print the mappings for security reasons.
4587 */
4588 if (secure)
4589 {
4590 secure = 2;
4591 msg_outtrans(eap->cmd);
4592 msg_putchar('\n');
4593 }
4594 do_exmap(eap, FALSE);
4595}
4596
4597/*
4598 * ":unmap" and friends.
4599 */
4600 static void
4601ex_unmap(eap)
4602 exarg_T *eap;
4603{
4604 do_exmap(eap, FALSE);
4605}
4606
4607/*
4608 * ":mapclear" and friends.
4609 */
4610 static void
4611ex_mapclear(eap)
4612 exarg_T *eap;
4613{
4614 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4615}
4616
4617/*
4618 * ":abclear" and friends.
4619 */
4620 static void
4621ex_abclear(eap)
4622 exarg_T *eap;
4623{
4624 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4625}
4626
4627#ifdef FEAT_AUTOCMD
4628 static void
4629ex_autocmd(eap)
4630 exarg_T *eap;
4631{
4632 /*
4633 * Disallow auto commands from .exrc and .vimrc in current
4634 * directory for security reasons.
4635 */
4636 if (secure)
4637 {
4638 secure = 2;
4639 eap->errmsg = e_curdir;
4640 }
4641 else if (eap->cmdidx == CMD_autocmd)
4642 do_autocmd(eap->arg, eap->forceit);
4643 else
4644 do_augroup(eap->arg, eap->forceit);
4645}
4646
4647/*
4648 * ":doautocmd": Apply the automatic commands to the current buffer.
4649 */
4650 static void
4651ex_doautocmd(eap)
4652 exarg_T *eap;
4653{
4654 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004655 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656}
4657#endif
4658
4659#ifdef FEAT_LISTCMDS
4660/*
4661 * :[N]bunload[!] [N] [bufname] unload buffer
4662 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4663 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4664 */
4665 static void
4666ex_bunload(eap)
4667 exarg_T *eap;
4668{
4669 eap->errmsg = do_bufdel(
4670 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4671 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4672 : DOBUF_UNLOAD, eap->arg,
4673 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4674}
4675
4676/*
4677 * :[N]buffer [N] to buffer N
4678 * :[N]sbuffer [N] to buffer N
4679 */
4680 static void
4681ex_buffer(eap)
4682 exarg_T *eap;
4683{
4684 if (*eap->arg)
4685 eap->errmsg = e_trailing;
4686 else
4687 {
4688 if (eap->addr_count == 0) /* default is current buffer */
4689 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4690 else
4691 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4692 }
4693}
4694
4695/*
4696 * :[N]bmodified [N] to next mod. buffer
4697 * :[N]sbmodified [N] to next mod. buffer
4698 */
4699 static void
4700ex_bmodified(eap)
4701 exarg_T *eap;
4702{
4703 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4704}
4705
4706/*
4707 * :[N]bnext [N] to next buffer
4708 * :[N]sbnext [N] split and to next buffer
4709 */
4710 static void
4711ex_bnext(eap)
4712 exarg_T *eap;
4713{
4714 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4715}
4716
4717/*
4718 * :[N]bNext [N] to previous buffer
4719 * :[N]bprevious [N] to previous buffer
4720 * :[N]sbNext [N] split and to previous buffer
4721 * :[N]sbprevious [N] split and to previous buffer
4722 */
4723 static void
4724ex_bprevious(eap)
4725 exarg_T *eap;
4726{
4727 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4728}
4729
4730/*
4731 * :brewind to first buffer
4732 * :bfirst to first buffer
4733 * :sbrewind split and to first buffer
4734 * :sbfirst split and to first buffer
4735 */
4736 static void
4737ex_brewind(eap)
4738 exarg_T *eap;
4739{
4740 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4741}
4742
4743/*
4744 * :blast to last buffer
4745 * :sblast split and to last buffer
4746 */
4747 static void
4748ex_blast(eap)
4749 exarg_T *eap;
4750{
4751 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4752}
4753#endif
4754
4755 int
4756ends_excmd(c)
4757 int c;
4758{
4759 return (c == NUL || c == '|' || c == '"' || c == '\n');
4760}
4761
4762#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4763 || defined(PROTO)
4764/*
4765 * Return the next command, after the first '|' or '\n'.
4766 * Return NULL if not found.
4767 */
4768 char_u *
4769find_nextcmd(p)
4770 char_u *p;
4771{
4772 while (*p != '|' && *p != '\n')
4773 {
4774 if (*p == NUL)
4775 return NULL;
4776 ++p;
4777 }
4778 return (p + 1);
4779}
4780#endif
4781
4782/*
4783 * Check if *p is a separator between Ex commands.
4784 * Return NULL if it isn't, (p + 1) if it is.
4785 */
4786 char_u *
4787check_nextcmd(p)
4788 char_u *p;
4789{
4790 p = skipwhite(p);
4791 if (*p == '|' || *p == '\n')
4792 return (p + 1);
4793 else
4794 return NULL;
4795}
4796
4797/*
4798 * - if there are more files to edit
4799 * - and this is the last window
4800 * - and forceit not used
4801 * - and not repeated twice on a row
4802 * return FAIL and give error message if 'message' TRUE
4803 * return OK otherwise
4804 */
4805 static int
4806check_more(message, forceit)
4807 int message; /* when FALSE check only, no messages */
4808 int forceit;
4809{
4810 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4811
4812 if (!forceit && only_one_window() && ARGCOUNT > 1 && !arg_had_last
4813 && n >= 0 && quitmore == 0)
4814 {
4815 if (message)
4816 {
4817#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4818 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4819 {
4820 char_u buff[IOSIZE];
4821
4822 if (n == 1)
4823 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4824 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004825 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 _("%d more files to edit. Quit anyway?"), n);
4827 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4828 return OK;
4829 return FAIL;
4830 }
4831#endif
4832 if (n == 1)
4833 EMSG(_("E173: 1 more file to edit"));
4834 else
4835 EMSGN(_("E173: %ld more files to edit"), n);
4836 quitmore = 2; /* next try to quit is allowed */
4837 }
4838 return FAIL;
4839 }
4840 return OK;
4841}
4842
4843#ifdef FEAT_CMDL_COMPL
4844/*
4845 * Function given to ExpandGeneric() to obtain the list of command names.
4846 */
4847/*ARGSUSED*/
4848 char_u *
4849get_command_name(xp, idx)
4850 expand_T *xp;
4851 int idx;
4852{
4853 if (idx >= (int)CMD_SIZE)
4854# ifdef FEAT_USR_CMDS
4855 return get_user_command_name(idx);
4856# else
4857 return NULL;
4858# endif
4859 return cmdnames[idx].cmd_name;
4860}
4861#endif
4862
4863#if defined(FEAT_USR_CMDS) || defined(PROTO)
4864static 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));
4865static void uc_list __ARGS((char_u *name, size_t name_len));
4866static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4867static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4868static 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));
4869
4870 static int
4871uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4872 char_u *name;
4873 size_t name_len;
4874 char_u *rep;
4875 long argt;
4876 long def;
4877 int flags;
4878 int compl;
4879 char_u *compl_arg;
4880 int force;
4881{
4882 ucmd_T *cmd = NULL;
4883 char_u *p;
4884 int i;
4885 int cmp = 1;
4886 char_u *rep_buf = NULL;
4887 garray_T *gap;
4888
4889 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4890 if (rep_buf == NULL)
4891 {
4892 /* Can't replace termcodes - try using the string as is */
4893 rep_buf = vim_strsave(rep);
4894
4895 /* Give up if out of memory */
4896 if (rep_buf == NULL)
4897 return FAIL;
4898 }
4899
4900 /* get address of growarray: global or in curbuf */
4901 if (flags & UC_BUFFER)
4902 {
4903 gap = &curbuf->b_ucmds;
4904 if (gap->ga_itemsize == 0)
4905 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4906 }
4907 else
4908 gap = &ucmds;
4909
4910 /* Search for the command in the already defined commands. */
4911 for (i = 0; i < gap->ga_len; ++i)
4912 {
4913 size_t len;
4914
4915 cmd = USER_CMD_GA(gap, i);
4916 len = STRLEN(cmd->uc_name);
4917 cmp = STRNCMP(name, cmd->uc_name, name_len);
4918 if (cmp == 0)
4919 {
4920 if (name_len < len)
4921 cmp = -1;
4922 else if (name_len > len)
4923 cmp = 1;
4924 }
4925
4926 if (cmp == 0)
4927 {
4928 if (!force)
4929 {
4930 EMSG(_("E174: Command already exists: add ! to replace it"));
4931 goto fail;
4932 }
4933
4934 vim_free(cmd->uc_rep);
4935 cmd->uc_rep = 0;
4936 break;
4937 }
4938
4939 /* Stop as soon as we pass the name to add */
4940 if (cmp < 0)
4941 break;
4942 }
4943
4944 /* Extend the array unless we're replacing an existing command */
4945 if (cmp != 0)
4946 {
4947 if (ga_grow(gap, 1) != OK)
4948 goto fail;
4949 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
4950 goto fail;
4951
4952 cmd = USER_CMD_GA(gap, i);
4953 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
4954
4955 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956
4957 cmd->uc_name = p;
4958 }
4959
4960 cmd->uc_rep = rep_buf;
4961 cmd->uc_argt = argt;
4962 cmd->uc_def = def;
4963 cmd->uc_compl = compl;
4964#ifdef FEAT_EVAL
4965 cmd->uc_scriptID = current_SID;
4966# ifdef FEAT_CMDL_COMPL
4967 cmd->uc_compl_arg = compl_arg;
4968# endif
4969#endif
4970
4971 return OK;
4972
4973fail:
4974 vim_free(rep_buf);
4975#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4976 vim_free(compl_arg);
4977#endif
4978 return FAIL;
4979}
4980
4981/*
4982 * List of names for completion for ":command" with the EXPAND_ flag.
4983 * Must be alphabetical for completion.
4984 */
4985static struct
4986{
4987 int expand;
4988 char *name;
4989} command_complete[] =
4990{
4991 {EXPAND_AUGROUP, "augroup"},
4992 {EXPAND_BUFFERS, "buffer"},
4993 {EXPAND_COMMANDS, "command"},
4994#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4995 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00004996 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997#endif
4998 {EXPAND_DIRECTORIES, "dir"},
4999 {EXPAND_ENV_VARS, "environment"},
5000 {EXPAND_EVENTS, "event"},
5001 {EXPAND_EXPRESSION, "expression"},
5002 {EXPAND_FILES, "file"},
5003 {EXPAND_FUNCTIONS, "function"},
5004 {EXPAND_HELP, "help"},
5005 {EXPAND_HIGHLIGHT, "highlight"},
5006 {EXPAND_MAPPINGS, "mapping"},
5007 {EXPAND_MENUS, "menu"},
5008 {EXPAND_SETTINGS, "option"},
5009 {EXPAND_TAGS, "tag"},
5010 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5011 {EXPAND_USER_VARS, "var"},
5012 {0, NULL}
5013};
5014
5015 static void
5016uc_list(name, name_len)
5017 char_u *name;
5018 size_t name_len;
5019{
5020 int i, j;
5021 int found = FALSE;
5022 ucmd_T *cmd;
5023 int len;
5024 long a;
5025 garray_T *gap;
5026
5027 gap = &curbuf->b_ucmds;
5028 for (;;)
5029 {
5030 for (i = 0; i < gap->ga_len; ++i)
5031 {
5032 cmd = USER_CMD_GA(gap, i);
5033 a = cmd->uc_argt;
5034
5035 /* Skip commands which don't match the requested prefix */
5036 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5037 continue;
5038
5039 /* Put out the title first time */
5040 if (!found)
5041 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5042 found = TRUE;
5043 msg_putchar('\n');
5044 if (got_int)
5045 break;
5046
5047 /* Special cases */
5048 msg_putchar(a & BANG ? '!' : ' ');
5049 msg_putchar(a & REGSTR ? '"' : ' ');
5050 msg_putchar(gap != &ucmds ? 'b' : ' ');
5051 msg_putchar(' ');
5052
5053 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5054 len = (int)STRLEN(cmd->uc_name) + 4;
5055
5056 do {
5057 msg_putchar(' ');
5058 ++len;
5059 } while (len < 16);
5060
5061 len = 0;
5062
5063 /* Arguments */
5064 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5065 {
5066 case 0: IObuff[len++] = '0'; break;
5067 case (EXTRA): IObuff[len++] = '*'; break;
5068 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5069 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5070 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5071 }
5072
5073 do {
5074 IObuff[len++] = ' ';
5075 } while (len < 5);
5076
5077 /* Range */
5078 if (a & (RANGE|COUNT))
5079 {
5080 if (a & COUNT)
5081 {
5082 /* -count=N */
5083 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5084 len += (int)STRLEN(IObuff + len);
5085 }
5086 else if (a & DFLALL)
5087 IObuff[len++] = '%';
5088 else if (cmd->uc_def >= 0)
5089 {
5090 /* -range=N */
5091 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5092 len += (int)STRLEN(IObuff + len);
5093 }
5094 else
5095 IObuff[len++] = '.';
5096 }
5097
5098 do {
5099 IObuff[len++] = ' ';
5100 } while (len < 11);
5101
5102 /* Completion */
5103 for (j = 0; command_complete[j].expand != 0; ++j)
5104 if (command_complete[j].expand == cmd->uc_compl)
5105 {
5106 STRCPY(IObuff + len, command_complete[j].name);
5107 len += (int)STRLEN(IObuff + len);
5108 break;
5109 }
5110
5111 do {
5112 IObuff[len++] = ' ';
5113 } while (len < 21);
5114
5115 IObuff[len] = '\0';
5116 msg_outtrans(IObuff);
5117
5118 msg_outtrans_special(cmd->uc_rep, FALSE);
5119 out_flush();
5120 ui_breakcheck();
5121 if (got_int)
5122 break;
5123 }
5124 if (gap == &ucmds || i < gap->ga_len)
5125 break;
5126 gap = &ucmds;
5127 }
5128
5129 if (!found)
5130 MSG(_("No user-defined commands found"));
5131}
5132
5133 static char_u *
5134uc_fun_cmd()
5135{
5136 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5137 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5138 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5139 0xb9, 0x7f, 0};
5140 int i;
5141
5142 for (i = 0; fcmd[i]; ++i)
5143 IObuff[i] = fcmd[i] - 0x40;
5144 IObuff[i] = 0;
5145 return IObuff;
5146}
5147
5148 static int
5149uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5150 char_u *attr;
5151 size_t len;
5152 long *argt;
5153 long *def;
5154 int *flags;
5155 int *compl;
5156 char_u **compl_arg;
5157{
5158 char_u *p;
5159
5160 if (len == 0)
5161 {
5162 EMSG(_("E175: No attribute specified"));
5163 return FAIL;
5164 }
5165
5166 /* First, try the simple attributes (no arguments) */
5167 if (STRNICMP(attr, "bang", len) == 0)
5168 *argt |= BANG;
5169 else if (STRNICMP(attr, "buffer", len) == 0)
5170 *flags |= UC_BUFFER;
5171 else if (STRNICMP(attr, "register", len) == 0)
5172 *argt |= REGSTR;
5173 else if (STRNICMP(attr, "bar", len) == 0)
5174 *argt |= TRLBAR;
5175 else
5176 {
5177 int i;
5178 char_u *val = NULL;
5179 size_t vallen = 0;
5180 size_t attrlen = len;
5181
5182 /* Look for the attribute name - which is the part before any '=' */
5183 for (i = 0; i < (int)len; ++i)
5184 {
5185 if (attr[i] == '=')
5186 {
5187 val = &attr[i + 1];
5188 vallen = len - i - 1;
5189 attrlen = i;
5190 break;
5191 }
5192 }
5193
5194 if (STRNICMP(attr, "nargs", attrlen) == 0)
5195 {
5196 if (vallen == 1)
5197 {
5198 if (*val == '0')
5199 /* Do nothing - this is the default */;
5200 else if (*val == '1')
5201 *argt |= (EXTRA | NOSPC | NEEDARG);
5202 else if (*val == '*')
5203 *argt |= EXTRA;
5204 else if (*val == '?')
5205 *argt |= (EXTRA | NOSPC);
5206 else if (*val == '+')
5207 *argt |= (EXTRA | NEEDARG);
5208 else
5209 goto wrong_nargs;
5210 }
5211 else
5212 {
5213wrong_nargs:
5214 EMSG(_("E176: Invalid number of arguments"));
5215 return FAIL;
5216 }
5217 }
5218 else if (STRNICMP(attr, "range", attrlen) == 0)
5219 {
5220 *argt |= RANGE;
5221 if (vallen == 1 && *val == '%')
5222 *argt |= DFLALL;
5223 else if (val != NULL)
5224 {
5225 p = val;
5226 if (*def >= 0)
5227 {
5228two_count:
5229 EMSG(_("E177: Count cannot be specified twice"));
5230 return FAIL;
5231 }
5232
5233 *def = getdigits(&p);
5234 *argt |= (ZEROR | NOTADR);
5235
5236 if (p != val + vallen || vallen == 0)
5237 {
5238invalid_count:
5239 EMSG(_("E178: Invalid default value for count"));
5240 return FAIL;
5241 }
5242 }
5243 }
5244 else if (STRNICMP(attr, "count", attrlen) == 0)
5245 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005246 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247
5248 if (val != NULL)
5249 {
5250 p = val;
5251 if (*def >= 0)
5252 goto two_count;
5253
5254 *def = getdigits(&p);
5255
5256 if (p != val + vallen)
5257 goto invalid_count;
5258 }
5259
5260 if (*def < 0)
5261 *def = 0;
5262 }
5263 else if (STRNICMP(attr, "complete", attrlen) == 0)
5264 {
5265 char_u *arg = NULL;
5266 size_t arglen = 0;
5267
5268 if (val == NULL)
5269 {
5270 EMSG(_("E179: argument required for complete"));
5271 return FAIL;
5272 }
5273 /* Look for any argument part - which is the part after any ',' */
5274 for (i = 0; i < (int)vallen; ++i)
5275 {
5276 if (val[i] == ',')
5277 {
5278 arg = &val[i + 1];
5279 arglen = vallen - i - 1;
5280 vallen = i;
5281 break;
5282 }
5283 }
5284
5285 for (i = 0; command_complete[i].expand != 0; ++i)
5286 {
5287 if (STRLEN(command_complete[i].name) == vallen
5288 && STRNCMP(val, command_complete[i].name, vallen) == 0)
5289 {
5290 *compl = command_complete[i].expand;
5291 if (command_complete[i].expand == EXPAND_BUFFERS)
5292 *argt |= BUFNAME;
5293 else if (command_complete[i].expand == EXPAND_DIRECTORIES
5294 || command_complete[i].expand == EXPAND_FILES)
5295 *argt |= XFILE;
5296 break;
5297 }
5298 }
5299
5300 if (command_complete[i].expand == 0)
5301 {
5302 EMSG2(_("E180: Invalid complete value: %s"), val);
5303 return FAIL;
5304 }
5305#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaara466c992005-07-09 21:03:22 +00005306 if (*compl != EXPAND_USER_DEFINED && *compl != EXPAND_USER_LIST &&
5307 arg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308#else
5309 if (arg != NULL)
5310#endif
5311 {
5312 EMSG(_("E468: Completion argument only allowed for custom completion"));
5313 return FAIL;
5314 }
5315#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaara466c992005-07-09 21:03:22 +00005316 if ((*compl == EXPAND_USER_DEFINED || *compl == EXPAND_USER_LIST) &&
5317 arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
5319 EMSG(_("E467: Custom completion requires a function argument"));
5320 return FAIL;
5321 }
5322 if (arg != NULL)
5323 *compl_arg = vim_strnsave(arg, (int)arglen);
5324#endif
5325 }
5326 else
5327 {
5328 char_u ch = attr[len];
5329 attr[len] = '\0';
5330 EMSG2(_("E181: Invalid attribute: %s"), attr);
5331 attr[len] = ch;
5332 return FAIL;
5333 }
5334 }
5335
5336 return OK;
5337}
5338
5339 static void
5340ex_command(eap)
5341 exarg_T *eap;
5342{
5343 char_u *name;
5344 char_u *end;
5345 char_u *p;
5346 long argt = 0;
5347 long def = -1;
5348 int flags = 0;
5349 int compl = EXPAND_NOTHING;
5350 char_u *compl_arg = NULL;
5351 int has_attr = (eap->arg[0] == '-');
5352
5353 p = eap->arg;
5354
5355 /* Check for attributes */
5356 while (*p == '-')
5357 {
5358 ++p;
5359 end = skiptowhite(p);
5360 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5361 == FAIL)
5362 return;
5363 p = skipwhite(end);
5364 }
5365
5366 /* Get the name (if any) and skip to the following argument */
5367 name = p;
5368 if (ASCII_ISALPHA(*p))
5369 while (ASCII_ISALNUM(*p))
5370 ++p;
5371 if (!ends_excmd(*p) && !vim_iswhite(*p))
5372 {
5373 EMSG(_("E182: Invalid command name"));
5374 return;
5375 }
5376 end = p;
5377
5378 /* If there is nothing after the name, and no attributes were specified,
5379 * we are listing commands
5380 */
5381 p = skipwhite(end);
5382 if (!has_attr && ends_excmd(*p))
5383 {
5384 uc_list(name, end - name);
5385 }
5386 else if (!ASCII_ISUPPER(*name))
5387 {
5388 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5389 return;
5390 }
5391 else
5392 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5393 eap->forceit);
5394}
5395
5396/*
5397 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 * Clear all user commands, global and for current buffer.
5399 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005400/*ARGSUSED*/
5401 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402ex_comclear(eap)
5403 exarg_T *eap;
5404{
5405 uc_clear(&ucmds);
5406 uc_clear(&curbuf->b_ucmds);
5407}
5408
5409/*
5410 * Clear all user commands for "gap".
5411 */
5412 void
5413uc_clear(gap)
5414 garray_T *gap;
5415{
5416 int i;
5417 ucmd_T *cmd;
5418
5419 for (i = 0; i < gap->ga_len; ++i)
5420 {
5421 cmd = USER_CMD_GA(gap, i);
5422 vim_free(cmd->uc_name);
5423 vim_free(cmd->uc_rep);
5424# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5425 vim_free(cmd->uc_compl_arg);
5426# endif
5427 }
5428 ga_clear(gap);
5429}
5430
5431 static void
5432ex_delcommand(eap)
5433 exarg_T *eap;
5434{
5435 int i = 0;
5436 ucmd_T *cmd = NULL;
5437 int cmp = -1;
5438 garray_T *gap;
5439
5440 gap = &curbuf->b_ucmds;
5441 for (;;)
5442 {
5443 for (i = 0; i < gap->ga_len; ++i)
5444 {
5445 cmd = USER_CMD_GA(gap, i);
5446 cmp = STRCMP(eap->arg, cmd->uc_name);
5447 if (cmp <= 0)
5448 break;
5449 }
5450 if (gap == &ucmds || cmp == 0)
5451 break;
5452 gap = &ucmds;
5453 }
5454
5455 if (cmp != 0)
5456 {
5457 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5458 return;
5459 }
5460
5461 vim_free(cmd->uc_name);
5462 vim_free(cmd->uc_rep);
5463# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5464 vim_free(cmd->uc_compl_arg);
5465# endif
5466
5467 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468
5469 if (i < gap->ga_len)
5470 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5471}
5472
5473 static char_u *
5474uc_split_args(arg, lenp)
5475 char_u *arg;
5476 size_t *lenp;
5477{
5478 char_u *buf;
5479 char_u *p;
5480 char_u *q;
5481 int len;
5482
5483 /* Precalculate length */
5484 p = arg;
5485 len = 2; /* Initial and final quotes */
5486
5487 while (*p)
5488 {
5489 if (p[0] == '\\' && vim_iswhite(p[1]))
5490 {
5491 len += 1;
5492 p += 2;
5493 }
5494 else if (*p == '\\' || *p == '"')
5495 {
5496 len += 2;
5497 p += 1;
5498 }
5499 else if (vim_iswhite(*p))
5500 {
5501 p = skipwhite(p);
5502 if (*p == NUL)
5503 break;
5504 len += 3; /* "," */
5505 }
5506 else
5507 {
5508 ++len;
5509 ++p;
5510 }
5511 }
5512
5513 buf = alloc(len + 1);
5514 if (buf == NULL)
5515 {
5516 *lenp = 0;
5517 return buf;
5518 }
5519
5520 p = arg;
5521 q = buf;
5522 *q++ = '"';
5523 while (*p)
5524 {
5525 if (p[0] == '\\' && vim_iswhite(p[1]))
5526 {
5527 *q++ = p[1];
5528 p += 2;
5529 }
5530 else if (*p == '\\' || *p == '"')
5531 {
5532 *q++ = '\\';
5533 *q++ = *p++;
5534 }
5535 else if (vim_iswhite(*p))
5536 {
5537 p = skipwhite(p);
5538 if (*p == NUL)
5539 break;
5540 *q++ = '"';
5541 *q++ = ',';
5542 *q++ = '"';
5543 }
5544 else
5545 {
5546 *q++ = *p++;
5547 }
5548 }
5549 *q++ = '"';
5550 *q = 0;
5551
5552 *lenp = len;
5553 return buf;
5554}
5555
5556/*
5557 * Check for a <> code in a user command.
5558 * "code" points to the '<'. "len" the length of the <> (inclusive).
5559 * "buf" is where the result is to be added.
5560 * "split_buf" points to a buffer used for splitting, caller should free it.
5561 * "split_len" is the length of what "split_buf" contains.
5562 * Returns the length of the replacement, which has been added to "buf".
5563 * Returns -1 if there was no match, and only the "<" has been copied.
5564 */
5565 static size_t
5566uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5567 char_u *code;
5568 size_t len;
5569 char_u *buf;
5570 ucmd_T *cmd; /* the user command we're expanding */
5571 exarg_T *eap; /* ex arguments */
5572 char_u **split_buf;
5573 size_t *split_len;
5574{
5575 size_t result = 0;
5576 char_u *p = code + 1;
5577 size_t l = len - 2;
5578 int quote = 0;
5579 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5580 ct_LT, ct_NONE } type = ct_NONE;
5581
5582 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5583 {
5584 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5585 p += 2;
5586 l -= 2;
5587 }
5588
5589 if (l < 1)
5590 type = ct_NONE;
5591 else if (STRNICMP(p, "args", l) == 0)
5592 type = ct_ARGS;
5593 else if (STRNICMP(p, "bang", l) == 0)
5594 type = ct_BANG;
5595 else if (STRNICMP(p, "count", l) == 0)
5596 type = ct_COUNT;
5597 else if (STRNICMP(p, "line1", l) == 0)
5598 type = ct_LINE1;
5599 else if (STRNICMP(p, "line2", l) == 0)
5600 type = ct_LINE2;
5601 else if (STRNICMP(p, "lt", l) == 0)
5602 type = ct_LT;
5603 else if (STRNICMP(p, "register", l) == 0)
5604 type = ct_REGISTER;
5605
5606 switch (type)
5607 {
5608 case ct_ARGS:
5609 /* Simple case first */
5610 if (*eap->arg == NUL)
5611 {
5612 if (quote == 1)
5613 {
5614 result = 2;
5615 if (buf != NULL)
5616 STRCPY(buf, "''");
5617 }
5618 else
5619 result = 0;
5620 break;
5621 }
5622
5623 /* When specified there is a single argument don't split it.
5624 * Works for ":Cmd %" when % is "a b c". */
5625 if ((eap->argt & NOSPC) && quote == 2)
5626 quote = 1;
5627
5628 switch (quote)
5629 {
5630 case 0: /* No quoting, no splitting */
5631 result = STRLEN(eap->arg);
5632 if (buf != NULL)
5633 STRCPY(buf, eap->arg);
5634 break;
5635 case 1: /* Quote, but don't split */
5636 result = STRLEN(eap->arg) + 2;
5637 for (p = eap->arg; *p; ++p)
5638 {
5639 if (*p == '\\' || *p == '"')
5640 ++result;
5641 }
5642
5643 if (buf != NULL)
5644 {
5645 *buf++ = '"';
5646 for (p = eap->arg; *p; ++p)
5647 {
5648 if (*p == '\\' || *p == '"')
5649 *buf++ = '\\';
5650 *buf++ = *p;
5651 }
5652 *buf = '"';
5653 }
5654
5655 break;
5656 case 2: /* Quote and split */
5657 /* This is hard, so only do it once, and cache the result */
5658 if (*split_buf == NULL)
5659 *split_buf = uc_split_args(eap->arg, split_len);
5660
5661 result = *split_len;
5662 if (buf != NULL && result != 0)
5663 STRCPY(buf, *split_buf);
5664
5665 break;
5666 }
5667 break;
5668
5669 case ct_BANG:
5670 result = eap->forceit ? 1 : 0;
5671 if (quote)
5672 result += 2;
5673 if (buf != NULL)
5674 {
5675 if (quote)
5676 *buf++ = '"';
5677 if (eap->forceit)
5678 *buf++ = '!';
5679 if (quote)
5680 *buf = '"';
5681 }
5682 break;
5683
5684 case ct_LINE1:
5685 case ct_LINE2:
5686 case ct_COUNT:
5687 {
5688 char num_buf[20];
5689 long num = (type == ct_LINE1) ? eap->line1 :
5690 (type == ct_LINE2) ? eap->line2 :
5691 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5692 size_t num_len;
5693
5694 sprintf(num_buf, "%ld", num);
5695 num_len = STRLEN(num_buf);
5696 result = num_len;
5697
5698 if (quote)
5699 result += 2;
5700
5701 if (buf != NULL)
5702 {
5703 if (quote)
5704 *buf++ = '"';
5705 STRCPY(buf, num_buf);
5706 buf += num_len;
5707 if (quote)
5708 *buf = '"';
5709 }
5710
5711 break;
5712 }
5713
5714 case ct_REGISTER:
5715 result = eap->regname ? 1 : 0;
5716 if (quote)
5717 result += 2;
5718 if (buf != NULL)
5719 {
5720 if (quote)
5721 *buf++ = '\'';
5722 if (eap->regname)
5723 *buf++ = eap->regname;
5724 if (quote)
5725 *buf = '\'';
5726 }
5727 break;
5728
5729 case ct_LT:
5730 result = 1;
5731 if (buf != NULL)
5732 *buf = '<';
5733 break;
5734
5735 default:
5736 /* Not recognized: just copy the '<' and return -1. */
5737 result = (size_t)-1;
5738 if (buf != NULL)
5739 *buf = '<';
5740 break;
5741 }
5742
5743 return result;
5744}
5745
5746 static void
5747do_ucmd(eap)
5748 exarg_T *eap;
5749{
5750 char_u *buf;
5751 char_u *p;
5752 char_u *q;
5753
5754 char_u *start;
5755 char_u *end;
5756 size_t len, totlen;
5757
5758 size_t split_len = 0;
5759 char_u *split_buf = NULL;
5760 ucmd_T *cmd;
5761#ifdef FEAT_EVAL
5762 scid_T save_current_SID = current_SID;
5763#endif
5764
5765 if (eap->cmdidx == CMD_USER)
5766 cmd = USER_CMD(eap->useridx);
5767 else
5768 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5769
5770 /*
5771 * Replace <> in the command by the arguments.
5772 */
5773 buf = NULL;
5774 for (;;)
5775 {
5776 p = cmd->uc_rep;
5777 q = buf;
5778 totlen = 0;
5779 while ((start = vim_strchr(p, '<')) != NULL
5780 && (end = vim_strchr(start + 1, '>')) != NULL)
5781 {
5782 /* Include the '>' */
5783 ++end;
5784
5785 /* Take everything up to the '<' */
5786 len = start - p;
5787 if (buf == NULL)
5788 totlen += len;
5789 else
5790 {
5791 mch_memmove(q, p, len);
5792 q += len;
5793 }
5794
5795 len = uc_check_code(start, end - start, q, cmd, eap,
5796 &split_buf, &split_len);
5797 if (len == (size_t)-1)
5798 {
5799 /* no match, continue after '<' */
5800 p = start + 1;
5801 len = 1;
5802 }
5803 else
5804 p = end;
5805 if (buf == NULL)
5806 totlen += len;
5807 else
5808 q += len;
5809 }
5810 if (buf != NULL) /* second time here, finished */
5811 {
5812 STRCPY(q, p);
5813 break;
5814 }
5815
5816 totlen += STRLEN(p); /* Add on the trailing characters */
5817 buf = alloc((unsigned)(totlen + 1));
5818 if (buf == NULL)
5819 {
5820 vim_free(split_buf);
5821 return;
5822 }
5823 }
5824
5825#ifdef FEAT_EVAL
5826 current_SID = cmd->uc_scriptID;
5827#endif
5828 (void)do_cmdline(buf, eap->getline, eap->cookie,
5829 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5830#ifdef FEAT_EVAL
5831 current_SID = save_current_SID;
5832#endif
5833 vim_free(buf);
5834 vim_free(split_buf);
5835}
5836
5837# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5838 static char_u *
5839get_user_command_name(idx)
5840 int idx;
5841{
5842 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5843}
5844
5845/*
5846 * Function given to ExpandGeneric() to obtain the list of user command names.
5847 */
5848/*ARGSUSED*/
5849 char_u *
5850get_user_commands(xp, idx)
5851 expand_T *xp;
5852 int idx;
5853{
5854 if (idx < curbuf->b_ucmds.ga_len)
5855 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5856 idx -= curbuf->b_ucmds.ga_len;
5857 if (idx < ucmds.ga_len)
5858 return USER_CMD(idx)->uc_name;
5859 return NULL;
5860}
5861
5862/*
5863 * Function given to ExpandGeneric() to obtain the list of user command
5864 * attributes.
5865 */
5866/*ARGSUSED*/
5867 char_u *
5868get_user_cmd_flags(xp, idx)
5869 expand_T *xp;
5870 int idx;
5871{
5872 static char *user_cmd_flags[] =
5873 {"bang", "bar", "buffer", "complete", "count",
5874 "nargs", "range", "register"};
5875
5876 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5877 return NULL;
5878 return (char_u *)user_cmd_flags[idx];
5879}
5880
5881/*
5882 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5883 */
5884/*ARGSUSED*/
5885 char_u *
5886get_user_cmd_nargs(xp, idx)
5887 expand_T *xp;
5888 int idx;
5889{
5890 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5891
5892 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5893 return NULL;
5894 return (char_u *)user_cmd_nargs[idx];
5895}
5896
5897/*
5898 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5899 */
5900/*ARGSUSED*/
5901 char_u *
5902get_user_cmd_complete(xp, idx)
5903 expand_T *xp;
5904 int idx;
5905{
5906 return (char_u *)command_complete[idx].name;
5907}
5908# endif /* FEAT_CMDL_COMPL */
5909
5910#endif /* FEAT_USR_CMDS */
5911
5912 static void
5913ex_colorscheme(eap)
5914 exarg_T *eap;
5915{
5916 if (load_colors(eap->arg) == FAIL)
5917 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
5918}
5919
5920 static void
5921ex_highlight(eap)
5922 exarg_T *eap;
5923{
5924 if (*eap->arg == NUL && eap->cmd[2] == '!')
5925 MSG(_("Greetings, Vim user!"));
5926 do_highlight(eap->arg, eap->forceit, FALSE);
5927}
5928
5929
5930/*
5931 * Call this function if we thought we were going to exit, but we won't
5932 * (because of an error). May need to restore the terminal mode.
5933 */
5934 void
5935not_exiting()
5936{
5937 exiting = FALSE;
5938 settmode(TMODE_RAW);
5939}
5940
5941/*
5942 * ":quit": quit current window, quit Vim if closed the last window.
5943 */
5944 static void
5945ex_quit(eap)
5946 exarg_T *eap;
5947{
5948#ifdef FEAT_CMDWIN
5949 if (cmdwin_type != 0)
5950 {
5951 cmdwin_result = Ctrl_C;
5952 return;
5953 }
5954#endif
5955
5956#ifdef FEAT_NETBEANS_INTG
5957 netbeansForcedQuit = eap->forceit;
5958#endif
5959
5960 /*
5961 * If there are more files or windows we won't exit.
5962 */
5963 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5964 exiting = TRUE;
5965 if ((!P_HID(curbuf)
5966 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
5967 || check_more(TRUE, eap->forceit) == FAIL
5968 || (only_one_window() && check_changed_any(eap->forceit)))
5969 {
5970 not_exiting();
5971 }
5972 else
5973 {
5974#ifdef FEAT_WINDOWS
5975 if (only_one_window()) /* quit last window */
5976#endif
5977 getout(0);
5978#ifdef FEAT_WINDOWS
5979# ifdef FEAT_GUI
5980 need_mouse_correct = TRUE;
5981# endif
5982 /* close window; may free buffer */
5983 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
5984#endif
5985 }
5986}
5987
5988/*
5989 * ":cquit".
5990 */
5991/*ARGSUSED*/
5992 static void
5993ex_cquit(eap)
5994 exarg_T *eap;
5995{
5996 getout(1); /* this does not always pass on the exit code to the Manx
5997 compiler. why? */
5998}
5999
6000/*
6001 * ":qall": try to quit all windows
6002 */
6003 static void
6004ex_quit_all(eap)
6005 exarg_T *eap;
6006{
6007# ifdef FEAT_CMDWIN
6008 if (cmdwin_type != 0)
6009 {
6010 if (eap->forceit)
6011 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6012 else
6013 cmdwin_result = K_XF2;
6014 return;
6015 }
6016# endif
6017 exiting = TRUE;
6018 if (eap->forceit || !check_changed_any(FALSE))
6019 getout(0);
6020 not_exiting();
6021}
6022
6023#ifdef FEAT_WINDOWS
6024/*
6025 * ":close": close current window, unless it is the last one
6026 */
6027 static void
6028ex_close(eap)
6029 exarg_T *eap;
6030{
6031# ifdef FEAT_CMDWIN
6032 if (cmdwin_type != 0)
6033 cmdwin_result = K_IGNORE;
6034 else
6035# endif
6036 ex_win_close(eap, curwin);
6037}
6038
6039 static void
6040ex_win_close(eap, win)
6041 exarg_T *eap;
6042 win_T *win;
6043{
6044 int need_hide;
6045 buf_T *buf = win->w_buffer;
6046
6047 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
6048 if (need_hide && !P_HID(buf) && !eap->forceit)
6049 {
6050#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6051 if ((p_confirm || cmdmod.confirm) && p_write)
6052 {
6053 dialog_changed(buf, FALSE);
6054 if (buf_valid(buf) && bufIsChanged(buf))
6055 return;
6056 need_hide = FALSE;
6057 }
6058 else
6059#endif
6060 {
6061 EMSG(_(e_nowrtmsg));
6062 return;
6063 }
6064 }
6065
6066#ifdef FEAT_GUI
6067 need_mouse_correct = TRUE;
6068#endif
6069 /* free buffer when not hiding it or when it's a scratch buffer */
6070 win_close(win, !need_hide && !P_HID(buf));
6071}
6072
6073#ifdef FEAT_QUICKFIX
6074/*
6075 * ":pclose": Close any preview window.
6076 */
6077 static void
6078ex_pclose(eap)
6079 exarg_T *eap;
6080{
6081 win_T *win;
6082
6083 for (win = firstwin; win != NULL; win = win->w_next)
6084 if (win->w_p_pvw)
6085 {
6086 ex_win_close(eap, win);
6087 break;
6088 }
6089}
6090#endif
6091
6092/*
6093 * ":only".
6094 */
6095 static void
6096ex_only(eap)
6097 exarg_T *eap;
6098{
6099# ifdef FEAT_GUI
6100 need_mouse_correct = TRUE;
6101# endif
6102 close_others(TRUE, eap->forceit);
6103}
6104
6105/*
6106 * ":all" and ":sall".
6107 */
6108 static void
6109ex_all(eap)
6110 exarg_T *eap;
6111{
6112 if (eap->addr_count == 0)
6113 eap->line2 = 9999;
6114 do_arg_all((int)eap->line2, eap->forceit);
6115}
6116#endif /* FEAT_WINDOWS */
6117
6118 static void
6119ex_hide(eap)
6120 exarg_T *eap;
6121{
6122 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6123 eap->errmsg = e_invarg;
6124 else
6125 {
6126 /* ":hide" or ":hide | cmd": hide current window */
6127 eap->nextcmd = check_nextcmd(eap->arg);
6128#ifdef FEAT_WINDOWS
6129 if (!eap->skip)
6130 {
6131# ifdef FEAT_GUI
6132 need_mouse_correct = TRUE;
6133# endif
6134 win_close(curwin, FALSE); /* don't free buffer */
6135 }
6136#endif
6137 }
6138}
6139
6140/*
6141 * ":stop" and ":suspend": Suspend Vim.
6142 */
6143 static void
6144ex_stop(eap)
6145 exarg_T *eap;
6146{
6147 /*
6148 * Disallow suspending for "rvim".
6149 */
6150 if (!check_restricted()
6151#ifdef WIN3264
6152 /*
6153 * Check if external commands are allowed now.
6154 */
6155 && can_end_termcap_mode(TRUE)
6156#endif
6157 )
6158 {
6159 if (!eap->forceit)
6160 autowrite_all();
6161 windgoto((int)Rows - 1, 0);
6162 out_char('\n');
6163 out_flush();
6164 stoptermcap();
6165 out_flush(); /* needed for SUN to restore xterm buffer */
6166#ifdef FEAT_TITLE
6167 mch_restore_title(3); /* restore window titles */
6168#endif
6169 ui_suspend(); /* call machine specific function */
6170#ifdef FEAT_TITLE
6171 maketitle();
6172 resettitle(); /* force updating the title */
6173#endif
6174 starttermcap();
6175 scroll_start(); /* scroll screen before redrawing */
6176 redraw_later_clear();
6177 shell_resized(); /* may have resized window */
6178 }
6179}
6180
6181/*
6182 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6183 */
6184 static void
6185ex_exit(eap)
6186 exarg_T *eap;
6187{
6188#ifdef FEAT_CMDWIN
6189 if (cmdwin_type != 0)
6190 {
6191 cmdwin_result = Ctrl_C;
6192 return;
6193 }
6194#endif
6195
6196 /*
6197 * if more files or windows we won't exit
6198 */
6199 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6200 exiting = TRUE;
6201 if ( ((eap->cmdidx == CMD_wq
6202 || curbufIsChanged())
6203 && do_write(eap) == FAIL)
6204 || check_more(TRUE, eap->forceit) == FAIL
6205 || (only_one_window() && check_changed_any(eap->forceit)))
6206 {
6207 not_exiting();
6208 }
6209 else
6210 {
6211#ifdef FEAT_WINDOWS
6212 if (only_one_window()) /* quit last window, exit Vim */
6213#endif
6214 getout(0);
6215#ifdef FEAT_WINDOWS
6216# ifdef FEAT_GUI
6217 need_mouse_correct = TRUE;
6218# endif
6219 /* quit current window, may free buffer */
6220 win_close(curwin, !P_HID(curwin->w_buffer));
6221#endif
6222 }
6223}
6224
6225/*
6226 * ":print", ":list", ":number".
6227 */
6228 static void
6229ex_print(eap)
6230 exarg_T *eap;
6231{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006232 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6233 EMSG(_(e_emptybuf));
6234 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006236 for ( ;!got_int; ui_breakcheck())
6237 {
6238 print_line(eap->line1,
6239 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6240 || (eap->flags & EXFLAG_NR)),
6241 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6242 if (++eap->line1 > eap->line2)
6243 break;
6244 out_flush(); /* show one line at a time */
6245 }
6246 setpcmark();
6247 /* put cursor at last line */
6248 curwin->w_cursor.lnum = eap->line2;
6249 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
6251
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253}
6254
6255#ifdef FEAT_BYTEOFF
6256 static void
6257ex_goto(eap)
6258 exarg_T *eap;
6259{
6260 goto_byte(eap->line2);
6261}
6262#endif
6263
6264/*
6265 * ":shell".
6266 */
6267/*ARGSUSED*/
6268 static void
6269ex_shell(eap)
6270 exarg_T *eap;
6271{
6272 do_shell(NULL, 0);
6273}
6274
6275#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6276 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
6277 || defined(PROTO)
6278
6279/*
6280 * Handle a file drop. The code is here because a drop is *nearly* like an
6281 * :args command, but not quite (we have a list of exact filenames, so we
6282 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6283 * code is very similar to :args and hence needs access to a lot of the static
6284 * functions in this file.
6285 *
6286 * The list should be allocated using alloc(), as should each item in the
6287 * list. This function takes over responsibility for freeing the list.
6288 *
6289 * XXX The list is made into the arggument list. This is freed using
6290 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6291 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6292 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6293 * file functionality is (currently) not in EMX this is not presently a
6294 * problem.
6295 */
6296 void
6297handle_drop(filec, filev, split)
6298 int filec; /* the number of files dropped */
6299 char_u **filev; /* the list of files dropped */
6300 int split; /* force splitting the window */
6301{
6302 exarg_T ea;
6303 int save_msg_scroll = msg_scroll;
6304
6305# ifdef FEAT_CMDWIN
6306 if (cmdwin_type != 0)
6307 return;
6308# endif
6309
6310 /* Check whether the current buffer is changed. If so, we will need
6311 * to split the current window or data could be lost.
6312 * We don't need to check if the 'hidden' option is set, as in this
6313 * case the buffer won't be lost.
6314 */
6315 if (!P_HID(curbuf) && !split)
6316 {
6317 ++emsg_off;
6318 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6319 --emsg_off;
6320 }
6321 if (split)
6322 {
6323# ifdef FEAT_WINDOWS
6324 if (win_split(0, 0) == FAIL)
6325 return;
6326# ifdef FEAT_SCROLLBIND
6327 curwin->w_p_scb = FALSE;
6328# endif
6329
6330 /* When splitting the window, create a new alist. Otherwise the
6331 * existing one is overwritten. */
6332 alist_unlink(curwin->w_alist);
6333 alist_new();
6334# else
6335 return; /* can't split, always fail */
6336# endif
6337 }
6338
6339 /*
6340 * Set up the new argument list.
6341 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006342 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343
6344 /*
6345 * Move to the first file.
6346 */
6347 /* Fake up a minimal "next" command for do_argfile() */
6348 vim_memset(&ea, 0, sizeof(ea));
6349 ea.cmd = (char_u *)"next";
6350 do_argfile(&ea, 0);
6351
6352 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6353 * mode that is not needed here. */
6354 need_start_insertmode = FALSE;
6355
6356 /* Restore msg_scroll, otherwise a following command may cause scrolling
6357 * unexpectedly. The screen will be redrawn by the caller, thus
6358 * msg_scroll being set by displaying a message is irrelevant. */
6359 msg_scroll = save_msg_scroll;
6360}
6361#endif
6362
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363/*
6364 * Clear an argument list: free all file names and reset it to zero entries.
6365 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006366 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367alist_clear(al)
6368 alist_T *al;
6369{
6370 while (--al->al_ga.ga_len >= 0)
6371 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6372 ga_clear(&al->al_ga);
6373}
6374
6375/*
6376 * Init an argument list.
6377 */
6378 void
6379alist_init(al)
6380 alist_T *al;
6381{
6382 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6383}
6384
6385#if defined(FEAT_WINDOWS) || defined(PROTO)
6386
6387/*
6388 * Remove a reference from an argument list.
6389 * Ignored when the argument list is the global one.
6390 * If the argument list is no longer used by any window, free it.
6391 */
6392 void
6393alist_unlink(al)
6394 alist_T *al;
6395{
6396 if (al != &global_alist && --al->al_refcount <= 0)
6397 {
6398 alist_clear(al);
6399 vim_free(al);
6400 }
6401}
6402
6403# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6404/*
6405 * Create a new argument list and use it for the current window.
6406 */
6407 void
6408alist_new()
6409{
6410 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6411 if (curwin->w_alist == NULL)
6412 {
6413 curwin->w_alist = &global_alist;
6414 ++global_alist.al_refcount;
6415 }
6416 else
6417 {
6418 curwin->w_alist->al_refcount = 1;
6419 alist_init(curwin->w_alist);
6420 }
6421}
6422# endif
6423#endif
6424
6425#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6426/*
6427 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006428 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6429 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 */
6431 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006432alist_expand(fnum_list, fnum_len)
6433 int *fnum_list;
6434 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435{
6436 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006437 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 char_u **new_arg_files;
6439 int new_arg_file_count;
6440 char_u *save_p_su = p_su;
6441 int i;
6442
6443 /* Don't use 'suffixes' here. This should work like the shell did the
6444 * expansion. Also, the vimrc file isn't read yet, thus the user
6445 * can't set the options. */
6446 p_su = empty_option;
6447 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6448 if (old_arg_files != NULL)
6449 {
6450 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006451 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6452 old_arg_count = GARGCOUNT;
6453 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 &new_arg_file_count, &new_arg_files,
6455 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6456 && new_arg_file_count > 0)
6457 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006458 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6459 TRUE, fnum_list, fnum_len);
6460 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 }
6463 p_su = save_p_su;
6464}
6465#endif
6466
6467/*
6468 * Set the argument list for the current window.
6469 * Takes over the allocated files[] and the allocated fnames in it.
6470 */
6471 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006472alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 alist_T *al;
6474 int count;
6475 char_u **files;
6476 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006477 int *fnum_list;
6478 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
6480 int i;
6481
6482 alist_clear(al);
6483 if (ga_grow(&al->al_ga, count) == OK)
6484 {
6485 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006486 {
6487 if (got_int)
6488 {
6489 /* When adding many buffers this can take a long time. Allow
6490 * interrupting here. */
6491 while (i < count)
6492 vim_free(files[i++]);
6493 break;
6494 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006495
6496 /* May set buffer name of a buffer previously used for the
6497 * argument list, so that it's re-used by alist_add. */
6498 if (fnum_list != NULL && i < fnum_len)
6499 buf_set_name(fnum_list[i], files[i]);
6500
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006502 ui_breakcheck();
6503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 vim_free(files);
6505 }
6506 else
6507 FreeWild(count, files);
6508#ifdef FEAT_WINDOWS
6509 if (al == &global_alist)
6510#endif
6511 arg_had_last = FALSE;
6512}
6513
6514/*
6515 * Add file "fname" to argument list "al".
6516 * "fname" must have been allocated and "al" must have been checked for room.
6517 */
6518 void
6519alist_add(al, fname, set_fnum)
6520 alist_T *al;
6521 char_u *fname;
6522 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6523{
6524 if (fname == NULL) /* don't add NULL file names */
6525 return;
6526#ifdef BACKSLASH_IN_FILENAME
6527 slash_adjust(fname);
6528#endif
6529 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6530 if (set_fnum > 0)
6531 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6532 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6533 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534}
6535
6536#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6537/*
6538 * Adjust slashes in file names. Called after 'shellslash' was set.
6539 */
6540 void
6541alist_slash_adjust()
6542{
6543 int i;
6544# ifdef FEAT_WINDOWS
6545 win_T *wp;
6546# endif
6547
6548 for (i = 0; i < GARGCOUNT; ++i)
6549 if (GARGLIST[i].ae_fname != NULL)
6550 slash_adjust(GARGLIST[i].ae_fname);
6551# ifdef FEAT_WINDOWS
6552 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6553 if (wp->w_alist != &global_alist)
6554 for (i = 0; i < WARGCOUNT(wp); ++i)
6555 if (WARGLIST(wp)[i].ae_fname != NULL)
6556 slash_adjust(WARGLIST(wp)[i].ae_fname);
6557# endif
6558}
6559#endif
6560
6561/*
6562 * ":preserve".
6563 */
6564/*ARGSUSED*/
6565 static void
6566ex_preserve(eap)
6567 exarg_T *eap;
6568{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006569 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 ml_preserve(curbuf, TRUE);
6571}
6572
6573/*
6574 * ":recover".
6575 */
6576 static void
6577ex_recover(eap)
6578 exarg_T *eap;
6579{
6580 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6581 recoverymode = TRUE;
6582 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6583 && (*eap->arg == NUL
6584 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6585 ml_recover();
6586 recoverymode = FALSE;
6587}
6588
6589/*
6590 * Command modifier used in a wrong way.
6591 */
6592 static void
6593ex_wrongmodifier(eap)
6594 exarg_T *eap;
6595{
6596 eap->errmsg = e_invcmd;
6597}
6598
6599#ifdef FEAT_WINDOWS
6600/*
6601 * :sview [+command] file split window with new file, read-only
6602 * :split [[+command] file] split window with current or new file
6603 * :vsplit [[+command] file] split window vertically with current or new file
6604 * :new [[+command] file] split window with no or new file
6605 * :vnew [[+command] file] split vertically window with no or new file
6606 * :sfind [+command] file split window with file in 'path'
6607 */
6608 void
6609ex_splitview(eap)
6610 exarg_T *eap;
6611{
6612 win_T *old_curwin;
6613#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6614 char_u *fname = NULL;
6615#endif
6616#ifdef FEAT_BROWSE
6617 int browse_flag = cmdmod.browse;
6618#endif
6619
6620#ifndef FEAT_VERTSPLIT
6621 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6622 {
6623 ex_ni(eap);
6624 return;
6625 }
6626#endif
6627
6628 old_curwin = curwin;
6629#ifdef FEAT_GUI
6630 need_mouse_correct = TRUE;
6631#endif
6632
6633#ifdef FEAT_QUICKFIX
6634 /* A ":split" in the quickfix window works like ":new". Don't want two
6635 * quickfix windows. */
6636 if (bt_quickfix(curbuf))
6637 {
6638 if (eap->cmdidx == CMD_split)
6639 eap->cmdidx = CMD_new;
6640# ifdef FEAT_VERTSPLIT
6641 if (eap->cmdidx == CMD_vsplit)
6642 eap->cmdidx = CMD_vnew;
6643# endif
6644 }
6645#endif
6646
6647#ifdef FEAT_SEARCHPATH
6648 if (eap->cmdidx == CMD_sfind)
6649 {
6650 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6651 FNAME_MESS, TRUE, curbuf->b_ffname);
6652 if (fname == NULL)
6653 goto theend;
6654 eap->arg = fname;
6655 }
6656# ifdef FEAT_BROWSE
6657 else
6658# endif
6659#endif
6660#ifdef FEAT_BROWSE
6661 if (cmdmod.browse
6662# ifdef FEAT_VERTSPLIT
6663 && eap->cmdidx != CMD_vnew
6664#endif
6665 && eap->cmdidx != CMD_new)
6666 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006667 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 eap->arg, NULL, NULL, NULL, curbuf);
6669 if (fname == NULL)
6670 goto theend;
6671 eap->arg = fname;
6672 }
6673 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
6674#endif
6675
6676 if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
6677 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6678 {
6679#ifdef FEAT_SCROLLBIND
6680 /* Reset 'scrollbind' when editing another file, but keep it when
6681 * doing ":split" without arguments. */
6682 if (*eap->arg != NUL
6683#ifdef FEAT_BROWSE
6684 || cmdmod.browse
6685#endif
6686 )
6687 curwin->w_p_scb = FALSE;
6688 else
6689 do_check_scrollbind(FALSE);
6690#endif
6691 do_exedit(eap, old_curwin);
6692 }
6693
6694#ifdef FEAT_BROWSE
6695 cmdmod.browse = browse_flag;
6696#endif
6697
6698#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6699theend:
6700 vim_free(fname);
6701#endif
6702}
6703#endif
6704
6705/*
6706 * ":mode": Set screen mode.
6707 * If no argument given, just get the screen size and redraw.
6708 */
6709 static void
6710ex_mode(eap)
6711 exarg_T *eap;
6712{
6713 if (*eap->arg == NUL)
6714 shell_resized();
6715 else
6716 mch_screenmode(eap->arg);
6717}
6718
6719#ifdef FEAT_WINDOWS
6720/*
6721 * ":resize".
6722 * set, increment or decrement current window height
6723 */
6724 static void
6725ex_resize(eap)
6726 exarg_T *eap;
6727{
6728 int n;
6729 win_T *wp = curwin;
6730
6731 if (eap->addr_count > 0)
6732 {
6733 n = eap->line2;
6734 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6735 ;
6736 }
6737
6738#ifdef FEAT_GUI
6739 need_mouse_correct = TRUE;
6740#endif
6741 n = atol((char *)eap->arg);
6742#ifdef FEAT_VERTSPLIT
6743 if (cmdmod.split & WSP_VERT)
6744 {
6745 if (*eap->arg == '-' || *eap->arg == '+')
6746 n += W_WIDTH(curwin);
6747 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6748 n = 9999;
6749 win_setwidth_win((int)n, wp);
6750 }
6751 else
6752#endif
6753 {
6754 if (*eap->arg == '-' || *eap->arg == '+')
6755 n += curwin->w_height;
6756 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6757 n = 9999;
6758 win_setheight_win((int)n, wp);
6759 }
6760}
6761#endif
6762
6763/*
6764 * ":find [+command] <file>" command.
6765 */
6766 static void
6767ex_find(eap)
6768 exarg_T *eap;
6769{
6770#ifdef FEAT_SEARCHPATH
6771 char_u *fname;
6772 int count;
6773
6774 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6775 TRUE, curbuf->b_ffname);
6776 if (eap->addr_count > 0)
6777 {
6778 /* Repeat finding the file "count" times. This matters when it
6779 * appears several times in the path. */
6780 count = eap->line2;
6781 while (fname != NULL && --count > 0)
6782 {
6783 vim_free(fname);
6784 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6785 FALSE, curbuf->b_ffname);
6786 }
6787 }
6788
6789 if (fname != NULL)
6790 {
6791 eap->arg = fname;
6792#endif
6793 do_exedit(eap, NULL);
6794#ifdef FEAT_SEARCHPATH
6795 vim_free(fname);
6796 }
6797#endif
6798}
6799
6800/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006801 * ":open" simulation: for now just work like ":visual".
6802 */
6803 static void
6804ex_open(eap)
6805 exarg_T *eap;
6806{
6807 regmatch_T regmatch;
6808 char_u *p;
6809
6810 curwin->w_cursor.lnum = eap->line2;
6811 beginline(BL_SOL | BL_FIX);
6812 if (*eap->arg == '/')
6813 {
6814 /* ":open /pattern/": put cursor in column found with pattern */
6815 ++eap->arg;
6816 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6817 *p = NUL;
6818 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6819 if (regmatch.regprog != NULL)
6820 {
6821 regmatch.rm_ic = p_ic;
6822 p = ml_get_curline();
6823 if (vim_regexec(&regmatch, p, (colnr_T)0))
6824 curwin->w_cursor.col = regmatch.startp[0] - p;
6825 else
6826 EMSG(_(e_nomatch));
6827 vim_free(regmatch.regprog);
6828 }
6829 /* Move to the NUL, ignore any other arguments. */
6830 eap->arg += STRLEN(eap->arg);
6831 }
6832 check_cursor();
6833
6834 eap->cmdidx = CMD_visual;
6835 do_exedit(eap, NULL);
6836}
6837
6838/*
6839 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 */
6841 static void
6842ex_edit(eap)
6843 exarg_T *eap;
6844{
6845 do_exedit(eap, NULL);
6846}
6847
6848/*
6849 * ":edit <file>" command and alikes.
6850 */
6851/*ARGSUSED*/
6852 void
6853do_exedit(eap, old_curwin)
6854 exarg_T *eap;
6855 win_T *old_curwin; /* curwin before doing a split or NULL */
6856{
6857 int n;
6858#ifdef FEAT_WINDOWS
6859 int need_hide;
6860#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006861 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862
6863 /*
6864 * ":vi" command ends Ex mode.
6865 */
6866 if (exmode_active && (eap->cmdidx == CMD_visual
6867 || eap->cmdidx == CMD_view))
6868 {
6869 exmode_active = FALSE;
6870 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006871 {
6872 /* Special case: ":global/pat/visual\NLvi-commands" */
6873 if (global_busy)
6874 {
6875 int rd = RedrawingDisabled;
6876 int nwr = no_wait_return;
6877 int ms = msg_scroll;
6878#ifdef FEAT_GUI
6879 int he = hold_gui_events;
6880#endif
6881
6882 if (eap->nextcmd != NULL)
6883 {
6884 stuffReadbuff(eap->nextcmd);
6885 eap->nextcmd = NULL;
6886 }
6887
6888 if (exmode_was != EXMODE_VIM)
6889 settmode(TMODE_RAW);
6890 RedrawingDisabled = 0;
6891 no_wait_return = 0;
6892 need_wait_return = FALSE;
6893 msg_scroll = 0;
6894#ifdef FEAT_GUI
6895 hold_gui_events = 0;
6896#endif
6897 must_redraw = CLEAR;
6898
6899 main_loop(FALSE, TRUE);
6900
6901 RedrawingDisabled = rd;
6902 no_wait_return = nwr;
6903 msg_scroll = ms;
6904#ifdef FEAT_GUI
6905 hold_gui_events = he;
6906#endif
6907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 }
6911
6912 if ((eap->cmdidx == CMD_new
6913#ifdef FEAT_VERTSPLIT
6914 || eap->cmdidx == CMD_vnew
6915#endif
6916 ) && *eap->arg == NUL)
6917 {
6918 /* ":new" without argument: edit an new empty buffer */
6919 setpcmark();
6920 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
6921 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
6922 }
6923 else if ((eap->cmdidx != CMD_split
6924#ifdef FEAT_VERTSPLIT
6925 && eap->cmdidx != CMD_vsplit
6926#endif
6927 )
6928 || *eap->arg != NUL
6929#ifdef FEAT_BROWSE
6930 || cmdmod.browse
6931#endif
6932 )
6933 {
6934 n = readonlymode;
6935 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6936 readonlymode = TRUE;
6937 else if (eap->cmdidx == CMD_enew)
6938 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
6939 empty buffer */
6940 setpcmark();
6941 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6942 NULL, eap,
6943 /* ":edit" goes to first line if Vi compatible */
6944 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6945 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6946 ? ECMD_ONE : eap->do_ecmd_lnum,
6947 (P_HID(curbuf) ? ECMD_HIDE : 0)
6948 + (eap->forceit ? ECMD_FORCEIT : 0)
6949#ifdef FEAT_LISTCMDS
6950 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
6951#endif
6952 ) == FAIL)
6953 {
6954 /* Editing the file failed. If the window was split, close it. */
6955#ifdef FEAT_WINDOWS
6956 if (old_curwin != NULL)
6957 {
6958 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
6959 if (!need_hide || P_HID(curbuf))
6960 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006961# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6962 cleanup_T cs;
6963
6964 /* Reset the error/interrupt/exception state here so that
6965 * aborting() returns FALSE when closing a window. */
6966 enter_cleanup(&cs);
6967# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968# ifdef FEAT_GUI
6969 need_mouse_correct = TRUE;
6970# endif
6971 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006972
6973# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6974 /* Restore the error/interrupt/exception state if not
6975 * discarded by a new aborting error, interrupt, or
6976 * uncaught exception. */
6977 leave_cleanup(&cs);
6978# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 }
6980 }
6981#endif
6982 }
6983 else if (readonlymode && curbuf->b_nwindows == 1)
6984 {
6985 /* When editing an already visited buffer, 'readonly' won't be set
6986 * but the previous value is kept. With ":view" and ":sview" we
6987 * want the file to be readonly, except when another window is
6988 * editing the same buffer. */
6989 curbuf->b_p_ro = TRUE;
6990 }
6991 readonlymode = n;
6992 }
6993 else
6994 {
6995 if (eap->do_ecmd_cmd != NULL)
6996 do_cmdline_cmd(eap->do_ecmd_cmd);
6997#ifdef FEAT_TITLE
6998 n = curwin->w_arg_idx_invalid;
6999#endif
7000 check_arg_idx(curwin);
7001#ifdef FEAT_TITLE
7002 if (n != curwin->w_arg_idx_invalid)
7003 maketitle();
7004#endif
7005 }
7006
7007#ifdef FEAT_WINDOWS
7008 /*
7009 * if ":split file" worked, set alternate file name in old window to new
7010 * file
7011 */
7012 if (old_curwin != NULL
7013 && *eap->arg != NUL
7014 && curwin != old_curwin
7015 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007016 && old_curwin->w_buffer != curbuf
7017 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 old_curwin->w_alt_fnum = curbuf->b_fnum;
7019#endif
7020
7021 ex_no_reprint = TRUE;
7022}
7023
7024#ifndef FEAT_GUI
7025/*
7026 * ":gui" and ":gvim" when there is no GUI.
7027 */
7028 static void
7029ex_nogui(eap)
7030 exarg_T *eap;
7031{
7032 eap->errmsg = e_nogvim;
7033}
7034#endif
7035
7036#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7037 static void
7038ex_tearoff(eap)
7039 exarg_T *eap;
7040{
7041 gui_make_tearoff(eap->arg);
7042}
7043#endif
7044
Bram Moolenaar843ee412004-06-30 16:16:41 +00007045#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_KDE) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 static void
7047ex_popup(eap)
7048 exarg_T *eap;
7049{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007050 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051}
7052#endif
7053
7054/*ARGSUSED*/
7055 static void
7056ex_swapname(eap)
7057 exarg_T *eap;
7058{
7059 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7060 MSG(_("No swap file"));
7061 else
7062 msg(curbuf->b_ml.ml_mfp->mf_fname);
7063}
7064
7065/*
7066 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7067 * offset.
7068 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7069 */
7070/*ARGSUSED*/
7071 static void
7072ex_syncbind(eap)
7073 exarg_T *eap;
7074{
7075#ifdef FEAT_SCROLLBIND
7076 win_T *wp;
7077 long topline;
7078 long y;
7079 linenr_T old_linenr = curwin->w_cursor.lnum;
7080
7081 setpcmark();
7082
7083 /*
7084 * determine max topline
7085 */
7086 if (curwin->w_p_scb)
7087 {
7088 topline = curwin->w_topline;
7089 for (wp = firstwin; wp; wp = wp->w_next)
7090 {
7091 if (wp->w_p_scb && wp->w_buffer)
7092 {
7093 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7094 if (topline > y)
7095 topline = y;
7096 }
7097 }
7098 if (topline < 1)
7099 topline = 1;
7100 }
7101 else
7102 {
7103 topline = 1;
7104 }
7105
7106
7107 /*
7108 * set all scrollbind windows to the same topline
7109 */
7110 wp = curwin;
7111 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7112 {
7113 if (curwin->w_p_scb)
7114 {
7115 y = topline - curwin->w_topline;
7116 if (y > 0)
7117 scrollup(y, TRUE);
7118 else
7119 scrolldown(-y, TRUE);
7120 curwin->w_scbind_pos = topline;
7121 redraw_later(VALID);
7122 cursor_correct();
7123#ifdef FEAT_WINDOWS
7124 curwin->w_redr_status = TRUE;
7125#endif
7126 }
7127 }
7128 curwin = wp;
7129 if (curwin->w_p_scb)
7130 {
7131 did_syncbind = TRUE;
7132 checkpcmark();
7133 if (old_linenr != curwin->w_cursor.lnum)
7134 {
7135 char_u ctrl_o[2];
7136
7137 ctrl_o[0] = Ctrl_O;
7138 ctrl_o[1] = 0;
7139 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7140 }
7141 }
7142#endif
7143}
7144
7145
7146 static void
7147ex_read(eap)
7148 exarg_T *eap;
7149{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007150 int i;
7151 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7152 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
7154 if (eap->usefilter) /* :r!cmd */
7155 do_bang(1, eap, FALSE, FALSE, TRUE);
7156 else
7157 {
7158 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7159 return;
7160
7161#ifdef FEAT_BROWSE
7162 if (cmdmod.browse)
7163 {
7164 char_u *browseFile;
7165
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007166 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 NULL, NULL, NULL, curbuf);
7168 if (browseFile != NULL)
7169 {
7170 i = readfile(browseFile, NULL,
7171 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7172 vim_free(browseFile);
7173 }
7174 else
7175 i = OK;
7176 }
7177 else
7178#endif
7179 if (*eap->arg == NUL)
7180 {
7181 if (check_fname() == FAIL) /* check for no file name */
7182 return;
7183 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7184 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7185 }
7186 else
7187 {
7188 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7189 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7190 i = readfile(eap->arg, NULL,
7191 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7192
7193 }
7194 if (i == FAIL)
7195 {
7196#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7197 if (!aborting())
7198#endif
7199 EMSG2(_(e_notopen), eap->arg);
7200 }
7201 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007202 {
7203 if (empty && exmode_active)
7204 {
7205 /* Delete the empty line that remains. Historically ex does
7206 * this but vi doesn't. */
7207 if (eap->line2 == 0)
7208 lnum = curbuf->b_ml.ml_line_count;
7209 else
7210 lnum = 1;
7211 if (*ml_get(lnum) == NUL)
7212 {
7213 ml_delete(lnum, FALSE);
7214 deleted_lines_mark(lnum, 1L);
7215 if (curwin->w_cursor.lnum >= lnum)
7216 --curwin->w_cursor.lnum;
7217 }
7218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 }
7222}
7223
Bram Moolenaarea408852005-06-25 22:49:46 +00007224static char_u *prev_dir = NULL;
7225
7226#if defined(EXITFREE) || defined(PROTO)
7227 void
7228free_cd_dir()
7229{
7230 vim_free(prev_dir);
7231}
7232#endif
7233
7234
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235/*
7236 * ":cd", ":lcd", ":chdir" and ":lchdir".
7237 */
7238 static void
7239ex_cd(eap)
7240 exarg_T *eap;
7241{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 char_u *new_dir;
7243 char_u *tofree;
7244
7245 new_dir = eap->arg;
7246#if !defined(UNIX) && !defined(VMS)
7247 /* for non-UNIX ":cd" means: print current directory */
7248 if (*new_dir == NUL)
7249 ex_pwd(NULL);
7250 else
7251#endif
7252 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007253 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7254 && !eap->forceit)
7255 {
7256 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7257 return;
7258 }
7259
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 /* ":cd -": Change to previous directory */
7261 if (STRCMP(new_dir, "-") == 0)
7262 {
7263 if (prev_dir == NULL)
7264 {
7265 EMSG(_("E186: No previous directory"));
7266 return;
7267 }
7268 new_dir = prev_dir;
7269 }
7270
7271 /* Save current directory for next ":cd -" */
7272 tofree = prev_dir;
7273 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7274 prev_dir = vim_strsave(NameBuff);
7275 else
7276 prev_dir = NULL;
7277
7278#if defined(UNIX) || defined(VMS)
7279 /* for UNIX ":cd" means: go to home directory */
7280 if (*new_dir == NUL)
7281 {
7282 /* use NameBuff for home directory name */
7283# ifdef VMS
7284 char_u *p;
7285
7286 p = mch_getenv((char_u *)"SYS$LOGIN");
7287 if (p == NULL || *p == NUL) /* empty is the same as not set */
7288 NameBuff[0] = NUL;
7289 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007290 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291# else
7292 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7293# endif
7294 new_dir = NameBuff;
7295 }
7296#endif
7297 if (new_dir == NULL || vim_chdir(new_dir))
7298 EMSG(_(e_failed));
7299 else
7300 {
7301 vim_free(curwin->w_localdir);
7302 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7303 {
7304 /* If still in global directory, need to remember current
7305 * directory as global directory. */
7306 if (globaldir == NULL && prev_dir != NULL)
7307 globaldir = vim_strsave(prev_dir);
7308 /* Remember this local directory for the window. */
7309 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7310 curwin->w_localdir = vim_strsave(NameBuff);
7311 }
7312 else
7313 {
7314 /* We are now in the global directory, no need to remember its
7315 * name. */
7316 vim_free(globaldir);
7317 globaldir = NULL;
7318 curwin->w_localdir = NULL;
7319 }
7320
7321 shorten_fnames(TRUE);
7322
7323 /* Echo the new current directory if the command was typed. */
7324 if (KeyTyped)
7325 ex_pwd(eap);
7326 }
7327 vim_free(tofree);
7328 }
7329}
7330
7331/*
7332 * ":pwd".
7333 */
7334/*ARGSUSED*/
7335 static void
7336ex_pwd(eap)
7337 exarg_T *eap;
7338{
7339 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7340 {
7341#ifdef BACKSLASH_IN_FILENAME
7342 slash_adjust(NameBuff);
7343#endif
7344 msg(NameBuff);
7345 }
7346 else
7347 EMSG(_("E187: Unknown"));
7348}
7349
7350/*
7351 * ":=".
7352 */
7353 static void
7354ex_equal(eap)
7355 exarg_T *eap;
7356{
7357 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007358 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359}
7360
7361 static void
7362ex_sleep(eap)
7363 exarg_T *eap;
7364{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007365 int n;
7366 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367
7368 if (cursor_valid())
7369 {
7370 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7371 if (n >= 0)
7372 windgoto((int)n, curwin->w_wcol);
7373 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007374
7375 len = eap->line2;
7376 switch (*eap->arg)
7377 {
7378 case 'm': break;
7379 case NUL: len *= 1000L; break;
7380 default: EMSG2(_(e_invarg2), eap->arg); return;
7381 }
7382 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383}
7384
7385/*
7386 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7387 */
7388 void
7389do_sleep(msec)
7390 long msec;
7391{
7392 long done;
7393
7394 cursor_on();
7395 out_flush();
7396 for (done = 0; !got_int && done < msec; done += 1000L)
7397 {
7398 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7399 ui_breakcheck();
7400 }
7401}
7402
7403 static void
7404do_exmap(eap, isabbrev)
7405 exarg_T *eap;
7406 int isabbrev;
7407{
7408 int mode;
7409 char_u *cmdp;
7410
7411 cmdp = eap->cmd;
7412 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7413
7414 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7415 eap->arg, mode, isabbrev))
7416 {
7417 case 1: EMSG(_(e_invarg));
7418 break;
7419 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7420 break;
7421 }
7422}
7423
7424/*
7425 * ":winsize" command (obsolete).
7426 */
7427 static void
7428ex_winsize(eap)
7429 exarg_T *eap;
7430{
7431 int w, h;
7432 char_u *arg = eap->arg;
7433 char_u *p;
7434
7435 w = getdigits(&arg);
7436 arg = skipwhite(arg);
7437 p = arg;
7438 h = getdigits(&arg);
7439 if (*p != NUL && *arg == NUL)
7440 set_shellsize(w, h, TRUE);
7441 else
7442 EMSG(_("E465: :winsize requires two number arguments"));
7443}
7444
7445#ifdef FEAT_WINDOWS
7446 static void
7447ex_wincmd(eap)
7448 exarg_T *eap;
7449{
7450 int xchar = NUL;
7451 char_u *p;
7452
7453 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7454 {
7455 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7456 if (eap->arg[1] == NUL)
7457 {
7458 EMSG(_(e_invarg));
7459 return;
7460 }
7461 xchar = eap->arg[1];
7462 p = eap->arg + 2;
7463 }
7464 else
7465 p = eap->arg + 1;
7466
7467 eap->nextcmd = check_nextcmd(p);
7468 p = skipwhite(p);
7469 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7470 EMSG(_(e_invarg));
7471 else
7472 {
7473 /* Pass flags on for ":vertical wincmd ]". */
7474 postponed_split_flags = cmdmod.split;
7475 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7476 postponed_split_flags = 0;
7477 }
7478}
7479#endif
7480
Bram Moolenaar843ee412004-06-30 16:16:41 +00007481#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482/*
7483 * ":winpos".
7484 */
7485 static void
7486ex_winpos(eap)
7487 exarg_T *eap;
7488{
7489 int x, y;
7490 char_u *arg = eap->arg;
7491 char_u *p;
7492
7493 if (*arg == NUL)
7494 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007495# if defined(FEAT_GUI) || defined(MSWIN)
7496# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007498# else
7499 if (mch_get_winpos(&x, &y) != FAIL)
7500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 {
7502 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7503 msg(IObuff);
7504 }
7505 else
7506# endif
7507 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7508 }
7509 else
7510 {
7511 x = getdigits(&arg);
7512 arg = skipwhite(arg);
7513 p = arg;
7514 y = getdigits(&arg);
7515 if (*p == NUL || *arg != NUL)
7516 {
7517 EMSG(_("E466: :winpos requires two number arguments"));
7518 return;
7519 }
7520# ifdef FEAT_GUI
7521 if (gui.in_use)
7522 gui_mch_set_winpos(x, y);
7523 else if (gui.starting)
7524 {
7525 /* Remember the coordinates for when the window is opened. */
7526 gui_win_x = x;
7527 gui_win_y = y;
7528 }
7529# ifdef HAVE_TGETENT
7530 else
7531# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007532# else
7533# ifdef MSWIN
7534 mch_set_winpos(x, y);
7535# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536# endif
7537# ifdef HAVE_TGETENT
7538 if (*T_CWP)
7539 term_set_winpos(x, y);
7540# endif
7541 }
7542}
7543#endif
7544
7545/*
7546 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7547 */
7548 static void
7549ex_operators(eap)
7550 exarg_T *eap;
7551{
7552 oparg_T oa;
7553
7554 clear_oparg(&oa);
7555 oa.regname = eap->regname;
7556 oa.start.lnum = eap->line1;
7557 oa.end.lnum = eap->line2;
7558 oa.line_count = eap->line2 - eap->line1 + 1;
7559 oa.motion_type = MLINE;
7560#ifdef FEAT_VIRTUALEDIT
7561 virtual_op = FALSE;
7562#endif
7563 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7564 {
7565 setpcmark();
7566 curwin->w_cursor.lnum = eap->line1;
7567 beginline(BL_SOL | BL_FIX);
7568 }
7569
7570 switch (eap->cmdidx)
7571 {
7572 case CMD_delete:
7573 oa.op_type = OP_DELETE;
7574 op_delete(&oa);
7575 break;
7576
7577 case CMD_yank:
7578 oa.op_type = OP_YANK;
7579 (void)op_yank(&oa, FALSE, TRUE);
7580 break;
7581
7582 default: /* CMD_rshift or CMD_lshift */
7583 if ((eap->cmdidx == CMD_rshift)
7584#ifdef FEAT_RIGHTLEFT
7585 ^ curwin->w_p_rl
7586#endif
7587 )
7588 oa.op_type = OP_RSHIFT;
7589 else
7590 oa.op_type = OP_LSHIFT;
7591 op_shift(&oa, FALSE, eap->amount);
7592 break;
7593 }
7594#ifdef FEAT_VIRTUALEDIT
7595 virtual_op = MAYBE;
7596#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007597 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598}
7599
7600/*
7601 * ":put".
7602 */
7603 static void
7604ex_put(eap)
7605 exarg_T *eap;
7606{
7607 /* ":0put" works like ":1put!". */
7608 if (eap->line2 == 0)
7609 {
7610 eap->line2 = 1;
7611 eap->forceit = TRUE;
7612 }
7613 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007614 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7615 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616}
7617
7618/*
7619 * Handle ":copy" and ":move".
7620 */
7621 static void
7622ex_copymove(eap)
7623 exarg_T *eap;
7624{
7625 long n;
7626
7627 n = get_address(&eap->arg, FALSE, FALSE);
7628 if (eap->arg == NULL) /* error detected */
7629 {
7630 eap->nextcmd = NULL;
7631 return;
7632 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007633 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634
7635 /*
7636 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7637 */
7638 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7639 {
7640 EMSG(_(e_invaddr));
7641 return;
7642 }
7643
7644 if (eap->cmdidx == CMD_move)
7645 {
7646 if (do_move(eap->line1, eap->line2, n) == FAIL)
7647 return;
7648 }
7649 else
7650 ex_copy(eap->line1, eap->line2, n);
7651 u_clearline();
7652 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007653 ex_may_print(eap);
7654}
7655
7656/*
7657 * Print the current line if flags were given to the Ex command.
7658 */
7659 static void
7660ex_may_print(eap)
7661 exarg_T *eap;
7662{
7663 if (eap->flags != 0)
7664 {
7665 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7666 (eap->flags & EXFLAG_LIST));
7667 ex_no_reprint = TRUE;
7668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669}
7670
7671/*
7672 * ":smagic" and ":snomagic".
7673 */
7674 static void
7675ex_submagic(eap)
7676 exarg_T *eap;
7677{
7678 int magic_save = p_magic;
7679
7680 p_magic = (eap->cmdidx == CMD_smagic);
7681 do_sub(eap);
7682 p_magic = magic_save;
7683}
7684
7685/*
7686 * ":join".
7687 */
7688 static void
7689ex_join(eap)
7690 exarg_T *eap;
7691{
7692 curwin->w_cursor.lnum = eap->line1;
7693 if (eap->line1 == eap->line2)
7694 {
7695 if (eap->addr_count >= 2) /* :2,2join does nothing */
7696 return;
7697 if (eap->line2 == curbuf->b_ml.ml_line_count)
7698 {
7699 beep_flush();
7700 return;
7701 }
7702 ++eap->line2;
7703 }
7704 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
7705 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007706 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707}
7708
7709/*
7710 * ":[addr]@r" or ":[addr]*r": execute register
7711 */
7712 static void
7713ex_at(eap)
7714 exarg_T *eap;
7715{
7716 int c;
7717
7718 curwin->w_cursor.lnum = eap->line2;
7719
7720#ifdef USE_ON_FLY_SCROLL
7721 dont_scroll = TRUE; /* disallow scrolling here */
7722#endif
7723
7724 /* get the register name. No name means to use the previous one */
7725 c = *eap->arg;
7726 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7727 c = '@';
7728 /* put the register in mapbuf */
7729 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007730 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 else
7734 {
7735 int save_efr = exec_from_reg;
7736
7737 exec_from_reg = TRUE;
7738
7739 /*
7740 * Execute from the typeahead buffer.
7741 * Originally this didn't check for the typeahead buffer to be empty,
7742 * thus could read more Ex commands from stdin. It's not clear why,
7743 * it is certainly unexpected.
7744 */
7745 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
7746 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7747
7748 exec_from_reg = save_efr;
7749 }
7750}
7751
7752/*
7753 * ":!".
7754 */
7755 static void
7756ex_bang(eap)
7757 exarg_T *eap;
7758{
7759 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7760}
7761
7762/*
7763 * ":undo".
7764 */
7765/*ARGSUSED*/
7766 static void
7767ex_undo(eap)
7768 exarg_T *eap;
7769{
7770 u_undo(1);
7771}
7772
7773/*
7774 * ":redo".
7775 */
7776/*ARGSUSED*/
7777 static void
7778ex_redo(eap)
7779 exarg_T *eap;
7780{
7781 u_redo(1);
7782}
7783
7784/*
7785 * ":redir": start/stop redirection.
7786 */
7787 static void
7788ex_redir(eap)
7789 exarg_T *eap;
7790{
7791 char *mode;
7792 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007793 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794
7795 if (STRICMP(eap->arg, "END") == 0)
7796 close_redir();
7797 else
7798 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007799 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007801 ++arg;
7802 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007804 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 mode = "a";
7806 }
7807 else
7808 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007809 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810
7811 close_redir();
7812
7813 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00007814 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 if (fname == NULL)
7816 return;
7817#ifdef FEAT_BROWSE
7818 if (cmdmod.browse)
7819 {
7820 char_u *browseFile;
7821
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007822 browseFile = do_browse(BROWSE_SAVE,
7823 (char_u *)_("Save Redirection"),
7824 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 if (browseFile == NULL)
7826 return; /* operation cancelled */
7827 vim_free(fname);
7828 fname = browseFile;
7829 eap->forceit = TRUE; /* since dialog already asked */
7830 }
7831#endif
7832
7833 redir_fd = open_exfile(fname, eap->forceit, mode);
7834 vim_free(fname);
7835 }
7836#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007837 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 {
7839 /* redirect to a register a-z (resp. A-Z for appending) */
7840 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007841 ++arg;
7842 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007844 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007846 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007848 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00007849 if (*arg == '>' && arg[1] == '>')
7850 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007851 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
7852 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007854 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007856 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007858 if (*arg == '>')
7859 arg++;
7860
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 /* make register empty */
7862 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
7863 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007864 }
7865 if (*arg != NUL)
7866 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007867 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00007868 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007869 }
7870 }
7871 else if (*arg == '=' && arg[1] == '>')
7872 {
7873 int append;
7874
7875 /* redirect to a variable */
7876 close_redir();
7877 arg += 2;
7878
7879 if (*arg == '>')
7880 {
7881 ++arg;
7882 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 }
7884 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007885 append = FALSE;
7886
7887 if (var_redir_start(skipwhite(arg), append) == OK)
7888 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 }
7890#endif
7891
7892 /* TODO: redirect to a buffer */
7893
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 else
Bram Moolenaarca472992005-01-21 11:46:23 +00007895 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 }
7897}
7898
7899/*
7900 * ":redraw": force redraw
7901 */
7902 static void
7903ex_redraw(eap)
7904 exarg_T *eap;
7905{
7906 int r = RedrawingDisabled;
7907 int p = p_lz;
7908
7909 RedrawingDisabled = 0;
7910 p_lz = FALSE;
7911 update_topline();
7912 update_screen(eap->forceit ? CLEAR :
7913#ifdef FEAT_VISUAL
7914 VIsual_active ? INVERTED :
7915#endif
7916 0);
7917#ifdef FEAT_TITLE
7918 if (need_maketitle)
7919 maketitle();
7920#endif
7921 RedrawingDisabled = r;
7922 p_lz = p;
7923
7924 /* Reset msg_didout, so that a message that's there is overwritten. */
7925 msg_didout = FALSE;
7926 msg_col = 0;
7927
7928 out_flush();
7929}
7930
7931/*
7932 * ":redrawstatus": force redraw of status line(s)
7933 */
7934/*ARGSUSED*/
7935 static void
7936ex_redrawstatus(eap)
7937 exarg_T *eap;
7938{
7939#if defined(FEAT_WINDOWS)
7940 int r = RedrawingDisabled;
7941 int p = p_lz;
7942
7943 RedrawingDisabled = 0;
7944 p_lz = FALSE;
7945 if (eap->forceit)
7946 status_redraw_all();
7947 else
7948 status_redraw_curbuf();
7949 update_screen(
7950# ifdef FEAT_VISUAL
7951 VIsual_active ? INVERTED :
7952# endif
7953 0);
7954 RedrawingDisabled = r;
7955 p_lz = p;
7956 out_flush();
7957#endif
7958}
7959
7960 static void
7961close_redir()
7962{
7963 if (redir_fd != NULL)
7964 {
7965 fclose(redir_fd);
7966 redir_fd = NULL;
7967 }
7968#ifdef FEAT_EVAL
7969 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007970 if (redir_vname)
7971 {
7972 var_redir_stop();
7973 redir_vname = 0;
7974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975#endif
7976}
7977
7978#if defined(FEAT_SESSION) && defined(USE_CRNL)
7979# define MKSESSION_NL
7980static int mksession_nl = FALSE; /* use NL only in put_eol() */
7981#endif
7982
7983/*
7984 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
7985 */
7986 static void
7987ex_mkrc(eap)
7988 exarg_T *eap;
7989{
7990 FILE *fd;
7991 int failed = FALSE;
7992 char_u *fname;
7993#ifdef FEAT_BROWSE
7994 char_u *browseFile = NULL;
7995#endif
7996#ifdef FEAT_SESSION
7997 int view_session = FALSE;
7998 int using_vdir = FALSE; /* using 'viewdir'? */
7999 char_u *viewFile = NULL;
8000 unsigned *flagp;
8001#endif
8002
8003 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8004 {
8005#ifdef FEAT_SESSION
8006 view_session = TRUE;
8007#else
8008 ex_ni(eap);
8009 return;
8010#endif
8011 }
8012
8013#ifdef FEAT_SESSION
8014 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8015 if (eap->cmdidx == CMD_mkview
8016 && (*eap->arg == NUL
8017 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8018 {
8019 eap->forceit = TRUE;
8020 fname = get_view_file(*eap->arg);
8021 if (fname == NULL)
8022 return;
8023 viewFile = fname;
8024 using_vdir = TRUE;
8025 }
8026 else
8027#endif
8028 if (*eap->arg != NUL)
8029 fname = eap->arg;
8030 else if (eap->cmdidx == CMD_mkvimrc)
8031 fname = (char_u *)VIMRC_FILE;
8032#ifdef FEAT_SESSION
8033 else if (eap->cmdidx == CMD_mksession)
8034 fname = (char_u *)SESSION_FILE;
8035#endif
8036 else
8037 fname = (char_u *)EXRC_FILE;
8038
8039#ifdef FEAT_BROWSE
8040 if (cmdmod.browse)
8041 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008042 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043# ifdef FEAT_SESSION
8044 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8045 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8046# endif
8047 (char_u *)_("Save Setup"),
8048 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8049 if (browseFile == NULL)
8050 goto theend;
8051 fname = browseFile;
8052 eap->forceit = TRUE; /* since dialog already asked */
8053 }
8054#endif
8055
8056#if defined(FEAT_SESSION) && defined(vim_mkdir)
8057 /* When using 'viewdir' may have to create the directory. */
8058 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008059 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060#endif
8061
8062 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8063 if (fd != NULL)
8064 {
8065#ifdef FEAT_SESSION
8066 if (eap->cmdidx == CMD_mkview)
8067 flagp = &vop_flags;
8068 else
8069 flagp = &ssop_flags;
8070#endif
8071
8072#ifdef MKSESSION_NL
8073 /* "unix" in 'sessionoptions': use NL line separator */
8074 if (view_session && (*flagp & SSOP_UNIX))
8075 mksession_nl = TRUE;
8076#endif
8077
8078 /* Write the version command for :mkvimrc */
8079 if (eap->cmdidx == CMD_mkvimrc)
8080 (void)put_line(fd, "version 6.0");
8081
8082#ifdef FEAT_SESSION
8083 if (eap->cmdidx != CMD_mkview)
8084#endif
8085 {
8086 /* Write setting 'compatible' first, because it has side effects.
8087 * For that same reason only do it when needed. */
8088 if (p_cp)
8089 (void)put_line(fd, "if !&cp | set cp | endif");
8090 else
8091 (void)put_line(fd, "if &cp | set nocp | endif");
8092 }
8093
8094#ifdef FEAT_SESSION
8095 if (!view_session
8096 || (eap->cmdidx == CMD_mksession
8097 && (*flagp & SSOP_OPTIONS)))
8098#endif
8099 failed |= (makemap(fd, NULL) == FAIL
8100 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8101
8102#ifdef FEAT_SESSION
8103 if (!failed && view_session)
8104 {
8105 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8106 failed = TRUE;
8107 if (eap->cmdidx == CMD_mksession)
8108 {
8109 char_u dirnow[MAXPATHL]; /* current directory */
8110
8111 /*
8112 * Change to session file's dir.
8113 */
8114 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8115 || mch_chdir((char *)dirnow) != 0)
8116 *dirnow = NUL;
8117 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8118 {
8119 if (vim_chdirfile(fname) == OK)
8120 shorten_fnames(TRUE);
8121 }
8122 else if (*dirnow != NUL
8123 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8124 {
8125 (void)mch_chdir((char *)globaldir);
8126 shorten_fnames(TRUE);
8127 }
8128
8129 failed |= (makeopens(fd, dirnow) == FAIL);
8130
8131 /* restore original dir */
8132 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8133 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8134 {
8135 if (mch_chdir((char *)dirnow) != 0)
8136 EMSG(_(e_prev_dir));
8137 shorten_fnames(TRUE);
8138 }
8139 }
8140 else
8141 {
8142 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8143 }
8144 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8145 == FAIL)
8146 failed = TRUE;
8147 }
8148#endif
8149 failed |= fclose(fd);
8150
8151 if (failed)
8152 EMSG(_(e_write));
8153#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8154 else if (eap->cmdidx == CMD_mksession)
8155 {
8156 /* successful session write - set this_session var */
8157 char_u tbuf[MAXPATHL];
8158
8159 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8160 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8161 }
8162#endif
8163#ifdef MKSESSION_NL
8164 mksession_nl = FALSE;
8165#endif
8166 }
8167
8168#ifdef FEAT_BROWSE
8169theend:
8170 vim_free(browseFile);
8171#endif
8172#ifdef FEAT_SESSION
8173 vim_free(viewFile);
8174#endif
8175}
8176
Bram Moolenaardf177f62005-02-22 08:39:57 +00008177#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8178 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008179/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008180 int
8181vim_mkdir_emsg(name, prot)
8182 char_u *name;
8183 int prot;
8184{
8185 if (vim_mkdir(name, prot) != 0)
8186 {
8187 EMSG2(_("E739: Cannot create directory: %s"), name);
8188 return FAIL;
8189 }
8190 return OK;
8191}
8192#endif
8193
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194/*
8195 * Open a file for writing for an Ex command, with some checks.
8196 * Return file descriptor, or NULL on failure.
8197 */
8198 FILE *
8199open_exfile(fname, forceit, mode)
8200 char_u *fname;
8201 int forceit;
8202 char *mode; /* "w" for create new file or "a" for append */
8203{
8204 FILE *fd;
8205
8206#ifdef UNIX
8207 /* with Unix it is possible to open a directory */
8208 if (mch_isdir(fname))
8209 {
8210 EMSG2(_(e_isadir2), fname);
8211 return NULL;
8212 }
8213#endif
8214 if (!forceit && *mode != 'a' && vim_fexists(fname))
8215 {
8216 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8217 return NULL;
8218 }
8219
8220 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8221 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8222
8223 return fd;
8224}
8225
8226/*
8227 * ":mark" and ":k".
8228 */
8229 static void
8230ex_mark(eap)
8231 exarg_T *eap;
8232{
8233 pos_T pos;
8234
8235 if (*eap->arg == NUL) /* No argument? */
8236 EMSG(_(e_argreq));
8237 else if (eap->arg[1] != NUL) /* more than one character? */
8238 EMSG(_(e_trailing));
8239 else
8240 {
8241 pos = curwin->w_cursor; /* save curwin->w_cursor */
8242 curwin->w_cursor.lnum = eap->line2;
8243 beginline(BL_WHITE | BL_FIX);
8244 if (setmark(*eap->arg) == FAIL) /* set mark */
8245 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8246 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8247 }
8248}
8249
8250/*
8251 * Update w_topline, w_leftcol and the cursor position.
8252 */
8253 void
8254update_topline_cursor()
8255{
8256 check_cursor(); /* put cursor on valid line */
8257 update_topline();
8258 if (!curwin->w_p_wrap)
8259 validate_cursor();
8260 update_curswant();
8261}
8262
8263#ifdef FEAT_EX_EXTRA
8264/*
8265 * ":normal[!] {commands}": Execute normal mode commands.
8266 */
8267 static void
8268ex_normal(eap)
8269 exarg_T *eap;
8270{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 int save_msg_scroll = msg_scroll;
8272 int save_restart_edit = restart_edit;
8273 int save_msg_didout = msg_didout;
8274 int save_State = State;
8275 tasave_T tabuf;
8276 int save_insertmode = p_im;
8277 int save_finish_op = finish_op;
8278#ifdef FEAT_MBYTE
8279 char_u *arg = NULL;
8280 int l;
8281 char_u *p;
8282#endif
8283
8284 if (ex_normal_busy >= p_mmd)
8285 {
8286 EMSG(_("E192: Recursive use of :normal too deep"));
8287 return;
8288 }
8289 ++ex_normal_busy;
8290
8291 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8292 restart_edit = 0; /* don't go to Insert mode */
8293 p_im = FALSE; /* don't use 'insertmode' */
8294
8295#ifdef FEAT_MBYTE
8296 /*
8297 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8298 * this for the K_SPECIAL leading byte, otherwise special keys will not
8299 * work.
8300 */
8301 if (has_mbyte)
8302 {
8303 int len = 0;
8304
8305 /* Count the number of characters to be escaped. */
8306 for (p = eap->arg; *p != NUL; ++p)
8307 {
8308# ifdef FEAT_GUI
8309 if (*p == CSI) /* leadbyte CSI */
8310 len += 2;
8311# endif
8312 for (l = (*mb_ptr2len_check)(p) - 1; l > 0; --l)
8313 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8314# ifdef FEAT_GUI
8315 || *p == CSI
8316# endif
8317 )
8318 len += 2;
8319 }
8320 if (len > 0)
8321 {
8322 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8323 if (arg != NULL)
8324 {
8325 len = 0;
8326 for (p = eap->arg; *p != NUL; ++p)
8327 {
8328 arg[len++] = *p;
8329# ifdef FEAT_GUI
8330 if (*p == CSI)
8331 {
8332 arg[len++] = KS_EXTRA;
8333 arg[len++] = (int)KE_CSI;
8334 }
8335# endif
8336 for (l = (*mb_ptr2len_check)(p) - 1; l > 0; --l)
8337 {
8338 arg[len++] = *++p;
8339 if (*p == K_SPECIAL)
8340 {
8341 arg[len++] = KS_SPECIAL;
8342 arg[len++] = KE_FILLER;
8343 }
8344# ifdef FEAT_GUI
8345 else if (*p == CSI)
8346 {
8347 arg[len++] = KS_EXTRA;
8348 arg[len++] = (int)KE_CSI;
8349 }
8350# endif
8351 }
8352 arg[len] = NUL;
8353 }
8354 }
8355 }
8356 }
8357#endif
8358
8359 /*
8360 * Save the current typeahead. This is required to allow using ":normal"
8361 * from an event handler and makes sure we don't hang when the argument
8362 * ends with half a command.
8363 */
8364 save_typeahead(&tabuf);
8365 if (tabuf.typebuf_valid)
8366 {
8367 /*
8368 * Repeat the :normal command for each line in the range. When no
8369 * range given, execute it just once, without positioning the cursor
8370 * first.
8371 */
8372 do
8373 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 if (eap->addr_count != 0)
8375 {
8376 curwin->w_cursor.lnum = eap->line1++;
8377 curwin->w_cursor.col = 0;
8378 }
8379
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008380 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381#ifdef FEAT_MBYTE
8382 arg != NULL ? arg :
8383#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008384 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 }
8386 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8387 }
8388
8389 /* Might not return to the main loop when in an event handler. */
8390 update_topline_cursor();
8391
8392 /* Restore the previous typeahead. */
8393 restore_typeahead(&tabuf);
8394
8395 --ex_normal_busy;
8396 msg_scroll = save_msg_scroll;
8397 restart_edit = save_restart_edit;
8398 p_im = save_insertmode;
8399 finish_op = save_finish_op;
8400 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8401
8402 /* Restore the state (needed when called from a function executed for
8403 * 'indentexpr'). */
8404 State = save_State;
8405#ifdef FEAT_MBYTE
8406 vim_free(arg);
8407#endif
8408}
8409
8410/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008411 * ":startinsert" and ":startreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 */
8413 static void
8414ex_startinsert(eap)
8415 exarg_T *eap;
8416{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008417 if (eap->forceit)
8418 {
8419 coladvance((colnr_T)MAXCOL);
8420 curwin->w_curswant = MAXCOL;
8421 curwin->w_set_curswant = FALSE;
8422 }
8423
Bram Moolenaara40c5002005-01-09 21:16:21 +00008424 /* Ignore the command when already in Insert mode. Inserting an
8425 * expression register that invokes a function can do this. */
8426 if (State & INSERT)
8427 return;
8428
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 if (eap->forceit)
8430 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008431 if (eap->cmdidx == CMD_startinsert)
8432 restart_edit = 'a';
8433 else
8434 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 }
8436 else
8437 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008438 if (eap->cmdidx == CMD_startinsert)
8439 restart_edit = 'i';
8440 else
8441 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 curwin->w_curswant = 0; /* avoid MAXCOL */
8443 }
8444}
8445
8446/*
8447 * ":stopinsert"
8448 */
8449/*ARGSUSED*/
8450 static void
8451ex_stopinsert(eap)
8452 exarg_T *eap;
8453{
8454 restart_edit = 0;
8455 stop_insert_mode = TRUE;
8456}
8457#endif
8458
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008459#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8460/*
8461 * Execute normal mode command "cmd".
8462 * "remap" can be REMAP_NONE or REMAP_YES.
8463 */
8464 void
8465exec_normal_cmd(cmd, remap, silent)
8466 char_u *cmd;
8467 int remap;
8468 int silent;
8469{
8470 oparg_T oa;
8471
8472 /*
8473 * Stuff the argument into the typeahead buffer.
8474 * Execute normal_cmd() until there is no typeahead left.
8475 */
8476 clear_oparg(&oa);
8477 finish_op = FALSE;
8478 ins_typebuf(cmd, remap, 0, TRUE, silent);
8479 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8480 && !got_int)
8481 {
8482 update_topline_cursor();
8483 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8484 }
8485}
8486#endif
8487
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488#ifdef FEAT_FIND_ID
8489 static void
8490ex_checkpath(eap)
8491 exarg_T *eap;
8492{
8493 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8494 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8495 (linenr_T)1, (linenr_T)MAXLNUM);
8496}
8497
8498#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8499/*
8500 * ":psearch"
8501 */
8502 static void
8503ex_psearch(eap)
8504 exarg_T *eap;
8505{
8506 g_do_tagpreview = p_pvh;
8507 ex_findpat(eap);
8508 g_do_tagpreview = 0;
8509}
8510#endif
8511
8512 static void
8513ex_findpat(eap)
8514 exarg_T *eap;
8515{
8516 int whole = TRUE;
8517 long n;
8518 char_u *p;
8519 int action;
8520
8521 switch (cmdnames[eap->cmdidx].cmd_name[2])
8522 {
8523 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8524 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8525 action = ACTION_GOTO;
8526 else
8527 action = ACTION_SHOW;
8528 break;
8529 case 'i': /* ":ilist" and ":dlist" */
8530 action = ACTION_SHOW_ALL;
8531 break;
8532 case 'u': /* ":ijump" and ":djump" */
8533 action = ACTION_GOTO;
8534 break;
8535 default: /* ":isplit" and ":dsplit" */
8536 action = ACTION_SPLIT;
8537 break;
8538 }
8539
8540 n = 1;
8541 if (vim_isdigit(*eap->arg)) /* get count */
8542 {
8543 n = getdigits(&eap->arg);
8544 eap->arg = skipwhite(eap->arg);
8545 }
8546 if (*eap->arg == '/') /* Match regexp, not just whole words */
8547 {
8548 whole = FALSE;
8549 ++eap->arg;
8550 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8551 if (*p)
8552 {
8553 *p++ = NUL;
8554 p = skipwhite(p);
8555
8556 /* Check for trailing illegal characters */
8557 if (!ends_excmd(*p))
8558 eap->errmsg = e_trailing;
8559 else
8560 eap->nextcmd = check_nextcmd(p);
8561 }
8562 }
8563 if (!eap->skip)
8564 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8565 whole, !eap->forceit,
8566 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8567 n, action, eap->line1, eap->line2);
8568}
8569#endif
8570
8571#ifdef FEAT_WINDOWS
8572
8573# ifdef FEAT_QUICKFIX
8574/*
8575 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8576 */
8577 static void
8578ex_ptag(eap)
8579 exarg_T *eap;
8580{
8581 g_do_tagpreview = p_pvh;
8582 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8583}
8584
8585/*
8586 * ":pedit"
8587 */
8588 static void
8589ex_pedit(eap)
8590 exarg_T *eap;
8591{
8592 win_T *curwin_save = curwin;
8593
8594 g_do_tagpreview = p_pvh;
8595 prepare_tagpreview();
8596 keep_help_flag = curwin_save->w_buffer->b_help;
8597 do_exedit(eap, NULL);
8598 keep_help_flag = FALSE;
8599 if (curwin != curwin_save && win_valid(curwin_save))
8600 {
8601 /* Return cursor to where we were */
8602 validate_cursor();
8603 redraw_later(VALID);
8604 win_enter(curwin_save, TRUE);
8605 }
8606 g_do_tagpreview = 0;
8607}
8608# endif
8609
8610/*
8611 * ":stag", ":stselect" and ":stjump".
8612 */
8613 static void
8614ex_stag(eap)
8615 exarg_T *eap;
8616{
8617 postponed_split = -1;
8618 postponed_split_flags = cmdmod.split;
8619 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8620 postponed_split_flags = 0;
8621}
8622#endif
8623
8624/*
8625 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8626 */
8627 static void
8628ex_tag(eap)
8629 exarg_T *eap;
8630{
8631 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8632}
8633
8634 static void
8635ex_tag_cmd(eap, name)
8636 exarg_T *eap;
8637 char_u *name;
8638{
8639 int cmd;
8640
8641 switch (name[1])
8642 {
8643 case 'j': cmd = DT_JUMP; /* ":tjump" */
8644 break;
8645 case 's': cmd = DT_SELECT; /* ":tselect" */
8646 break;
8647 case 'p': cmd = DT_PREV; /* ":tprevious" */
8648 break;
8649 case 'N': cmd = DT_PREV; /* ":tNext" */
8650 break;
8651 case 'n': cmd = DT_NEXT; /* ":tnext" */
8652 break;
8653 case 'o': cmd = DT_POP; /* ":pop" */
8654 break;
8655 case 'f': /* ":tfirst" */
8656 case 'r': cmd = DT_FIRST; /* ":trewind" */
8657 break;
8658 case 'l': cmd = DT_LAST; /* ":tlast" */
8659 break;
8660 default: /* ":tag" */
8661#ifdef FEAT_CSCOPE
8662 if (p_cst)
8663 {
8664 do_cstag(eap);
8665 return;
8666 }
8667#endif
8668 cmd = DT_TAG;
8669 break;
8670 }
8671
8672 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8673 eap->forceit, TRUE);
8674}
8675
8676/*
8677 * Evaluate cmdline variables.
8678 *
8679 * change '%' to curbuf->b_ffname
8680 * '#' to curwin->w_altfile
8681 * '<cword>' to word under the cursor
8682 * '<cWORD>' to WORD under the cursor
8683 * '<cfile>' to path name under the cursor
8684 * '<sfile>' to sourced file name
8685 * '<afile>' to file name for autocommand
8686 * '<abuf>' to buffer number for autocommand
8687 * '<amatch>' to matching name for autocommand
8688 *
8689 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8690 * "" for error without a message) and NULL is returned.
8691 * Returns an allocated string if a valid match was found.
8692 * Returns NULL if no match was found. "usedlen" then still contains the
8693 * number of characters to skip.
8694 */
8695 char_u *
8696eval_vars(src, usedlen, lnump, errormsg, srcstart)
8697 char_u *src; /* pointer into commandline */
8698 int *usedlen; /* characters after src that are used */
8699 linenr_T *lnump; /* line number for :e command, or NULL */
8700 char_u **errormsg; /* pointer to error message */
8701 char_u *srcstart; /* beginning of valid memory for src */
8702{
8703 int i;
8704 char_u *s;
8705 char_u *result;
8706 char_u *resultbuf = NULL;
8707 int resultlen;
8708 buf_T *buf;
8709 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
8710 int spec_idx;
8711#ifdef FEAT_MODIFY_FNAME
8712 int skip_mod = FALSE;
8713#endif
8714 static char *(spec_str[]) =
8715 {
8716 "%",
8717#define SPEC_PERC 0
8718 "#",
8719#define SPEC_HASH 1
8720 "<cword>", /* cursor word */
8721#define SPEC_CWORD 2
8722 "<cWORD>", /* cursor WORD */
8723#define SPEC_CCWORD 3
8724 "<cfile>", /* cursor path name */
8725#define SPEC_CFILE 4
8726 "<sfile>", /* ":so" file name */
8727#define SPEC_SFILE 5
8728#ifdef FEAT_AUTOCMD
8729 "<afile>", /* autocommand file name */
8730# define SPEC_AFILE 6
8731 "<abuf>", /* autocommand buffer number */
8732# define SPEC_ABUF 7
8733 "<amatch>", /* autocommand match name */
8734# define SPEC_AMATCH 8
8735#endif
8736#ifdef FEAT_CLIENTSERVER
8737 "<client>"
8738# define SPEC_CLIENT 9
8739#endif
8740 };
8741#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
8742
8743#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
8744 char_u strbuf[30];
8745#endif
8746
8747 *errormsg = NULL;
8748
8749 /*
8750 * Check if there is something to do.
8751 */
8752 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
8753 {
8754 *usedlen = (int)STRLEN(spec_str[spec_idx]);
8755 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
8756 break;
8757 }
8758 if (spec_idx == SPEC_COUNT) /* no match */
8759 {
8760 *usedlen = 1;
8761 return NULL;
8762 }
8763
8764 /*
8765 * Skip when preceded with a backslash "\%" and "\#".
8766 * Note: In "\\%" the % is also not recognized!
8767 */
8768 if (src > srcstart && src[-1] == '\\')
8769 {
8770 *usedlen = 0;
8771 STRCPY(src - 1, src); /* remove backslash */
8772 return NULL;
8773 }
8774
8775 /*
8776 * word or WORD under cursor
8777 */
8778 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
8779 {
8780 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
8781 (FIND_IDENT|FIND_STRING) : FIND_STRING);
8782 if (resultlen == 0)
8783 {
8784 *errormsg = (char_u *)"";
8785 return NULL;
8786 }
8787 }
8788
8789 /*
8790 * '#': Alternate file name
8791 * '%': Current file name
8792 * File name under the cursor
8793 * File name for autocommand
8794 * and following modifiers
8795 */
8796 else
8797 {
8798 switch (spec_idx)
8799 {
8800 case SPEC_PERC: /* '%': current file */
8801 if (curbuf->b_fname == NULL)
8802 {
8803 result = (char_u *)"";
8804 valid = 0; /* Must have ":p:h" to be valid */
8805 }
8806 else
8807#ifdef RISCOS
8808 /* Always use the full path for RISC OS if possible. */
8809 result = curbuf->b_ffname;
8810 if (result == NULL)
8811 result = curbuf->b_fname;
8812#else
8813 result = curbuf->b_fname;
8814#endif
8815 break;
8816
8817 case SPEC_HASH: /* '#' or "#99": alternate file */
8818 if (src[1] == '#') /* "##": the argument list */
8819 {
8820 result = arg_all();
8821 resultbuf = result;
8822 *usedlen = 2;
8823#ifdef FEAT_MODIFY_FNAME
8824 skip_mod = TRUE;
8825#endif
8826 break;
8827 }
8828 s = src + 1;
8829 i = (int)getdigits(&s);
8830 *usedlen = (int)(s - src); /* length of what we expand */
8831
8832 buf = buflist_findnr(i);
8833 if (buf == NULL)
8834 {
8835 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
8836 return NULL;
8837 }
8838 if (lnump != NULL)
8839 *lnump = ECMD_LAST;
8840 if (buf->b_fname == NULL)
8841 {
8842 result = (char_u *)"";
8843 valid = 0; /* Must have ":p:h" to be valid */
8844 }
8845 else
8846 result = buf->b_fname;
8847 break;
8848
8849#ifdef FEAT_SEARCHPATH
8850 case SPEC_CFILE: /* file name under cursor */
8851 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L);
8852 if (result == NULL)
8853 {
8854 *errormsg = (char_u *)"";
8855 return NULL;
8856 }
8857 resultbuf = result; /* remember allocated string */
8858 break;
8859#endif
8860
8861#ifdef FEAT_AUTOCMD
8862 case SPEC_AFILE: /* file name for autocommand */
8863 result = autocmd_fname;
8864 if (result == NULL)
8865 {
8866 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
8867 return NULL;
8868 }
8869 break;
8870
8871 case SPEC_ABUF: /* buffer number for autocommand */
8872 if (autocmd_bufnr <= 0)
8873 {
8874 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
8875 return NULL;
8876 }
8877 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8878 result = strbuf;
8879 break;
8880
8881 case SPEC_AMATCH: /* match name for autocommand */
8882 result = autocmd_match;
8883 if (result == NULL)
8884 {
8885 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
8886 return NULL;
8887 }
8888 break;
8889
8890#endif
8891 case SPEC_SFILE: /* file name for ":so" command */
8892 result = sourcing_name;
8893 if (result == NULL)
8894 {
8895 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
8896 return NULL;
8897 }
8898 break;
8899#if defined(FEAT_CLIENTSERVER)
8900 case SPEC_CLIENT: /* Source of last submitted input */
8901 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
8902 result = strbuf;
8903 break;
8904#endif
8905 }
8906
8907 resultlen = (int)STRLEN(result); /* length of new string */
8908 if (src[*usedlen] == '<') /* remove the file name extension */
8909 {
8910 ++*usedlen;
8911#ifdef RISCOS
8912 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
8913#else
8914 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
8915#endif
8916 resultlen = (int)(s - result);
8917 }
8918#ifdef FEAT_MODIFY_FNAME
8919 else if (!skip_mod)
8920 {
8921 valid |= modify_fname(src, usedlen, &result, &resultbuf,
8922 &resultlen);
8923 if (result == NULL)
8924 {
8925 *errormsg = (char_u *)"";
8926 return NULL;
8927 }
8928 }
8929#endif
8930 }
8931
8932 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8933 {
8934 if (valid != VALID_HEAD + VALID_PATH)
8935 /* xgettext:no-c-format */
8936 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
8937 else
8938 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
8939 result = NULL;
8940 }
8941 else
8942 result = vim_strnsave(result, resultlen);
8943 vim_free(resultbuf);
8944 return result;
8945}
8946
8947/*
8948 * Concatenate all files in the argument list, separated by spaces, and return
8949 * it in one allocated string.
8950 * Spaces and backslashes in the file names are escaped with a backslash.
8951 * Returns NULL when out of memory.
8952 */
8953 static char_u *
8954arg_all()
8955{
8956 int len;
8957 int idx;
8958 char_u *retval = NULL;
8959 char_u *p;
8960
8961 /*
8962 * Do this loop two times:
8963 * first time: compute the total length
8964 * second time: concatenate the names
8965 */
8966 for (;;)
8967 {
8968 len = 0;
8969 for (idx = 0; idx < ARGCOUNT; ++idx)
8970 {
8971 p = alist_name(&ARGLIST[idx]);
8972 if (p != NULL)
8973 {
8974 if (len > 0)
8975 {
8976 /* insert a space in between names */
8977 if (retval != NULL)
8978 retval[len] = ' ';
8979 ++len;
8980 }
8981 for ( ; *p != NUL; ++p)
8982 {
8983 if (*p == ' ' || *p == '\\')
8984 {
8985 /* insert a backslash */
8986 if (retval != NULL)
8987 retval[len] = '\\';
8988 ++len;
8989 }
8990 if (retval != NULL)
8991 retval[len] = *p;
8992 ++len;
8993 }
8994 }
8995 }
8996
8997 /* second time: break here */
8998 if (retval != NULL)
8999 {
9000 retval[len] = NUL;
9001 break;
9002 }
9003
9004 /* allocate memory */
9005 retval = alloc(len + 1);
9006 if (retval == NULL)
9007 break;
9008 }
9009
9010 return retval;
9011}
9012
9013#if defined(FEAT_AUTOCMD) || defined(PROTO)
9014/*
9015 * Expand the <sfile> string in "arg".
9016 *
9017 * Returns an allocated string, or NULL for any error.
9018 */
9019 char_u *
9020expand_sfile(arg)
9021 char_u *arg;
9022{
9023 char_u *errormsg;
9024 int len;
9025 char_u *result;
9026 char_u *newres;
9027 char_u *repl;
9028 int srclen;
9029 char_u *p;
9030
9031 result = vim_strsave(arg);
9032 if (result == NULL)
9033 return NULL;
9034
9035 for (p = result; *p; )
9036 {
9037 if (STRNCMP(p, "<sfile>", 7) != 0)
9038 ++p;
9039 else
9040 {
9041 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9042 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9043 if (errormsg != NULL)
9044 {
9045 if (*errormsg)
9046 emsg(errormsg);
9047 vim_free(result);
9048 return NULL;
9049 }
9050 if (repl == NULL) /* no match (cannot happen) */
9051 {
9052 p += srclen;
9053 continue;
9054 }
9055 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9056 newres = alloc(len);
9057 if (newres == NULL)
9058 {
9059 vim_free(repl);
9060 vim_free(result);
9061 return NULL;
9062 }
9063 mch_memmove(newres, result, (size_t)(p - result));
9064 STRCPY(newres + (p - result), repl);
9065 len = (int)STRLEN(newres);
9066 STRCAT(newres, p + srclen);
9067 vim_free(repl);
9068 vim_free(result);
9069 result = newres;
9070 p = newres + len; /* continue after the match */
9071 }
9072 }
9073
9074 return result;
9075}
9076#endif
9077
9078#ifdef FEAT_SESSION
9079static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9080static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9081static frame_T *ses_skipframe __ARGS((frame_T *fr));
9082static int ses_do_frame __ARGS((frame_T *fr));
9083static int ses_do_win __ARGS((win_T *wp));
9084static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9085static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9086static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9087
9088/*
9089 * Write openfile commands for the current buffers to an .exrc file.
9090 * Return FAIL on error, OK otherwise.
9091 */
9092 static int
9093makeopens(fd, dirnow)
9094 FILE *fd;
9095 char_u *dirnow; /* Current directory name */
9096{
9097 buf_T *buf;
9098 int only_save_windows = TRUE;
9099 int nr;
9100 int cnr = 1;
9101 int restore_size = TRUE;
9102 win_T *wp;
9103 char_u *sname;
9104 win_T *edited_win = NULL;
9105
9106 if (ssop_flags & SSOP_BUFFERS)
9107 only_save_windows = FALSE; /* Save ALL buffers */
9108
9109 /*
9110 * Begin by setting the this_session variable, and then other
9111 * sessionable variables.
9112 */
9113#ifdef FEAT_EVAL
9114 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9115 return FAIL;
9116 if (ssop_flags & SSOP_GLOBALS)
9117 if (store_session_globals(fd) == FAIL)
9118 return FAIL;
9119#endif
9120
9121 /*
9122 * Close all windows but one.
9123 */
9124 if (put_line(fd, "silent only") == FAIL)
9125 return FAIL;
9126
9127 /*
9128 * Now a :cd command to the session directory or the current directory
9129 */
9130 if (ssop_flags & SSOP_SESDIR)
9131 {
9132 if (put_line(fd, "exe \"cd \" . expand(\"<sfile>:p:h\")") == FAIL)
9133 return FAIL;
9134 }
9135 else if (ssop_flags & SSOP_CURDIR)
9136 {
9137 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9138 if (sname == NULL
9139 || fprintf(fd, "cd %s", sname) < 0 || put_eol(fd) == FAIL)
9140 return FAIL;
9141 vim_free(sname);
9142 }
9143
9144 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009145 * If there is an empty, unnamed buffer we will wipe it out later.
9146 * Remember the buffer number.
9147 */
9148 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9149 return FAIL;
9150 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9151 return FAIL;
9152 if (put_line(fd, "endif") == FAIL)
9153 return FAIL;
9154
9155 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 * Now save the current files, current buffer first.
9157 */
9158 if (put_line(fd, "set shortmess=aoO") == FAIL)
9159 return FAIL;
9160
9161 /* Now put the other buffers into the buffer list */
9162 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9163 {
9164 if (!(only_save_windows && buf->b_nwindows == 0)
9165 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9166 && buf->b_fname != NULL
9167 && buf->b_p_bl)
9168 {
9169 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9170 : buf->b_wininfo->wi_fpos.lnum) < 0
9171 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9172 return FAIL;
9173 }
9174 }
9175
9176 /* the global argument list */
9177 if (ses_arglist(fd, "args", &global_alist.al_ga,
9178 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9179 return FAIL;
9180
9181 if (ssop_flags & SSOP_RESIZE)
9182 {
9183 /* Note: after the restore we still check it worked!*/
9184 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9185 || put_eol(fd) == FAIL)
9186 return FAIL;
9187 }
9188
9189#ifdef FEAT_GUI
9190 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9191 {
9192 int x, y;
9193
9194 if (gui_mch_get_winpos(&x, &y) == OK)
9195 {
9196 /* Note: after the restore we still check it worked!*/
9197 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9198 return FAIL;
9199 }
9200 }
9201#endif
9202
9203 /*
9204 * Before creating the window layout, try loading one file. If this is
9205 * aborted we don't end up with a number of useless windows.
9206 * This may have side effects! (e.g., compressed or network file).
9207 */
9208 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9209 {
9210 if (ses_do_win(wp)
9211 && wp->w_buffer->b_ffname != NULL
9212 && !wp->w_buffer->b_help
9213#ifdef FEAT_QUICKFIX
9214 && !bt_nofile(wp->w_buffer)
9215#endif
9216 )
9217 {
9218 if (fputs("edit ", fd) < 0
9219 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9220 return FAIL;
9221 if (!wp->w_arg_idx_invalid)
9222 edited_win = wp;
9223 break;
9224 }
9225 }
9226
9227 /*
9228 * Save current window layout.
9229 */
9230 if (put_line(fd, "set splitbelow splitright") == FAIL)
9231 return FAIL;
9232 if (ses_win_rec(fd, topframe) == FAIL)
9233 return FAIL;
9234 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9235 return FAIL;
9236 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9237 return FAIL;
9238
9239 /*
9240 * Check if window sizes can be restored (no windows omitted).
9241 * Remember the window number of the current window after restoring.
9242 */
9243 nr = 0;
9244 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9245 {
9246 if (ses_do_win(wp))
9247 ++nr;
9248 else
9249 restore_size = FALSE;
9250 if (curwin == wp)
9251 cnr = nr;
9252 }
9253
9254 /* Go to the first window. */
9255 if (put_line(fd, "wincmd t") == FAIL)
9256 return FAIL;
9257
9258 /*
9259 * If more than one window, see if sizes can be restored.
9260 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9261 * resized when moving between windows.
9262 * Do this before restoring the view, so that the topline and the cursor
9263 * can be set. This is done again below.
9264 */
9265 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9266 return FAIL;
9267 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9268 return FAIL;
9269
9270 /*
9271 * Restore the view of the window (options, file, cursor, etc.).
9272 */
9273 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9274 {
9275 if (!ses_do_win(wp))
9276 continue;
9277 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9278 return FAIL;
9279 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9280 return FAIL;
9281 }
9282
9283 /*
9284 * Restore cursor to the current window if it's not the first one.
9285 */
9286 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9287 return FAIL;
9288
9289 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009290 * Wipe out an empty unnamed buffer we started in.
9291 */
9292 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9293 return FAIL;
9294 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9295 return FAIL;
9296 if (put_line(fd, "endif") == FAIL)
9297 return FAIL;
9298 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9299 return FAIL;
9300
9301 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 * Restore window sizes again after jumping around in windows, because the
9303 * current window has a minimum size while others may not.
9304 */
9305 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9306 return FAIL;
9307
9308 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9309 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9310 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9311 return FAIL;
9312
9313 /*
9314 * Lastly, execute the x.vim file if it exists.
9315 */
9316 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9317 || put_line(fd, "if file_readable(s:sx)") == FAIL
9318 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9319 || put_line(fd, "endif") == FAIL)
9320 return FAIL;
9321
9322 return OK;
9323}
9324
9325 static int
9326ses_winsizes(fd, restore_size)
9327 FILE *fd;
9328 int restore_size;
9329{
9330 int n = 0;
9331 win_T *wp;
9332
9333 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9334 {
9335 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9336 {
9337 if (!ses_do_win(wp))
9338 continue;
9339 ++n;
9340
9341 /* restore height when not full height */
9342 if (wp->w_height + wp->w_status_height < topframe->fr_height
9343 && (fprintf(fd,
9344 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9345 n, (long)wp->w_height, Rows / 2, Rows) < 0
9346 || put_eol(fd) == FAIL))
9347 return FAIL;
9348
9349 /* restore width when not full width */
9350 if (wp->w_width < Columns && (fprintf(fd,
9351 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9352 n, (long)wp->w_width, Columns / 2, Columns) < 0
9353 || put_eol(fd) == FAIL))
9354 return FAIL;
9355 }
9356 }
9357 else
9358 {
9359 /* Just equalise window sizes */
9360 if (put_line(fd, "wincmd =") == FAIL)
9361 return FAIL;
9362 }
9363 return OK;
9364}
9365
9366/*
9367 * Write commands to "fd" to recursively create windows for frame "fr",
9368 * horizontally and vertically split.
9369 * After the commands the last window in the frame is the current window.
9370 * Returns FAIL when writing the commands to "fd" fails.
9371 */
9372 static int
9373ses_win_rec(fd, fr)
9374 FILE *fd;
9375 frame_T *fr;
9376{
9377 frame_T *frc;
9378 int count = 0;
9379
9380 if (fr->fr_layout != FR_LEAF)
9381 {
9382 /* Find first frame that's not skipped and then create a window for
9383 * each following one (first frame is already there). */
9384 frc = ses_skipframe(fr->fr_child);
9385 if (frc != NULL)
9386 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9387 {
9388 /* Make window as big as possible so that we have lots of room
9389 * to split. */
9390 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9391 || put_line(fd, fr->fr_layout == FR_COL
9392 ? "split" : "vsplit") == FAIL)
9393 return FAIL;
9394 ++count;
9395 }
9396
9397 /* Go back to the first window. */
9398 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9399 ? "%dwincmd k" : "%dwincmd h", count) < 0
9400 || put_eol(fd) == FAIL))
9401 return FAIL;
9402
9403 /* Recursively create frames/windows in each window of this column or
9404 * row. */
9405 frc = ses_skipframe(fr->fr_child);
9406 while (frc != NULL)
9407 {
9408 ses_win_rec(fd, frc);
9409 frc = ses_skipframe(frc->fr_next);
9410 /* Go to next window. */
9411 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9412 return FAIL;
9413 }
9414 }
9415 return OK;
9416}
9417
9418/*
9419 * Skip frames that don't contain windows we want to save in the Session.
9420 * Returns NULL when there none.
9421 */
9422 static frame_T *
9423ses_skipframe(fr)
9424 frame_T *fr;
9425{
9426 frame_T *frc;
9427
9428 for (frc = fr; frc != NULL; frc = frc->fr_next)
9429 if (ses_do_frame(frc))
9430 break;
9431 return frc;
9432}
9433
9434/*
9435 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9436 * the Session.
9437 */
9438 static int
9439ses_do_frame(fr)
9440 frame_T *fr;
9441{
9442 frame_T *frc;
9443
9444 if (fr->fr_layout == FR_LEAF)
9445 return ses_do_win(fr->fr_win);
9446 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9447 if (ses_do_frame(frc))
9448 return TRUE;
9449 return FALSE;
9450}
9451
9452/*
9453 * Return non-zero if window "wp" is to be stored in the Session.
9454 */
9455 static int
9456ses_do_win(wp)
9457 win_T *wp;
9458{
9459 if (wp->w_buffer->b_fname == NULL
9460#ifdef FEAT_QUICKFIX
9461 /* When 'buftype' is "nofile" can't restore the window contents. */
9462 || bt_nofile(wp->w_buffer)
9463#endif
9464 )
9465 return (ssop_flags & SSOP_BLANK);
9466 if (wp->w_buffer->b_help)
9467 return (ssop_flags & SSOP_HELP);
9468 return TRUE;
9469}
9470
9471/*
9472 * Write commands to "fd" to restore the view of a window.
9473 * Caller must make sure 'scrolloff' is zero.
9474 */
9475 static int
9476put_view(fd, wp, add_edit, flagp)
9477 FILE *fd;
9478 win_T *wp;
9479 int add_edit; /* add ":edit" command to view */
9480 unsigned *flagp; /* vop_flags or ssop_flags */
9481{
9482 win_T *save_curwin;
9483 int f;
9484 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009485 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486
9487 /* Always restore cursor position for ":mksession". For ":mkview" only
9488 * when 'viewoptions' contains "cursor". */
9489 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9490
9491 /*
9492 * Local argument list.
9493 */
9494 if (wp->w_alist == &global_alist)
9495 {
9496 if (put_line(fd, "argglobal") == FAIL)
9497 return FAIL;
9498 }
9499 else
9500 {
9501 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9502 flagp == &vop_flags
9503 || !(*flagp & SSOP_CURDIR)
9504 || wp->w_localdir != NULL, flagp) == FAIL)
9505 return FAIL;
9506 }
9507
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009508 /* Only when part of a session: restore the argument index. Some
9509 * arguments may have been deleted, check if the index is valid. */
9510 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
9511 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 {
9513 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9514 || put_eol(fd) == FAIL)
9515 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009516 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 }
9518
9519 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009520 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 {
9522 /*
9523 * Load the file.
9524 */
9525 if (wp->w_buffer->b_ffname != NULL
9526#ifdef FEAT_QUICKFIX
9527 && !bt_nofile(wp->w_buffer)
9528#endif
9529 )
9530 {
9531 /*
9532 * Editing a file in this buffer: use ":edit file".
9533 * This may have side effects! (e.g., compressed or network file).
9534 */
9535 if (fputs("edit ", fd) < 0
9536 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9537 return FAIL;
9538 }
9539 else
9540 {
9541 /* No file in this buffer, just make it empty. */
9542 if (put_line(fd, "enew") == FAIL)
9543 return FAIL;
9544#ifdef FEAT_QUICKFIX
9545 if (wp->w_buffer->b_ffname != NULL)
9546 {
9547 /* The buffer does have a name, but it's not a file name. */
9548 if (fputs("file ", fd) < 0
9549 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9550 return FAIL;
9551 }
9552#endif
9553 do_cursor = FALSE;
9554 }
9555 }
9556
9557 /*
9558 * Local mappings and abbreviations.
9559 */
9560 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9561 && makemap(fd, wp->w_buffer) == FAIL)
9562 return FAIL;
9563
9564 /*
9565 * Local options. Need to go to the window temporarily.
9566 * Store only local values when using ":mkview" and when ":mksession" is
9567 * used and 'sessionoptions' doesn't include "options".
9568 * Some folding options are always stored when "folds" is included,
9569 * otherwise the folds would not be restored correctly.
9570 */
9571 save_curwin = curwin;
9572 curwin = wp;
9573 curbuf = curwin->w_buffer;
9574 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9575 f = makeset(fd, OPT_LOCAL,
9576 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
9577#ifdef FEAT_FOLDING
9578 else if (*flagp & SSOP_FOLDS)
9579 f = makefoldset(fd);
9580#endif
9581 else
9582 f = OK;
9583 curwin = save_curwin;
9584 curbuf = curwin->w_buffer;
9585 if (f == FAIL)
9586 return FAIL;
9587
9588#ifdef FEAT_FOLDING
9589 /*
9590 * Save Folds when 'buftype' is empty and for help files.
9591 */
9592 if ((*flagp & SSOP_FOLDS)
9593 && wp->w_buffer->b_ffname != NULL
9594 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help))
9595 {
9596 if (put_folds(fd, wp) == FAIL)
9597 return FAIL;
9598 }
9599#endif
9600
9601 /*
9602 * Set the cursor after creating folds, since that moves the cursor.
9603 */
9604 if (do_cursor)
9605 {
9606
9607 /* Restore the cursor line in the file and relatively in the
9608 * window. Don't use "G", it changes the jumplist. */
9609 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
9610 (long)wp->w_cursor.lnum,
9611 (long)(wp->w_cursor.lnum - wp->w_topline),
9612 (long)wp->w_height / 2, (long)wp->w_height) < 0
9613 || put_eol(fd) == FAIL
9614 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
9615 || put_line(fd, "exe s:l") == FAIL
9616 || put_line(fd, "normal! zt") == FAIL
9617 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
9618 || put_eol(fd) == FAIL)
9619 return FAIL;
9620 /* Restore the cursor column and left offset when not wrapping. */
9621 if (wp->w_cursor.col == 0)
9622 {
9623 if (put_line(fd, "normal! 0") == FAIL)
9624 return FAIL;
9625 }
9626 else
9627 {
9628 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
9629 {
9630 if (fprintf(fd,
9631 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
9632 (long)wp->w_cursor.col,
9633 (long)(wp->w_cursor.col - wp->w_leftcol),
9634 (long)wp->w_width / 2, (long)wp->w_width) < 0
9635 || put_eol(fd) == FAIL
9636 || put_line(fd, "if s:c > 0") == FAIL
9637 || fprintf(fd,
9638 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
9639 (long)wp->w_cursor.col) < 0
9640 || put_eol(fd) == FAIL
9641 || put_line(fd, "else") == FAIL
9642 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
9643 || put_eol(fd) == FAIL
9644 || put_line(fd, "endif") == FAIL)
9645 return FAIL;
9646 }
9647 else
9648 {
9649 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
9650 || put_eol(fd) == FAIL)
9651 return FAIL;
9652 }
9653 }
9654 }
9655
9656 /*
9657 * Local directory.
9658 */
9659 if (wp->w_localdir != NULL)
9660 {
9661 if (fputs("lcd ", fd) < 0
9662 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
9663 || put_eol(fd) == FAIL)
9664 return FAIL;
9665 }
9666
9667 return OK;
9668}
9669
9670/*
9671 * Write an argument list to the session file.
9672 * Returns FAIL if writing fails.
9673 */
9674 static int
9675ses_arglist(fd, cmd, gap, fullname, flagp)
9676 FILE *fd;
9677 char *cmd;
9678 garray_T *gap;
9679 int fullname; /* TRUE: use full path name */
9680 unsigned *flagp;
9681{
9682 int i;
9683 char_u buf[MAXPATHL];
9684 char_u *s;
9685
9686 if (gap->ga_len == 0)
9687 return put_line(fd, "silent! argdel *");
9688 if (fputs(cmd, fd) < 0)
9689 return FAIL;
9690 for (i = 0; i < gap->ga_len; ++i)
9691 {
9692 /* NULL file names are skipped (only happens when out of memory). */
9693 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
9694 if (s != NULL)
9695 {
9696 if (fullname)
9697 {
9698 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
9699 s = buf;
9700 }
9701 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
9702 return FAIL;
9703 }
9704 }
9705 return put_eol(fd);
9706}
9707
9708/*
9709 * Write a buffer name to the session file.
9710 * Also ends the line.
9711 * Returns FAIL if writing fails.
9712 */
9713 static int
9714ses_fname(fd, buf, flagp)
9715 FILE *fd;
9716 buf_T *buf;
9717 unsigned *flagp;
9718{
9719 char_u *name;
9720
9721 /* Use the short file name if the current directory is known at the time
9722 * the session file will be sourced. Don't do this for ":mkview", we
9723 * don't know the current directory. */
9724 if (buf->b_sfname != NULL
9725 && flagp == &ssop_flags
9726 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
9727 name = buf->b_sfname;
9728 else
9729 name = buf->b_ffname;
9730 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
9731 return FAIL;
9732 return OK;
9733}
9734
9735/*
9736 * Write a file name to the session file.
9737 * Takes care of the "slash" option in 'sessionoptions' and escapes special
9738 * characters.
9739 * Returns FAIL if writing fails.
9740 */
9741 static int
9742ses_put_fname(fd, name, flagp)
9743 FILE *fd;
9744 char_u *name;
9745 unsigned *flagp;
9746{
9747 char_u *sname;
9748 int retval = OK;
9749 int c;
9750
9751 sname = home_replace_save(NULL, name);
9752 if (sname != NULL)
9753 name = sname;
9754 while (*name != NUL)
9755 {
9756#ifdef FEAT_MBYTE
9757 {
9758 int l;
9759
9760 if (has_mbyte && (l = (*mb_ptr2len_check)(name)) > 1)
9761 {
9762 /* copy a multibyte char */
9763 while (--l >= 0)
9764 {
9765 if (putc(*name, fd) != *name)
9766 retval = FAIL;
9767 ++name;
9768 }
9769 continue;
9770 }
9771 }
9772#endif
9773 c = *name++;
9774 if (c == '\\' && (*flagp & SSOP_SLASH))
9775 /* change a backslash to a forward slash */
9776 c = '/';
9777 else if ((vim_strchr(escape_chars, c) != NULL
9778#ifdef BACKSLASH_IN_FILENAME
9779 && c != '\\'
9780#endif
9781 ) || c == '#' || c == '%')
9782 {
9783 /* escape a special character with a backslash */
9784 if (putc('\\', fd) != '\\')
9785 retval = FAIL;
9786 }
9787 if (putc(c, fd) != c)
9788 retval = FAIL;
9789 }
9790 vim_free(sname);
9791 return retval;
9792}
9793
9794/*
9795 * ":loadview [nr]"
9796 */
9797 static void
9798ex_loadview(eap)
9799 exarg_T *eap;
9800{
9801 char_u *fname;
9802
9803 fname = get_view_file(*eap->arg);
9804 if (fname != NULL)
9805 {
9806 do_source(fname, FALSE, FALSE);
9807 vim_free(fname);
9808 }
9809}
9810
9811/*
9812 * Get the name of the view file for the current buffer.
9813 */
9814 static char_u *
9815get_view_file(c)
9816 int c;
9817{
9818 int len = 0;
9819 char_u *p, *s;
9820 char_u *retval;
9821 char_u *sname;
9822
9823 if (curbuf->b_ffname == NULL)
9824 {
9825 EMSG(_(e_noname));
9826 return NULL;
9827 }
9828 sname = home_replace_save(NULL, curbuf->b_ffname);
9829 if (sname == NULL)
9830 return NULL;
9831
9832 /*
9833 * We want a file name without separators, because we're not going to make
9834 * a directory.
9835 * "normal" path separator -> "=+"
9836 * "=" -> "=="
9837 * ":" path separator -> "=-"
9838 */
9839 for (p = sname; *p; ++p)
9840 if (*p == '=' || vim_ispathsep(*p))
9841 ++len;
9842 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
9843 if (retval != NULL)
9844 {
9845 STRCPY(retval, p_vdir);
9846 add_pathsep(retval);
9847 s = retval + STRLEN(retval);
9848 for (p = sname; *p; ++p)
9849 {
9850 if (*p == '=')
9851 {
9852 *s++ = '=';
9853 *s++ = '=';
9854 }
9855 else if (vim_ispathsep(*p))
9856 {
9857 *s++ = '=';
9858#ifdef MACOS_CLASSIC /* TODO: Is it also needed for MACOS_X? (Dany) */
9859 *s++ = '+';
9860#else
9861# if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
9862 || defined(VMS)
9863 if (*p == ':')
9864 *s++ = '-';
9865 else
9866# endif
9867 *s++ = '+';
9868#endif
9869 }
9870 else
9871 *s++ = *p;
9872 }
9873 *s++ = '=';
9874 *s++ = c;
9875 STRCPY(s, ".vim");
9876 }
9877
9878 vim_free(sname);
9879 return retval;
9880}
9881
9882#endif /* FEAT_SESSION */
9883
9884/*
9885 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
9886 * Return FAIL for a write error.
9887 */
9888 int
9889put_eol(fd)
9890 FILE *fd;
9891{
9892 if (
9893#ifdef USE_CRNL
9894 (
9895# ifdef MKSESSION_NL
9896 !mksession_nl &&
9897# endif
9898 (putc('\r', fd) < 0)) ||
9899#endif
9900 (putc('\n', fd) < 0))
9901 return FAIL;
9902 return OK;
9903}
9904
9905/*
9906 * Write a line to "fd".
9907 * Return FAIL for a write error.
9908 */
9909 int
9910put_line(fd, s)
9911 FILE *fd;
9912 char *s;
9913{
9914 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
9915 return FAIL;
9916 return OK;
9917}
9918
9919#ifdef FEAT_VIMINFO
9920/*
9921 * ":rviminfo" and ":wviminfo".
9922 */
9923 static void
9924ex_viminfo(eap)
9925 exarg_T *eap;
9926{
9927 char_u *save_viminfo;
9928
9929 save_viminfo = p_viminfo;
9930 if (*p_viminfo == NUL)
9931 p_viminfo = (char_u *)"'100";
9932 if (eap->cmdidx == CMD_rviminfo)
9933 {
9934 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
9935 EMSG(_("E195: Cannot open viminfo file for reading"));
9936 }
9937 else
9938 write_viminfo(eap->arg, eap->forceit);
9939 p_viminfo = save_viminfo;
9940}
9941#endif
9942
9943#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009944/*
9945 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009946 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 void
9949dialog_msg(buff, format, fname)
9950 char_u *buff;
9951 char *format;
9952 char_u *fname;
9953{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 if (fname == NULL)
9955 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009956 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957}
9958#endif
9959
9960/*
9961 * ":behave {mswin,xterm}"
9962 */
9963 static void
9964ex_behave(eap)
9965 exarg_T *eap;
9966{
9967 if (STRCMP(eap->arg, "mswin") == 0)
9968 {
9969 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9970 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9971 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9972 set_option_value((char_u *)"keymodel", 0L,
9973 (char_u *)"startsel,stopsel", 0);
9974 }
9975 else if (STRCMP(eap->arg, "xterm") == 0)
9976 {
9977 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9978 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9979 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9980 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9981 }
9982 else
9983 EMSG2(_(e_invarg2), eap->arg);
9984}
9985
9986#ifdef FEAT_AUTOCMD
9987static int filetype_detect = FALSE;
9988static int filetype_plugin = FALSE;
9989static int filetype_indent = FALSE;
9990
9991/*
9992 * ":filetype [plugin] [indent] {on,off,detect}"
9993 * on: Load the filetype.vim file to install autocommands for file types.
9994 * off: Load the ftoff.vim file to remove all autocommands for file types.
9995 * plugin on: load filetype.vim and ftplugin.vim
9996 * plugin off: load ftplugof.vim
9997 * indent on: load filetype.vim and indent.vim
9998 * indent off: load indoff.vim
9999 */
10000 static void
10001ex_filetype(eap)
10002 exarg_T *eap;
10003{
10004 char_u *arg = eap->arg;
10005 int plugin = FALSE;
10006 int indent = FALSE;
10007
10008 if (*eap->arg == NUL)
10009 {
10010 /* Print current status. */
10011 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10012 filetype_detect ? "ON" : "OFF",
10013 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10014 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10015 return;
10016 }
10017
10018 /* Accept "plugin" and "indent" in any order. */
10019 for (;;)
10020 {
10021 if (STRNCMP(arg, "plugin", 6) == 0)
10022 {
10023 plugin = TRUE;
10024 arg = skipwhite(arg + 6);
10025 continue;
10026 }
10027 if (STRNCMP(arg, "indent", 6) == 0)
10028 {
10029 indent = TRUE;
10030 arg = skipwhite(arg + 6);
10031 continue;
10032 }
10033 break;
10034 }
10035 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10036 {
10037 if (*arg == 'o' || !filetype_detect)
10038 {
10039 cmd_runtime((char_u *)FILETYPE_FILE, TRUE);
10040 filetype_detect = TRUE;
10041 if (plugin)
10042 {
10043 cmd_runtime((char_u *)FTPLUGIN_FILE, TRUE);
10044 filetype_plugin = TRUE;
10045 }
10046 if (indent)
10047 {
10048 cmd_runtime((char_u *)INDENT_FILE, TRUE);
10049 filetype_indent = TRUE;
10050 }
10051 }
10052 if (*arg == 'd')
10053 {
10054 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +000010055 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 }
10057 }
10058 else if (STRCMP(arg, "off") == 0)
10059 {
10060 if (plugin || indent)
10061 {
10062 if (plugin)
10063 {
10064 cmd_runtime((char_u *)FTPLUGOF_FILE, TRUE);
10065 filetype_plugin = FALSE;
10066 }
10067 if (indent)
10068 {
10069 cmd_runtime((char_u *)INDOFF_FILE, TRUE);
10070 filetype_indent = FALSE;
10071 }
10072 }
10073 else
10074 {
10075 cmd_runtime((char_u *)FTOFF_FILE, TRUE);
10076 filetype_detect = FALSE;
10077 }
10078 }
10079 else
10080 EMSG2(_(e_invarg2), arg);
10081}
10082
10083/*
10084 * ":setfiletype {name}"
10085 */
10086 static void
10087ex_setfiletype(eap)
10088 exarg_T *eap;
10089{
10090 if (!did_filetype)
10091 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10092}
10093#endif
10094
10095/*ARGSUSED*/
10096 static void
10097ex_digraphs(eap)
10098 exarg_T *eap;
10099{
10100#ifdef FEAT_DIGRAPHS
10101 if (*eap->arg != NUL)
10102 putdigraph(eap->arg);
10103 else
10104 listdigraphs();
10105#else
10106 EMSG(_("E196: No digraphs in this version"));
10107#endif
10108}
10109
10110 static void
10111ex_set(eap)
10112 exarg_T *eap;
10113{
10114 int flags = 0;
10115
10116 if (eap->cmdidx == CMD_setlocal)
10117 flags = OPT_LOCAL;
10118 else if (eap->cmdidx == CMD_setglobal)
10119 flags = OPT_GLOBAL;
10120#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10121 if (cmdmod.browse && flags == 0)
10122 ex_options(eap);
10123 else
10124#endif
10125 (void)do_set(eap->arg, flags);
10126}
10127
10128#ifdef FEAT_SEARCH_EXTRA
10129/*
10130 * ":nohlsearch"
10131 */
10132/*ARGSUSED*/
10133 static void
10134ex_nohlsearch(eap)
10135 exarg_T *eap;
10136{
10137 no_hlsearch = TRUE;
10138 redraw_all_later(NOT_VALID);
10139}
10140
10141/*
10142 * ":match {group} {pattern}"
10143 * Sets nextcmd to the start of the next command, if any. Also called when
10144 * skipping commands to find the next command.
10145 */
10146 static void
10147ex_match(eap)
10148 exarg_T *eap;
10149{
10150 char_u *p;
10151 char_u *end;
10152 int c;
10153
10154 /* First clear any old pattern. */
10155 if (!eap->skip)
10156 {
10157 vim_free(curwin->w_match.regprog);
10158 curwin->w_match.regprog = NULL;
10159 redraw_later(NOT_VALID); /* always need a redraw */
10160 }
10161
10162 if (ends_excmd(*eap->arg))
10163 end = eap->arg;
10164 else if ((STRNICMP(eap->arg, "none", 4) == 0
10165 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10166 end = eap->arg + 4;
10167 else
10168 {
10169 p = skiptowhite(eap->arg);
10170 if (!eap->skip)
10171 {
10172 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg));
10173 if (curwin->w_match_id == 0)
10174 {
10175 EMSG2(_(e_nogroup), eap->arg);
10176 return;
10177 }
10178 }
10179 p = skipwhite(p);
10180 if (*p == NUL)
10181 {
10182 /* There must be two arguments. */
10183 EMSG2(_(e_invarg2), eap->arg);
10184 return;
10185 }
10186 end = skip_regexp(p + 1, *p, TRUE, NULL);
10187 if (!eap->skip)
10188 {
10189 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10190 {
10191 eap->errmsg = e_trailing;
10192 return;
10193 }
10194
10195 c = *end;
10196 *end = NUL;
10197 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC);
10198 *end = c;
10199 if (curwin->w_match.regprog == NULL)
10200 {
10201 EMSG2(_(e_invarg2), p);
10202 return;
10203 }
10204 }
10205 }
10206 eap->nextcmd = find_nextcmd(end);
10207}
10208#endif
10209
10210#ifdef FEAT_CRYPT
10211/*
10212 * ":X": Get crypt key
10213 */
10214/*ARGSUSED*/
10215 static void
10216ex_X(eap)
10217 exarg_T *eap;
10218{
10219 (void)get_crypt_key(TRUE, TRUE);
10220}
10221#endif
10222
10223#ifdef FEAT_FOLDING
10224 static void
10225ex_fold(eap)
10226 exarg_T *eap;
10227{
10228 if (foldManualAllowed(TRUE))
10229 foldCreate(eap->line1, eap->line2);
10230}
10231
10232 static void
10233ex_foldopen(eap)
10234 exarg_T *eap;
10235{
10236 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10237 eap->forceit, FALSE);
10238}
10239
10240 static void
10241ex_folddo(eap)
10242 exarg_T *eap;
10243{
10244 linenr_T lnum;
10245
10246 /* First set the marks for all lines closed/open. */
10247 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10248 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10249 ml_setmarked(lnum);
10250
10251 /* Execute the command on the marked lines. */
10252 global_exe(eap->arg);
10253 ml_clearmarked(); /* clear rest of the marks */
10254}
10255#endif