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