blob: 42c609870f3119b4b4d3086cd33150332fd43a6d [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
3339 /* Command modifiers: return the argument. */
3340 case CMD_aboveleft:
3341 case CMD_belowright:
3342 case CMD_botright:
3343 case CMD_browse:
3344 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003345 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 case CMD_folddoclosed:
3347 case CMD_folddoopen:
3348 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003349 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 case CMD_keepjumps:
3351 case CMD_keepmarks:
3352 case CMD_leftabove:
3353 case CMD_lockmarks:
3354 case CMD_rightbelow:
3355 case CMD_silent:
3356 case CMD_topleft:
3357 case CMD_verbose:
3358 case CMD_vertical:
3359 return arg;
3360
3361#ifdef FEAT_SEARCH_EXTRA
3362 case CMD_match:
3363 if (*arg == NUL || !ends_excmd(*arg))
3364 {
3365 /* Dummy call to clear variables. */
3366 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3367 xp->xp_context = EXPAND_HIGHLIGHT;
3368 xp->xp_pattern = arg;
3369 arg = skipwhite(skiptowhite(arg));
3370 if (*arg != NUL)
3371 {
3372 xp->xp_context = EXPAND_NOTHING;
3373 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3374 }
3375 }
3376 return find_nextcmd(arg);
3377#endif
3378
3379#ifdef FEAT_CMDL_COMPL
3380/*
3381 * All completion for the +cmdline_compl feature goes here.
3382 */
3383
3384# ifdef FEAT_USR_CMDS
3385 case CMD_command:
3386 /* Check for attributes */
3387 while (*arg == '-')
3388 {
3389 arg++; /* Skip "-" */
3390 p = skiptowhite(arg);
3391 if (*p == NUL)
3392 {
3393 /* Cursor is still in the attribute */
3394 p = vim_strchr(arg, '=');
3395 if (p == NULL)
3396 {
3397 /* No "=", so complete attribute names */
3398 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3399 xp->xp_pattern = arg;
3400 return NULL;
3401 }
3402
3403 /* For the -complete and -nargs attributes, we complete
3404 * their arguments as well.
3405 */
3406 if (STRNICMP(arg, "complete", p - arg) == 0)
3407 {
3408 xp->xp_context = EXPAND_USER_COMPLETE;
3409 xp->xp_pattern = p + 1;
3410 return NULL;
3411 }
3412 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3413 {
3414 xp->xp_context = EXPAND_USER_NARGS;
3415 xp->xp_pattern = p + 1;
3416 return NULL;
3417 }
3418 return NULL;
3419 }
3420 arg = skipwhite(p);
3421 }
3422
3423 /* After the attributes comes the new command name */
3424 p = skiptowhite(arg);
3425 if (*p == NUL)
3426 {
3427 xp->xp_context = EXPAND_USER_COMMANDS;
3428 xp->xp_pattern = arg;
3429 break;
3430 }
3431
3432 /* And finally comes a normal command */
3433 return skipwhite(p);
3434
3435 case CMD_delcommand:
3436 xp->xp_context = EXPAND_USER_COMMANDS;
3437 xp->xp_pattern = arg;
3438 break;
3439# endif
3440
3441 case CMD_global:
3442 case CMD_vglobal:
3443 delim = *arg; /* get the delimiter */
3444 if (delim)
3445 ++arg; /* skip delimiter if there is one */
3446
3447 while (arg[0] != NUL && arg[0] != delim)
3448 {
3449 if (arg[0] == '\\' && arg[1] != NUL)
3450 ++arg;
3451 ++arg;
3452 }
3453 if (arg[0] != NUL)
3454 return arg + 1;
3455 break;
3456 case CMD_and:
3457 case CMD_substitute:
3458 delim = *arg;
3459 if (delim)
3460 {
3461 /* skip "from" part */
3462 ++arg;
3463 arg = skip_regexp(arg, delim, p_magic, NULL);
3464 }
3465 /* skip "to" part */
3466 while (arg[0] != NUL && arg[0] != delim)
3467 {
3468 if (arg[0] == '\\' && arg[1] != NUL)
3469 ++arg;
3470 ++arg;
3471 }
3472 if (arg[0] != NUL) /* skip delimiter */
3473 ++arg;
3474 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3475 ++arg;
3476 if (arg[0] != NUL)
3477 return arg;
3478 break;
3479 case CMD_isearch:
3480 case CMD_dsearch:
3481 case CMD_ilist:
3482 case CMD_dlist:
3483 case CMD_ijump:
3484 case CMD_psearch:
3485 case CMD_djump:
3486 case CMD_isplit:
3487 case CMD_dsplit:
3488 arg = skipwhite(skipdigits(arg)); /* skip count */
3489 if (*arg == '/') /* Match regexp, not just whole words */
3490 {
3491 for (++arg; *arg && *arg != '/'; arg++)
3492 if (*arg == '\\' && arg[1] != NUL)
3493 arg++;
3494 if (*arg)
3495 {
3496 arg = skipwhite(arg + 1);
3497
3498 /* Check for trailing illegal characters */
3499 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3500 xp->xp_context = EXPAND_NOTHING;
3501 else
3502 return arg;
3503 }
3504 }
3505 break;
3506#ifdef FEAT_AUTOCMD
3507 case CMD_autocmd:
3508 return set_context_in_autocmd(xp, arg, FALSE);
3509
3510 case CMD_doautocmd:
3511 return set_context_in_autocmd(xp, arg, TRUE);
3512#endif
3513 case CMD_set:
3514 set_context_in_set_cmd(xp, arg, 0);
3515 break;
3516 case CMD_setglobal:
3517 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3518 break;
3519 case CMD_setlocal:
3520 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3521 break;
3522 case CMD_tag:
3523 case CMD_stag:
3524 case CMD_ptag:
3525 case CMD_tselect:
3526 case CMD_stselect:
3527 case CMD_ptselect:
3528 case CMD_tjump:
3529 case CMD_stjump:
3530 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003531 if (*p_wop != NUL)
3532 xp->xp_context = EXPAND_TAGS_LISTFILES;
3533 else
3534 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 xp->xp_pattern = arg;
3536 break;
3537 case CMD_augroup:
3538 xp->xp_context = EXPAND_AUGROUP;
3539 xp->xp_pattern = arg;
3540 break;
3541#ifdef FEAT_SYN_HL
3542 case CMD_syntax:
3543 set_context_in_syntax_cmd(xp, arg);
3544 break;
3545#endif
3546#ifdef FEAT_EVAL
3547 case CMD_let:
3548 case CMD_if:
3549 case CMD_elseif:
3550 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003551 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 case CMD_echo:
3553 case CMD_echon:
3554 case CMD_execute:
3555 case CMD_echomsg:
3556 case CMD_echoerr:
3557 case CMD_call:
3558 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003559 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 break;
3561
3562 case CMD_unlet:
3563 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3564 arg = xp->xp_pattern + 1;
3565 xp->xp_context = EXPAND_USER_VARS;
3566 xp->xp_pattern = arg;
3567 break;
3568
3569 case CMD_function:
3570 case CMD_delfunction:
3571 xp->xp_context = EXPAND_USER_FUNC;
3572 xp->xp_pattern = arg;
3573 break;
3574
3575 case CMD_echohl:
3576 xp->xp_context = EXPAND_HIGHLIGHT;
3577 xp->xp_pattern = arg;
3578 break;
3579#endif
3580 case CMD_highlight:
3581 set_context_in_highlight_cmd(xp, arg);
3582 break;
3583#ifdef FEAT_LISTCMDS
3584 case CMD_bdelete:
3585 case CMD_bwipeout:
3586 case CMD_bunload:
3587 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3588 arg = xp->xp_pattern + 1;
3589 /*FALLTHROUGH*/
3590 case CMD_buffer:
3591 case CMD_sbuffer:
3592 case CMD_checktime:
3593 xp->xp_context = EXPAND_BUFFERS;
3594 xp->xp_pattern = arg;
3595 break;
3596#endif
3597#ifdef FEAT_USR_CMDS
3598 case CMD_USER:
3599 case CMD_USER_BUF:
3600 if (compl != EXPAND_NOTHING)
3601 {
3602 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003603 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
3605# ifdef FEAT_MENU
3606 if (compl == EXPAND_MENUS)
3607 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3608# endif
3609 if (compl == EXPAND_COMMANDS)
3610 return arg;
3611 if (compl == EXPAND_MAPPINGS)
3612 return set_context_in_map_cmd(xp, (char_u *)"map",
3613 arg, forceit, FALSE, FALSE, CMD_map);
3614 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3615 arg = xp->xp_pattern + 1;
3616 xp->xp_pattern = arg;
3617 }
3618 xp->xp_context = compl;
3619 }
3620 break;
3621#endif
3622 case CMD_map: case CMD_noremap:
3623 case CMD_nmap: case CMD_nnoremap:
3624 case CMD_vmap: case CMD_vnoremap:
3625 case CMD_omap: case CMD_onoremap:
3626 case CMD_imap: case CMD_inoremap:
3627 case CMD_cmap: case CMD_cnoremap:
3628 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003629 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 case CMD_unmap:
3631 case CMD_nunmap:
3632 case CMD_vunmap:
3633 case CMD_ounmap:
3634 case CMD_iunmap:
3635 case CMD_cunmap:
3636 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003637 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 case CMD_abbreviate: case CMD_noreabbrev:
3639 case CMD_cabbrev: case CMD_cnoreabbrev:
3640 case CMD_iabbrev: case CMD_inoreabbrev:
3641 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003642 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 case CMD_unabbreviate:
3644 case CMD_cunabbrev:
3645 case CMD_iunabbrev:
3646 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003647 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648#ifdef FEAT_MENU
3649 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3650 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3651 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3652 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3653 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3654 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3655 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3656 case CMD_tmenu: case CMD_tunmenu:
3657 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3658 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3659#endif
3660
3661 case CMD_colorscheme:
3662 xp->xp_context = EXPAND_COLORS;
3663 xp->xp_pattern = arg;
3664 break;
3665
3666 case CMD_compiler:
3667 xp->xp_context = EXPAND_COMPILER;
3668 xp->xp_pattern = arg;
3669 break;
3670
3671#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3672 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3673 case CMD_language:
3674 if (*skiptowhite(arg) == NUL)
3675 {
3676 xp->xp_context = EXPAND_LANGUAGE;
3677 xp->xp_pattern = arg;
3678 }
3679 else
3680 xp->xp_context = EXPAND_NOTHING;
3681 break;
3682#endif
3683
3684#endif /* FEAT_CMDL_COMPL */
3685
3686 default:
3687 break;
3688 }
3689 return NULL;
3690}
3691
3692/*
3693 * skip a range specifier of the form: addr [,addr] [;addr] ..
3694 *
3695 * Backslashed delimiters after / or ? will be skipped, and commands will
3696 * not be expanded between /'s and ?'s or after "'".
3697 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003698 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 * Returns the "cmd" pointer advanced to beyond the range.
3700 */
3701 char_u *
3702skip_range(cmd, ctx)
3703 char_u *cmd;
3704 int *ctx; /* pointer to xp_context or NULL */
3705{
3706 int delim;
3707
Bram Moolenaardf177f62005-02-22 08:39:57 +00003708 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 {
3710 if (*cmd == '\'')
3711 {
3712 if (*++cmd == NUL && ctx != NULL)
3713 *ctx = EXPAND_NOTHING;
3714 }
3715 else if (*cmd == '/' || *cmd == '?')
3716 {
3717 delim = *cmd++;
3718 while (*cmd != NUL && *cmd != delim)
3719 if (*cmd++ == '\\' && *cmd != NUL)
3720 ++cmd;
3721 if (*cmd == NUL && ctx != NULL)
3722 *ctx = EXPAND_NOTHING;
3723 }
3724 if (*cmd != NUL)
3725 ++cmd;
3726 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003727
3728 /* Skip ":" and white space. */
3729 while (*cmd == ':')
3730 cmd = skipwhite(cmd + 1);
3731
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 return cmd;
3733}
3734
3735/*
3736 * get a single EX address
3737 *
3738 * Set ptr to the next character after the part that was interpreted.
3739 * Set ptr to NULL when an error is encountered.
3740 *
3741 * Return MAXLNUM when no Ex address was found.
3742 */
3743 static linenr_T
3744get_address(ptr, skip, to_other_file)
3745 char_u **ptr;
3746 int skip; /* only skip the address, don't use it */
3747 int to_other_file; /* flag: may jump to other file */
3748{
3749 int c;
3750 int i;
3751 long n;
3752 char_u *cmd;
3753 pos_T pos;
3754 pos_T *fp;
3755 linenr_T lnum;
3756
3757 cmd = skipwhite(*ptr);
3758 lnum = MAXLNUM;
3759 do
3760 {
3761 switch (*cmd)
3762 {
3763 case '.': /* '.' - Cursor position */
3764 ++cmd;
3765 lnum = curwin->w_cursor.lnum;
3766 break;
3767
3768 case '$': /* '$' - last line */
3769 ++cmd;
3770 lnum = curbuf->b_ml.ml_line_count;
3771 break;
3772
3773 case '\'': /* ''' - mark */
3774 if (*++cmd == NUL)
3775 {
3776 cmd = NULL;
3777 goto error;
3778 }
3779 if (skip)
3780 ++cmd;
3781 else
3782 {
3783 /* Only accept a mark in another file when it is
3784 * used by itself: ":'M". */
3785 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3786 ++cmd;
3787 if (fp == (pos_T *)-1)
3788 /* Jumped to another file. */
3789 lnum = curwin->w_cursor.lnum;
3790 else
3791 {
3792 if (check_mark(fp) == FAIL)
3793 {
3794 cmd = NULL;
3795 goto error;
3796 }
3797 lnum = fp->lnum;
3798 }
3799 }
3800 break;
3801
3802 case '/':
3803 case '?': /* '/' or '?' - search */
3804 c = *cmd++;
3805 if (skip) /* skip "/pat/" */
3806 {
3807 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3808 if (*cmd == c)
3809 ++cmd;
3810 }
3811 else
3812 {
3813 pos = curwin->w_cursor; /* save curwin->w_cursor */
3814 /*
3815 * When '/' or '?' follows another address, start
3816 * from there.
3817 */
3818 if (lnum != MAXLNUM)
3819 curwin->w_cursor.lnum = lnum;
3820 /*
3821 * Start a forward search at the end of the line.
3822 * Start a backward search at the start of the line.
3823 * This makes sure we never match in the current
3824 * line, and can match anywhere in the
3825 * next/previous line.
3826 */
3827 if (c == '/')
3828 curwin->w_cursor.col = MAXCOL;
3829 else
3830 curwin->w_cursor.col = 0;
3831 searchcmdlen = 0;
3832 if (!do_search(NULL, c, cmd, 1L,
3833 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3834 {
3835 curwin->w_cursor = pos;
3836 cmd = NULL;
3837 goto error;
3838 }
3839 lnum = curwin->w_cursor.lnum;
3840 curwin->w_cursor = pos;
3841 /* adjust command string pointer */
3842 cmd += searchcmdlen;
3843 }
3844 break;
3845
3846 case '\\': /* "\?", "\/" or "\&", repeat search */
3847 ++cmd;
3848 if (*cmd == '&')
3849 i = RE_SUBST;
3850 else if (*cmd == '?' || *cmd == '/')
3851 i = RE_SEARCH;
3852 else
3853 {
3854 EMSG(_(e_backslash));
3855 cmd = NULL;
3856 goto error;
3857 }
3858
3859 if (!skip)
3860 {
3861 /*
3862 * When search follows another address, start from
3863 * there.
3864 */
3865 if (lnum != MAXLNUM)
3866 pos.lnum = lnum;
3867 else
3868 pos.lnum = curwin->w_cursor.lnum;
3869
3870 /*
3871 * Start the search just like for the above
3872 * do_search().
3873 */
3874 if (*cmd != '?')
3875 pos.col = MAXCOL;
3876 else
3877 pos.col = 0;
3878 if (searchit(curwin, curbuf, &pos,
3879 *cmd == '?' ? BACKWARD : FORWARD,
3880 (char_u *)"", 1L,
3881 SEARCH_MSG + SEARCH_START, i) != FAIL)
3882 lnum = pos.lnum;
3883 else
3884 {
3885 cmd = NULL;
3886 goto error;
3887 }
3888 }
3889 ++cmd;
3890 break;
3891
3892 default:
3893 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3894 lnum = getdigits(&cmd);
3895 }
3896
3897 for (;;)
3898 {
3899 cmd = skipwhite(cmd);
3900 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3901 break;
3902
3903 if (lnum == MAXLNUM)
3904 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3905 if (VIM_ISDIGIT(*cmd))
3906 i = '+'; /* "number" is same as "+number" */
3907 else
3908 i = *cmd++;
3909 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3910 n = 1;
3911 else
3912 n = getdigits(&cmd);
3913 if (i == '-')
3914 lnum -= n;
3915 else
3916 lnum += n;
3917 }
3918 } while (*cmd == '/' || *cmd == '?');
3919
3920error:
3921 *ptr = cmd;
3922 return lnum;
3923}
3924
3925/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003926 * Get flags from an Ex command argument.
3927 */
3928 static void
3929get_flags(eap)
3930 exarg_T *eap;
3931{
3932 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3933 {
3934 if (*eap->arg == 'l')
3935 eap->flags |= EXFLAG_LIST;
3936 else if (*eap->arg == 'p')
3937 eap->flags |= EXFLAG_PRINT;
3938 else
3939 eap->flags |= EXFLAG_NR;
3940 eap->arg = skipwhite(eap->arg + 1);
3941 }
3942}
3943
3944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 * Function called for command which is Not Implemented. NI!
3946 */
3947 void
3948ex_ni(eap)
3949 exarg_T *eap;
3950{
3951 if (!eap->skip)
3952 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3953}
3954
3955#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003956 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957/*
3958 * Function called for script command which is Not Implemented. NI!
3959 * Skips over ":perl <<EOF" constructs.
3960 */
3961 static void
3962ex_script_ni(eap)
3963 exarg_T *eap;
3964{
3965 if (!eap->skip)
3966 ex_ni(eap);
3967 else
3968 vim_free(script_get(eap, eap->arg));
3969}
3970#endif
3971
3972/*
3973 * Check range in Ex command for validity.
3974 * Return NULL when valid, error message when invalid.
3975 */
3976 static char_u *
3977invalid_range(eap)
3978 exarg_T *eap;
3979{
3980 if ( eap->line1 < 0
3981 || eap->line2 < 0
3982 || eap->line1 > eap->line2
3983 || ((eap->argt & RANGE)
3984 && !(eap->argt & NOTADR)
3985 && eap->line2 > curbuf->b_ml.ml_line_count
3986#ifdef FEAT_DIFF
3987 + (eap->cmdidx == CMD_diffget)
3988#endif
3989 ))
3990 return (char_u *)_(e_invrange);
3991 return NULL;
3992}
3993
3994/*
3995 * Correct the range for zero line number, if required.
3996 */
3997 static void
3998correct_range(eap)
3999 exarg_T *eap;
4000{
4001 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4002 {
4003 if (eap->line1 == 0)
4004 eap->line1 = 1;
4005 if (eap->line2 == 0)
4006 eap->line2 = 1;
4007 }
4008}
4009
Bram Moolenaar748bf032005-02-02 23:04:36 +00004010#ifdef FEAT_QUICKFIX
4011static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4012
4013/*
4014 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4015 * pattern. Otherwise return eap->arg.
4016 */
4017 static char_u *
4018skip_grep_pat(eap)
4019 exarg_T *eap;
4020{
4021 char_u *p = eap->arg;
4022
4023 if (*p != NUL && (eap->cmdidx == CMD_vimgrep
4024 || eap->cmdidx == CMD_vimgrepadd || grep_internal(eap->cmdidx)))
4025 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004026 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004027 if (p == NULL)
4028 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004029 }
4030 return p;
4031}
4032#endif
4033
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034/*
4035 * Expand file name in Ex command argument.
4036 * Return FAIL for failure, OK otherwise.
4037 */
4038 int
4039expand_filename(eap, cmdlinep, errormsgp)
4040 exarg_T *eap;
4041 char_u **cmdlinep;
4042 char_u **errormsgp;
4043{
4044 int has_wildcards; /* need to expand wildcards */
4045 char_u *repl;
4046 int srclen;
4047 char_u *p;
4048 int n;
4049
Bram Moolenaar748bf032005-02-02 23:04:36 +00004050#ifdef FEAT_QUICKFIX
4051 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4052 p = skip_grep_pat(eap);
4053#else
4054 p = eap->arg;
4055#endif
4056
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 /*
4058 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4059 * the file name contains a wildcard it should not cause expanding.
4060 * (it will be expanded anyway if there is a wildcard before replacing).
4061 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004062 has_wildcards = mch_has_wildcard(p);
4063 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004065#ifdef FEAT_EVAL
4066 /* Skip over `=expr`, wildcards in it are not expanded. */
4067 if (p[0] == '`' && p[1] == '=')
4068 {
4069 p += 2;
4070 (void)skip_expr(&p);
4071 if (*p == '`')
4072 ++p;
4073 continue;
4074 }
4075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 /*
4077 * Quick check if this cannot be the start of a special string.
4078 * Also removes backslash before '%', '#' and '<'.
4079 */
4080 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4081 {
4082 ++p;
4083 continue;
4084 }
4085
4086 /*
4087 * Try to find a match at this position.
4088 */
4089 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4090 if (*errormsgp != NULL) /* error detected */
4091 return FAIL;
4092 if (repl == NULL) /* no match found */
4093 {
4094 p += srclen;
4095 continue;
4096 }
4097
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004098 /* Wildcards won't be expanded below, the replacement is taken
4099 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4100 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4101 {
4102 char_u *l = repl;
4103
4104 repl = expand_env_save(repl);
4105 vim_free(l);
4106 }
4107
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 /* Need to escape white space et al. with a backslash. Don't do this
4109 * for shell commands (may have to use quotes instead). Don't do this
4110 * for non-unix systems when there is a single argument (spaces don't
4111 * separate arguments then). */
4112 if (!eap->usefilter
4113 && eap->cmdidx != CMD_bang
4114 && eap->cmdidx != CMD_make
4115 && eap->cmdidx != CMD_grep
4116 && eap->cmdidx != CMD_grepadd
4117#ifndef UNIX
4118 && !(eap->argt & NOSPC)
4119#endif
4120 )
4121 {
4122 char_u *l;
4123#ifdef BACKSLASH_IN_FILENAME
4124 /* Don't escape a backslash here, because rem_backslash() doesn't
4125 * remove it later. */
4126 static char_u *nobslash = (char_u *)" \t\"|";
4127# define ESCAPE_CHARS nobslash
4128#else
4129# define ESCAPE_CHARS escape_chars
4130#endif
4131
4132 for (l = repl; *l; ++l)
4133 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4134 {
4135 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4136 if (l != NULL)
4137 {
4138 vim_free(repl);
4139 repl = l;
4140 }
4141 break;
4142 }
4143 }
4144
4145 /* For a shell command a '!' must be escaped. */
4146 if ((eap->usefilter || eap->cmdidx == CMD_bang)
4147 && vim_strchr(repl, '!') != NULL)
4148 {
4149 char_u *l;
4150
4151 l = vim_strsave_escaped(repl, (char_u *)"!");
4152 if (l != NULL)
4153 {
4154 vim_free(repl);
4155 repl = l;
4156 /* For a sh-like shell escape it another time. */
4157 if (strstr((char *)p_sh, "sh") != NULL)
4158 {
4159 l = vim_strsave_escaped(repl, (char_u *)"!");
4160 if (l != NULL)
4161 {
4162 vim_free(repl);
4163 repl = l;
4164 }
4165 }
4166 }
4167 }
4168
4169 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4170 vim_free(repl);
4171 if (p == NULL)
4172 return FAIL;
4173 }
4174
4175 /*
4176 * One file argument: Expand wildcards.
4177 * Don't do this with ":r !command" or ":w !command".
4178 */
4179 if ((eap->argt & NOSPC) && !eap->usefilter)
4180 {
4181 /*
4182 * May do this twice:
4183 * 1. Replace environment variables.
4184 * 2. Replace any other wildcards, remove backslashes.
4185 */
4186 for (n = 1; n <= 2; ++n)
4187 {
4188 if (n == 2)
4189 {
4190#ifdef UNIX
4191 /*
4192 * Only for Unix we check for more than one file name.
4193 * For other systems spaces are considered to be part
4194 * of the file name.
4195 * Only check here if there is no wildcard, otherwise
4196 * ExpandOne() will check for errors. This allows
4197 * ":e `ls ve*.c`" on Unix.
4198 */
4199 if (!has_wildcards)
4200 for (p = eap->arg; *p; ++p)
4201 {
4202 /* skip escaped characters */
4203 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4204 ++p;
4205 else if (vim_iswhite(*p))
4206 {
4207 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4208 return FAIL;
4209 }
4210 }
4211#endif
4212
4213 /*
4214 * Halve the number of backslashes (this is Vi compatible).
4215 * For Unix and OS/2, when wildcards are expanded, this is
4216 * done by ExpandOne() below.
4217 */
4218#if defined(UNIX) || defined(OS2)
4219 if (!has_wildcards)
4220#endif
4221 backslash_halve(eap->arg);
4222#ifdef MACOS_CLASSIC
4223 /*
4224 * translate unix-like path components
4225 */
4226 slash_n_colon_adjust(eap->arg);
4227#endif
4228 }
4229
4230 if (has_wildcards)
4231 {
4232 if (n == 1)
4233 {
4234 /*
4235 * First loop: May expand environment variables. This
4236 * can be done much faster with expand_env() than with
4237 * something else (e.g., calling a shell).
4238 * After expanding environment variables, check again
4239 * if there are still wildcards present.
4240 */
4241 if (vim_strchr(eap->arg, '$') != NULL
4242 || vim_strchr(eap->arg, '~') != NULL)
4243 {
4244 expand_env_esc(eap->arg, NameBuff, MAXPATHL, TRUE);
4245 has_wildcards = mch_has_wildcard(NameBuff);
4246 p = NameBuff;
4247 }
4248 else
4249 p = NULL;
4250 }
4251 else /* n == 2 */
4252 {
4253 expand_T xpc;
4254
4255 ExpandInit(&xpc);
4256 xpc.xp_context = EXPAND_FILES;
4257 p = ExpandOne(&xpc, eap->arg, NULL,
4258 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4259 WILD_EXPAND_FREE);
4260 ExpandCleanup(&xpc);
4261 if (p == NULL)
4262 return FAIL;
4263 }
4264 if (p != NULL)
4265 {
4266 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4267 p, cmdlinep);
4268 if (n == 2) /* p came from ExpandOne() */
4269 vim_free(p);
4270 }
4271 }
4272 }
4273 }
4274 return OK;
4275}
4276
4277/*
4278 * Replace part of the command line, keeping eap->cmd, eap->arg and
4279 * eap->nextcmd correct.
4280 * "src" points to the part that is to be replaced, of length "srclen".
4281 * "repl" is the replacement string.
4282 * Returns a pointer to the character after the replaced string.
4283 * Returns NULL for failure.
4284 */
4285 static char_u *
4286repl_cmdline(eap, src, srclen, repl, cmdlinep)
4287 exarg_T *eap;
4288 char_u *src;
4289 int srclen;
4290 char_u *repl;
4291 char_u **cmdlinep;
4292{
4293 int len;
4294 int i;
4295 char_u *new_cmdline;
4296
4297 /*
4298 * The new command line is build in new_cmdline[].
4299 * First allocate it.
4300 * Careful: a "+cmd" argument may have been NUL terminated.
4301 */
4302 len = (int)STRLEN(repl);
4303 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
4304 if (eap->nextcmd)
4305 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4306 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4307 return NULL; /* out of memory! */
4308
4309 /*
4310 * Copy the stuff before the expanded part.
4311 * Copy the expanded stuff.
4312 * Copy what came after the expanded part.
4313 * Copy the next commands, if there are any.
4314 */
4315 i = (int)(src - *cmdlinep); /* length of part before match */
4316 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
4317 mch_memmove(new_cmdline + i, repl, (size_t)len);
4318 i += len; /* remember the end of the string */
4319 STRCPY(new_cmdline + i, src + srclen);
4320 src = new_cmdline + i; /* remember where to continue */
4321
4322 if (eap->nextcmd) /* append next command */
4323 {
4324 i = (int)STRLEN(new_cmdline) + 1;
4325 STRCPY(new_cmdline + i, eap->nextcmd);
4326 eap->nextcmd = new_cmdline + i;
4327 }
4328 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4329 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4330 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4331 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4332 vim_free(*cmdlinep);
4333 *cmdlinep = new_cmdline;
4334
4335 return src;
4336}
4337
4338/*
4339 * Check for '|' to separate commands and '"' to start comments.
4340 */
4341 void
4342separate_nextcmd(eap)
4343 exarg_T *eap;
4344{
4345 char_u *p;
4346
Bram Moolenaar86b68352004-12-27 21:59:20 +00004347#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004348 p = skip_grep_pat(eap);
4349#else
4350 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004351#endif
4352
4353 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 {
4355 if (*p == Ctrl_V)
4356 {
4357 if (eap->argt & (USECTRLV | XFILE))
4358 ++p; /* skip CTRL-V and next char */
4359 else
4360 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4361 if (*p == NUL) /* stop at NUL after CTRL-V */
4362 break;
4363 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004364
4365#ifdef FEAT_EVAL
4366 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004367 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004368 {
4369 p += 2;
4370 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004371 }
4372#endif
4373
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 /* Check for '"': start of comment or '|': next command */
4375 /* :@" and :*" do not start a comment!
4376 * :redir @" doesn't either. */
4377 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4378 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4379 || p != eap->arg)
4380 && (eap->cmdidx != CMD_redir
4381 || p != eap->arg + 1 || p[-1] != '@'))
4382 || *p == '|' || *p == '\n')
4383 {
4384 /*
4385 * We remove the '\' before the '|', unless USECTRLV is used
4386 * AND 'b' is present in 'cpoptions'.
4387 */
4388 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4389 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4390 {
4391 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4392 --p;
4393 }
4394 else
4395 {
4396 eap->nextcmd = check_nextcmd(p);
4397 *p = NUL;
4398 break;
4399 }
4400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004402
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4404 del_trailing_spaces(eap->arg);
4405}
4406
4407/*
4408 * get + command from ex argument
4409 */
4410 static char_u *
4411getargcmd(argp)
4412 char_u **argp;
4413{
4414 char_u *arg = *argp;
4415 char_u *command = NULL;
4416
4417 if (*arg == '+') /* +[command] */
4418 {
4419 ++arg;
4420 if (vim_isspace(*arg))
4421 command = dollar_command;
4422 else
4423 {
4424 command = arg;
4425 arg = skip_cmd_arg(command, TRUE);
4426 if (*arg != NUL)
4427 *arg++ = NUL; /* terminate command with NUL */
4428 }
4429
4430 arg = skipwhite(arg); /* skip over spaces */
4431 *argp = arg;
4432 }
4433 return command;
4434}
4435
4436/*
4437 * Find end of "+command" argument. Skip over "\ " and "\\".
4438 */
4439 static char_u *
4440skip_cmd_arg(p, rembs)
4441 char_u *p;
4442 int rembs; /* TRUE to halve the number of backslashes */
4443{
4444 while (*p && !vim_isspace(*p))
4445 {
4446 if (*p == '\\' && p[1] != NUL)
4447 {
4448 if (rembs)
4449 mch_memmove(p, p + 1, STRLEN(p));
4450 else
4451 ++p;
4452 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004453 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
4455 return p;
4456}
4457
4458/*
4459 * Get "++opt=arg" argument.
4460 * Return FAIL or OK.
4461 */
4462 static int
4463getargopt(eap)
4464 exarg_T *eap;
4465{
4466 char_u *arg = eap->arg + 2;
4467 int *pp = NULL;
4468#ifdef FEAT_MBYTE
4469 char_u *p;
4470#endif
4471
4472 /* ":edit ++[no]bin[ary] file" */
4473 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4474 {
4475 if (*arg == 'n')
4476 {
4477 arg += 2;
4478 eap->force_bin = FORCE_NOBIN;
4479 }
4480 else
4481 eap->force_bin = FORCE_BIN;
4482 if (!checkforcmd(&arg, "binary", 3))
4483 return FAIL;
4484 eap->arg = skipwhite(arg);
4485 return OK;
4486 }
4487
4488 if (STRNCMP(arg, "ff", 2) == 0)
4489 {
4490 arg += 2;
4491 pp = &eap->force_ff;
4492 }
4493 else if (STRNCMP(arg, "fileformat", 10) == 0)
4494 {
4495 arg += 10;
4496 pp = &eap->force_ff;
4497 }
4498#ifdef FEAT_MBYTE
4499 else if (STRNCMP(arg, "enc", 3) == 0)
4500 {
4501 arg += 3;
4502 pp = &eap->force_enc;
4503 }
4504 else if (STRNCMP(arg, "encoding", 8) == 0)
4505 {
4506 arg += 8;
4507 pp = &eap->force_enc;
4508 }
4509#endif
4510
4511 if (pp == NULL || *arg != '=')
4512 return FAIL;
4513
4514 ++arg;
4515 *pp = (int)(arg - eap->cmd);
4516 arg = skip_cmd_arg(arg, FALSE);
4517 eap->arg = skipwhite(arg);
4518 *arg = NUL;
4519
4520#ifdef FEAT_MBYTE
4521 if (pp == &eap->force_ff)
4522 {
4523#endif
4524 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4525 return FAIL;
4526#ifdef FEAT_MBYTE
4527 }
4528 else
4529 {
4530 /* Make 'fileencoding' lower case. */
4531 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4532 *p = TOLOWER_ASC(*p);
4533 }
4534#endif
4535
4536 return OK;
4537}
4538
4539/*
4540 * ":abbreviate" and friends.
4541 */
4542 static void
4543ex_abbreviate(eap)
4544 exarg_T *eap;
4545{
4546 do_exmap(eap, TRUE); /* almost the same as mapping */
4547}
4548
4549/*
4550 * ":map" and friends.
4551 */
4552 static void
4553ex_map(eap)
4554 exarg_T *eap;
4555{
4556 /*
4557 * If we are sourcing .exrc or .vimrc in current directory we
4558 * print the mappings for security reasons.
4559 */
4560 if (secure)
4561 {
4562 secure = 2;
4563 msg_outtrans(eap->cmd);
4564 msg_putchar('\n');
4565 }
4566 do_exmap(eap, FALSE);
4567}
4568
4569/*
4570 * ":unmap" and friends.
4571 */
4572 static void
4573ex_unmap(eap)
4574 exarg_T *eap;
4575{
4576 do_exmap(eap, FALSE);
4577}
4578
4579/*
4580 * ":mapclear" and friends.
4581 */
4582 static void
4583ex_mapclear(eap)
4584 exarg_T *eap;
4585{
4586 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4587}
4588
4589/*
4590 * ":abclear" and friends.
4591 */
4592 static void
4593ex_abclear(eap)
4594 exarg_T *eap;
4595{
4596 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4597}
4598
4599#ifdef FEAT_AUTOCMD
4600 static void
4601ex_autocmd(eap)
4602 exarg_T *eap;
4603{
4604 /*
4605 * Disallow auto commands from .exrc and .vimrc in current
4606 * directory for security reasons.
4607 */
4608 if (secure)
4609 {
4610 secure = 2;
4611 eap->errmsg = e_curdir;
4612 }
4613 else if (eap->cmdidx == CMD_autocmd)
4614 do_autocmd(eap->arg, eap->forceit);
4615 else
4616 do_augroup(eap->arg, eap->forceit);
4617}
4618
4619/*
4620 * ":doautocmd": Apply the automatic commands to the current buffer.
4621 */
4622 static void
4623ex_doautocmd(eap)
4624 exarg_T *eap;
4625{
4626 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004627 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628}
4629#endif
4630
4631#ifdef FEAT_LISTCMDS
4632/*
4633 * :[N]bunload[!] [N] [bufname] unload buffer
4634 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4635 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4636 */
4637 static void
4638ex_bunload(eap)
4639 exarg_T *eap;
4640{
4641 eap->errmsg = do_bufdel(
4642 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4643 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4644 : DOBUF_UNLOAD, eap->arg,
4645 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4646}
4647
4648/*
4649 * :[N]buffer [N] to buffer N
4650 * :[N]sbuffer [N] to buffer N
4651 */
4652 static void
4653ex_buffer(eap)
4654 exarg_T *eap;
4655{
4656 if (*eap->arg)
4657 eap->errmsg = e_trailing;
4658 else
4659 {
4660 if (eap->addr_count == 0) /* default is current buffer */
4661 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4662 else
4663 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4664 }
4665}
4666
4667/*
4668 * :[N]bmodified [N] to next mod. buffer
4669 * :[N]sbmodified [N] to next mod. buffer
4670 */
4671 static void
4672ex_bmodified(eap)
4673 exarg_T *eap;
4674{
4675 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4676}
4677
4678/*
4679 * :[N]bnext [N] to next buffer
4680 * :[N]sbnext [N] split and to next buffer
4681 */
4682 static void
4683ex_bnext(eap)
4684 exarg_T *eap;
4685{
4686 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4687}
4688
4689/*
4690 * :[N]bNext [N] to previous buffer
4691 * :[N]bprevious [N] to previous buffer
4692 * :[N]sbNext [N] split and to previous buffer
4693 * :[N]sbprevious [N] split and to previous buffer
4694 */
4695 static void
4696ex_bprevious(eap)
4697 exarg_T *eap;
4698{
4699 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4700}
4701
4702/*
4703 * :brewind to first buffer
4704 * :bfirst to first buffer
4705 * :sbrewind split and to first buffer
4706 * :sbfirst split and to first buffer
4707 */
4708 static void
4709ex_brewind(eap)
4710 exarg_T *eap;
4711{
4712 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4713}
4714
4715/*
4716 * :blast to last buffer
4717 * :sblast split and to last buffer
4718 */
4719 static void
4720ex_blast(eap)
4721 exarg_T *eap;
4722{
4723 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4724}
4725#endif
4726
4727 int
4728ends_excmd(c)
4729 int c;
4730{
4731 return (c == NUL || c == '|' || c == '"' || c == '\n');
4732}
4733
4734#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4735 || defined(PROTO)
4736/*
4737 * Return the next command, after the first '|' or '\n'.
4738 * Return NULL if not found.
4739 */
4740 char_u *
4741find_nextcmd(p)
4742 char_u *p;
4743{
4744 while (*p != '|' && *p != '\n')
4745 {
4746 if (*p == NUL)
4747 return NULL;
4748 ++p;
4749 }
4750 return (p + 1);
4751}
4752#endif
4753
4754/*
4755 * Check if *p is a separator between Ex commands.
4756 * Return NULL if it isn't, (p + 1) if it is.
4757 */
4758 char_u *
4759check_nextcmd(p)
4760 char_u *p;
4761{
4762 p = skipwhite(p);
4763 if (*p == '|' || *p == '\n')
4764 return (p + 1);
4765 else
4766 return NULL;
4767}
4768
4769/*
4770 * - if there are more files to edit
4771 * - and this is the last window
4772 * - and forceit not used
4773 * - and not repeated twice on a row
4774 * return FAIL and give error message if 'message' TRUE
4775 * return OK otherwise
4776 */
4777 static int
4778check_more(message, forceit)
4779 int message; /* when FALSE check only, no messages */
4780 int forceit;
4781{
4782 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4783
4784 if (!forceit && only_one_window() && ARGCOUNT > 1 && !arg_had_last
4785 && n >= 0 && quitmore == 0)
4786 {
4787 if (message)
4788 {
4789#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4790 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4791 {
4792 char_u buff[IOSIZE];
4793
4794 if (n == 1)
4795 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4796 else
4797 sprintf((char *)buff,
4798 _("%d more files to edit. Quit anyway?"), n);
4799 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4800 return OK;
4801 return FAIL;
4802 }
4803#endif
4804 if (n == 1)
4805 EMSG(_("E173: 1 more file to edit"));
4806 else
4807 EMSGN(_("E173: %ld more files to edit"), n);
4808 quitmore = 2; /* next try to quit is allowed */
4809 }
4810 return FAIL;
4811 }
4812 return OK;
4813}
4814
4815#ifdef FEAT_CMDL_COMPL
4816/*
4817 * Function given to ExpandGeneric() to obtain the list of command names.
4818 */
4819/*ARGSUSED*/
4820 char_u *
4821get_command_name(xp, idx)
4822 expand_T *xp;
4823 int idx;
4824{
4825 if (idx >= (int)CMD_SIZE)
4826# ifdef FEAT_USR_CMDS
4827 return get_user_command_name(idx);
4828# else
4829 return NULL;
4830# endif
4831 return cmdnames[idx].cmd_name;
4832}
4833#endif
4834
4835#if defined(FEAT_USR_CMDS) || defined(PROTO)
4836static 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));
4837static void uc_list __ARGS((char_u *name, size_t name_len));
4838static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4839static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4840static 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));
4841
4842 static int
4843uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4844 char_u *name;
4845 size_t name_len;
4846 char_u *rep;
4847 long argt;
4848 long def;
4849 int flags;
4850 int compl;
4851 char_u *compl_arg;
4852 int force;
4853{
4854 ucmd_T *cmd = NULL;
4855 char_u *p;
4856 int i;
4857 int cmp = 1;
4858 char_u *rep_buf = NULL;
4859 garray_T *gap;
4860
4861 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4862 if (rep_buf == NULL)
4863 {
4864 /* Can't replace termcodes - try using the string as is */
4865 rep_buf = vim_strsave(rep);
4866
4867 /* Give up if out of memory */
4868 if (rep_buf == NULL)
4869 return FAIL;
4870 }
4871
4872 /* get address of growarray: global or in curbuf */
4873 if (flags & UC_BUFFER)
4874 {
4875 gap = &curbuf->b_ucmds;
4876 if (gap->ga_itemsize == 0)
4877 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4878 }
4879 else
4880 gap = &ucmds;
4881
4882 /* Search for the command in the already defined commands. */
4883 for (i = 0; i < gap->ga_len; ++i)
4884 {
4885 size_t len;
4886
4887 cmd = USER_CMD_GA(gap, i);
4888 len = STRLEN(cmd->uc_name);
4889 cmp = STRNCMP(name, cmd->uc_name, name_len);
4890 if (cmp == 0)
4891 {
4892 if (name_len < len)
4893 cmp = -1;
4894 else if (name_len > len)
4895 cmp = 1;
4896 }
4897
4898 if (cmp == 0)
4899 {
4900 if (!force)
4901 {
4902 EMSG(_("E174: Command already exists: add ! to replace it"));
4903 goto fail;
4904 }
4905
4906 vim_free(cmd->uc_rep);
4907 cmd->uc_rep = 0;
4908 break;
4909 }
4910
4911 /* Stop as soon as we pass the name to add */
4912 if (cmp < 0)
4913 break;
4914 }
4915
4916 /* Extend the array unless we're replacing an existing command */
4917 if (cmp != 0)
4918 {
4919 if (ga_grow(gap, 1) != OK)
4920 goto fail;
4921 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
4922 goto fail;
4923
4924 cmd = USER_CMD_GA(gap, i);
4925 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
4926
4927 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928
4929 cmd->uc_name = p;
4930 }
4931
4932 cmd->uc_rep = rep_buf;
4933 cmd->uc_argt = argt;
4934 cmd->uc_def = def;
4935 cmd->uc_compl = compl;
4936#ifdef FEAT_EVAL
4937 cmd->uc_scriptID = current_SID;
4938# ifdef FEAT_CMDL_COMPL
4939 cmd->uc_compl_arg = compl_arg;
4940# endif
4941#endif
4942
4943 return OK;
4944
4945fail:
4946 vim_free(rep_buf);
4947#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4948 vim_free(compl_arg);
4949#endif
4950 return FAIL;
4951}
4952
4953/*
4954 * List of names for completion for ":command" with the EXPAND_ flag.
4955 * Must be alphabetical for completion.
4956 */
4957static struct
4958{
4959 int expand;
4960 char *name;
4961} command_complete[] =
4962{
4963 {EXPAND_AUGROUP, "augroup"},
4964 {EXPAND_BUFFERS, "buffer"},
4965 {EXPAND_COMMANDS, "command"},
4966#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4967 {EXPAND_USER_DEFINED, "custom"},
4968#endif
4969 {EXPAND_DIRECTORIES, "dir"},
4970 {EXPAND_ENV_VARS, "environment"},
4971 {EXPAND_EVENTS, "event"},
4972 {EXPAND_EXPRESSION, "expression"},
4973 {EXPAND_FILES, "file"},
4974 {EXPAND_FUNCTIONS, "function"},
4975 {EXPAND_HELP, "help"},
4976 {EXPAND_HIGHLIGHT, "highlight"},
4977 {EXPAND_MAPPINGS, "mapping"},
4978 {EXPAND_MENUS, "menu"},
4979 {EXPAND_SETTINGS, "option"},
4980 {EXPAND_TAGS, "tag"},
4981 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
4982 {EXPAND_USER_VARS, "var"},
4983 {0, NULL}
4984};
4985
4986 static void
4987uc_list(name, name_len)
4988 char_u *name;
4989 size_t name_len;
4990{
4991 int i, j;
4992 int found = FALSE;
4993 ucmd_T *cmd;
4994 int len;
4995 long a;
4996 garray_T *gap;
4997
4998 gap = &curbuf->b_ucmds;
4999 for (;;)
5000 {
5001 for (i = 0; i < gap->ga_len; ++i)
5002 {
5003 cmd = USER_CMD_GA(gap, i);
5004 a = cmd->uc_argt;
5005
5006 /* Skip commands which don't match the requested prefix */
5007 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5008 continue;
5009
5010 /* Put out the title first time */
5011 if (!found)
5012 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5013 found = TRUE;
5014 msg_putchar('\n');
5015 if (got_int)
5016 break;
5017
5018 /* Special cases */
5019 msg_putchar(a & BANG ? '!' : ' ');
5020 msg_putchar(a & REGSTR ? '"' : ' ');
5021 msg_putchar(gap != &ucmds ? 'b' : ' ');
5022 msg_putchar(' ');
5023
5024 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5025 len = (int)STRLEN(cmd->uc_name) + 4;
5026
5027 do {
5028 msg_putchar(' ');
5029 ++len;
5030 } while (len < 16);
5031
5032 len = 0;
5033
5034 /* Arguments */
5035 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5036 {
5037 case 0: IObuff[len++] = '0'; break;
5038 case (EXTRA): IObuff[len++] = '*'; break;
5039 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5040 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5041 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5042 }
5043
5044 do {
5045 IObuff[len++] = ' ';
5046 } while (len < 5);
5047
5048 /* Range */
5049 if (a & (RANGE|COUNT))
5050 {
5051 if (a & COUNT)
5052 {
5053 /* -count=N */
5054 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5055 len += (int)STRLEN(IObuff + len);
5056 }
5057 else if (a & DFLALL)
5058 IObuff[len++] = '%';
5059 else if (cmd->uc_def >= 0)
5060 {
5061 /* -range=N */
5062 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5063 len += (int)STRLEN(IObuff + len);
5064 }
5065 else
5066 IObuff[len++] = '.';
5067 }
5068
5069 do {
5070 IObuff[len++] = ' ';
5071 } while (len < 11);
5072
5073 /* Completion */
5074 for (j = 0; command_complete[j].expand != 0; ++j)
5075 if (command_complete[j].expand == cmd->uc_compl)
5076 {
5077 STRCPY(IObuff + len, command_complete[j].name);
5078 len += (int)STRLEN(IObuff + len);
5079 break;
5080 }
5081
5082 do {
5083 IObuff[len++] = ' ';
5084 } while (len < 21);
5085
5086 IObuff[len] = '\0';
5087 msg_outtrans(IObuff);
5088
5089 msg_outtrans_special(cmd->uc_rep, FALSE);
5090 out_flush();
5091 ui_breakcheck();
5092 if (got_int)
5093 break;
5094 }
5095 if (gap == &ucmds || i < gap->ga_len)
5096 break;
5097 gap = &ucmds;
5098 }
5099
5100 if (!found)
5101 MSG(_("No user-defined commands found"));
5102}
5103
5104 static char_u *
5105uc_fun_cmd()
5106{
5107 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5108 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5109 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5110 0xb9, 0x7f, 0};
5111 int i;
5112
5113 for (i = 0; fcmd[i]; ++i)
5114 IObuff[i] = fcmd[i] - 0x40;
5115 IObuff[i] = 0;
5116 return IObuff;
5117}
5118
5119 static int
5120uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5121 char_u *attr;
5122 size_t len;
5123 long *argt;
5124 long *def;
5125 int *flags;
5126 int *compl;
5127 char_u **compl_arg;
5128{
5129 char_u *p;
5130
5131 if (len == 0)
5132 {
5133 EMSG(_("E175: No attribute specified"));
5134 return FAIL;
5135 }
5136
5137 /* First, try the simple attributes (no arguments) */
5138 if (STRNICMP(attr, "bang", len) == 0)
5139 *argt |= BANG;
5140 else if (STRNICMP(attr, "buffer", len) == 0)
5141 *flags |= UC_BUFFER;
5142 else if (STRNICMP(attr, "register", len) == 0)
5143 *argt |= REGSTR;
5144 else if (STRNICMP(attr, "bar", len) == 0)
5145 *argt |= TRLBAR;
5146 else
5147 {
5148 int i;
5149 char_u *val = NULL;
5150 size_t vallen = 0;
5151 size_t attrlen = len;
5152
5153 /* Look for the attribute name - which is the part before any '=' */
5154 for (i = 0; i < (int)len; ++i)
5155 {
5156 if (attr[i] == '=')
5157 {
5158 val = &attr[i + 1];
5159 vallen = len - i - 1;
5160 attrlen = i;
5161 break;
5162 }
5163 }
5164
5165 if (STRNICMP(attr, "nargs", attrlen) == 0)
5166 {
5167 if (vallen == 1)
5168 {
5169 if (*val == '0')
5170 /* Do nothing - this is the default */;
5171 else if (*val == '1')
5172 *argt |= (EXTRA | NOSPC | NEEDARG);
5173 else if (*val == '*')
5174 *argt |= EXTRA;
5175 else if (*val == '?')
5176 *argt |= (EXTRA | NOSPC);
5177 else if (*val == '+')
5178 *argt |= (EXTRA | NEEDARG);
5179 else
5180 goto wrong_nargs;
5181 }
5182 else
5183 {
5184wrong_nargs:
5185 EMSG(_("E176: Invalid number of arguments"));
5186 return FAIL;
5187 }
5188 }
5189 else if (STRNICMP(attr, "range", attrlen) == 0)
5190 {
5191 *argt |= RANGE;
5192 if (vallen == 1 && *val == '%')
5193 *argt |= DFLALL;
5194 else if (val != NULL)
5195 {
5196 p = val;
5197 if (*def >= 0)
5198 {
5199two_count:
5200 EMSG(_("E177: Count cannot be specified twice"));
5201 return FAIL;
5202 }
5203
5204 *def = getdigits(&p);
5205 *argt |= (ZEROR | NOTADR);
5206
5207 if (p != val + vallen || vallen == 0)
5208 {
5209invalid_count:
5210 EMSG(_("E178: Invalid default value for count"));
5211 return FAIL;
5212 }
5213 }
5214 }
5215 else if (STRNICMP(attr, "count", attrlen) == 0)
5216 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005217 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218
5219 if (val != NULL)
5220 {
5221 p = val;
5222 if (*def >= 0)
5223 goto two_count;
5224
5225 *def = getdigits(&p);
5226
5227 if (p != val + vallen)
5228 goto invalid_count;
5229 }
5230
5231 if (*def < 0)
5232 *def = 0;
5233 }
5234 else if (STRNICMP(attr, "complete", attrlen) == 0)
5235 {
5236 char_u *arg = NULL;
5237 size_t arglen = 0;
5238
5239 if (val == NULL)
5240 {
5241 EMSG(_("E179: argument required for complete"));
5242 return FAIL;
5243 }
5244 /* Look for any argument part - which is the part after any ',' */
5245 for (i = 0; i < (int)vallen; ++i)
5246 {
5247 if (val[i] == ',')
5248 {
5249 arg = &val[i + 1];
5250 arglen = vallen - i - 1;
5251 vallen = i;
5252 break;
5253 }
5254 }
5255
5256 for (i = 0; command_complete[i].expand != 0; ++i)
5257 {
5258 if (STRLEN(command_complete[i].name) == vallen
5259 && STRNCMP(val, command_complete[i].name, vallen) == 0)
5260 {
5261 *compl = command_complete[i].expand;
5262 if (command_complete[i].expand == EXPAND_BUFFERS)
5263 *argt |= BUFNAME;
5264 else if (command_complete[i].expand == EXPAND_DIRECTORIES
5265 || command_complete[i].expand == EXPAND_FILES)
5266 *argt |= XFILE;
5267 break;
5268 }
5269 }
5270
5271 if (command_complete[i].expand == 0)
5272 {
5273 EMSG2(_("E180: Invalid complete value: %s"), val);
5274 return FAIL;
5275 }
5276#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5277 if (*compl != EXPAND_USER_DEFINED && arg != NULL)
5278#else
5279 if (arg != NULL)
5280#endif
5281 {
5282 EMSG(_("E468: Completion argument only allowed for custom completion"));
5283 return FAIL;
5284 }
5285#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5286 if (*compl == EXPAND_USER_DEFINED && arg == NULL)
5287 {
5288 EMSG(_("E467: Custom completion requires a function argument"));
5289 return FAIL;
5290 }
5291 if (arg != NULL)
5292 *compl_arg = vim_strnsave(arg, (int)arglen);
5293#endif
5294 }
5295 else
5296 {
5297 char_u ch = attr[len];
5298 attr[len] = '\0';
5299 EMSG2(_("E181: Invalid attribute: %s"), attr);
5300 attr[len] = ch;
5301 return FAIL;
5302 }
5303 }
5304
5305 return OK;
5306}
5307
5308 static void
5309ex_command(eap)
5310 exarg_T *eap;
5311{
5312 char_u *name;
5313 char_u *end;
5314 char_u *p;
5315 long argt = 0;
5316 long def = -1;
5317 int flags = 0;
5318 int compl = EXPAND_NOTHING;
5319 char_u *compl_arg = NULL;
5320 int has_attr = (eap->arg[0] == '-');
5321
5322 p = eap->arg;
5323
5324 /* Check for attributes */
5325 while (*p == '-')
5326 {
5327 ++p;
5328 end = skiptowhite(p);
5329 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5330 == FAIL)
5331 return;
5332 p = skipwhite(end);
5333 }
5334
5335 /* Get the name (if any) and skip to the following argument */
5336 name = p;
5337 if (ASCII_ISALPHA(*p))
5338 while (ASCII_ISALNUM(*p))
5339 ++p;
5340 if (!ends_excmd(*p) && !vim_iswhite(*p))
5341 {
5342 EMSG(_("E182: Invalid command name"));
5343 return;
5344 }
5345 end = p;
5346
5347 /* If there is nothing after the name, and no attributes were specified,
5348 * we are listing commands
5349 */
5350 p = skipwhite(end);
5351 if (!has_attr && ends_excmd(*p))
5352 {
5353 uc_list(name, end - name);
5354 }
5355 else if (!ASCII_ISUPPER(*name))
5356 {
5357 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5358 return;
5359 }
5360 else
5361 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5362 eap->forceit);
5363}
5364
5365/*
5366 * ":comclear"
5367 */
5368/*ARGSUSED*/
5369/*
5370 * Clear all user commands, global and for current buffer.
5371 */
5372 static void
5373ex_comclear(eap)
5374 exarg_T *eap;
5375{
5376 uc_clear(&ucmds);
5377 uc_clear(&curbuf->b_ucmds);
5378}
5379
5380/*
5381 * Clear all user commands for "gap".
5382 */
5383 void
5384uc_clear(gap)
5385 garray_T *gap;
5386{
5387 int i;
5388 ucmd_T *cmd;
5389
5390 for (i = 0; i < gap->ga_len; ++i)
5391 {
5392 cmd = USER_CMD_GA(gap, i);
5393 vim_free(cmd->uc_name);
5394 vim_free(cmd->uc_rep);
5395# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5396 vim_free(cmd->uc_compl_arg);
5397# endif
5398 }
5399 ga_clear(gap);
5400}
5401
5402 static void
5403ex_delcommand(eap)
5404 exarg_T *eap;
5405{
5406 int i = 0;
5407 ucmd_T *cmd = NULL;
5408 int cmp = -1;
5409 garray_T *gap;
5410
5411 gap = &curbuf->b_ucmds;
5412 for (;;)
5413 {
5414 for (i = 0; i < gap->ga_len; ++i)
5415 {
5416 cmd = USER_CMD_GA(gap, i);
5417 cmp = STRCMP(eap->arg, cmd->uc_name);
5418 if (cmp <= 0)
5419 break;
5420 }
5421 if (gap == &ucmds || cmp == 0)
5422 break;
5423 gap = &ucmds;
5424 }
5425
5426 if (cmp != 0)
5427 {
5428 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5429 return;
5430 }
5431
5432 vim_free(cmd->uc_name);
5433 vim_free(cmd->uc_rep);
5434# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5435 vim_free(cmd->uc_compl_arg);
5436# endif
5437
5438 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439
5440 if (i < gap->ga_len)
5441 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5442}
5443
5444 static char_u *
5445uc_split_args(arg, lenp)
5446 char_u *arg;
5447 size_t *lenp;
5448{
5449 char_u *buf;
5450 char_u *p;
5451 char_u *q;
5452 int len;
5453
5454 /* Precalculate length */
5455 p = arg;
5456 len = 2; /* Initial and final quotes */
5457
5458 while (*p)
5459 {
5460 if (p[0] == '\\' && vim_iswhite(p[1]))
5461 {
5462 len += 1;
5463 p += 2;
5464 }
5465 else if (*p == '\\' || *p == '"')
5466 {
5467 len += 2;
5468 p += 1;
5469 }
5470 else if (vim_iswhite(*p))
5471 {
5472 p = skipwhite(p);
5473 if (*p == NUL)
5474 break;
5475 len += 3; /* "," */
5476 }
5477 else
5478 {
5479 ++len;
5480 ++p;
5481 }
5482 }
5483
5484 buf = alloc(len + 1);
5485 if (buf == NULL)
5486 {
5487 *lenp = 0;
5488 return buf;
5489 }
5490
5491 p = arg;
5492 q = buf;
5493 *q++ = '"';
5494 while (*p)
5495 {
5496 if (p[0] == '\\' && vim_iswhite(p[1]))
5497 {
5498 *q++ = p[1];
5499 p += 2;
5500 }
5501 else if (*p == '\\' || *p == '"')
5502 {
5503 *q++ = '\\';
5504 *q++ = *p++;
5505 }
5506 else if (vim_iswhite(*p))
5507 {
5508 p = skipwhite(p);
5509 if (*p == NUL)
5510 break;
5511 *q++ = '"';
5512 *q++ = ',';
5513 *q++ = '"';
5514 }
5515 else
5516 {
5517 *q++ = *p++;
5518 }
5519 }
5520 *q++ = '"';
5521 *q = 0;
5522
5523 *lenp = len;
5524 return buf;
5525}
5526
5527/*
5528 * Check for a <> code in a user command.
5529 * "code" points to the '<'. "len" the length of the <> (inclusive).
5530 * "buf" is where the result is to be added.
5531 * "split_buf" points to a buffer used for splitting, caller should free it.
5532 * "split_len" is the length of what "split_buf" contains.
5533 * Returns the length of the replacement, which has been added to "buf".
5534 * Returns -1 if there was no match, and only the "<" has been copied.
5535 */
5536 static size_t
5537uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5538 char_u *code;
5539 size_t len;
5540 char_u *buf;
5541 ucmd_T *cmd; /* the user command we're expanding */
5542 exarg_T *eap; /* ex arguments */
5543 char_u **split_buf;
5544 size_t *split_len;
5545{
5546 size_t result = 0;
5547 char_u *p = code + 1;
5548 size_t l = len - 2;
5549 int quote = 0;
5550 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5551 ct_LT, ct_NONE } type = ct_NONE;
5552
5553 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5554 {
5555 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5556 p += 2;
5557 l -= 2;
5558 }
5559
5560 if (l < 1)
5561 type = ct_NONE;
5562 else if (STRNICMP(p, "args", l) == 0)
5563 type = ct_ARGS;
5564 else if (STRNICMP(p, "bang", l) == 0)
5565 type = ct_BANG;
5566 else if (STRNICMP(p, "count", l) == 0)
5567 type = ct_COUNT;
5568 else if (STRNICMP(p, "line1", l) == 0)
5569 type = ct_LINE1;
5570 else if (STRNICMP(p, "line2", l) == 0)
5571 type = ct_LINE2;
5572 else if (STRNICMP(p, "lt", l) == 0)
5573 type = ct_LT;
5574 else if (STRNICMP(p, "register", l) == 0)
5575 type = ct_REGISTER;
5576
5577 switch (type)
5578 {
5579 case ct_ARGS:
5580 /* Simple case first */
5581 if (*eap->arg == NUL)
5582 {
5583 if (quote == 1)
5584 {
5585 result = 2;
5586 if (buf != NULL)
5587 STRCPY(buf, "''");
5588 }
5589 else
5590 result = 0;
5591 break;
5592 }
5593
5594 /* When specified there is a single argument don't split it.
5595 * Works for ":Cmd %" when % is "a b c". */
5596 if ((eap->argt & NOSPC) && quote == 2)
5597 quote = 1;
5598
5599 switch (quote)
5600 {
5601 case 0: /* No quoting, no splitting */
5602 result = STRLEN(eap->arg);
5603 if (buf != NULL)
5604 STRCPY(buf, eap->arg);
5605 break;
5606 case 1: /* Quote, but don't split */
5607 result = STRLEN(eap->arg) + 2;
5608 for (p = eap->arg; *p; ++p)
5609 {
5610 if (*p == '\\' || *p == '"')
5611 ++result;
5612 }
5613
5614 if (buf != NULL)
5615 {
5616 *buf++ = '"';
5617 for (p = eap->arg; *p; ++p)
5618 {
5619 if (*p == '\\' || *p == '"')
5620 *buf++ = '\\';
5621 *buf++ = *p;
5622 }
5623 *buf = '"';
5624 }
5625
5626 break;
5627 case 2: /* Quote and split */
5628 /* This is hard, so only do it once, and cache the result */
5629 if (*split_buf == NULL)
5630 *split_buf = uc_split_args(eap->arg, split_len);
5631
5632 result = *split_len;
5633 if (buf != NULL && result != 0)
5634 STRCPY(buf, *split_buf);
5635
5636 break;
5637 }
5638 break;
5639
5640 case ct_BANG:
5641 result = eap->forceit ? 1 : 0;
5642 if (quote)
5643 result += 2;
5644 if (buf != NULL)
5645 {
5646 if (quote)
5647 *buf++ = '"';
5648 if (eap->forceit)
5649 *buf++ = '!';
5650 if (quote)
5651 *buf = '"';
5652 }
5653 break;
5654
5655 case ct_LINE1:
5656 case ct_LINE2:
5657 case ct_COUNT:
5658 {
5659 char num_buf[20];
5660 long num = (type == ct_LINE1) ? eap->line1 :
5661 (type == ct_LINE2) ? eap->line2 :
5662 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5663 size_t num_len;
5664
5665 sprintf(num_buf, "%ld", num);
5666 num_len = STRLEN(num_buf);
5667 result = num_len;
5668
5669 if (quote)
5670 result += 2;
5671
5672 if (buf != NULL)
5673 {
5674 if (quote)
5675 *buf++ = '"';
5676 STRCPY(buf, num_buf);
5677 buf += num_len;
5678 if (quote)
5679 *buf = '"';
5680 }
5681
5682 break;
5683 }
5684
5685 case ct_REGISTER:
5686 result = eap->regname ? 1 : 0;
5687 if (quote)
5688 result += 2;
5689 if (buf != NULL)
5690 {
5691 if (quote)
5692 *buf++ = '\'';
5693 if (eap->regname)
5694 *buf++ = eap->regname;
5695 if (quote)
5696 *buf = '\'';
5697 }
5698 break;
5699
5700 case ct_LT:
5701 result = 1;
5702 if (buf != NULL)
5703 *buf = '<';
5704 break;
5705
5706 default:
5707 /* Not recognized: just copy the '<' and return -1. */
5708 result = (size_t)-1;
5709 if (buf != NULL)
5710 *buf = '<';
5711 break;
5712 }
5713
5714 return result;
5715}
5716
5717 static void
5718do_ucmd(eap)
5719 exarg_T *eap;
5720{
5721 char_u *buf;
5722 char_u *p;
5723 char_u *q;
5724
5725 char_u *start;
5726 char_u *end;
5727 size_t len, totlen;
5728
5729 size_t split_len = 0;
5730 char_u *split_buf = NULL;
5731 ucmd_T *cmd;
5732#ifdef FEAT_EVAL
5733 scid_T save_current_SID = current_SID;
5734#endif
5735
5736 if (eap->cmdidx == CMD_USER)
5737 cmd = USER_CMD(eap->useridx);
5738 else
5739 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5740
5741 /*
5742 * Replace <> in the command by the arguments.
5743 */
5744 buf = NULL;
5745 for (;;)
5746 {
5747 p = cmd->uc_rep;
5748 q = buf;
5749 totlen = 0;
5750 while ((start = vim_strchr(p, '<')) != NULL
5751 && (end = vim_strchr(start + 1, '>')) != NULL)
5752 {
5753 /* Include the '>' */
5754 ++end;
5755
5756 /* Take everything up to the '<' */
5757 len = start - p;
5758 if (buf == NULL)
5759 totlen += len;
5760 else
5761 {
5762 mch_memmove(q, p, len);
5763 q += len;
5764 }
5765
5766 len = uc_check_code(start, end - start, q, cmd, eap,
5767 &split_buf, &split_len);
5768 if (len == (size_t)-1)
5769 {
5770 /* no match, continue after '<' */
5771 p = start + 1;
5772 len = 1;
5773 }
5774 else
5775 p = end;
5776 if (buf == NULL)
5777 totlen += len;
5778 else
5779 q += len;
5780 }
5781 if (buf != NULL) /* second time here, finished */
5782 {
5783 STRCPY(q, p);
5784 break;
5785 }
5786
5787 totlen += STRLEN(p); /* Add on the trailing characters */
5788 buf = alloc((unsigned)(totlen + 1));
5789 if (buf == NULL)
5790 {
5791 vim_free(split_buf);
5792 return;
5793 }
5794 }
5795
5796#ifdef FEAT_EVAL
5797 current_SID = cmd->uc_scriptID;
5798#endif
5799 (void)do_cmdline(buf, eap->getline, eap->cookie,
5800 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5801#ifdef FEAT_EVAL
5802 current_SID = save_current_SID;
5803#endif
5804 vim_free(buf);
5805 vim_free(split_buf);
5806}
5807
5808# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5809 static char_u *
5810get_user_command_name(idx)
5811 int idx;
5812{
5813 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5814}
5815
5816/*
5817 * Function given to ExpandGeneric() to obtain the list of user command names.
5818 */
5819/*ARGSUSED*/
5820 char_u *
5821get_user_commands(xp, idx)
5822 expand_T *xp;
5823 int idx;
5824{
5825 if (idx < curbuf->b_ucmds.ga_len)
5826 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5827 idx -= curbuf->b_ucmds.ga_len;
5828 if (idx < ucmds.ga_len)
5829 return USER_CMD(idx)->uc_name;
5830 return NULL;
5831}
5832
5833/*
5834 * Function given to ExpandGeneric() to obtain the list of user command
5835 * attributes.
5836 */
5837/*ARGSUSED*/
5838 char_u *
5839get_user_cmd_flags(xp, idx)
5840 expand_T *xp;
5841 int idx;
5842{
5843 static char *user_cmd_flags[] =
5844 {"bang", "bar", "buffer", "complete", "count",
5845 "nargs", "range", "register"};
5846
5847 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5848 return NULL;
5849 return (char_u *)user_cmd_flags[idx];
5850}
5851
5852/*
5853 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5854 */
5855/*ARGSUSED*/
5856 char_u *
5857get_user_cmd_nargs(xp, idx)
5858 expand_T *xp;
5859 int idx;
5860{
5861 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5862
5863 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5864 return NULL;
5865 return (char_u *)user_cmd_nargs[idx];
5866}
5867
5868/*
5869 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5870 */
5871/*ARGSUSED*/
5872 char_u *
5873get_user_cmd_complete(xp, idx)
5874 expand_T *xp;
5875 int idx;
5876{
5877 return (char_u *)command_complete[idx].name;
5878}
5879# endif /* FEAT_CMDL_COMPL */
5880
5881#endif /* FEAT_USR_CMDS */
5882
5883 static void
5884ex_colorscheme(eap)
5885 exarg_T *eap;
5886{
5887 if (load_colors(eap->arg) == FAIL)
5888 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
5889}
5890
5891 static void
5892ex_highlight(eap)
5893 exarg_T *eap;
5894{
5895 if (*eap->arg == NUL && eap->cmd[2] == '!')
5896 MSG(_("Greetings, Vim user!"));
5897 do_highlight(eap->arg, eap->forceit, FALSE);
5898}
5899
5900
5901/*
5902 * Call this function if we thought we were going to exit, but we won't
5903 * (because of an error). May need to restore the terminal mode.
5904 */
5905 void
5906not_exiting()
5907{
5908 exiting = FALSE;
5909 settmode(TMODE_RAW);
5910}
5911
5912/*
5913 * ":quit": quit current window, quit Vim if closed the last window.
5914 */
5915 static void
5916ex_quit(eap)
5917 exarg_T *eap;
5918{
5919#ifdef FEAT_CMDWIN
5920 if (cmdwin_type != 0)
5921 {
5922 cmdwin_result = Ctrl_C;
5923 return;
5924 }
5925#endif
5926
5927#ifdef FEAT_NETBEANS_INTG
5928 netbeansForcedQuit = eap->forceit;
5929#endif
5930
5931 /*
5932 * If there are more files or windows we won't exit.
5933 */
5934 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5935 exiting = TRUE;
5936 if ((!P_HID(curbuf)
5937 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
5938 || check_more(TRUE, eap->forceit) == FAIL
5939 || (only_one_window() && check_changed_any(eap->forceit)))
5940 {
5941 not_exiting();
5942 }
5943 else
5944 {
5945#ifdef FEAT_WINDOWS
5946 if (only_one_window()) /* quit last window */
5947#endif
5948 getout(0);
5949#ifdef FEAT_WINDOWS
5950# ifdef FEAT_GUI
5951 need_mouse_correct = TRUE;
5952# endif
5953 /* close window; may free buffer */
5954 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
5955#endif
5956 }
5957}
5958
5959/*
5960 * ":cquit".
5961 */
5962/*ARGSUSED*/
5963 static void
5964ex_cquit(eap)
5965 exarg_T *eap;
5966{
5967 getout(1); /* this does not always pass on the exit code to the Manx
5968 compiler. why? */
5969}
5970
5971/*
5972 * ":qall": try to quit all windows
5973 */
5974 static void
5975ex_quit_all(eap)
5976 exarg_T *eap;
5977{
5978# ifdef FEAT_CMDWIN
5979 if (cmdwin_type != 0)
5980 {
5981 if (eap->forceit)
5982 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5983 else
5984 cmdwin_result = K_XF2;
5985 return;
5986 }
5987# endif
5988 exiting = TRUE;
5989 if (eap->forceit || !check_changed_any(FALSE))
5990 getout(0);
5991 not_exiting();
5992}
5993
5994#ifdef FEAT_WINDOWS
5995/*
5996 * ":close": close current window, unless it is the last one
5997 */
5998 static void
5999ex_close(eap)
6000 exarg_T *eap;
6001{
6002# ifdef FEAT_CMDWIN
6003 if (cmdwin_type != 0)
6004 cmdwin_result = K_IGNORE;
6005 else
6006# endif
6007 ex_win_close(eap, curwin);
6008}
6009
6010 static void
6011ex_win_close(eap, win)
6012 exarg_T *eap;
6013 win_T *win;
6014{
6015 int need_hide;
6016 buf_T *buf = win->w_buffer;
6017
6018 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
6019 if (need_hide && !P_HID(buf) && !eap->forceit)
6020 {
6021#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6022 if ((p_confirm || cmdmod.confirm) && p_write)
6023 {
6024 dialog_changed(buf, FALSE);
6025 if (buf_valid(buf) && bufIsChanged(buf))
6026 return;
6027 need_hide = FALSE;
6028 }
6029 else
6030#endif
6031 {
6032 EMSG(_(e_nowrtmsg));
6033 return;
6034 }
6035 }
6036
6037#ifdef FEAT_GUI
6038 need_mouse_correct = TRUE;
6039#endif
6040 /* free buffer when not hiding it or when it's a scratch buffer */
6041 win_close(win, !need_hide && !P_HID(buf));
6042}
6043
6044#ifdef FEAT_QUICKFIX
6045/*
6046 * ":pclose": Close any preview window.
6047 */
6048 static void
6049ex_pclose(eap)
6050 exarg_T *eap;
6051{
6052 win_T *win;
6053
6054 for (win = firstwin; win != NULL; win = win->w_next)
6055 if (win->w_p_pvw)
6056 {
6057 ex_win_close(eap, win);
6058 break;
6059 }
6060}
6061#endif
6062
6063/*
6064 * ":only".
6065 */
6066 static void
6067ex_only(eap)
6068 exarg_T *eap;
6069{
6070# ifdef FEAT_GUI
6071 need_mouse_correct = TRUE;
6072# endif
6073 close_others(TRUE, eap->forceit);
6074}
6075
6076/*
6077 * ":all" and ":sall".
6078 */
6079 static void
6080ex_all(eap)
6081 exarg_T *eap;
6082{
6083 if (eap->addr_count == 0)
6084 eap->line2 = 9999;
6085 do_arg_all((int)eap->line2, eap->forceit);
6086}
6087#endif /* FEAT_WINDOWS */
6088
6089 static void
6090ex_hide(eap)
6091 exarg_T *eap;
6092{
6093 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6094 eap->errmsg = e_invarg;
6095 else
6096 {
6097 /* ":hide" or ":hide | cmd": hide current window */
6098 eap->nextcmd = check_nextcmd(eap->arg);
6099#ifdef FEAT_WINDOWS
6100 if (!eap->skip)
6101 {
6102# ifdef FEAT_GUI
6103 need_mouse_correct = TRUE;
6104# endif
6105 win_close(curwin, FALSE); /* don't free buffer */
6106 }
6107#endif
6108 }
6109}
6110
6111/*
6112 * ":stop" and ":suspend": Suspend Vim.
6113 */
6114 static void
6115ex_stop(eap)
6116 exarg_T *eap;
6117{
6118 /*
6119 * Disallow suspending for "rvim".
6120 */
6121 if (!check_restricted()
6122#ifdef WIN3264
6123 /*
6124 * Check if external commands are allowed now.
6125 */
6126 && can_end_termcap_mode(TRUE)
6127#endif
6128 )
6129 {
6130 if (!eap->forceit)
6131 autowrite_all();
6132 windgoto((int)Rows - 1, 0);
6133 out_char('\n');
6134 out_flush();
6135 stoptermcap();
6136 out_flush(); /* needed for SUN to restore xterm buffer */
6137#ifdef FEAT_TITLE
6138 mch_restore_title(3); /* restore window titles */
6139#endif
6140 ui_suspend(); /* call machine specific function */
6141#ifdef FEAT_TITLE
6142 maketitle();
6143 resettitle(); /* force updating the title */
6144#endif
6145 starttermcap();
6146 scroll_start(); /* scroll screen before redrawing */
6147 redraw_later_clear();
6148 shell_resized(); /* may have resized window */
6149 }
6150}
6151
6152/*
6153 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6154 */
6155 static void
6156ex_exit(eap)
6157 exarg_T *eap;
6158{
6159#ifdef FEAT_CMDWIN
6160 if (cmdwin_type != 0)
6161 {
6162 cmdwin_result = Ctrl_C;
6163 return;
6164 }
6165#endif
6166
6167 /*
6168 * if more files or windows we won't exit
6169 */
6170 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6171 exiting = TRUE;
6172 if ( ((eap->cmdidx == CMD_wq
6173 || curbufIsChanged())
6174 && do_write(eap) == FAIL)
6175 || check_more(TRUE, eap->forceit) == FAIL
6176 || (only_one_window() && check_changed_any(eap->forceit)))
6177 {
6178 not_exiting();
6179 }
6180 else
6181 {
6182#ifdef FEAT_WINDOWS
6183 if (only_one_window()) /* quit last window, exit Vim */
6184#endif
6185 getout(0);
6186#ifdef FEAT_WINDOWS
6187# ifdef FEAT_GUI
6188 need_mouse_correct = TRUE;
6189# endif
6190 /* quit current window, may free buffer */
6191 win_close(curwin, !P_HID(curwin->w_buffer));
6192#endif
6193 }
6194}
6195
6196/*
6197 * ":print", ":list", ":number".
6198 */
6199 static void
6200ex_print(eap)
6201 exarg_T *eap;
6202{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006203 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6204 EMSG(_(e_emptybuf));
6205 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006207 for ( ;!got_int; ui_breakcheck())
6208 {
6209 print_line(eap->line1,
6210 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6211 || (eap->flags & EXFLAG_NR)),
6212 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6213 if (++eap->line1 > eap->line2)
6214 break;
6215 out_flush(); /* show one line at a time */
6216 }
6217 setpcmark();
6218 /* put cursor at last line */
6219 curwin->w_cursor.lnum = eap->line2;
6220 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 }
6222
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224}
6225
6226#ifdef FEAT_BYTEOFF
6227 static void
6228ex_goto(eap)
6229 exarg_T *eap;
6230{
6231 goto_byte(eap->line2);
6232}
6233#endif
6234
6235/*
6236 * ":shell".
6237 */
6238/*ARGSUSED*/
6239 static void
6240ex_shell(eap)
6241 exarg_T *eap;
6242{
6243 do_shell(NULL, 0);
6244}
6245
6246#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6247 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
6248 || defined(PROTO)
6249
6250/*
6251 * Handle a file drop. The code is here because a drop is *nearly* like an
6252 * :args command, but not quite (we have a list of exact filenames, so we
6253 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6254 * code is very similar to :args and hence needs access to a lot of the static
6255 * functions in this file.
6256 *
6257 * The list should be allocated using alloc(), as should each item in the
6258 * list. This function takes over responsibility for freeing the list.
6259 *
6260 * XXX The list is made into the arggument list. This is freed using
6261 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6262 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6263 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6264 * file functionality is (currently) not in EMX this is not presently a
6265 * problem.
6266 */
6267 void
6268handle_drop(filec, filev, split)
6269 int filec; /* the number of files dropped */
6270 char_u **filev; /* the list of files dropped */
6271 int split; /* force splitting the window */
6272{
6273 exarg_T ea;
6274 int save_msg_scroll = msg_scroll;
6275
6276# ifdef FEAT_CMDWIN
6277 if (cmdwin_type != 0)
6278 return;
6279# endif
6280
6281 /* Check whether the current buffer is changed. If so, we will need
6282 * to split the current window or data could be lost.
6283 * We don't need to check if the 'hidden' option is set, as in this
6284 * case the buffer won't be lost.
6285 */
6286 if (!P_HID(curbuf) && !split)
6287 {
6288 ++emsg_off;
6289 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6290 --emsg_off;
6291 }
6292 if (split)
6293 {
6294# ifdef FEAT_WINDOWS
6295 if (win_split(0, 0) == FAIL)
6296 return;
6297# ifdef FEAT_SCROLLBIND
6298 curwin->w_p_scb = FALSE;
6299# endif
6300
6301 /* When splitting the window, create a new alist. Otherwise the
6302 * existing one is overwritten. */
6303 alist_unlink(curwin->w_alist);
6304 alist_new();
6305# else
6306 return; /* can't split, always fail */
6307# endif
6308 }
6309
6310 /*
6311 * Set up the new argument list.
6312 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006313 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
6315 /*
6316 * Move to the first file.
6317 */
6318 /* Fake up a minimal "next" command for do_argfile() */
6319 vim_memset(&ea, 0, sizeof(ea));
6320 ea.cmd = (char_u *)"next";
6321 do_argfile(&ea, 0);
6322
6323 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6324 * mode that is not needed here. */
6325 need_start_insertmode = FALSE;
6326
6327 /* Restore msg_scroll, otherwise a following command may cause scrolling
6328 * unexpectedly. The screen will be redrawn by the caller, thus
6329 * msg_scroll being set by displaying a message is irrelevant. */
6330 msg_scroll = save_msg_scroll;
6331}
6332#endif
6333
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334/*
6335 * Clear an argument list: free all file names and reset it to zero entries.
6336 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006337 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338alist_clear(al)
6339 alist_T *al;
6340{
6341 while (--al->al_ga.ga_len >= 0)
6342 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6343 ga_clear(&al->al_ga);
6344}
6345
6346/*
6347 * Init an argument list.
6348 */
6349 void
6350alist_init(al)
6351 alist_T *al;
6352{
6353 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6354}
6355
6356#if defined(FEAT_WINDOWS) || defined(PROTO)
6357
6358/*
6359 * Remove a reference from an argument list.
6360 * Ignored when the argument list is the global one.
6361 * If the argument list is no longer used by any window, free it.
6362 */
6363 void
6364alist_unlink(al)
6365 alist_T *al;
6366{
6367 if (al != &global_alist && --al->al_refcount <= 0)
6368 {
6369 alist_clear(al);
6370 vim_free(al);
6371 }
6372}
6373
6374# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6375/*
6376 * Create a new argument list and use it for the current window.
6377 */
6378 void
6379alist_new()
6380{
6381 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6382 if (curwin->w_alist == NULL)
6383 {
6384 curwin->w_alist = &global_alist;
6385 ++global_alist.al_refcount;
6386 }
6387 else
6388 {
6389 curwin->w_alist->al_refcount = 1;
6390 alist_init(curwin->w_alist);
6391 }
6392}
6393# endif
6394#endif
6395
6396#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6397/*
6398 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006399 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6400 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 */
6402 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006403alist_expand(fnum_list, fnum_len)
6404 int *fnum_list;
6405 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406{
6407 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006408 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 char_u **new_arg_files;
6410 int new_arg_file_count;
6411 char_u *save_p_su = p_su;
6412 int i;
6413
6414 /* Don't use 'suffixes' here. This should work like the shell did the
6415 * expansion. Also, the vimrc file isn't read yet, thus the user
6416 * can't set the options. */
6417 p_su = empty_option;
6418 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6419 if (old_arg_files != NULL)
6420 {
6421 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006422 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6423 old_arg_count = GARGCOUNT;
6424 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 &new_arg_file_count, &new_arg_files,
6426 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6427 && new_arg_file_count > 0)
6428 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006429 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6430 TRUE, fnum_list, fnum_len);
6431 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 }
6434 p_su = save_p_su;
6435}
6436#endif
6437
6438/*
6439 * Set the argument list for the current window.
6440 * Takes over the allocated files[] and the allocated fnames in it.
6441 */
6442 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006443alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 alist_T *al;
6445 int count;
6446 char_u **files;
6447 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006448 int *fnum_list;
6449 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
6451 int i;
6452
6453 alist_clear(al);
6454 if (ga_grow(&al->al_ga, count) == OK)
6455 {
6456 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006457 {
6458 if (got_int)
6459 {
6460 /* When adding many buffers this can take a long time. Allow
6461 * interrupting here. */
6462 while (i < count)
6463 vim_free(files[i++]);
6464 break;
6465 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006466
6467 /* May set buffer name of a buffer previously used for the
6468 * argument list, so that it's re-used by alist_add. */
6469 if (fnum_list != NULL && i < fnum_len)
6470 buf_set_name(fnum_list[i], files[i]);
6471
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006473 ui_breakcheck();
6474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 vim_free(files);
6476 }
6477 else
6478 FreeWild(count, files);
6479#ifdef FEAT_WINDOWS
6480 if (al == &global_alist)
6481#endif
6482 arg_had_last = FALSE;
6483}
6484
6485/*
6486 * Add file "fname" to argument list "al".
6487 * "fname" must have been allocated and "al" must have been checked for room.
6488 */
6489 void
6490alist_add(al, fname, set_fnum)
6491 alist_T *al;
6492 char_u *fname;
6493 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6494{
6495 if (fname == NULL) /* don't add NULL file names */
6496 return;
6497#ifdef BACKSLASH_IN_FILENAME
6498 slash_adjust(fname);
6499#endif
6500 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6501 if (set_fnum > 0)
6502 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6503 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6504 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505}
6506
6507#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6508/*
6509 * Adjust slashes in file names. Called after 'shellslash' was set.
6510 */
6511 void
6512alist_slash_adjust()
6513{
6514 int i;
6515# ifdef FEAT_WINDOWS
6516 win_T *wp;
6517# endif
6518
6519 for (i = 0; i < GARGCOUNT; ++i)
6520 if (GARGLIST[i].ae_fname != NULL)
6521 slash_adjust(GARGLIST[i].ae_fname);
6522# ifdef FEAT_WINDOWS
6523 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6524 if (wp->w_alist != &global_alist)
6525 for (i = 0; i < WARGCOUNT(wp); ++i)
6526 if (WARGLIST(wp)[i].ae_fname != NULL)
6527 slash_adjust(WARGLIST(wp)[i].ae_fname);
6528# endif
6529}
6530#endif
6531
6532/*
6533 * ":preserve".
6534 */
6535/*ARGSUSED*/
6536 static void
6537ex_preserve(eap)
6538 exarg_T *eap;
6539{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006540 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 ml_preserve(curbuf, TRUE);
6542}
6543
6544/*
6545 * ":recover".
6546 */
6547 static void
6548ex_recover(eap)
6549 exarg_T *eap;
6550{
6551 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6552 recoverymode = TRUE;
6553 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6554 && (*eap->arg == NUL
6555 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6556 ml_recover();
6557 recoverymode = FALSE;
6558}
6559
6560/*
6561 * Command modifier used in a wrong way.
6562 */
6563 static void
6564ex_wrongmodifier(eap)
6565 exarg_T *eap;
6566{
6567 eap->errmsg = e_invcmd;
6568}
6569
6570#ifdef FEAT_WINDOWS
6571/*
6572 * :sview [+command] file split window with new file, read-only
6573 * :split [[+command] file] split window with current or new file
6574 * :vsplit [[+command] file] split window vertically with current or new file
6575 * :new [[+command] file] split window with no or new file
6576 * :vnew [[+command] file] split vertically window with no or new file
6577 * :sfind [+command] file split window with file in 'path'
6578 */
6579 void
6580ex_splitview(eap)
6581 exarg_T *eap;
6582{
6583 win_T *old_curwin;
6584#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6585 char_u *fname = NULL;
6586#endif
6587#ifdef FEAT_BROWSE
6588 int browse_flag = cmdmod.browse;
6589#endif
6590
6591#ifndef FEAT_VERTSPLIT
6592 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6593 {
6594 ex_ni(eap);
6595 return;
6596 }
6597#endif
6598
6599 old_curwin = curwin;
6600#ifdef FEAT_GUI
6601 need_mouse_correct = TRUE;
6602#endif
6603
6604#ifdef FEAT_QUICKFIX
6605 /* A ":split" in the quickfix window works like ":new". Don't want two
6606 * quickfix windows. */
6607 if (bt_quickfix(curbuf))
6608 {
6609 if (eap->cmdidx == CMD_split)
6610 eap->cmdidx = CMD_new;
6611# ifdef FEAT_VERTSPLIT
6612 if (eap->cmdidx == CMD_vsplit)
6613 eap->cmdidx = CMD_vnew;
6614# endif
6615 }
6616#endif
6617
6618#ifdef FEAT_SEARCHPATH
6619 if (eap->cmdidx == CMD_sfind)
6620 {
6621 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6622 FNAME_MESS, TRUE, curbuf->b_ffname);
6623 if (fname == NULL)
6624 goto theend;
6625 eap->arg = fname;
6626 }
6627# ifdef FEAT_BROWSE
6628 else
6629# endif
6630#endif
6631#ifdef FEAT_BROWSE
6632 if (cmdmod.browse
6633# ifdef FEAT_VERTSPLIT
6634 && eap->cmdidx != CMD_vnew
6635#endif
6636 && eap->cmdidx != CMD_new)
6637 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006638 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 eap->arg, NULL, NULL, NULL, curbuf);
6640 if (fname == NULL)
6641 goto theend;
6642 eap->arg = fname;
6643 }
6644 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
6645#endif
6646
6647 if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
6648 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6649 {
6650#ifdef FEAT_SCROLLBIND
6651 /* Reset 'scrollbind' when editing another file, but keep it when
6652 * doing ":split" without arguments. */
6653 if (*eap->arg != NUL
6654#ifdef FEAT_BROWSE
6655 || cmdmod.browse
6656#endif
6657 )
6658 curwin->w_p_scb = FALSE;
6659 else
6660 do_check_scrollbind(FALSE);
6661#endif
6662 do_exedit(eap, old_curwin);
6663 }
6664
6665#ifdef FEAT_BROWSE
6666 cmdmod.browse = browse_flag;
6667#endif
6668
6669#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6670theend:
6671 vim_free(fname);
6672#endif
6673}
6674#endif
6675
6676/*
6677 * ":mode": Set screen mode.
6678 * If no argument given, just get the screen size and redraw.
6679 */
6680 static void
6681ex_mode(eap)
6682 exarg_T *eap;
6683{
6684 if (*eap->arg == NUL)
6685 shell_resized();
6686 else
6687 mch_screenmode(eap->arg);
6688}
6689
6690#ifdef FEAT_WINDOWS
6691/*
6692 * ":resize".
6693 * set, increment or decrement current window height
6694 */
6695 static void
6696ex_resize(eap)
6697 exarg_T *eap;
6698{
6699 int n;
6700 win_T *wp = curwin;
6701
6702 if (eap->addr_count > 0)
6703 {
6704 n = eap->line2;
6705 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6706 ;
6707 }
6708
6709#ifdef FEAT_GUI
6710 need_mouse_correct = TRUE;
6711#endif
6712 n = atol((char *)eap->arg);
6713#ifdef FEAT_VERTSPLIT
6714 if (cmdmod.split & WSP_VERT)
6715 {
6716 if (*eap->arg == '-' || *eap->arg == '+')
6717 n += W_WIDTH(curwin);
6718 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6719 n = 9999;
6720 win_setwidth_win((int)n, wp);
6721 }
6722 else
6723#endif
6724 {
6725 if (*eap->arg == '-' || *eap->arg == '+')
6726 n += curwin->w_height;
6727 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6728 n = 9999;
6729 win_setheight_win((int)n, wp);
6730 }
6731}
6732#endif
6733
6734/*
6735 * ":find [+command] <file>" command.
6736 */
6737 static void
6738ex_find(eap)
6739 exarg_T *eap;
6740{
6741#ifdef FEAT_SEARCHPATH
6742 char_u *fname;
6743 int count;
6744
6745 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6746 TRUE, curbuf->b_ffname);
6747 if (eap->addr_count > 0)
6748 {
6749 /* Repeat finding the file "count" times. This matters when it
6750 * appears several times in the path. */
6751 count = eap->line2;
6752 while (fname != NULL && --count > 0)
6753 {
6754 vim_free(fname);
6755 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6756 FALSE, curbuf->b_ffname);
6757 }
6758 }
6759
6760 if (fname != NULL)
6761 {
6762 eap->arg = fname;
6763#endif
6764 do_exedit(eap, NULL);
6765#ifdef FEAT_SEARCHPATH
6766 vim_free(fname);
6767 }
6768#endif
6769}
6770
6771/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006772 * ":open" simulation: for now just work like ":visual".
6773 */
6774 static void
6775ex_open(eap)
6776 exarg_T *eap;
6777{
6778 regmatch_T regmatch;
6779 char_u *p;
6780
6781 curwin->w_cursor.lnum = eap->line2;
6782 beginline(BL_SOL | BL_FIX);
6783 if (*eap->arg == '/')
6784 {
6785 /* ":open /pattern/": put cursor in column found with pattern */
6786 ++eap->arg;
6787 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6788 *p = NUL;
6789 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6790 if (regmatch.regprog != NULL)
6791 {
6792 regmatch.rm_ic = p_ic;
6793 p = ml_get_curline();
6794 if (vim_regexec(&regmatch, p, (colnr_T)0))
6795 curwin->w_cursor.col = regmatch.startp[0] - p;
6796 else
6797 EMSG(_(e_nomatch));
6798 vim_free(regmatch.regprog);
6799 }
6800 /* Move to the NUL, ignore any other arguments. */
6801 eap->arg += STRLEN(eap->arg);
6802 }
6803 check_cursor();
6804
6805 eap->cmdidx = CMD_visual;
6806 do_exedit(eap, NULL);
6807}
6808
6809/*
6810 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 */
6812 static void
6813ex_edit(eap)
6814 exarg_T *eap;
6815{
6816 do_exedit(eap, NULL);
6817}
6818
6819/*
6820 * ":edit <file>" command and alikes.
6821 */
6822/*ARGSUSED*/
6823 void
6824do_exedit(eap, old_curwin)
6825 exarg_T *eap;
6826 win_T *old_curwin; /* curwin before doing a split or NULL */
6827{
6828 int n;
6829#ifdef FEAT_WINDOWS
6830 int need_hide;
6831#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006832 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833
6834 /*
6835 * ":vi" command ends Ex mode.
6836 */
6837 if (exmode_active && (eap->cmdidx == CMD_visual
6838 || eap->cmdidx == CMD_view))
6839 {
6840 exmode_active = FALSE;
6841 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006842 {
6843 /* Special case: ":global/pat/visual\NLvi-commands" */
6844 if (global_busy)
6845 {
6846 int rd = RedrawingDisabled;
6847 int nwr = no_wait_return;
6848 int ms = msg_scroll;
6849#ifdef FEAT_GUI
6850 int he = hold_gui_events;
6851#endif
6852
6853 if (eap->nextcmd != NULL)
6854 {
6855 stuffReadbuff(eap->nextcmd);
6856 eap->nextcmd = NULL;
6857 }
6858
6859 if (exmode_was != EXMODE_VIM)
6860 settmode(TMODE_RAW);
6861 RedrawingDisabled = 0;
6862 no_wait_return = 0;
6863 need_wait_return = FALSE;
6864 msg_scroll = 0;
6865#ifdef FEAT_GUI
6866 hold_gui_events = 0;
6867#endif
6868 must_redraw = CLEAR;
6869
6870 main_loop(FALSE, TRUE);
6871
6872 RedrawingDisabled = rd;
6873 no_wait_return = nwr;
6874 msg_scroll = ms;
6875#ifdef FEAT_GUI
6876 hold_gui_events = he;
6877#endif
6878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 }
6882
6883 if ((eap->cmdidx == CMD_new
6884#ifdef FEAT_VERTSPLIT
6885 || eap->cmdidx == CMD_vnew
6886#endif
6887 ) && *eap->arg == NUL)
6888 {
6889 /* ":new" without argument: edit an new empty buffer */
6890 setpcmark();
6891 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
6892 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
6893 }
6894 else if ((eap->cmdidx != CMD_split
6895#ifdef FEAT_VERTSPLIT
6896 && eap->cmdidx != CMD_vsplit
6897#endif
6898 )
6899 || *eap->arg != NUL
6900#ifdef FEAT_BROWSE
6901 || cmdmod.browse
6902#endif
6903 )
6904 {
6905 n = readonlymode;
6906 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6907 readonlymode = TRUE;
6908 else if (eap->cmdidx == CMD_enew)
6909 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
6910 empty buffer */
6911 setpcmark();
6912 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6913 NULL, eap,
6914 /* ":edit" goes to first line if Vi compatible */
6915 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6916 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6917 ? ECMD_ONE : eap->do_ecmd_lnum,
6918 (P_HID(curbuf) ? ECMD_HIDE : 0)
6919 + (eap->forceit ? ECMD_FORCEIT : 0)
6920#ifdef FEAT_LISTCMDS
6921 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
6922#endif
6923 ) == FAIL)
6924 {
6925 /* Editing the file failed. If the window was split, close it. */
6926#ifdef FEAT_WINDOWS
6927 if (old_curwin != NULL)
6928 {
6929 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
6930 if (!need_hide || P_HID(curbuf))
6931 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006932# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6933 cleanup_T cs;
6934
6935 /* Reset the error/interrupt/exception state here so that
6936 * aborting() returns FALSE when closing a window. */
6937 enter_cleanup(&cs);
6938# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939# ifdef FEAT_GUI
6940 need_mouse_correct = TRUE;
6941# endif
6942 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006943
6944# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6945 /* Restore the error/interrupt/exception state if not
6946 * discarded by a new aborting error, interrupt, or
6947 * uncaught exception. */
6948 leave_cleanup(&cs);
6949# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 }
6951 }
6952#endif
6953 }
6954 else if (readonlymode && curbuf->b_nwindows == 1)
6955 {
6956 /* When editing an already visited buffer, 'readonly' won't be set
6957 * but the previous value is kept. With ":view" and ":sview" we
6958 * want the file to be readonly, except when another window is
6959 * editing the same buffer. */
6960 curbuf->b_p_ro = TRUE;
6961 }
6962 readonlymode = n;
6963 }
6964 else
6965 {
6966 if (eap->do_ecmd_cmd != NULL)
6967 do_cmdline_cmd(eap->do_ecmd_cmd);
6968#ifdef FEAT_TITLE
6969 n = curwin->w_arg_idx_invalid;
6970#endif
6971 check_arg_idx(curwin);
6972#ifdef FEAT_TITLE
6973 if (n != curwin->w_arg_idx_invalid)
6974 maketitle();
6975#endif
6976 }
6977
6978#ifdef FEAT_WINDOWS
6979 /*
6980 * if ":split file" worked, set alternate file name in old window to new
6981 * file
6982 */
6983 if (old_curwin != NULL
6984 && *eap->arg != NUL
6985 && curwin != old_curwin
6986 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006987 && old_curwin->w_buffer != curbuf
6988 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 old_curwin->w_alt_fnum = curbuf->b_fnum;
6990#endif
6991
6992 ex_no_reprint = TRUE;
6993}
6994
6995#ifndef FEAT_GUI
6996/*
6997 * ":gui" and ":gvim" when there is no GUI.
6998 */
6999 static void
7000ex_nogui(eap)
7001 exarg_T *eap;
7002{
7003 eap->errmsg = e_nogvim;
7004}
7005#endif
7006
7007#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7008 static void
7009ex_tearoff(eap)
7010 exarg_T *eap;
7011{
7012 gui_make_tearoff(eap->arg);
7013}
7014#endif
7015
Bram Moolenaar843ee412004-06-30 16:16:41 +00007016#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_KDE) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 static void
7018ex_popup(eap)
7019 exarg_T *eap;
7020{
7021 gui_make_popup(eap->arg);
7022}
7023#endif
7024
7025/*ARGSUSED*/
7026 static void
7027ex_swapname(eap)
7028 exarg_T *eap;
7029{
7030 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7031 MSG(_("No swap file"));
7032 else
7033 msg(curbuf->b_ml.ml_mfp->mf_fname);
7034}
7035
7036/*
7037 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7038 * offset.
7039 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7040 */
7041/*ARGSUSED*/
7042 static void
7043ex_syncbind(eap)
7044 exarg_T *eap;
7045{
7046#ifdef FEAT_SCROLLBIND
7047 win_T *wp;
7048 long topline;
7049 long y;
7050 linenr_T old_linenr = curwin->w_cursor.lnum;
7051
7052 setpcmark();
7053
7054 /*
7055 * determine max topline
7056 */
7057 if (curwin->w_p_scb)
7058 {
7059 topline = curwin->w_topline;
7060 for (wp = firstwin; wp; wp = wp->w_next)
7061 {
7062 if (wp->w_p_scb && wp->w_buffer)
7063 {
7064 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7065 if (topline > y)
7066 topline = y;
7067 }
7068 }
7069 if (topline < 1)
7070 topline = 1;
7071 }
7072 else
7073 {
7074 topline = 1;
7075 }
7076
7077
7078 /*
7079 * set all scrollbind windows to the same topline
7080 */
7081 wp = curwin;
7082 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7083 {
7084 if (curwin->w_p_scb)
7085 {
7086 y = topline - curwin->w_topline;
7087 if (y > 0)
7088 scrollup(y, TRUE);
7089 else
7090 scrolldown(-y, TRUE);
7091 curwin->w_scbind_pos = topline;
7092 redraw_later(VALID);
7093 cursor_correct();
7094#ifdef FEAT_WINDOWS
7095 curwin->w_redr_status = TRUE;
7096#endif
7097 }
7098 }
7099 curwin = wp;
7100 if (curwin->w_p_scb)
7101 {
7102 did_syncbind = TRUE;
7103 checkpcmark();
7104 if (old_linenr != curwin->w_cursor.lnum)
7105 {
7106 char_u ctrl_o[2];
7107
7108 ctrl_o[0] = Ctrl_O;
7109 ctrl_o[1] = 0;
7110 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7111 }
7112 }
7113#endif
7114}
7115
7116
7117 static void
7118ex_read(eap)
7119 exarg_T *eap;
7120{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007121 int i;
7122 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7123 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124
7125 if (eap->usefilter) /* :r!cmd */
7126 do_bang(1, eap, FALSE, FALSE, TRUE);
7127 else
7128 {
7129 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7130 return;
7131
7132#ifdef FEAT_BROWSE
7133 if (cmdmod.browse)
7134 {
7135 char_u *browseFile;
7136
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007137 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 NULL, NULL, NULL, curbuf);
7139 if (browseFile != NULL)
7140 {
7141 i = readfile(browseFile, NULL,
7142 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7143 vim_free(browseFile);
7144 }
7145 else
7146 i = OK;
7147 }
7148 else
7149#endif
7150 if (*eap->arg == NUL)
7151 {
7152 if (check_fname() == FAIL) /* check for no file name */
7153 return;
7154 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7155 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7156 }
7157 else
7158 {
7159 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7160 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7161 i = readfile(eap->arg, NULL,
7162 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7163
7164 }
7165 if (i == FAIL)
7166 {
7167#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7168 if (!aborting())
7169#endif
7170 EMSG2(_(e_notopen), eap->arg);
7171 }
7172 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007173 {
7174 if (empty && exmode_active)
7175 {
7176 /* Delete the empty line that remains. Historically ex does
7177 * this but vi doesn't. */
7178 if (eap->line2 == 0)
7179 lnum = curbuf->b_ml.ml_line_count;
7180 else
7181 lnum = 1;
7182 if (*ml_get(lnum) == NUL)
7183 {
7184 ml_delete(lnum, FALSE);
7185 deleted_lines_mark(lnum, 1L);
7186 if (curwin->w_cursor.lnum >= lnum)
7187 --curwin->w_cursor.lnum;
7188 }
7189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 }
7193}
7194
7195/*
7196 * ":cd", ":lcd", ":chdir" and ":lchdir".
7197 */
7198 static void
7199ex_cd(eap)
7200 exarg_T *eap;
7201{
7202 static char_u *prev_dir = NULL;
7203 char_u *new_dir;
7204 char_u *tofree;
7205
7206 new_dir = eap->arg;
7207#if !defined(UNIX) && !defined(VMS)
7208 /* for non-UNIX ":cd" means: print current directory */
7209 if (*new_dir == NUL)
7210 ex_pwd(NULL);
7211 else
7212#endif
7213 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007214 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7215 && !eap->forceit)
7216 {
7217 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7218 return;
7219 }
7220
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 /* ":cd -": Change to previous directory */
7222 if (STRCMP(new_dir, "-") == 0)
7223 {
7224 if (prev_dir == NULL)
7225 {
7226 EMSG(_("E186: No previous directory"));
7227 return;
7228 }
7229 new_dir = prev_dir;
7230 }
7231
7232 /* Save current directory for next ":cd -" */
7233 tofree = prev_dir;
7234 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7235 prev_dir = vim_strsave(NameBuff);
7236 else
7237 prev_dir = NULL;
7238
7239#if defined(UNIX) || defined(VMS)
7240 /* for UNIX ":cd" means: go to home directory */
7241 if (*new_dir == NUL)
7242 {
7243 /* use NameBuff for home directory name */
7244# ifdef VMS
7245 char_u *p;
7246
7247 p = mch_getenv((char_u *)"SYS$LOGIN");
7248 if (p == NULL || *p == NUL) /* empty is the same as not set */
7249 NameBuff[0] = NUL;
7250 else
7251 STRNCPY(NameBuff, p, MAXPATHL);
7252# else
7253 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7254# endif
7255 new_dir = NameBuff;
7256 }
7257#endif
7258 if (new_dir == NULL || vim_chdir(new_dir))
7259 EMSG(_(e_failed));
7260 else
7261 {
7262 vim_free(curwin->w_localdir);
7263 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7264 {
7265 /* If still in global directory, need to remember current
7266 * directory as global directory. */
7267 if (globaldir == NULL && prev_dir != NULL)
7268 globaldir = vim_strsave(prev_dir);
7269 /* Remember this local directory for the window. */
7270 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7271 curwin->w_localdir = vim_strsave(NameBuff);
7272 }
7273 else
7274 {
7275 /* We are now in the global directory, no need to remember its
7276 * name. */
7277 vim_free(globaldir);
7278 globaldir = NULL;
7279 curwin->w_localdir = NULL;
7280 }
7281
7282 shorten_fnames(TRUE);
7283
7284 /* Echo the new current directory if the command was typed. */
7285 if (KeyTyped)
7286 ex_pwd(eap);
7287 }
7288 vim_free(tofree);
7289 }
7290}
7291
7292/*
7293 * ":pwd".
7294 */
7295/*ARGSUSED*/
7296 static void
7297ex_pwd(eap)
7298 exarg_T *eap;
7299{
7300 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7301 {
7302#ifdef BACKSLASH_IN_FILENAME
7303 slash_adjust(NameBuff);
7304#endif
7305 msg(NameBuff);
7306 }
7307 else
7308 EMSG(_("E187: Unknown"));
7309}
7310
7311/*
7312 * ":=".
7313 */
7314 static void
7315ex_equal(eap)
7316 exarg_T *eap;
7317{
7318 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007319 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320}
7321
7322 static void
7323ex_sleep(eap)
7324 exarg_T *eap;
7325{
7326 int n;
7327
7328 if (cursor_valid())
7329 {
7330 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7331 if (n >= 0)
7332 windgoto((int)n, curwin->w_wcol);
7333 }
7334 do_sleep(eap->line2 * (*eap->arg == 'm' ? 1L : 1000L));
7335}
7336
7337/*
7338 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7339 */
7340 void
7341do_sleep(msec)
7342 long msec;
7343{
7344 long done;
7345
7346 cursor_on();
7347 out_flush();
7348 for (done = 0; !got_int && done < msec; done += 1000L)
7349 {
7350 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7351 ui_breakcheck();
7352 }
7353}
7354
7355 static void
7356do_exmap(eap, isabbrev)
7357 exarg_T *eap;
7358 int isabbrev;
7359{
7360 int mode;
7361 char_u *cmdp;
7362
7363 cmdp = eap->cmd;
7364 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7365
7366 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7367 eap->arg, mode, isabbrev))
7368 {
7369 case 1: EMSG(_(e_invarg));
7370 break;
7371 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7372 break;
7373 }
7374}
7375
7376/*
7377 * ":winsize" command (obsolete).
7378 */
7379 static void
7380ex_winsize(eap)
7381 exarg_T *eap;
7382{
7383 int w, h;
7384 char_u *arg = eap->arg;
7385 char_u *p;
7386
7387 w = getdigits(&arg);
7388 arg = skipwhite(arg);
7389 p = arg;
7390 h = getdigits(&arg);
7391 if (*p != NUL && *arg == NUL)
7392 set_shellsize(w, h, TRUE);
7393 else
7394 EMSG(_("E465: :winsize requires two number arguments"));
7395}
7396
7397#ifdef FEAT_WINDOWS
7398 static void
7399ex_wincmd(eap)
7400 exarg_T *eap;
7401{
7402 int xchar = NUL;
7403 char_u *p;
7404
7405 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7406 {
7407 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7408 if (eap->arg[1] == NUL)
7409 {
7410 EMSG(_(e_invarg));
7411 return;
7412 }
7413 xchar = eap->arg[1];
7414 p = eap->arg + 2;
7415 }
7416 else
7417 p = eap->arg + 1;
7418
7419 eap->nextcmd = check_nextcmd(p);
7420 p = skipwhite(p);
7421 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7422 EMSG(_(e_invarg));
7423 else
7424 {
7425 /* Pass flags on for ":vertical wincmd ]". */
7426 postponed_split_flags = cmdmod.split;
7427 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7428 postponed_split_flags = 0;
7429 }
7430}
7431#endif
7432
Bram Moolenaar843ee412004-06-30 16:16:41 +00007433#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434/*
7435 * ":winpos".
7436 */
7437 static void
7438ex_winpos(eap)
7439 exarg_T *eap;
7440{
7441 int x, y;
7442 char_u *arg = eap->arg;
7443 char_u *p;
7444
7445 if (*arg == NUL)
7446 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007447# if defined(FEAT_GUI) || defined(MSWIN)
7448# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007450# else
7451 if (mch_get_winpos(&x, &y) != FAIL)
7452# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 {
7454 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7455 msg(IObuff);
7456 }
7457 else
7458# endif
7459 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7460 }
7461 else
7462 {
7463 x = getdigits(&arg);
7464 arg = skipwhite(arg);
7465 p = arg;
7466 y = getdigits(&arg);
7467 if (*p == NUL || *arg != NUL)
7468 {
7469 EMSG(_("E466: :winpos requires two number arguments"));
7470 return;
7471 }
7472# ifdef FEAT_GUI
7473 if (gui.in_use)
7474 gui_mch_set_winpos(x, y);
7475 else if (gui.starting)
7476 {
7477 /* Remember the coordinates for when the window is opened. */
7478 gui_win_x = x;
7479 gui_win_y = y;
7480 }
7481# ifdef HAVE_TGETENT
7482 else
7483# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007484# else
7485# ifdef MSWIN
7486 mch_set_winpos(x, y);
7487# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488# endif
7489# ifdef HAVE_TGETENT
7490 if (*T_CWP)
7491 term_set_winpos(x, y);
7492# endif
7493 }
7494}
7495#endif
7496
7497/*
7498 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7499 */
7500 static void
7501ex_operators(eap)
7502 exarg_T *eap;
7503{
7504 oparg_T oa;
7505
7506 clear_oparg(&oa);
7507 oa.regname = eap->regname;
7508 oa.start.lnum = eap->line1;
7509 oa.end.lnum = eap->line2;
7510 oa.line_count = eap->line2 - eap->line1 + 1;
7511 oa.motion_type = MLINE;
7512#ifdef FEAT_VIRTUALEDIT
7513 virtual_op = FALSE;
7514#endif
7515 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7516 {
7517 setpcmark();
7518 curwin->w_cursor.lnum = eap->line1;
7519 beginline(BL_SOL | BL_FIX);
7520 }
7521
7522 switch (eap->cmdidx)
7523 {
7524 case CMD_delete:
7525 oa.op_type = OP_DELETE;
7526 op_delete(&oa);
7527 break;
7528
7529 case CMD_yank:
7530 oa.op_type = OP_YANK;
7531 (void)op_yank(&oa, FALSE, TRUE);
7532 break;
7533
7534 default: /* CMD_rshift or CMD_lshift */
7535 if ((eap->cmdidx == CMD_rshift)
7536#ifdef FEAT_RIGHTLEFT
7537 ^ curwin->w_p_rl
7538#endif
7539 )
7540 oa.op_type = OP_RSHIFT;
7541 else
7542 oa.op_type = OP_LSHIFT;
7543 op_shift(&oa, FALSE, eap->amount);
7544 break;
7545 }
7546#ifdef FEAT_VIRTUALEDIT
7547 virtual_op = MAYBE;
7548#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007549 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550}
7551
7552/*
7553 * ":put".
7554 */
7555 static void
7556ex_put(eap)
7557 exarg_T *eap;
7558{
7559 /* ":0put" works like ":1put!". */
7560 if (eap->line2 == 0)
7561 {
7562 eap->line2 = 1;
7563 eap->forceit = TRUE;
7564 }
7565 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007566 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7567 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568}
7569
7570/*
7571 * Handle ":copy" and ":move".
7572 */
7573 static void
7574ex_copymove(eap)
7575 exarg_T *eap;
7576{
7577 long n;
7578
7579 n = get_address(&eap->arg, FALSE, FALSE);
7580 if (eap->arg == NULL) /* error detected */
7581 {
7582 eap->nextcmd = NULL;
7583 return;
7584 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007585 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586
7587 /*
7588 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7589 */
7590 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7591 {
7592 EMSG(_(e_invaddr));
7593 return;
7594 }
7595
7596 if (eap->cmdidx == CMD_move)
7597 {
7598 if (do_move(eap->line1, eap->line2, n) == FAIL)
7599 return;
7600 }
7601 else
7602 ex_copy(eap->line1, eap->line2, n);
7603 u_clearline();
7604 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007605 ex_may_print(eap);
7606}
7607
7608/*
7609 * Print the current line if flags were given to the Ex command.
7610 */
7611 static void
7612ex_may_print(eap)
7613 exarg_T *eap;
7614{
7615 if (eap->flags != 0)
7616 {
7617 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7618 (eap->flags & EXFLAG_LIST));
7619 ex_no_reprint = TRUE;
7620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621}
7622
7623/*
7624 * ":smagic" and ":snomagic".
7625 */
7626 static void
7627ex_submagic(eap)
7628 exarg_T *eap;
7629{
7630 int magic_save = p_magic;
7631
7632 p_magic = (eap->cmdidx == CMD_smagic);
7633 do_sub(eap);
7634 p_magic = magic_save;
7635}
7636
7637/*
7638 * ":join".
7639 */
7640 static void
7641ex_join(eap)
7642 exarg_T *eap;
7643{
7644 curwin->w_cursor.lnum = eap->line1;
7645 if (eap->line1 == eap->line2)
7646 {
7647 if (eap->addr_count >= 2) /* :2,2join does nothing */
7648 return;
7649 if (eap->line2 == curbuf->b_ml.ml_line_count)
7650 {
7651 beep_flush();
7652 return;
7653 }
7654 ++eap->line2;
7655 }
7656 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
7657 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007658 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659}
7660
7661/*
7662 * ":[addr]@r" or ":[addr]*r": execute register
7663 */
7664 static void
7665ex_at(eap)
7666 exarg_T *eap;
7667{
7668 int c;
7669
7670 curwin->w_cursor.lnum = eap->line2;
7671
7672#ifdef USE_ON_FLY_SCROLL
7673 dont_scroll = TRUE; /* disallow scrolling here */
7674#endif
7675
7676 /* get the register name. No name means to use the previous one */
7677 c = *eap->arg;
7678 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7679 c = '@';
7680 /* put the register in mapbuf */
7681 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007682 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 else
7686 {
7687 int save_efr = exec_from_reg;
7688
7689 exec_from_reg = TRUE;
7690
7691 /*
7692 * Execute from the typeahead buffer.
7693 * Originally this didn't check for the typeahead buffer to be empty,
7694 * thus could read more Ex commands from stdin. It's not clear why,
7695 * it is certainly unexpected.
7696 */
7697 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
7698 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7699
7700 exec_from_reg = save_efr;
7701 }
7702}
7703
7704/*
7705 * ":!".
7706 */
7707 static void
7708ex_bang(eap)
7709 exarg_T *eap;
7710{
7711 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7712}
7713
7714/*
7715 * ":undo".
7716 */
7717/*ARGSUSED*/
7718 static void
7719ex_undo(eap)
7720 exarg_T *eap;
7721{
7722 u_undo(1);
7723}
7724
7725/*
7726 * ":redo".
7727 */
7728/*ARGSUSED*/
7729 static void
7730ex_redo(eap)
7731 exarg_T *eap;
7732{
7733 u_redo(1);
7734}
7735
7736/*
7737 * ":redir": start/stop redirection.
7738 */
7739 static void
7740ex_redir(eap)
7741 exarg_T *eap;
7742{
7743 char *mode;
7744 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007745 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746
7747 if (STRICMP(eap->arg, "END") == 0)
7748 close_redir();
7749 else
7750 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007751 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007753 ++arg;
7754 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007756 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 mode = "a";
7758 }
7759 else
7760 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007761 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762
7763 close_redir();
7764
7765 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00007766 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 if (fname == NULL)
7768 return;
7769#ifdef FEAT_BROWSE
7770 if (cmdmod.browse)
7771 {
7772 char_u *browseFile;
7773
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007774 browseFile = do_browse(BROWSE_SAVE,
7775 (char_u *)_("Save Redirection"),
7776 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 if (browseFile == NULL)
7778 return; /* operation cancelled */
7779 vim_free(fname);
7780 fname = browseFile;
7781 eap->forceit = TRUE; /* since dialog already asked */
7782 }
7783#endif
7784
7785 redir_fd = open_exfile(fname, eap->forceit, mode);
7786 vim_free(fname);
7787 }
7788#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007789 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 {
7791 /* redirect to a register a-z (resp. A-Z for appending) */
7792 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007793 ++arg;
7794 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007796 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007798 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007800 redir_reg = *arg++;
7801 if (*arg == '>')
7802 ++arg;
7803 else if (*arg == NUL && (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007805 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007807 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 {
7809 /* make register empty */
7810 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
7811 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007812 }
7813 if (*arg != NUL)
7814 {
7815 EMSG2(_(e_invarg2), eap->arg);
7816 redir_reg = 0;
7817 }
7818 }
7819 else if (*arg == '=' && arg[1] == '>')
7820 {
7821 int append;
7822
7823 /* redirect to a variable */
7824 close_redir();
7825 arg += 2;
7826
7827 if (*arg == '>')
7828 {
7829 ++arg;
7830 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 }
7832 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007833 append = FALSE;
7834
7835 if (var_redir_start(skipwhite(arg), append) == OK)
7836 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 }
7838#endif
7839
7840 /* TODO: redirect to a buffer */
7841
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 else
Bram Moolenaarca472992005-01-21 11:46:23 +00007843 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 }
7845}
7846
7847/*
7848 * ":redraw": force redraw
7849 */
7850 static void
7851ex_redraw(eap)
7852 exarg_T *eap;
7853{
7854 int r = RedrawingDisabled;
7855 int p = p_lz;
7856
7857 RedrawingDisabled = 0;
7858 p_lz = FALSE;
7859 update_topline();
7860 update_screen(eap->forceit ? CLEAR :
7861#ifdef FEAT_VISUAL
7862 VIsual_active ? INVERTED :
7863#endif
7864 0);
7865#ifdef FEAT_TITLE
7866 if (need_maketitle)
7867 maketitle();
7868#endif
7869 RedrawingDisabled = r;
7870 p_lz = p;
7871
7872 /* Reset msg_didout, so that a message that's there is overwritten. */
7873 msg_didout = FALSE;
7874 msg_col = 0;
7875
7876 out_flush();
7877}
7878
7879/*
7880 * ":redrawstatus": force redraw of status line(s)
7881 */
7882/*ARGSUSED*/
7883 static void
7884ex_redrawstatus(eap)
7885 exarg_T *eap;
7886{
7887#if defined(FEAT_WINDOWS)
7888 int r = RedrawingDisabled;
7889 int p = p_lz;
7890
7891 RedrawingDisabled = 0;
7892 p_lz = FALSE;
7893 if (eap->forceit)
7894 status_redraw_all();
7895 else
7896 status_redraw_curbuf();
7897 update_screen(
7898# ifdef FEAT_VISUAL
7899 VIsual_active ? INVERTED :
7900# endif
7901 0);
7902 RedrawingDisabled = r;
7903 p_lz = p;
7904 out_flush();
7905#endif
7906}
7907
7908 static void
7909close_redir()
7910{
7911 if (redir_fd != NULL)
7912 {
7913 fclose(redir_fd);
7914 redir_fd = NULL;
7915 }
7916#ifdef FEAT_EVAL
7917 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007918 if (redir_vname)
7919 {
7920 var_redir_stop();
7921 redir_vname = 0;
7922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923#endif
7924}
7925
7926#if defined(FEAT_SESSION) && defined(USE_CRNL)
7927# define MKSESSION_NL
7928static int mksession_nl = FALSE; /* use NL only in put_eol() */
7929#endif
7930
7931/*
7932 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
7933 */
7934 static void
7935ex_mkrc(eap)
7936 exarg_T *eap;
7937{
7938 FILE *fd;
7939 int failed = FALSE;
7940 char_u *fname;
7941#ifdef FEAT_BROWSE
7942 char_u *browseFile = NULL;
7943#endif
7944#ifdef FEAT_SESSION
7945 int view_session = FALSE;
7946 int using_vdir = FALSE; /* using 'viewdir'? */
7947 char_u *viewFile = NULL;
7948 unsigned *flagp;
7949#endif
7950
7951 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
7952 {
7953#ifdef FEAT_SESSION
7954 view_session = TRUE;
7955#else
7956 ex_ni(eap);
7957 return;
7958#endif
7959 }
7960
7961#ifdef FEAT_SESSION
7962 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
7963 if (eap->cmdidx == CMD_mkview
7964 && (*eap->arg == NUL
7965 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
7966 {
7967 eap->forceit = TRUE;
7968 fname = get_view_file(*eap->arg);
7969 if (fname == NULL)
7970 return;
7971 viewFile = fname;
7972 using_vdir = TRUE;
7973 }
7974 else
7975#endif
7976 if (*eap->arg != NUL)
7977 fname = eap->arg;
7978 else if (eap->cmdidx == CMD_mkvimrc)
7979 fname = (char_u *)VIMRC_FILE;
7980#ifdef FEAT_SESSION
7981 else if (eap->cmdidx == CMD_mksession)
7982 fname = (char_u *)SESSION_FILE;
7983#endif
7984 else
7985 fname = (char_u *)EXRC_FILE;
7986
7987#ifdef FEAT_BROWSE
7988 if (cmdmod.browse)
7989 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007990 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991# ifdef FEAT_SESSION
7992 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
7993 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
7994# endif
7995 (char_u *)_("Save Setup"),
7996 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
7997 if (browseFile == NULL)
7998 goto theend;
7999 fname = browseFile;
8000 eap->forceit = TRUE; /* since dialog already asked */
8001 }
8002#endif
8003
8004#if defined(FEAT_SESSION) && defined(vim_mkdir)
8005 /* When using 'viewdir' may have to create the directory. */
8006 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008007 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008#endif
8009
8010 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8011 if (fd != NULL)
8012 {
8013#ifdef FEAT_SESSION
8014 if (eap->cmdidx == CMD_mkview)
8015 flagp = &vop_flags;
8016 else
8017 flagp = &ssop_flags;
8018#endif
8019
8020#ifdef MKSESSION_NL
8021 /* "unix" in 'sessionoptions': use NL line separator */
8022 if (view_session && (*flagp & SSOP_UNIX))
8023 mksession_nl = TRUE;
8024#endif
8025
8026 /* Write the version command for :mkvimrc */
8027 if (eap->cmdidx == CMD_mkvimrc)
8028 (void)put_line(fd, "version 6.0");
8029
8030#ifdef FEAT_SESSION
8031 if (eap->cmdidx != CMD_mkview)
8032#endif
8033 {
8034 /* Write setting 'compatible' first, because it has side effects.
8035 * For that same reason only do it when needed. */
8036 if (p_cp)
8037 (void)put_line(fd, "if !&cp | set cp | endif");
8038 else
8039 (void)put_line(fd, "if &cp | set nocp | endif");
8040 }
8041
8042#ifdef FEAT_SESSION
8043 if (!view_session
8044 || (eap->cmdidx == CMD_mksession
8045 && (*flagp & SSOP_OPTIONS)))
8046#endif
8047 failed |= (makemap(fd, NULL) == FAIL
8048 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8049
8050#ifdef FEAT_SESSION
8051 if (!failed && view_session)
8052 {
8053 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8054 failed = TRUE;
8055 if (eap->cmdidx == CMD_mksession)
8056 {
8057 char_u dirnow[MAXPATHL]; /* current directory */
8058
8059 /*
8060 * Change to session file's dir.
8061 */
8062 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8063 || mch_chdir((char *)dirnow) != 0)
8064 *dirnow = NUL;
8065 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8066 {
8067 if (vim_chdirfile(fname) == OK)
8068 shorten_fnames(TRUE);
8069 }
8070 else if (*dirnow != NUL
8071 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8072 {
8073 (void)mch_chdir((char *)globaldir);
8074 shorten_fnames(TRUE);
8075 }
8076
8077 failed |= (makeopens(fd, dirnow) == FAIL);
8078
8079 /* restore original dir */
8080 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8081 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8082 {
8083 if (mch_chdir((char *)dirnow) != 0)
8084 EMSG(_(e_prev_dir));
8085 shorten_fnames(TRUE);
8086 }
8087 }
8088 else
8089 {
8090 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8091 }
8092 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8093 == FAIL)
8094 failed = TRUE;
8095 }
8096#endif
8097 failed |= fclose(fd);
8098
8099 if (failed)
8100 EMSG(_(e_write));
8101#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8102 else if (eap->cmdidx == CMD_mksession)
8103 {
8104 /* successful session write - set this_session var */
8105 char_u tbuf[MAXPATHL];
8106
8107 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8108 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8109 }
8110#endif
8111#ifdef MKSESSION_NL
8112 mksession_nl = FALSE;
8113#endif
8114 }
8115
8116#ifdef FEAT_BROWSE
8117theend:
8118 vim_free(browseFile);
8119#endif
8120#ifdef FEAT_SESSION
8121 vim_free(viewFile);
8122#endif
8123}
8124
Bram Moolenaardf177f62005-02-22 08:39:57 +00008125#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8126 || defined(PROTO)
8127 int
8128vim_mkdir_emsg(name, prot)
8129 char_u *name;
8130 int prot;
8131{
8132 if (vim_mkdir(name, prot) != 0)
8133 {
8134 EMSG2(_("E739: Cannot create directory: %s"), name);
8135 return FAIL;
8136 }
8137 return OK;
8138}
8139#endif
8140
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141/*
8142 * Open a file for writing for an Ex command, with some checks.
8143 * Return file descriptor, or NULL on failure.
8144 */
8145 FILE *
8146open_exfile(fname, forceit, mode)
8147 char_u *fname;
8148 int forceit;
8149 char *mode; /* "w" for create new file or "a" for append */
8150{
8151 FILE *fd;
8152
8153#ifdef UNIX
8154 /* with Unix it is possible to open a directory */
8155 if (mch_isdir(fname))
8156 {
8157 EMSG2(_(e_isadir2), fname);
8158 return NULL;
8159 }
8160#endif
8161 if (!forceit && *mode != 'a' && vim_fexists(fname))
8162 {
8163 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8164 return NULL;
8165 }
8166
8167 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8168 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8169
8170 return fd;
8171}
8172
8173/*
8174 * ":mark" and ":k".
8175 */
8176 static void
8177ex_mark(eap)
8178 exarg_T *eap;
8179{
8180 pos_T pos;
8181
8182 if (*eap->arg == NUL) /* No argument? */
8183 EMSG(_(e_argreq));
8184 else if (eap->arg[1] != NUL) /* more than one character? */
8185 EMSG(_(e_trailing));
8186 else
8187 {
8188 pos = curwin->w_cursor; /* save curwin->w_cursor */
8189 curwin->w_cursor.lnum = eap->line2;
8190 beginline(BL_WHITE | BL_FIX);
8191 if (setmark(*eap->arg) == FAIL) /* set mark */
8192 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8193 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8194 }
8195}
8196
8197/*
8198 * Update w_topline, w_leftcol and the cursor position.
8199 */
8200 void
8201update_topline_cursor()
8202{
8203 check_cursor(); /* put cursor on valid line */
8204 update_topline();
8205 if (!curwin->w_p_wrap)
8206 validate_cursor();
8207 update_curswant();
8208}
8209
8210#ifdef FEAT_EX_EXTRA
8211/*
8212 * ":normal[!] {commands}": Execute normal mode commands.
8213 */
8214 static void
8215ex_normal(eap)
8216 exarg_T *eap;
8217{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 int save_msg_scroll = msg_scroll;
8219 int save_restart_edit = restart_edit;
8220 int save_msg_didout = msg_didout;
8221 int save_State = State;
8222 tasave_T tabuf;
8223 int save_insertmode = p_im;
8224 int save_finish_op = finish_op;
8225#ifdef FEAT_MBYTE
8226 char_u *arg = NULL;
8227 int l;
8228 char_u *p;
8229#endif
8230
8231 if (ex_normal_busy >= p_mmd)
8232 {
8233 EMSG(_("E192: Recursive use of :normal too deep"));
8234 return;
8235 }
8236 ++ex_normal_busy;
8237
8238 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8239 restart_edit = 0; /* don't go to Insert mode */
8240 p_im = FALSE; /* don't use 'insertmode' */
8241
8242#ifdef FEAT_MBYTE
8243 /*
8244 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8245 * this for the K_SPECIAL leading byte, otherwise special keys will not
8246 * work.
8247 */
8248 if (has_mbyte)
8249 {
8250 int len = 0;
8251
8252 /* Count the number of characters to be escaped. */
8253 for (p = eap->arg; *p != NUL; ++p)
8254 {
8255# ifdef FEAT_GUI
8256 if (*p == CSI) /* leadbyte CSI */
8257 len += 2;
8258# endif
8259 for (l = (*mb_ptr2len_check)(p) - 1; l > 0; --l)
8260 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8261# ifdef FEAT_GUI
8262 || *p == CSI
8263# endif
8264 )
8265 len += 2;
8266 }
8267 if (len > 0)
8268 {
8269 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8270 if (arg != NULL)
8271 {
8272 len = 0;
8273 for (p = eap->arg; *p != NUL; ++p)
8274 {
8275 arg[len++] = *p;
8276# ifdef FEAT_GUI
8277 if (*p == CSI)
8278 {
8279 arg[len++] = KS_EXTRA;
8280 arg[len++] = (int)KE_CSI;
8281 }
8282# endif
8283 for (l = (*mb_ptr2len_check)(p) - 1; l > 0; --l)
8284 {
8285 arg[len++] = *++p;
8286 if (*p == K_SPECIAL)
8287 {
8288 arg[len++] = KS_SPECIAL;
8289 arg[len++] = KE_FILLER;
8290 }
8291# ifdef FEAT_GUI
8292 else if (*p == CSI)
8293 {
8294 arg[len++] = KS_EXTRA;
8295 arg[len++] = (int)KE_CSI;
8296 }
8297# endif
8298 }
8299 arg[len] = NUL;
8300 }
8301 }
8302 }
8303 }
8304#endif
8305
8306 /*
8307 * Save the current typeahead. This is required to allow using ":normal"
8308 * from an event handler and makes sure we don't hang when the argument
8309 * ends with half a command.
8310 */
8311 save_typeahead(&tabuf);
8312 if (tabuf.typebuf_valid)
8313 {
8314 /*
8315 * Repeat the :normal command for each line in the range. When no
8316 * range given, execute it just once, without positioning the cursor
8317 * first.
8318 */
8319 do
8320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 if (eap->addr_count != 0)
8322 {
8323 curwin->w_cursor.lnum = eap->line1++;
8324 curwin->w_cursor.col = 0;
8325 }
8326
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008327 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328#ifdef FEAT_MBYTE
8329 arg != NULL ? arg :
8330#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008331 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 }
8333 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8334 }
8335
8336 /* Might not return to the main loop when in an event handler. */
8337 update_topline_cursor();
8338
8339 /* Restore the previous typeahead. */
8340 restore_typeahead(&tabuf);
8341
8342 --ex_normal_busy;
8343 msg_scroll = save_msg_scroll;
8344 restart_edit = save_restart_edit;
8345 p_im = save_insertmode;
8346 finish_op = save_finish_op;
8347 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8348
8349 /* Restore the state (needed when called from a function executed for
8350 * 'indentexpr'). */
8351 State = save_State;
8352#ifdef FEAT_MBYTE
8353 vim_free(arg);
8354#endif
8355}
8356
8357/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008358 * ":startinsert" and ":startreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 */
8360 static void
8361ex_startinsert(eap)
8362 exarg_T *eap;
8363{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008364 if (eap->forceit)
8365 {
8366 coladvance((colnr_T)MAXCOL);
8367 curwin->w_curswant = MAXCOL;
8368 curwin->w_set_curswant = FALSE;
8369 }
8370
Bram Moolenaara40c5002005-01-09 21:16:21 +00008371 /* Ignore the command when already in Insert mode. Inserting an
8372 * expression register that invokes a function can do this. */
8373 if (State & INSERT)
8374 return;
8375
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 if (eap->forceit)
8377 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008378 if (eap->cmdidx == CMD_startinsert)
8379 restart_edit = 'a';
8380 else
8381 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 }
8383 else
8384 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008385 if (eap->cmdidx == CMD_startinsert)
8386 restart_edit = 'i';
8387 else
8388 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 curwin->w_curswant = 0; /* avoid MAXCOL */
8390 }
8391}
8392
8393/*
8394 * ":stopinsert"
8395 */
8396/*ARGSUSED*/
8397 static void
8398ex_stopinsert(eap)
8399 exarg_T *eap;
8400{
8401 restart_edit = 0;
8402 stop_insert_mode = TRUE;
8403}
8404#endif
8405
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008406#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8407/*
8408 * Execute normal mode command "cmd".
8409 * "remap" can be REMAP_NONE or REMAP_YES.
8410 */
8411 void
8412exec_normal_cmd(cmd, remap, silent)
8413 char_u *cmd;
8414 int remap;
8415 int silent;
8416{
8417 oparg_T oa;
8418
8419 /*
8420 * Stuff the argument into the typeahead buffer.
8421 * Execute normal_cmd() until there is no typeahead left.
8422 */
8423 clear_oparg(&oa);
8424 finish_op = FALSE;
8425 ins_typebuf(cmd, remap, 0, TRUE, silent);
8426 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8427 && !got_int)
8428 {
8429 update_topline_cursor();
8430 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8431 }
8432}
8433#endif
8434
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435#ifdef FEAT_FIND_ID
8436 static void
8437ex_checkpath(eap)
8438 exarg_T *eap;
8439{
8440 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8441 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8442 (linenr_T)1, (linenr_T)MAXLNUM);
8443}
8444
8445#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8446/*
8447 * ":psearch"
8448 */
8449 static void
8450ex_psearch(eap)
8451 exarg_T *eap;
8452{
8453 g_do_tagpreview = p_pvh;
8454 ex_findpat(eap);
8455 g_do_tagpreview = 0;
8456}
8457#endif
8458
8459 static void
8460ex_findpat(eap)
8461 exarg_T *eap;
8462{
8463 int whole = TRUE;
8464 long n;
8465 char_u *p;
8466 int action;
8467
8468 switch (cmdnames[eap->cmdidx].cmd_name[2])
8469 {
8470 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8471 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8472 action = ACTION_GOTO;
8473 else
8474 action = ACTION_SHOW;
8475 break;
8476 case 'i': /* ":ilist" and ":dlist" */
8477 action = ACTION_SHOW_ALL;
8478 break;
8479 case 'u': /* ":ijump" and ":djump" */
8480 action = ACTION_GOTO;
8481 break;
8482 default: /* ":isplit" and ":dsplit" */
8483 action = ACTION_SPLIT;
8484 break;
8485 }
8486
8487 n = 1;
8488 if (vim_isdigit(*eap->arg)) /* get count */
8489 {
8490 n = getdigits(&eap->arg);
8491 eap->arg = skipwhite(eap->arg);
8492 }
8493 if (*eap->arg == '/') /* Match regexp, not just whole words */
8494 {
8495 whole = FALSE;
8496 ++eap->arg;
8497 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8498 if (*p)
8499 {
8500 *p++ = NUL;
8501 p = skipwhite(p);
8502
8503 /* Check for trailing illegal characters */
8504 if (!ends_excmd(*p))
8505 eap->errmsg = e_trailing;
8506 else
8507 eap->nextcmd = check_nextcmd(p);
8508 }
8509 }
8510 if (!eap->skip)
8511 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8512 whole, !eap->forceit,
8513 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8514 n, action, eap->line1, eap->line2);
8515}
8516#endif
8517
8518#ifdef FEAT_WINDOWS
8519
8520# ifdef FEAT_QUICKFIX
8521/*
8522 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8523 */
8524 static void
8525ex_ptag(eap)
8526 exarg_T *eap;
8527{
8528 g_do_tagpreview = p_pvh;
8529 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8530}
8531
8532/*
8533 * ":pedit"
8534 */
8535 static void
8536ex_pedit(eap)
8537 exarg_T *eap;
8538{
8539 win_T *curwin_save = curwin;
8540
8541 g_do_tagpreview = p_pvh;
8542 prepare_tagpreview();
8543 keep_help_flag = curwin_save->w_buffer->b_help;
8544 do_exedit(eap, NULL);
8545 keep_help_flag = FALSE;
8546 if (curwin != curwin_save && win_valid(curwin_save))
8547 {
8548 /* Return cursor to where we were */
8549 validate_cursor();
8550 redraw_later(VALID);
8551 win_enter(curwin_save, TRUE);
8552 }
8553 g_do_tagpreview = 0;
8554}
8555# endif
8556
8557/*
8558 * ":stag", ":stselect" and ":stjump".
8559 */
8560 static void
8561ex_stag(eap)
8562 exarg_T *eap;
8563{
8564 postponed_split = -1;
8565 postponed_split_flags = cmdmod.split;
8566 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8567 postponed_split_flags = 0;
8568}
8569#endif
8570
8571/*
8572 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8573 */
8574 static void
8575ex_tag(eap)
8576 exarg_T *eap;
8577{
8578 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8579}
8580
8581 static void
8582ex_tag_cmd(eap, name)
8583 exarg_T *eap;
8584 char_u *name;
8585{
8586 int cmd;
8587
8588 switch (name[1])
8589 {
8590 case 'j': cmd = DT_JUMP; /* ":tjump" */
8591 break;
8592 case 's': cmd = DT_SELECT; /* ":tselect" */
8593 break;
8594 case 'p': cmd = DT_PREV; /* ":tprevious" */
8595 break;
8596 case 'N': cmd = DT_PREV; /* ":tNext" */
8597 break;
8598 case 'n': cmd = DT_NEXT; /* ":tnext" */
8599 break;
8600 case 'o': cmd = DT_POP; /* ":pop" */
8601 break;
8602 case 'f': /* ":tfirst" */
8603 case 'r': cmd = DT_FIRST; /* ":trewind" */
8604 break;
8605 case 'l': cmd = DT_LAST; /* ":tlast" */
8606 break;
8607 default: /* ":tag" */
8608#ifdef FEAT_CSCOPE
8609 if (p_cst)
8610 {
8611 do_cstag(eap);
8612 return;
8613 }
8614#endif
8615 cmd = DT_TAG;
8616 break;
8617 }
8618
8619 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8620 eap->forceit, TRUE);
8621}
8622
8623/*
8624 * Evaluate cmdline variables.
8625 *
8626 * change '%' to curbuf->b_ffname
8627 * '#' to curwin->w_altfile
8628 * '<cword>' to word under the cursor
8629 * '<cWORD>' to WORD under the cursor
8630 * '<cfile>' to path name under the cursor
8631 * '<sfile>' to sourced file name
8632 * '<afile>' to file name for autocommand
8633 * '<abuf>' to buffer number for autocommand
8634 * '<amatch>' to matching name for autocommand
8635 *
8636 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8637 * "" for error without a message) and NULL is returned.
8638 * Returns an allocated string if a valid match was found.
8639 * Returns NULL if no match was found. "usedlen" then still contains the
8640 * number of characters to skip.
8641 */
8642 char_u *
8643eval_vars(src, usedlen, lnump, errormsg, srcstart)
8644 char_u *src; /* pointer into commandline */
8645 int *usedlen; /* characters after src that are used */
8646 linenr_T *lnump; /* line number for :e command, or NULL */
8647 char_u **errormsg; /* pointer to error message */
8648 char_u *srcstart; /* beginning of valid memory for src */
8649{
8650 int i;
8651 char_u *s;
8652 char_u *result;
8653 char_u *resultbuf = NULL;
8654 int resultlen;
8655 buf_T *buf;
8656 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
8657 int spec_idx;
8658#ifdef FEAT_MODIFY_FNAME
8659 int skip_mod = FALSE;
8660#endif
8661 static char *(spec_str[]) =
8662 {
8663 "%",
8664#define SPEC_PERC 0
8665 "#",
8666#define SPEC_HASH 1
8667 "<cword>", /* cursor word */
8668#define SPEC_CWORD 2
8669 "<cWORD>", /* cursor WORD */
8670#define SPEC_CCWORD 3
8671 "<cfile>", /* cursor path name */
8672#define SPEC_CFILE 4
8673 "<sfile>", /* ":so" file name */
8674#define SPEC_SFILE 5
8675#ifdef FEAT_AUTOCMD
8676 "<afile>", /* autocommand file name */
8677# define SPEC_AFILE 6
8678 "<abuf>", /* autocommand buffer number */
8679# define SPEC_ABUF 7
8680 "<amatch>", /* autocommand match name */
8681# define SPEC_AMATCH 8
8682#endif
8683#ifdef FEAT_CLIENTSERVER
8684 "<client>"
8685# define SPEC_CLIENT 9
8686#endif
8687 };
8688#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
8689
8690#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
8691 char_u strbuf[30];
8692#endif
8693
8694 *errormsg = NULL;
8695
8696 /*
8697 * Check if there is something to do.
8698 */
8699 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
8700 {
8701 *usedlen = (int)STRLEN(spec_str[spec_idx]);
8702 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
8703 break;
8704 }
8705 if (spec_idx == SPEC_COUNT) /* no match */
8706 {
8707 *usedlen = 1;
8708 return NULL;
8709 }
8710
8711 /*
8712 * Skip when preceded with a backslash "\%" and "\#".
8713 * Note: In "\\%" the % is also not recognized!
8714 */
8715 if (src > srcstart && src[-1] == '\\')
8716 {
8717 *usedlen = 0;
8718 STRCPY(src - 1, src); /* remove backslash */
8719 return NULL;
8720 }
8721
8722 /*
8723 * word or WORD under cursor
8724 */
8725 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
8726 {
8727 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
8728 (FIND_IDENT|FIND_STRING) : FIND_STRING);
8729 if (resultlen == 0)
8730 {
8731 *errormsg = (char_u *)"";
8732 return NULL;
8733 }
8734 }
8735
8736 /*
8737 * '#': Alternate file name
8738 * '%': Current file name
8739 * File name under the cursor
8740 * File name for autocommand
8741 * and following modifiers
8742 */
8743 else
8744 {
8745 switch (spec_idx)
8746 {
8747 case SPEC_PERC: /* '%': current file */
8748 if (curbuf->b_fname == NULL)
8749 {
8750 result = (char_u *)"";
8751 valid = 0; /* Must have ":p:h" to be valid */
8752 }
8753 else
8754#ifdef RISCOS
8755 /* Always use the full path for RISC OS if possible. */
8756 result = curbuf->b_ffname;
8757 if (result == NULL)
8758 result = curbuf->b_fname;
8759#else
8760 result = curbuf->b_fname;
8761#endif
8762 break;
8763
8764 case SPEC_HASH: /* '#' or "#99": alternate file */
8765 if (src[1] == '#') /* "##": the argument list */
8766 {
8767 result = arg_all();
8768 resultbuf = result;
8769 *usedlen = 2;
8770#ifdef FEAT_MODIFY_FNAME
8771 skip_mod = TRUE;
8772#endif
8773 break;
8774 }
8775 s = src + 1;
8776 i = (int)getdigits(&s);
8777 *usedlen = (int)(s - src); /* length of what we expand */
8778
8779 buf = buflist_findnr(i);
8780 if (buf == NULL)
8781 {
8782 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
8783 return NULL;
8784 }
8785 if (lnump != NULL)
8786 *lnump = ECMD_LAST;
8787 if (buf->b_fname == NULL)
8788 {
8789 result = (char_u *)"";
8790 valid = 0; /* Must have ":p:h" to be valid */
8791 }
8792 else
8793 result = buf->b_fname;
8794 break;
8795
8796#ifdef FEAT_SEARCHPATH
8797 case SPEC_CFILE: /* file name under cursor */
8798 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L);
8799 if (result == NULL)
8800 {
8801 *errormsg = (char_u *)"";
8802 return NULL;
8803 }
8804 resultbuf = result; /* remember allocated string */
8805 break;
8806#endif
8807
8808#ifdef FEAT_AUTOCMD
8809 case SPEC_AFILE: /* file name for autocommand */
8810 result = autocmd_fname;
8811 if (result == NULL)
8812 {
8813 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
8814 return NULL;
8815 }
8816 break;
8817
8818 case SPEC_ABUF: /* buffer number for autocommand */
8819 if (autocmd_bufnr <= 0)
8820 {
8821 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
8822 return NULL;
8823 }
8824 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8825 result = strbuf;
8826 break;
8827
8828 case SPEC_AMATCH: /* match name for autocommand */
8829 result = autocmd_match;
8830 if (result == NULL)
8831 {
8832 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
8833 return NULL;
8834 }
8835 break;
8836
8837#endif
8838 case SPEC_SFILE: /* file name for ":so" command */
8839 result = sourcing_name;
8840 if (result == NULL)
8841 {
8842 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
8843 return NULL;
8844 }
8845 break;
8846#if defined(FEAT_CLIENTSERVER)
8847 case SPEC_CLIENT: /* Source of last submitted input */
8848 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
8849 result = strbuf;
8850 break;
8851#endif
8852 }
8853
8854 resultlen = (int)STRLEN(result); /* length of new string */
8855 if (src[*usedlen] == '<') /* remove the file name extension */
8856 {
8857 ++*usedlen;
8858#ifdef RISCOS
8859 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
8860#else
8861 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
8862#endif
8863 resultlen = (int)(s - result);
8864 }
8865#ifdef FEAT_MODIFY_FNAME
8866 else if (!skip_mod)
8867 {
8868 valid |= modify_fname(src, usedlen, &result, &resultbuf,
8869 &resultlen);
8870 if (result == NULL)
8871 {
8872 *errormsg = (char_u *)"";
8873 return NULL;
8874 }
8875 }
8876#endif
8877 }
8878
8879 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8880 {
8881 if (valid != VALID_HEAD + VALID_PATH)
8882 /* xgettext:no-c-format */
8883 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
8884 else
8885 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
8886 result = NULL;
8887 }
8888 else
8889 result = vim_strnsave(result, resultlen);
8890 vim_free(resultbuf);
8891 return result;
8892}
8893
8894/*
8895 * Concatenate all files in the argument list, separated by spaces, and return
8896 * it in one allocated string.
8897 * Spaces and backslashes in the file names are escaped with a backslash.
8898 * Returns NULL when out of memory.
8899 */
8900 static char_u *
8901arg_all()
8902{
8903 int len;
8904 int idx;
8905 char_u *retval = NULL;
8906 char_u *p;
8907
8908 /*
8909 * Do this loop two times:
8910 * first time: compute the total length
8911 * second time: concatenate the names
8912 */
8913 for (;;)
8914 {
8915 len = 0;
8916 for (idx = 0; idx < ARGCOUNT; ++idx)
8917 {
8918 p = alist_name(&ARGLIST[idx]);
8919 if (p != NULL)
8920 {
8921 if (len > 0)
8922 {
8923 /* insert a space in between names */
8924 if (retval != NULL)
8925 retval[len] = ' ';
8926 ++len;
8927 }
8928 for ( ; *p != NUL; ++p)
8929 {
8930 if (*p == ' ' || *p == '\\')
8931 {
8932 /* insert a backslash */
8933 if (retval != NULL)
8934 retval[len] = '\\';
8935 ++len;
8936 }
8937 if (retval != NULL)
8938 retval[len] = *p;
8939 ++len;
8940 }
8941 }
8942 }
8943
8944 /* second time: break here */
8945 if (retval != NULL)
8946 {
8947 retval[len] = NUL;
8948 break;
8949 }
8950
8951 /* allocate memory */
8952 retval = alloc(len + 1);
8953 if (retval == NULL)
8954 break;
8955 }
8956
8957 return retval;
8958}
8959
8960#if defined(FEAT_AUTOCMD) || defined(PROTO)
8961/*
8962 * Expand the <sfile> string in "arg".
8963 *
8964 * Returns an allocated string, or NULL for any error.
8965 */
8966 char_u *
8967expand_sfile(arg)
8968 char_u *arg;
8969{
8970 char_u *errormsg;
8971 int len;
8972 char_u *result;
8973 char_u *newres;
8974 char_u *repl;
8975 int srclen;
8976 char_u *p;
8977
8978 result = vim_strsave(arg);
8979 if (result == NULL)
8980 return NULL;
8981
8982 for (p = result; *p; )
8983 {
8984 if (STRNCMP(p, "<sfile>", 7) != 0)
8985 ++p;
8986 else
8987 {
8988 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
8989 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
8990 if (errormsg != NULL)
8991 {
8992 if (*errormsg)
8993 emsg(errormsg);
8994 vim_free(result);
8995 return NULL;
8996 }
8997 if (repl == NULL) /* no match (cannot happen) */
8998 {
8999 p += srclen;
9000 continue;
9001 }
9002 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9003 newres = alloc(len);
9004 if (newres == NULL)
9005 {
9006 vim_free(repl);
9007 vim_free(result);
9008 return NULL;
9009 }
9010 mch_memmove(newres, result, (size_t)(p - result));
9011 STRCPY(newres + (p - result), repl);
9012 len = (int)STRLEN(newres);
9013 STRCAT(newres, p + srclen);
9014 vim_free(repl);
9015 vim_free(result);
9016 result = newres;
9017 p = newres + len; /* continue after the match */
9018 }
9019 }
9020
9021 return result;
9022}
9023#endif
9024
9025#ifdef FEAT_SESSION
9026static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9027static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9028static frame_T *ses_skipframe __ARGS((frame_T *fr));
9029static int ses_do_frame __ARGS((frame_T *fr));
9030static int ses_do_win __ARGS((win_T *wp));
9031static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9032static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9033static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9034
9035/*
9036 * Write openfile commands for the current buffers to an .exrc file.
9037 * Return FAIL on error, OK otherwise.
9038 */
9039 static int
9040makeopens(fd, dirnow)
9041 FILE *fd;
9042 char_u *dirnow; /* Current directory name */
9043{
9044 buf_T *buf;
9045 int only_save_windows = TRUE;
9046 int nr;
9047 int cnr = 1;
9048 int restore_size = TRUE;
9049 win_T *wp;
9050 char_u *sname;
9051 win_T *edited_win = NULL;
9052
9053 if (ssop_flags & SSOP_BUFFERS)
9054 only_save_windows = FALSE; /* Save ALL buffers */
9055
9056 /*
9057 * Begin by setting the this_session variable, and then other
9058 * sessionable variables.
9059 */
9060#ifdef FEAT_EVAL
9061 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9062 return FAIL;
9063 if (ssop_flags & SSOP_GLOBALS)
9064 if (store_session_globals(fd) == FAIL)
9065 return FAIL;
9066#endif
9067
9068 /*
9069 * Close all windows but one.
9070 */
9071 if (put_line(fd, "silent only") == FAIL)
9072 return FAIL;
9073
9074 /*
9075 * Now a :cd command to the session directory or the current directory
9076 */
9077 if (ssop_flags & SSOP_SESDIR)
9078 {
9079 if (put_line(fd, "exe \"cd \" . expand(\"<sfile>:p:h\")") == FAIL)
9080 return FAIL;
9081 }
9082 else if (ssop_flags & SSOP_CURDIR)
9083 {
9084 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9085 if (sname == NULL
9086 || fprintf(fd, "cd %s", sname) < 0 || put_eol(fd) == FAIL)
9087 return FAIL;
9088 vim_free(sname);
9089 }
9090
9091 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009092 * If there is an empty, unnamed buffer we will wipe it out later.
9093 * Remember the buffer number.
9094 */
9095 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9096 return FAIL;
9097 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9098 return FAIL;
9099 if (put_line(fd, "endif") == FAIL)
9100 return FAIL;
9101
9102 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 * Now save the current files, current buffer first.
9104 */
9105 if (put_line(fd, "set shortmess=aoO") == FAIL)
9106 return FAIL;
9107
9108 /* Now put the other buffers into the buffer list */
9109 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9110 {
9111 if (!(only_save_windows && buf->b_nwindows == 0)
9112 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9113 && buf->b_fname != NULL
9114 && buf->b_p_bl)
9115 {
9116 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9117 : buf->b_wininfo->wi_fpos.lnum) < 0
9118 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9119 return FAIL;
9120 }
9121 }
9122
9123 /* the global argument list */
9124 if (ses_arglist(fd, "args", &global_alist.al_ga,
9125 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9126 return FAIL;
9127
9128 if (ssop_flags & SSOP_RESIZE)
9129 {
9130 /* Note: after the restore we still check it worked!*/
9131 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9132 || put_eol(fd) == FAIL)
9133 return FAIL;
9134 }
9135
9136#ifdef FEAT_GUI
9137 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9138 {
9139 int x, y;
9140
9141 if (gui_mch_get_winpos(&x, &y) == OK)
9142 {
9143 /* Note: after the restore we still check it worked!*/
9144 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9145 return FAIL;
9146 }
9147 }
9148#endif
9149
9150 /*
9151 * Before creating the window layout, try loading one file. If this is
9152 * aborted we don't end up with a number of useless windows.
9153 * This may have side effects! (e.g., compressed or network file).
9154 */
9155 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9156 {
9157 if (ses_do_win(wp)
9158 && wp->w_buffer->b_ffname != NULL
9159 && !wp->w_buffer->b_help
9160#ifdef FEAT_QUICKFIX
9161 && !bt_nofile(wp->w_buffer)
9162#endif
9163 )
9164 {
9165 if (fputs("edit ", fd) < 0
9166 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9167 return FAIL;
9168 if (!wp->w_arg_idx_invalid)
9169 edited_win = wp;
9170 break;
9171 }
9172 }
9173
9174 /*
9175 * Save current window layout.
9176 */
9177 if (put_line(fd, "set splitbelow splitright") == FAIL)
9178 return FAIL;
9179 if (ses_win_rec(fd, topframe) == FAIL)
9180 return FAIL;
9181 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9182 return FAIL;
9183 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9184 return FAIL;
9185
9186 /*
9187 * Check if window sizes can be restored (no windows omitted).
9188 * Remember the window number of the current window after restoring.
9189 */
9190 nr = 0;
9191 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9192 {
9193 if (ses_do_win(wp))
9194 ++nr;
9195 else
9196 restore_size = FALSE;
9197 if (curwin == wp)
9198 cnr = nr;
9199 }
9200
9201 /* Go to the first window. */
9202 if (put_line(fd, "wincmd t") == FAIL)
9203 return FAIL;
9204
9205 /*
9206 * If more than one window, see if sizes can be restored.
9207 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9208 * resized when moving between windows.
9209 * Do this before restoring the view, so that the topline and the cursor
9210 * can be set. This is done again below.
9211 */
9212 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9213 return FAIL;
9214 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9215 return FAIL;
9216
9217 /*
9218 * Restore the view of the window (options, file, cursor, etc.).
9219 */
9220 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9221 {
9222 if (!ses_do_win(wp))
9223 continue;
9224 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9225 return FAIL;
9226 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9227 return FAIL;
9228 }
9229
9230 /*
9231 * Restore cursor to the current window if it's not the first one.
9232 */
9233 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9234 return FAIL;
9235
9236 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009237 * Wipe out an empty unnamed buffer we started in.
9238 */
9239 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9240 return FAIL;
9241 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9242 return FAIL;
9243 if (put_line(fd, "endif") == FAIL)
9244 return FAIL;
9245 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9246 return FAIL;
9247
9248 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 * Restore window sizes again after jumping around in windows, because the
9250 * current window has a minimum size while others may not.
9251 */
9252 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9253 return FAIL;
9254
9255 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9256 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9257 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9258 return FAIL;
9259
9260 /*
9261 * Lastly, execute the x.vim file if it exists.
9262 */
9263 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9264 || put_line(fd, "if file_readable(s:sx)") == FAIL
9265 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9266 || put_line(fd, "endif") == FAIL)
9267 return FAIL;
9268
9269 return OK;
9270}
9271
9272 static int
9273ses_winsizes(fd, restore_size)
9274 FILE *fd;
9275 int restore_size;
9276{
9277 int n = 0;
9278 win_T *wp;
9279
9280 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9281 {
9282 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9283 {
9284 if (!ses_do_win(wp))
9285 continue;
9286 ++n;
9287
9288 /* restore height when not full height */
9289 if (wp->w_height + wp->w_status_height < topframe->fr_height
9290 && (fprintf(fd,
9291 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9292 n, (long)wp->w_height, Rows / 2, Rows) < 0
9293 || put_eol(fd) == FAIL))
9294 return FAIL;
9295
9296 /* restore width when not full width */
9297 if (wp->w_width < Columns && (fprintf(fd,
9298 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9299 n, (long)wp->w_width, Columns / 2, Columns) < 0
9300 || put_eol(fd) == FAIL))
9301 return FAIL;
9302 }
9303 }
9304 else
9305 {
9306 /* Just equalise window sizes */
9307 if (put_line(fd, "wincmd =") == FAIL)
9308 return FAIL;
9309 }
9310 return OK;
9311}
9312
9313/*
9314 * Write commands to "fd" to recursively create windows for frame "fr",
9315 * horizontally and vertically split.
9316 * After the commands the last window in the frame is the current window.
9317 * Returns FAIL when writing the commands to "fd" fails.
9318 */
9319 static int
9320ses_win_rec(fd, fr)
9321 FILE *fd;
9322 frame_T *fr;
9323{
9324 frame_T *frc;
9325 int count = 0;
9326
9327 if (fr->fr_layout != FR_LEAF)
9328 {
9329 /* Find first frame that's not skipped and then create a window for
9330 * each following one (first frame is already there). */
9331 frc = ses_skipframe(fr->fr_child);
9332 if (frc != NULL)
9333 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9334 {
9335 /* Make window as big as possible so that we have lots of room
9336 * to split. */
9337 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9338 || put_line(fd, fr->fr_layout == FR_COL
9339 ? "split" : "vsplit") == FAIL)
9340 return FAIL;
9341 ++count;
9342 }
9343
9344 /* Go back to the first window. */
9345 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9346 ? "%dwincmd k" : "%dwincmd h", count) < 0
9347 || put_eol(fd) == FAIL))
9348 return FAIL;
9349
9350 /* Recursively create frames/windows in each window of this column or
9351 * row. */
9352 frc = ses_skipframe(fr->fr_child);
9353 while (frc != NULL)
9354 {
9355 ses_win_rec(fd, frc);
9356 frc = ses_skipframe(frc->fr_next);
9357 /* Go to next window. */
9358 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9359 return FAIL;
9360 }
9361 }
9362 return OK;
9363}
9364
9365/*
9366 * Skip frames that don't contain windows we want to save in the Session.
9367 * Returns NULL when there none.
9368 */
9369 static frame_T *
9370ses_skipframe(fr)
9371 frame_T *fr;
9372{
9373 frame_T *frc;
9374
9375 for (frc = fr; frc != NULL; frc = frc->fr_next)
9376 if (ses_do_frame(frc))
9377 break;
9378 return frc;
9379}
9380
9381/*
9382 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9383 * the Session.
9384 */
9385 static int
9386ses_do_frame(fr)
9387 frame_T *fr;
9388{
9389 frame_T *frc;
9390
9391 if (fr->fr_layout == FR_LEAF)
9392 return ses_do_win(fr->fr_win);
9393 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9394 if (ses_do_frame(frc))
9395 return TRUE;
9396 return FALSE;
9397}
9398
9399/*
9400 * Return non-zero if window "wp" is to be stored in the Session.
9401 */
9402 static int
9403ses_do_win(wp)
9404 win_T *wp;
9405{
9406 if (wp->w_buffer->b_fname == NULL
9407#ifdef FEAT_QUICKFIX
9408 /* When 'buftype' is "nofile" can't restore the window contents. */
9409 || bt_nofile(wp->w_buffer)
9410#endif
9411 )
9412 return (ssop_flags & SSOP_BLANK);
9413 if (wp->w_buffer->b_help)
9414 return (ssop_flags & SSOP_HELP);
9415 return TRUE;
9416}
9417
9418/*
9419 * Write commands to "fd" to restore the view of a window.
9420 * Caller must make sure 'scrolloff' is zero.
9421 */
9422 static int
9423put_view(fd, wp, add_edit, flagp)
9424 FILE *fd;
9425 win_T *wp;
9426 int add_edit; /* add ":edit" command to view */
9427 unsigned *flagp; /* vop_flags or ssop_flags */
9428{
9429 win_T *save_curwin;
9430 int f;
9431 int do_cursor;
9432
9433 /* Always restore cursor position for ":mksession". For ":mkview" only
9434 * when 'viewoptions' contains "cursor". */
9435 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9436
9437 /*
9438 * Local argument list.
9439 */
9440 if (wp->w_alist == &global_alist)
9441 {
9442 if (put_line(fd, "argglobal") == FAIL)
9443 return FAIL;
9444 }
9445 else
9446 {
9447 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9448 flagp == &vop_flags
9449 || !(*flagp & SSOP_CURDIR)
9450 || wp->w_localdir != NULL, flagp) == FAIL)
9451 return FAIL;
9452 }
9453
9454 /* Only when part of a session: restore the argument index. */
9455 if (wp->w_arg_idx != 0 && flagp == &ssop_flags)
9456 {
9457 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9458 || put_eol(fd) == FAIL)
9459 return FAIL;
9460 }
9461
9462 /* Edit the file. Skip this when ":next" already did it. */
9463 if (add_edit && (wp->w_arg_idx == 0 || flagp != &ssop_flags
9464 || wp->w_arg_idx_invalid))
9465 {
9466 /*
9467 * Load the file.
9468 */
9469 if (wp->w_buffer->b_ffname != NULL
9470#ifdef FEAT_QUICKFIX
9471 && !bt_nofile(wp->w_buffer)
9472#endif
9473 )
9474 {
9475 /*
9476 * Editing a file in this buffer: use ":edit file".
9477 * This may have side effects! (e.g., compressed or network file).
9478 */
9479 if (fputs("edit ", fd) < 0
9480 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9481 return FAIL;
9482 }
9483 else
9484 {
9485 /* No file in this buffer, just make it empty. */
9486 if (put_line(fd, "enew") == FAIL)
9487 return FAIL;
9488#ifdef FEAT_QUICKFIX
9489 if (wp->w_buffer->b_ffname != NULL)
9490 {
9491 /* The buffer does have a name, but it's not a file name. */
9492 if (fputs("file ", fd) < 0
9493 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9494 return FAIL;
9495 }
9496#endif
9497 do_cursor = FALSE;
9498 }
9499 }
9500
9501 /*
9502 * Local mappings and abbreviations.
9503 */
9504 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9505 && makemap(fd, wp->w_buffer) == FAIL)
9506 return FAIL;
9507
9508 /*
9509 * Local options. Need to go to the window temporarily.
9510 * Store only local values when using ":mkview" and when ":mksession" is
9511 * used and 'sessionoptions' doesn't include "options".
9512 * Some folding options are always stored when "folds" is included,
9513 * otherwise the folds would not be restored correctly.
9514 */
9515 save_curwin = curwin;
9516 curwin = wp;
9517 curbuf = curwin->w_buffer;
9518 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9519 f = makeset(fd, OPT_LOCAL,
9520 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
9521#ifdef FEAT_FOLDING
9522 else if (*flagp & SSOP_FOLDS)
9523 f = makefoldset(fd);
9524#endif
9525 else
9526 f = OK;
9527 curwin = save_curwin;
9528 curbuf = curwin->w_buffer;
9529 if (f == FAIL)
9530 return FAIL;
9531
9532#ifdef FEAT_FOLDING
9533 /*
9534 * Save Folds when 'buftype' is empty and for help files.
9535 */
9536 if ((*flagp & SSOP_FOLDS)
9537 && wp->w_buffer->b_ffname != NULL
9538 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help))
9539 {
9540 if (put_folds(fd, wp) == FAIL)
9541 return FAIL;
9542 }
9543#endif
9544
9545 /*
9546 * Set the cursor after creating folds, since that moves the cursor.
9547 */
9548 if (do_cursor)
9549 {
9550
9551 /* Restore the cursor line in the file and relatively in the
9552 * window. Don't use "G", it changes the jumplist. */
9553 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
9554 (long)wp->w_cursor.lnum,
9555 (long)(wp->w_cursor.lnum - wp->w_topline),
9556 (long)wp->w_height / 2, (long)wp->w_height) < 0
9557 || put_eol(fd) == FAIL
9558 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
9559 || put_line(fd, "exe s:l") == FAIL
9560 || put_line(fd, "normal! zt") == FAIL
9561 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
9562 || put_eol(fd) == FAIL)
9563 return FAIL;
9564 /* Restore the cursor column and left offset when not wrapping. */
9565 if (wp->w_cursor.col == 0)
9566 {
9567 if (put_line(fd, "normal! 0") == FAIL)
9568 return FAIL;
9569 }
9570 else
9571 {
9572 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
9573 {
9574 if (fprintf(fd,
9575 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
9576 (long)wp->w_cursor.col,
9577 (long)(wp->w_cursor.col - wp->w_leftcol),
9578 (long)wp->w_width / 2, (long)wp->w_width) < 0
9579 || put_eol(fd) == FAIL
9580 || put_line(fd, "if s:c > 0") == FAIL
9581 || fprintf(fd,
9582 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
9583 (long)wp->w_cursor.col) < 0
9584 || put_eol(fd) == FAIL
9585 || put_line(fd, "else") == FAIL
9586 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
9587 || put_eol(fd) == FAIL
9588 || put_line(fd, "endif") == FAIL)
9589 return FAIL;
9590 }
9591 else
9592 {
9593 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
9594 || put_eol(fd) == FAIL)
9595 return FAIL;
9596 }
9597 }
9598 }
9599
9600 /*
9601 * Local directory.
9602 */
9603 if (wp->w_localdir != NULL)
9604 {
9605 if (fputs("lcd ", fd) < 0
9606 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
9607 || put_eol(fd) == FAIL)
9608 return FAIL;
9609 }
9610
9611 return OK;
9612}
9613
9614/*
9615 * Write an argument list to the session file.
9616 * Returns FAIL if writing fails.
9617 */
9618 static int
9619ses_arglist(fd, cmd, gap, fullname, flagp)
9620 FILE *fd;
9621 char *cmd;
9622 garray_T *gap;
9623 int fullname; /* TRUE: use full path name */
9624 unsigned *flagp;
9625{
9626 int i;
9627 char_u buf[MAXPATHL];
9628 char_u *s;
9629
9630 if (gap->ga_len == 0)
9631 return put_line(fd, "silent! argdel *");
9632 if (fputs(cmd, fd) < 0)
9633 return FAIL;
9634 for (i = 0; i < gap->ga_len; ++i)
9635 {
9636 /* NULL file names are skipped (only happens when out of memory). */
9637 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
9638 if (s != NULL)
9639 {
9640 if (fullname)
9641 {
9642 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
9643 s = buf;
9644 }
9645 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
9646 return FAIL;
9647 }
9648 }
9649 return put_eol(fd);
9650}
9651
9652/*
9653 * Write a buffer name to the session file.
9654 * Also ends the line.
9655 * Returns FAIL if writing fails.
9656 */
9657 static int
9658ses_fname(fd, buf, flagp)
9659 FILE *fd;
9660 buf_T *buf;
9661 unsigned *flagp;
9662{
9663 char_u *name;
9664
9665 /* Use the short file name if the current directory is known at the time
9666 * the session file will be sourced. Don't do this for ":mkview", we
9667 * don't know the current directory. */
9668 if (buf->b_sfname != NULL
9669 && flagp == &ssop_flags
9670 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
9671 name = buf->b_sfname;
9672 else
9673 name = buf->b_ffname;
9674 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
9675 return FAIL;
9676 return OK;
9677}
9678
9679/*
9680 * Write a file name to the session file.
9681 * Takes care of the "slash" option in 'sessionoptions' and escapes special
9682 * characters.
9683 * Returns FAIL if writing fails.
9684 */
9685 static int
9686ses_put_fname(fd, name, flagp)
9687 FILE *fd;
9688 char_u *name;
9689 unsigned *flagp;
9690{
9691 char_u *sname;
9692 int retval = OK;
9693 int c;
9694
9695 sname = home_replace_save(NULL, name);
9696 if (sname != NULL)
9697 name = sname;
9698 while (*name != NUL)
9699 {
9700#ifdef FEAT_MBYTE
9701 {
9702 int l;
9703
9704 if (has_mbyte && (l = (*mb_ptr2len_check)(name)) > 1)
9705 {
9706 /* copy a multibyte char */
9707 while (--l >= 0)
9708 {
9709 if (putc(*name, fd) != *name)
9710 retval = FAIL;
9711 ++name;
9712 }
9713 continue;
9714 }
9715 }
9716#endif
9717 c = *name++;
9718 if (c == '\\' && (*flagp & SSOP_SLASH))
9719 /* change a backslash to a forward slash */
9720 c = '/';
9721 else if ((vim_strchr(escape_chars, c) != NULL
9722#ifdef BACKSLASH_IN_FILENAME
9723 && c != '\\'
9724#endif
9725 ) || c == '#' || c == '%')
9726 {
9727 /* escape a special character with a backslash */
9728 if (putc('\\', fd) != '\\')
9729 retval = FAIL;
9730 }
9731 if (putc(c, fd) != c)
9732 retval = FAIL;
9733 }
9734 vim_free(sname);
9735 return retval;
9736}
9737
9738/*
9739 * ":loadview [nr]"
9740 */
9741 static void
9742ex_loadview(eap)
9743 exarg_T *eap;
9744{
9745 char_u *fname;
9746
9747 fname = get_view_file(*eap->arg);
9748 if (fname != NULL)
9749 {
9750 do_source(fname, FALSE, FALSE);
9751 vim_free(fname);
9752 }
9753}
9754
9755/*
9756 * Get the name of the view file for the current buffer.
9757 */
9758 static char_u *
9759get_view_file(c)
9760 int c;
9761{
9762 int len = 0;
9763 char_u *p, *s;
9764 char_u *retval;
9765 char_u *sname;
9766
9767 if (curbuf->b_ffname == NULL)
9768 {
9769 EMSG(_(e_noname));
9770 return NULL;
9771 }
9772 sname = home_replace_save(NULL, curbuf->b_ffname);
9773 if (sname == NULL)
9774 return NULL;
9775
9776 /*
9777 * We want a file name without separators, because we're not going to make
9778 * a directory.
9779 * "normal" path separator -> "=+"
9780 * "=" -> "=="
9781 * ":" path separator -> "=-"
9782 */
9783 for (p = sname; *p; ++p)
9784 if (*p == '=' || vim_ispathsep(*p))
9785 ++len;
9786 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
9787 if (retval != NULL)
9788 {
9789 STRCPY(retval, p_vdir);
9790 add_pathsep(retval);
9791 s = retval + STRLEN(retval);
9792 for (p = sname; *p; ++p)
9793 {
9794 if (*p == '=')
9795 {
9796 *s++ = '=';
9797 *s++ = '=';
9798 }
9799 else if (vim_ispathsep(*p))
9800 {
9801 *s++ = '=';
9802#ifdef MACOS_CLASSIC /* TODO: Is it also needed for MACOS_X? (Dany) */
9803 *s++ = '+';
9804#else
9805# if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
9806 || defined(VMS)
9807 if (*p == ':')
9808 *s++ = '-';
9809 else
9810# endif
9811 *s++ = '+';
9812#endif
9813 }
9814 else
9815 *s++ = *p;
9816 }
9817 *s++ = '=';
9818 *s++ = c;
9819 STRCPY(s, ".vim");
9820 }
9821
9822 vim_free(sname);
9823 return retval;
9824}
9825
9826#endif /* FEAT_SESSION */
9827
9828/*
9829 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
9830 * Return FAIL for a write error.
9831 */
9832 int
9833put_eol(fd)
9834 FILE *fd;
9835{
9836 if (
9837#ifdef USE_CRNL
9838 (
9839# ifdef MKSESSION_NL
9840 !mksession_nl &&
9841# endif
9842 (putc('\r', fd) < 0)) ||
9843#endif
9844 (putc('\n', fd) < 0))
9845 return FAIL;
9846 return OK;
9847}
9848
9849/*
9850 * Write a line to "fd".
9851 * Return FAIL for a write error.
9852 */
9853 int
9854put_line(fd, s)
9855 FILE *fd;
9856 char *s;
9857{
9858 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
9859 return FAIL;
9860 return OK;
9861}
9862
9863#ifdef FEAT_VIMINFO
9864/*
9865 * ":rviminfo" and ":wviminfo".
9866 */
9867 static void
9868ex_viminfo(eap)
9869 exarg_T *eap;
9870{
9871 char_u *save_viminfo;
9872
9873 save_viminfo = p_viminfo;
9874 if (*p_viminfo == NUL)
9875 p_viminfo = (char_u *)"'100";
9876 if (eap->cmdidx == CMD_rviminfo)
9877 {
9878 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
9879 EMSG(_("E195: Cannot open viminfo file for reading"));
9880 }
9881 else
9882 write_viminfo(eap->arg, eap->forceit);
9883 p_viminfo = save_viminfo;
9884}
9885#endif
9886
9887#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
9888 void
9889dialog_msg(buff, format, fname)
9890 char_u *buff;
9891 char *format;
9892 char_u *fname;
9893{
9894 int len;
9895
9896 if (fname == NULL)
9897 fname = (char_u *)_("Untitled");
9898 len = (int)STRLEN(format) + (int)STRLEN(fname);
9899 if (len >= IOSIZE)
9900 sprintf((char *)buff, format, (int)(IOSIZE - STRLEN(format)), fname);
9901 else
9902 sprintf((char *)buff, format, (int)STRLEN(fname), fname);
9903}
9904#endif
9905
9906/*
9907 * ":behave {mswin,xterm}"
9908 */
9909 static void
9910ex_behave(eap)
9911 exarg_T *eap;
9912{
9913 if (STRCMP(eap->arg, "mswin") == 0)
9914 {
9915 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9916 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9917 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9918 set_option_value((char_u *)"keymodel", 0L,
9919 (char_u *)"startsel,stopsel", 0);
9920 }
9921 else if (STRCMP(eap->arg, "xterm") == 0)
9922 {
9923 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9924 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9925 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9926 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9927 }
9928 else
9929 EMSG2(_(e_invarg2), eap->arg);
9930}
9931
9932#ifdef FEAT_AUTOCMD
9933static int filetype_detect = FALSE;
9934static int filetype_plugin = FALSE;
9935static int filetype_indent = FALSE;
9936
9937/*
9938 * ":filetype [plugin] [indent] {on,off,detect}"
9939 * on: Load the filetype.vim file to install autocommands for file types.
9940 * off: Load the ftoff.vim file to remove all autocommands for file types.
9941 * plugin on: load filetype.vim and ftplugin.vim
9942 * plugin off: load ftplugof.vim
9943 * indent on: load filetype.vim and indent.vim
9944 * indent off: load indoff.vim
9945 */
9946 static void
9947ex_filetype(eap)
9948 exarg_T *eap;
9949{
9950 char_u *arg = eap->arg;
9951 int plugin = FALSE;
9952 int indent = FALSE;
9953
9954 if (*eap->arg == NUL)
9955 {
9956 /* Print current status. */
9957 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
9958 filetype_detect ? "ON" : "OFF",
9959 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9960 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9961 return;
9962 }
9963
9964 /* Accept "plugin" and "indent" in any order. */
9965 for (;;)
9966 {
9967 if (STRNCMP(arg, "plugin", 6) == 0)
9968 {
9969 plugin = TRUE;
9970 arg = skipwhite(arg + 6);
9971 continue;
9972 }
9973 if (STRNCMP(arg, "indent", 6) == 0)
9974 {
9975 indent = TRUE;
9976 arg = skipwhite(arg + 6);
9977 continue;
9978 }
9979 break;
9980 }
9981 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9982 {
9983 if (*arg == 'o' || !filetype_detect)
9984 {
9985 cmd_runtime((char_u *)FILETYPE_FILE, TRUE);
9986 filetype_detect = TRUE;
9987 if (plugin)
9988 {
9989 cmd_runtime((char_u *)FTPLUGIN_FILE, TRUE);
9990 filetype_plugin = TRUE;
9991 }
9992 if (indent)
9993 {
9994 cmd_runtime((char_u *)INDENT_FILE, TRUE);
9995 filetype_indent = TRUE;
9996 }
9997 }
9998 if (*arg == 'd')
9999 {
10000 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +000010001 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 }
10003 }
10004 else if (STRCMP(arg, "off") == 0)
10005 {
10006 if (plugin || indent)
10007 {
10008 if (plugin)
10009 {
10010 cmd_runtime((char_u *)FTPLUGOF_FILE, TRUE);
10011 filetype_plugin = FALSE;
10012 }
10013 if (indent)
10014 {
10015 cmd_runtime((char_u *)INDOFF_FILE, TRUE);
10016 filetype_indent = FALSE;
10017 }
10018 }
10019 else
10020 {
10021 cmd_runtime((char_u *)FTOFF_FILE, TRUE);
10022 filetype_detect = FALSE;
10023 }
10024 }
10025 else
10026 EMSG2(_(e_invarg2), arg);
10027}
10028
10029/*
10030 * ":setfiletype {name}"
10031 */
10032 static void
10033ex_setfiletype(eap)
10034 exarg_T *eap;
10035{
10036 if (!did_filetype)
10037 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10038}
10039#endif
10040
10041/*ARGSUSED*/
10042 static void
10043ex_digraphs(eap)
10044 exarg_T *eap;
10045{
10046#ifdef FEAT_DIGRAPHS
10047 if (*eap->arg != NUL)
10048 putdigraph(eap->arg);
10049 else
10050 listdigraphs();
10051#else
10052 EMSG(_("E196: No digraphs in this version"));
10053#endif
10054}
10055
10056 static void
10057ex_set(eap)
10058 exarg_T *eap;
10059{
10060 int flags = 0;
10061
10062 if (eap->cmdidx == CMD_setlocal)
10063 flags = OPT_LOCAL;
10064 else if (eap->cmdidx == CMD_setglobal)
10065 flags = OPT_GLOBAL;
10066#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10067 if (cmdmod.browse && flags == 0)
10068 ex_options(eap);
10069 else
10070#endif
10071 (void)do_set(eap->arg, flags);
10072}
10073
10074#ifdef FEAT_SEARCH_EXTRA
10075/*
10076 * ":nohlsearch"
10077 */
10078/*ARGSUSED*/
10079 static void
10080ex_nohlsearch(eap)
10081 exarg_T *eap;
10082{
10083 no_hlsearch = TRUE;
10084 redraw_all_later(NOT_VALID);
10085}
10086
10087/*
10088 * ":match {group} {pattern}"
10089 * Sets nextcmd to the start of the next command, if any. Also called when
10090 * skipping commands to find the next command.
10091 */
10092 static void
10093ex_match(eap)
10094 exarg_T *eap;
10095{
10096 char_u *p;
10097 char_u *end;
10098 int c;
10099
10100 /* First clear any old pattern. */
10101 if (!eap->skip)
10102 {
10103 vim_free(curwin->w_match.regprog);
10104 curwin->w_match.regprog = NULL;
10105 redraw_later(NOT_VALID); /* always need a redraw */
10106 }
10107
10108 if (ends_excmd(*eap->arg))
10109 end = eap->arg;
10110 else if ((STRNICMP(eap->arg, "none", 4) == 0
10111 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10112 end = eap->arg + 4;
10113 else
10114 {
10115 p = skiptowhite(eap->arg);
10116 if (!eap->skip)
10117 {
10118 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg));
10119 if (curwin->w_match_id == 0)
10120 {
10121 EMSG2(_(e_nogroup), eap->arg);
10122 return;
10123 }
10124 }
10125 p = skipwhite(p);
10126 if (*p == NUL)
10127 {
10128 /* There must be two arguments. */
10129 EMSG2(_(e_invarg2), eap->arg);
10130 return;
10131 }
10132 end = skip_regexp(p + 1, *p, TRUE, NULL);
10133 if (!eap->skip)
10134 {
10135 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10136 {
10137 eap->errmsg = e_trailing;
10138 return;
10139 }
10140
10141 c = *end;
10142 *end = NUL;
10143 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC);
10144 *end = c;
10145 if (curwin->w_match.regprog == NULL)
10146 {
10147 EMSG2(_(e_invarg2), p);
10148 return;
10149 }
10150 }
10151 }
10152 eap->nextcmd = find_nextcmd(end);
10153}
10154#endif
10155
10156#ifdef FEAT_CRYPT
10157/*
10158 * ":X": Get crypt key
10159 */
10160/*ARGSUSED*/
10161 static void
10162ex_X(eap)
10163 exarg_T *eap;
10164{
10165 (void)get_crypt_key(TRUE, TRUE);
10166}
10167#endif
10168
10169#ifdef FEAT_FOLDING
10170 static void
10171ex_fold(eap)
10172 exarg_T *eap;
10173{
10174 if (foldManualAllowed(TRUE))
10175 foldCreate(eap->line1, eap->line2);
10176}
10177
10178 static void
10179ex_foldopen(eap)
10180 exarg_T *eap;
10181{
10182 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10183 eap->forceit, FALSE);
10184}
10185
10186 static void
10187ex_folddo(eap)
10188 exarg_T *eap;
10189{
10190 linenr_T lnum;
10191
10192 /* First set the marks for all lines closed/open. */
10193 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10194 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10195 ml_setmarked(lnum);
10196
10197 /* Execute the command on the marked lines. */
10198 global_exe(eap->arg);
10199 ml_clearmarked(); /* clear rest of the marks */
10200}
10201#endif