blob: b5c636c76af7503541d184604a362c36eb9b00d3 [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);
4344 mch_memmove(new_cmdline + i, repl, (size_t)len);
4345 i += len; /* remember the end of the string */
4346 STRCPY(new_cmdline + i, src + srclen);
4347 src = new_cmdline + i; /* remember where to continue */
4348
4349 if (eap->nextcmd) /* append next command */
4350 {
4351 i = (int)STRLEN(new_cmdline) + 1;
4352 STRCPY(new_cmdline + i, eap->nextcmd);
4353 eap->nextcmd = new_cmdline + i;
4354 }
4355 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4356 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4357 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4358 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4359 vim_free(*cmdlinep);
4360 *cmdlinep = new_cmdline;
4361
4362 return src;
4363}
4364
4365/*
4366 * Check for '|' to separate commands and '"' to start comments.
4367 */
4368 void
4369separate_nextcmd(eap)
4370 exarg_T *eap;
4371{
4372 char_u *p;
4373
Bram Moolenaar86b68352004-12-27 21:59:20 +00004374#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004375 p = skip_grep_pat(eap);
4376#else
4377 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004378#endif
4379
4380 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 {
4382 if (*p == Ctrl_V)
4383 {
4384 if (eap->argt & (USECTRLV | XFILE))
4385 ++p; /* skip CTRL-V and next char */
4386 else
4387 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4388 if (*p == NUL) /* stop at NUL after CTRL-V */
4389 break;
4390 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004391
4392#ifdef FEAT_EVAL
4393 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004394 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004395 {
4396 p += 2;
4397 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004398 }
4399#endif
4400
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 /* Check for '"': start of comment or '|': next command */
4402 /* :@" and :*" do not start a comment!
4403 * :redir @" doesn't either. */
4404 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4405 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4406 || p != eap->arg)
4407 && (eap->cmdidx != CMD_redir
4408 || p != eap->arg + 1 || p[-1] != '@'))
4409 || *p == '|' || *p == '\n')
4410 {
4411 /*
4412 * We remove the '\' before the '|', unless USECTRLV is used
4413 * AND 'b' is present in 'cpoptions'.
4414 */
4415 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4416 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4417 {
4418 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4419 --p;
4420 }
4421 else
4422 {
4423 eap->nextcmd = check_nextcmd(p);
4424 *p = NUL;
4425 break;
4426 }
4427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004429
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4431 del_trailing_spaces(eap->arg);
4432}
4433
4434/*
4435 * get + command from ex argument
4436 */
4437 static char_u *
4438getargcmd(argp)
4439 char_u **argp;
4440{
4441 char_u *arg = *argp;
4442 char_u *command = NULL;
4443
4444 if (*arg == '+') /* +[command] */
4445 {
4446 ++arg;
4447 if (vim_isspace(*arg))
4448 command = dollar_command;
4449 else
4450 {
4451 command = arg;
4452 arg = skip_cmd_arg(command, TRUE);
4453 if (*arg != NUL)
4454 *arg++ = NUL; /* terminate command with NUL */
4455 }
4456
4457 arg = skipwhite(arg); /* skip over spaces */
4458 *argp = arg;
4459 }
4460 return command;
4461}
4462
4463/*
4464 * Find end of "+command" argument. Skip over "\ " and "\\".
4465 */
4466 static char_u *
4467skip_cmd_arg(p, rembs)
4468 char_u *p;
4469 int rembs; /* TRUE to halve the number of backslashes */
4470{
4471 while (*p && !vim_isspace(*p))
4472 {
4473 if (*p == '\\' && p[1] != NUL)
4474 {
4475 if (rembs)
4476 mch_memmove(p, p + 1, STRLEN(p));
4477 else
4478 ++p;
4479 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004480 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 }
4482 return p;
4483}
4484
4485/*
4486 * Get "++opt=arg" argument.
4487 * Return FAIL or OK.
4488 */
4489 static int
4490getargopt(eap)
4491 exarg_T *eap;
4492{
4493 char_u *arg = eap->arg + 2;
4494 int *pp = NULL;
4495#ifdef FEAT_MBYTE
4496 char_u *p;
4497#endif
4498
4499 /* ":edit ++[no]bin[ary] file" */
4500 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4501 {
4502 if (*arg == 'n')
4503 {
4504 arg += 2;
4505 eap->force_bin = FORCE_NOBIN;
4506 }
4507 else
4508 eap->force_bin = FORCE_BIN;
4509 if (!checkforcmd(&arg, "binary", 3))
4510 return FAIL;
4511 eap->arg = skipwhite(arg);
4512 return OK;
4513 }
4514
4515 if (STRNCMP(arg, "ff", 2) == 0)
4516 {
4517 arg += 2;
4518 pp = &eap->force_ff;
4519 }
4520 else if (STRNCMP(arg, "fileformat", 10) == 0)
4521 {
4522 arg += 10;
4523 pp = &eap->force_ff;
4524 }
4525#ifdef FEAT_MBYTE
4526 else if (STRNCMP(arg, "enc", 3) == 0)
4527 {
4528 arg += 3;
4529 pp = &eap->force_enc;
4530 }
4531 else if (STRNCMP(arg, "encoding", 8) == 0)
4532 {
4533 arg += 8;
4534 pp = &eap->force_enc;
4535 }
4536#endif
4537
4538 if (pp == NULL || *arg != '=')
4539 return FAIL;
4540
4541 ++arg;
4542 *pp = (int)(arg - eap->cmd);
4543 arg = skip_cmd_arg(arg, FALSE);
4544 eap->arg = skipwhite(arg);
4545 *arg = NUL;
4546
4547#ifdef FEAT_MBYTE
4548 if (pp == &eap->force_ff)
4549 {
4550#endif
4551 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4552 return FAIL;
4553#ifdef FEAT_MBYTE
4554 }
4555 else
4556 {
4557 /* Make 'fileencoding' lower case. */
4558 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4559 *p = TOLOWER_ASC(*p);
4560 }
4561#endif
4562
4563 return OK;
4564}
4565
4566/*
4567 * ":abbreviate" and friends.
4568 */
4569 static void
4570ex_abbreviate(eap)
4571 exarg_T *eap;
4572{
4573 do_exmap(eap, TRUE); /* almost the same as mapping */
4574}
4575
4576/*
4577 * ":map" and friends.
4578 */
4579 static void
4580ex_map(eap)
4581 exarg_T *eap;
4582{
4583 /*
4584 * If we are sourcing .exrc or .vimrc in current directory we
4585 * print the mappings for security reasons.
4586 */
4587 if (secure)
4588 {
4589 secure = 2;
4590 msg_outtrans(eap->cmd);
4591 msg_putchar('\n');
4592 }
4593 do_exmap(eap, FALSE);
4594}
4595
4596/*
4597 * ":unmap" and friends.
4598 */
4599 static void
4600ex_unmap(eap)
4601 exarg_T *eap;
4602{
4603 do_exmap(eap, FALSE);
4604}
4605
4606/*
4607 * ":mapclear" and friends.
4608 */
4609 static void
4610ex_mapclear(eap)
4611 exarg_T *eap;
4612{
4613 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4614}
4615
4616/*
4617 * ":abclear" and friends.
4618 */
4619 static void
4620ex_abclear(eap)
4621 exarg_T *eap;
4622{
4623 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4624}
4625
4626#ifdef FEAT_AUTOCMD
4627 static void
4628ex_autocmd(eap)
4629 exarg_T *eap;
4630{
4631 /*
4632 * Disallow auto commands from .exrc and .vimrc in current
4633 * directory for security reasons.
4634 */
4635 if (secure)
4636 {
4637 secure = 2;
4638 eap->errmsg = e_curdir;
4639 }
4640 else if (eap->cmdidx == CMD_autocmd)
4641 do_autocmd(eap->arg, eap->forceit);
4642 else
4643 do_augroup(eap->arg, eap->forceit);
4644}
4645
4646/*
4647 * ":doautocmd": Apply the automatic commands to the current buffer.
4648 */
4649 static void
4650ex_doautocmd(eap)
4651 exarg_T *eap;
4652{
4653 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004654 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655}
4656#endif
4657
4658#ifdef FEAT_LISTCMDS
4659/*
4660 * :[N]bunload[!] [N] [bufname] unload buffer
4661 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4662 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4663 */
4664 static void
4665ex_bunload(eap)
4666 exarg_T *eap;
4667{
4668 eap->errmsg = do_bufdel(
4669 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4670 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4671 : DOBUF_UNLOAD, eap->arg,
4672 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4673}
4674
4675/*
4676 * :[N]buffer [N] to buffer N
4677 * :[N]sbuffer [N] to buffer N
4678 */
4679 static void
4680ex_buffer(eap)
4681 exarg_T *eap;
4682{
4683 if (*eap->arg)
4684 eap->errmsg = e_trailing;
4685 else
4686 {
4687 if (eap->addr_count == 0) /* default is current buffer */
4688 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4689 else
4690 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4691 }
4692}
4693
4694/*
4695 * :[N]bmodified [N] to next mod. buffer
4696 * :[N]sbmodified [N] to next mod. buffer
4697 */
4698 static void
4699ex_bmodified(eap)
4700 exarg_T *eap;
4701{
4702 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4703}
4704
4705/*
4706 * :[N]bnext [N] to next buffer
4707 * :[N]sbnext [N] split and to next buffer
4708 */
4709 static void
4710ex_bnext(eap)
4711 exarg_T *eap;
4712{
4713 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4714}
4715
4716/*
4717 * :[N]bNext [N] to previous buffer
4718 * :[N]bprevious [N] to previous buffer
4719 * :[N]sbNext [N] split and to previous buffer
4720 * :[N]sbprevious [N] split and to previous buffer
4721 */
4722 static void
4723ex_bprevious(eap)
4724 exarg_T *eap;
4725{
4726 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4727}
4728
4729/*
4730 * :brewind to first buffer
4731 * :bfirst to first buffer
4732 * :sbrewind split and to first buffer
4733 * :sbfirst split and to first buffer
4734 */
4735 static void
4736ex_brewind(eap)
4737 exarg_T *eap;
4738{
4739 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4740}
4741
4742/*
4743 * :blast to last buffer
4744 * :sblast split and to last buffer
4745 */
4746 static void
4747ex_blast(eap)
4748 exarg_T *eap;
4749{
4750 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4751}
4752#endif
4753
4754 int
4755ends_excmd(c)
4756 int c;
4757{
4758 return (c == NUL || c == '|' || c == '"' || c == '\n');
4759}
4760
4761#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4762 || defined(PROTO)
4763/*
4764 * Return the next command, after the first '|' or '\n'.
4765 * Return NULL if not found.
4766 */
4767 char_u *
4768find_nextcmd(p)
4769 char_u *p;
4770{
4771 while (*p != '|' && *p != '\n')
4772 {
4773 if (*p == NUL)
4774 return NULL;
4775 ++p;
4776 }
4777 return (p + 1);
4778}
4779#endif
4780
4781/*
4782 * Check if *p is a separator between Ex commands.
4783 * Return NULL if it isn't, (p + 1) if it is.
4784 */
4785 char_u *
4786check_nextcmd(p)
4787 char_u *p;
4788{
4789 p = skipwhite(p);
4790 if (*p == '|' || *p == '\n')
4791 return (p + 1);
4792 else
4793 return NULL;
4794}
4795
4796/*
4797 * - if there are more files to edit
4798 * - and this is the last window
4799 * - and forceit not used
4800 * - and not repeated twice on a row
4801 * return FAIL and give error message if 'message' TRUE
4802 * return OK otherwise
4803 */
4804 static int
4805check_more(message, forceit)
4806 int message; /* when FALSE check only, no messages */
4807 int forceit;
4808{
4809 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4810
4811 if (!forceit && only_one_window() && ARGCOUNT > 1 && !arg_had_last
4812 && n >= 0 && quitmore == 0)
4813 {
4814 if (message)
4815 {
4816#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4817 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4818 {
4819 char_u buff[IOSIZE];
4820
4821 if (n == 1)
4822 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4823 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004824 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 _("%d more files to edit. Quit anyway?"), n);
4826 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4827 return OK;
4828 return FAIL;
4829 }
4830#endif
4831 if (n == 1)
4832 EMSG(_("E173: 1 more file to edit"));
4833 else
4834 EMSGN(_("E173: %ld more files to edit"), n);
4835 quitmore = 2; /* next try to quit is allowed */
4836 }
4837 return FAIL;
4838 }
4839 return OK;
4840}
4841
4842#ifdef FEAT_CMDL_COMPL
4843/*
4844 * Function given to ExpandGeneric() to obtain the list of command names.
4845 */
4846/*ARGSUSED*/
4847 char_u *
4848get_command_name(xp, idx)
4849 expand_T *xp;
4850 int idx;
4851{
4852 if (idx >= (int)CMD_SIZE)
4853# ifdef FEAT_USR_CMDS
4854 return get_user_command_name(idx);
4855# else
4856 return NULL;
4857# endif
4858 return cmdnames[idx].cmd_name;
4859}
4860#endif
4861
4862#if defined(FEAT_USR_CMDS) || defined(PROTO)
4863static 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));
4864static void uc_list __ARGS((char_u *name, size_t name_len));
4865static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4866static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4867static 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));
4868
4869 static int
4870uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4871 char_u *name;
4872 size_t name_len;
4873 char_u *rep;
4874 long argt;
4875 long def;
4876 int flags;
4877 int compl;
4878 char_u *compl_arg;
4879 int force;
4880{
4881 ucmd_T *cmd = NULL;
4882 char_u *p;
4883 int i;
4884 int cmp = 1;
4885 char_u *rep_buf = NULL;
4886 garray_T *gap;
4887
4888 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4889 if (rep_buf == NULL)
4890 {
4891 /* Can't replace termcodes - try using the string as is */
4892 rep_buf = vim_strsave(rep);
4893
4894 /* Give up if out of memory */
4895 if (rep_buf == NULL)
4896 return FAIL;
4897 }
4898
4899 /* get address of growarray: global or in curbuf */
4900 if (flags & UC_BUFFER)
4901 {
4902 gap = &curbuf->b_ucmds;
4903 if (gap->ga_itemsize == 0)
4904 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4905 }
4906 else
4907 gap = &ucmds;
4908
4909 /* Search for the command in the already defined commands. */
4910 for (i = 0; i < gap->ga_len; ++i)
4911 {
4912 size_t len;
4913
4914 cmd = USER_CMD_GA(gap, i);
4915 len = STRLEN(cmd->uc_name);
4916 cmp = STRNCMP(name, cmd->uc_name, name_len);
4917 if (cmp == 0)
4918 {
4919 if (name_len < len)
4920 cmp = -1;
4921 else if (name_len > len)
4922 cmp = 1;
4923 }
4924
4925 if (cmp == 0)
4926 {
4927 if (!force)
4928 {
4929 EMSG(_("E174: Command already exists: add ! to replace it"));
4930 goto fail;
4931 }
4932
4933 vim_free(cmd->uc_rep);
4934 cmd->uc_rep = 0;
4935 break;
4936 }
4937
4938 /* Stop as soon as we pass the name to add */
4939 if (cmp < 0)
4940 break;
4941 }
4942
4943 /* Extend the array unless we're replacing an existing command */
4944 if (cmp != 0)
4945 {
4946 if (ga_grow(gap, 1) != OK)
4947 goto fail;
4948 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
4949 goto fail;
4950
4951 cmd = USER_CMD_GA(gap, i);
4952 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
4953
4954 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955
4956 cmd->uc_name = p;
4957 }
4958
4959 cmd->uc_rep = rep_buf;
4960 cmd->uc_argt = argt;
4961 cmd->uc_def = def;
4962 cmd->uc_compl = compl;
4963#ifdef FEAT_EVAL
4964 cmd->uc_scriptID = current_SID;
4965# ifdef FEAT_CMDL_COMPL
4966 cmd->uc_compl_arg = compl_arg;
4967# endif
4968#endif
4969
4970 return OK;
4971
4972fail:
4973 vim_free(rep_buf);
4974#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4975 vim_free(compl_arg);
4976#endif
4977 return FAIL;
4978}
4979
4980/*
4981 * List of names for completion for ":command" with the EXPAND_ flag.
4982 * Must be alphabetical for completion.
4983 */
4984static struct
4985{
4986 int expand;
4987 char *name;
4988} command_complete[] =
4989{
4990 {EXPAND_AUGROUP, "augroup"},
4991 {EXPAND_BUFFERS, "buffer"},
4992 {EXPAND_COMMANDS, "command"},
4993#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4994 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00004995 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996#endif
4997 {EXPAND_DIRECTORIES, "dir"},
4998 {EXPAND_ENV_VARS, "environment"},
4999 {EXPAND_EVENTS, "event"},
5000 {EXPAND_EXPRESSION, "expression"},
5001 {EXPAND_FILES, "file"},
5002 {EXPAND_FUNCTIONS, "function"},
5003 {EXPAND_HELP, "help"},
5004 {EXPAND_HIGHLIGHT, "highlight"},
5005 {EXPAND_MAPPINGS, "mapping"},
5006 {EXPAND_MENUS, "menu"},
5007 {EXPAND_SETTINGS, "option"},
5008 {EXPAND_TAGS, "tag"},
5009 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5010 {EXPAND_USER_VARS, "var"},
5011 {0, NULL}
5012};
5013
5014 static void
5015uc_list(name, name_len)
5016 char_u *name;
5017 size_t name_len;
5018{
5019 int i, j;
5020 int found = FALSE;
5021 ucmd_T *cmd;
5022 int len;
5023 long a;
5024 garray_T *gap;
5025
5026 gap = &curbuf->b_ucmds;
5027 for (;;)
5028 {
5029 for (i = 0; i < gap->ga_len; ++i)
5030 {
5031 cmd = USER_CMD_GA(gap, i);
5032 a = cmd->uc_argt;
5033
5034 /* Skip commands which don't match the requested prefix */
5035 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5036 continue;
5037
5038 /* Put out the title first time */
5039 if (!found)
5040 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5041 found = TRUE;
5042 msg_putchar('\n');
5043 if (got_int)
5044 break;
5045
5046 /* Special cases */
5047 msg_putchar(a & BANG ? '!' : ' ');
5048 msg_putchar(a & REGSTR ? '"' : ' ');
5049 msg_putchar(gap != &ucmds ? 'b' : ' ');
5050 msg_putchar(' ');
5051
5052 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5053 len = (int)STRLEN(cmd->uc_name) + 4;
5054
5055 do {
5056 msg_putchar(' ');
5057 ++len;
5058 } while (len < 16);
5059
5060 len = 0;
5061
5062 /* Arguments */
5063 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5064 {
5065 case 0: IObuff[len++] = '0'; break;
5066 case (EXTRA): IObuff[len++] = '*'; break;
5067 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5068 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5069 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5070 }
5071
5072 do {
5073 IObuff[len++] = ' ';
5074 } while (len < 5);
5075
5076 /* Range */
5077 if (a & (RANGE|COUNT))
5078 {
5079 if (a & COUNT)
5080 {
5081 /* -count=N */
5082 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5083 len += (int)STRLEN(IObuff + len);
5084 }
5085 else if (a & DFLALL)
5086 IObuff[len++] = '%';
5087 else if (cmd->uc_def >= 0)
5088 {
5089 /* -range=N */
5090 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5091 len += (int)STRLEN(IObuff + len);
5092 }
5093 else
5094 IObuff[len++] = '.';
5095 }
5096
5097 do {
5098 IObuff[len++] = ' ';
5099 } while (len < 11);
5100
5101 /* Completion */
5102 for (j = 0; command_complete[j].expand != 0; ++j)
5103 if (command_complete[j].expand == cmd->uc_compl)
5104 {
5105 STRCPY(IObuff + len, command_complete[j].name);
5106 len += (int)STRLEN(IObuff + len);
5107 break;
5108 }
5109
5110 do {
5111 IObuff[len++] = ' ';
5112 } while (len < 21);
5113
5114 IObuff[len] = '\0';
5115 msg_outtrans(IObuff);
5116
5117 msg_outtrans_special(cmd->uc_rep, FALSE);
5118 out_flush();
5119 ui_breakcheck();
5120 if (got_int)
5121 break;
5122 }
5123 if (gap == &ucmds || i < gap->ga_len)
5124 break;
5125 gap = &ucmds;
5126 }
5127
5128 if (!found)
5129 MSG(_("No user-defined commands found"));
5130}
5131
5132 static char_u *
5133uc_fun_cmd()
5134{
5135 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5136 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5137 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5138 0xb9, 0x7f, 0};
5139 int i;
5140
5141 for (i = 0; fcmd[i]; ++i)
5142 IObuff[i] = fcmd[i] - 0x40;
5143 IObuff[i] = 0;
5144 return IObuff;
5145}
5146
5147 static int
5148uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5149 char_u *attr;
5150 size_t len;
5151 long *argt;
5152 long *def;
5153 int *flags;
5154 int *compl;
5155 char_u **compl_arg;
5156{
5157 char_u *p;
5158
5159 if (len == 0)
5160 {
5161 EMSG(_("E175: No attribute specified"));
5162 return FAIL;
5163 }
5164
5165 /* First, try the simple attributes (no arguments) */
5166 if (STRNICMP(attr, "bang", len) == 0)
5167 *argt |= BANG;
5168 else if (STRNICMP(attr, "buffer", len) == 0)
5169 *flags |= UC_BUFFER;
5170 else if (STRNICMP(attr, "register", len) == 0)
5171 *argt |= REGSTR;
5172 else if (STRNICMP(attr, "bar", len) == 0)
5173 *argt |= TRLBAR;
5174 else
5175 {
5176 int i;
5177 char_u *val = NULL;
5178 size_t vallen = 0;
5179 size_t attrlen = len;
5180
5181 /* Look for the attribute name - which is the part before any '=' */
5182 for (i = 0; i < (int)len; ++i)
5183 {
5184 if (attr[i] == '=')
5185 {
5186 val = &attr[i + 1];
5187 vallen = len - i - 1;
5188 attrlen = i;
5189 break;
5190 }
5191 }
5192
5193 if (STRNICMP(attr, "nargs", attrlen) == 0)
5194 {
5195 if (vallen == 1)
5196 {
5197 if (*val == '0')
5198 /* Do nothing - this is the default */;
5199 else if (*val == '1')
5200 *argt |= (EXTRA | NOSPC | NEEDARG);
5201 else if (*val == '*')
5202 *argt |= EXTRA;
5203 else if (*val == '?')
5204 *argt |= (EXTRA | NOSPC);
5205 else if (*val == '+')
5206 *argt |= (EXTRA | NEEDARG);
5207 else
5208 goto wrong_nargs;
5209 }
5210 else
5211 {
5212wrong_nargs:
5213 EMSG(_("E176: Invalid number of arguments"));
5214 return FAIL;
5215 }
5216 }
5217 else if (STRNICMP(attr, "range", attrlen) == 0)
5218 {
5219 *argt |= RANGE;
5220 if (vallen == 1 && *val == '%')
5221 *argt |= DFLALL;
5222 else if (val != NULL)
5223 {
5224 p = val;
5225 if (*def >= 0)
5226 {
5227two_count:
5228 EMSG(_("E177: Count cannot be specified twice"));
5229 return FAIL;
5230 }
5231
5232 *def = getdigits(&p);
5233 *argt |= (ZEROR | NOTADR);
5234
5235 if (p != val + vallen || vallen == 0)
5236 {
5237invalid_count:
5238 EMSG(_("E178: Invalid default value for count"));
5239 return FAIL;
5240 }
5241 }
5242 }
5243 else if (STRNICMP(attr, "count", attrlen) == 0)
5244 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005245 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246
5247 if (val != NULL)
5248 {
5249 p = val;
5250 if (*def >= 0)
5251 goto two_count;
5252
5253 *def = getdigits(&p);
5254
5255 if (p != val + vallen)
5256 goto invalid_count;
5257 }
5258
5259 if (*def < 0)
5260 *def = 0;
5261 }
5262 else if (STRNICMP(attr, "complete", attrlen) == 0)
5263 {
5264 char_u *arg = NULL;
5265 size_t arglen = 0;
5266
5267 if (val == NULL)
5268 {
5269 EMSG(_("E179: argument required for complete"));
5270 return FAIL;
5271 }
5272 /* Look for any argument part - which is the part after any ',' */
5273 for (i = 0; i < (int)vallen; ++i)
5274 {
5275 if (val[i] == ',')
5276 {
5277 arg = &val[i + 1];
5278 arglen = vallen - i - 1;
5279 vallen = i;
5280 break;
5281 }
5282 }
5283
5284 for (i = 0; command_complete[i].expand != 0; ++i)
5285 {
5286 if (STRLEN(command_complete[i].name) == vallen
5287 && STRNCMP(val, command_complete[i].name, vallen) == 0)
5288 {
5289 *compl = command_complete[i].expand;
5290 if (command_complete[i].expand == EXPAND_BUFFERS)
5291 *argt |= BUFNAME;
5292 else if (command_complete[i].expand == EXPAND_DIRECTORIES
5293 || command_complete[i].expand == EXPAND_FILES)
5294 *argt |= XFILE;
5295 break;
5296 }
5297 }
5298
5299 if (command_complete[i].expand == 0)
5300 {
5301 EMSG2(_("E180: Invalid complete value: %s"), val);
5302 return FAIL;
5303 }
5304#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaara466c992005-07-09 21:03:22 +00005305 if (*compl != EXPAND_USER_DEFINED && *compl != EXPAND_USER_LIST &&
5306 arg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307#else
5308 if (arg != NULL)
5309#endif
5310 {
5311 EMSG(_("E468: Completion argument only allowed for custom completion"));
5312 return FAIL;
5313 }
5314#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaara466c992005-07-09 21:03:22 +00005315 if ((*compl == EXPAND_USER_DEFINED || *compl == EXPAND_USER_LIST) &&
5316 arg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 {
5318 EMSG(_("E467: Custom completion requires a function argument"));
5319 return FAIL;
5320 }
5321 if (arg != NULL)
5322 *compl_arg = vim_strnsave(arg, (int)arglen);
5323#endif
5324 }
5325 else
5326 {
5327 char_u ch = attr[len];
5328 attr[len] = '\0';
5329 EMSG2(_("E181: Invalid attribute: %s"), attr);
5330 attr[len] = ch;
5331 return FAIL;
5332 }
5333 }
5334
5335 return OK;
5336}
5337
5338 static void
5339ex_command(eap)
5340 exarg_T *eap;
5341{
5342 char_u *name;
5343 char_u *end;
5344 char_u *p;
5345 long argt = 0;
5346 long def = -1;
5347 int flags = 0;
5348 int compl = EXPAND_NOTHING;
5349 char_u *compl_arg = NULL;
5350 int has_attr = (eap->arg[0] == '-');
5351
5352 p = eap->arg;
5353
5354 /* Check for attributes */
5355 while (*p == '-')
5356 {
5357 ++p;
5358 end = skiptowhite(p);
5359 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5360 == FAIL)
5361 return;
5362 p = skipwhite(end);
5363 }
5364
5365 /* Get the name (if any) and skip to the following argument */
5366 name = p;
5367 if (ASCII_ISALPHA(*p))
5368 while (ASCII_ISALNUM(*p))
5369 ++p;
5370 if (!ends_excmd(*p) && !vim_iswhite(*p))
5371 {
5372 EMSG(_("E182: Invalid command name"));
5373 return;
5374 }
5375 end = p;
5376
5377 /* If there is nothing after the name, and no attributes were specified,
5378 * we are listing commands
5379 */
5380 p = skipwhite(end);
5381 if (!has_attr && ends_excmd(*p))
5382 {
5383 uc_list(name, end - name);
5384 }
5385 else if (!ASCII_ISUPPER(*name))
5386 {
5387 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5388 return;
5389 }
5390 else
5391 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5392 eap->forceit);
5393}
5394
5395/*
5396 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 * Clear all user commands, global and for current buffer.
5398 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005399/*ARGSUSED*/
5400 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401ex_comclear(eap)
5402 exarg_T *eap;
5403{
5404 uc_clear(&ucmds);
5405 uc_clear(&curbuf->b_ucmds);
5406}
5407
5408/*
5409 * Clear all user commands for "gap".
5410 */
5411 void
5412uc_clear(gap)
5413 garray_T *gap;
5414{
5415 int i;
5416 ucmd_T *cmd;
5417
5418 for (i = 0; i < gap->ga_len; ++i)
5419 {
5420 cmd = USER_CMD_GA(gap, i);
5421 vim_free(cmd->uc_name);
5422 vim_free(cmd->uc_rep);
5423# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5424 vim_free(cmd->uc_compl_arg);
5425# endif
5426 }
5427 ga_clear(gap);
5428}
5429
5430 static void
5431ex_delcommand(eap)
5432 exarg_T *eap;
5433{
5434 int i = 0;
5435 ucmd_T *cmd = NULL;
5436 int cmp = -1;
5437 garray_T *gap;
5438
5439 gap = &curbuf->b_ucmds;
5440 for (;;)
5441 {
5442 for (i = 0; i < gap->ga_len; ++i)
5443 {
5444 cmd = USER_CMD_GA(gap, i);
5445 cmp = STRCMP(eap->arg, cmd->uc_name);
5446 if (cmp <= 0)
5447 break;
5448 }
5449 if (gap == &ucmds || cmp == 0)
5450 break;
5451 gap = &ucmds;
5452 }
5453
5454 if (cmp != 0)
5455 {
5456 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5457 return;
5458 }
5459
5460 vim_free(cmd->uc_name);
5461 vim_free(cmd->uc_rep);
5462# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5463 vim_free(cmd->uc_compl_arg);
5464# endif
5465
5466 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467
5468 if (i < gap->ga_len)
5469 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5470}
5471
5472 static char_u *
5473uc_split_args(arg, lenp)
5474 char_u *arg;
5475 size_t *lenp;
5476{
5477 char_u *buf;
5478 char_u *p;
5479 char_u *q;
5480 int len;
5481
5482 /* Precalculate length */
5483 p = arg;
5484 len = 2; /* Initial and final quotes */
5485
5486 while (*p)
5487 {
5488 if (p[0] == '\\' && vim_iswhite(p[1]))
5489 {
5490 len += 1;
5491 p += 2;
5492 }
5493 else if (*p == '\\' || *p == '"')
5494 {
5495 len += 2;
5496 p += 1;
5497 }
5498 else if (vim_iswhite(*p))
5499 {
5500 p = skipwhite(p);
5501 if (*p == NUL)
5502 break;
5503 len += 3; /* "," */
5504 }
5505 else
5506 {
5507 ++len;
5508 ++p;
5509 }
5510 }
5511
5512 buf = alloc(len + 1);
5513 if (buf == NULL)
5514 {
5515 *lenp = 0;
5516 return buf;
5517 }
5518
5519 p = arg;
5520 q = buf;
5521 *q++ = '"';
5522 while (*p)
5523 {
5524 if (p[0] == '\\' && vim_iswhite(p[1]))
5525 {
5526 *q++ = p[1];
5527 p += 2;
5528 }
5529 else if (*p == '\\' || *p == '"')
5530 {
5531 *q++ = '\\';
5532 *q++ = *p++;
5533 }
5534 else if (vim_iswhite(*p))
5535 {
5536 p = skipwhite(p);
5537 if (*p == NUL)
5538 break;
5539 *q++ = '"';
5540 *q++ = ',';
5541 *q++ = '"';
5542 }
5543 else
5544 {
5545 *q++ = *p++;
5546 }
5547 }
5548 *q++ = '"';
5549 *q = 0;
5550
5551 *lenp = len;
5552 return buf;
5553}
5554
5555/*
5556 * Check for a <> code in a user command.
5557 * "code" points to the '<'. "len" the length of the <> (inclusive).
5558 * "buf" is where the result is to be added.
5559 * "split_buf" points to a buffer used for splitting, caller should free it.
5560 * "split_len" is the length of what "split_buf" contains.
5561 * Returns the length of the replacement, which has been added to "buf".
5562 * Returns -1 if there was no match, and only the "<" has been copied.
5563 */
5564 static size_t
5565uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5566 char_u *code;
5567 size_t len;
5568 char_u *buf;
5569 ucmd_T *cmd; /* the user command we're expanding */
5570 exarg_T *eap; /* ex arguments */
5571 char_u **split_buf;
5572 size_t *split_len;
5573{
5574 size_t result = 0;
5575 char_u *p = code + 1;
5576 size_t l = len - 2;
5577 int quote = 0;
5578 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5579 ct_LT, ct_NONE } type = ct_NONE;
5580
5581 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5582 {
5583 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5584 p += 2;
5585 l -= 2;
5586 }
5587
5588 if (l < 1)
5589 type = ct_NONE;
5590 else if (STRNICMP(p, "args", l) == 0)
5591 type = ct_ARGS;
5592 else if (STRNICMP(p, "bang", l) == 0)
5593 type = ct_BANG;
5594 else if (STRNICMP(p, "count", l) == 0)
5595 type = ct_COUNT;
5596 else if (STRNICMP(p, "line1", l) == 0)
5597 type = ct_LINE1;
5598 else if (STRNICMP(p, "line2", l) == 0)
5599 type = ct_LINE2;
5600 else if (STRNICMP(p, "lt", l) == 0)
5601 type = ct_LT;
5602 else if (STRNICMP(p, "register", l) == 0)
5603 type = ct_REGISTER;
5604
5605 switch (type)
5606 {
5607 case ct_ARGS:
5608 /* Simple case first */
5609 if (*eap->arg == NUL)
5610 {
5611 if (quote == 1)
5612 {
5613 result = 2;
5614 if (buf != NULL)
5615 STRCPY(buf, "''");
5616 }
5617 else
5618 result = 0;
5619 break;
5620 }
5621
5622 /* When specified there is a single argument don't split it.
5623 * Works for ":Cmd %" when % is "a b c". */
5624 if ((eap->argt & NOSPC) && quote == 2)
5625 quote = 1;
5626
5627 switch (quote)
5628 {
5629 case 0: /* No quoting, no splitting */
5630 result = STRLEN(eap->arg);
5631 if (buf != NULL)
5632 STRCPY(buf, eap->arg);
5633 break;
5634 case 1: /* Quote, but don't split */
5635 result = STRLEN(eap->arg) + 2;
5636 for (p = eap->arg; *p; ++p)
5637 {
5638 if (*p == '\\' || *p == '"')
5639 ++result;
5640 }
5641
5642 if (buf != NULL)
5643 {
5644 *buf++ = '"';
5645 for (p = eap->arg; *p; ++p)
5646 {
5647 if (*p == '\\' || *p == '"')
5648 *buf++ = '\\';
5649 *buf++ = *p;
5650 }
5651 *buf = '"';
5652 }
5653
5654 break;
5655 case 2: /* Quote and split */
5656 /* This is hard, so only do it once, and cache the result */
5657 if (*split_buf == NULL)
5658 *split_buf = uc_split_args(eap->arg, split_len);
5659
5660 result = *split_len;
5661 if (buf != NULL && result != 0)
5662 STRCPY(buf, *split_buf);
5663
5664 break;
5665 }
5666 break;
5667
5668 case ct_BANG:
5669 result = eap->forceit ? 1 : 0;
5670 if (quote)
5671 result += 2;
5672 if (buf != NULL)
5673 {
5674 if (quote)
5675 *buf++ = '"';
5676 if (eap->forceit)
5677 *buf++ = '!';
5678 if (quote)
5679 *buf = '"';
5680 }
5681 break;
5682
5683 case ct_LINE1:
5684 case ct_LINE2:
5685 case ct_COUNT:
5686 {
5687 char num_buf[20];
5688 long num = (type == ct_LINE1) ? eap->line1 :
5689 (type == ct_LINE2) ? eap->line2 :
5690 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5691 size_t num_len;
5692
5693 sprintf(num_buf, "%ld", num);
5694 num_len = STRLEN(num_buf);
5695 result = num_len;
5696
5697 if (quote)
5698 result += 2;
5699
5700 if (buf != NULL)
5701 {
5702 if (quote)
5703 *buf++ = '"';
5704 STRCPY(buf, num_buf);
5705 buf += num_len;
5706 if (quote)
5707 *buf = '"';
5708 }
5709
5710 break;
5711 }
5712
5713 case ct_REGISTER:
5714 result = eap->regname ? 1 : 0;
5715 if (quote)
5716 result += 2;
5717 if (buf != NULL)
5718 {
5719 if (quote)
5720 *buf++ = '\'';
5721 if (eap->regname)
5722 *buf++ = eap->regname;
5723 if (quote)
5724 *buf = '\'';
5725 }
5726 break;
5727
5728 case ct_LT:
5729 result = 1;
5730 if (buf != NULL)
5731 *buf = '<';
5732 break;
5733
5734 default:
5735 /* Not recognized: just copy the '<' and return -1. */
5736 result = (size_t)-1;
5737 if (buf != NULL)
5738 *buf = '<';
5739 break;
5740 }
5741
5742 return result;
5743}
5744
5745 static void
5746do_ucmd(eap)
5747 exarg_T *eap;
5748{
5749 char_u *buf;
5750 char_u *p;
5751 char_u *q;
5752
5753 char_u *start;
5754 char_u *end;
5755 size_t len, totlen;
5756
5757 size_t split_len = 0;
5758 char_u *split_buf = NULL;
5759 ucmd_T *cmd;
5760#ifdef FEAT_EVAL
5761 scid_T save_current_SID = current_SID;
5762#endif
5763
5764 if (eap->cmdidx == CMD_USER)
5765 cmd = USER_CMD(eap->useridx);
5766 else
5767 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5768
5769 /*
5770 * Replace <> in the command by the arguments.
5771 */
5772 buf = NULL;
5773 for (;;)
5774 {
5775 p = cmd->uc_rep;
5776 q = buf;
5777 totlen = 0;
5778 while ((start = vim_strchr(p, '<')) != NULL
5779 && (end = vim_strchr(start + 1, '>')) != NULL)
5780 {
5781 /* Include the '>' */
5782 ++end;
5783
5784 /* Take everything up to the '<' */
5785 len = start - p;
5786 if (buf == NULL)
5787 totlen += len;
5788 else
5789 {
5790 mch_memmove(q, p, len);
5791 q += len;
5792 }
5793
5794 len = uc_check_code(start, end - start, q, cmd, eap,
5795 &split_buf, &split_len);
5796 if (len == (size_t)-1)
5797 {
5798 /* no match, continue after '<' */
5799 p = start + 1;
5800 len = 1;
5801 }
5802 else
5803 p = end;
5804 if (buf == NULL)
5805 totlen += len;
5806 else
5807 q += len;
5808 }
5809 if (buf != NULL) /* second time here, finished */
5810 {
5811 STRCPY(q, p);
5812 break;
5813 }
5814
5815 totlen += STRLEN(p); /* Add on the trailing characters */
5816 buf = alloc((unsigned)(totlen + 1));
5817 if (buf == NULL)
5818 {
5819 vim_free(split_buf);
5820 return;
5821 }
5822 }
5823
5824#ifdef FEAT_EVAL
5825 current_SID = cmd->uc_scriptID;
5826#endif
5827 (void)do_cmdline(buf, eap->getline, eap->cookie,
5828 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5829#ifdef FEAT_EVAL
5830 current_SID = save_current_SID;
5831#endif
5832 vim_free(buf);
5833 vim_free(split_buf);
5834}
5835
5836# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5837 static char_u *
5838get_user_command_name(idx)
5839 int idx;
5840{
5841 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5842}
5843
5844/*
5845 * Function given to ExpandGeneric() to obtain the list of user command names.
5846 */
5847/*ARGSUSED*/
5848 char_u *
5849get_user_commands(xp, idx)
5850 expand_T *xp;
5851 int idx;
5852{
5853 if (idx < curbuf->b_ucmds.ga_len)
5854 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5855 idx -= curbuf->b_ucmds.ga_len;
5856 if (idx < ucmds.ga_len)
5857 return USER_CMD(idx)->uc_name;
5858 return NULL;
5859}
5860
5861/*
5862 * Function given to ExpandGeneric() to obtain the list of user command
5863 * attributes.
5864 */
5865/*ARGSUSED*/
5866 char_u *
5867get_user_cmd_flags(xp, idx)
5868 expand_T *xp;
5869 int idx;
5870{
5871 static char *user_cmd_flags[] =
5872 {"bang", "bar", "buffer", "complete", "count",
5873 "nargs", "range", "register"};
5874
5875 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5876 return NULL;
5877 return (char_u *)user_cmd_flags[idx];
5878}
5879
5880/*
5881 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5882 */
5883/*ARGSUSED*/
5884 char_u *
5885get_user_cmd_nargs(xp, idx)
5886 expand_T *xp;
5887 int idx;
5888{
5889 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5890
5891 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5892 return NULL;
5893 return (char_u *)user_cmd_nargs[idx];
5894}
5895
5896/*
5897 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5898 */
5899/*ARGSUSED*/
5900 char_u *
5901get_user_cmd_complete(xp, idx)
5902 expand_T *xp;
5903 int idx;
5904{
5905 return (char_u *)command_complete[idx].name;
5906}
5907# endif /* FEAT_CMDL_COMPL */
5908
5909#endif /* FEAT_USR_CMDS */
5910
5911 static void
5912ex_colorscheme(eap)
5913 exarg_T *eap;
5914{
5915 if (load_colors(eap->arg) == FAIL)
5916 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
5917}
5918
5919 static void
5920ex_highlight(eap)
5921 exarg_T *eap;
5922{
5923 if (*eap->arg == NUL && eap->cmd[2] == '!')
5924 MSG(_("Greetings, Vim user!"));
5925 do_highlight(eap->arg, eap->forceit, FALSE);
5926}
5927
5928
5929/*
5930 * Call this function if we thought we were going to exit, but we won't
5931 * (because of an error). May need to restore the terminal mode.
5932 */
5933 void
5934not_exiting()
5935{
5936 exiting = FALSE;
5937 settmode(TMODE_RAW);
5938}
5939
5940/*
5941 * ":quit": quit current window, quit Vim if closed the last window.
5942 */
5943 static void
5944ex_quit(eap)
5945 exarg_T *eap;
5946{
5947#ifdef FEAT_CMDWIN
5948 if (cmdwin_type != 0)
5949 {
5950 cmdwin_result = Ctrl_C;
5951 return;
5952 }
5953#endif
5954
5955#ifdef FEAT_NETBEANS_INTG
5956 netbeansForcedQuit = eap->forceit;
5957#endif
5958
5959 /*
5960 * If there are more files or windows we won't exit.
5961 */
5962 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5963 exiting = TRUE;
5964 if ((!P_HID(curbuf)
5965 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
5966 || check_more(TRUE, eap->forceit) == FAIL
5967 || (only_one_window() && check_changed_any(eap->forceit)))
5968 {
5969 not_exiting();
5970 }
5971 else
5972 {
5973#ifdef FEAT_WINDOWS
5974 if (only_one_window()) /* quit last window */
5975#endif
5976 getout(0);
5977#ifdef FEAT_WINDOWS
5978# ifdef FEAT_GUI
5979 need_mouse_correct = TRUE;
5980# endif
5981 /* close window; may free buffer */
5982 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
5983#endif
5984 }
5985}
5986
5987/*
5988 * ":cquit".
5989 */
5990/*ARGSUSED*/
5991 static void
5992ex_cquit(eap)
5993 exarg_T *eap;
5994{
5995 getout(1); /* this does not always pass on the exit code to the Manx
5996 compiler. why? */
5997}
5998
5999/*
6000 * ":qall": try to quit all windows
6001 */
6002 static void
6003ex_quit_all(eap)
6004 exarg_T *eap;
6005{
6006# ifdef FEAT_CMDWIN
6007 if (cmdwin_type != 0)
6008 {
6009 if (eap->forceit)
6010 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6011 else
6012 cmdwin_result = K_XF2;
6013 return;
6014 }
6015# endif
6016 exiting = TRUE;
6017 if (eap->forceit || !check_changed_any(FALSE))
6018 getout(0);
6019 not_exiting();
6020}
6021
6022#ifdef FEAT_WINDOWS
6023/*
6024 * ":close": close current window, unless it is the last one
6025 */
6026 static void
6027ex_close(eap)
6028 exarg_T *eap;
6029{
6030# ifdef FEAT_CMDWIN
6031 if (cmdwin_type != 0)
6032 cmdwin_result = K_IGNORE;
6033 else
6034# endif
6035 ex_win_close(eap, curwin);
6036}
6037
6038 static void
6039ex_win_close(eap, win)
6040 exarg_T *eap;
6041 win_T *win;
6042{
6043 int need_hide;
6044 buf_T *buf = win->w_buffer;
6045
6046 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
6047 if (need_hide && !P_HID(buf) && !eap->forceit)
6048 {
6049#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6050 if ((p_confirm || cmdmod.confirm) && p_write)
6051 {
6052 dialog_changed(buf, FALSE);
6053 if (buf_valid(buf) && bufIsChanged(buf))
6054 return;
6055 need_hide = FALSE;
6056 }
6057 else
6058#endif
6059 {
6060 EMSG(_(e_nowrtmsg));
6061 return;
6062 }
6063 }
6064
6065#ifdef FEAT_GUI
6066 need_mouse_correct = TRUE;
6067#endif
6068 /* free buffer when not hiding it or when it's a scratch buffer */
6069 win_close(win, !need_hide && !P_HID(buf));
6070}
6071
6072#ifdef FEAT_QUICKFIX
6073/*
6074 * ":pclose": Close any preview window.
6075 */
6076 static void
6077ex_pclose(eap)
6078 exarg_T *eap;
6079{
6080 win_T *win;
6081
6082 for (win = firstwin; win != NULL; win = win->w_next)
6083 if (win->w_p_pvw)
6084 {
6085 ex_win_close(eap, win);
6086 break;
6087 }
6088}
6089#endif
6090
6091/*
6092 * ":only".
6093 */
6094 static void
6095ex_only(eap)
6096 exarg_T *eap;
6097{
6098# ifdef FEAT_GUI
6099 need_mouse_correct = TRUE;
6100# endif
6101 close_others(TRUE, eap->forceit);
6102}
6103
6104/*
6105 * ":all" and ":sall".
6106 */
6107 static void
6108ex_all(eap)
6109 exarg_T *eap;
6110{
6111 if (eap->addr_count == 0)
6112 eap->line2 = 9999;
6113 do_arg_all((int)eap->line2, eap->forceit);
6114}
6115#endif /* FEAT_WINDOWS */
6116
6117 static void
6118ex_hide(eap)
6119 exarg_T *eap;
6120{
6121 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6122 eap->errmsg = e_invarg;
6123 else
6124 {
6125 /* ":hide" or ":hide | cmd": hide current window */
6126 eap->nextcmd = check_nextcmd(eap->arg);
6127#ifdef FEAT_WINDOWS
6128 if (!eap->skip)
6129 {
6130# ifdef FEAT_GUI
6131 need_mouse_correct = TRUE;
6132# endif
6133 win_close(curwin, FALSE); /* don't free buffer */
6134 }
6135#endif
6136 }
6137}
6138
6139/*
6140 * ":stop" and ":suspend": Suspend Vim.
6141 */
6142 static void
6143ex_stop(eap)
6144 exarg_T *eap;
6145{
6146 /*
6147 * Disallow suspending for "rvim".
6148 */
6149 if (!check_restricted()
6150#ifdef WIN3264
6151 /*
6152 * Check if external commands are allowed now.
6153 */
6154 && can_end_termcap_mode(TRUE)
6155#endif
6156 )
6157 {
6158 if (!eap->forceit)
6159 autowrite_all();
6160 windgoto((int)Rows - 1, 0);
6161 out_char('\n');
6162 out_flush();
6163 stoptermcap();
6164 out_flush(); /* needed for SUN to restore xterm buffer */
6165#ifdef FEAT_TITLE
6166 mch_restore_title(3); /* restore window titles */
6167#endif
6168 ui_suspend(); /* call machine specific function */
6169#ifdef FEAT_TITLE
6170 maketitle();
6171 resettitle(); /* force updating the title */
6172#endif
6173 starttermcap();
6174 scroll_start(); /* scroll screen before redrawing */
6175 redraw_later_clear();
6176 shell_resized(); /* may have resized window */
6177 }
6178}
6179
6180/*
6181 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6182 */
6183 static void
6184ex_exit(eap)
6185 exarg_T *eap;
6186{
6187#ifdef FEAT_CMDWIN
6188 if (cmdwin_type != 0)
6189 {
6190 cmdwin_result = Ctrl_C;
6191 return;
6192 }
6193#endif
6194
6195 /*
6196 * if more files or windows we won't exit
6197 */
6198 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6199 exiting = TRUE;
6200 if ( ((eap->cmdidx == CMD_wq
6201 || curbufIsChanged())
6202 && do_write(eap) == FAIL)
6203 || check_more(TRUE, eap->forceit) == FAIL
6204 || (only_one_window() && check_changed_any(eap->forceit)))
6205 {
6206 not_exiting();
6207 }
6208 else
6209 {
6210#ifdef FEAT_WINDOWS
6211 if (only_one_window()) /* quit last window, exit Vim */
6212#endif
6213 getout(0);
6214#ifdef FEAT_WINDOWS
6215# ifdef FEAT_GUI
6216 need_mouse_correct = TRUE;
6217# endif
6218 /* quit current window, may free buffer */
6219 win_close(curwin, !P_HID(curwin->w_buffer));
6220#endif
6221 }
6222}
6223
6224/*
6225 * ":print", ":list", ":number".
6226 */
6227 static void
6228ex_print(eap)
6229 exarg_T *eap;
6230{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006231 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6232 EMSG(_(e_emptybuf));
6233 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006235 for ( ;!got_int; ui_breakcheck())
6236 {
6237 print_line(eap->line1,
6238 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6239 || (eap->flags & EXFLAG_NR)),
6240 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6241 if (++eap->line1 > eap->line2)
6242 break;
6243 out_flush(); /* show one line at a time */
6244 }
6245 setpcmark();
6246 /* put cursor at last line */
6247 curwin->w_cursor.lnum = eap->line2;
6248 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 }
6250
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252}
6253
6254#ifdef FEAT_BYTEOFF
6255 static void
6256ex_goto(eap)
6257 exarg_T *eap;
6258{
6259 goto_byte(eap->line2);
6260}
6261#endif
6262
6263/*
6264 * ":shell".
6265 */
6266/*ARGSUSED*/
6267 static void
6268ex_shell(eap)
6269 exarg_T *eap;
6270{
6271 do_shell(NULL, 0);
6272}
6273
6274#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6275 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
6276 || defined(PROTO)
6277
6278/*
6279 * Handle a file drop. The code is here because a drop is *nearly* like an
6280 * :args command, but not quite (we have a list of exact filenames, so we
6281 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6282 * code is very similar to :args and hence needs access to a lot of the static
6283 * functions in this file.
6284 *
6285 * The list should be allocated using alloc(), as should each item in the
6286 * list. This function takes over responsibility for freeing the list.
6287 *
6288 * XXX The list is made into the arggument list. This is freed using
6289 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6290 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6291 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6292 * file functionality is (currently) not in EMX this is not presently a
6293 * problem.
6294 */
6295 void
6296handle_drop(filec, filev, split)
6297 int filec; /* the number of files dropped */
6298 char_u **filev; /* the list of files dropped */
6299 int split; /* force splitting the window */
6300{
6301 exarg_T ea;
6302 int save_msg_scroll = msg_scroll;
6303
6304# ifdef FEAT_CMDWIN
6305 if (cmdwin_type != 0)
6306 return;
6307# endif
6308
6309 /* Check whether the current buffer is changed. If so, we will need
6310 * to split the current window or data could be lost.
6311 * We don't need to check if the 'hidden' option is set, as in this
6312 * case the buffer won't be lost.
6313 */
6314 if (!P_HID(curbuf) && !split)
6315 {
6316 ++emsg_off;
6317 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6318 --emsg_off;
6319 }
6320 if (split)
6321 {
6322# ifdef FEAT_WINDOWS
6323 if (win_split(0, 0) == FAIL)
6324 return;
6325# ifdef FEAT_SCROLLBIND
6326 curwin->w_p_scb = FALSE;
6327# endif
6328
6329 /* When splitting the window, create a new alist. Otherwise the
6330 * existing one is overwritten. */
6331 alist_unlink(curwin->w_alist);
6332 alist_new();
6333# else
6334 return; /* can't split, always fail */
6335# endif
6336 }
6337
6338 /*
6339 * Set up the new argument list.
6340 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006341 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342
6343 /*
6344 * Move to the first file.
6345 */
6346 /* Fake up a minimal "next" command for do_argfile() */
6347 vim_memset(&ea, 0, sizeof(ea));
6348 ea.cmd = (char_u *)"next";
6349 do_argfile(&ea, 0);
6350
6351 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6352 * mode that is not needed here. */
6353 need_start_insertmode = FALSE;
6354
6355 /* Restore msg_scroll, otherwise a following command may cause scrolling
6356 * unexpectedly. The screen will be redrawn by the caller, thus
6357 * msg_scroll being set by displaying a message is irrelevant. */
6358 msg_scroll = save_msg_scroll;
6359}
6360#endif
6361
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362/*
6363 * Clear an argument list: free all file names and reset it to zero entries.
6364 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006365 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366alist_clear(al)
6367 alist_T *al;
6368{
6369 while (--al->al_ga.ga_len >= 0)
6370 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6371 ga_clear(&al->al_ga);
6372}
6373
6374/*
6375 * Init an argument list.
6376 */
6377 void
6378alist_init(al)
6379 alist_T *al;
6380{
6381 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6382}
6383
6384#if defined(FEAT_WINDOWS) || defined(PROTO)
6385
6386/*
6387 * Remove a reference from an argument list.
6388 * Ignored when the argument list is the global one.
6389 * If the argument list is no longer used by any window, free it.
6390 */
6391 void
6392alist_unlink(al)
6393 alist_T *al;
6394{
6395 if (al != &global_alist && --al->al_refcount <= 0)
6396 {
6397 alist_clear(al);
6398 vim_free(al);
6399 }
6400}
6401
6402# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6403/*
6404 * Create a new argument list and use it for the current window.
6405 */
6406 void
6407alist_new()
6408{
6409 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6410 if (curwin->w_alist == NULL)
6411 {
6412 curwin->w_alist = &global_alist;
6413 ++global_alist.al_refcount;
6414 }
6415 else
6416 {
6417 curwin->w_alist->al_refcount = 1;
6418 alist_init(curwin->w_alist);
6419 }
6420}
6421# endif
6422#endif
6423
6424#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6425/*
6426 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006427 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6428 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 */
6430 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006431alist_expand(fnum_list, fnum_len)
6432 int *fnum_list;
6433 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434{
6435 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006436 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 char_u **new_arg_files;
6438 int new_arg_file_count;
6439 char_u *save_p_su = p_su;
6440 int i;
6441
6442 /* Don't use 'suffixes' here. This should work like the shell did the
6443 * expansion. Also, the vimrc file isn't read yet, thus the user
6444 * can't set the options. */
6445 p_su = empty_option;
6446 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6447 if (old_arg_files != NULL)
6448 {
6449 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006450 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6451 old_arg_count = GARGCOUNT;
6452 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 &new_arg_file_count, &new_arg_files,
6454 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6455 && new_arg_file_count > 0)
6456 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006457 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6458 TRUE, fnum_list, fnum_len);
6459 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 }
6462 p_su = save_p_su;
6463}
6464#endif
6465
6466/*
6467 * Set the argument list for the current window.
6468 * Takes over the allocated files[] and the allocated fnames in it.
6469 */
6470 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006471alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 alist_T *al;
6473 int count;
6474 char_u **files;
6475 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006476 int *fnum_list;
6477 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
6479 int i;
6480
6481 alist_clear(al);
6482 if (ga_grow(&al->al_ga, count) == OK)
6483 {
6484 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006485 {
6486 if (got_int)
6487 {
6488 /* When adding many buffers this can take a long time. Allow
6489 * interrupting here. */
6490 while (i < count)
6491 vim_free(files[i++]);
6492 break;
6493 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006494
6495 /* May set buffer name of a buffer previously used for the
6496 * argument list, so that it's re-used by alist_add. */
6497 if (fnum_list != NULL && i < fnum_len)
6498 buf_set_name(fnum_list[i], files[i]);
6499
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006501 ui_breakcheck();
6502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 vim_free(files);
6504 }
6505 else
6506 FreeWild(count, files);
6507#ifdef FEAT_WINDOWS
6508 if (al == &global_alist)
6509#endif
6510 arg_had_last = FALSE;
6511}
6512
6513/*
6514 * Add file "fname" to argument list "al".
6515 * "fname" must have been allocated and "al" must have been checked for room.
6516 */
6517 void
6518alist_add(al, fname, set_fnum)
6519 alist_T *al;
6520 char_u *fname;
6521 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6522{
6523 if (fname == NULL) /* don't add NULL file names */
6524 return;
6525#ifdef BACKSLASH_IN_FILENAME
6526 slash_adjust(fname);
6527#endif
6528 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6529 if (set_fnum > 0)
6530 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6531 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6532 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533}
6534
6535#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6536/*
6537 * Adjust slashes in file names. Called after 'shellslash' was set.
6538 */
6539 void
6540alist_slash_adjust()
6541{
6542 int i;
6543# ifdef FEAT_WINDOWS
6544 win_T *wp;
6545# endif
6546
6547 for (i = 0; i < GARGCOUNT; ++i)
6548 if (GARGLIST[i].ae_fname != NULL)
6549 slash_adjust(GARGLIST[i].ae_fname);
6550# ifdef FEAT_WINDOWS
6551 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6552 if (wp->w_alist != &global_alist)
6553 for (i = 0; i < WARGCOUNT(wp); ++i)
6554 if (WARGLIST(wp)[i].ae_fname != NULL)
6555 slash_adjust(WARGLIST(wp)[i].ae_fname);
6556# endif
6557}
6558#endif
6559
6560/*
6561 * ":preserve".
6562 */
6563/*ARGSUSED*/
6564 static void
6565ex_preserve(eap)
6566 exarg_T *eap;
6567{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006568 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 ml_preserve(curbuf, TRUE);
6570}
6571
6572/*
6573 * ":recover".
6574 */
6575 static void
6576ex_recover(eap)
6577 exarg_T *eap;
6578{
6579 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6580 recoverymode = TRUE;
6581 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6582 && (*eap->arg == NUL
6583 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6584 ml_recover();
6585 recoverymode = FALSE;
6586}
6587
6588/*
6589 * Command modifier used in a wrong way.
6590 */
6591 static void
6592ex_wrongmodifier(eap)
6593 exarg_T *eap;
6594{
6595 eap->errmsg = e_invcmd;
6596}
6597
6598#ifdef FEAT_WINDOWS
6599/*
6600 * :sview [+command] file split window with new file, read-only
6601 * :split [[+command] file] split window with current or new file
6602 * :vsplit [[+command] file] split window vertically with current or new file
6603 * :new [[+command] file] split window with no or new file
6604 * :vnew [[+command] file] split vertically window with no or new file
6605 * :sfind [+command] file split window with file in 'path'
6606 */
6607 void
6608ex_splitview(eap)
6609 exarg_T *eap;
6610{
6611 win_T *old_curwin;
6612#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6613 char_u *fname = NULL;
6614#endif
6615#ifdef FEAT_BROWSE
6616 int browse_flag = cmdmod.browse;
6617#endif
6618
6619#ifndef FEAT_VERTSPLIT
6620 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6621 {
6622 ex_ni(eap);
6623 return;
6624 }
6625#endif
6626
6627 old_curwin = curwin;
6628#ifdef FEAT_GUI
6629 need_mouse_correct = TRUE;
6630#endif
6631
6632#ifdef FEAT_QUICKFIX
6633 /* A ":split" in the quickfix window works like ":new". Don't want two
6634 * quickfix windows. */
6635 if (bt_quickfix(curbuf))
6636 {
6637 if (eap->cmdidx == CMD_split)
6638 eap->cmdidx = CMD_new;
6639# ifdef FEAT_VERTSPLIT
6640 if (eap->cmdidx == CMD_vsplit)
6641 eap->cmdidx = CMD_vnew;
6642# endif
6643 }
6644#endif
6645
6646#ifdef FEAT_SEARCHPATH
6647 if (eap->cmdidx == CMD_sfind)
6648 {
6649 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6650 FNAME_MESS, TRUE, curbuf->b_ffname);
6651 if (fname == NULL)
6652 goto theend;
6653 eap->arg = fname;
6654 }
6655# ifdef FEAT_BROWSE
6656 else
6657# endif
6658#endif
6659#ifdef FEAT_BROWSE
6660 if (cmdmod.browse
6661# ifdef FEAT_VERTSPLIT
6662 && eap->cmdidx != CMD_vnew
6663#endif
6664 && eap->cmdidx != CMD_new)
6665 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006666 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 eap->arg, NULL, NULL, NULL, curbuf);
6668 if (fname == NULL)
6669 goto theend;
6670 eap->arg = fname;
6671 }
6672 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
6673#endif
6674
6675 if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
6676 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6677 {
6678#ifdef FEAT_SCROLLBIND
6679 /* Reset 'scrollbind' when editing another file, but keep it when
6680 * doing ":split" without arguments. */
6681 if (*eap->arg != NUL
6682#ifdef FEAT_BROWSE
6683 || cmdmod.browse
6684#endif
6685 )
6686 curwin->w_p_scb = FALSE;
6687 else
6688 do_check_scrollbind(FALSE);
6689#endif
6690 do_exedit(eap, old_curwin);
6691 }
6692
6693#ifdef FEAT_BROWSE
6694 cmdmod.browse = browse_flag;
6695#endif
6696
6697#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6698theend:
6699 vim_free(fname);
6700#endif
6701}
6702#endif
6703
6704/*
6705 * ":mode": Set screen mode.
6706 * If no argument given, just get the screen size and redraw.
6707 */
6708 static void
6709ex_mode(eap)
6710 exarg_T *eap;
6711{
6712 if (*eap->arg == NUL)
6713 shell_resized();
6714 else
6715 mch_screenmode(eap->arg);
6716}
6717
6718#ifdef FEAT_WINDOWS
6719/*
6720 * ":resize".
6721 * set, increment or decrement current window height
6722 */
6723 static void
6724ex_resize(eap)
6725 exarg_T *eap;
6726{
6727 int n;
6728 win_T *wp = curwin;
6729
6730 if (eap->addr_count > 0)
6731 {
6732 n = eap->line2;
6733 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6734 ;
6735 }
6736
6737#ifdef FEAT_GUI
6738 need_mouse_correct = TRUE;
6739#endif
6740 n = atol((char *)eap->arg);
6741#ifdef FEAT_VERTSPLIT
6742 if (cmdmod.split & WSP_VERT)
6743 {
6744 if (*eap->arg == '-' || *eap->arg == '+')
6745 n += W_WIDTH(curwin);
6746 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6747 n = 9999;
6748 win_setwidth_win((int)n, wp);
6749 }
6750 else
6751#endif
6752 {
6753 if (*eap->arg == '-' || *eap->arg == '+')
6754 n += curwin->w_height;
6755 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6756 n = 9999;
6757 win_setheight_win((int)n, wp);
6758 }
6759}
6760#endif
6761
6762/*
6763 * ":find [+command] <file>" command.
6764 */
6765 static void
6766ex_find(eap)
6767 exarg_T *eap;
6768{
6769#ifdef FEAT_SEARCHPATH
6770 char_u *fname;
6771 int count;
6772
6773 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6774 TRUE, curbuf->b_ffname);
6775 if (eap->addr_count > 0)
6776 {
6777 /* Repeat finding the file "count" times. This matters when it
6778 * appears several times in the path. */
6779 count = eap->line2;
6780 while (fname != NULL && --count > 0)
6781 {
6782 vim_free(fname);
6783 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6784 FALSE, curbuf->b_ffname);
6785 }
6786 }
6787
6788 if (fname != NULL)
6789 {
6790 eap->arg = fname;
6791#endif
6792 do_exedit(eap, NULL);
6793#ifdef FEAT_SEARCHPATH
6794 vim_free(fname);
6795 }
6796#endif
6797}
6798
6799/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006800 * ":open" simulation: for now just work like ":visual".
6801 */
6802 static void
6803ex_open(eap)
6804 exarg_T *eap;
6805{
6806 regmatch_T regmatch;
6807 char_u *p;
6808
6809 curwin->w_cursor.lnum = eap->line2;
6810 beginline(BL_SOL | BL_FIX);
6811 if (*eap->arg == '/')
6812 {
6813 /* ":open /pattern/": put cursor in column found with pattern */
6814 ++eap->arg;
6815 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6816 *p = NUL;
6817 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6818 if (regmatch.regprog != NULL)
6819 {
6820 regmatch.rm_ic = p_ic;
6821 p = ml_get_curline();
6822 if (vim_regexec(&regmatch, p, (colnr_T)0))
6823 curwin->w_cursor.col = regmatch.startp[0] - p;
6824 else
6825 EMSG(_(e_nomatch));
6826 vim_free(regmatch.regprog);
6827 }
6828 /* Move to the NUL, ignore any other arguments. */
6829 eap->arg += STRLEN(eap->arg);
6830 }
6831 check_cursor();
6832
6833 eap->cmdidx = CMD_visual;
6834 do_exedit(eap, NULL);
6835}
6836
6837/*
6838 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 */
6840 static void
6841ex_edit(eap)
6842 exarg_T *eap;
6843{
6844 do_exedit(eap, NULL);
6845}
6846
6847/*
6848 * ":edit <file>" command and alikes.
6849 */
6850/*ARGSUSED*/
6851 void
6852do_exedit(eap, old_curwin)
6853 exarg_T *eap;
6854 win_T *old_curwin; /* curwin before doing a split or NULL */
6855{
6856 int n;
6857#ifdef FEAT_WINDOWS
6858 int need_hide;
6859#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006860 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861
6862 /*
6863 * ":vi" command ends Ex mode.
6864 */
6865 if (exmode_active && (eap->cmdidx == CMD_visual
6866 || eap->cmdidx == CMD_view))
6867 {
6868 exmode_active = FALSE;
6869 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006870 {
6871 /* Special case: ":global/pat/visual\NLvi-commands" */
6872 if (global_busy)
6873 {
6874 int rd = RedrawingDisabled;
6875 int nwr = no_wait_return;
6876 int ms = msg_scroll;
6877#ifdef FEAT_GUI
6878 int he = hold_gui_events;
6879#endif
6880
6881 if (eap->nextcmd != NULL)
6882 {
6883 stuffReadbuff(eap->nextcmd);
6884 eap->nextcmd = NULL;
6885 }
6886
6887 if (exmode_was != EXMODE_VIM)
6888 settmode(TMODE_RAW);
6889 RedrawingDisabled = 0;
6890 no_wait_return = 0;
6891 need_wait_return = FALSE;
6892 msg_scroll = 0;
6893#ifdef FEAT_GUI
6894 hold_gui_events = 0;
6895#endif
6896 must_redraw = CLEAR;
6897
6898 main_loop(FALSE, TRUE);
6899
6900 RedrawingDisabled = rd;
6901 no_wait_return = nwr;
6902 msg_scroll = ms;
6903#ifdef FEAT_GUI
6904 hold_gui_events = he;
6905#endif
6906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 }
6910
6911 if ((eap->cmdidx == CMD_new
6912#ifdef FEAT_VERTSPLIT
6913 || eap->cmdidx == CMD_vnew
6914#endif
6915 ) && *eap->arg == NUL)
6916 {
6917 /* ":new" without argument: edit an new empty buffer */
6918 setpcmark();
6919 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
6920 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
6921 }
6922 else if ((eap->cmdidx != CMD_split
6923#ifdef FEAT_VERTSPLIT
6924 && eap->cmdidx != CMD_vsplit
6925#endif
6926 )
6927 || *eap->arg != NUL
6928#ifdef FEAT_BROWSE
6929 || cmdmod.browse
6930#endif
6931 )
6932 {
6933 n = readonlymode;
6934 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6935 readonlymode = TRUE;
6936 else if (eap->cmdidx == CMD_enew)
6937 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
6938 empty buffer */
6939 setpcmark();
6940 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6941 NULL, eap,
6942 /* ":edit" goes to first line if Vi compatible */
6943 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6944 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6945 ? ECMD_ONE : eap->do_ecmd_lnum,
6946 (P_HID(curbuf) ? ECMD_HIDE : 0)
6947 + (eap->forceit ? ECMD_FORCEIT : 0)
6948#ifdef FEAT_LISTCMDS
6949 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
6950#endif
6951 ) == FAIL)
6952 {
6953 /* Editing the file failed. If the window was split, close it. */
6954#ifdef FEAT_WINDOWS
6955 if (old_curwin != NULL)
6956 {
6957 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
6958 if (!need_hide || P_HID(curbuf))
6959 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006960# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6961 cleanup_T cs;
6962
6963 /* Reset the error/interrupt/exception state here so that
6964 * aborting() returns FALSE when closing a window. */
6965 enter_cleanup(&cs);
6966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967# ifdef FEAT_GUI
6968 need_mouse_correct = TRUE;
6969# endif
6970 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006971
6972# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6973 /* Restore the error/interrupt/exception state if not
6974 * discarded by a new aborting error, interrupt, or
6975 * uncaught exception. */
6976 leave_cleanup(&cs);
6977# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 }
6979 }
6980#endif
6981 }
6982 else if (readonlymode && curbuf->b_nwindows == 1)
6983 {
6984 /* When editing an already visited buffer, 'readonly' won't be set
6985 * but the previous value is kept. With ":view" and ":sview" we
6986 * want the file to be readonly, except when another window is
6987 * editing the same buffer. */
6988 curbuf->b_p_ro = TRUE;
6989 }
6990 readonlymode = n;
6991 }
6992 else
6993 {
6994 if (eap->do_ecmd_cmd != NULL)
6995 do_cmdline_cmd(eap->do_ecmd_cmd);
6996#ifdef FEAT_TITLE
6997 n = curwin->w_arg_idx_invalid;
6998#endif
6999 check_arg_idx(curwin);
7000#ifdef FEAT_TITLE
7001 if (n != curwin->w_arg_idx_invalid)
7002 maketitle();
7003#endif
7004 }
7005
7006#ifdef FEAT_WINDOWS
7007 /*
7008 * if ":split file" worked, set alternate file name in old window to new
7009 * file
7010 */
7011 if (old_curwin != NULL
7012 && *eap->arg != NUL
7013 && curwin != old_curwin
7014 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007015 && old_curwin->w_buffer != curbuf
7016 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 old_curwin->w_alt_fnum = curbuf->b_fnum;
7018#endif
7019
7020 ex_no_reprint = TRUE;
7021}
7022
7023#ifndef FEAT_GUI
7024/*
7025 * ":gui" and ":gvim" when there is no GUI.
7026 */
7027 static void
7028ex_nogui(eap)
7029 exarg_T *eap;
7030{
7031 eap->errmsg = e_nogvim;
7032}
7033#endif
7034
7035#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7036 static void
7037ex_tearoff(eap)
7038 exarg_T *eap;
7039{
7040 gui_make_tearoff(eap->arg);
7041}
7042#endif
7043
Bram Moolenaar843ee412004-06-30 16:16:41 +00007044#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_KDE) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 static void
7046ex_popup(eap)
7047 exarg_T *eap;
7048{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007049 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050}
7051#endif
7052
7053/*ARGSUSED*/
7054 static void
7055ex_swapname(eap)
7056 exarg_T *eap;
7057{
7058 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7059 MSG(_("No swap file"));
7060 else
7061 msg(curbuf->b_ml.ml_mfp->mf_fname);
7062}
7063
7064/*
7065 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7066 * offset.
7067 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7068 */
7069/*ARGSUSED*/
7070 static void
7071ex_syncbind(eap)
7072 exarg_T *eap;
7073{
7074#ifdef FEAT_SCROLLBIND
7075 win_T *wp;
7076 long topline;
7077 long y;
7078 linenr_T old_linenr = curwin->w_cursor.lnum;
7079
7080 setpcmark();
7081
7082 /*
7083 * determine max topline
7084 */
7085 if (curwin->w_p_scb)
7086 {
7087 topline = curwin->w_topline;
7088 for (wp = firstwin; wp; wp = wp->w_next)
7089 {
7090 if (wp->w_p_scb && wp->w_buffer)
7091 {
7092 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7093 if (topline > y)
7094 topline = y;
7095 }
7096 }
7097 if (topline < 1)
7098 topline = 1;
7099 }
7100 else
7101 {
7102 topline = 1;
7103 }
7104
7105
7106 /*
7107 * set all scrollbind windows to the same topline
7108 */
7109 wp = curwin;
7110 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7111 {
7112 if (curwin->w_p_scb)
7113 {
7114 y = topline - curwin->w_topline;
7115 if (y > 0)
7116 scrollup(y, TRUE);
7117 else
7118 scrolldown(-y, TRUE);
7119 curwin->w_scbind_pos = topline;
7120 redraw_later(VALID);
7121 cursor_correct();
7122#ifdef FEAT_WINDOWS
7123 curwin->w_redr_status = TRUE;
7124#endif
7125 }
7126 }
7127 curwin = wp;
7128 if (curwin->w_p_scb)
7129 {
7130 did_syncbind = TRUE;
7131 checkpcmark();
7132 if (old_linenr != curwin->w_cursor.lnum)
7133 {
7134 char_u ctrl_o[2];
7135
7136 ctrl_o[0] = Ctrl_O;
7137 ctrl_o[1] = 0;
7138 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7139 }
7140 }
7141#endif
7142}
7143
7144
7145 static void
7146ex_read(eap)
7147 exarg_T *eap;
7148{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007149 int i;
7150 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7151 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152
7153 if (eap->usefilter) /* :r!cmd */
7154 do_bang(1, eap, FALSE, FALSE, TRUE);
7155 else
7156 {
7157 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7158 return;
7159
7160#ifdef FEAT_BROWSE
7161 if (cmdmod.browse)
7162 {
7163 char_u *browseFile;
7164
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007165 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 NULL, NULL, NULL, curbuf);
7167 if (browseFile != NULL)
7168 {
7169 i = readfile(browseFile, NULL,
7170 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7171 vim_free(browseFile);
7172 }
7173 else
7174 i = OK;
7175 }
7176 else
7177#endif
7178 if (*eap->arg == NUL)
7179 {
7180 if (check_fname() == FAIL) /* check for no file name */
7181 return;
7182 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7183 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7184 }
7185 else
7186 {
7187 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7188 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7189 i = readfile(eap->arg, NULL,
7190 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7191
7192 }
7193 if (i == FAIL)
7194 {
7195#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7196 if (!aborting())
7197#endif
7198 EMSG2(_(e_notopen), eap->arg);
7199 }
7200 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007201 {
7202 if (empty && exmode_active)
7203 {
7204 /* Delete the empty line that remains. Historically ex does
7205 * this but vi doesn't. */
7206 if (eap->line2 == 0)
7207 lnum = curbuf->b_ml.ml_line_count;
7208 else
7209 lnum = 1;
7210 if (*ml_get(lnum) == NUL)
7211 {
7212 ml_delete(lnum, FALSE);
7213 deleted_lines_mark(lnum, 1L);
7214 if (curwin->w_cursor.lnum >= lnum)
7215 --curwin->w_cursor.lnum;
7216 }
7217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 }
7221}
7222
Bram Moolenaarea408852005-06-25 22:49:46 +00007223static char_u *prev_dir = NULL;
7224
7225#if defined(EXITFREE) || defined(PROTO)
7226 void
7227free_cd_dir()
7228{
7229 vim_free(prev_dir);
7230}
7231#endif
7232
7233
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234/*
7235 * ":cd", ":lcd", ":chdir" and ":lchdir".
7236 */
7237 static void
7238ex_cd(eap)
7239 exarg_T *eap;
7240{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 char_u *new_dir;
7242 char_u *tofree;
7243
7244 new_dir = eap->arg;
7245#if !defined(UNIX) && !defined(VMS)
7246 /* for non-UNIX ":cd" means: print current directory */
7247 if (*new_dir == NUL)
7248 ex_pwd(NULL);
7249 else
7250#endif
7251 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007252 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7253 && !eap->forceit)
7254 {
7255 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7256 return;
7257 }
7258
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 /* ":cd -": Change to previous directory */
7260 if (STRCMP(new_dir, "-") == 0)
7261 {
7262 if (prev_dir == NULL)
7263 {
7264 EMSG(_("E186: No previous directory"));
7265 return;
7266 }
7267 new_dir = prev_dir;
7268 }
7269
7270 /* Save current directory for next ":cd -" */
7271 tofree = prev_dir;
7272 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7273 prev_dir = vim_strsave(NameBuff);
7274 else
7275 prev_dir = NULL;
7276
7277#if defined(UNIX) || defined(VMS)
7278 /* for UNIX ":cd" means: go to home directory */
7279 if (*new_dir == NUL)
7280 {
7281 /* use NameBuff for home directory name */
7282# ifdef VMS
7283 char_u *p;
7284
7285 p = mch_getenv((char_u *)"SYS$LOGIN");
7286 if (p == NULL || *p == NUL) /* empty is the same as not set */
7287 NameBuff[0] = NUL;
7288 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007289 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290# else
7291 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7292# endif
7293 new_dir = NameBuff;
7294 }
7295#endif
7296 if (new_dir == NULL || vim_chdir(new_dir))
7297 EMSG(_(e_failed));
7298 else
7299 {
7300 vim_free(curwin->w_localdir);
7301 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7302 {
7303 /* If still in global directory, need to remember current
7304 * directory as global directory. */
7305 if (globaldir == NULL && prev_dir != NULL)
7306 globaldir = vim_strsave(prev_dir);
7307 /* Remember this local directory for the window. */
7308 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7309 curwin->w_localdir = vim_strsave(NameBuff);
7310 }
7311 else
7312 {
7313 /* We are now in the global directory, no need to remember its
7314 * name. */
7315 vim_free(globaldir);
7316 globaldir = NULL;
7317 curwin->w_localdir = NULL;
7318 }
7319
7320 shorten_fnames(TRUE);
7321
7322 /* Echo the new current directory if the command was typed. */
7323 if (KeyTyped)
7324 ex_pwd(eap);
7325 }
7326 vim_free(tofree);
7327 }
7328}
7329
7330/*
7331 * ":pwd".
7332 */
7333/*ARGSUSED*/
7334 static void
7335ex_pwd(eap)
7336 exarg_T *eap;
7337{
7338 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7339 {
7340#ifdef BACKSLASH_IN_FILENAME
7341 slash_adjust(NameBuff);
7342#endif
7343 msg(NameBuff);
7344 }
7345 else
7346 EMSG(_("E187: Unknown"));
7347}
7348
7349/*
7350 * ":=".
7351 */
7352 static void
7353ex_equal(eap)
7354 exarg_T *eap;
7355{
7356 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007357 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358}
7359
7360 static void
7361ex_sleep(eap)
7362 exarg_T *eap;
7363{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007364 int n;
7365 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366
7367 if (cursor_valid())
7368 {
7369 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7370 if (n >= 0)
7371 windgoto((int)n, curwin->w_wcol);
7372 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007373
7374 len = eap->line2;
7375 switch (*eap->arg)
7376 {
7377 case 'm': break;
7378 case NUL: len *= 1000L; break;
7379 default: EMSG2(_(e_invarg2), eap->arg); return;
7380 }
7381 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382}
7383
7384/*
7385 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7386 */
7387 void
7388do_sleep(msec)
7389 long msec;
7390{
7391 long done;
7392
7393 cursor_on();
7394 out_flush();
7395 for (done = 0; !got_int && done < msec; done += 1000L)
7396 {
7397 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7398 ui_breakcheck();
7399 }
7400}
7401
7402 static void
7403do_exmap(eap, isabbrev)
7404 exarg_T *eap;
7405 int isabbrev;
7406{
7407 int mode;
7408 char_u *cmdp;
7409
7410 cmdp = eap->cmd;
7411 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7412
7413 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7414 eap->arg, mode, isabbrev))
7415 {
7416 case 1: EMSG(_(e_invarg));
7417 break;
7418 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7419 break;
7420 }
7421}
7422
7423/*
7424 * ":winsize" command (obsolete).
7425 */
7426 static void
7427ex_winsize(eap)
7428 exarg_T *eap;
7429{
7430 int w, h;
7431 char_u *arg = eap->arg;
7432 char_u *p;
7433
7434 w = getdigits(&arg);
7435 arg = skipwhite(arg);
7436 p = arg;
7437 h = getdigits(&arg);
7438 if (*p != NUL && *arg == NUL)
7439 set_shellsize(w, h, TRUE);
7440 else
7441 EMSG(_("E465: :winsize requires two number arguments"));
7442}
7443
7444#ifdef FEAT_WINDOWS
7445 static void
7446ex_wincmd(eap)
7447 exarg_T *eap;
7448{
7449 int xchar = NUL;
7450 char_u *p;
7451
7452 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7453 {
7454 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7455 if (eap->arg[1] == NUL)
7456 {
7457 EMSG(_(e_invarg));
7458 return;
7459 }
7460 xchar = eap->arg[1];
7461 p = eap->arg + 2;
7462 }
7463 else
7464 p = eap->arg + 1;
7465
7466 eap->nextcmd = check_nextcmd(p);
7467 p = skipwhite(p);
7468 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7469 EMSG(_(e_invarg));
7470 else
7471 {
7472 /* Pass flags on for ":vertical wincmd ]". */
7473 postponed_split_flags = cmdmod.split;
7474 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7475 postponed_split_flags = 0;
7476 }
7477}
7478#endif
7479
Bram Moolenaar843ee412004-06-30 16:16:41 +00007480#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481/*
7482 * ":winpos".
7483 */
7484 static void
7485ex_winpos(eap)
7486 exarg_T *eap;
7487{
7488 int x, y;
7489 char_u *arg = eap->arg;
7490 char_u *p;
7491
7492 if (*arg == NUL)
7493 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007494# if defined(FEAT_GUI) || defined(MSWIN)
7495# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007497# else
7498 if (mch_get_winpos(&x, &y) != FAIL)
7499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 {
7501 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7502 msg(IObuff);
7503 }
7504 else
7505# endif
7506 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7507 }
7508 else
7509 {
7510 x = getdigits(&arg);
7511 arg = skipwhite(arg);
7512 p = arg;
7513 y = getdigits(&arg);
7514 if (*p == NUL || *arg != NUL)
7515 {
7516 EMSG(_("E466: :winpos requires two number arguments"));
7517 return;
7518 }
7519# ifdef FEAT_GUI
7520 if (gui.in_use)
7521 gui_mch_set_winpos(x, y);
7522 else if (gui.starting)
7523 {
7524 /* Remember the coordinates for when the window is opened. */
7525 gui_win_x = x;
7526 gui_win_y = y;
7527 }
7528# ifdef HAVE_TGETENT
7529 else
7530# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007531# else
7532# ifdef MSWIN
7533 mch_set_winpos(x, y);
7534# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535# endif
7536# ifdef HAVE_TGETENT
7537 if (*T_CWP)
7538 term_set_winpos(x, y);
7539# endif
7540 }
7541}
7542#endif
7543
7544/*
7545 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7546 */
7547 static void
7548ex_operators(eap)
7549 exarg_T *eap;
7550{
7551 oparg_T oa;
7552
7553 clear_oparg(&oa);
7554 oa.regname = eap->regname;
7555 oa.start.lnum = eap->line1;
7556 oa.end.lnum = eap->line2;
7557 oa.line_count = eap->line2 - eap->line1 + 1;
7558 oa.motion_type = MLINE;
7559#ifdef FEAT_VIRTUALEDIT
7560 virtual_op = FALSE;
7561#endif
7562 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7563 {
7564 setpcmark();
7565 curwin->w_cursor.lnum = eap->line1;
7566 beginline(BL_SOL | BL_FIX);
7567 }
7568
7569 switch (eap->cmdidx)
7570 {
7571 case CMD_delete:
7572 oa.op_type = OP_DELETE;
7573 op_delete(&oa);
7574 break;
7575
7576 case CMD_yank:
7577 oa.op_type = OP_YANK;
7578 (void)op_yank(&oa, FALSE, TRUE);
7579 break;
7580
7581 default: /* CMD_rshift or CMD_lshift */
7582 if ((eap->cmdidx == CMD_rshift)
7583#ifdef FEAT_RIGHTLEFT
7584 ^ curwin->w_p_rl
7585#endif
7586 )
7587 oa.op_type = OP_RSHIFT;
7588 else
7589 oa.op_type = OP_LSHIFT;
7590 op_shift(&oa, FALSE, eap->amount);
7591 break;
7592 }
7593#ifdef FEAT_VIRTUALEDIT
7594 virtual_op = MAYBE;
7595#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007596 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597}
7598
7599/*
7600 * ":put".
7601 */
7602 static void
7603ex_put(eap)
7604 exarg_T *eap;
7605{
7606 /* ":0put" works like ":1put!". */
7607 if (eap->line2 == 0)
7608 {
7609 eap->line2 = 1;
7610 eap->forceit = TRUE;
7611 }
7612 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007613 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7614 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615}
7616
7617/*
7618 * Handle ":copy" and ":move".
7619 */
7620 static void
7621ex_copymove(eap)
7622 exarg_T *eap;
7623{
7624 long n;
7625
7626 n = get_address(&eap->arg, FALSE, FALSE);
7627 if (eap->arg == NULL) /* error detected */
7628 {
7629 eap->nextcmd = NULL;
7630 return;
7631 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007632 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633
7634 /*
7635 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7636 */
7637 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7638 {
7639 EMSG(_(e_invaddr));
7640 return;
7641 }
7642
7643 if (eap->cmdidx == CMD_move)
7644 {
7645 if (do_move(eap->line1, eap->line2, n) == FAIL)
7646 return;
7647 }
7648 else
7649 ex_copy(eap->line1, eap->line2, n);
7650 u_clearline();
7651 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007652 ex_may_print(eap);
7653}
7654
7655/*
7656 * Print the current line if flags were given to the Ex command.
7657 */
7658 static void
7659ex_may_print(eap)
7660 exarg_T *eap;
7661{
7662 if (eap->flags != 0)
7663 {
7664 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7665 (eap->flags & EXFLAG_LIST));
7666 ex_no_reprint = TRUE;
7667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668}
7669
7670/*
7671 * ":smagic" and ":snomagic".
7672 */
7673 static void
7674ex_submagic(eap)
7675 exarg_T *eap;
7676{
7677 int magic_save = p_magic;
7678
7679 p_magic = (eap->cmdidx == CMD_smagic);
7680 do_sub(eap);
7681 p_magic = magic_save;
7682}
7683
7684/*
7685 * ":join".
7686 */
7687 static void
7688ex_join(eap)
7689 exarg_T *eap;
7690{
7691 curwin->w_cursor.lnum = eap->line1;
7692 if (eap->line1 == eap->line2)
7693 {
7694 if (eap->addr_count >= 2) /* :2,2join does nothing */
7695 return;
7696 if (eap->line2 == curbuf->b_ml.ml_line_count)
7697 {
7698 beep_flush();
7699 return;
7700 }
7701 ++eap->line2;
7702 }
7703 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
7704 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007705 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706}
7707
7708/*
7709 * ":[addr]@r" or ":[addr]*r": execute register
7710 */
7711 static void
7712ex_at(eap)
7713 exarg_T *eap;
7714{
7715 int c;
7716
7717 curwin->w_cursor.lnum = eap->line2;
7718
7719#ifdef USE_ON_FLY_SCROLL
7720 dont_scroll = TRUE; /* disallow scrolling here */
7721#endif
7722
7723 /* get the register name. No name means to use the previous one */
7724 c = *eap->arg;
7725 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7726 c = '@';
7727 /* put the register in mapbuf */
7728 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 else
7733 {
7734 int save_efr = exec_from_reg;
7735
7736 exec_from_reg = TRUE;
7737
7738 /*
7739 * Execute from the typeahead buffer.
7740 * Originally this didn't check for the typeahead buffer to be empty,
7741 * thus could read more Ex commands from stdin. It's not clear why,
7742 * it is certainly unexpected.
7743 */
7744 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
7745 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7746
7747 exec_from_reg = save_efr;
7748 }
7749}
7750
7751/*
7752 * ":!".
7753 */
7754 static void
7755ex_bang(eap)
7756 exarg_T *eap;
7757{
7758 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7759}
7760
7761/*
7762 * ":undo".
7763 */
7764/*ARGSUSED*/
7765 static void
7766ex_undo(eap)
7767 exarg_T *eap;
7768{
7769 u_undo(1);
7770}
7771
7772/*
7773 * ":redo".
7774 */
7775/*ARGSUSED*/
7776 static void
7777ex_redo(eap)
7778 exarg_T *eap;
7779{
7780 u_redo(1);
7781}
7782
7783/*
7784 * ":redir": start/stop redirection.
7785 */
7786 static void
7787ex_redir(eap)
7788 exarg_T *eap;
7789{
7790 char *mode;
7791 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007792 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793
7794 if (STRICMP(eap->arg, "END") == 0)
7795 close_redir();
7796 else
7797 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007798 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007800 ++arg;
7801 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007803 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 mode = "a";
7805 }
7806 else
7807 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007808 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809
7810 close_redir();
7811
7812 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00007813 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 if (fname == NULL)
7815 return;
7816#ifdef FEAT_BROWSE
7817 if (cmdmod.browse)
7818 {
7819 char_u *browseFile;
7820
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007821 browseFile = do_browse(BROWSE_SAVE,
7822 (char_u *)_("Save Redirection"),
7823 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 if (browseFile == NULL)
7825 return; /* operation cancelled */
7826 vim_free(fname);
7827 fname = browseFile;
7828 eap->forceit = TRUE; /* since dialog already asked */
7829 }
7830#endif
7831
7832 redir_fd = open_exfile(fname, eap->forceit, mode);
7833 vim_free(fname);
7834 }
7835#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007836 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 {
7838 /* redirect to a register a-z (resp. A-Z for appending) */
7839 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007840 ++arg;
7841 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007843 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007845 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007847 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00007848 if (*arg == '>' && arg[1] == '>')
7849 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007850 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
7851 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007853 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007855 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007857 if (*arg == '>')
7858 arg++;
7859
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 /* make register empty */
7861 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
7862 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007863 }
7864 if (*arg != NUL)
7865 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007866 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00007867 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007868 }
7869 }
7870 else if (*arg == '=' && arg[1] == '>')
7871 {
7872 int append;
7873
7874 /* redirect to a variable */
7875 close_redir();
7876 arg += 2;
7877
7878 if (*arg == '>')
7879 {
7880 ++arg;
7881 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 }
7883 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007884 append = FALSE;
7885
7886 if (var_redir_start(skipwhite(arg), append) == OK)
7887 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 }
7889#endif
7890
7891 /* TODO: redirect to a buffer */
7892
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 else
Bram Moolenaarca472992005-01-21 11:46:23 +00007894 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 }
7896}
7897
7898/*
7899 * ":redraw": force redraw
7900 */
7901 static void
7902ex_redraw(eap)
7903 exarg_T *eap;
7904{
7905 int r = RedrawingDisabled;
7906 int p = p_lz;
7907
7908 RedrawingDisabled = 0;
7909 p_lz = FALSE;
7910 update_topline();
7911 update_screen(eap->forceit ? CLEAR :
7912#ifdef FEAT_VISUAL
7913 VIsual_active ? INVERTED :
7914#endif
7915 0);
7916#ifdef FEAT_TITLE
7917 if (need_maketitle)
7918 maketitle();
7919#endif
7920 RedrawingDisabled = r;
7921 p_lz = p;
7922
7923 /* Reset msg_didout, so that a message that's there is overwritten. */
7924 msg_didout = FALSE;
7925 msg_col = 0;
7926
7927 out_flush();
7928}
7929
7930/*
7931 * ":redrawstatus": force redraw of status line(s)
7932 */
7933/*ARGSUSED*/
7934 static void
7935ex_redrawstatus(eap)
7936 exarg_T *eap;
7937{
7938#if defined(FEAT_WINDOWS)
7939 int r = RedrawingDisabled;
7940 int p = p_lz;
7941
7942 RedrawingDisabled = 0;
7943 p_lz = FALSE;
7944 if (eap->forceit)
7945 status_redraw_all();
7946 else
7947 status_redraw_curbuf();
7948 update_screen(
7949# ifdef FEAT_VISUAL
7950 VIsual_active ? INVERTED :
7951# endif
7952 0);
7953 RedrawingDisabled = r;
7954 p_lz = p;
7955 out_flush();
7956#endif
7957}
7958
7959 static void
7960close_redir()
7961{
7962 if (redir_fd != NULL)
7963 {
7964 fclose(redir_fd);
7965 redir_fd = NULL;
7966 }
7967#ifdef FEAT_EVAL
7968 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007969 if (redir_vname)
7970 {
7971 var_redir_stop();
7972 redir_vname = 0;
7973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974#endif
7975}
7976
7977#if defined(FEAT_SESSION) && defined(USE_CRNL)
7978# define MKSESSION_NL
7979static int mksession_nl = FALSE; /* use NL only in put_eol() */
7980#endif
7981
7982/*
7983 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
7984 */
7985 static void
7986ex_mkrc(eap)
7987 exarg_T *eap;
7988{
7989 FILE *fd;
7990 int failed = FALSE;
7991 char_u *fname;
7992#ifdef FEAT_BROWSE
7993 char_u *browseFile = NULL;
7994#endif
7995#ifdef FEAT_SESSION
7996 int view_session = FALSE;
7997 int using_vdir = FALSE; /* using 'viewdir'? */
7998 char_u *viewFile = NULL;
7999 unsigned *flagp;
8000#endif
8001
8002 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8003 {
8004#ifdef FEAT_SESSION
8005 view_session = TRUE;
8006#else
8007 ex_ni(eap);
8008 return;
8009#endif
8010 }
8011
8012#ifdef FEAT_SESSION
8013 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8014 if (eap->cmdidx == CMD_mkview
8015 && (*eap->arg == NUL
8016 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8017 {
8018 eap->forceit = TRUE;
8019 fname = get_view_file(*eap->arg);
8020 if (fname == NULL)
8021 return;
8022 viewFile = fname;
8023 using_vdir = TRUE;
8024 }
8025 else
8026#endif
8027 if (*eap->arg != NUL)
8028 fname = eap->arg;
8029 else if (eap->cmdidx == CMD_mkvimrc)
8030 fname = (char_u *)VIMRC_FILE;
8031#ifdef FEAT_SESSION
8032 else if (eap->cmdidx == CMD_mksession)
8033 fname = (char_u *)SESSION_FILE;
8034#endif
8035 else
8036 fname = (char_u *)EXRC_FILE;
8037
8038#ifdef FEAT_BROWSE
8039 if (cmdmod.browse)
8040 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008041 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042# ifdef FEAT_SESSION
8043 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8044 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8045# endif
8046 (char_u *)_("Save Setup"),
8047 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8048 if (browseFile == NULL)
8049 goto theend;
8050 fname = browseFile;
8051 eap->forceit = TRUE; /* since dialog already asked */
8052 }
8053#endif
8054
8055#if defined(FEAT_SESSION) && defined(vim_mkdir)
8056 /* When using 'viewdir' may have to create the directory. */
8057 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008058 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059#endif
8060
8061 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8062 if (fd != NULL)
8063 {
8064#ifdef FEAT_SESSION
8065 if (eap->cmdidx == CMD_mkview)
8066 flagp = &vop_flags;
8067 else
8068 flagp = &ssop_flags;
8069#endif
8070
8071#ifdef MKSESSION_NL
8072 /* "unix" in 'sessionoptions': use NL line separator */
8073 if (view_session && (*flagp & SSOP_UNIX))
8074 mksession_nl = TRUE;
8075#endif
8076
8077 /* Write the version command for :mkvimrc */
8078 if (eap->cmdidx == CMD_mkvimrc)
8079 (void)put_line(fd, "version 6.0");
8080
8081#ifdef FEAT_SESSION
8082 if (eap->cmdidx != CMD_mkview)
8083#endif
8084 {
8085 /* Write setting 'compatible' first, because it has side effects.
8086 * For that same reason only do it when needed. */
8087 if (p_cp)
8088 (void)put_line(fd, "if !&cp | set cp | endif");
8089 else
8090 (void)put_line(fd, "if &cp | set nocp | endif");
8091 }
8092
8093#ifdef FEAT_SESSION
8094 if (!view_session
8095 || (eap->cmdidx == CMD_mksession
8096 && (*flagp & SSOP_OPTIONS)))
8097#endif
8098 failed |= (makemap(fd, NULL) == FAIL
8099 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8100
8101#ifdef FEAT_SESSION
8102 if (!failed && view_session)
8103 {
8104 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8105 failed = TRUE;
8106 if (eap->cmdidx == CMD_mksession)
8107 {
8108 char_u dirnow[MAXPATHL]; /* current directory */
8109
8110 /*
8111 * Change to session file's dir.
8112 */
8113 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8114 || mch_chdir((char *)dirnow) != 0)
8115 *dirnow = NUL;
8116 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8117 {
8118 if (vim_chdirfile(fname) == OK)
8119 shorten_fnames(TRUE);
8120 }
8121 else if (*dirnow != NUL
8122 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8123 {
8124 (void)mch_chdir((char *)globaldir);
8125 shorten_fnames(TRUE);
8126 }
8127
8128 failed |= (makeopens(fd, dirnow) == FAIL);
8129
8130 /* restore original dir */
8131 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8132 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8133 {
8134 if (mch_chdir((char *)dirnow) != 0)
8135 EMSG(_(e_prev_dir));
8136 shorten_fnames(TRUE);
8137 }
8138 }
8139 else
8140 {
8141 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8142 }
8143 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8144 == FAIL)
8145 failed = TRUE;
8146 }
8147#endif
8148 failed |= fclose(fd);
8149
8150 if (failed)
8151 EMSG(_(e_write));
8152#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8153 else if (eap->cmdidx == CMD_mksession)
8154 {
8155 /* successful session write - set this_session var */
8156 char_u tbuf[MAXPATHL];
8157
8158 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8159 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8160 }
8161#endif
8162#ifdef MKSESSION_NL
8163 mksession_nl = FALSE;
8164#endif
8165 }
8166
8167#ifdef FEAT_BROWSE
8168theend:
8169 vim_free(browseFile);
8170#endif
8171#ifdef FEAT_SESSION
8172 vim_free(viewFile);
8173#endif
8174}
8175
Bram Moolenaardf177f62005-02-22 08:39:57 +00008176#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8177 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008178/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008179 int
8180vim_mkdir_emsg(name, prot)
8181 char_u *name;
8182 int prot;
8183{
8184 if (vim_mkdir(name, prot) != 0)
8185 {
8186 EMSG2(_("E739: Cannot create directory: %s"), name);
8187 return FAIL;
8188 }
8189 return OK;
8190}
8191#endif
8192
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193/*
8194 * Open a file for writing for an Ex command, with some checks.
8195 * Return file descriptor, or NULL on failure.
8196 */
8197 FILE *
8198open_exfile(fname, forceit, mode)
8199 char_u *fname;
8200 int forceit;
8201 char *mode; /* "w" for create new file or "a" for append */
8202{
8203 FILE *fd;
8204
8205#ifdef UNIX
8206 /* with Unix it is possible to open a directory */
8207 if (mch_isdir(fname))
8208 {
8209 EMSG2(_(e_isadir2), fname);
8210 return NULL;
8211 }
8212#endif
8213 if (!forceit && *mode != 'a' && vim_fexists(fname))
8214 {
8215 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8216 return NULL;
8217 }
8218
8219 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8220 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8221
8222 return fd;
8223}
8224
8225/*
8226 * ":mark" and ":k".
8227 */
8228 static void
8229ex_mark(eap)
8230 exarg_T *eap;
8231{
8232 pos_T pos;
8233
8234 if (*eap->arg == NUL) /* No argument? */
8235 EMSG(_(e_argreq));
8236 else if (eap->arg[1] != NUL) /* more than one character? */
8237 EMSG(_(e_trailing));
8238 else
8239 {
8240 pos = curwin->w_cursor; /* save curwin->w_cursor */
8241 curwin->w_cursor.lnum = eap->line2;
8242 beginline(BL_WHITE | BL_FIX);
8243 if (setmark(*eap->arg) == FAIL) /* set mark */
8244 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8245 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8246 }
8247}
8248
8249/*
8250 * Update w_topline, w_leftcol and the cursor position.
8251 */
8252 void
8253update_topline_cursor()
8254{
8255 check_cursor(); /* put cursor on valid line */
8256 update_topline();
8257 if (!curwin->w_p_wrap)
8258 validate_cursor();
8259 update_curswant();
8260}
8261
8262#ifdef FEAT_EX_EXTRA
8263/*
8264 * ":normal[!] {commands}": Execute normal mode commands.
8265 */
8266 static void
8267ex_normal(eap)
8268 exarg_T *eap;
8269{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 int save_msg_scroll = msg_scroll;
8271 int save_restart_edit = restart_edit;
8272 int save_msg_didout = msg_didout;
8273 int save_State = State;
8274 tasave_T tabuf;
8275 int save_insertmode = p_im;
8276 int save_finish_op = finish_op;
8277#ifdef FEAT_MBYTE
8278 char_u *arg = NULL;
8279 int l;
8280 char_u *p;
8281#endif
8282
8283 if (ex_normal_busy >= p_mmd)
8284 {
8285 EMSG(_("E192: Recursive use of :normal too deep"));
8286 return;
8287 }
8288 ++ex_normal_busy;
8289
8290 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8291 restart_edit = 0; /* don't go to Insert mode */
8292 p_im = FALSE; /* don't use 'insertmode' */
8293
8294#ifdef FEAT_MBYTE
8295 /*
8296 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8297 * this for the K_SPECIAL leading byte, otherwise special keys will not
8298 * work.
8299 */
8300 if (has_mbyte)
8301 {
8302 int len = 0;
8303
8304 /* Count the number of characters to be escaped. */
8305 for (p = eap->arg; *p != NUL; ++p)
8306 {
8307# ifdef FEAT_GUI
8308 if (*p == CSI) /* leadbyte CSI */
8309 len += 2;
8310# endif
8311 for (l = (*mb_ptr2len_check)(p) - 1; l > 0; --l)
8312 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8313# ifdef FEAT_GUI
8314 || *p == CSI
8315# endif
8316 )
8317 len += 2;
8318 }
8319 if (len > 0)
8320 {
8321 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8322 if (arg != NULL)
8323 {
8324 len = 0;
8325 for (p = eap->arg; *p != NUL; ++p)
8326 {
8327 arg[len++] = *p;
8328# ifdef FEAT_GUI
8329 if (*p == CSI)
8330 {
8331 arg[len++] = KS_EXTRA;
8332 arg[len++] = (int)KE_CSI;
8333 }
8334# endif
8335 for (l = (*mb_ptr2len_check)(p) - 1; l > 0; --l)
8336 {
8337 arg[len++] = *++p;
8338 if (*p == K_SPECIAL)
8339 {
8340 arg[len++] = KS_SPECIAL;
8341 arg[len++] = KE_FILLER;
8342 }
8343# ifdef FEAT_GUI
8344 else if (*p == CSI)
8345 {
8346 arg[len++] = KS_EXTRA;
8347 arg[len++] = (int)KE_CSI;
8348 }
8349# endif
8350 }
8351 arg[len] = NUL;
8352 }
8353 }
8354 }
8355 }
8356#endif
8357
8358 /*
8359 * Save the current typeahead. This is required to allow using ":normal"
8360 * from an event handler and makes sure we don't hang when the argument
8361 * ends with half a command.
8362 */
8363 save_typeahead(&tabuf);
8364 if (tabuf.typebuf_valid)
8365 {
8366 /*
8367 * Repeat the :normal command for each line in the range. When no
8368 * range given, execute it just once, without positioning the cursor
8369 * first.
8370 */
8371 do
8372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 if (eap->addr_count != 0)
8374 {
8375 curwin->w_cursor.lnum = eap->line1++;
8376 curwin->w_cursor.col = 0;
8377 }
8378
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008379 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380#ifdef FEAT_MBYTE
8381 arg != NULL ? arg :
8382#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008383 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 }
8385 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8386 }
8387
8388 /* Might not return to the main loop when in an event handler. */
8389 update_topline_cursor();
8390
8391 /* Restore the previous typeahead. */
8392 restore_typeahead(&tabuf);
8393
8394 --ex_normal_busy;
8395 msg_scroll = save_msg_scroll;
8396 restart_edit = save_restart_edit;
8397 p_im = save_insertmode;
8398 finish_op = save_finish_op;
8399 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8400
8401 /* Restore the state (needed when called from a function executed for
8402 * 'indentexpr'). */
8403 State = save_State;
8404#ifdef FEAT_MBYTE
8405 vim_free(arg);
8406#endif
8407}
8408
8409/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008410 * ":startinsert" and ":startreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 */
8412 static void
8413ex_startinsert(eap)
8414 exarg_T *eap;
8415{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008416 if (eap->forceit)
8417 {
8418 coladvance((colnr_T)MAXCOL);
8419 curwin->w_curswant = MAXCOL;
8420 curwin->w_set_curswant = FALSE;
8421 }
8422
Bram Moolenaara40c5002005-01-09 21:16:21 +00008423 /* Ignore the command when already in Insert mode. Inserting an
8424 * expression register that invokes a function can do this. */
8425 if (State & INSERT)
8426 return;
8427
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 if (eap->forceit)
8429 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008430 if (eap->cmdidx == CMD_startinsert)
8431 restart_edit = 'a';
8432 else
8433 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 }
8435 else
8436 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008437 if (eap->cmdidx == CMD_startinsert)
8438 restart_edit = 'i';
8439 else
8440 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 curwin->w_curswant = 0; /* avoid MAXCOL */
8442 }
8443}
8444
8445/*
8446 * ":stopinsert"
8447 */
8448/*ARGSUSED*/
8449 static void
8450ex_stopinsert(eap)
8451 exarg_T *eap;
8452{
8453 restart_edit = 0;
8454 stop_insert_mode = TRUE;
8455}
8456#endif
8457
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008458#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8459/*
8460 * Execute normal mode command "cmd".
8461 * "remap" can be REMAP_NONE or REMAP_YES.
8462 */
8463 void
8464exec_normal_cmd(cmd, remap, silent)
8465 char_u *cmd;
8466 int remap;
8467 int silent;
8468{
8469 oparg_T oa;
8470
8471 /*
8472 * Stuff the argument into the typeahead buffer.
8473 * Execute normal_cmd() until there is no typeahead left.
8474 */
8475 clear_oparg(&oa);
8476 finish_op = FALSE;
8477 ins_typebuf(cmd, remap, 0, TRUE, silent);
8478 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8479 && !got_int)
8480 {
8481 update_topline_cursor();
8482 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8483 }
8484}
8485#endif
8486
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487#ifdef FEAT_FIND_ID
8488 static void
8489ex_checkpath(eap)
8490 exarg_T *eap;
8491{
8492 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8493 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8494 (linenr_T)1, (linenr_T)MAXLNUM);
8495}
8496
8497#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8498/*
8499 * ":psearch"
8500 */
8501 static void
8502ex_psearch(eap)
8503 exarg_T *eap;
8504{
8505 g_do_tagpreview = p_pvh;
8506 ex_findpat(eap);
8507 g_do_tagpreview = 0;
8508}
8509#endif
8510
8511 static void
8512ex_findpat(eap)
8513 exarg_T *eap;
8514{
8515 int whole = TRUE;
8516 long n;
8517 char_u *p;
8518 int action;
8519
8520 switch (cmdnames[eap->cmdidx].cmd_name[2])
8521 {
8522 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8523 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8524 action = ACTION_GOTO;
8525 else
8526 action = ACTION_SHOW;
8527 break;
8528 case 'i': /* ":ilist" and ":dlist" */
8529 action = ACTION_SHOW_ALL;
8530 break;
8531 case 'u': /* ":ijump" and ":djump" */
8532 action = ACTION_GOTO;
8533 break;
8534 default: /* ":isplit" and ":dsplit" */
8535 action = ACTION_SPLIT;
8536 break;
8537 }
8538
8539 n = 1;
8540 if (vim_isdigit(*eap->arg)) /* get count */
8541 {
8542 n = getdigits(&eap->arg);
8543 eap->arg = skipwhite(eap->arg);
8544 }
8545 if (*eap->arg == '/') /* Match regexp, not just whole words */
8546 {
8547 whole = FALSE;
8548 ++eap->arg;
8549 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8550 if (*p)
8551 {
8552 *p++ = NUL;
8553 p = skipwhite(p);
8554
8555 /* Check for trailing illegal characters */
8556 if (!ends_excmd(*p))
8557 eap->errmsg = e_trailing;
8558 else
8559 eap->nextcmd = check_nextcmd(p);
8560 }
8561 }
8562 if (!eap->skip)
8563 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8564 whole, !eap->forceit,
8565 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8566 n, action, eap->line1, eap->line2);
8567}
8568#endif
8569
8570#ifdef FEAT_WINDOWS
8571
8572# ifdef FEAT_QUICKFIX
8573/*
8574 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8575 */
8576 static void
8577ex_ptag(eap)
8578 exarg_T *eap;
8579{
8580 g_do_tagpreview = p_pvh;
8581 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8582}
8583
8584/*
8585 * ":pedit"
8586 */
8587 static void
8588ex_pedit(eap)
8589 exarg_T *eap;
8590{
8591 win_T *curwin_save = curwin;
8592
8593 g_do_tagpreview = p_pvh;
8594 prepare_tagpreview();
8595 keep_help_flag = curwin_save->w_buffer->b_help;
8596 do_exedit(eap, NULL);
8597 keep_help_flag = FALSE;
8598 if (curwin != curwin_save && win_valid(curwin_save))
8599 {
8600 /* Return cursor to where we were */
8601 validate_cursor();
8602 redraw_later(VALID);
8603 win_enter(curwin_save, TRUE);
8604 }
8605 g_do_tagpreview = 0;
8606}
8607# endif
8608
8609/*
8610 * ":stag", ":stselect" and ":stjump".
8611 */
8612 static void
8613ex_stag(eap)
8614 exarg_T *eap;
8615{
8616 postponed_split = -1;
8617 postponed_split_flags = cmdmod.split;
8618 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8619 postponed_split_flags = 0;
8620}
8621#endif
8622
8623/*
8624 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8625 */
8626 static void
8627ex_tag(eap)
8628 exarg_T *eap;
8629{
8630 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8631}
8632
8633 static void
8634ex_tag_cmd(eap, name)
8635 exarg_T *eap;
8636 char_u *name;
8637{
8638 int cmd;
8639
8640 switch (name[1])
8641 {
8642 case 'j': cmd = DT_JUMP; /* ":tjump" */
8643 break;
8644 case 's': cmd = DT_SELECT; /* ":tselect" */
8645 break;
8646 case 'p': cmd = DT_PREV; /* ":tprevious" */
8647 break;
8648 case 'N': cmd = DT_PREV; /* ":tNext" */
8649 break;
8650 case 'n': cmd = DT_NEXT; /* ":tnext" */
8651 break;
8652 case 'o': cmd = DT_POP; /* ":pop" */
8653 break;
8654 case 'f': /* ":tfirst" */
8655 case 'r': cmd = DT_FIRST; /* ":trewind" */
8656 break;
8657 case 'l': cmd = DT_LAST; /* ":tlast" */
8658 break;
8659 default: /* ":tag" */
8660#ifdef FEAT_CSCOPE
8661 if (p_cst)
8662 {
8663 do_cstag(eap);
8664 return;
8665 }
8666#endif
8667 cmd = DT_TAG;
8668 break;
8669 }
8670
8671 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8672 eap->forceit, TRUE);
8673}
8674
8675/*
8676 * Evaluate cmdline variables.
8677 *
8678 * change '%' to curbuf->b_ffname
8679 * '#' to curwin->w_altfile
8680 * '<cword>' to word under the cursor
8681 * '<cWORD>' to WORD under the cursor
8682 * '<cfile>' to path name under the cursor
8683 * '<sfile>' to sourced file name
8684 * '<afile>' to file name for autocommand
8685 * '<abuf>' to buffer number for autocommand
8686 * '<amatch>' to matching name for autocommand
8687 *
8688 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8689 * "" for error without a message) and NULL is returned.
8690 * Returns an allocated string if a valid match was found.
8691 * Returns NULL if no match was found. "usedlen" then still contains the
8692 * number of characters to skip.
8693 */
8694 char_u *
8695eval_vars(src, usedlen, lnump, errormsg, srcstart)
8696 char_u *src; /* pointer into commandline */
8697 int *usedlen; /* characters after src that are used */
8698 linenr_T *lnump; /* line number for :e command, or NULL */
8699 char_u **errormsg; /* pointer to error message */
8700 char_u *srcstart; /* beginning of valid memory for src */
8701{
8702 int i;
8703 char_u *s;
8704 char_u *result;
8705 char_u *resultbuf = NULL;
8706 int resultlen;
8707 buf_T *buf;
8708 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
8709 int spec_idx;
8710#ifdef FEAT_MODIFY_FNAME
8711 int skip_mod = FALSE;
8712#endif
8713 static char *(spec_str[]) =
8714 {
8715 "%",
8716#define SPEC_PERC 0
8717 "#",
8718#define SPEC_HASH 1
8719 "<cword>", /* cursor word */
8720#define SPEC_CWORD 2
8721 "<cWORD>", /* cursor WORD */
8722#define SPEC_CCWORD 3
8723 "<cfile>", /* cursor path name */
8724#define SPEC_CFILE 4
8725 "<sfile>", /* ":so" file name */
8726#define SPEC_SFILE 5
8727#ifdef FEAT_AUTOCMD
8728 "<afile>", /* autocommand file name */
8729# define SPEC_AFILE 6
8730 "<abuf>", /* autocommand buffer number */
8731# define SPEC_ABUF 7
8732 "<amatch>", /* autocommand match name */
8733# define SPEC_AMATCH 8
8734#endif
8735#ifdef FEAT_CLIENTSERVER
8736 "<client>"
8737# define SPEC_CLIENT 9
8738#endif
8739 };
8740#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
8741
8742#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
8743 char_u strbuf[30];
8744#endif
8745
8746 *errormsg = NULL;
8747
8748 /*
8749 * Check if there is something to do.
8750 */
8751 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
8752 {
8753 *usedlen = (int)STRLEN(spec_str[spec_idx]);
8754 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
8755 break;
8756 }
8757 if (spec_idx == SPEC_COUNT) /* no match */
8758 {
8759 *usedlen = 1;
8760 return NULL;
8761 }
8762
8763 /*
8764 * Skip when preceded with a backslash "\%" and "\#".
8765 * Note: In "\\%" the % is also not recognized!
8766 */
8767 if (src > srcstart && src[-1] == '\\')
8768 {
8769 *usedlen = 0;
8770 STRCPY(src - 1, src); /* remove backslash */
8771 return NULL;
8772 }
8773
8774 /*
8775 * word or WORD under cursor
8776 */
8777 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
8778 {
8779 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
8780 (FIND_IDENT|FIND_STRING) : FIND_STRING);
8781 if (resultlen == 0)
8782 {
8783 *errormsg = (char_u *)"";
8784 return NULL;
8785 }
8786 }
8787
8788 /*
8789 * '#': Alternate file name
8790 * '%': Current file name
8791 * File name under the cursor
8792 * File name for autocommand
8793 * and following modifiers
8794 */
8795 else
8796 {
8797 switch (spec_idx)
8798 {
8799 case SPEC_PERC: /* '%': current file */
8800 if (curbuf->b_fname == NULL)
8801 {
8802 result = (char_u *)"";
8803 valid = 0; /* Must have ":p:h" to be valid */
8804 }
8805 else
8806#ifdef RISCOS
8807 /* Always use the full path for RISC OS if possible. */
8808 result = curbuf->b_ffname;
8809 if (result == NULL)
8810 result = curbuf->b_fname;
8811#else
8812 result = curbuf->b_fname;
8813#endif
8814 break;
8815
8816 case SPEC_HASH: /* '#' or "#99": alternate file */
8817 if (src[1] == '#') /* "##": the argument list */
8818 {
8819 result = arg_all();
8820 resultbuf = result;
8821 *usedlen = 2;
8822#ifdef FEAT_MODIFY_FNAME
8823 skip_mod = TRUE;
8824#endif
8825 break;
8826 }
8827 s = src + 1;
8828 i = (int)getdigits(&s);
8829 *usedlen = (int)(s - src); /* length of what we expand */
8830
8831 buf = buflist_findnr(i);
8832 if (buf == NULL)
8833 {
8834 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
8835 return NULL;
8836 }
8837 if (lnump != NULL)
8838 *lnump = ECMD_LAST;
8839 if (buf->b_fname == NULL)
8840 {
8841 result = (char_u *)"";
8842 valid = 0; /* Must have ":p:h" to be valid */
8843 }
8844 else
8845 result = buf->b_fname;
8846 break;
8847
8848#ifdef FEAT_SEARCHPATH
8849 case SPEC_CFILE: /* file name under cursor */
8850 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L);
8851 if (result == NULL)
8852 {
8853 *errormsg = (char_u *)"";
8854 return NULL;
8855 }
8856 resultbuf = result; /* remember allocated string */
8857 break;
8858#endif
8859
8860#ifdef FEAT_AUTOCMD
8861 case SPEC_AFILE: /* file name for autocommand */
8862 result = autocmd_fname;
8863 if (result == NULL)
8864 {
8865 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
8866 return NULL;
8867 }
8868 break;
8869
8870 case SPEC_ABUF: /* buffer number for autocommand */
8871 if (autocmd_bufnr <= 0)
8872 {
8873 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
8874 return NULL;
8875 }
8876 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8877 result = strbuf;
8878 break;
8879
8880 case SPEC_AMATCH: /* match name for autocommand */
8881 result = autocmd_match;
8882 if (result == NULL)
8883 {
8884 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
8885 return NULL;
8886 }
8887 break;
8888
8889#endif
8890 case SPEC_SFILE: /* file name for ":so" command */
8891 result = sourcing_name;
8892 if (result == NULL)
8893 {
8894 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
8895 return NULL;
8896 }
8897 break;
8898#if defined(FEAT_CLIENTSERVER)
8899 case SPEC_CLIENT: /* Source of last submitted input */
8900 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
8901 result = strbuf;
8902 break;
8903#endif
8904 }
8905
8906 resultlen = (int)STRLEN(result); /* length of new string */
8907 if (src[*usedlen] == '<') /* remove the file name extension */
8908 {
8909 ++*usedlen;
8910#ifdef RISCOS
8911 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
8912#else
8913 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
8914#endif
8915 resultlen = (int)(s - result);
8916 }
8917#ifdef FEAT_MODIFY_FNAME
8918 else if (!skip_mod)
8919 {
8920 valid |= modify_fname(src, usedlen, &result, &resultbuf,
8921 &resultlen);
8922 if (result == NULL)
8923 {
8924 *errormsg = (char_u *)"";
8925 return NULL;
8926 }
8927 }
8928#endif
8929 }
8930
8931 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8932 {
8933 if (valid != VALID_HEAD + VALID_PATH)
8934 /* xgettext:no-c-format */
8935 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
8936 else
8937 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
8938 result = NULL;
8939 }
8940 else
8941 result = vim_strnsave(result, resultlen);
8942 vim_free(resultbuf);
8943 return result;
8944}
8945
8946/*
8947 * Concatenate all files in the argument list, separated by spaces, and return
8948 * it in one allocated string.
8949 * Spaces and backslashes in the file names are escaped with a backslash.
8950 * Returns NULL when out of memory.
8951 */
8952 static char_u *
8953arg_all()
8954{
8955 int len;
8956 int idx;
8957 char_u *retval = NULL;
8958 char_u *p;
8959
8960 /*
8961 * Do this loop two times:
8962 * first time: compute the total length
8963 * second time: concatenate the names
8964 */
8965 for (;;)
8966 {
8967 len = 0;
8968 for (idx = 0; idx < ARGCOUNT; ++idx)
8969 {
8970 p = alist_name(&ARGLIST[idx]);
8971 if (p != NULL)
8972 {
8973 if (len > 0)
8974 {
8975 /* insert a space in between names */
8976 if (retval != NULL)
8977 retval[len] = ' ';
8978 ++len;
8979 }
8980 for ( ; *p != NUL; ++p)
8981 {
8982 if (*p == ' ' || *p == '\\')
8983 {
8984 /* insert a backslash */
8985 if (retval != NULL)
8986 retval[len] = '\\';
8987 ++len;
8988 }
8989 if (retval != NULL)
8990 retval[len] = *p;
8991 ++len;
8992 }
8993 }
8994 }
8995
8996 /* second time: break here */
8997 if (retval != NULL)
8998 {
8999 retval[len] = NUL;
9000 break;
9001 }
9002
9003 /* allocate memory */
9004 retval = alloc(len + 1);
9005 if (retval == NULL)
9006 break;
9007 }
9008
9009 return retval;
9010}
9011
9012#if defined(FEAT_AUTOCMD) || defined(PROTO)
9013/*
9014 * Expand the <sfile> string in "arg".
9015 *
9016 * Returns an allocated string, or NULL for any error.
9017 */
9018 char_u *
9019expand_sfile(arg)
9020 char_u *arg;
9021{
9022 char_u *errormsg;
9023 int len;
9024 char_u *result;
9025 char_u *newres;
9026 char_u *repl;
9027 int srclen;
9028 char_u *p;
9029
9030 result = vim_strsave(arg);
9031 if (result == NULL)
9032 return NULL;
9033
9034 for (p = result; *p; )
9035 {
9036 if (STRNCMP(p, "<sfile>", 7) != 0)
9037 ++p;
9038 else
9039 {
9040 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9041 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9042 if (errormsg != NULL)
9043 {
9044 if (*errormsg)
9045 emsg(errormsg);
9046 vim_free(result);
9047 return NULL;
9048 }
9049 if (repl == NULL) /* no match (cannot happen) */
9050 {
9051 p += srclen;
9052 continue;
9053 }
9054 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9055 newres = alloc(len);
9056 if (newres == NULL)
9057 {
9058 vim_free(repl);
9059 vim_free(result);
9060 return NULL;
9061 }
9062 mch_memmove(newres, result, (size_t)(p - result));
9063 STRCPY(newres + (p - result), repl);
9064 len = (int)STRLEN(newres);
9065 STRCAT(newres, p + srclen);
9066 vim_free(repl);
9067 vim_free(result);
9068 result = newres;
9069 p = newres + len; /* continue after the match */
9070 }
9071 }
9072
9073 return result;
9074}
9075#endif
9076
9077#ifdef FEAT_SESSION
9078static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9079static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9080static frame_T *ses_skipframe __ARGS((frame_T *fr));
9081static int ses_do_frame __ARGS((frame_T *fr));
9082static int ses_do_win __ARGS((win_T *wp));
9083static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9084static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9085static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9086
9087/*
9088 * Write openfile commands for the current buffers to an .exrc file.
9089 * Return FAIL on error, OK otherwise.
9090 */
9091 static int
9092makeopens(fd, dirnow)
9093 FILE *fd;
9094 char_u *dirnow; /* Current directory name */
9095{
9096 buf_T *buf;
9097 int only_save_windows = TRUE;
9098 int nr;
9099 int cnr = 1;
9100 int restore_size = TRUE;
9101 win_T *wp;
9102 char_u *sname;
9103 win_T *edited_win = NULL;
9104
9105 if (ssop_flags & SSOP_BUFFERS)
9106 only_save_windows = FALSE; /* Save ALL buffers */
9107
9108 /*
9109 * Begin by setting the this_session variable, and then other
9110 * sessionable variables.
9111 */
9112#ifdef FEAT_EVAL
9113 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9114 return FAIL;
9115 if (ssop_flags & SSOP_GLOBALS)
9116 if (store_session_globals(fd) == FAIL)
9117 return FAIL;
9118#endif
9119
9120 /*
9121 * Close all windows but one.
9122 */
9123 if (put_line(fd, "silent only") == FAIL)
9124 return FAIL;
9125
9126 /*
9127 * Now a :cd command to the session directory or the current directory
9128 */
9129 if (ssop_flags & SSOP_SESDIR)
9130 {
9131 if (put_line(fd, "exe \"cd \" . expand(\"<sfile>:p:h\")") == FAIL)
9132 return FAIL;
9133 }
9134 else if (ssop_flags & SSOP_CURDIR)
9135 {
9136 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9137 if (sname == NULL
9138 || fprintf(fd, "cd %s", sname) < 0 || put_eol(fd) == FAIL)
9139 return FAIL;
9140 vim_free(sname);
9141 }
9142
9143 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009144 * If there is an empty, unnamed buffer we will wipe it out later.
9145 * Remember the buffer number.
9146 */
9147 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9148 return FAIL;
9149 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9150 return FAIL;
9151 if (put_line(fd, "endif") == FAIL)
9152 return FAIL;
9153
9154 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 * Now save the current files, current buffer first.
9156 */
9157 if (put_line(fd, "set shortmess=aoO") == FAIL)
9158 return FAIL;
9159
9160 /* Now put the other buffers into the buffer list */
9161 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9162 {
9163 if (!(only_save_windows && buf->b_nwindows == 0)
9164 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9165 && buf->b_fname != NULL
9166 && buf->b_p_bl)
9167 {
9168 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9169 : buf->b_wininfo->wi_fpos.lnum) < 0
9170 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9171 return FAIL;
9172 }
9173 }
9174
9175 /* the global argument list */
9176 if (ses_arglist(fd, "args", &global_alist.al_ga,
9177 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9178 return FAIL;
9179
9180 if (ssop_flags & SSOP_RESIZE)
9181 {
9182 /* Note: after the restore we still check it worked!*/
9183 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9184 || put_eol(fd) == FAIL)
9185 return FAIL;
9186 }
9187
9188#ifdef FEAT_GUI
9189 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9190 {
9191 int x, y;
9192
9193 if (gui_mch_get_winpos(&x, &y) == OK)
9194 {
9195 /* Note: after the restore we still check it worked!*/
9196 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9197 return FAIL;
9198 }
9199 }
9200#endif
9201
9202 /*
9203 * Before creating the window layout, try loading one file. If this is
9204 * aborted we don't end up with a number of useless windows.
9205 * This may have side effects! (e.g., compressed or network file).
9206 */
9207 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9208 {
9209 if (ses_do_win(wp)
9210 && wp->w_buffer->b_ffname != NULL
9211 && !wp->w_buffer->b_help
9212#ifdef FEAT_QUICKFIX
9213 && !bt_nofile(wp->w_buffer)
9214#endif
9215 )
9216 {
9217 if (fputs("edit ", fd) < 0
9218 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9219 return FAIL;
9220 if (!wp->w_arg_idx_invalid)
9221 edited_win = wp;
9222 break;
9223 }
9224 }
9225
9226 /*
9227 * Save current window layout.
9228 */
9229 if (put_line(fd, "set splitbelow splitright") == FAIL)
9230 return FAIL;
9231 if (ses_win_rec(fd, topframe) == FAIL)
9232 return FAIL;
9233 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9234 return FAIL;
9235 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9236 return FAIL;
9237
9238 /*
9239 * Check if window sizes can be restored (no windows omitted).
9240 * Remember the window number of the current window after restoring.
9241 */
9242 nr = 0;
9243 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9244 {
9245 if (ses_do_win(wp))
9246 ++nr;
9247 else
9248 restore_size = FALSE;
9249 if (curwin == wp)
9250 cnr = nr;
9251 }
9252
9253 /* Go to the first window. */
9254 if (put_line(fd, "wincmd t") == FAIL)
9255 return FAIL;
9256
9257 /*
9258 * If more than one window, see if sizes can be restored.
9259 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9260 * resized when moving between windows.
9261 * Do this before restoring the view, so that the topline and the cursor
9262 * can be set. This is done again below.
9263 */
9264 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9265 return FAIL;
9266 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9267 return FAIL;
9268
9269 /*
9270 * Restore the view of the window (options, file, cursor, etc.).
9271 */
9272 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9273 {
9274 if (!ses_do_win(wp))
9275 continue;
9276 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9277 return FAIL;
9278 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9279 return FAIL;
9280 }
9281
9282 /*
9283 * Restore cursor to the current window if it's not the first one.
9284 */
9285 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9286 return FAIL;
9287
9288 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009289 * Wipe out an empty unnamed buffer we started in.
9290 */
9291 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9292 return FAIL;
9293 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9294 return FAIL;
9295 if (put_line(fd, "endif") == FAIL)
9296 return FAIL;
9297 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9298 return FAIL;
9299
9300 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 * Restore window sizes again after jumping around in windows, because the
9302 * current window has a minimum size while others may not.
9303 */
9304 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9305 return FAIL;
9306
9307 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9308 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9309 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9310 return FAIL;
9311
9312 /*
9313 * Lastly, execute the x.vim file if it exists.
9314 */
9315 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9316 || put_line(fd, "if file_readable(s:sx)") == FAIL
9317 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9318 || put_line(fd, "endif") == FAIL)
9319 return FAIL;
9320
9321 return OK;
9322}
9323
9324 static int
9325ses_winsizes(fd, restore_size)
9326 FILE *fd;
9327 int restore_size;
9328{
9329 int n = 0;
9330 win_T *wp;
9331
9332 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9333 {
9334 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9335 {
9336 if (!ses_do_win(wp))
9337 continue;
9338 ++n;
9339
9340 /* restore height when not full height */
9341 if (wp->w_height + wp->w_status_height < topframe->fr_height
9342 && (fprintf(fd,
9343 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9344 n, (long)wp->w_height, Rows / 2, Rows) < 0
9345 || put_eol(fd) == FAIL))
9346 return FAIL;
9347
9348 /* restore width when not full width */
9349 if (wp->w_width < Columns && (fprintf(fd,
9350 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9351 n, (long)wp->w_width, Columns / 2, Columns) < 0
9352 || put_eol(fd) == FAIL))
9353 return FAIL;
9354 }
9355 }
9356 else
9357 {
9358 /* Just equalise window sizes */
9359 if (put_line(fd, "wincmd =") == FAIL)
9360 return FAIL;
9361 }
9362 return OK;
9363}
9364
9365/*
9366 * Write commands to "fd" to recursively create windows for frame "fr",
9367 * horizontally and vertically split.
9368 * After the commands the last window in the frame is the current window.
9369 * Returns FAIL when writing the commands to "fd" fails.
9370 */
9371 static int
9372ses_win_rec(fd, fr)
9373 FILE *fd;
9374 frame_T *fr;
9375{
9376 frame_T *frc;
9377 int count = 0;
9378
9379 if (fr->fr_layout != FR_LEAF)
9380 {
9381 /* Find first frame that's not skipped and then create a window for
9382 * each following one (first frame is already there). */
9383 frc = ses_skipframe(fr->fr_child);
9384 if (frc != NULL)
9385 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9386 {
9387 /* Make window as big as possible so that we have lots of room
9388 * to split. */
9389 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9390 || put_line(fd, fr->fr_layout == FR_COL
9391 ? "split" : "vsplit") == FAIL)
9392 return FAIL;
9393 ++count;
9394 }
9395
9396 /* Go back to the first window. */
9397 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9398 ? "%dwincmd k" : "%dwincmd h", count) < 0
9399 || put_eol(fd) == FAIL))
9400 return FAIL;
9401
9402 /* Recursively create frames/windows in each window of this column or
9403 * row. */
9404 frc = ses_skipframe(fr->fr_child);
9405 while (frc != NULL)
9406 {
9407 ses_win_rec(fd, frc);
9408 frc = ses_skipframe(frc->fr_next);
9409 /* Go to next window. */
9410 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9411 return FAIL;
9412 }
9413 }
9414 return OK;
9415}
9416
9417/*
9418 * Skip frames that don't contain windows we want to save in the Session.
9419 * Returns NULL when there none.
9420 */
9421 static frame_T *
9422ses_skipframe(fr)
9423 frame_T *fr;
9424{
9425 frame_T *frc;
9426
9427 for (frc = fr; frc != NULL; frc = frc->fr_next)
9428 if (ses_do_frame(frc))
9429 break;
9430 return frc;
9431}
9432
9433/*
9434 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9435 * the Session.
9436 */
9437 static int
9438ses_do_frame(fr)
9439 frame_T *fr;
9440{
9441 frame_T *frc;
9442
9443 if (fr->fr_layout == FR_LEAF)
9444 return ses_do_win(fr->fr_win);
9445 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9446 if (ses_do_frame(frc))
9447 return TRUE;
9448 return FALSE;
9449}
9450
9451/*
9452 * Return non-zero if window "wp" is to be stored in the Session.
9453 */
9454 static int
9455ses_do_win(wp)
9456 win_T *wp;
9457{
9458 if (wp->w_buffer->b_fname == NULL
9459#ifdef FEAT_QUICKFIX
9460 /* When 'buftype' is "nofile" can't restore the window contents. */
9461 || bt_nofile(wp->w_buffer)
9462#endif
9463 )
9464 return (ssop_flags & SSOP_BLANK);
9465 if (wp->w_buffer->b_help)
9466 return (ssop_flags & SSOP_HELP);
9467 return TRUE;
9468}
9469
9470/*
9471 * Write commands to "fd" to restore the view of a window.
9472 * Caller must make sure 'scrolloff' is zero.
9473 */
9474 static int
9475put_view(fd, wp, add_edit, flagp)
9476 FILE *fd;
9477 win_T *wp;
9478 int add_edit; /* add ":edit" command to view */
9479 unsigned *flagp; /* vop_flags or ssop_flags */
9480{
9481 win_T *save_curwin;
9482 int f;
9483 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009484 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485
9486 /* Always restore cursor position for ":mksession". For ":mkview" only
9487 * when 'viewoptions' contains "cursor". */
9488 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9489
9490 /*
9491 * Local argument list.
9492 */
9493 if (wp->w_alist == &global_alist)
9494 {
9495 if (put_line(fd, "argglobal") == FAIL)
9496 return FAIL;
9497 }
9498 else
9499 {
9500 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9501 flagp == &vop_flags
9502 || !(*flagp & SSOP_CURDIR)
9503 || wp->w_localdir != NULL, flagp) == FAIL)
9504 return FAIL;
9505 }
9506
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009507 /* Only when part of a session: restore the argument index. Some
9508 * arguments may have been deleted, check if the index is valid. */
9509 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
9510 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 {
9512 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9513 || put_eol(fd) == FAIL)
9514 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009515 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 }
9517
9518 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009519 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 {
9521 /*
9522 * Load the file.
9523 */
9524 if (wp->w_buffer->b_ffname != NULL
9525#ifdef FEAT_QUICKFIX
9526 && !bt_nofile(wp->w_buffer)
9527#endif
9528 )
9529 {
9530 /*
9531 * Editing a file in this buffer: use ":edit file".
9532 * This may have side effects! (e.g., compressed or network file).
9533 */
9534 if (fputs("edit ", fd) < 0
9535 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9536 return FAIL;
9537 }
9538 else
9539 {
9540 /* No file in this buffer, just make it empty. */
9541 if (put_line(fd, "enew") == FAIL)
9542 return FAIL;
9543#ifdef FEAT_QUICKFIX
9544 if (wp->w_buffer->b_ffname != NULL)
9545 {
9546 /* The buffer does have a name, but it's not a file name. */
9547 if (fputs("file ", fd) < 0
9548 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9549 return FAIL;
9550 }
9551#endif
9552 do_cursor = FALSE;
9553 }
9554 }
9555
9556 /*
9557 * Local mappings and abbreviations.
9558 */
9559 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9560 && makemap(fd, wp->w_buffer) == FAIL)
9561 return FAIL;
9562
9563 /*
9564 * Local options. Need to go to the window temporarily.
9565 * Store only local values when using ":mkview" and when ":mksession" is
9566 * used and 'sessionoptions' doesn't include "options".
9567 * Some folding options are always stored when "folds" is included,
9568 * otherwise the folds would not be restored correctly.
9569 */
9570 save_curwin = curwin;
9571 curwin = wp;
9572 curbuf = curwin->w_buffer;
9573 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9574 f = makeset(fd, OPT_LOCAL,
9575 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
9576#ifdef FEAT_FOLDING
9577 else if (*flagp & SSOP_FOLDS)
9578 f = makefoldset(fd);
9579#endif
9580 else
9581 f = OK;
9582 curwin = save_curwin;
9583 curbuf = curwin->w_buffer;
9584 if (f == FAIL)
9585 return FAIL;
9586
9587#ifdef FEAT_FOLDING
9588 /*
9589 * Save Folds when 'buftype' is empty and for help files.
9590 */
9591 if ((*flagp & SSOP_FOLDS)
9592 && wp->w_buffer->b_ffname != NULL
9593 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help))
9594 {
9595 if (put_folds(fd, wp) == FAIL)
9596 return FAIL;
9597 }
9598#endif
9599
9600 /*
9601 * Set the cursor after creating folds, since that moves the cursor.
9602 */
9603 if (do_cursor)
9604 {
9605
9606 /* Restore the cursor line in the file and relatively in the
9607 * window. Don't use "G", it changes the jumplist. */
9608 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
9609 (long)wp->w_cursor.lnum,
9610 (long)(wp->w_cursor.lnum - wp->w_topline),
9611 (long)wp->w_height / 2, (long)wp->w_height) < 0
9612 || put_eol(fd) == FAIL
9613 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
9614 || put_line(fd, "exe s:l") == FAIL
9615 || put_line(fd, "normal! zt") == FAIL
9616 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
9617 || put_eol(fd) == FAIL)
9618 return FAIL;
9619 /* Restore the cursor column and left offset when not wrapping. */
9620 if (wp->w_cursor.col == 0)
9621 {
9622 if (put_line(fd, "normal! 0") == FAIL)
9623 return FAIL;
9624 }
9625 else
9626 {
9627 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
9628 {
9629 if (fprintf(fd,
9630 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
9631 (long)wp->w_cursor.col,
9632 (long)(wp->w_cursor.col - wp->w_leftcol),
9633 (long)wp->w_width / 2, (long)wp->w_width) < 0
9634 || put_eol(fd) == FAIL
9635 || put_line(fd, "if s:c > 0") == FAIL
9636 || fprintf(fd,
9637 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
9638 (long)wp->w_cursor.col) < 0
9639 || put_eol(fd) == FAIL
9640 || put_line(fd, "else") == FAIL
9641 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
9642 || put_eol(fd) == FAIL
9643 || put_line(fd, "endif") == FAIL)
9644 return FAIL;
9645 }
9646 else
9647 {
9648 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
9649 || put_eol(fd) == FAIL)
9650 return FAIL;
9651 }
9652 }
9653 }
9654
9655 /*
9656 * Local directory.
9657 */
9658 if (wp->w_localdir != NULL)
9659 {
9660 if (fputs("lcd ", fd) < 0
9661 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
9662 || put_eol(fd) == FAIL)
9663 return FAIL;
9664 }
9665
9666 return OK;
9667}
9668
9669/*
9670 * Write an argument list to the session file.
9671 * Returns FAIL if writing fails.
9672 */
9673 static int
9674ses_arglist(fd, cmd, gap, fullname, flagp)
9675 FILE *fd;
9676 char *cmd;
9677 garray_T *gap;
9678 int fullname; /* TRUE: use full path name */
9679 unsigned *flagp;
9680{
9681 int i;
9682 char_u buf[MAXPATHL];
9683 char_u *s;
9684
9685 if (gap->ga_len == 0)
9686 return put_line(fd, "silent! argdel *");
9687 if (fputs(cmd, fd) < 0)
9688 return FAIL;
9689 for (i = 0; i < gap->ga_len; ++i)
9690 {
9691 /* NULL file names are skipped (only happens when out of memory). */
9692 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
9693 if (s != NULL)
9694 {
9695 if (fullname)
9696 {
9697 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
9698 s = buf;
9699 }
9700 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
9701 return FAIL;
9702 }
9703 }
9704 return put_eol(fd);
9705}
9706
9707/*
9708 * Write a buffer name to the session file.
9709 * Also ends the line.
9710 * Returns FAIL if writing fails.
9711 */
9712 static int
9713ses_fname(fd, buf, flagp)
9714 FILE *fd;
9715 buf_T *buf;
9716 unsigned *flagp;
9717{
9718 char_u *name;
9719
9720 /* Use the short file name if the current directory is known at the time
9721 * the session file will be sourced. Don't do this for ":mkview", we
9722 * don't know the current directory. */
9723 if (buf->b_sfname != NULL
9724 && flagp == &ssop_flags
9725 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
9726 name = buf->b_sfname;
9727 else
9728 name = buf->b_ffname;
9729 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
9730 return FAIL;
9731 return OK;
9732}
9733
9734/*
9735 * Write a file name to the session file.
9736 * Takes care of the "slash" option in 'sessionoptions' and escapes special
9737 * characters.
9738 * Returns FAIL if writing fails.
9739 */
9740 static int
9741ses_put_fname(fd, name, flagp)
9742 FILE *fd;
9743 char_u *name;
9744 unsigned *flagp;
9745{
9746 char_u *sname;
9747 int retval = OK;
9748 int c;
9749
9750 sname = home_replace_save(NULL, name);
9751 if (sname != NULL)
9752 name = sname;
9753 while (*name != NUL)
9754 {
9755#ifdef FEAT_MBYTE
9756 {
9757 int l;
9758
9759 if (has_mbyte && (l = (*mb_ptr2len_check)(name)) > 1)
9760 {
9761 /* copy a multibyte char */
9762 while (--l >= 0)
9763 {
9764 if (putc(*name, fd) != *name)
9765 retval = FAIL;
9766 ++name;
9767 }
9768 continue;
9769 }
9770 }
9771#endif
9772 c = *name++;
9773 if (c == '\\' && (*flagp & SSOP_SLASH))
9774 /* change a backslash to a forward slash */
9775 c = '/';
9776 else if ((vim_strchr(escape_chars, c) != NULL
9777#ifdef BACKSLASH_IN_FILENAME
9778 && c != '\\'
9779#endif
9780 ) || c == '#' || c == '%')
9781 {
9782 /* escape a special character with a backslash */
9783 if (putc('\\', fd) != '\\')
9784 retval = FAIL;
9785 }
9786 if (putc(c, fd) != c)
9787 retval = FAIL;
9788 }
9789 vim_free(sname);
9790 return retval;
9791}
9792
9793/*
9794 * ":loadview [nr]"
9795 */
9796 static void
9797ex_loadview(eap)
9798 exarg_T *eap;
9799{
9800 char_u *fname;
9801
9802 fname = get_view_file(*eap->arg);
9803 if (fname != NULL)
9804 {
9805 do_source(fname, FALSE, FALSE);
9806 vim_free(fname);
9807 }
9808}
9809
9810/*
9811 * Get the name of the view file for the current buffer.
9812 */
9813 static char_u *
9814get_view_file(c)
9815 int c;
9816{
9817 int len = 0;
9818 char_u *p, *s;
9819 char_u *retval;
9820 char_u *sname;
9821
9822 if (curbuf->b_ffname == NULL)
9823 {
9824 EMSG(_(e_noname));
9825 return NULL;
9826 }
9827 sname = home_replace_save(NULL, curbuf->b_ffname);
9828 if (sname == NULL)
9829 return NULL;
9830
9831 /*
9832 * We want a file name without separators, because we're not going to make
9833 * a directory.
9834 * "normal" path separator -> "=+"
9835 * "=" -> "=="
9836 * ":" path separator -> "=-"
9837 */
9838 for (p = sname; *p; ++p)
9839 if (*p == '=' || vim_ispathsep(*p))
9840 ++len;
9841 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
9842 if (retval != NULL)
9843 {
9844 STRCPY(retval, p_vdir);
9845 add_pathsep(retval);
9846 s = retval + STRLEN(retval);
9847 for (p = sname; *p; ++p)
9848 {
9849 if (*p == '=')
9850 {
9851 *s++ = '=';
9852 *s++ = '=';
9853 }
9854 else if (vim_ispathsep(*p))
9855 {
9856 *s++ = '=';
9857#ifdef MACOS_CLASSIC /* TODO: Is it also needed for MACOS_X? (Dany) */
9858 *s++ = '+';
9859#else
9860# if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
9861 || defined(VMS)
9862 if (*p == ':')
9863 *s++ = '-';
9864 else
9865# endif
9866 *s++ = '+';
9867#endif
9868 }
9869 else
9870 *s++ = *p;
9871 }
9872 *s++ = '=';
9873 *s++ = c;
9874 STRCPY(s, ".vim");
9875 }
9876
9877 vim_free(sname);
9878 return retval;
9879}
9880
9881#endif /* FEAT_SESSION */
9882
9883/*
9884 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
9885 * Return FAIL for a write error.
9886 */
9887 int
9888put_eol(fd)
9889 FILE *fd;
9890{
9891 if (
9892#ifdef USE_CRNL
9893 (
9894# ifdef MKSESSION_NL
9895 !mksession_nl &&
9896# endif
9897 (putc('\r', fd) < 0)) ||
9898#endif
9899 (putc('\n', fd) < 0))
9900 return FAIL;
9901 return OK;
9902}
9903
9904/*
9905 * Write a line to "fd".
9906 * Return FAIL for a write error.
9907 */
9908 int
9909put_line(fd, s)
9910 FILE *fd;
9911 char *s;
9912{
9913 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
9914 return FAIL;
9915 return OK;
9916}
9917
9918#ifdef FEAT_VIMINFO
9919/*
9920 * ":rviminfo" and ":wviminfo".
9921 */
9922 static void
9923ex_viminfo(eap)
9924 exarg_T *eap;
9925{
9926 char_u *save_viminfo;
9927
9928 save_viminfo = p_viminfo;
9929 if (*p_viminfo == NUL)
9930 p_viminfo = (char_u *)"'100";
9931 if (eap->cmdidx == CMD_rviminfo)
9932 {
9933 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
9934 EMSG(_("E195: Cannot open viminfo file for reading"));
9935 }
9936 else
9937 write_viminfo(eap->arg, eap->forceit);
9938 p_viminfo = save_viminfo;
9939}
9940#endif
9941
9942#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009943/*
9944 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009945 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 void
9948dialog_msg(buff, format, fname)
9949 char_u *buff;
9950 char *format;
9951 char_u *fname;
9952{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 if (fname == NULL)
9954 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009955 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956}
9957#endif
9958
9959/*
9960 * ":behave {mswin,xterm}"
9961 */
9962 static void
9963ex_behave(eap)
9964 exarg_T *eap;
9965{
9966 if (STRCMP(eap->arg, "mswin") == 0)
9967 {
9968 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9969 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9970 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9971 set_option_value((char_u *)"keymodel", 0L,
9972 (char_u *)"startsel,stopsel", 0);
9973 }
9974 else if (STRCMP(eap->arg, "xterm") == 0)
9975 {
9976 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9977 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9978 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9979 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9980 }
9981 else
9982 EMSG2(_(e_invarg2), eap->arg);
9983}
9984
9985#ifdef FEAT_AUTOCMD
9986static int filetype_detect = FALSE;
9987static int filetype_plugin = FALSE;
9988static int filetype_indent = FALSE;
9989
9990/*
9991 * ":filetype [plugin] [indent] {on,off,detect}"
9992 * on: Load the filetype.vim file to install autocommands for file types.
9993 * off: Load the ftoff.vim file to remove all autocommands for file types.
9994 * plugin on: load filetype.vim and ftplugin.vim
9995 * plugin off: load ftplugof.vim
9996 * indent on: load filetype.vim and indent.vim
9997 * indent off: load indoff.vim
9998 */
9999 static void
10000ex_filetype(eap)
10001 exarg_T *eap;
10002{
10003 char_u *arg = eap->arg;
10004 int plugin = FALSE;
10005 int indent = FALSE;
10006
10007 if (*eap->arg == NUL)
10008 {
10009 /* Print current status. */
10010 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10011 filetype_detect ? "ON" : "OFF",
10012 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10013 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10014 return;
10015 }
10016
10017 /* Accept "plugin" and "indent" in any order. */
10018 for (;;)
10019 {
10020 if (STRNCMP(arg, "plugin", 6) == 0)
10021 {
10022 plugin = TRUE;
10023 arg = skipwhite(arg + 6);
10024 continue;
10025 }
10026 if (STRNCMP(arg, "indent", 6) == 0)
10027 {
10028 indent = TRUE;
10029 arg = skipwhite(arg + 6);
10030 continue;
10031 }
10032 break;
10033 }
10034 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10035 {
10036 if (*arg == 'o' || !filetype_detect)
10037 {
10038 cmd_runtime((char_u *)FILETYPE_FILE, TRUE);
10039 filetype_detect = TRUE;
10040 if (plugin)
10041 {
10042 cmd_runtime((char_u *)FTPLUGIN_FILE, TRUE);
10043 filetype_plugin = TRUE;
10044 }
10045 if (indent)
10046 {
10047 cmd_runtime((char_u *)INDENT_FILE, TRUE);
10048 filetype_indent = TRUE;
10049 }
10050 }
10051 if (*arg == 'd')
10052 {
10053 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +000010054 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 }
10056 }
10057 else if (STRCMP(arg, "off") == 0)
10058 {
10059 if (plugin || indent)
10060 {
10061 if (plugin)
10062 {
10063 cmd_runtime((char_u *)FTPLUGOF_FILE, TRUE);
10064 filetype_plugin = FALSE;
10065 }
10066 if (indent)
10067 {
10068 cmd_runtime((char_u *)INDOFF_FILE, TRUE);
10069 filetype_indent = FALSE;
10070 }
10071 }
10072 else
10073 {
10074 cmd_runtime((char_u *)FTOFF_FILE, TRUE);
10075 filetype_detect = FALSE;
10076 }
10077 }
10078 else
10079 EMSG2(_(e_invarg2), arg);
10080}
10081
10082/*
10083 * ":setfiletype {name}"
10084 */
10085 static void
10086ex_setfiletype(eap)
10087 exarg_T *eap;
10088{
10089 if (!did_filetype)
10090 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10091}
10092#endif
10093
10094/*ARGSUSED*/
10095 static void
10096ex_digraphs(eap)
10097 exarg_T *eap;
10098{
10099#ifdef FEAT_DIGRAPHS
10100 if (*eap->arg != NUL)
10101 putdigraph(eap->arg);
10102 else
10103 listdigraphs();
10104#else
10105 EMSG(_("E196: No digraphs in this version"));
10106#endif
10107}
10108
10109 static void
10110ex_set(eap)
10111 exarg_T *eap;
10112{
10113 int flags = 0;
10114
10115 if (eap->cmdidx == CMD_setlocal)
10116 flags = OPT_LOCAL;
10117 else if (eap->cmdidx == CMD_setglobal)
10118 flags = OPT_GLOBAL;
10119#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10120 if (cmdmod.browse && flags == 0)
10121 ex_options(eap);
10122 else
10123#endif
10124 (void)do_set(eap->arg, flags);
10125}
10126
10127#ifdef FEAT_SEARCH_EXTRA
10128/*
10129 * ":nohlsearch"
10130 */
10131/*ARGSUSED*/
10132 static void
10133ex_nohlsearch(eap)
10134 exarg_T *eap;
10135{
10136 no_hlsearch = TRUE;
10137 redraw_all_later(NOT_VALID);
10138}
10139
10140/*
10141 * ":match {group} {pattern}"
10142 * Sets nextcmd to the start of the next command, if any. Also called when
10143 * skipping commands to find the next command.
10144 */
10145 static void
10146ex_match(eap)
10147 exarg_T *eap;
10148{
10149 char_u *p;
10150 char_u *end;
10151 int c;
10152
10153 /* First clear any old pattern. */
10154 if (!eap->skip)
10155 {
10156 vim_free(curwin->w_match.regprog);
10157 curwin->w_match.regprog = NULL;
10158 redraw_later(NOT_VALID); /* always need a redraw */
10159 }
10160
10161 if (ends_excmd(*eap->arg))
10162 end = eap->arg;
10163 else if ((STRNICMP(eap->arg, "none", 4) == 0
10164 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10165 end = eap->arg + 4;
10166 else
10167 {
10168 p = skiptowhite(eap->arg);
10169 if (!eap->skip)
10170 {
10171 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg));
10172 if (curwin->w_match_id == 0)
10173 {
10174 EMSG2(_(e_nogroup), eap->arg);
10175 return;
10176 }
10177 }
10178 p = skipwhite(p);
10179 if (*p == NUL)
10180 {
10181 /* There must be two arguments. */
10182 EMSG2(_(e_invarg2), eap->arg);
10183 return;
10184 }
10185 end = skip_regexp(p + 1, *p, TRUE, NULL);
10186 if (!eap->skip)
10187 {
10188 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10189 {
10190 eap->errmsg = e_trailing;
10191 return;
10192 }
10193
10194 c = *end;
10195 *end = NUL;
10196 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC);
10197 *end = c;
10198 if (curwin->w_match.regprog == NULL)
10199 {
10200 EMSG2(_(e_invarg2), p);
10201 return;
10202 }
10203 }
10204 }
10205 eap->nextcmd = find_nextcmd(end);
10206}
10207#endif
10208
10209#ifdef FEAT_CRYPT
10210/*
10211 * ":X": Get crypt key
10212 */
10213/*ARGSUSED*/
10214 static void
10215ex_X(eap)
10216 exarg_T *eap;
10217{
10218 (void)get_crypt_key(TRUE, TRUE);
10219}
10220#endif
10221
10222#ifdef FEAT_FOLDING
10223 static void
10224ex_fold(eap)
10225 exarg_T *eap;
10226{
10227 if (foldManualAllowed(TRUE))
10228 foldCreate(eap->line1, eap->line2);
10229}
10230
10231 static void
10232ex_foldopen(eap)
10233 exarg_T *eap;
10234{
10235 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10236 eap->forceit, FALSE);
10237}
10238
10239 static void
10240ex_folddo(eap)
10241 exarg_T *eap;
10242{
10243 linenr_T lnum;
10244
10245 /* First set the marks for all lines closed/open. */
10246 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10247 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10248 ml_setmarked(lnum);
10249
10250 /* Execute the command on the marked lines. */
10251 global_exe(eap->arg);
10252 ml_clearmarked(); /* clear rest of the marks */
10253}
10254#endif