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