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