Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

11275 lines (10411 sloc) 260.513 kb
/* vi:set ts=8 sts=4 sw=4:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* ex_docmd.c: functions for executing an Ex command line.
*/
#include "vim.h"
static int quitmore = 0;
static int ex_pressedreturn = FALSE;
#ifndef FEAT_PRINTER
# define ex_hardcopy ex_ni
#endif
#ifdef FEAT_USR_CMDS
typedef struct ucmd
{
char_u *uc_name; /* The command name */
long_u uc_argt; /* The argument type */
char_u *uc_rep; /* The command's replacement string */
long uc_def; /* The default value for a range/count */
int uc_compl; /* completion type */
# ifdef FEAT_EVAL
scid_T uc_scriptID; /* SID where the command was defined */
# ifdef FEAT_CMDL_COMPL
char_u *uc_compl_arg; /* completion argument if any */
# endif
# endif
} ucmd_T;
#define UC_BUFFER 1 /* -buffer: local to current buffer */
static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
static void do_ucmd __ARGS((exarg_T *eap));
static void ex_command __ARGS((exarg_T *eap));
static void ex_delcommand __ARGS((exarg_T *eap));
# ifdef FEAT_CMDL_COMPL
static char_u *get_user_command_name __ARGS((int idx));
# endif
#else
# define ex_command ex_ni
# define ex_comclear ex_ni
# define ex_delcommand ex_ni
#endif
#ifdef FEAT_EVAL
static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
#else
static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
static int if_level = 0; /* depth in :if */
#endif
static char_u *find_command __ARGS((exarg_T *eap, int *full));
static void ex_abbreviate __ARGS((exarg_T *eap));
static void ex_map __ARGS((exarg_T *eap));
static void ex_unmap __ARGS((exarg_T *eap));
static void ex_mapclear __ARGS((exarg_T *eap));
static void ex_abclear __ARGS((exarg_T *eap));
#ifndef FEAT_MENU
# define ex_emenu ex_ni
# define ex_menu ex_ni
# define ex_menutranslate ex_ni
#endif
#ifdef FEAT_AUTOCMD
static void ex_autocmd __ARGS((exarg_T *eap));
static void ex_doautocmd __ARGS((exarg_T *eap));
#else
# define ex_autocmd ex_ni
# define ex_doautocmd ex_ni
# define ex_doautoall ex_ni
#endif
#ifdef FEAT_LISTCMDS
static void ex_bunload __ARGS((exarg_T *eap));
static void ex_buffer __ARGS((exarg_T *eap));
static void ex_bmodified __ARGS((exarg_T *eap));
static void ex_bnext __ARGS((exarg_T *eap));
static void ex_bprevious __ARGS((exarg_T *eap));
static void ex_brewind __ARGS((exarg_T *eap));
static void ex_blast __ARGS((exarg_T *eap));
#else
# define ex_bunload ex_ni
# define ex_buffer ex_ni
# define ex_bmodified ex_ni
# define ex_bnext ex_ni
# define ex_bprevious ex_ni
# define ex_brewind ex_ni
# define ex_blast ex_ni
# define buflist_list ex_ni
# define ex_checktime ex_ni
#endif
#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
# define ex_buffer_all ex_ni
#endif
static char_u *getargcmd __ARGS((char_u **));
static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
static int getargopt __ARGS((exarg_T *eap));
#ifndef FEAT_QUICKFIX
# define ex_make ex_ni
# define ex_cbuffer ex_ni
# define ex_cc ex_ni
# define ex_cnext ex_ni
# define ex_cfile ex_ni
# define qf_list ex_ni
# define qf_age ex_ni
# define ex_helpgrep ex_ni
# define ex_vimgrep ex_ni
#endif
#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
# define ex_cclose ex_ni
# define ex_copen ex_ni
# define ex_cwindow ex_ni
#endif
#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
# define ex_cexpr ex_ni
#endif
static int check_more __ARGS((int, int));
static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
static void get_flags __ARGS((exarg_T *eap));
#if !defined(FEAT_PERL) \
|| !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
|| !defined(FEAT_TCL) \
|| !defined(FEAT_RUBY) \
|| !defined(FEAT_LUA) \
|| !defined(FEAT_MZSCHEME)
# define HAVE_EX_SCRIPT_NI
static void ex_script_ni __ARGS((exarg_T *eap));
#endif
static char_u *invalid_range __ARGS((exarg_T *eap));
static void correct_range __ARGS((exarg_T *eap));
#ifdef FEAT_QUICKFIX
static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
#endif
static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
static void ex_highlight __ARGS((exarg_T *eap));
static void ex_colorscheme __ARGS((exarg_T *eap));
static void ex_quit __ARGS((exarg_T *eap));
static void ex_cquit __ARGS((exarg_T *eap));
static void ex_quit_all __ARGS((exarg_T *eap));
#ifdef FEAT_WINDOWS
static void ex_close __ARGS((exarg_T *eap));
static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
static void ex_only __ARGS((exarg_T *eap));
static void ex_resize __ARGS((exarg_T *eap));
static void ex_stag __ARGS((exarg_T *eap));
static void ex_tabclose __ARGS((exarg_T *eap));
static void ex_tabonly __ARGS((exarg_T *eap));
static void ex_tabnext __ARGS((exarg_T *eap));
static void ex_tabmove __ARGS((exarg_T *eap));
static void ex_tabs __ARGS((exarg_T *eap));
#else
# define ex_close ex_ni
# define ex_only ex_ni
# define ex_all ex_ni
# define ex_resize ex_ni
# define ex_splitview ex_ni
# define ex_stag ex_ni
# define ex_tabnext ex_ni
# define ex_tabmove ex_ni
# define ex_tabs ex_ni
# define ex_tabclose ex_ni
# define ex_tabonly ex_ni
#endif
#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
static void ex_pclose __ARGS((exarg_T *eap));
static void ex_ptag __ARGS((exarg_T *eap));
static void ex_pedit __ARGS((exarg_T *eap));
#else
# define ex_pclose ex_ni
# define ex_ptag ex_ni
# define ex_pedit ex_ni
#endif
static void ex_hide __ARGS((exarg_T *eap));
static void ex_stop __ARGS((exarg_T *eap));
static void ex_exit __ARGS((exarg_T *eap));
static void ex_print __ARGS((exarg_T *eap));
#ifdef FEAT_BYTEOFF
static void ex_goto __ARGS((exarg_T *eap));
#else
# define ex_goto ex_ni
#endif
static void ex_shell __ARGS((exarg_T *eap));
static void ex_preserve __ARGS((exarg_T *eap));
static void ex_recover __ARGS((exarg_T *eap));
#ifndef FEAT_LISTCMDS
# define ex_argedit ex_ni
# define ex_argadd ex_ni
# define ex_argdelete ex_ni
# define ex_listdo ex_ni
#endif
static void ex_mode __ARGS((exarg_T *eap));
static void ex_wrongmodifier __ARGS((exarg_T *eap));
static void ex_find __ARGS((exarg_T *eap));
static void ex_open __ARGS((exarg_T *eap));
static void ex_edit __ARGS((exarg_T *eap));
#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
# define ex_drop ex_ni
#endif
#ifndef FEAT_GUI
# define ex_gui ex_nogui
static void ex_nogui __ARGS((exarg_T *eap));
#endif
#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
static void ex_tearoff __ARGS((exarg_T *eap));
#else
# define ex_tearoff ex_ni
#endif
#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
static void ex_popup __ARGS((exarg_T *eap));
#else
# define ex_popup ex_ni
#endif
#ifndef FEAT_GUI_MSWIN
# define ex_simalt ex_ni
#endif
#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
# define gui_mch_find_dialog ex_ni
# define gui_mch_replace_dialog ex_ni
#endif
#if !defined(FEAT_GUI_GTK)
# define ex_helpfind ex_ni
#endif
#ifndef FEAT_CSCOPE
# define do_cscope ex_ni
# define do_scscope ex_ni
# define do_cstag ex_ni
#endif
#ifndef FEAT_SYN_HL
# define ex_syntax ex_ni
# define ex_ownsyntax ex_ni
#endif
#ifndef FEAT_SPELL
# define ex_spell ex_ni
# define ex_mkspell ex_ni
# define ex_spelldump ex_ni
# define ex_spellinfo ex_ni
# define ex_spellrepall ex_ni
#endif
#ifndef FEAT_PERSISTENT_UNDO
# define ex_rundo ex_ni
# define ex_wundo ex_ni
#endif
#ifndef FEAT_LUA
# define ex_lua ex_script_ni
# define ex_luado ex_ni
# define ex_luafile ex_ni
#endif
#ifndef FEAT_MZSCHEME
# define ex_mzscheme ex_script_ni
# define ex_mzfile ex_ni
#endif
#ifndef FEAT_PERL
# define ex_perl ex_script_ni
# define ex_perldo ex_ni
#endif
#ifndef FEAT_PYTHON
# define ex_python ex_script_ni
# define ex_pyfile ex_ni
#endif
#ifndef FEAT_PYTHON3
# define ex_py3 ex_script_ni
# define ex_py3file ex_ni
#endif
#ifndef FEAT_TCL
# define ex_tcl ex_script_ni
# define ex_tcldo ex_ni
# define ex_tclfile ex_ni
#endif
#ifndef FEAT_RUBY
# define ex_ruby ex_script_ni
# define ex_rubydo ex_ni
# define ex_rubyfile ex_ni
#endif
#ifndef FEAT_SNIFF
# define ex_sniff ex_ni
#endif
#ifndef FEAT_KEYMAP
# define ex_loadkeymap ex_ni
#endif
static void ex_swapname __ARGS((exarg_T *eap));
static void ex_syncbind __ARGS((exarg_T *eap));
static void ex_read __ARGS((exarg_T *eap));
static void ex_pwd __ARGS((exarg_T *eap));
static void ex_equal __ARGS((exarg_T *eap));
static void ex_sleep __ARGS((exarg_T *eap));
static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
static void ex_winsize __ARGS((exarg_T *eap));
#ifdef FEAT_WINDOWS
static void ex_wincmd __ARGS((exarg_T *eap));
#else
# define ex_wincmd ex_ni
#endif
#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
static void ex_winpos __ARGS((exarg_T *eap));
#else
# define ex_winpos ex_ni
#endif
static void ex_operators __ARGS((exarg_T *eap));
static void ex_put __ARGS((exarg_T *eap));
static void ex_copymove __ARGS((exarg_T *eap));
static void ex_may_print __ARGS((exarg_T *eap));
static void ex_submagic __ARGS((exarg_T *eap));
static void ex_join __ARGS((exarg_T *eap));
static void ex_at __ARGS((exarg_T *eap));
static void ex_bang __ARGS((exarg_T *eap));
static void ex_undo __ARGS((exarg_T *eap));
#ifdef FEAT_PERSISTENT_UNDO
static void ex_wundo __ARGS((exarg_T *eap));
static void ex_rundo __ARGS((exarg_T *eap));
#endif
static void ex_redo __ARGS((exarg_T *eap));
static void ex_later __ARGS((exarg_T *eap));
static void ex_redir __ARGS((exarg_T *eap));
static void ex_redraw __ARGS((exarg_T *eap));
static void ex_redrawstatus __ARGS((exarg_T *eap));
static void close_redir __ARGS((void));
static void ex_mkrc __ARGS((exarg_T *eap));
static void ex_mark __ARGS((exarg_T *eap));
#ifdef FEAT_USR_CMDS
static char_u *uc_fun_cmd __ARGS((void));
static char_u *find_ucmd __ARGS((exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl));
#endif
#ifdef FEAT_EX_EXTRA
static void ex_normal __ARGS((exarg_T *eap));
static void ex_startinsert __ARGS((exarg_T *eap));
static void ex_stopinsert __ARGS((exarg_T *eap));
#else
# define ex_normal ex_ni
# define ex_align ex_ni
# define ex_retab ex_ni
# define ex_startinsert ex_ni
# define ex_stopinsert ex_ni
# define ex_helptags ex_ni
# define ex_sort ex_ni
#endif
#ifdef FEAT_FIND_ID
static void ex_checkpath __ARGS((exarg_T *eap));
static void ex_findpat __ARGS((exarg_T *eap));
#else
# define ex_findpat ex_ni
# define ex_checkpath ex_ni
#endif
#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
static void ex_psearch __ARGS((exarg_T *eap));
#else
# define ex_psearch ex_ni
#endif
static void ex_tag __ARGS((exarg_T *eap));
static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
#ifndef FEAT_EVAL
# define ex_scriptnames ex_ni
# define ex_finish ex_ni
# define ex_echo ex_ni
# define ex_echohl ex_ni
# define ex_execute ex_ni
# define ex_call ex_ni
# define ex_if ex_ni
# define ex_endif ex_ni
# define ex_else ex_ni
# define ex_while ex_ni
# define ex_for ex_ni
# define ex_continue ex_ni
# define ex_break ex_ni
# define ex_endwhile ex_ni
# define ex_endfor ex_ni
# define ex_throw ex_ni
# define ex_try ex_ni
# define ex_catch ex_ni
# define ex_finally ex_ni
# define ex_endtry ex_ni
# define ex_endfunction ex_ni
# define ex_let ex_ni
# define ex_unlet ex_ni
# define ex_lockvar ex_ni
# define ex_unlockvar ex_ni
# define ex_function ex_ni
# define ex_delfunction ex_ni
# define ex_return ex_ni
# define ex_oldfiles ex_ni
#endif
static char_u *arg_all __ARGS((void));
#ifdef FEAT_SESSION
static int makeopens __ARGS((FILE *fd, char_u *dirnow));
static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx));
static void ex_loadview __ARGS((exarg_T *eap));
static char_u *get_view_file __ARGS((int c));
static int did_lcd; /* whether ":lcd" was produced for a session */
#else
# define ex_loadview ex_ni
#endif
#ifndef FEAT_EVAL
# define ex_compiler ex_ni
#endif
#ifdef FEAT_VIMINFO
static void ex_viminfo __ARGS((exarg_T *eap));
#else
# define ex_viminfo ex_ni
#endif
static void ex_behave __ARGS((exarg_T *eap));
#ifdef FEAT_AUTOCMD
static void ex_filetype __ARGS((exarg_T *eap));
static void ex_setfiletype __ARGS((exarg_T *eap));
#else
# define ex_filetype ex_ni
# define ex_setfiletype ex_ni
#endif
#ifndef FEAT_DIFF
# define ex_diffoff ex_ni
# define ex_diffpatch ex_ni
# define ex_diffgetput ex_ni
# define ex_diffsplit ex_ni
# define ex_diffthis ex_ni
# define ex_diffupdate ex_ni
#endif
static void ex_digraphs __ARGS((exarg_T *eap));
static void ex_set __ARGS((exarg_T *eap));
#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
# define ex_options ex_ni
#endif
#ifdef FEAT_SEARCH_EXTRA
static void ex_nohlsearch __ARGS((exarg_T *eap));
static void ex_match __ARGS((exarg_T *eap));
#else
# define ex_nohlsearch ex_ni
# define ex_match ex_ni
#endif
#ifdef FEAT_CRYPT
static void ex_X __ARGS((exarg_T *eap));
#else
# define ex_X ex_ni
#endif
#ifdef FEAT_FOLDING
static void ex_fold __ARGS((exarg_T *eap));
static void ex_foldopen __ARGS((exarg_T *eap));
static void ex_folddo __ARGS((exarg_T *eap));
#else
# define ex_fold ex_ni
# define ex_foldopen ex_ni
# define ex_folddo ex_ni
#endif
#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
&& (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
# define ex_language ex_ni
#endif
#ifndef FEAT_SIGNS
# define ex_sign ex_ni
#endif
#ifndef FEAT_SUN_WORKSHOP
# define ex_wsverb ex_ni
#endif
#ifndef FEAT_NETBEANS_INTG
# define ex_nbclose ex_ni
# define ex_nbkey ex_ni
# define ex_nbstart ex_ni
#endif
#ifndef FEAT_EVAL
# define ex_debug ex_ni
# define ex_breakadd ex_ni
# define ex_debuggreedy ex_ni
# define ex_breakdel ex_ni
# define ex_breaklist ex_ni
#endif
#ifndef FEAT_CMDHIST
# define ex_history ex_ni
#endif
#ifndef FEAT_JUMPLIST
# define ex_jumps ex_ni
# define ex_changes ex_ni
#endif
#ifndef FEAT_PROFILE
# define ex_profile ex_ni
#endif
/*
* Declare cmdnames[].
*/
#define DO_DECLARE_EXCMD
#include "ex_cmds.h"
/*
* Table used to quickly search for a command, based on its first character.
*/
static cmdidx_T cmdidxs[27] =
{
CMD_append,
CMD_buffer,
CMD_change,
CMD_delete,
CMD_edit,
CMD_file,
CMD_global,
CMD_help,
CMD_insert,
CMD_join,
CMD_k,
CMD_list,
CMD_move,
CMD_next,
CMD_open,
CMD_print,
CMD_quit,
CMD_read,
CMD_substitute,
CMD_t,
CMD_undo,
CMD_vglobal,
CMD_write,
CMD_xit,
CMD_yank,
CMD_z,
CMD_bang
};
static char_u dollar_command[2] = {'$', 0};
#ifdef FEAT_EVAL
/* Struct for storing a line inside a while/for loop */
typedef struct
{
char_u *line; /* command line */
linenr_T lnum; /* sourcing_lnum of the line */
} wcmd_T;
/*
* Structure used to store info for line position in a while or for loop.
* This is required, because do_one_cmd() may invoke ex_function(), which
* reads more lines that may come from the while/for loop.
*/
struct loop_cookie
{
garray_T *lines_gap; /* growarray with line info */
int current_line; /* last read line from growarray */
int repeating; /* TRUE when looping a second time */
/* When "repeating" is FALSE use "getline" and "cookie" to get lines */
char_u *(*getline) __ARGS((int, void *, int));
void *cookie;
};
static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
static int store_loop_line __ARGS((garray_T *gap, char_u *line));
static void free_cmdlines __ARGS((garray_T *gap));
/* Struct to save a few things while debugging. Used in do_cmdline() only. */
struct dbg_stuff
{
int trylevel;
int force_abort;
except_T *caught_stack;
char_u *vv_exception;
char_u *vv_throwpoint;
int did_emsg;
int got_int;
int did_throw;
int need_rethrow;
int check_cstack;
except_T *current_exception;
};
static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
static void
save_dbg_stuff(dsp)
struct dbg_stuff *dsp;
{
dsp->trylevel = trylevel; trylevel = 0;
dsp->force_abort = force_abort; force_abort = FALSE;
dsp->caught_stack = caught_stack; caught_stack = NULL;
dsp->vv_exception = v_exception(NULL);
dsp->vv_throwpoint = v_throwpoint(NULL);
/* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
dsp->did_emsg = did_emsg; did_emsg = FALSE;
dsp->got_int = got_int; got_int = FALSE;
dsp->did_throw = did_throw; did_throw = FALSE;
dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
dsp->check_cstack = check_cstack; check_cstack = FALSE;
dsp->current_exception = current_exception; current_exception = NULL;
}
static void
restore_dbg_stuff(dsp)
struct dbg_stuff *dsp;
{
suppress_errthrow = FALSE;
trylevel = dsp->trylevel;
force_abort = dsp->force_abort;
caught_stack = dsp->caught_stack;
(void)v_exception(dsp->vv_exception);
(void)v_throwpoint(dsp->vv_throwpoint);
did_emsg = dsp->did_emsg;
got_int = dsp->got_int;
did_throw = dsp->did_throw;
need_rethrow = dsp->need_rethrow;
check_cstack = dsp->check_cstack;
current_exception = dsp->current_exception;
}
#endif
/*
* do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
* command is given.
*/
void
do_exmode(improved)
int improved; /* TRUE for "improved Ex" mode */
{
int save_msg_scroll;
int prev_msg_row;
linenr_T prev_line;
int changedtick;
if (improved)
exmode_active = EXMODE_VIM;
else
exmode_active = EXMODE_NORMAL;
State = NORMAL;
/* When using ":global /pat/ visual" and then "Q" we return to continue
* the :global command. */
if (global_busy)
return;
save_msg_scroll = msg_scroll;
++RedrawingDisabled; /* don't redisplay the window */
++no_wait_return; /* don't wait for return */
#ifdef FEAT_GUI
/* Ignore scrollbar and mouse events in Ex mode */
++hold_gui_events;
#endif
#ifdef FEAT_SNIFF
want_sniff_request = 0; /* No K_SNIFF wanted */
#endif
MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
while (exmode_active)
{
#ifdef FEAT_EX_EXTRA
/* Check for a ":normal" command and no more characters left. */
if (ex_normal_busy > 0 && typebuf.tb_len == 0)
{
exmode_active = FALSE;
break;
}
#endif
msg_scroll = TRUE;
need_wait_return = FALSE;
ex_pressedreturn = FALSE;
ex_no_reprint = FALSE;
changedtick = curbuf->b_changedtick;
prev_msg_row = msg_row;
prev_line = curwin->w_cursor.lnum;
#ifdef FEAT_SNIFF
ProcessSniffRequests();
#endif
if (improved)
{
cmdline_row = msg_row;
do_cmdline(NULL, getexline, NULL, 0);
}
else
do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
lines_left = Rows - 1;
if ((prev_line != curwin->w_cursor.lnum
|| changedtick != curbuf->b_changedtick) && !ex_no_reprint)
{
if (curbuf->b_ml.ml_flags & ML_EMPTY)
EMSG(_(e_emptybuf));
else
{
if (ex_pressedreturn)
{
/* go up one line, to overwrite the ":<CR>" line, so the
* output doesn't contain empty lines. */
msg_row = prev_msg_row;
if (prev_msg_row == Rows - 1)
msg_row--;
}
msg_col = 0;
print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
msg_clr_eos();
}
}
else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
{
if (curbuf->b_ml.ml_flags & ML_EMPTY)
EMSG(_(e_emptybuf));
else
EMSG(_("E501: At end-of-file"));
}
}
#ifdef FEAT_GUI
--hold_gui_events;
#endif
--RedrawingDisabled;
--no_wait_return;
update_screen(CLEAR);
need_wait_return = FALSE;
msg_scroll = save_msg_scroll;
}
/*
* Execute a simple command line. Used for translated commands like "*".
*/
int
do_cmdline_cmd(cmd)
char_u *cmd;
{
return do_cmdline(cmd, NULL, NULL,
DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
}
/*
* do_cmdline(): execute one Ex command line
*
* 1. Execute "cmdline" when it is not NULL.
* If "cmdline" is NULL, or more lines are needed, fgetline() is used.
* 2. Split up in parts separated with '|'.
*
* This function can be called recursively!
*
* flags:
* DOCMD_VERBOSE - The command will be included in the error message.
* DOCMD_NOWAIT - Don't call wait_return() and friends.
* DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
* DOCMD_KEYTYPED - Don't reset KeyTyped.
* DOCMD_EXCRESET - Reset the exception environment (used for debugging).
* DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
*
* return FAIL if cmdline could not be executed, OK otherwise
*/
int
do_cmdline(cmdline, fgetline, cookie, flags)
char_u *cmdline;
char_u *(*fgetline) __ARGS((int, void *, int));
void *cookie; /* argument for fgetline() */
int flags;
{
char_u *next_cmdline; /* next cmd to execute */
char_u *cmdline_copy = NULL; /* copy of cmd line */
int used_getline = FALSE; /* used "fgetline" to obtain command */
static int recursive = 0; /* recursive depth */
int msg_didout_before_start = 0;
int count = 0; /* line number count */
int did_inc = FALSE; /* incremented RedrawingDisabled */
int retval = OK;
#ifdef FEAT_EVAL
struct condstack cstack; /* conditional stack */
garray_T lines_ga; /* keep lines for ":while"/":for" */
int current_line = 0; /* active line in lines_ga */
char_u *fname = NULL; /* function or script name */
linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
struct dbg_stuff debug_saved; /* saved things for debug mode */
int initial_trylevel;
struct msglist **saved_msg_list = NULL;
struct msglist *private_msg_list;
/* "fgetline" and "cookie" passed to do_one_cmd() */
char_u *(*cmd_getline) __ARGS((int, void *, int));
void *cmd_cookie;
struct loop_cookie cmd_loop_cookie;
void *real_cookie;
int getline_is_func;
#else
# define cmd_getline fgetline
# define cmd_cookie cookie
#endif
static int call_depth = 0; /* recursiveness */
#ifdef FEAT_EVAL
/* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
* location for storing error messages to be converted to an exception.
* This ensures that the do_errthrow() call in do_one_cmd() does not
* combine the messages stored by an earlier invocation of do_one_cmd()
* with the command name of the later one. This would happen when
* BufWritePost autocommands are executed after a write error. */
saved_msg_list = msg_list;
msg_list = &private_msg_list;
private_msg_list = NULL;
#endif
/* It's possible to create an endless loop with ":execute", catch that
* here. The value of 200 allows nested function calls, ":source", etc. */
if (call_depth == 200)
{
EMSG(_("E169: Command too recursive"));
#ifdef FEAT_EVAL
/* When converting to an exception, we do not include the command name
* since this is not an error of the specific command. */
do_errthrow((struct condstack *)NULL, (char_u *)NULL);
msg_list = saved_msg_list;
#endif
return FAIL;
}
++call_depth;
#ifdef FEAT_EVAL
cstack.cs_idx = -1;
cstack.cs_looplevel = 0;
cstack.cs_trylevel = 0;
cstack.cs_emsg_silent_list = NULL;
cstack.cs_lflags = 0;
ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
real_cookie = getline_cookie(fgetline, cookie);
/* Inside a function use a higher nesting level. */
getline_is_func = getline_equal(fgetline, cookie, get_func_line);
if (getline_is_func && ex_nesting_level == func_level(real_cookie))
++ex_nesting_level;
/* Get the function or script name and the address where the next breakpoint
* line and the debug tick for a function or script are stored. */
if (getline_is_func)
{
fname = func_name(real_cookie);
breakpoint = func_breakpoint(real_cookie);
dbg_tick = func_dbg_tick(real_cookie);
}
else if (getline_equal(fgetline, cookie, getsourceline))
{
fname = sourcing_name;
breakpoint = source_breakpoint(real_cookie);
dbg_tick = source_dbg_tick(real_cookie);
}
/*
* Initialize "force_abort" and "suppress_errthrow" at the top level.
*/
if (!recursive)
{
force_abort = FALSE;
suppress_errthrow = FALSE;
}
/*
* If requested, store and reset the global values controlling the
* exception handling (used when debugging). Otherwise clear it to avoid
* a bogus compiler warning when the optimizer uses inline functions...
*/
if (flags & DOCMD_EXCRESET)
save_dbg_stuff(&debug_saved);
else
vim_memset(&debug_saved, 0, 1);
initial_trylevel = trylevel;
/*
* "did_throw" will be set to TRUE when an exception is being thrown.
*/
did_throw = FALSE;
#endif
/*
* "did_emsg" will be set to TRUE when emsg() is used, in which case we
* cancel the whole command line, and any if/endif or loop.
* If force_abort is set, we cancel everything.
*/
did_emsg = FALSE;
/*
* KeyTyped is only set when calling vgetc(). Reset it here when not
* calling vgetc() (sourced command lines).
*/
if (!(flags & DOCMD_KEYTYPED)
&& !getline_equal(fgetline, cookie, getexline))
KeyTyped = FALSE;
/*
* Continue executing command lines:
* - when inside an ":if", ":while" or ":for"
* - for multiple commands on one line, separated with '|'
* - when repeating until there are no more lines (for ":source")
*/
next_cmdline = cmdline;
do
{
#ifdef FEAT_EVAL
getline_is_func = getline_equal(fgetline, cookie, get_func_line);
#endif
/* stop skipping cmds for an error msg after all endif/while/for */
if (next_cmdline == NULL
#ifdef FEAT_EVAL
&& !force_abort
&& cstack.cs_idx < 0
&& !(getline_is_func && func_has_abort(real_cookie))
#endif
)
did_emsg = FALSE;
/*
* 1. If repeating a line in a loop, get a line from lines_ga.
* 2. If no line given: Get an allocated line with fgetline().
* 3. If a line is given: Make a copy, so we can mess with it.
*/
#ifdef FEAT_EVAL
/* 1. If repeating, get a previous line from lines_ga. */
if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
{
/* Each '|' separated command is stored separately in lines_ga, to
* be able to jump to it. Don't use next_cmdline now. */
vim_free(cmdline_copy);
cmdline_copy = NULL;
/* Check if a function has returned or, unless it has an unclosed
* try conditional, aborted. */
if (getline_is_func)
{
# ifdef FEAT_PROFILE
if (do_profiling == PROF_YES)
func_line_end(real_cookie);
# endif
if (func_has_ended(real_cookie))
{
retval = FAIL;
break;
}
}
#ifdef FEAT_PROFILE
else if (do_profiling == PROF_YES
&& getline_equal(fgetline, cookie, getsourceline))
script_line_end();
#endif
/* Check if a sourced file hit a ":finish" command. */
if (source_finished(fgetline, cookie))
{
retval = FAIL;
break;
}
/* If breakpoints have been added/deleted need to check for it. */
if (breakpoint != NULL && dbg_tick != NULL
&& *dbg_tick != debug_tick)
{
*breakpoint = dbg_find_breakpoint(
getline_equal(fgetline, cookie, getsourceline),
fname, sourcing_lnum);
*dbg_tick = debug_tick;
}
next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
/* Did we encounter a breakpoint? */
if (breakpoint != NULL && *breakpoint != 0
&& *breakpoint <= sourcing_lnum)
{
dbg_breakpoint(fname, sourcing_lnum);
/* Find next breakpoint. */
*breakpoint = dbg_find_breakpoint(
getline_equal(fgetline, cookie, getsourceline),
fname, sourcing_lnum);
*dbg_tick = debug_tick;
}
# ifdef FEAT_PROFILE
if (do_profiling == PROF_YES)
{
if (getline_is_func)
func_line_start(real_cookie);
else if (getline_equal(fgetline, cookie, getsourceline))
script_line_start();
}
# endif
}
if (cstack.cs_looplevel > 0)
{
/* Inside a while/for loop we need to store the lines and use them
* again. Pass a different "fgetline" function to do_one_cmd()
* below, so that it stores lines in or reads them from
* "lines_ga". Makes it possible to define a function inside a
* while/for loop. */
cmd_getline = get_loop_line;
cmd_cookie = (void *)&cmd_loop_cookie;
cmd_loop_cookie.lines_gap = &lines_ga;
cmd_loop_cookie.current_line = current_line;
cmd_loop_cookie.getline = fgetline;
cmd_loop_cookie.cookie = cookie;
cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
}
else
{
cmd_getline = fgetline;
cmd_cookie = cookie;
}
#endif
/* 2. If no line given, get an allocated line with fgetline(). */
if (next_cmdline == NULL)
{
/*
* Need to set msg_didout for the first line after an ":if",
* otherwise the ":if" will be overwritten.
*/
if (count == 1 && getline_equal(fgetline, cookie, getexline))
msg_didout = TRUE;
if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
#ifdef FEAT_EVAL
cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
#else
0
#endif
)) == NULL)
{
/* Don't call wait_return for aborted command line. The NULL
* returned for the end of a sourced file or executed function
* doesn't do this. */
if (KeyTyped && !(flags & DOCMD_REPEAT))
need_wait_return = FALSE;
retval = FAIL;
break;
}
used_getline = TRUE;
/*
* Keep the first typed line. Clear it when more lines are typed.
*/
if (flags & DOCMD_KEEPLINE)
{
vim_free(repeat_cmdline);
if (count == 0)
repeat_cmdline = vim_strsave(next_cmdline);
else
repeat_cmdline = NULL;
}
}
/* 3. Make a copy of the command so we can mess with it. */
else if (cmdline_copy == NULL)
{
next_cmdline = vim_strsave(next_cmdline);
if (next_cmdline == NULL)
{
EMSG(_(e_outofmem));
retval = FAIL;
break;
}
}
cmdline_copy = next_cmdline;
#ifdef FEAT_EVAL
/*
* Save the current line when inside a ":while" or ":for", and when
* the command looks like a ":while" or ":for", because we may need it
* later. When there is a '|' and another command, it is stored
* separately, because we need to be able to jump back to it from an
* :endwhile/:endfor.
*/
if (current_line == lines_ga.ga_len
&& (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
{
if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
{
retval = FAIL;
break;
}
}
did_endif = FALSE;
#endif
if (count++ == 0)
{
/*
* All output from the commands is put below each other, without
* waiting for a return. Don't do this when executing commands
* from a script or when being called recursive (e.g. for ":e
* +command file").
*/
if (!(flags & DOCMD_NOWAIT) && !recursive)
{
msg_didout_before_start = msg_didout;
msg_didany = FALSE; /* no output yet */
msg_start();
msg_scroll = TRUE; /* put messages below each other */
++no_wait_return; /* dont wait for return until finished */
++RedrawingDisabled;
did_inc = TRUE;
}
}
if (p_verbose >= 15 && sourcing_name != NULL)
{
++no_wait_return;
verbose_enter_scroll();
smsg((char_u *)_("line %ld: %s"),
(long)sourcing_lnum, cmdline_copy);
if (msg_silent == 0)
msg_puts((char_u *)"\n"); /* don't overwrite this */
verbose_leave_scroll();
--no_wait_return;
}
/*
* 2. Execute one '|' separated command.
* do_one_cmd() will return NULL if there is no trailing '|'.
* "cmdline_copy" can change, e.g. for '%' and '#' expansion.
*/
++recursive;
next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
#ifdef FEAT_EVAL
&cstack,
#endif
cmd_getline, cmd_cookie);
--recursive;
#ifdef FEAT_EVAL
if (cmd_cookie == (void *)&cmd_loop_cookie)
/* Use "current_line" from "cmd_loop_cookie", it may have been
* incremented when defining a function. */
current_line = cmd_loop_cookie.current_line;
#endif
if (next_cmdline == NULL)
{
vim_free(cmdline_copy);
cmdline_copy = NULL;
#ifdef FEAT_CMDHIST
/*
* If the command was typed, remember it for the ':' register.
* Do this AFTER executing the command to make :@: work.
*/
if (getline_equal(fgetline, cookie, getexline)
&& new_last_cmdline != NULL)
{
vim_free(last_cmdline);
last_cmdline = new_last_cmdline;
new_last_cmdline = NULL;
}
#endif
}
else
{
/* need to copy the command after the '|' to cmdline_copy, for the
* next do_one_cmd() */
STRMOVE(cmdline_copy, next_cmdline);
next_cmdline = cmdline_copy;
}
#ifdef FEAT_EVAL
/* reset did_emsg for a function that is not aborted by an error */
if (did_emsg && !force_abort
&& getline_equal(fgetline, cookie, get_func_line)
&& !func_has_abort(real_cookie))
did_emsg = FALSE;
if (cstack.cs_looplevel > 0)
{
++current_line;
/*
* An ":endwhile", ":endfor" and ":continue" is handled here.
* If we were executing commands, jump back to the ":while" or
* ":for".
* If we were not executing commands, decrement cs_looplevel.
*/
if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
{
cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
/* Jump back to the matching ":while" or ":for". Be careful
* not to use a cs_line[] from an entry that isn't a ":while"
* or ":for": It would make "current_line" invalid and can
* cause a crash. */
if (!did_emsg && !got_int && !did_throw
&& cstack.cs_idx >= 0
&& (cstack.cs_flags[cstack.cs_idx]
& (CSF_WHILE | CSF_FOR))
&& cstack.cs_line[cstack.cs_idx] >= 0
&& (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
{
current_line = cstack.cs_line[cstack.cs_idx];
/* remember we jumped there */
cstack.cs_lflags |= CSL_HAD_LOOP;
line_breakcheck(); /* check if CTRL-C typed */
/* Check for the next breakpoint at or after the ":while"
* or ":for". */
if (breakpoint != NULL)
{
*breakpoint = dbg_find_breakpoint(
getline_equal(fgetline, cookie, getsourceline),
fname,
((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
*dbg_tick = debug_tick;
}
}
else
{
/* can only get here with ":endwhile" or ":endfor" */
if (cstack.cs_idx >= 0)
rewind_conditionals(&cstack, cstack.cs_idx - 1,
CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
}
}
/*
* For a ":while" or ":for" we need to remember the line number.
*/
else if (cstack.cs_lflags & CSL_HAD_LOOP)
{
cstack.cs_lflags &= ~CSL_HAD_LOOP;
cstack.cs_line[cstack.cs_idx] = current_line - 1;
}
}
/*
* When not inside any ":while" loop, clear remembered lines.
*/
if (cstack.cs_looplevel == 0)
{
if (lines_ga.ga_len > 0)
{
sourcing_lnum =
((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
free_cmdlines(&lines_ga);
}
current_line = 0;
}
/*
* A ":finally" makes did_emsg, got_int, and did_throw pending for
* being restored at the ":endtry". Reset them here and set the
* ACTIVE and FINALLY flags, so that the finally clause gets executed.
* This includes the case where a missing ":endif", ":endwhile" or
* ":endfor" was detected by the ":finally" itself.
*/
if (cstack.cs_lflags & CSL_HAD_FINA)
{
cstack.cs_lflags &= ~CSL_HAD_FINA;
report_make_pending(cstack.cs_pending[cstack.cs_idx]
& (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
did_throw ? (void *)current_exception : NULL);
did_emsg = got_int = did_throw = FALSE;
cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
}
/* Update global "trylevel" for recursive calls to do_cmdline() from
* within this loop. */
trylevel = initial_trylevel + cstack.cs_trylevel;
/*
* If the outermost try conditional (across function calls and sourced
* files) is aborted because of an error, an interrupt, or an uncaught
* exception, cancel everything. If it is left normally, reset
* force_abort to get the non-EH compatible abortion behavior for
* the rest of the script.
*/
if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
force_abort = FALSE;
/* Convert an interrupt to an exception if appropriate. */
(void)do_intthrow(&cstack);
#endif /* FEAT_EVAL */
}
/*
* Continue executing command lines when:
* - no CTRL-C typed, no aborting error, no exception thrown or try
* conditionals need to be checked for executing finally clauses or
* catching an interrupt exception
* - didn't get an error message or lines are not typed
* - there is a command after '|', inside a :if, :while, :for or :try, or
* looping for ":source" command or function call.
*/
while (!((got_int
#ifdef FEAT_EVAL
|| (did_emsg && force_abort) || did_throw
#endif
)
#ifdef FEAT_EVAL
&& cstack.cs_trylevel == 0
#endif
)
&& !(did_emsg && used_getline
&& (getline_equal(fgetline, cookie, getexmodeline)
|| getline_equal(fgetline, cookie, getexline)))
&& (next_cmdline != NULL
#ifdef FEAT_EVAL
|| cstack.cs_idx >= 0
#endif
|| (flags & DOCMD_REPEAT)));
vim_free(cmdline_copy);
#ifdef FEAT_EVAL
free_cmdlines(&lines_ga);
ga_clear(&lines_ga);
if (cstack.cs_idx >= 0)
{
/*
* If a sourced file or executed function ran to its end, report the
* unclosed conditional.
*/
if (!got_int && !did_throw
&& ((getline_equal(fgetline, cookie, getsourceline)
&& !source_finished(fgetline, cookie))
|| (getline_equal(fgetline, cookie, get_func_line)
&& !func_has_ended(real_cookie))))
{
if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
EMSG(_(e_endtry));
else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
EMSG(_(e_endwhile));
else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
EMSG(_(e_endfor));
else
EMSG(_(e_endif));
}
/*
* Reset "trylevel" in case of a ":finish" or ":return" or a missing
* ":endtry" in a sourced file or executed function. If the try
* conditional is in its finally clause, ignore anything pending.
* If it is in a catch clause, finish the caught exception.
* Also cleanup any "cs_forinfo" structures.
*/
do
{
int idx = cleanup_conditionals(&cstack, 0, TRUE);
if (idx >= 0)
--idx; /* remove try block not in its finally clause */
rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
&cstack.cs_looplevel);
}
while (cstack.cs_idx >= 0);
trylevel = initial_trylevel;
}
/* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
* lack was reported above and the error message is to be converted to an
* exception, do this now after rewinding the cstack. */
do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
? (char_u *)"endfunction" : (char_u *)NULL);
if (trylevel == 0)
{
/*
* When an exception is being thrown out of the outermost try
* conditional, discard the uncaught exception, disable the conversion
* of interrupts or errors to exceptions, and ensure that no more
* commands are executed.
*/
if (did_throw)
{
void *p = NULL;
char_u *saved_sourcing_name;
int saved_sourcing_lnum;
struct msglist *messages = NULL, *next;
/*
* If the uncaught exception is a user exception, report it as an
* error. If it is an error exception, display the saved error
* message now. For an interrupt exception, do nothing; the
* interrupt message is given elsewhere.
*/
switch (current_exception->type)
{
case ET_USER:
vim_snprintf((char *)IObuff, IOSIZE,
_("E605: Exception not caught: %s"),
current_exception->value);
p = vim_strsave(IObuff);
break;
case ET_ERROR:
messages = current_exception->messages;
current_exception->messages = NULL;
break;
case ET_INTERRUPT:
break;
default:
p = vim_strsave((char_u *)_(e_internal));
}
saved_sourcing_name = sourcing_name;
saved_sourcing_lnum = sourcing_lnum;
sourcing_name = current_exception->throw_name;
sourcing_lnum = current_exception->throw_lnum;
current_exception->throw_name = NULL;
discard_current_exception(); /* uses IObuff if 'verbose' */
suppress_errthrow = TRUE;
force_abort = TRUE;
if (messages != NULL)
{
do
{
next = messages->next;
emsg(messages->msg);
vim_free(messages->msg);
vim_free(messages);
messages = next;
}
while (messages != NULL);
}
else if (p != NULL)
{
emsg(p);
vim_free(p);
}
vim_free(sourcing_name);
sourcing_name = saved_sourcing_name;
sourcing_lnum = saved_sourcing_lnum;
}
/*
* On an interrupt or an aborting error not converted to an exception,
* disable the conversion of errors to exceptions. (Interrupts are not
* converted any more, here.) This enables also the interrupt message
* when force_abort is set and did_emsg unset in case of an interrupt
* from a finally clause after an error.
*/
else if (got_int || (did_emsg && force_abort))
suppress_errthrow = TRUE;
}
/*
* The current cstack will be freed when do_cmdline() returns. An uncaught
* exception will have to be rethrown in the previous cstack. If a function
* has just returned or a script file was just finished and the previous
* cstack belongs to the same function or, respectively, script file, it
* will have to be checked for finally clauses to be executed due to the
* ":return" or ":finish". This is done in do_one_cmd().
*/
if (did_throw)
need_rethrow = TRUE;
if ((getline_equal(fgetline, cookie, getsourceline)
&& ex_nesting_level > source_level(real_cookie))
|| (getline_equal(fgetline, cookie, get_func_line)
&& ex_nesting_level > func_level(real_cookie) + 1))
{
if (!did_throw)
check_cstack = TRUE;
}
else
{
/* When leaving a function, reduce nesting level. */
if (getline_equal(fgetline, cookie, get_func_line))
--ex_nesting_level;
/*
* Go to debug mode when returning from a function in which we are
* single-stepping.
*/
if ((getline_equal(fgetline, cookie, getsourceline)
|| getline_equal(fgetline, cookie, get_func_line))
&& ex_nesting_level + 1 <= debug_break_level)
do_debug(getline_equal(fgetline, cookie, getsourceline)
? (char_u *)_("End of sourced file")
: (char_u *)_("End of function"));
}
/*
* Restore the exception environment (done after returning from the
* debugger).
*/
if (flags & DOCMD_EXCRESET)
restore_dbg_stuff(&debug_saved);
msg_list = saved_msg_list;
#endif /* FEAT_EVAL */
/*
* If there was too much output to fit on the command line, ask the user to
* hit return before redrawing the screen. With the ":global" command we do
* this only once after the command is finished.
*/
if (did_inc)
{
--RedrawingDisabled;
--no_wait_return;
msg_scroll = FALSE;
/*
* When just finished an ":if"-":else" which was typed, no need to
* wait for hit-return. Also for an error situation.
*/
if (retval == FAIL
#ifdef FEAT_EVAL
|| (did_endif && KeyTyped && !did_emsg)
#endif
)
{
need_wait_return = FALSE;
msg_didany = FALSE; /* don't wait when restarting edit */
}
else if (need_wait_return)
{
/*
* The msg_start() above clears msg_didout. The wait_return we do
* here should not overwrite the command that may be shown before
* doing that.
*/
msg_didout |= msg_didout_before_start;
wait_return(FALSE);
}
}
#ifndef FEAT_EVAL
/*
* Reset if_level, in case a sourced script file contains more ":if" than
* ":endif" (could be ":if x | foo | endif").
*/
if_level = 0;
#endif
--call_depth;
return retval;
}
#ifdef FEAT_EVAL
/*
* Obtain a line when inside a ":while" or ":for" loop.
*/
static char_u *
get_loop_line(c, cookie, indent)
int c;
void *cookie;
int indent;
{
struct loop_cookie *cp = (struct loop_cookie *)cookie;
wcmd_T *wp;
char_u *line;
if (cp->current_line + 1 >= cp->lines_gap->ga_len)
{
if (cp->repeating)
return NULL; /* trying to read past ":endwhile"/":endfor" */
/* First time inside the ":while"/":for": get line normally. */
if (cp->getline == NULL)
line = getcmdline(c, 0L, indent);
else
line = cp->getline(c, cp->cookie, indent);
if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
++cp->current_line;
return line;
}
KeyTyped = FALSE;
++cp->current_line;
wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
sourcing_lnum = wp->lnum;
return vim_strsave(wp->line);
}
/*
* Store a line in "gap" so that a ":while" loop can execute it again.
*/
static int
store_loop_line(gap, line)
garray_T *gap;
char_u *line;
{
if (ga_grow(gap, 1) == FAIL)
return FAIL;
((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
++gap->ga_len;
return OK;
}
/*
* Free the lines stored for a ":while" or ":for" loop.
*/
static void
free_cmdlines(gap)
garray_T *gap;
{
while (gap->ga_len > 0)
{
vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
--gap->ga_len;
}
}
#endif
/*
* If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
* "func". * Otherwise return TRUE when "fgetline" equals "func".
*/
int
getline_equal(fgetline, cookie, func)
char_u *(*fgetline) __ARGS((int, void *, int));
void *cookie UNUSED; /* argument for fgetline() */
char_u *(*func) __ARGS((int, void *, int));
{
#ifdef FEAT_EVAL
char_u *(*gp) __ARGS((int, void *, int));
struct loop_cookie *cp;
/* When "fgetline" is "get_loop_line()" use the "cookie" to find the
* function that's originally used to obtain the lines. This may be
* nested several levels. */
gp = fgetline;
cp = (struct loop_cookie *)cookie;
while (gp == get_loop_line)
{
gp = cp->getline;
cp = cp->cookie;
}
return gp == func;
#else
return fgetline == func;
#endif
}
#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
/*
* If "fgetline" is get_loop_line(), return the cookie used by the original
* getline function. Otherwise return "cookie".
*/
void *
getline_cookie(fgetline, cookie)
char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
void *cookie; /* argument for fgetline() */
{
# ifdef FEAT_EVAL
char_u *(*gp) __ARGS((int, void *, int));
struct loop_cookie *cp;
/* When "fgetline" is "get_loop_line()" use the "cookie" to find the
* cookie that's originally used to obtain the lines. This may be nested
* several levels. */
gp = fgetline;
cp = (struct loop_cookie *)cookie;
while (gp == get_loop_line)
{
gp = cp->getline;
cp = cp->cookie;
}
return cp;
# else
return cookie;
# endif
}
#endif
/*
* Execute one Ex command.
*
* If 'sourcing' is TRUE, the command will be included in the error message.
*
* 1. skip comment lines and leading space
* 2. handle command modifiers
* 3. parse range
* 4. parse command
* 5. parse arguments
* 6. switch on command name
*
* Note: "fgetline" can be NULL.
*
* This function may be called recursively!
*/
#if (_MSC_VER == 1200)
/*
* Avoid optimisation bug in VC++ version 6.0
*/
#pragma optimize( "g", off )
#endif
static char_u *
do_one_cmd(cmdlinep, sourcing,
#ifdef FEAT_EVAL
cstack,
#endif
fgetline, cookie)
char_u **cmdlinep;
int sourcing;
#ifdef FEAT_EVAL
struct condstack *cstack;
#endif
char_u *(*fgetline) __ARGS((int, void *, int));
void *cookie; /* argument for fgetline() */
{
char_u *p;
linenr_T lnum;
long n;
char_u *errormsg = NULL; /* error message */
exarg_T ea; /* Ex command arguments */
long verbose_save = -1;
int save_msg_scroll = msg_scroll;
int save_msg_silent = -1;
int did_esilent = 0;
#ifdef HAVE_SANDBOX
int did_sandbox = FALSE;
#endif
cmdmod_T save_cmdmod;
int ni; /* set when Not Implemented */
vim_memset(&ea, 0, sizeof(ea));
ea.line1 = 1;
ea.line2 = 1;
#ifdef FEAT_EVAL
++ex_nesting_level;
#endif
/* when not editing the last file :q has to be typed twice */
if (quitmore
#ifdef FEAT_EVAL
/* avoid that a function call in 'statusline' does this */
&& !getline_equal(fgetline, cookie, get_func_line)
#endif
)
--quitmore;
/*
* Reset browse, confirm, etc.. They are restored when returning, for
* recursive calls.
*/
save_cmdmod = cmdmod;
vim_memset(&cmdmod, 0, sizeof(cmdmod));
/* "#!anything" is handled like a comment. */
if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
goto doend;
/*
* Repeat until no more command modifiers are found.
*/
ea.cmd = *cmdlinep;
for (;;)
{
/*
* 1. skip comment lines and leading white space and colons
*/
while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
++ea.cmd;
/* in ex mode, an empty line works like :+ */
if (*ea.cmd == NUL && exmode_active
&& (getline_equal(fgetline, cookie, getexmodeline)
|| getline_equal(fgetline, cookie, getexline))
&& curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
{
ea.cmd = (char_u *)"+";
ex_pressedreturn = TRUE;
}
/* ignore comment and empty lines */
if (*ea.cmd == '"')
goto doend;
if (*ea.cmd == NUL)
{
ex_pressedreturn = TRUE;
goto doend;
}
/*
* 2. handle command modifiers.
*/
p = ea.cmd;
if (VIM_ISDIGIT(*ea.cmd))
p = skipwhite(skipdigits(ea.cmd));
switch (*p)
{
/* When adding an entry, also modify cmd_exists(). */
case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
break;
#ifdef FEAT_WINDOWS
cmdmod.split |= WSP_ABOVE;
#endif
continue;
case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
{
#ifdef FEAT_WINDOWS
cmdmod.split |= WSP_BELOW;
#endif
continue;
}
if (checkforcmd(&ea.cmd, "browse", 3))
{
#ifdef FEAT_BROWSE_CMD
cmdmod.browse = TRUE;
#endif
continue;
}
if (!checkforcmd(&ea.cmd, "botright", 2))
break;
#ifdef FEAT_WINDOWS
cmdmod.split |= WSP_BOT;
#endif
continue;
case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
break;
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
cmdmod.confirm = TRUE;
#endif
continue;
case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
{
cmdmod.keepmarks = TRUE;
continue;
}
if (checkforcmd(&ea.cmd, "keepalt", 5))
{
cmdmod.keepalt = TRUE;
continue;
}
if (!checkforcmd(&ea.cmd, "keepjumps", 5))
break;
cmdmod.keepjumps = TRUE;
continue;
/* ":hide" and ":hide | cmd" are not modifiers */
case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
|| *p == NUL || ends_excmd(*p))
break;
ea.cmd = p;
cmdmod.hide = TRUE;
continue;
case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
{
cmdmod.lockmarks = TRUE;
continue;
}
if (!checkforcmd(&ea.cmd, "leftabove", 5))
break;
#ifdef FEAT_WINDOWS
cmdmod.split |= WSP_ABOVE;
#endif
continue;
case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
break;
#ifdef FEAT_AUTOCMD
if (cmdmod.save_ei == NULL)
{
/* Set 'eventignore' to "all". Restore the
* existing option value later. */
cmdmod.save_ei = vim_strsave(p_ei);
set_string_option_direct((char_u *)"ei", -1,
(char_u *)"all", OPT_FREE, SID_NONE);
}
#endif
continue;
case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
break;
#ifdef FEAT_WINDOWS
cmdmod.split |= WSP_BELOW;
#endif
continue;
case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
{
#ifdef HAVE_SANDBOX
if (!did_sandbox)
++sandbox;
did_sandbox = TRUE;
#endif
continue;
}
if (!checkforcmd(&ea.cmd, "silent", 3))
break;
if (save_msg_silent == -1)
save_msg_silent = msg_silent;
++msg_silent;
if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
{
/* ":silent!", but not "silent !cmd" */
ea.cmd = skipwhite(ea.cmd + 1);
++emsg_silent;
++did_esilent;
}
continue;
case 't': if (checkforcmd(&p, "tab", 3))
{
#ifdef FEAT_WINDOWS
if (vim_isdigit(*ea.cmd))
cmdmod.tab = atoi((char *)ea.cmd) + 1;
else
cmdmod.tab = tabpage_index(curtab) + 1;
ea.cmd = p;
#endif
continue;
}
if (!checkforcmd(&ea.cmd, "topleft", 2))
break;
#ifdef FEAT_WINDOWS
cmdmod.split |= WSP_TOP;
#endif
continue;
case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
break;
if (save_msg_silent == -1)
save_msg_silent = msg_silent;
msg_silent = 0;
continue;
case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
{
#ifdef FEAT_VERTSPLIT
cmdmod.split |= WSP_VERT;
#endif
continue;
}
if (!checkforcmd(&p, "verbose", 4))
break;
if (verbose_save < 0)
verbose_save = p_verbose;
if (vim_isdigit(*ea.cmd))
p_verbose = atoi((char *)ea.cmd);
else
p_verbose = 1;
ea.cmd = p;
continue;
}
break;
}
#ifdef FEAT_EVAL
ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
&& !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
#else
ea.skip = (if_level > 0);
#endif
#ifdef FEAT_EVAL
# ifdef FEAT_PROFILE
/* Count this line for profiling if ea.skip is FALSE. */
if (do_profiling == PROF_YES && !ea.skip)
{
if (getline_equal(fgetline, cookie, get_func_line))
func_line_exec(getline_cookie(fgetline, cookie));
else if (getline_equal(fgetline, cookie, getsourceline))
script_line_exec();
}
#endif
/* May go to debug mode. If this happens and the ">quit" debug command is
* used, throw an interrupt exception and skip the next command. */
dbg_check_breakpoint(&ea);
if (!ea.skip && got_int)
{
ea.skip = TRUE;
(void)do_intthrow(cstack);
}
#endif
/*
* 3. parse a range specifier of the form: addr [,addr] [;addr] ..
*
* where 'addr' is:
*
* % (entire file)
* $ [+-NUM]
* 'x [+-NUM] (where x denotes a currently defined mark)
* . [+-NUM]
* [+-NUM]..
* NUM
*
* The ea.cmd pointer is updated to point to the first character following the
* range spec. If an initial address is found, but no second, the upper bound
* is equal to the lower.
*/
/* repeat for all ',' or ';' separated addresses */
for (;;)
{
ea.line1 = ea.line2;
ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
ea.cmd = skipwhite(ea.cmd);
lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
if (ea.cmd == NULL) /* error detected */
goto doend;
if (lnum == MAXLNUM)
{
if (*ea.cmd == '%') /* '%' - all lines */
{
++ea.cmd;
ea.line1 = 1;
ea.line2 = curbuf->b_ml.ml_line_count;
++ea.addr_count;
}
/* '*' - visual area */
else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
{
pos_T *fp;
++ea.cmd;
if (!ea.skip)
{
fp = getmark('<', FALSE);
if (check_mark(fp) == FAIL)
goto doend;
ea.line1 = fp->lnum;
fp = getmark('>', FALSE);
if (check_mark(fp) == FAIL)
goto doend;
ea.line2 = fp->lnum;
++ea.addr_count;
}
}
}
else
ea.line2 = lnum;
ea.addr_count++;
if (*ea.cmd == ';')
{
if (!ea.skip)
curwin->w_cursor.lnum = ea.line2;
}
else if (*ea.cmd != ',')
break;
++ea.cmd;
}
/* One address given: set start and end lines */
if (ea.addr_count == 1)
{
ea.line1 = ea.line2;
/* ... but only implicit: really no address given */
if (lnum == MAXLNUM)
ea.addr_count = 0;
}
/* Don't leave the cursor on an illegal line (caused by ';') */
check_cursor_lnum();
/*
* 4. parse command
*/
/*
* Skip ':' and any white space
*/
ea.cmd = skipwhite(ea.cmd);
while (*ea.cmd == ':')
ea.cmd = skipwhite(ea.cmd + 1);
/*
* If we got a line, but no command, then go to the line.
* If we find a '|' or '\n' we set ea.nextcmd.
*/
if (*ea.cmd == NUL || *ea.cmd == '"' ||
(ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
{
/*
* strange vi behaviour:
* ":3" jumps to line 3
* ":3|..." prints line 3
* ":|" prints current line
*/
if (ea.skip) /* skip this if inside :if */
goto doend;
if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
{
ea.cmdidx = CMD_print;
ea.argt = RANGE+COUNT+TRLBAR;
if ((errormsg = invalid_range(&ea)) == NULL)
{
correct_range(&ea);
ex_print(&ea);
}
}
else if (ea.addr_count != 0)
{
if (ea.line2 > curbuf->b_ml.ml_line_count)
{
/* With '-' in 'cpoptions' a line number past the file is an
* error, otherwise put it at the end of the file. */
if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
ea.line2 = -1;
else
ea.line2 = curbuf->b_ml.ml_line_count;
}
if (ea.line2 < 0)
errormsg = (char_u *)_(e_invrange);
else
{
if (ea.line2 == 0)
curwin->w_cursor.lnum = 1;
else
curwin->w_cursor.lnum = ea.line2;
beginline(BL_SOL | BL_FIX);
}
}
goto doend;
}
/* Find the command and let "p" point to after it. */
p = find_command(&ea, NULL);
#ifdef FEAT_USR_CMDS
if (p == NULL)
{
if (!ea.skip)
errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
goto doend;
}
/* Check for wrong commands. */
if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
{
errormsg = uc_fun_cmd();
goto doend;
}
#endif
if (ea.cmdidx == CMD_SIZE)
{
if (!ea.skip)
{
STRCPY(IObuff, _("E492: Not an editor command"));
if (!sourcing)
{
STRCAT(IObuff, ": ");
STRNCAT(IObuff, *cmdlinep, 40);
}
errormsg = IObuff;
}
goto doend;
}
ni = (
#ifdef FEAT_USR_CMDS
!USER_CMDIDX(ea.cmdidx) &&
#endif
(cmdnames[ea.cmdidx].cmd_func == ex_ni
#ifdef HAVE_EX_SCRIPT_NI
|| cmdnames[ea.cmdidx].cmd_func == ex_script_ni
#endif
));
#ifndef FEAT_EVAL
/*
* When the expression evaluation is disabled, recognize the ":if" and
* ":endif" commands and ignore everything in between it.
*/
if (ea.cmdidx == CMD_if)
++if_level;
if (if_level)
{
if (ea.cmdidx == CMD_endif)
--if_level;
goto doend;
}
#endif
/* forced commands */
if (*p == '!' && ea.cmdidx != CMD_substitute
&& ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
{
++p;
ea.forceit = TRUE;
}
else
ea.forceit = FALSE;
/*
* 5. parse arguments
*/
#ifdef FEAT_USR_CMDS
if (!USER_CMDIDX(ea.cmdidx))
#endif
ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
if (!ea.skip)
{
#ifdef HAVE_SANDBOX
if (sandbox != 0 && !(ea.argt & SBOXOK))
{
/* Command not allowed in sandbox. */
errormsg = (char_u *)_(e_sandbox);
goto doend;
}
#endif
if (!curbuf->b_p_ma && (ea.argt & MODIFY))
{
/* Command not allowed in non-'modifiable' buffer */
errormsg = (char_u *)_(e_modifiable);
goto doend;
}
if (text_locked() && !(ea.argt & CMDWIN)
# ifdef FEAT_USR_CMDS
&& !USER_CMDIDX(ea.cmdidx)
# endif
)
{
/* Command not allowed when editing the command line. */
#ifdef FEAT_CMDWIN
if (cmdwin_type != 0)
errormsg = (char_u *)_(e_cmdwin);
else
#endif
errormsg = (char_u *)_(e_secure);
goto doend;
}
#ifdef FEAT_AUTOCMD
/* Disallow editing another buffer when "curbuf_lock" is set.
* Do allow ":edit" (check for argument later).
* Do allow ":checktime" (it's postponed). */
if (!(ea.argt & CMDWIN)
&& ea.cmdidx != CMD_edit
&& ea.cmdidx != CMD_checktime
# ifdef FEAT_USR_CMDS
&& !USER_CMDIDX(ea.cmdidx)
# endif
&& curbuf_locked())
goto doend;
#endif
if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
{
/* no range allowed */
errormsg = (char_u *)_(e_norange);
goto doend;
}
}
if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
{
errormsg = (char_u *)_(e_nobang);
goto doend;
}
/*
* Don't complain about the range if it is not used
* (could happen if line_count is accidentally set to 0).
*/
if (!ea.skip && !ni)
{
/*
* If the range is backwards, ask for confirmation and, if given, swap
* ea.line1 & ea.line2 so it's forwards again.
* When global command is busy, don't ask, will fail below.
*/
if (!global_busy && ea.line1 > ea.line2)
{
if (msg_silent == 0)
{
if (sourcing || exmode_active)
{
errormsg = (char_u *)_("E493: Backwards range given");
goto doend;
}
if (ask_yesno((char_u *)
_("Backwards range given, OK to swap"), FALSE) != 'y')
goto doend;
}
lnum = ea.line1;
ea.line1 = ea.line2;
ea.line2 = lnum;
}
if ((errormsg = invalid_range(&ea)) != NULL)
goto doend;
}
if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
ea.line2 = 1;
correct_range(&ea);
#ifdef FEAT_FOLDING
if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
{
/* Put the first line at the start of a closed fold, put the last line
* at the end of a closed fold. */
(void)hasFolding(ea.line1, &ea.line1, NULL);
(void)hasFolding(ea.line2, NULL, &ea.line2);
}
#endif
#ifdef FEAT_QUICKFIX
/*
* For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
* option here, so things like % get expanded.
*/
p = replace_makeprg(&ea, p, cmdlinep);
if (p == NULL)
goto doend;
#endif
/*
* Skip to start of argument.
* Don't do this for the ":!" command, because ":!! -l" needs the space.
*/
if (ea.cmdidx == CMD_bang)
ea.arg = p;
else
ea.arg = skipwhite(p);
/*
* Check for "++opt=val" argument.
* Must be first, allow ":w ++enc=utf8 !cmd"
*/
if (ea.argt & ARGOPT)
while (ea.arg[0] == '+' && ea.arg[1] == '+')
if (getargopt(&ea) == FAIL && !ni)
{
errormsg = (char_u *)_(e_invarg);
goto doend;
}
if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
{
if (*ea.arg == '>') /* append */
{
if (*++ea.arg != '>') /* typed wrong */
{
errormsg = (char_u *)_("E494: Use w or w>>");
goto doend;
}
ea.arg = skipwhite(ea.arg + 1);
ea.append = TRUE;
}
else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
{
++ea.arg;
ea.usefilter = TRUE;
}
}
if (ea.cmdidx == CMD_read)
{
if (ea.forceit)
{
ea.usefilter = TRUE; /* :r! filter if ea.forceit */
ea.forceit = FALSE;
}
else if (*ea.arg == '!') /* :r !filter */
{
++ea.arg;
ea.usefilter = TRUE;
}
}
if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
{
ea.amount = 1;
while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
{
++ea.arg;
++ea.amount;
}
ea.arg = skipwhite(ea.arg);
}
/*
* Check for "+command" argument, before checking for next command.
* Don't do this for ":read !cmd" and ":write !cmd".
*/
if ((ea.argt & EDITCMD) && !ea.usefilter)
ea.do_ecmd_cmd = getargcmd(&ea.arg);
/*
* Check for '|' to separate commands and '"' to start comments.
* Don't do this for ":read !cmd" and ":write !cmd".
*/
if ((ea.argt & TRLBAR) && !ea.usefilter)
separate_nextcmd(&ea);
/*
* Check for <newline> to end a shell command.
* Also do this for ":read !cmd", ":write !cmd" and ":global".
* Any others?
*/
else if (ea.cmdidx == CMD_bang
|| ea.cmdidx == CMD_global
|| ea.cmdidx == CMD_vglobal
|| ea.usefilter)
{
for (p = ea.arg; *p; ++p)
{
/* Remove one backslash before a newline, so that it's possible to
* pass a newline to the shell and also a newline that is preceded
* with a backslash. This makes it impossible to end a shell
* command in a backslash, but that doesn't appear useful.
* Halving the number of backslashes is incompatible with previous
* versions. */
if (*p == '\\' && p[1] == '\n')
STRMOVE(p, p + 1);
else if (*p == '\n')
{
ea.nextcmd = p + 1;
*p = NUL;
break;
}
}
}
if ((ea.argt & DFLALL) && ea.addr_count == 0)
{
ea.line1 = 1;
ea.line2 = curbuf->b_ml.ml_line_count;
}
/* accept numbered register only when no count allowed (:put) */
if ( (ea.argt & REGSTR)
&& *ea.arg != NUL
#ifdef FEAT_USR_CMDS
&& valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
&& USER_CMDIDX(ea.cmdidx)))
/* Do not allow register = for user commands */
&& (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
#else
&& valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
#endif
&& !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
{
ea.regname = *ea.arg++;
#ifdef FEAT_EVAL
/* for '=' register: accept the rest of the line as an expression */
if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
{
set_expr_line(vim_strsave(ea.arg));
ea.arg += STRLEN(ea.arg);
}
#endif
ea.arg = skipwhite(ea.arg);
}
/*
* Check for a count. When accepting a BUFNAME, don't use "123foo" as a
* count, it's a buffer name.
*/
if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
&& (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
|| vim_iswhite(*p)))
{
n = getdigits(&ea.arg);
ea.arg = skipwhite(ea.arg);
if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
{
errormsg = (char_u *)_(e_zerocount);
goto doend;
}
if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
{
ea.line2 = n;
if (ea.addr_count == 0)
ea.addr_count = 1;
}
else
{
ea.line1 = ea.line2;
ea.line2 += n - 1;
++ea.addr_count;
/*
* Be vi compatible: no error message for out of range.
*/
if (ea.line2 > curbuf->b_ml.ml_line_count)
ea.line2 = curbuf->b_ml.ml_line_count;
}
}
/*
* Check for flags: 'l', 'p' and '#'.
*/
if (ea.argt & EXFLAGS)
get_flags(&ea);
/* no arguments allowed */
if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
&& *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
{
errormsg = (char_u *)_(e_trailing);
goto doend;
}
if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
{
errormsg = (char_u *)_(e_argreq);
goto doend;
}
#ifdef FEAT_EVAL
/*
* Skip the command when it's not going to be executed.
* The commands like :if, :endif, etc. always need to be executed.
* Also make an exception for commands that handle a trailing command
* themselves.
*/
if (ea.skip)
{
switch (ea.cmdidx)
{
/* commands that need evaluation */
case CMD_while:
case CMD_endwhile:
case CMD_for:
case CMD_endfor:
case CMD_if:
case CMD_elseif:
case CMD_else:
case CMD_endif:
case CMD_try:
case CMD_catch:
case CMD_finally:
case CMD_endtry:
case CMD_function:
break;
/* Commands that handle '|' themselves. Check: A command should
* either have the TRLBAR flag, appear in this list or appear in
* the list at ":help :bar". */
case CMD_aboveleft:
case CMD_and:
case CMD_belowright:
case CMD_botright:
case CMD_browse:
case CMD_call:
case CMD_confirm:
case CMD_delfunction:
case CMD_djump:
case CMD_dlist:
case CMD_dsearch:
case CMD_dsplit:
case CMD_echo:
case CMD_echoerr:
case CMD_echomsg:
case CMD_echon:
case CMD_execute:
case CMD_help:
case CMD_hide:
case CMD_ijump:
case CMD_ilist:
case CMD_isearch:
case CMD_isplit:
case CMD_keepalt:
case CMD_keepjumps:
case CMD_keepmarks:
case CMD_leftabove:
case CMD_let:
case CMD_lockmarks:
case CMD_lua:
case CMD_match:
case CMD_mzscheme:
case CMD_perl:
case CMD_psearch:
case CMD_python:
case CMD_py3:
case CMD_python3:
case CMD_return:
case CMD_rightbelow:
case CMD_ruby:
case CMD_silent:
case CMD_smagic:
case CMD_snomagic:
case CMD_substitute:
case CMD_syntax:
case CMD_tab:
case CMD_tcl:
case CMD_throw:
case CMD_tilde:
case CMD_topleft:
case CMD_unlet:
case CMD_verbose:
case CMD_vertical:
break;
default: goto doend;
}
}
#endif
if (ea.argt & XFILE)
{
if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
goto doend;
}
#ifdef FEAT_LISTCMDS
/*
* Accept buffer name. Cannot be used at the same time with a buffer
* number. Don't do this for a user command.
*/
if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
# ifdef FEAT_USR_CMDS
&& !USER_CMDIDX(ea.cmdidx)
# endif
)
{
/*
* :bdelete, :bwipeout and :bunload take several arguments, separated
* by spaces: find next space (skipping over escaped characters).
* The others take one argument: ignore trailing spaces.
*/
if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
|| ea.cmdidx == CMD_bunload)
p = skiptowhite_esc(ea.arg);
else
{
p = ea.arg + STRLEN(ea.arg);
while (p > ea.arg && vim_iswhite(p[-1]))
--p;
}
ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
if (ea.line2 < 0) /* failed */
goto doend;
ea.addr_count = 1;
ea.arg = skipwhite(p);
}
#endif
/*
* 6. switch on command name
*
* The "ea" structure holds the arguments that can be used.
*/
ea.cmdlinep = cmdlinep;
ea.getline = fgetline;
ea.cookie = cookie;
#ifdef FEAT_EVAL
ea.cstack = cstack;
#endif
#ifdef FEAT_USR_CMDS
if (USER_CMDIDX(ea.cmdidx))
{
/*
* Execute a user-defined command.
*/
do_ucmd(&ea);
}
else
#endif
{
/*
* Call the function to execute the command.
*/
ea.errmsg = NULL;
(cmdnames[ea.cmdidx].cmd_func)(&ea);
if (ea.errmsg != NULL)
errormsg = (char_u *)_(ea.errmsg);
}
#ifdef FEAT_EVAL
/*
* If the command just executed called do_cmdline(), any throw or ":return"
* or ":finish" encountered there must also check the cstack of the still
* active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
* exception, or reanimate a returned function or finished script file and
* return or finish it again.
*/
if (need_rethrow)
do_throw(cstack);
else if (check_cstack)
{
if (source_finished(fgetline, cookie))
do_finish(&ea, TRUE);
else if (getline_equal(fgetline, cookie, get_func_line)
&& current_func_returned())
do_return(&ea, TRUE, FALSE, NULL);
}
need_rethrow = check_cstack = FALSE;
#endif
doend:
if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
curwin->w_cursor.lnum = 1;
if (errormsg != NULL && *errormsg != NUL && !did_emsg)
{
if (sourcing)
{
if (errormsg != IObuff)
{
STRCPY(IObuff, errormsg);
errormsg = IObuff;
}
STRCAT(errormsg, ": ");
STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
}
emsg(errormsg);
}
#ifdef FEAT_EVAL
do_errthrow(cstack,
(ea.cmdidx != CMD_SIZE
# ifdef FEAT_USR_CMDS
&& !USER_CMDIDX(ea.cmdidx)
# endif
) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
#endif
if (verbose_save >= 0)
p_verbose = verbose_save;
#ifdef FEAT_AUTOCMD
if (cmdmod.save_ei != NULL)
{
/* Restore 'eventignore' to the value before ":noautocmd". */
set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
OPT_FREE, SID_NONE);
free_string_option(cmdmod.save_ei);
}
#endif
cmdmod = save_cmdmod;
if (save_msg_silent != -1)
{
/* messages could be enabled for a serious error, need to check if the
* counters don't become negative */
if (!did_emsg || msg_silent > save_msg_silent)
msg_silent = save_msg_silent;
emsg_silent -= did_esilent;
if (emsg_silent < 0)
emsg_silent = 0;
/* Restore msg_scroll, it's set by file I/O commands, even when no
* message is actually displayed. */
msg_scroll = save_msg_scroll;
/* "silent reg" or "silent echo x" inside "redir" leaves msg_col
* somewhere in the line. Put it back in the first column. */
if (redirecting())
msg_col = 0;
}
#ifdef HAVE_SANDBOX
if (did_sandbox)
--sandbox;
#endif
if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
ea.nextcmd = NULL;
#ifdef FEAT_EVAL
--ex_nesting_level;
#endif
return ea.nextcmd;
}
#if (_MSC_VER == 1200)
#pragma optimize( "", on )
#endif
/*
* Check for an Ex command with optional tail.
* If there is a match advance "pp" to the argument and return TRUE.
*/
int
checkforcmd(pp, cmd, len)
char_u **pp; /* start of command */
char *cmd; /* name of command */
int len; /* required length */
{
int i;
for (i = 0; cmd[i] != NUL; ++i)
if (((char_u *)cmd)[i] != (*pp)[i])
break;
if (i >= len && !isalpha((*pp)[i]))
{
*pp = skipwhite(*pp + i);
return TRUE;
}
return FALSE;
}
/*
* Find an Ex command by its name, either built-in or user.
* Start of the name can be found at eap->cmd.
* Returns pointer to char after the command name.
* "full" is set to TRUE if the whole command name matched.
* Returns NULL for an ambiguous user command.
*/
static char_u *
find_command(eap, full)
exarg_T *eap;
int *full UNUSED;
{
int len;
char_u *p;
int i;
/*
* Isolate the command and search for it in the command table.
* Exceptions:
* - the 'k' command can directly be followed by any character.
* - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
* but :sre[wind] is another command, as are :scrip[tnames],
* :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
* - the "d" command can directly be followed by 'l' or 'p' flag.
*/
p = eap->cmd;
if (*p == 'k')
{
eap->cmdidx = CMD_k;
++p;
}
else if (p[0] == 's'
&& ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
&& p[3] != 'i' && p[4] != 'p')
|| p[1] == 'g'
|| (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
|| p[1] == 'I'
|| (p[1] == 'r' && p[2] != 'e')))
{
eap->cmdidx = CMD_substitute;
++p;
}
else
{
while (ASCII_ISALPHA(*p))
++p;
/* for python 3.x support ":py3", ":python3", ":py3file", etc. */
if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
while (ASCII_ISALNUM(*p))
++p;
/* check for non-alpha command */
if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
++p;
len = (int)(p - eap->cmd);
if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
{
/* Check for ":dl", ":dell", etc. to ":deletel": that's
* :delete with the 'l' flag. Same for 'p'. */
for (i = 0; i < len; ++i)
if (eap->cmd[i] != ((char_u *)"delete")[i])
break;
if (i == len - 1)
{
--len;
if (p[-1] == 'l')
eap->flags |= EXFLAG_LIST;
else
eap->flags |= EXFLAG_PRINT;
}
}
if (ASCII_ISLOWER(*eap->cmd))
eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
else
eap->cmdidx = cmdidxs[26];
for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
(size_t)len) == 0)
{
#ifdef FEAT_EVAL
if (full != NULL
&& cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
*full = TRUE;
#endif
break;
}
#ifdef FEAT_USR_CMDS
/* Look for a user defined command as a last resort. Let ":Print" be
* overruled by a user defined command. */
if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
&& *eap->cmd >= 'A' && *eap->cmd <= 'Z')
{
/* User defined commands may contain digits. */
while (ASCII_ISALNUM(*p))
++p;
p = find_ucmd(eap, p, full, NULL, NULL);
}
#endif
if (p == eap->cmd)
eap->cmdidx = CMD_SIZE;
}
return p;
}
#ifdef FEAT_USR_CMDS
/*
* Search for a user command that matches "eap->cmd".
* Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
* Return a pointer to just after the command.
* Return NULL if there is no matching command.
*/
static char_u *
find_ucmd(eap, p, full, xp, compl)
exarg_T *eap;
char_u *p; /* end of the command (possibly including count) */
int *full; /* set to TRUE for a full match */
expand_T *xp; /* used for completion, NULL otherwise */
int *compl; /* completion flags or NULL */
{
int len = (int)(p - eap->cmd);
int j, k, matchlen = 0;
ucmd_T *uc;
int found = FALSE;
int possible = FALSE;
char_u *cp, *np; /* Point into typed cmd and test name */
garray_T *gap;
int amb_local = FALSE; /* Found ambiguous buffer-local command,
only full match global is accepted. */
/*
* Look for buffer-local user commands first, then global ones.
*/
gap = &curbuf->b_ucmds;
for (;;)
{
for (j = 0; j < gap->ga_len; ++j)
{
uc = USER_CMD_GA(gap, j);
cp = eap->cmd;
np = uc->uc_name;
k = 0;
while (k < len && *np != NUL && *cp++ == *np++)
k++;
if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
{
/* If finding a second match, the command is ambiguous. But
* not if a buffer-local command wasn't a full match and a
* global command is a full match. */
if (k == len && found && *np != NUL)
{
if (gap == &ucmds)
return NULL;
amb_local = TRUE;
}
if (!found || (k == len && *np == NUL))
{
/* If we matched up to a digit, then there could
* be another command including the digit that we
* should use instead.
*/
if (k == len)
found = TRUE;
else
possible = TRUE;
if (gap == &ucmds)
eap->cmdidx = CMD_USER;
else
eap->cmdidx = CMD_USER_BUF;
eap->argt = (long)uc->uc_argt;
eap->useridx = j;
# ifdef FEAT_CMDL_COMPL
if (compl != NULL)
*compl = uc->uc_compl;
# ifdef FEAT_EVAL
if (xp != NULL)
{
xp->xp_arg = uc->uc_compl_arg;
xp->xp_scriptID = uc->uc_scriptID;
}
# endif
# endif
/* Do not search for further abbreviations
* if this is an exact match. */
matchlen = k;
if (k == len && *np == NUL)
{
if (full != NULL)
*full = TRUE;
amb_local = FALSE;
break;
}
}
}
}
/* Stop if we found a full match or searched all. */
if (j < gap->ga_len || gap == &ucmds)
break;
gap = &ucmds;
}
/* Only found ambiguous matches. */
if (amb_local)
{
if (xp != NULL)
xp->xp_context = EXPAND_UNSUCCESSFUL;
return NULL;
}
/* The match we found may be followed immediately by a number. Move "p"
* back to point to it. */
if (found || possible)
return p + (matchlen - len);
return p;
}
#endif
#if defined(FEAT_EVAL) || defined(PROTO)
static struct cmdmod
{
char *name;
int minlen;
int has_count; /* :123verbose :3tab */
} cmdmods[] = {
{"aboveleft", 3, FALSE},
{"belowright", 3, FALSE},
{"botright", 2, FALSE},
{"browse", 3, FALSE},
{"confirm", 4, FALSE},
{"hide", 3, FALSE},
{"keepalt", 5, FALSE},
{"keepjumps", 5, FALSE},
{"keepmarks", 3, FALSE},
{"leftabove", 5, FALSE},
{"lockmarks", 3, FALSE},
{"noautocmd", 3, FALSE},
{"rightbelow", 6, FALSE},
{"sandbox", 3, FALSE},
{"silent", 3, FALSE},
{"tab", 3, TRUE},
{"topleft", 2, FALSE},
{"unsilent", 3, FALSE},
{"verbose", 4, TRUE},
{"vertical", 4, FALSE},
};
/*
* Return length of a command modifier (including optional count).
* Return zero when it's not a modifier.
*/
int
modifier_len(cmd)
char_u *cmd;
{
int i, j;
char_u *p = cmd;
if (VIM_ISDIGIT(*cmd))
p = skipwhite(skipdigits(cmd));
for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
{
for (j = 0; p[j] != NUL; ++j)
if (p[j] != cmdmods[i].name[j])
break;
if (!isalpha(p[j]) && j >= cmdmods[i].minlen
&& (p == cmd || cmdmods[i].has_count))
return j + (int)(p - cmd);
}
return 0;
}
/*
* Return > 0 if an Ex command "name" exists.
* Return 2 if there is an exact match.
* Return 3 if there is an ambiguous match.
*/
int
cmd_exists(name)
char_u *name;
{
exarg_T ea;
int full = FALSE;
int i;
int j;
char_u *p;
/* Check command modifiers. */
for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
{
for (j = 0; name[j] != NUL; ++j)
if (name[j] != cmdmods[i].name[j])
break;
if (name[j] == NUL && j >= cmdmods[i].minlen)
return (cmdmods[i].name[j] == NUL ? 2 : 1);
}
/* Check built-in commands and user defined commands.
* For ":2match" and ":3match" we need to skip the number. */
ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
ea.cmdidx = (cmdidx_T)0;
p = find_command(&ea, &full);
if (p == NULL)
return 3;
if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
return 0;
if (*skipwhite(p) != NUL)
return 0; /* trailing garbage */
return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
}
#endif
/*
* This is all pretty much copied from do_one_cmd(), with all the extra stuff
* we don't need/want deleted. Maybe this could be done better if we didn't
* repeat all this stuff. The only problem is that they may not stay
* perfectly compatible with each other, but then the command line syntax
* probably won't change that much -- webb.
*/
char_u *
set_one_cmd_context(xp, buff)
expand_T *xp;
char_u *buff; /* buffer for command string */
{
char_u *p;
char_u *cmd, *arg;
int len = 0;
exarg_T ea;
#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
int compl = EXPAND_NOTHING;
#endif
#ifdef FEAT_CMDL_COMPL
int delim;
#endif
int forceit = FALSE;
int usefilter = FALSE; /* filter instead of file name */
ExpandInit(xp);
xp->xp_pattern = buff;
xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
ea.argt = 0;
/*
* 2. skip comment lines and leading space, colons or bars
*/
for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
;
xp->xp_pattern = cmd;
if (*cmd == NUL)
return NULL;
if (*cmd == '"') /* ignore comment lines */
{
xp->xp_context = EXPAND_NOTHING;
return NULL;
}
/*
* 3. parse a range specifier of the form: addr [,addr] [;addr] ..
*/
cmd = skip_range(cmd, &xp->xp_context);
/*
* 4. parse command
*/
xp->xp_pattern = cmd;
if (*cmd == NUL)
return NULL;
if (*cmd == '"')
{
xp->xp_context = EXPAND_NOTHING;
return NULL;
}
if (*cmd == '|' || *cmd == '\n')
return cmd + 1; /* There's another command */
/*
* Isolate the command and search for it in the command table.
* Exceptions:
* - the 'k' command can directly be followed by any character, but
* do accept "keepmarks", "keepalt" and "keepjumps".
* - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
*/
if (*cmd == 'k' && cmd[1] != 'e')
{
ea.cmdidx = CMD_k;
p = cmd + 1;
}
else
{
p = cmd;
while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
++p;
/* check for non-alpha command */
if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
++p;
len = (int)(p - cmd);
if (len == 0)
{
xp->xp_context = EXPAND_UNSUCCESSFUL;
return NULL;
}
for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
(size_t)len) == 0)
break;
#ifdef FEAT_USR_CMDS
if (cmd[0] >= 'A' && cmd[0] <= 'Z')
while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
++p;
#endif
}
/*
* If the cursor is touching the command, and it ends in an alpha-numeric
* character, complete the command name.
*/
if (*p == NUL && ASCII_ISALNUM(p[-1]))
return NULL;
if (ea.cmdidx == CMD_SIZE)
{
if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
{
ea.cmdidx = CMD_substitute;
p = cmd + 1;
}
#ifdef FEAT_USR_CMDS
else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
{
ea.cmd = cmd;
p = find_ucmd(&ea, p, NULL, xp,
# if defined(FEAT_CMDL_COMPL)
&compl
# else
NULL
# endif
);
if (p == NULL)
ea.cmdidx = CMD_SIZE; /* ambiguous user command */
}
#endif
}
if (ea.cmdidx == CMD_SIZE)
{
/* Not still touching the command and it was an illegal one */
xp->xp_context = EXPAND_UNSUCCESSFUL;
return NULL;
}
xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
if (*p == '!') /* forced commands */
{
forceit = TRUE;
++p;
}
/*
* 5. parse arguments
*/
#ifdef FEAT_USR_CMDS
if (!USER_CMDIDX(ea.cmdidx))
#endif
ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
arg = skipwhite(p);
if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
{
if (*arg == '>') /* append */
{
if (*++arg == '>')
++arg;
arg = skipwhite(arg);
}
else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
{
++arg;
usefilter = TRUE;
}
}
if (ea.cmdidx == CMD_read)
{
usefilter = forceit; /* :r! filter if forced */
if (*arg == '!') /* :r !filter */
{
++arg;
usefilter = TRUE;
}
}
if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
{
while (*arg == *cmd) /* allow any number of '>' or '<' */
++arg;
arg = skipwhite(arg);
}
/* Does command allow "+command"? */
if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
{
/* Check if we're in the +command */
p = arg + 1;
arg = skip_cmd_arg(arg, FALSE);
/* Still touching the command after '+'? */
if (*arg == NUL)
return p;
/* Skip space(s) after +command to get to the real argument */
arg = skipwhite(arg);
}
/*
* Check for '|' to separate commands and '"' to start comments.
* Don't do this for ":read !cmd" and ":write !cmd".
*/
if ((ea.argt & TRLBAR) && !usefilter)
{
p = arg;
/* ":redir @" is not the start of a comment */
if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
p += 2;
while (*p)
{
if (*p == Ctrl_V)
{
if (p[1] != NUL)
++p;
}
else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
|| *p == '|' || *p == '\n')
{
if (*(p - 1) != '\\')
{
if (*p == '|' || *p == '\n')
return p + 1;
return NULL; /* It's a comment */
}
}
mb_ptr_adv(p);
}
}
/* no arguments allowed */
if (!(ea.argt & EXTRA) && *arg != NUL &&
vim_strchr((char_u *)"|\"", *arg) == NULL)
return NULL;
/* Find start of last argument (argument just before cursor): */
p = buff + STRLEN(buff);
while (p != arg && *p != ' ' && *p != TAB)
p--;
if (*p == ' ' || *p == TAB)
p++;
xp->xp_pattern = p;
if (ea.argt & XFILE)
{
int c;
int in_quote = FALSE;
char_u *bow = NULL; /* Beginning of word */
/*
* Allow spaces within back-quotes to count as part of the argument
* being expanded.
*/
xp->xp_pattern = skipwhite(arg);
p = xp->xp_pattern;
while (*p != NUL)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
c = mb_ptr2char(p);
else
#endif
c = *p;
if (c == '\\' && p[1] != NUL)
++p;
else if (c == '`')
{
if (!in_quote)
{
xp->xp_pattern = p;
bow = p + 1;
}
in_quote = !in_quote;
}
/* An argument can contain just about everything, except
* characters that end the command and white space. */
else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
#ifdef SPACE_IN_FILENAME
&& (!(ea.argt & NOSPC) || usefilter)
#endif
))
{
len = 0; /* avoid getting stuck when space is in 'isfname' */
while (*p != NUL)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
c = mb_ptr2char(p);
else
#endif
c = *p;
if (c == '`' || vim_isfilec_or_wc(c))
break;
#ifdef FEAT_MBYTE
if (has_mbyte)
len = (*mb_ptr2len)(p);
else
#endif
len = 1;
mb_ptr_adv(p);
}
if (in_quote)
bow = p;
else
xp->xp_pattern = p;
p -= len;
}
mb_ptr_adv(p);
}
/*
* If we are still inside the quotes, and we passed a space, just
* expand from there.
*/
if (bow != NULL && in_quote)
xp->xp_pattern = bow;
xp->xp_context = EXPAND_FILES;
/* For a shell command more chars need to be escaped. */
if (usefilter || ea.cmdidx == CMD_bang)
{
#ifndef BACKSLASH_IN_FILENAME
xp->xp_shell = TRUE;
#endif
/* When still after the command name expand executables. */
if (xp->xp_pattern == skipwhite(arg))
xp->xp_context = EXPAND_SHELLCMD;
}
/* Check for environment variable */
if (*xp->xp_pattern == '$'
#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
|| *xp->xp_pattern == '%'
#endif
)
{
for (p = xp->xp_pattern + 1; *p != NUL; ++p)
if (!vim_isIDc(*p))
break;
if (*p == NUL)
{
xp->xp_context = EXPAND_ENV_VARS;
++xp->xp_pattern;
#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
/* Avoid that the assignment uses EXPAND_FILES again. */
if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
compl = EXPAND_ENV_VARS;
#endif
}
}
}
/*
* 6. switch on command name
*/
switch (ea.cmdidx)
{
case CMD_find:
case CMD_sfind:
case CMD_tabfind:
if (xp->xp_context == EXPAND_FILES)
xp->xp_context = EXPAND_FILES_IN_PATH;
break;
case CMD_cd:
case CMD_chdir:
case CMD_lcd:
case CMD_lchdir:
if (xp->xp_context == EXPAND_FILES)
xp->xp_context = EXPAND_DIRECTORIES;
break;
case CMD_help:
xp->xp_context = EXPAND_HELP;
xp->xp_pattern = arg;
break;
/* Command modifiers: return the argument.
* Also for commands with an argument that is a command. */
case CMD_aboveleft:
case CMD_argdo:
case CMD_belowright:
case CMD_botright:
case CMD_browse:
case CMD_bufdo:
case CMD_confirm:
case CMD_debug:
case CMD_folddoclosed:
case CMD_folddoopen:
case CMD_hide:
case CMD_keepalt:
case CMD_keepjumps:
case CMD_keepmarks:
case CMD_leftabove:
case CMD_lockmarks:
case CMD_rightbelow:
case CMD_sandbox:
case CMD_silent:
case CMD_tab:
case CMD_topleft:
case CMD_verbose:
case CMD_vertical:
case CMD_windo:
return arg;
#ifdef FEAT_CMDL_COMPL
# ifdef FEAT_SEARCH_EXTRA
case CMD_match:
if (*arg == NUL || !ends_excmd(*arg))
{
/* also complete "None" */
set_context_in_echohl_cmd(xp, arg);
arg = skipwhite(skiptowhite(arg));
if (*arg != NUL)
{
xp->xp_context = EXPAND_NOTHING;
arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
}
}
return find_nextcmd(arg);
# endif
/*
* All completion for the +cmdline_compl feature goes here.
*/
# ifdef FEAT_USR_CMDS
case CMD_command:
/* Check for attributes */
while (*arg == '-')
{
arg++; /* Skip "-" */
p = skiptowhite(arg);
if (*p == NUL)
{
/* Cursor is still in the attribute */
p = vim_strchr(arg, '=');
if (p == NULL)
{
/* No "=", so complete attribute names */
xp->xp_context = EXPAND_USER_CMD_FLAGS;
xp->xp_pattern = arg;
return NULL;
}
/* For the -complete and -nargs attributes, we complete
* their arguments as well.
*/
if (STRNICMP(arg, "complete", p - arg) == 0)
{
xp->xp_context = EXPAND_USER_COMPLETE;
xp->xp_pattern = p + 1;
return NULL;
}
else if (STRNICMP(arg, "nargs", p - arg) == 0)
{
xp->xp_context = EXPAND_USER_NARGS;
xp->xp_pattern = p + 1;
return NULL;
}
return NULL;
}
arg = skipwhite(p);
}
/* After the attributes comes the new command name */
p = skiptowhite(arg);
if (*p == NUL)
{
xp->xp_context = EXPAND_USER_COMMANDS;
xp->xp_pattern = arg;
break;
}
/* And finally comes a normal command */
return skipwhite(p);
case CMD_delcommand:
xp->xp_context = EXPAND_USER_COMMANDS;
xp->xp_pattern = arg;
break;
# endif
case CMD_global:
case CMD_vglobal:
delim = *arg; /* get the delimiter */
if (delim)
++arg; /* skip delimiter if there is one */
while (arg[0] != NUL && arg[0] != delim)
{
if (arg[0] == '\\' && arg[1] != NUL)
++arg;
++arg;
}
if (arg[0] != NUL)
return arg + 1;
break;
case CMD_and:
case CMD_substitute:
delim = *arg;
if (delim)
{
/* skip "from" part */
++arg;
arg = skip_regexp(arg, delim, p_magic, NULL);
}
/* skip "to" part */
while (arg[0] != NUL && arg[0] != delim)
{
if (arg[0] == '\\' && arg[1] != NUL)
++arg;
++arg;
}
if (arg[0] != NUL) /* skip delimiter */
++arg;
while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
++arg;
if (arg[0] != NUL)
return arg;
break;
case CMD_isearch:
case CMD_dsearch:
case CMD_ilist:
case CMD_dlist:
case CMD_ijump:
case CMD_psearch:
case CMD_djump:
case CMD_isplit:
case CMD_dsplit:
arg = skipwhite(skipdigits(arg)); /* skip count */
if (*arg == '/') /* Match regexp, not just whole words */
{
for (++arg; *arg && *arg != '/'; arg++)
if (*arg == '\\' && arg[1] != NUL)
arg++;
if (*arg)
{
arg = skipwhite(arg + 1);
/* Check for trailing illegal characters */
if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
xp->xp_context = EXPAND_NOTHING;
else
return arg;
}
}
break;
#ifdef FEAT_AUTOCMD
case CMD_autocmd:
return set_context_in_autocmd(xp, arg, FALSE);
case CMD_doautocmd:
case CMD_doautoall:
return set_context_in_autocmd(xp, arg, TRUE);
#endif
case CMD_set:
set_context_in_set_cmd(xp, arg, 0);
break;
case CMD_setglobal:
set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
break;
case CMD_setlocal:
set_context_in_set_cmd(xp, arg, OPT_LOCAL);
break;
case CMD_tag:
case CMD_stag:
case CMD_ptag:
case CMD_ltag:
case CMD_tselect:
case CMD_stselect:
case CMD_ptselect:
case CMD_tjump:
case CMD_stjump:
case CMD_ptjump:
if (*p_wop != NUL)
xp->xp_context = EXPAND_TAGS_LISTFILES;
else
xp->xp_context = EXPAND_TAGS;
xp->xp_pattern = arg;
break;
case CMD_augroup:
xp->xp_context = EXPAND_AUGROUP;
xp->xp_pattern = arg;
break;
#ifdef FEAT_SYN_HL
case CMD_syntax:
set_context_in_syntax_cmd(xp, arg);
break;
#endif
#ifdef FEAT_EVAL
case CMD_let:
case CMD_if:
case CMD_elseif:
case CMD_while:
case CMD_for:
case CMD_echo:
case CMD_echon:
case CMD_execute:
case CMD_echomsg:
case CMD_echoerr:
case CMD_call:
case CMD_return:
set_context_for_expression(xp, arg, ea.cmdidx);
break;
case CMD_unlet:
while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
arg = xp->xp_pattern + 1;
xp->xp_context = EXPAND_USER_VARS;
xp->xp_pattern = arg;
break;
case CMD_function:
case CMD_delfunction:
xp->xp_context = EXPAND_USER_FUNC;
xp->xp_pattern = arg;
break;
case CMD_echohl:
set_context_in_echohl_cmd(xp, arg);
break;
#endif
case CMD_highlight:
set_context_in_highlight_cmd(xp, arg);
break;
#ifdef FEAT_CSCOPE
case CMD_cscope:
case CMD_lcscope:
case CMD_scscope:
set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
break;
#endif
#ifdef FEAT_SIGNS
case CMD_sign:
set_context_in_sign_cmd(xp, arg);
break;
#endif
#ifdef FEAT_LISTCMDS
case CMD_bdelete:
case CMD_bwipeout:
case CMD_bunload:
while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
arg = xp->xp_pattern + 1;
/*FALLTHROUGH*/
case CMD_buffer:
case CMD_sbuffer:
case CMD_checktime:
xp->xp_context = EXPAND_BUFFERS;
xp->xp_pattern = arg;
break;
#endif
#ifdef FEAT_USR_CMDS
case CMD_USER:
case CMD_USER_BUF:
if (compl != EXPAND_NOTHING)
{
/* XFILE: file names are handled above */
if (!(ea.argt & XFILE))
{
# ifdef FEAT_MENU
if (compl == EXPAND_MENUS)
return set_context_in_menu_cmd(xp, cmd, arg, forceit);
# endif
if (compl == EXPAND_COMMANDS)
return arg;
if (compl == EXPAND_MAPPINGS)
return set_context_in_map_cmd(xp, (char_u *)"map",
arg, forceit, FALSE, FALSE, CMD_map);
while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
arg = xp->xp_pattern + 1;
xp->xp_pattern = arg;
}
xp->xp_context = compl;
}
break;
#endif
case CMD_map: case CMD_noremap:
case CMD_nmap: case CMD_nnoremap:
case CMD_vmap: case CMD_vnoremap:
case CMD_omap: case CMD_onoremap:
case CMD_imap: case CMD_inoremap:
case CMD_cmap: case CMD_cnoremap:
case CMD_lmap: case CMD_lnoremap:
return set_context_in_map_cmd(xp, cmd, arg, forceit,
FALSE, FALSE, ea.cmdidx);
case CMD_unmap:
case CMD_nunmap:
case CMD_vunmap:
case CMD_ounmap:
case CMD_iunmap:
case CMD_cunmap:
case CMD_lunmap:
return set_context_in_map_cmd(xp, cmd, arg, forceit,
FALSE, TRUE, ea.cmdidx);
case CMD_abbreviate: case CMD_noreabbrev:
case CMD_cabbrev: case CMD_cnoreabbrev:
case CMD_iabbrev: case CMD_inoreabbrev:
return set_context_in_map_cmd(xp, cmd, arg, forceit,
TRUE, FALSE, ea.cmdidx);
case CMD_unabbreviate:
case CMD_cunabbrev:
case CMD_iunabbrev:
return set_context_in_map_cmd(xp, cmd, arg, forceit,
TRUE, TRUE, ea.cmdidx);
#ifdef FEAT_MENU
case CMD_menu: case CMD_noremenu: case CMD_unmenu:
case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
case CMD_tmenu: case CMD_tunmenu:
case CMD_popup: case CMD_tearoff: case CMD_emenu:
return set_context_in_menu_cmd(xp, cmd, arg, forceit);
#endif
case CMD_colorscheme:
xp->xp_context = EXPAND_COLORS;
xp->xp_pattern = arg;
break;
case CMD_compiler:
xp->xp_context = EXPAND_COMPILER;
xp->xp_pattern = arg;
break;
case CMD_ownsyntax:
xp->xp_context = EXPAND_OWNSYNTAX;
xp->xp_pattern = arg;
break;
case CMD_setfiletype:
xp->xp_context = EXPAND_FILETYPE;
xp->xp_pattern = arg;
break;
#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
&& (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
case CMD_language:
if (*skiptowhite(arg) == NUL)
{
xp->xp_context = EXPAND_LANGUAGE;
xp->xp_pattern = arg;
}
else
xp->xp_context = EXPAND_NOTHING;
break;
#endif
#if defined(FEAT_PROFILE)
case CMD_profile:
set_context_in_profile_cmd(xp, arg);
break;
#endif
case CMD_behave:
xp->xp_context = EXPAND_BEHAVE;
break;
#endif /* FEAT_CMDL_COMPL */
default:
break;
}
return NULL;
}
/*
* skip a range specifier of the form: addr [,addr] [;addr] ..
*
* Backslashed delimiters after / or ? will be skipped, and commands will
* not be expanded between /'s and ?'s or after "'".
*
* Also skip white space and ":" characters.
* Returns the "cmd" pointer advanced to beyond the range.
*/
char_u *
skip_range(cmd, ctx)
char_u *cmd;
int *ctx; /* pointer to xp_context or NULL */
{
unsigned delim;
while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
{
if (*cmd == '\'')
{
if (*++cmd == NUL && ctx != NULL)
*ctx = EXPAND_NOTHING;
}
else if (*cmd == '/' || *cmd == '?')
{
delim = *cmd++;
while (*cmd != NUL && *cmd != delim)
if (*cmd++ == '\\' && *cmd != NUL)
++cmd;
if (*cmd == NUL && ctx != NULL)
*ctx = EXPAND_NOTHING;
}
if (*cmd != NUL)
++cmd;
}
/* Skip ":" and white space. */
while (*cmd == ':')
cmd = skipwhite(cmd + 1);
return cmd;
}
/*
* get a single EX address
*
* Set ptr to the next character after the part that was interpreted.
* Set ptr to NULL when an error is encountered.
*
* Return MAXLNUM when no Ex address was found.
*/
static linenr_T
get_address(ptr, skip, to_other_file)
char_u **ptr;
int skip; /* only skip the address, don't use it */
int to_other_file; /* flag: may jump to other file */
{
int c;
int i;
long n;
char_u *cmd;
pos_T pos;
pos_T *fp;
linenr_T lnum;
cmd = skipwhite(*ptr);
lnum = MAXLNUM;
do
{
switch (*cmd)
{
case '.': /* '.' - Cursor position */
++cmd;
lnum = curwin->w_cursor.lnum;
break;
case '$': /* '$' - last line */
++cmd;
lnum = curbuf->b_ml.ml_line_count;
break;
case '\'': /* ''' - mark */
if (*++cmd == NUL)
{
cmd = NULL;
goto error;
}
if (skip)
++cmd;
else
{
/* Only accept a mark in another file when it is
* used by itself: ":'M". */
fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
++cmd;
if (fp == (pos_T *)-1)
/* Jumped to another file. */
lnum = curwin->w_cursor.lnum;
else
{
if (check_mark(fp) == FAIL)
{
cmd = NULL;
goto error;
}
lnum = fp->lnum;
}
}
break;
case '/':
case '?': /* '/' or '?' - search */
c = *cmd++;
if (skip) /* skip "/pat/" */
{
cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
if (*cmd == c)
++cmd;
}
else
{
pos = curwin->w_cursor; /* save curwin->w_cursor */
/*
* When '/' or '?' follows another address, start
* from there.
*/
if (lnum != MAXLNUM)
curwin->w_cursor.lnum = lnum;
/*
* Start a forward search at the end of the line.
* Start a backward search at the start of the line.
* This makes sure we never match in the current
* line, and can match anywhere in the
* next/previous line.
*/
if (c == '/')
curwin->w_cursor.col = MAXCOL;
else
curwin->w_cursor.col = 0;
searchcmdlen = 0;
if (!do_search(NULL, c, cmd, 1L,
SEARCH_HIS | SEARCH_MSG, NULL))
{
curwin->w_cursor = pos;
cmd = NULL;
goto error;
}
lnum = curwin->w_cursor.lnum;
curwin->w_cursor = pos;
/* adjust command string pointer */
cmd += searchcmdlen;
}
break;
case '\\': /* "\?", "\/" or "\&", repeat search */
++cmd;
if (*cmd == '&')
i = RE_SUBST;
else if (*cmd == '?' || *cmd == '/')
i = RE_SEARCH;
else
{
EMSG(_(e_backslash));
cmd = NULL;
goto error;
}
if (!skip)
{
/*
* When search follows another address, start from
* there.
*/
if (lnum != MAXLNUM)
pos.lnum = lnum;
else
pos.lnum = curwin->w_cursor.lnum;
/*
* Start the search just like for the above
* do_search().
*/
if (*cmd != '?')
pos.col = MAXCOL;
else
pos.col = 0;
if (searchit(curwin, curbuf, &pos,
*cmd == '?' ? BACKWARD : FORWARD,
(char_u *)"", 1L, SEARCH_MSG,
i, (linenr_T)0, NULL) != FAIL)
lnum = pos.lnum;
else
{
cmd = NULL;
goto error;
}
}
++cmd;
break;
default:
if (VIM_ISDIGIT(*cmd)) /* absolute line number */
lnum = getdigits(&cmd);
}
for (;;)
{
cmd = skipwhite(cmd);
if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
break;
if (lnum == MAXLNUM)
lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
if (VIM_ISDIGIT(*cmd))
i = '+'; /* "number" is same as "+number" */
else
i = *cmd++;
if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
n = 1;
else
n = getdigits(&cmd);
if (i == '-')
lnum -= n;
else
lnum += n;
}
} while (*cmd == '/' || *cmd == '?');
error:
*ptr = cmd;
return lnum;
}
/*
* Get flags from an Ex command argument.
*/
static void
get_flags(eap)
exarg_T *eap;
{
while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
{
if (*eap->arg == 'l')
eap->flags |= EXFLAG_LIST;
else if (*eap->arg == 'p')
eap->flags |= EXFLAG_PRINT;
else
eap->flags |= EXFLAG_NR;
eap->arg = skipwhite(eap->arg + 1);
}
}
/*
* Function called for command which is Not Implemented. NI!
*/
void
ex_ni(eap)
exarg_T *eap;
{
if (!eap->skip)
eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
}
#ifdef HAVE_EX_SCRIPT_NI
/*
* Function called for script command which is Not Implemented. NI!
* Skips over ":perl <<EOF" constructs.
*/
static void
ex_script_ni(eap)
exarg_T *eap;
{
if (!eap->skip)
ex_ni(eap);
else
vim_free(script_get(eap, eap->arg));
}
#endif
/*
* Check range in Ex command for validity.
* Return NULL when valid, error message when invalid.
*/
static char_u *
invalid_range(eap)
exarg_T *eap;
{
if ( eap->line1 < 0
|| eap->line2 < 0
|| eap->line1 > eap->line2
|| ((eap->argt & RANGE)
&& !(eap->argt & NOTADR)
&& eap->line2 > curbuf->b_ml.ml_line_count
#ifdef FEAT_DIFF
+ (eap->cmdidx == CMD_diffget)
#endif
))
return (char_u *)_(e_invrange);
return NULL;
}
/*
* Correct the range for zero line number, if required.
*/
static void
correct_range(eap)
exarg_T *eap;
{
if (!(eap->argt & ZEROR)) /* zero in range not allowed */
{
if (eap->line1 == 0)
eap->line1 = 1;
if (eap->line2 == 0)
eap->line2 = 1;
}
}
#ifdef FEAT_QUICKFIX
static char_u *skip_grep_pat __ARGS((exarg_T *eap));
/*
* For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
* pattern. Otherwise return eap->arg.
*/
static char_u *
skip_grep_pat(eap)
exarg_T *eap;
{
char_u *p = eap->arg;
if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
|| eap->cmdidx == CMD_vimgrepadd
|| eap->cmdidx == CMD_lvimgrepadd
|| grep_internal(eap->cmdidx)))
{
p = skip_vimgrep_pat(p, NULL, NULL);
if (p == NULL)
p = eap->arg;
}
return p;
}
/*
* For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
* in the command line, so that things like % get expanded.
*/
static char_u *
replace_makeprg(eap, p, cmdlinep)
exarg_T *eap;
char_u *p;
char_u **cmdlinep;
{
char_u *new_cmdline;
char_u *program;
char_u *pos;
char_u *ptr;
int len;
int i;
/*
* Don't do it when ":vimgrep" is used for ":grep".
*/
if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
|| eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
|| eap->cmdidx == CMD_grepadd
|| eap->cmdidx == CMD_lgrepadd)
&& !grep_internal(eap->cmdidx))
{
if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
|| eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
{
if (*curbuf->b_p_gp == NUL)
program = p_gp;
else
program = curbuf->b_p_gp;
}
else
{
if (*curbuf->b_p_mp == NUL)
program = p_mp;
else
program = curbuf->b_p_mp;
}
p = skipwhite(p);
if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
{
/* replace $* by given arguments */
i = 1;
while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
++i;
len = (int)STRLEN(p);
new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
if (new_cmdline == NULL)
return NULL; /* out of memory */
ptr = new_cmdline;
while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
{
i = (int)(pos - program);
STRNCPY(ptr, program, i);
STRCPY(ptr += i, p);
ptr += len;
program = pos + 2;
}
STRCPY(ptr, program);
}
else
{
new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
if (new_cmdline == NULL)
return NULL; /* out of memory */
STRCPY(new_cmdline, program);
STRCAT(new_cmdline, " ");
STRCAT(new_cmdline, p);
}
msg_make(p);
/* 'eap->cmd' is not set here, because it is not used at CMD_make */
vim_free(*cmdlinep);
*cmdlinep = new_cmdline;
p = new_cmdline;
}
return p;
}
#endif
/*
* Expand file name in Ex command argument.
* Return FAIL for failure, OK otherwise.
*/
int
expand_filename(eap, cmdlinep, errormsgp)
exarg_T *eap;
char_u **cmdlinep;
char_u **errormsgp;
{
int has_wildcards; /* need to expand wildcards */
char_u *repl;
int srclen;
char_u *p;
int n;
int escaped;
#ifdef FEAT_QUICKFIX
/* Skip a regexp pattern for ":vimgrep[add] pat file..." */
p = skip_grep_pat(eap);
#else
p = eap->arg;
#endif
/*
* Decide to expand wildcards *before* replacing '%', '#', etc. If
* the file name contains a wildcard it should not cause expanding.
* (it will be expanded anyway if there is a wildcard before replacing).
*/
has_wildcards = mch_has_wildcard(p);
while (*p != NUL)
{
#ifdef FEAT_EVAL
/* Skip over `=expr`, wildcards in it are not expanded. */
if (p[0] == '`' && p[1] == '=')
{
p += 2;
(void)skip_expr(&p);
if (*p == '`')
++p;
continue;
}
#endif
/*
* Quick check if this cannot be the start of a special string.
* Also removes backslash before '%', '#' and '<'.
*/
if (vim_strchr((char_u *)"%#<", *p) == NULL)
{
++p;
continue;
}
/*
* Try to find a match at this position.
*/
repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
errormsgp, &escaped);
if (*errormsgp != NULL) /* error detected */
return FAIL;
if (repl == NULL) /* no match found */
{
p += srclen;
continue;
}
/* Wildcards won't be expanded below, the replacement is taken
* literally. But do expand "~/file", "~user/file" and "$HOME/file". */
if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
{
char_u *l = repl;
repl = expand_env_save(repl);
vim_free(l);
}
/* Need to escape white space et al. with a backslash.
* Don't do this for:
* - replacement that already has been escaped: "##"
* - shell commands (may have to use quotes instead).
* - non-unix systems when there is a single argument (spaces don't
* separate arguments then).
*/
if (!eap->usefilter
&& !escaped
&& eap->cmdidx != CMD_bang
&& eap->cmdidx != CMD_make
&& eap->cmdidx != CMD_lmake