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