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