blob: 0317050d6b897ef796b9f7b361b5bc54c2df2edc [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +010030 int uc_addr_type; /* The command's address type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010031# ifdef FEAT_EVAL
32 scid_T uc_scriptID; /* SID where the command was defined */
33# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036# endif
37} ucmd_T;
38
39#define UC_BUFFER 1 /* -buffer: local to current buffer */
40
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000041static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
44#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
45
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static void do_ucmd(exarg_T *eap);
47static void ex_command(exarg_T *eap);
48static void ex_delcommand(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# ifdef FEAT_CMDL_COMPL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010050static char_u *get_user_command_name(int idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051# endif
52
Bram Moolenaar958636c2014-10-21 20:01:58 +020053/* Wether a command index indicates a user command. */
54# define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#else
57# define ex_command ex_ni
58# define ex_comclear ex_ni
59# define ex_delcommand ex_ni
Bram Moolenaar958636c2014-10-21 20:01:58 +020060/* Wether a command index indicates a user command. */
61# define IS_USER_CMDIDX(idx) (FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
63
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010064static int compute_buffer_local_count(int addr_type, int lnum, int local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010066static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010068static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int if_level = 0; /* depth in :if */
70#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void append_command(char_u *cmd);
72static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010074static void ex_abbreviate(exarg_T *eap);
75static void ex_map(exarg_T *eap);
76static void ex_unmap(exarg_T *eap);
77static void ex_mapclear(exarg_T *eap);
78static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifndef FEAT_MENU
80# define ex_emenu ex_ni
81# define ex_menu ex_ni
82# define ex_menutranslate ex_ni
83#endif
84#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static void ex_autocmd(exarg_T *eap);
86static void ex_doautocmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#else
88# define ex_autocmd ex_ni
89# define ex_doautocmd ex_ni
90# define ex_doautoall ex_ni
91#endif
92#ifdef FEAT_LISTCMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static void ex_bunload(exarg_T *eap);
94static void ex_buffer(exarg_T *eap);
95static void ex_bmodified(exarg_T *eap);
96static void ex_bnext(exarg_T *eap);
97static void ex_bprevious(exarg_T *eap);
98static void ex_brewind(exarg_T *eap);
99static void ex_blast(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#else
101# define ex_bunload ex_ni
102# define ex_buffer ex_ni
103# define ex_bmodified ex_ni
104# define ex_bnext ex_ni
105# define ex_bprevious ex_ni
106# define ex_brewind ex_ni
107# define ex_blast ex_ni
108# define buflist_list ex_ni
109# define ex_checktime ex_ni
110#endif
111#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
112# define ex_buffer_all ex_ni
113#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static char_u *getargcmd(char_u **);
115static char_u *skip_cmd_arg(char_u *p, int rembs);
116static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifndef FEAT_QUICKFIX
118# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# define ex_cc ex_ni
121# define ex_cnext ex_ni
122# define ex_cfile ex_ni
123# define qf_list ex_ni
124# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200125# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000127# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
129#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
130# define ex_cclose ex_ni
131# define ex_copen ex_ni
132# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200133# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000135#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
136# define ex_cexpr ex_ni
137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static int check_more(int, int);
Bram Moolenaarded27822017-01-02 14:27:34 +0100140static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100141static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200142#if !defined(FEAT_PERL) \
143 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
144 || !defined(FEAT_TCL) \
145 || !defined(FEAT_RUBY) \
146 || !defined(FEAT_LUA) \
147 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000148# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100149static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100151static char_u *invalid_range(exarg_T *eap);
152static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000153#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000155#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100156static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
157static void ex_highlight(exarg_T *eap);
158static void ex_colorscheme(exarg_T *eap);
159static void ex_quit(exarg_T *eap);
160static void ex_cquit(exarg_T *eap);
161static void ex_quit_all(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100163static void ex_close(exarg_T *eap);
164static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
165static void ex_only(exarg_T *eap);
166static void ex_resize(exarg_T *eap);
167static void ex_stag(exarg_T *eap);
168static void ex_tabclose(exarg_T *eap);
169static void ex_tabonly(exarg_T *eap);
170static void ex_tabnext(exarg_T *eap);
171static void ex_tabmove(exarg_T *eap);
172static void ex_tabs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#else
174# define ex_close ex_ni
175# define ex_only ex_ni
176# define ex_all ex_ni
177# define ex_resize ex_ni
178# define ex_splitview ex_ni
179# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000180# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000181# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000182# define ex_tabs ex_ni
183# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000184# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#endif
186#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100187static void ex_pclose(exarg_T *eap);
188static void ex_ptag(exarg_T *eap);
189static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#else
191# define ex_pclose ex_ni
192# define ex_ptag ex_ni
193# define ex_pedit ex_ni
194#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100195static void ex_hide(exarg_T *eap);
196static void ex_stop(exarg_T *eap);
197static void ex_exit(exarg_T *eap);
198static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100200static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#else
202# define ex_goto ex_ni
203#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100204static void ex_shell(exarg_T *eap);
205static void ex_preserve(exarg_T *eap);
206static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifndef FEAT_LISTCMDS
208# define ex_argedit ex_ni
209# define ex_argadd ex_ni
210# define ex_argdelete ex_ni
211# define ex_listdo ex_ni
212#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void ex_mode(exarg_T *eap);
214static void ex_wrongmodifier(exarg_T *eap);
215static void ex_find(exarg_T *eap);
216static void ex_open(exarg_T *eap);
217static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
219# define ex_drop ex_ni
220#endif
221#ifndef FEAT_GUI
222# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#endif
225#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_tearoff ex_ni
229#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000230#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100231static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#else
233# define ex_popup ex_ni
234#endif
235#ifndef FEAT_GUI_MSWIN
236# define ex_simalt ex_ni
237#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000238#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# define gui_mch_find_dialog ex_ni
240# define gui_mch_replace_dialog ex_ni
241#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000242#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243# define ex_helpfind ex_ni
244#endif
245#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100246# define ex_cscope ex_ni
247# define ex_scscope ex_ni
248# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#endif
250#ifndef FEAT_SYN_HL
251# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200252# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000253#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200254#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200255# define ex_syntime ex_ni
256#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000257#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000258# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000259# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000260# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000261# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000262# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000263#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200264#ifndef FEAT_PERSISTENT_UNDO
265# define ex_rundo ex_ni
266# define ex_wundo ex_ni
267#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200268#ifndef FEAT_LUA
269# define ex_lua ex_script_ni
270# define ex_luado ex_ni
271# define ex_luafile ex_ni
272#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000273#ifndef FEAT_MZSCHEME
274# define ex_mzscheme ex_script_ni
275# define ex_mzfile ex_ni
276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_PERL
278# define ex_perl ex_script_ni
279# define ex_perldo ex_ni
280#endif
281#ifndef FEAT_PYTHON
282# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200283# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_pyfile ex_ni
285#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200286#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200287# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200288# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200289# define ex_py3file ex_ni
290#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100291#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
292# define ex_pyx ex_script_ni
293# define ex_pyxdo ex_ni
294# define ex_pyxfile ex_ni
295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296#ifndef FEAT_TCL
297# define ex_tcl ex_script_ni
298# define ex_tcldo ex_ni
299# define ex_tclfile ex_ni
300#endif
301#ifndef FEAT_RUBY
302# define ex_ruby ex_script_ni
303# define ex_rubydo ex_ni
304# define ex_rubyfile ex_ni
305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#ifndef FEAT_KEYMAP
307# define ex_loadkeymap ex_ni
308#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100309static void ex_swapname(exarg_T *eap);
310static void ex_syncbind(exarg_T *eap);
311static void ex_read(exarg_T *eap);
312static void ex_pwd(exarg_T *eap);
313static void ex_equal(exarg_T *eap);
314static void ex_sleep(exarg_T *eap);
315static void do_exmap(exarg_T *eap, int isabbrev);
316static void ex_winsize(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_wincmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#else
320# define ex_wincmd ex_ni
321#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000322#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100323static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324#else
325# define ex_winpos ex_ni
326#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_operators(exarg_T *eap);
328static void ex_put(exarg_T *eap);
329static void ex_copymove(exarg_T *eap);
330static void ex_submagic(exarg_T *eap);
331static void ex_join(exarg_T *eap);
332static void ex_at(exarg_T *eap);
333static void ex_bang(exarg_T *eap);
334static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200335#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_wundo(exarg_T *eap);
337static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200338#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100339static void ex_redo(exarg_T *eap);
340static void ex_later(exarg_T *eap);
341static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_redrawstatus(exarg_T *eap);
343static void close_redir(void);
344static void ex_mkrc(exarg_T *eap);
345static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100347static char_u *uc_fun_cmd(void);
348static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100350static void ex_startinsert(exarg_T *eap);
351static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100353static void ex_checkpath(exarg_T *eap);
354static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355#else
356# define ex_findpat ex_ni
357# define ex_checkpath ex_ni
358#endif
359#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100360static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#else
362# define ex_psearch ex_ni
363#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100364static void ex_tag(exarg_T *eap);
365static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366#ifndef FEAT_EVAL
367# define ex_scriptnames ex_ni
368# define ex_finish ex_ni
369# define ex_echo ex_ni
370# define ex_echohl ex_ni
371# define ex_execute ex_ni
372# define ex_call ex_ni
373# define ex_if ex_ni
374# define ex_endif ex_ni
375# define ex_else ex_ni
376# define ex_while ex_ni
377# define ex_continue ex_ni
378# define ex_break ex_ni
379# define ex_endwhile ex_ni
380# define ex_throw ex_ni
381# define ex_try ex_ni
382# define ex_catch ex_ni
383# define ex_finally ex_ni
384# define ex_endtry ex_ni
385# define ex_endfunction ex_ni
386# define ex_let ex_ni
387# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000388# define ex_lockvar ex_ni
389# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390# define ex_function ex_ni
391# define ex_delfunction ex_ni
392# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000393# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100395static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100397static int makeopens(FILE *fd, char_u *dirnow);
398static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
399static void ex_loadview(exarg_T *eap);
400static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000401static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#else
403# define ex_loadview ex_ni
404#endif
405#ifndef FEAT_EVAL
406# define ex_compiler ex_ni
407#endif
408#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100409static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#else
411# define ex_viminfo ex_ni
412#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100413static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100415static void ex_filetype(exarg_T *eap);
416static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#else
418# define ex_filetype ex_ni
419# define ex_setfiletype ex_ni
420#endif
421#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000422# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423# define ex_diffpatch ex_ni
424# define ex_diffgetput ex_ni
425# define ex_diffsplit ex_ni
426# define ex_diffthis ex_ni
427# define ex_diffupdate ex_ni
428#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100429static void ex_digraphs(exarg_T *eap);
430static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
432# define ex_options ex_ni
433#endif
434#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100435static void ex_nohlsearch(exarg_T *eap);
436static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437#else
438# define ex_nohlsearch ex_ni
439# define ex_match ex_ni
440#endif
441#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100442static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443#else
444# define ex_X ex_ni
445#endif
446#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100447static void ex_fold(exarg_T *eap);
448static void ex_foldopen(exarg_T *eap);
449static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450#else
451# define ex_fold ex_ni
452# define ex_foldopen ex_ni
453# define ex_folddo ex_ni
454#endif
455#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
456 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
457# define ex_language ex_ni
458#endif
459#ifndef FEAT_SIGNS
460# define ex_sign ex_ni
461#endif
462#ifndef FEAT_SUN_WORKSHOP
463# define ex_wsverb ex_ni
464#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000465#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200466# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000467# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200468# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
471#ifndef FEAT_EVAL
472# define ex_debug ex_ni
473# define ex_breakadd ex_ni
474# define ex_debuggreedy ex_ni
475# define ex_breakdel ex_ni
476# define ex_breaklist ex_ni
477#endif
478
479#ifndef FEAT_CMDHIST
480# define ex_history ex_ni
481#endif
482#ifndef FEAT_JUMPLIST
483# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200484# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485# define ex_changes ex_ni
486#endif
487
Bram Moolenaar05159a02005-02-26 23:04:13 +0000488#ifndef FEAT_PROFILE
489# define ex_profile ex_ni
490#endif
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492/*
493 * Declare cmdnames[].
494 */
495#define DO_DECLARE_EXCMD
496#include "ex_cmds.h"
497
498/*
499 * Table used to quickly search for a command, based on its first character.
500 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000501static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502{
503 CMD_append,
504 CMD_buffer,
505 CMD_change,
506 CMD_delete,
507 CMD_edit,
508 CMD_file,
509 CMD_global,
510 CMD_help,
511 CMD_insert,
512 CMD_join,
513 CMD_k,
514 CMD_list,
515 CMD_move,
516 CMD_next,
517 CMD_open,
518 CMD_print,
519 CMD_quit,
520 CMD_read,
521 CMD_substitute,
522 CMD_t,
523 CMD_undo,
524 CMD_vglobal,
525 CMD_write,
526 CMD_xit,
527 CMD_yank,
528 CMD_z,
529 CMD_bang
530};
531
532static char_u dollar_command[2] = {'$', 0};
533
534
535#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000536/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537typedef struct
538{
539 char_u *line; /* command line */
540 linenr_T lnum; /* sourcing_lnum of the line */
541} wcmd_T;
542
543/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000544 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000546 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000548struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
550 garray_T *lines_gap; /* growarray with line info */
551 int current_line; /* last read line from growarray */
552 int repeating; /* TRUE when looping a second time */
553 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100554 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 void *cookie;
556};
557
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100558static char_u *get_loop_line(int c, void *cookie, int indent);
559static int store_loop_line(garray_T *gap, char_u *line);
560static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000561
562/* Struct to save a few things while debugging. Used in do_cmdline() only. */
563struct dbg_stuff
564{
565 int trylevel;
566 int force_abort;
567 except_T *caught_stack;
568 char_u *vv_exception;
569 char_u *vv_throwpoint;
570 int did_emsg;
571 int got_int;
572 int did_throw;
573 int need_rethrow;
574 int check_cstack;
575 except_T *current_exception;
576};
577
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100578static void save_dbg_stuff(struct dbg_stuff *dsp);
579static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000580
581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100582save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000583{
584 dsp->trylevel = trylevel; trylevel = 0;
585 dsp->force_abort = force_abort; force_abort = FALSE;
586 dsp->caught_stack = caught_stack; caught_stack = NULL;
587 dsp->vv_exception = v_exception(NULL);
588 dsp->vv_throwpoint = v_throwpoint(NULL);
589
590 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
591 dsp->did_emsg = did_emsg; did_emsg = FALSE;
592 dsp->got_int = got_int; got_int = FALSE;
593 dsp->did_throw = did_throw; did_throw = FALSE;
594 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
595 dsp->check_cstack = check_cstack; check_cstack = FALSE;
596 dsp->current_exception = current_exception; current_exception = NULL;
597}
598
599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100600restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000601{
602 suppress_errthrow = FALSE;
603 trylevel = dsp->trylevel;
604 force_abort = dsp->force_abort;
605 caught_stack = dsp->caught_stack;
606 (void)v_exception(dsp->vv_exception);
607 (void)v_throwpoint(dsp->vv_throwpoint);
608 did_emsg = dsp->did_emsg;
609 got_int = dsp->got_int;
610 did_throw = dsp->did_throw;
611 need_rethrow = dsp->need_rethrow;
612 check_cstack = dsp->check_cstack;
613 current_exception = dsp->current_exception;
614}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615#endif
616
617
618/*
619 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
620 * command is given.
621 */
622 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100623do_exmode(
624 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625{
626 int save_msg_scroll;
627 int prev_msg_row;
628 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000629 int changedtick;
630
631 if (improved)
632 exmode_active = EXMODE_VIM;
633 else
634 exmode_active = EXMODE_NORMAL;
635 State = NORMAL;
636
637 /* When using ":global /pat/ visual" and then "Q" we return to continue
638 * the :global command. */
639 if (global_busy)
640 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
642 save_msg_scroll = msg_scroll;
643 ++RedrawingDisabled; /* don't redisplay the window */
644 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#ifdef FEAT_GUI
646 /* Ignore scrollbar and mouse events in Ex mode */
647 ++hold_gui_events;
648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
651 while (exmode_active)
652 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000653 /* Check for a ":normal" command and no more characters left. */
654 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
655 {
656 exmode_active = FALSE;
657 break;
658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 msg_scroll = TRUE;
660 need_wait_return = FALSE;
661 ex_pressedreturn = FALSE;
662 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000663 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 prev_msg_row = msg_row;
665 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if (improved)
667 {
668 cmdline_row = msg_row;
669 do_cmdline(NULL, getexline, NULL, 0);
670 }
671 else
672 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
673 lines_left = Rows - 1;
674
Bram Moolenaardf177f62005-02-22 08:39:57 +0000675 if ((prev_line != curwin->w_cursor.lnum
676 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000678 if (curbuf->b_ml.ml_flags & ML_EMPTY)
679 EMSG(_(e_emptybuf));
680 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000682 if (ex_pressedreturn)
683 {
684 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000685 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000686 msg_row = prev_msg_row;
687 if (prev_msg_row == Rows - 1)
688 msg_row--;
689 }
690 msg_col = 0;
691 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
692 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000695 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
696 {
697 if (curbuf->b_ml.ml_flags & ML_EMPTY)
698 EMSG(_(e_emptybuf));
699 else
700 EMSG(_("E501: At end-of-file"));
701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 }
703
704#ifdef FEAT_GUI
705 --hold_gui_events;
706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 --RedrawingDisabled;
708 --no_wait_return;
709 update_screen(CLEAR);
710 need_wait_return = FALSE;
711 msg_scroll = save_msg_scroll;
712}
713
714/*
715 * Execute a simple command line. Used for translated commands like "*".
716 */
717 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100718do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719{
720 return do_cmdline(cmd, NULL, NULL,
721 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
722}
723
724/*
725 * do_cmdline(): execute one Ex command line
726 *
727 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100728 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 * 2. Split up in parts separated with '|'.
730 *
731 * This function can be called recursively!
732 *
733 * flags:
734 * DOCMD_VERBOSE - The command will be included in the error message.
735 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100736 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 * DOCMD_KEYTYPED - Don't reset KeyTyped.
738 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
739 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
740 *
741 * return FAIL if cmdline could not be executed, OK otherwise
742 */
743 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100744do_cmdline(
745 char_u *cmdline,
746 char_u *(*fgetline)(int, void *, int),
747 void *cookie, /* argument for fgetline() */
748 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749{
750 char_u *next_cmdline; /* next cmd to execute */
751 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 static int recursive = 0; /* recursive depth */
754 int msg_didout_before_start = 0;
755 int count = 0; /* line number count */
756 int did_inc = FALSE; /* incremented RedrawingDisabled */
757 int retval = OK;
758#ifdef FEAT_EVAL
759 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000760 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 int current_line = 0; /* active line in lines_ga */
762 char_u *fname = NULL; /* function or script name */
763 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
764 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000765 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 int initial_trylevel;
767 struct msglist **saved_msg_list = NULL;
768 struct msglist *private_msg_list;
769
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100770 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100771 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000773 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000775 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100777# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778# define cmd_cookie cookie
779#endif
780 static int call_depth = 0; /* recursiveness */
781
782#ifdef FEAT_EVAL
783 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
784 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000785 * This ensures that the do_errthrow() call in do_one_cmd() does not
786 * combine the messages stored by an earlier invocation of do_one_cmd()
787 * with the command name of the later one. This would happen when
788 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 saved_msg_list = msg_list;
790 msg_list = &private_msg_list;
791 private_msg_list = NULL;
792#endif
793
794 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100795 * here. The value of 200 allows nested function calls, ":source", etc.
796 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100797 if (call_depth >= 200
798#ifdef FEAT_EVAL
799 && call_depth >= p_mfd
800#endif
801 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {
803 EMSG(_("E169: Command too recursive"));
804#ifdef FEAT_EVAL
805 /* When converting to an exception, we do not include the command name
806 * since this is not an error of the specific command. */
807 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
808 msg_list = saved_msg_list;
809#endif
810 return FAIL;
811 }
812 ++call_depth;
813
814#ifdef FEAT_EVAL
815 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000816 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 cstack.cs_trylevel = 0;
818 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000819 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
821
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100822 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
824 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 ++ex_nesting_level;
828
829 /* Get the function or script name and the address where the next breakpoint
830 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000831 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 {
833 fname = func_name(real_cookie);
834 breakpoint = func_breakpoint(real_cookie);
835 dbg_tick = func_dbg_tick(real_cookie);
836 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100837 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
839 fname = sourcing_name;
840 breakpoint = source_breakpoint(real_cookie);
841 dbg_tick = source_dbg_tick(real_cookie);
842 }
843
844 /*
845 * Initialize "force_abort" and "suppress_errthrow" at the top level.
846 */
847 if (!recursive)
848 {
849 force_abort = FALSE;
850 suppress_errthrow = FALSE;
851 }
852
853 /*
854 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000855 * exception handling (used when debugging). Otherwise clear it to avoid
856 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000858 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000859 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000860 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200861 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
863 initial_trylevel = trylevel;
864
865 /*
866 * "did_throw" will be set to TRUE when an exception is being thrown.
867 */
868 did_throw = FALSE;
869#endif
870 /*
871 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000872 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 * If force_abort is set, we cancel everything.
874 */
875 did_emsg = FALSE;
876
877 /*
878 * KeyTyped is only set when calling vgetc(). Reset it here when not
879 * calling vgetc() (sourced command lines).
880 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100881 if (!(flags & DOCMD_KEYTYPED)
882 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 KeyTyped = FALSE;
884
885 /*
886 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000887 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 * - for multiple commands on one line, separated with '|'
889 * - when repeating until there are no more lines (for ":source")
890 */
891 next_cmdline = cmdline;
892 do
893 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000894#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100895 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896#endif
897
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000898 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if (next_cmdline == NULL
900#ifdef FEAT_EVAL
901 && !force_abort
902 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904#endif
905 )
906 did_emsg = FALSE;
907
908 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000909 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100910 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 * 3. If a line is given: Make a copy, so we can mess with it.
912 */
913
914#ifdef FEAT_EVAL
915 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000916 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 {
918 /* Each '|' separated command is stored separately in lines_ga, to
919 * be able to jump to it. Don't use next_cmdline now. */
920 vim_free(cmdline_copy);
921 cmdline_copy = NULL;
922
923 /* Check if a function has returned or, unless it has an unclosed
924 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000925 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000928 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000929 func_line_end(real_cookie);
930# endif
931 if (func_has_ended(real_cookie))
932 {
933 retval = FAIL;
934 break;
935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000937#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000938 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100939 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000940 script_line_end();
941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
943 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100944 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
946 retval = FAIL;
947 break;
948 }
949
950 /* If breakpoints have been added/deleted need to check for it. */
951 if (breakpoint != NULL && dbg_tick != NULL
952 && *dbg_tick != debug_tick)
953 {
954 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100955 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 fname, sourcing_lnum);
957 *dbg_tick = debug_tick;
958 }
959
960 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
961 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
962
963 /* Did we encounter a breakpoint? */
964 if (breakpoint != NULL && *breakpoint != 0
965 && *breakpoint <= sourcing_lnum)
966 {
967 dbg_breakpoint(fname, sourcing_lnum);
968 /* Find next breakpoint. */
969 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100970 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 fname, sourcing_lnum);
972 *dbg_tick = debug_tick;
973 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000974# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000975 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000976 {
977 if (getline_is_func)
978 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100979 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000980 script_line_start();
981 }
982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 }
984
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000985 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100988 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 * below, so that it stores lines in or reads them from
990 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000991 * while/for loop. */
992 cmd_getline = get_loop_line;
993 cmd_cookie = (void *)&cmd_loop_cookie;
994 cmd_loop_cookie.lines_gap = &lines_ga;
995 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100996 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000997 cmd_loop_cookie.cookie = cookie;
998 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 }
1000 else
1001 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001002 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 cmd_cookie = cookie;
1004 }
1005#endif
1006
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001007 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (next_cmdline == NULL)
1009 {
1010 /*
1011 * Need to set msg_didout for the first line after an ":if",
1012 * otherwise the ":if" will be overwritten.
1013 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001014 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001016 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#ifdef FEAT_EVAL
1018 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1019#else
1020 0
1021#endif
1022 )) == NULL)
1023 {
1024 /* Don't call wait_return for aborted command line. The NULL
1025 * returned for the end of a sourced file or executed function
1026 * doesn't do this. */
1027 if (KeyTyped && !(flags & DOCMD_REPEAT))
1028 need_wait_return = FALSE;
1029 retval = FAIL;
1030 break;
1031 }
1032 used_getline = TRUE;
1033
1034 /*
1035 * Keep the first typed line. Clear it when more lines are typed.
1036 */
1037 if (flags & DOCMD_KEEPLINE)
1038 {
1039 vim_free(repeat_cmdline);
1040 if (count == 0)
1041 repeat_cmdline = vim_strsave(next_cmdline);
1042 else
1043 repeat_cmdline = NULL;
1044 }
1045 }
1046
1047 /* 3. Make a copy of the command so we can mess with it. */
1048 else if (cmdline_copy == NULL)
1049 {
1050 next_cmdline = vim_strsave(next_cmdline);
1051 if (next_cmdline == NULL)
1052 {
1053 EMSG(_(e_outofmem));
1054 retval = FAIL;
1055 break;
1056 }
1057 }
1058 cmdline_copy = next_cmdline;
1059
1060#ifdef FEAT_EVAL
1061 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 * Save the current line when inside a ":while" or ":for", and when
1063 * the command looks like a ":while" or ":for", because we may need it
1064 * later. When there is a '|' and another command, it is stored
1065 * separately, because we need to be able to jump back to it from an
1066 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 if (current_line == lines_ga.ga_len
1069 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001071 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
1073 retval = FAIL;
1074 break;
1075 }
1076 }
1077 did_endif = FALSE;
1078#endif
1079
1080 if (count++ == 0)
1081 {
1082 /*
1083 * All output from the commands is put below each other, without
1084 * waiting for a return. Don't do this when executing commands
1085 * from a script or when being called recursive (e.g. for ":e
1086 * +command file").
1087 */
1088 if (!(flags & DOCMD_NOWAIT) && !recursive)
1089 {
1090 msg_didout_before_start = msg_didout;
1091 msg_didany = FALSE; /* no output yet */
1092 msg_start();
1093 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001094 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 ++RedrawingDisabled;
1096 did_inc = TRUE;
1097 }
1098 }
1099
1100 if (p_verbose >= 15 && sourcing_name != NULL)
1101 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001103 verbose_enter_scroll();
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 smsg((char_u *)_("line %ld: %s"),
1106 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001107 if (msg_silent == 0)
1108 msg_puts((char_u *)"\n"); /* don't overwrite this */
1109
1110 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 --no_wait_return;
1112 }
1113
1114 /*
1115 * 2. Execute one '|' separated command.
1116 * do_one_cmd() will return NULL if there is no trailing '|'.
1117 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1118 */
1119 ++recursive;
1120 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1121#ifdef FEAT_EVAL
1122 &cstack,
1123#endif
1124 cmd_getline, cmd_cookie);
1125 --recursive;
1126
1127#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001128 if (cmd_cookie == (void *)&cmd_loop_cookie)
1129 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132#endif
1133
1134 if (next_cmdline == NULL)
1135 {
1136 vim_free(cmdline_copy);
1137 cmdline_copy = NULL;
1138#ifdef FEAT_CMDHIST
1139 /*
1140 * If the command was typed, remember it for the ':' register.
1141 * Do this AFTER executing the command to make :@: work.
1142 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001143 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 && new_last_cmdline != NULL)
1145 {
1146 vim_free(last_cmdline);
1147 last_cmdline = new_last_cmdline;
1148 new_last_cmdline = NULL;
1149 }
1150#endif
1151 }
1152 else
1153 {
1154 /* need to copy the command after the '|' to cmdline_copy, for the
1155 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001156 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 next_cmdline = cmdline_copy;
1158 }
1159
1160
1161#ifdef FEAT_EVAL
1162 /* reset did_emsg for a function that is not aborted by an error */
1163 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001164 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 && !func_has_abort(real_cookie))
1166 did_emsg = FALSE;
1167
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
1170 ++current_line;
1171
1172 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 * An ":endwhile", ":endfor" and ":continue" is handled here.
1174 * If we were executing commands, jump back to the ":while" or
1175 * ":for".
1176 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 /* Jump back to the matching ":while" or ":for". Be careful
1183 * not to use a cs_line[] from an entry that isn't a ":while"
1184 * or ":for": It would make "current_line" invalid and can
1185 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 if (!did_emsg && !got_int && !did_throw
1187 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001188 && (cstack.cs_flags[cstack.cs_idx]
1189 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 && cstack.cs_line[cstack.cs_idx] >= 0
1191 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1192 {
1193 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 /* remember we jumped there */
1195 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 line_breakcheck(); /* check if CTRL-C typed */
1197
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 /* Check for the next breakpoint at or after the ":while"
1199 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 if (breakpoint != NULL)
1201 {
1202 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001203 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 fname,
1205 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1206 *dbg_tick = debug_tick;
1207 }
1208 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001209 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001213 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1214 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 }
1216 }
1217
1218 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001219 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1225 }
1226 }
1227
1228 /*
1229 * When not inside any ":while" loop, clear remembered lines.
1230 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001231 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 {
1233 if (lines_ga.ga_len > 0)
1234 {
1235 sourcing_lnum =
1236 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1237 free_cmdlines(&lines_ga);
1238 }
1239 current_line = 0;
1240 }
1241
1242 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001243 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1244 * being restored at the ":endtry". Reset them here and set the
1245 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1246 * This includes the case where a missing ":endif", ":endwhile" or
1247 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001249 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001251 cstack.cs_lflags &= ~CSL_HAD_FINA;
1252 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1253 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 did_throw ? (void *)current_exception : NULL);
1255 did_emsg = got_int = did_throw = FALSE;
1256 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1257 }
1258
1259 /* Update global "trylevel" for recursive calls to do_cmdline() from
1260 * within this loop. */
1261 trylevel = initial_trylevel + cstack.cs_trylevel;
1262
1263 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001264 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 * files) is aborted because of an error, an interrupt, or an uncaught
1266 * exception, cancel everything. If it is left normally, reset
1267 * force_abort to get the non-EH compatible abortion behavior for
1268 * the rest of the script.
1269 */
1270 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1271 force_abort = FALSE;
1272
1273 /* Convert an interrupt to an exception if appropriate. */
1274 (void)do_intthrow(&cstack);
1275#endif /* FEAT_EVAL */
1276
1277 }
1278 /*
1279 * Continue executing command lines when:
1280 * - no CTRL-C typed, no aborting error, no exception thrown or try
1281 * conditionals need to be checked for executing finally clauses or
1282 * catching an interrupt exception
1283 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001284 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 * looping for ":source" command or function call.
1286 */
1287 while (!((got_int
1288#ifdef FEAT_EVAL
1289 || (did_emsg && force_abort) || did_throw
1290#endif
1291 )
1292#ifdef FEAT_EVAL
1293 && cstack.cs_trylevel == 0
1294#endif
1295 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001296 && !(did_emsg
1297#ifdef FEAT_EVAL
1298 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001299 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001300 * the :endtry to be missed. */
1301 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1302#endif
1303 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001304 && (getline_equal(fgetline, cookie, getexmodeline)
1305 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 && (next_cmdline != NULL
1307#ifdef FEAT_EVAL
1308 || cstack.cs_idx >= 0
1309#endif
1310 || (flags & DOCMD_REPEAT)));
1311
1312 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001313 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314#ifdef FEAT_EVAL
1315 free_cmdlines(&lines_ga);
1316 ga_clear(&lines_ga);
1317
1318 if (cstack.cs_idx >= 0)
1319 {
1320 /*
1321 * If a sourced file or executed function ran to its end, report the
1322 * unclosed conditional.
1323 */
1324 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001325 && ((getline_equal(fgetline, cookie, getsourceline)
1326 && !source_finished(fgetline, cookie))
1327 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 && !func_has_ended(real_cookie))))
1329 {
1330 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1331 EMSG(_(e_endtry));
1332 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1333 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001334 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1335 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 else
1337 EMSG(_(e_endif));
1338 }
1339
1340 /*
1341 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1342 * ":endtry" in a sourced file or executed function. If the try
1343 * conditional is in its finally clause, ignore anything pending.
1344 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001345 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 */
1347 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001348 {
1349 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1350
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001351 if (idx >= 0)
1352 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001353 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1354 &cstack.cs_looplevel);
1355 }
1356 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 trylevel = initial_trylevel;
1358 }
1359
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001360 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1361 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001363 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 ? (char_u *)"endfunction" : (char_u *)NULL);
1365
1366 if (trylevel == 0)
1367 {
1368 /*
1369 * When an exception is being thrown out of the outermost try
1370 * conditional, discard the uncaught exception, disable the conversion
1371 * of interrupts or errors to exceptions, and ensure that no more
1372 * commands are executed.
1373 */
1374 if (did_throw)
1375 {
1376 void *p = NULL;
1377 char_u *saved_sourcing_name;
1378 int saved_sourcing_lnum;
1379 struct msglist *messages = NULL, *next;
1380
1381 /*
1382 * If the uncaught exception is a user exception, report it as an
1383 * error. If it is an error exception, display the saved error
1384 * message now. For an interrupt exception, do nothing; the
1385 * interrupt message is given elsewhere.
1386 */
1387 switch (current_exception->type)
1388 {
1389 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001390 vim_snprintf((char *)IObuff, IOSIZE,
1391 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 current_exception->value);
1393 p = vim_strsave(IObuff);
1394 break;
1395 case ET_ERROR:
1396 messages = current_exception->messages;
1397 current_exception->messages = NULL;
1398 break;
1399 case ET_INTERRUPT:
1400 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
1402
1403 saved_sourcing_name = sourcing_name;
1404 saved_sourcing_lnum = sourcing_lnum;
1405 sourcing_name = current_exception->throw_name;
1406 sourcing_lnum = current_exception->throw_lnum;
1407 current_exception->throw_name = NULL;
1408
1409 discard_current_exception(); /* uses IObuff if 'verbose' */
1410 suppress_errthrow = TRUE;
1411 force_abort = TRUE;
1412
1413 if (messages != NULL)
1414 {
1415 do
1416 {
1417 next = messages->next;
1418 emsg(messages->msg);
1419 vim_free(messages->msg);
1420 vim_free(messages);
1421 messages = next;
1422 }
1423 while (messages != NULL);
1424 }
1425 else if (p != NULL)
1426 {
1427 emsg(p);
1428 vim_free(p);
1429 }
1430 vim_free(sourcing_name);
1431 sourcing_name = saved_sourcing_name;
1432 sourcing_lnum = saved_sourcing_lnum;
1433 }
1434
1435 /*
1436 * On an interrupt or an aborting error not converted to an exception,
1437 * disable the conversion of errors to exceptions. (Interrupts are not
1438 * converted any more, here.) This enables also the interrupt message
1439 * when force_abort is set and did_emsg unset in case of an interrupt
1440 * from a finally clause after an error.
1441 */
1442 else if (got_int || (did_emsg && force_abort))
1443 suppress_errthrow = TRUE;
1444 }
1445
1446 /*
1447 * The current cstack will be freed when do_cmdline() returns. An uncaught
1448 * exception will have to be rethrown in the previous cstack. If a function
1449 * has just returned or a script file was just finished and the previous
1450 * cstack belongs to the same function or, respectively, script file, it
1451 * will have to be checked for finally clauses to be executed due to the
1452 * ":return" or ":finish". This is done in do_one_cmd().
1453 */
1454 if (did_throw)
1455 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001456 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001458 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 && ex_nesting_level > func_level(real_cookie) + 1))
1460 {
1461 if (!did_throw)
1462 check_cstack = TRUE;
1463 }
1464 else
1465 {
1466 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001467 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 --ex_nesting_level;
1469 /*
1470 * Go to debug mode when returning from a function in which we are
1471 * single-stepping.
1472 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001473 if ((getline_equal(fgetline, cookie, getsourceline)
1474 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001476 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 ? (char_u *)_("End of sourced file")
1478 : (char_u *)_("End of function"));
1479 }
1480
1481 /*
1482 * Restore the exception environment (done after returning from the
1483 * debugger).
1484 */
1485 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001486 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487
1488 msg_list = saved_msg_list;
1489#endif /* FEAT_EVAL */
1490
1491 /*
1492 * If there was too much output to fit on the command line, ask the user to
1493 * hit return before redrawing the screen. With the ":global" command we do
1494 * this only once after the command is finished.
1495 */
1496 if (did_inc)
1497 {
1498 --RedrawingDisabled;
1499 --no_wait_return;
1500 msg_scroll = FALSE;
1501
1502 /*
1503 * When just finished an ":if"-":else" which was typed, no need to
1504 * wait for hit-return. Also for an error situation.
1505 */
1506 if (retval == FAIL
1507#ifdef FEAT_EVAL
1508 || (did_endif && KeyTyped && !did_emsg)
1509#endif
1510 )
1511 {
1512 need_wait_return = FALSE;
1513 msg_didany = FALSE; /* don't wait when restarting edit */
1514 }
1515 else if (need_wait_return)
1516 {
1517 /*
1518 * The msg_start() above clears msg_didout. The wait_return we do
1519 * here should not overwrite the command that may be shown before
1520 * doing that.
1521 */
1522 msg_didout |= msg_didout_before_start;
1523 wait_return(FALSE);
1524 }
1525 }
1526
Bram Moolenaar2a942252012-11-28 23:03:07 +01001527#ifdef FEAT_EVAL
1528 did_endif = FALSE; /* in case do_cmdline used recursively */
1529#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 /*
1531 * Reset if_level, in case a sourced script file contains more ":if" than
1532 * ":endif" (could be ":if x | foo | endif").
1533 */
1534 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001535#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001536
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 --call_depth;
1538 return retval;
1539}
1540
1541#ifdef FEAT_EVAL
1542/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001543 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 */
1545 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001546get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001548 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 wcmd_T *wp;
1550 char_u *line;
1551
1552 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1553 {
1554 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001557 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 if (cp->getline == NULL)
1559 line = getcmdline(c, 0L, indent);
1560 else
1561 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001562 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 ++cp->current_line;
1564
1565 return line;
1566 }
1567
1568 KeyTyped = FALSE;
1569 ++cp->current_line;
1570 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1571 sourcing_lnum = wp->lnum;
1572 return vim_strsave(wp->line);
1573}
1574
1575/*
1576 * Store a line in "gap" so that a ":while" loop can execute it again.
1577 */
1578 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001579store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 if (ga_grow(gap, 1) == FAIL)
1582 return FAIL;
1583 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1584 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1585 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 return OK;
1587}
1588
1589/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001590 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 */
1592 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001593free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594{
1595 while (gap->ga_len > 0)
1596 {
1597 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1598 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600}
1601#endif
1602
1603/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001604 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1605 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001608getline_equal(
1609 char_u *(*fgetline)(int, void *, int),
1610 void *cookie UNUSED, /* argument for fgetline() */
1611 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
1613#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001614 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001615 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
Bram Moolenaar89d40322006-08-29 15:30:07 +00001617 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001618 * function that's originally used to obtain the lines. This may be
1619 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001620 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001621 cp = (struct loop_cookie *)cookie;
1622 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 gp = cp->getline;
1625 cp = cp->cookie;
1626 }
1627 return gp == func;
1628#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001629 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#endif
1631}
1632
1633#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1634/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001635 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 * getline function. Otherwise return "cookie".
1637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001639getline_cookie(
1640 char_u *(*fgetline)(int, void *, int) UNUSED,
1641 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642{
1643# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001644 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001645 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
Bram Moolenaar89d40322006-08-29 15:30:07 +00001647 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001648 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001650 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001651 cp = (struct loop_cookie *)cookie;
1652 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
1654 gp = cp->getline;
1655 cp = cp->cookie;
1656 }
1657 return cp;
1658# else
1659 return cookie;
1660# endif
1661}
1662#endif
1663
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001664
1665/*
1666 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1667 * ":bwipeout", etc.
1668 * Returns the buffer number.
1669 */
1670 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001671compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001672{
1673 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001674 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675 int count = offset;
1676
1677 buf = firstbuf;
1678 while (buf->b_next != NULL && buf->b_fnum < lnum)
1679 buf = buf->b_next;
1680 while (count != 0)
1681 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001682 count += (offset < 0) ? 1 : -1;
1683 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1684 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001685 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001686 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001687 if (addr_type == ADDR_LOADED_BUFFERS)
1688 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001689 while (buf->b_ml.ml_mfp == NULL)
1690 {
1691 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1692 if (nextbuf == NULL)
1693 break;
1694 buf = nextbuf;
1695 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001696 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001697 /* we might have gone too far, last buffer is not loadedd */
1698 if (addr_type == ADDR_LOADED_BUFFERS)
1699 while (buf->b_ml.ml_mfp == NULL)
1700 {
1701 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1702 if (nextbuf == NULL)
1703 break;
1704 buf = nextbuf;
1705 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001706 return buf->b_fnum;
1707}
1708
Bram Moolenaarf240e182014-11-27 18:33:02 +01001709#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001710static int current_win_nr(win_T *win);
1711static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001712
1713 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001714current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001715{
1716 win_T *wp;
1717 int nr = 0;
1718
Bram Moolenaar29323592016-07-24 22:04:11 +02001719 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001720 {
1721 ++nr;
1722 if (wp == win)
1723 break;
1724 }
1725 return nr;
1726}
1727
1728 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001729current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001730{
1731 tabpage_T *tp;
1732 int nr = 0;
1733
Bram Moolenaar29323592016-07-24 22:04:11 +02001734 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001735 {
1736 ++nr;
1737 if (tp == tab)
1738 break;
1739 }
1740 return nr;
1741}
1742
1743# define CURRENT_WIN_NR current_win_nr(curwin)
1744# define LAST_WIN_NR current_win_nr(NULL)
1745# define CURRENT_TAB_NR current_tab_nr(curtab)
1746# define LAST_TAB_NR current_tab_nr(NULL)
1747#else
1748# define CURRENT_WIN_NR 1
1749# define LAST_WIN_NR 1
1750# define CURRENT_TAB_NR 1
1751# define LAST_TAB_NR 1
1752#endif
1753
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755/*
1756 * Execute one Ex command.
1757 *
1758 * If 'sourcing' is TRUE, the command will be included in the error message.
1759 *
1760 * 1. skip comment lines and leading space
1761 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001762 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001763 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001764 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001765 * 6. parse arguments
1766 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001768 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 *
1770 * This function may be called recursively!
1771 */
1772#if (_MSC_VER == 1200)
1773/*
Bram Moolenaared203462004-06-16 11:19:22 +00001774 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001776 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777#endif
1778 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001779do_one_cmd(
1780 char_u **cmdlinep,
1781 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001783 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001785 char_u *(*fgetline)(int, void *, int),
1786 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
1788 char_u *p;
1789 linenr_T lnum;
1790 long n;
1791 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001792 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 exarg_T ea; /* Ex command arguments */
1794 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001795 int save_msg_scroll = msg_scroll;
1796 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001798#ifdef HAVE_SANDBOX
1799 int did_sandbox = FALSE;
1800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 cmdmod_T save_cmdmod;
1802 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001803 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001804 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
1806 vim_memset(&ea, 0, sizeof(ea));
1807 ea.line1 = 1;
1808 ea.line2 = 1;
1809#ifdef FEAT_EVAL
1810 ++ex_nesting_level;
1811#endif
1812
Bram Moolenaar21691f82012-12-05 19:13:18 +01001813 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (quitmore
1815#ifdef FEAT_EVAL
1816 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001817 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001818#endif
1819#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001820 /* avoid that an autocommand, e.g. QuitPre, does this */
1821 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822#endif
1823 )
1824 --quitmore;
1825
1826 /*
1827 * Reset browse, confirm, etc.. They are restored when returning, for
1828 * recursive calls.
1829 */
1830 save_cmdmod = cmdmod;
1831 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1832
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001833 /* "#!anything" is handled like a comment. */
1834 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1835 goto doend;
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 /*
1838 * Repeat until no more command modifiers are found.
1839 */
1840 ea.cmd = *cmdlinep;
1841 for (;;)
1842 {
1843/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001844 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 */
1846 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1847 ++ea.cmd;
1848
1849 /* in ex mode, an empty line works like :+ */
1850 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001851 && (getline_equal(fgetline, cookie, getexmodeline)
1852 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001853 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 {
1855 ea.cmd = (char_u *)"+";
1856 ex_pressedreturn = TRUE;
1857 }
1858
1859 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001860 if (*ea.cmd == '"')
1861 goto doend;
1862 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001863 {
1864 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001869 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001871 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 switch (*p)
1873 {
1874 /* When adding an entry, also modify cmd_exists(). */
1875 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1876 break;
1877#ifdef FEAT_WINDOWS
1878 cmdmod.split |= WSP_ABOVE;
1879#endif
1880 continue;
1881
1882 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1883 {
1884#ifdef FEAT_WINDOWS
1885 cmdmod.split |= WSP_BELOW;
1886#endif
1887 continue;
1888 }
1889 if (checkforcmd(&ea.cmd, "browse", 3))
1890 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001891#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 cmdmod.browse = TRUE;
1893#endif
1894 continue;
1895 }
1896 if (!checkforcmd(&ea.cmd, "botright", 2))
1897 break;
1898#ifdef FEAT_WINDOWS
1899 cmdmod.split |= WSP_BOT;
1900#endif
1901 continue;
1902
1903 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1904 break;
1905#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1906 cmdmod.confirm = TRUE;
1907#endif
1908 continue;
1909
1910 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1911 {
1912 cmdmod.keepmarks = TRUE;
1913 continue;
1914 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001915 if (checkforcmd(&ea.cmd, "keepalt", 5))
1916 {
1917 cmdmod.keepalt = TRUE;
1918 continue;
1919 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001920 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1921 {
1922 cmdmod.keeppatterns = TRUE;
1923 continue;
1924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1926 break;
1927 cmdmod.keepjumps = TRUE;
1928 continue;
1929
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001930 case 'f': /* only accept ":filter {pat} cmd" */
1931 {
1932 char_u *reg_pat;
1933
1934 if (!checkforcmd(&p, "filter", 4)
1935 || *p == NUL || ends_excmd(*p))
1936 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001937 if (*p == '!')
1938 {
1939 cmdmod.filter_force = TRUE;
1940 p = skipwhite(p + 1);
1941 if (*p == NUL || ends_excmd(*p))
1942 break;
1943 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001944 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1945 if (p == NULL || *p == NUL)
1946 break;
1947 cmdmod.filter_regmatch.regprog =
1948 vim_regcomp(reg_pat, RE_MAGIC);
1949 if (cmdmod.filter_regmatch.regprog == NULL)
1950 break;
1951 ea.cmd = p;
1952 continue;
1953 }
1954
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 /* ":hide" and ":hide | cmd" are not modifiers */
1956 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1957 || *p == NUL || ends_excmd(*p))
1958 break;
1959 ea.cmd = p;
1960 cmdmod.hide = TRUE;
1961 continue;
1962
1963 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1964 {
1965 cmdmod.lockmarks = TRUE;
1966 continue;
1967 }
1968
1969 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1970 break;
1971#ifdef FEAT_WINDOWS
1972 cmdmod.split |= WSP_ABOVE;
1973#endif
1974 continue;
1975
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001976 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001977 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001978#ifdef FEAT_AUTOCMD
1979 if (cmdmod.save_ei == NULL)
1980 {
1981 /* Set 'eventignore' to "all". Restore the
1982 * existing option value later. */
1983 cmdmod.save_ei = vim_strsave(p_ei);
1984 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001985 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001986 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001987#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001988 continue;
1989 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001990 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001991 break;
1992 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001993 continue;
1994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1996 break;
1997#ifdef FEAT_WINDOWS
1998 cmdmod.split |= WSP_BELOW;
1999#endif
2000 continue;
2001
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002002 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
2003 {
2004#ifdef HAVE_SANDBOX
2005 if (!did_sandbox)
2006 ++sandbox;
2007 did_sandbox = TRUE;
2008#endif
2009 continue;
2010 }
2011 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002013 if (save_msg_silent == -1)
2014 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
2017 {
2018 /* ":silent!", but not "silent !cmd" */
2019 ea.cmd = skipwhite(ea.cmd + 1);
2020 ++emsg_silent;
2021 ++did_esilent;
2022 }
2023 continue;
2024
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002025 case 't': if (checkforcmd(&p, "tab", 3))
2026 {
2027#ifdef FEAT_WINDOWS
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002028 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01002029 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002030 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002031 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002032 else
2033 {
2034 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2035 {
2036 errormsg = (char_u *)_(e_invrange);
2037 goto doend;
2038 }
2039 cmdmod.tab = tabnr + 1;
2040 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002041 ea.cmd = p;
2042#endif
2043 continue;
2044 }
2045 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 break;
2047#ifdef FEAT_WINDOWS
2048 cmdmod.split |= WSP_TOP;
2049#endif
2050 continue;
2051
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002052 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2053 break;
2054 if (save_msg_silent == -1)
2055 save_msg_silent = msg_silent;
2056 msg_silent = 0;
2057 continue;
2058
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2060 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002061#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 cmdmod.split |= WSP_VERT;
2063#endif
2064 continue;
2065 }
2066 if (!checkforcmd(&p, "verbose", 4))
2067 break;
2068 if (verbose_save < 0)
2069 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002070 if (vim_isdigit(*ea.cmd))
2071 p_verbose = atoi((char *)ea.cmd);
2072 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 p_verbose = 1;
2074 ea.cmd = p;
2075 continue;
2076 }
2077 break;
2078 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002079 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
2081#ifdef FEAT_EVAL
2082 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2083 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2084#else
2085 ea.skip = (if_level > 0);
2086#endif
2087
2088#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002089# ifdef FEAT_PROFILE
2090 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002091 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002092 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002093 if (getline_equal(fgetline, cookie, get_func_line))
2094 func_line_exec(getline_cookie(fgetline, cookie));
2095 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002096 script_line_exec();
2097 }
2098#endif
2099
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 /* May go to debug mode. If this happens and the ">quit" debug command is
2101 * used, throw an interrupt exception and skip the next command. */
2102 dbg_check_breakpoint(&ea);
2103 if (!ea.skip && got_int)
2104 {
2105 ea.skip = TRUE;
2106 (void)do_intthrow(cstack);
2107 }
2108#endif
2109
2110/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002111 * 3. Skip over the range to find the command. Let "p" point to after it.
2112 *
2113 * We need the command to know what kind of range it uses.
2114 */
2115 cmd = ea.cmd;
2116 ea.cmd = skip_range(ea.cmd, NULL);
2117 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2118 ea.cmd = skipwhite(ea.cmd + 1);
2119 p = find_command(&ea, NULL);
2120
2121/*
2122 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 *
2124 * where 'addr' is:
2125 *
2126 * % (entire file)
2127 * $ [+-NUM]
2128 * 'x [+-NUM] (where x denotes a currently defined mark)
2129 * . [+-NUM]
2130 * [+-NUM]..
2131 * NUM
2132 *
2133 * The ea.cmd pointer is updated to point to the first character following the
2134 * range spec. If an initial address is found, but no second, the upper bound
2135 * is equal to the lower.
2136 */
2137
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002138 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002139 if (!IS_USER_CMDIDX(ea.cmdidx))
2140 {
2141 if (ea.cmdidx != CMD_SIZE)
2142 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2143 else
2144 ea.addr_type = ADDR_LINES;
2145
2146#ifdef FEAT_WINDOWS
2147 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002148 if (ea.cmdidx == CMD_wincmd && p != NULL)
2149 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002150#endif
2151 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002152
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002154 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 for (;;)
2156 {
2157 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002158 switch (ea.addr_type)
2159 {
2160 case ADDR_LINES:
2161 /* default is current line number */
2162 ea.line2 = curwin->w_cursor.lnum;
2163 break;
2164 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002165 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002166 break;
2167 case ADDR_ARGUMENTS:
2168 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002169 if (ea.line2 > ARGCOUNT)
2170 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002171 break;
2172 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002173 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002174 ea.line2 = curbuf->b_fnum;
2175 break;
2176 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002177 ea.line2 = CURRENT_TAB_NR;
2178 break;
2179 case ADDR_TABS_RELATIVE:
2180 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002181 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002182#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002183 case ADDR_QUICKFIX:
2184 ea.line2 = qf_get_cur_valid_idx(&ea);
2185 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002186#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002189 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002190 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 if (ea.cmd == NULL) /* error detected */
2192 goto doend;
2193 if (lnum == MAXLNUM)
2194 {
2195 if (*ea.cmd == '%') /* '%' - all lines */
2196 {
2197 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002198 switch (ea.addr_type)
2199 {
2200 case ADDR_LINES:
2201 ea.line1 = 1;
2202 ea.line2 = curbuf->b_ml.ml_line_count;
2203 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002204 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002205 {
2206 buf_T *buf = firstbuf;
2207
2208 while (buf->b_next != NULL
2209 && buf->b_ml.ml_mfp == NULL)
2210 buf = buf->b_next;
2211 ea.line1 = buf->b_fnum;
2212 buf = lastbuf;
2213 while (buf->b_prev != NULL
2214 && buf->b_ml.ml_mfp == NULL)
2215 buf = buf->b_prev;
2216 ea.line2 = buf->b_fnum;
2217 break;
2218 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002219 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002220 ea.line1 = firstbuf->b_fnum;
2221 ea.line2 = lastbuf->b_fnum;
2222 break;
2223 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002224 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002225 if (IS_USER_CMDIDX(ea.cmdidx))
2226 {
2227 ea.line1 = 1;
2228 ea.line2 = ea.addr_type == ADDR_WINDOWS
2229 ? LAST_WIN_NR : LAST_TAB_NR;
2230 }
2231 else
2232 {
2233 /* there is no Vim command which uses '%' and
2234 * ADDR_WINDOWS or ADDR_TABS */
2235 errormsg = (char_u *)_(e_invrange);
2236 goto doend;
2237 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002238 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002239 case ADDR_TABS_RELATIVE:
2240 errormsg = (char_u *)_(e_invrange);
2241 goto doend;
2242 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002243 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002244 if (ARGCOUNT == 0)
2245 ea.line1 = ea.line2 = 0;
2246 else
2247 {
2248 ea.line1 = 1;
2249 ea.line2 = ARGCOUNT;
2250 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002251 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002252#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002253 case ADDR_QUICKFIX:
2254 ea.line1 = 1;
2255 ea.line2 = qf_get_size(&ea);
2256 if (ea.line2 == 0)
2257 ea.line2 = 1;
2258 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002259#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 ++ea.addr_count;
2262 }
2263 /* '*' - visual area */
2264 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2265 {
2266 pos_T *fp;
2267
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002268 if (ea.addr_type != ADDR_LINES)
2269 {
2270 errormsg = (char_u *)_(e_invrange);
2271 goto doend;
2272 }
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 ++ea.cmd;
2275 if (!ea.skip)
2276 {
2277 fp = getmark('<', FALSE);
2278 if (check_mark(fp) == FAIL)
2279 goto doend;
2280 ea.line1 = fp->lnum;
2281 fp = getmark('>', FALSE);
2282 if (check_mark(fp) == FAIL)
2283 goto doend;
2284 ea.line2 = fp->lnum;
2285 ++ea.addr_count;
2286 }
2287 }
2288 }
2289 else
2290 ea.line2 = lnum;
2291 ea.addr_count++;
2292
2293 if (*ea.cmd == ';')
2294 {
2295 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002298 /* don't leave the cursor on an illegal line */
2299 check_cursor_lnum();
2300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
2302 else if (*ea.cmd != ',')
2303 break;
2304 ++ea.cmd;
2305 }
2306
2307 /* One address given: set start and end lines */
2308 if (ea.addr_count == 1)
2309 {
2310 ea.line1 = ea.line2;
2311 /* ... but only implicit: really no address given */
2312 if (lnum == MAXLNUM)
2313 ea.addr_count = 0;
2314 }
2315
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002317 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 */
2319
2320 /*
2321 * Skip ':' and any white space
2322 */
2323 ea.cmd = skipwhite(ea.cmd);
2324 while (*ea.cmd == ':')
2325 ea.cmd = skipwhite(ea.cmd + 1);
2326
2327 /*
2328 * If we got a line, but no command, then go to the line.
2329 * If we find a '|' or '\n' we set ea.nextcmd.
2330 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002331 if (*ea.cmd == NUL || *ea.cmd == '"'
2332 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
2334 /*
2335 * strange vi behaviour:
2336 * ":3" jumps to line 3
2337 * ":3|..." prints line 3
2338 * ":|" prints current line
2339 */
2340 if (ea.skip) /* skip this if inside :if */
2341 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002342 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 ea.cmdidx = CMD_print;
2345 ea.argt = RANGE+COUNT+TRLBAR;
2346 if ((errormsg = invalid_range(&ea)) == NULL)
2347 {
2348 correct_range(&ea);
2349 ex_print(&ea);
2350 }
2351 }
2352 else if (ea.addr_count != 0)
2353 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002354 if (ea.line2 > curbuf->b_ml.ml_line_count)
2355 {
2356 /* With '-' in 'cpoptions' a line number past the file is an
2357 * error, otherwise put it at the end of the file. */
2358 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2359 ea.line2 = -1;
2360 else
2361 ea.line2 = curbuf->b_ml.ml_line_count;
2362 }
2363
2364 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002365 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 else
2367 {
2368 if (ea.line2 == 0)
2369 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 else
2371 curwin->w_cursor.lnum = ea.line2;
2372 beginline(BL_SOL | BL_FIX);
2373 }
2374 }
2375 goto doend;
2376 }
2377
Bram Moolenaard5005162014-08-22 23:05:54 +02002378#ifdef FEAT_AUTOCMD
2379 /* If this looks like an undefined user command and there are CmdUndefined
2380 * autocommands defined, trigger the matching autocommands. */
2381 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2382 && ASCII_ISUPPER(*ea.cmd)
2383 && has_cmdundefined())
2384 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002385 int ret;
2386
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002387 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002388 while (ASCII_ISALNUM(*p))
2389 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002390 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002391 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2392 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002393 /* If the autocommands did something and didn't cause an error, try
2394 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002395 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002396 }
2397#endif
2398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399#ifdef FEAT_USR_CMDS
2400 if (p == NULL)
2401 {
2402 if (!ea.skip)
2403 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2404 goto doend;
2405 }
2406 /* Check for wrong commands. */
2407 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2408 {
2409 errormsg = uc_fun_cmd();
2410 goto doend;
2411 }
2412#endif
2413 if (ea.cmdidx == CMD_SIZE)
2414 {
2415 if (!ea.skip)
2416 {
2417 STRCPY(IObuff, _("E492: Not an editor command"));
2418 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002419 {
2420 /* If the modifier was parsed OK the error must be in the
2421 * following command */
2422 if (after_modifier != NULL)
2423 append_command(after_modifier);
2424 else
2425 append_command(*cmdlinep);
2426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002428 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
2430 goto doend;
2431 }
2432
Bram Moolenaar958636c2014-10-21 20:01:58 +02002433 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2434 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002435#ifdef HAVE_EX_SCRIPT_NI
2436 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2437#endif
2438 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440#ifndef FEAT_EVAL
2441 /*
2442 * When the expression evaluation is disabled, recognize the ":if" and
2443 * ":endif" commands and ignore everything in between it.
2444 */
2445 if (ea.cmdidx == CMD_if)
2446 ++if_level;
2447 if (if_level)
2448 {
2449 if (ea.cmdidx == CMD_endif)
2450 --if_level;
2451 goto doend;
2452 }
2453
2454#endif
2455
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002456 /* forced commands */
2457 if (*p == '!' && ea.cmdidx != CMD_substitute
2458 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
2460 ++p;
2461 ea.forceit = TRUE;
2462 }
2463 else
2464 ea.forceit = FALSE;
2465
2466/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002467 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002469 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002470 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
2472 if (!ea.skip)
2473 {
2474#ifdef HAVE_SANDBOX
2475 if (sandbox != 0 && !(ea.argt & SBOXOK))
2476 {
2477 /* Command not allowed in sandbox. */
2478 errormsg = (char_u *)_(e_sandbox);
2479 goto doend;
2480 }
2481#endif
2482 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2483 {
2484 /* Command not allowed in non-'modifiable' buffer */
2485 errormsg = (char_u *)_(e_modifiable);
2486 goto doend;
2487 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002488
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002489 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002490 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002492 /* Command not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02002493 errormsg = get_text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 goto doend;
2495 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002496#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002497 /* Disallow editing another buffer when "curbuf_lock" is set.
2498 * Do allow ":edit" (check for argument later).
2499 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002500 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002501 && ea.cmdidx != CMD_edit
2502 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002503 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002504 && curbuf_locked())
2505 goto doend;
2506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
2508 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2509 {
2510 /* no range allowed */
2511 errormsg = (char_u *)_(e_norange);
2512 goto doend;
2513 }
2514 }
2515
2516 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2517 {
2518 errormsg = (char_u *)_(e_nobang);
2519 goto doend;
2520 }
2521
2522 /*
2523 * Don't complain about the range if it is not used
2524 * (could happen if line_count is accidentally set to 0).
2525 */
2526 if (!ea.skip && !ni)
2527 {
2528 /*
2529 * If the range is backwards, ask for confirmation and, if given, swap
2530 * ea.line1 & ea.line2 so it's forwards again.
2531 * When global command is busy, don't ask, will fail below.
2532 */
2533 if (!global_busy && ea.line1 > ea.line2)
2534 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002535 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002537 if (sourcing || exmode_active)
2538 {
2539 errormsg = (char_u *)_("E493: Backwards range given");
2540 goto doend;
2541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 if (ask_yesno((char_u *)
2543 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002544 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 }
2546 lnum = ea.line1;
2547 ea.line1 = ea.line2;
2548 ea.line2 = lnum;
2549 }
2550 if ((errormsg = invalid_range(&ea)) != NULL)
2551 goto doend;
2552 }
2553
2554 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2555 ea.line2 = 1;
2556
2557 correct_range(&ea);
2558
2559#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002560 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2561 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
2563 /* Put the first line at the start of a closed fold, put the last line
2564 * at the end of a closed fold. */
2565 (void)hasFolding(ea.line1, &ea.line1, NULL);
2566 (void)hasFolding(ea.line2, NULL, &ea.line2);
2567 }
2568#endif
2569
2570#ifdef FEAT_QUICKFIX
2571 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002572 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 * option here, so things like % get expanded.
2574 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002575 p = replace_makeprg(&ea, p, cmdlinep);
2576 if (p == NULL)
2577 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578#endif
2579
2580 /*
2581 * Skip to start of argument.
2582 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2583 */
2584 if (ea.cmdidx == CMD_bang)
2585 ea.arg = p;
2586 else
2587 ea.arg = skipwhite(p);
2588
2589 /*
2590 * Check for "++opt=val" argument.
2591 * Must be first, allow ":w ++enc=utf8 !cmd"
2592 */
2593 if (ea.argt & ARGOPT)
2594 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2595 if (getargopt(&ea) == FAIL && !ni)
2596 {
2597 errormsg = (char_u *)_(e_invarg);
2598 goto doend;
2599 }
2600
2601 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2602 {
2603 if (*ea.arg == '>') /* append */
2604 {
2605 if (*++ea.arg != '>') /* typed wrong */
2606 {
2607 errormsg = (char_u *)_("E494: Use w or w>>");
2608 goto doend;
2609 }
2610 ea.arg = skipwhite(ea.arg + 1);
2611 ea.append = TRUE;
2612 }
2613 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2614 {
2615 ++ea.arg;
2616 ea.usefilter = TRUE;
2617 }
2618 }
2619
2620 if (ea.cmdidx == CMD_read)
2621 {
2622 if (ea.forceit)
2623 {
2624 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2625 ea.forceit = FALSE;
2626 }
2627 else if (*ea.arg == '!') /* :r !filter */
2628 {
2629 ++ea.arg;
2630 ea.usefilter = TRUE;
2631 }
2632 }
2633
2634 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2635 {
2636 ea.amount = 1;
2637 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2638 {
2639 ++ea.arg;
2640 ++ea.amount;
2641 }
2642 ea.arg = skipwhite(ea.arg);
2643 }
2644
2645 /*
2646 * Check for "+command" argument, before checking for next command.
2647 * Don't do this for ":read !cmd" and ":write !cmd".
2648 */
2649 if ((ea.argt & EDITCMD) && !ea.usefilter)
2650 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2651
2652 /*
2653 * Check for '|' to separate commands and '"' to start comments.
2654 * Don't do this for ":read !cmd" and ":write !cmd".
2655 */
2656 if ((ea.argt & TRLBAR) && !ea.usefilter)
2657 separate_nextcmd(&ea);
2658
2659 /*
2660 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002661 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2662 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002664 else if (ea.cmdidx == CMD_bang
2665 || ea.cmdidx == CMD_global
2666 || ea.cmdidx == CMD_vglobal
2667 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 {
2669 for (p = ea.arg; *p; ++p)
2670 {
2671 /* Remove one backslash before a newline, so that it's possible to
2672 * pass a newline to the shell and also a newline that is preceded
2673 * with a backslash. This makes it impossible to end a shell
2674 * command in a backslash, but that doesn't appear useful.
2675 * Halving the number of backslashes is incompatible with previous
2676 * versions. */
2677 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002678 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 else if (*p == '\n')
2680 {
2681 ea.nextcmd = p + 1;
2682 *p = NUL;
2683 break;
2684 }
2685 }
2686 }
2687
2688 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2689 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002690 buf_T *buf;
2691
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002693 switch (ea.addr_type)
2694 {
2695 case ADDR_LINES:
2696 ea.line2 = curbuf->b_ml.ml_line_count;
2697 break;
2698 case ADDR_LOADED_BUFFERS:
2699 buf = firstbuf;
2700 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2701 buf = buf->b_next;
2702 ea.line1 = buf->b_fnum;
2703 buf = lastbuf;
2704 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2705 buf = buf->b_prev;
2706 ea.line2 = buf->b_fnum;
2707 break;
2708 case ADDR_BUFFERS:
2709 ea.line1 = firstbuf->b_fnum;
2710 ea.line2 = lastbuf->b_fnum;
2711 break;
2712 case ADDR_WINDOWS:
2713 ea.line2 = LAST_WIN_NR;
2714 break;
2715 case ADDR_TABS:
2716 ea.line2 = LAST_TAB_NR;
2717 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002718 case ADDR_TABS_RELATIVE:
2719 ea.line2 = 1;
2720 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002721 case ADDR_ARGUMENTS:
2722 if (ARGCOUNT == 0)
2723 ea.line1 = ea.line2 = 0;
2724 else
2725 ea.line2 = ARGCOUNT;
2726 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002727#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002728 case ADDR_QUICKFIX:
2729 ea.line2 = qf_get_size(&ea);
2730 if (ea.line2 == 0)
2731 ea.line2 = 1;
2732 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002733#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736
2737 /* accept numbered register only when no count allowed (:put) */
2738 if ( (ea.argt & REGSTR)
2739 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002740 /* Do not allow register = for user commands */
2741 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2743 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002744#ifndef FEAT_CLIPBOARD
2745 /* check these explicitly for a more specific error message */
2746 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002748 errormsg = (char_u *)_(e_invalidreg);
2749 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 }
2751#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002752 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2753 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002754 {
2755 ea.regname = *ea.arg++;
2756#ifdef FEAT_EVAL
2757 /* for '=' register: accept the rest of the line as an expression */
2758 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2759 {
2760 set_expr_line(vim_strsave(ea.arg));
2761 ea.arg += STRLEN(ea.arg);
2762 }
2763#endif
2764 ea.arg = skipwhite(ea.arg);
2765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
2767
2768 /*
2769 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2770 * count, it's a buffer name.
2771 */
2772 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2773 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2774 || vim_iswhite(*p)))
2775 {
2776 n = getdigits(&ea.arg);
2777 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002778 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
2780 errormsg = (char_u *)_(e_zerocount);
2781 goto doend;
2782 }
2783 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2784 {
2785 ea.line2 = n;
2786 if (ea.addr_count == 0)
2787 ea.addr_count = 1;
2788 }
2789 else
2790 {
2791 ea.line1 = ea.line2;
2792 ea.line2 += n - 1;
2793 ++ea.addr_count;
2794 /*
2795 * Be vi compatible: no error message for out of range.
2796 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002797 if (ea.addr_type == ADDR_LINES
2798 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 ea.line2 = curbuf->b_ml.ml_line_count;
2800 }
2801 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002802
2803 /*
2804 * Check for flags: 'l', 'p' and '#'.
2805 */
2806 if (ea.argt & EXFLAGS)
2807 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002809 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002810 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 {
2812 errormsg = (char_u *)_(e_trailing);
2813 goto doend;
2814 }
2815
2816 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2817 {
2818 errormsg = (char_u *)_(e_argreq);
2819 goto doend;
2820 }
2821
2822#ifdef FEAT_EVAL
2823 /*
2824 * Skip the command when it's not going to be executed.
2825 * The commands like :if, :endif, etc. always need to be executed.
2826 * Also make an exception for commands that handle a trailing command
2827 * themselves.
2828 */
2829 if (ea.skip)
2830 {
2831 switch (ea.cmdidx)
2832 {
2833 /* commands that need evaluation */
2834 case CMD_while:
2835 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002836 case CMD_for:
2837 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 case CMD_if:
2839 case CMD_elseif:
2840 case CMD_else:
2841 case CMD_endif:
2842 case CMD_try:
2843 case CMD_catch:
2844 case CMD_finally:
2845 case CMD_endtry:
2846 case CMD_function:
2847 break;
2848
2849 /* Commands that handle '|' themselves. Check: A command should
2850 * either have the TRLBAR flag, appear in this list or appear in
2851 * the list at ":help :bar". */
2852 case CMD_aboveleft:
2853 case CMD_and:
2854 case CMD_belowright:
2855 case CMD_botright:
2856 case CMD_browse:
2857 case CMD_call:
2858 case CMD_confirm:
2859 case CMD_delfunction:
2860 case CMD_djump:
2861 case CMD_dlist:
2862 case CMD_dsearch:
2863 case CMD_dsplit:
2864 case CMD_echo:
2865 case CMD_echoerr:
2866 case CMD_echomsg:
2867 case CMD_echon:
2868 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002869 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 case CMD_help:
2871 case CMD_hide:
2872 case CMD_ijump:
2873 case CMD_ilist:
2874 case CMD_isearch:
2875 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002876 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 case CMD_keepjumps:
2878 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002879 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 case CMD_leftabove:
2881 case CMD_let:
2882 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002883 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002885 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002886 case CMD_noautocmd:
2887 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 case CMD_perl:
2889 case CMD_psearch:
2890 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002891 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002892 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 case CMD_return:
2894 case CMD_rightbelow:
2895 case CMD_ruby:
2896 case CMD_silent:
2897 case CMD_smagic:
2898 case CMD_snomagic:
2899 case CMD_substitute:
2900 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002901 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 case CMD_tcl:
2903 case CMD_throw:
2904 case CMD_tilde:
2905 case CMD_topleft:
2906 case CMD_unlet:
2907 case CMD_verbose:
2908 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002909 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 break;
2911
2912 default: goto doend;
2913 }
2914 }
2915#endif
2916
2917 if (ea.argt & XFILE)
2918 {
2919 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2920 goto doend;
2921 }
2922
2923#ifdef FEAT_LISTCMDS
2924 /*
2925 * Accept buffer name. Cannot be used at the same time with a buffer
2926 * number. Don't do this for a user command.
2927 */
2928 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002929 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
2931 /*
2932 * :bdelete, :bwipeout and :bunload take several arguments, separated
2933 * by spaces: find next space (skipping over escaped characters).
2934 * The others take one argument: ignore trailing spaces.
2935 */
2936 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2937 || ea.cmdidx == CMD_bunload)
2938 p = skiptowhite_esc(ea.arg);
2939 else
2940 {
2941 p = ea.arg + STRLEN(ea.arg);
2942 while (p > ea.arg && vim_iswhite(p[-1]))
2943 --p;
2944 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002945 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2946 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 if (ea.line2 < 0) /* failed */
2948 goto doend;
2949 ea.addr_count = 1;
2950 ea.arg = skipwhite(p);
2951 }
2952#endif
2953
2954/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002955 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 *
2957 * The "ea" structure holds the arguments that can be used.
2958 */
2959 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002960 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 ea.cookie = cookie;
2962#ifdef FEAT_EVAL
2963 ea.cstack = cstack;
2964#endif
2965
2966#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002967 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 {
2969 /*
2970 * Execute a user-defined command.
2971 */
2972 do_ucmd(&ea);
2973 }
2974 else
2975#endif
2976 {
2977 /*
2978 * Call the function to execute the command.
2979 */
2980 ea.errmsg = NULL;
2981 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2982 if (ea.errmsg != NULL)
2983 errormsg = (char_u *)_(ea.errmsg);
2984 }
2985
2986#ifdef FEAT_EVAL
2987 /*
2988 * If the command just executed called do_cmdline(), any throw or ":return"
2989 * or ":finish" encountered there must also check the cstack of the still
2990 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2991 * exception, or reanimate a returned function or finished script file and
2992 * return or finish it again.
2993 */
2994 if (need_rethrow)
2995 do_throw(cstack);
2996 else if (check_cstack)
2997 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002998 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003000 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 && current_func_returned())
3002 do_return(&ea, TRUE, FALSE, NULL);
3003 }
3004 need_rethrow = check_cstack = FALSE;
3005#endif
3006
3007doend:
3008 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
3009 curwin->w_cursor.lnum = 1;
3010
3011 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
3012 {
3013 if (sourcing)
3014 {
3015 if (errormsg != IObuff)
3016 {
3017 STRCPY(IObuff, errormsg);
3018 errormsg = IObuff;
3019 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003020 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 }
3022 emsg(errormsg);
3023 }
3024#ifdef FEAT_EVAL
3025 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02003026 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
3027 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028#endif
3029
3030 if (verbose_save >= 0)
3031 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003032#ifdef FEAT_AUTOCMD
3033 if (cmdmod.save_ei != NULL)
3034 {
3035 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003036 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
3037 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003038 free_string_option(cmdmod.save_ei);
3039 }
3040#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003041 if (cmdmod.filter_regmatch.regprog != NULL)
3042 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
3044 cmdmod = save_cmdmod;
3045
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003046 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 {
3048 /* messages could be enabled for a serious error, need to check if the
3049 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003050 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003051 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 emsg_silent -= did_esilent;
3053 if (emsg_silent < 0)
3054 emsg_silent = 0;
3055 /* Restore msg_scroll, it's set by file I/O commands, even when no
3056 * message is actually displayed. */
3057 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003058
3059 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3060 * somewhere in the line. Put it back in the first column. */
3061 if (redirecting())
3062 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
3064
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003065#ifdef HAVE_SANDBOX
3066 if (did_sandbox)
3067 --sandbox;
3068#endif
3069
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3071 ea.nextcmd = NULL;
3072
3073#ifdef FEAT_EVAL
3074 --ex_nesting_level;
3075#endif
3076
3077 return ea.nextcmd;
3078}
3079#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003080 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081#endif
3082
3083/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003084 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 * If there is a match advance "pp" to the argument and return TRUE.
3086 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003087 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003088checkforcmd(
3089 char_u **pp, /* start of command */
3090 char *cmd, /* name of command */
3091 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 int i;
3094
3095 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003096 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 break;
3098 if (i >= len && !isalpha((*pp)[i]))
3099 {
3100 *pp = skipwhite(*pp + i);
3101 return TRUE;
3102 }
3103 return FALSE;
3104}
3105
3106/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003107 * Append "cmd" to the error message in IObuff.
3108 * Takes care of limiting the length and handling 0xa0, which would be
3109 * invisible otherwise.
3110 */
3111 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003112append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003113{
3114 char_u *s = cmd;
3115 char_u *d;
3116
3117 STRCAT(IObuff, ": ");
3118 d = IObuff + STRLEN(IObuff);
3119 while (*s != NUL && d - IObuff < IOSIZE - 7)
3120 {
3121 if (
3122#ifdef FEAT_MBYTE
3123 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3124#endif
3125 *s == 0xa0)
3126 {
3127 s +=
3128#ifdef FEAT_MBYTE
3129 enc_utf8 ? 2 :
3130#endif
3131 1;
3132 STRCPY(d, "<a0>");
3133 d += 4;
3134 }
3135 else
3136 MB_COPY_CHAR(s, d);
3137 }
3138 *d = NUL;
3139}
3140
3141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003143 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003145 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 * Returns NULL for an ambiguous user command.
3147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003149find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
3151 int len;
3152 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003153 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
3155 /*
3156 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003157 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 * - the 'k' command can directly be followed by any character.
3159 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003160 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003162 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 */
3164 p = eap->cmd;
3165 if (*p == 'k')
3166 {
3167 eap->cmdidx = CMD_k;
3168 ++p;
3169 }
3170 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003171 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3172 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 || p[1] == 'g'
3174 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3175 || p[1] == 'I'
3176 || (p[1] == 'r' && p[2] != 'e')))
3177 {
3178 eap->cmdidx = CMD_substitute;
3179 ++p;
3180 }
3181 else
3182 {
3183 while (ASCII_ISALPHA(*p))
3184 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003185 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003186 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003187 while (ASCII_ISALNUM(*p))
3188 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 /* check for non-alpha command */
3191 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3192 ++p;
3193 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003194 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3195 {
3196 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3197 * :delete with the 'l' flag. Same for 'p'. */
3198 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003199 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003200 break;
3201 if (i == len - 1)
3202 {
3203 --len;
3204 if (p[-1] == 'l')
3205 eap->flags |= EXFLAG_LIST;
3206 else
3207 eap->flags |= EXFLAG_PRINT;
3208 }
3209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210
3211 if (ASCII_ISLOWER(*eap->cmd))
3212 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3213 else
3214 eap->cmdidx = cmdidxs[26];
3215
3216 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3217 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3218 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3219 (size_t)len) == 0)
3220 {
3221#ifdef FEAT_EVAL
3222 if (full != NULL
3223 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3224 *full = TRUE;
3225#endif
3226 break;
3227 }
3228
3229#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003230 /* Look for a user defined command as a last resort. Let ":Print" be
3231 * overruled by a user defined command. */
3232 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3233 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003235 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 while (ASCII_ISALNUM(*p))
3237 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003238 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 }
3240#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003241 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 eap->cmdidx = CMD_SIZE;
3243 }
3244
3245 return p;
3246}
3247
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003248#ifdef FEAT_USR_CMDS
3249/*
3250 * Search for a user command that matches "eap->cmd".
3251 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3252 * Return a pointer to just after the command.
3253 * Return NULL if there is no matching command.
3254 */
3255 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003256find_ucmd(
3257 exarg_T *eap,
3258 char_u *p, /* end of the command (possibly including count) */
3259 int *full, /* set to TRUE for a full match */
3260 expand_T *xp, /* used for completion, NULL otherwise */
3261 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003262{
3263 int len = (int)(p - eap->cmd);
3264 int j, k, matchlen = 0;
3265 ucmd_T *uc;
3266 int found = FALSE;
3267 int possible = FALSE;
3268 char_u *cp, *np; /* Point into typed cmd and test name */
3269 garray_T *gap;
3270 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3271 only full match global is accepted. */
3272
3273 /*
3274 * Look for buffer-local user commands first, then global ones.
3275 */
3276 gap = &curbuf->b_ucmds;
3277 for (;;)
3278 {
3279 for (j = 0; j < gap->ga_len; ++j)
3280 {
3281 uc = USER_CMD_GA(gap, j);
3282 cp = eap->cmd;
3283 np = uc->uc_name;
3284 k = 0;
3285 while (k < len && *np != NUL && *cp++ == *np++)
3286 k++;
3287 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3288 {
3289 /* If finding a second match, the command is ambiguous. But
3290 * not if a buffer-local command wasn't a full match and a
3291 * global command is a full match. */
3292 if (k == len && found && *np != NUL)
3293 {
3294 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003295 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003296 amb_local = TRUE;
3297 }
3298
3299 if (!found || (k == len && *np == NUL))
3300 {
3301 /* If we matched up to a digit, then there could
3302 * be another command including the digit that we
3303 * should use instead.
3304 */
3305 if (k == len)
3306 found = TRUE;
3307 else
3308 possible = TRUE;
3309
3310 if (gap == &ucmds)
3311 eap->cmdidx = CMD_USER;
3312 else
3313 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003314 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003315 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003316 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003317
3318# ifdef FEAT_CMDL_COMPL
3319 if (compl != NULL)
3320 *compl = uc->uc_compl;
3321# ifdef FEAT_EVAL
3322 if (xp != NULL)
3323 {
3324 xp->xp_arg = uc->uc_compl_arg;
3325 xp->xp_scriptID = uc->uc_scriptID;
3326 }
3327# endif
3328# endif
3329 /* Do not search for further abbreviations
3330 * if this is an exact match. */
3331 matchlen = k;
3332 if (k == len && *np == NUL)
3333 {
3334 if (full != NULL)
3335 *full = TRUE;
3336 amb_local = FALSE;
3337 break;
3338 }
3339 }
3340 }
3341 }
3342
3343 /* Stop if we found a full match or searched all. */
3344 if (j < gap->ga_len || gap == &ucmds)
3345 break;
3346 gap = &ucmds;
3347 }
3348
3349 /* Only found ambiguous matches. */
3350 if (amb_local)
3351 {
3352 if (xp != NULL)
3353 xp->xp_context = EXPAND_UNSUCCESSFUL;
3354 return NULL;
3355 }
3356
3357 /* The match we found may be followed immediately by a number. Move "p"
3358 * back to point to it. */
3359 if (found || possible)
3360 return p + (matchlen - len);
3361 return p;
3362}
3363#endif
3364
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003366static struct cmdmod
3367{
3368 char *name;
3369 int minlen;
3370 int has_count; /* :123verbose :3tab */
3371} cmdmods[] = {
3372 {"aboveleft", 3, FALSE},
3373 {"belowright", 3, FALSE},
3374 {"botright", 2, FALSE},
3375 {"browse", 3, FALSE},
3376 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003377 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003378 {"hide", 3, FALSE},
3379 {"keepalt", 5, FALSE},
3380 {"keepjumps", 5, FALSE},
3381 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003382 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003383 {"leftabove", 5, FALSE},
3384 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003385 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003386 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003387 {"rightbelow", 6, FALSE},
3388 {"sandbox", 3, FALSE},
3389 {"silent", 3, FALSE},
3390 {"tab", 3, TRUE},
3391 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003392 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003393 {"verbose", 4, TRUE},
3394 {"vertical", 4, FALSE},
3395};
3396
3397/*
3398 * Return length of a command modifier (including optional count).
3399 * Return zero when it's not a modifier.
3400 */
3401 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003402modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003403{
3404 int i, j;
3405 char_u *p = cmd;
3406
3407 if (VIM_ISDIGIT(*cmd))
3408 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003409 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003410 {
3411 for (j = 0; p[j] != NUL; ++j)
3412 if (p[j] != cmdmods[i].name[j])
3413 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003414 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003415 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003416 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003417 }
3418 return 0;
3419}
3420
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421/*
3422 * Return > 0 if an Ex command "name" exists.
3423 * Return 2 if there is an exact match.
3424 * Return 3 if there is an ambiguous match.
3425 */
3426 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003427cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
3429 exarg_T ea;
3430 int full = FALSE;
3431 int i;
3432 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003433 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003436 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 {
3438 for (j = 0; name[j] != NUL; ++j)
3439 if (name[j] != cmdmods[i].name[j])
3440 break;
3441 if (name[j] == NUL && j >= cmdmods[i].minlen)
3442 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3443 }
3444
Bram Moolenaara9587612006-05-04 21:47:50 +00003445 /* Check built-in commands and user defined commands.
3446 * For ":2match" and ":3match" we need to skip the number. */
3447 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003449 p = find_command(&ea, &full);
3450 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003452 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3453 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003454 if (*skipwhite(p) != NUL)
3455 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3457}
3458#endif
3459
3460/*
3461 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3462 * we don't need/want deleted. Maybe this could be done better if we didn't
3463 * repeat all this stuff. The only problem is that they may not stay
3464 * perfectly compatible with each other, but then the command line syntax
3465 * probably won't change that much -- webb.
3466 */
3467 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003468set_one_cmd_context(
3469 expand_T *xp,
3470 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471{
3472 char_u *p;
3473 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003474 int len = 0;
3475 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3477 int compl = EXPAND_NOTHING;
3478#endif
3479#ifdef FEAT_CMDL_COMPL
3480 int delim;
3481#endif
3482 int forceit = FALSE;
3483 int usefilter = FALSE; /* filter instead of file name */
3484
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003485 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 xp->xp_pattern = buff;
3487 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003488 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003491 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 */
3493 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3494 ;
3495 xp->xp_pattern = cmd;
3496
3497 if (*cmd == NUL)
3498 return NULL;
3499 if (*cmd == '"') /* ignore comment lines */
3500 {
3501 xp->xp_context = EXPAND_NOTHING;
3502 return NULL;
3503 }
3504
3505/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003506 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 */
3508 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 xp->xp_pattern = cmd;
3510 if (*cmd == NUL)
3511 return NULL;
3512 if (*cmd == '"')
3513 {
3514 xp->xp_context = EXPAND_NOTHING;
3515 return NULL;
3516 }
3517
3518 if (*cmd == '|' || *cmd == '\n')
3519 return cmd + 1; /* There's another command */
3520
3521 /*
3522 * Isolate the command and search for it in the command table.
3523 * Exceptions:
3524 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003525 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3527 */
3528 if (*cmd == 'k' && cmd[1] != 'e')
3529 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003530 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 p = cmd + 1;
3532 }
3533 else
3534 {
3535 p = cmd;
3536 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3537 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003538 /* a user command may contain digits */
3539 if (ASCII_ISUPPER(cmd[0]))
3540 while (ASCII_ISALNUM(*p) || *p == '*')
3541 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003542 /* for python 3.x: ":py3*" commands completion */
3543 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003544 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003545 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003546 while (ASCII_ISALPHA(*p) || *p == '*')
3547 ++p;
3548 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003549 /* check for non-alpha command */
3550 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3551 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003552 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003554 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
3556 xp->xp_context = EXPAND_UNSUCCESSFUL;
3557 return NULL;
3558 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003559 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003560 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3561 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3562 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 break;
3564
3565#ifdef FEAT_USR_CMDS
3566 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3568 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569#endif
3570 }
3571
3572 /*
3573 * If the cursor is touching the command, and it ends in an alpha-numeric
3574 * character, complete the command name.
3575 */
3576 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3577 return NULL;
3578
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003579 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
3581 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3582 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003583 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 p = cmd + 1;
3585 }
3586#ifdef FEAT_USR_CMDS
3587 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3588 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003589 ea.cmd = cmd;
3590 p = find_ucmd(&ea, p, NULL, xp,
3591# if defined(FEAT_CMDL_COMPL)
3592 &compl
3593# else
3594 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003596 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003597 if (p == NULL)
3598 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 }
3600#endif
3601 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003602 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
3604 /* Not still touching the command and it was an illegal one */
3605 xp->xp_context = EXPAND_UNSUCCESSFUL;
3606 return NULL;
3607 }
3608
3609 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3610
3611 if (*p == '!') /* forced commands */
3612 {
3613 forceit = TRUE;
3614 ++p;
3615 }
3616
3617/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003618 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003620 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003621 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
3623 arg = skipwhite(p);
3624
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003625 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
3627 if (*arg == '>') /* append */
3628 {
3629 if (*++arg == '>')
3630 ++arg;
3631 arg = skipwhite(arg);
3632 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003633 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
3635 ++arg;
3636 usefilter = TRUE;
3637 }
3638 }
3639
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003640 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 {
3642 usefilter = forceit; /* :r! filter if forced */
3643 if (*arg == '!') /* :r !filter */
3644 {
3645 ++arg;
3646 usefilter = TRUE;
3647 }
3648 }
3649
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003650 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
3652 while (*arg == *cmd) /* allow any number of '>' or '<' */
3653 ++arg;
3654 arg = skipwhite(arg);
3655 }
3656
3657 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003658 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 /* Check if we're in the +command */
3661 p = arg + 1;
3662 arg = skip_cmd_arg(arg, FALSE);
3663
3664 /* Still touching the command after '+'? */
3665 if (*arg == NUL)
3666 return p;
3667
3668 /* Skip space(s) after +command to get to the real argument */
3669 arg = skipwhite(arg);
3670 }
3671
3672 /*
3673 * Check for '|' to separate commands and '"' to start comments.
3674 * Don't do this for ":read !cmd" and ":write !cmd".
3675 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003676 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 {
3678 p = arg;
3679 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003680 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 p += 2;
3682 while (*p)
3683 {
3684 if (*p == Ctrl_V)
3685 {
3686 if (p[1] != NUL)
3687 ++p;
3688 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003689 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 || *p == '|' || *p == '\n')
3691 {
3692 if (*(p - 1) != '\\')
3693 {
3694 if (*p == '|' || *p == '\n')
3695 return p + 1;
3696 return NULL; /* It's a comment */
3697 }
3698 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003699 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701 }
3702
3703 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003704 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 vim_strchr((char_u *)"|\"", *arg) == NULL)
3706 return NULL;
3707
3708 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003709 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003711 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003712 while (*p && p < buff + len)
3713 {
3714 if (*p == ' ' || *p == TAB)
3715 {
3716 /* argument starts after a space */
3717 xp->xp_pattern = ++p;
3718 }
3719 else
3720 {
3721 if (*p == '\\' && *(p + 1) != NUL)
3722 ++p; /* skip over escaped character */
3723 mb_ptr_adv(p);
3724 }
3725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003727 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003729 int c;
3730 int in_quote = FALSE;
3731 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
3733 /*
3734 * Allow spaces within back-quotes to count as part of the argument
3735 * being expanded.
3736 */
3737 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003738 p = xp->xp_pattern;
3739 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003741#ifdef FEAT_MBYTE
3742 if (has_mbyte)
3743 c = mb_ptr2char(p);
3744 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003746 c = *p;
3747 if (c == '\\' && p[1] != NUL)
3748 ++p;
3749 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
3751 if (!in_quote)
3752 {
3753 xp->xp_pattern = p;
3754 bow = p + 1;
3755 }
3756 in_quote = !in_quote;
3757 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003758 /* An argument can contain just about everything, except
3759 * characters that end the command and white space. */
3760 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003761#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003762 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003763#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003764 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003765 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003766 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003767 while (*p != NUL)
3768 {
3769#ifdef FEAT_MBYTE
3770 if (has_mbyte)
3771 c = mb_ptr2char(p);
3772 else
3773#endif
3774 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003775 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003776 break;
3777#ifdef FEAT_MBYTE
3778 if (has_mbyte)
3779 len = (*mb_ptr2len)(p);
3780 else
3781#endif
3782 len = 1;
3783 mb_ptr_adv(p);
3784 }
3785 if (in_quote)
3786 bow = p;
3787 else
3788 xp->xp_pattern = p;
3789 p -= len;
3790 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003791 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793
3794 /*
3795 * If we are still inside the quotes, and we passed a space, just
3796 * expand from there.
3797 */
3798 if (bow != NULL && in_quote)
3799 xp->xp_pattern = bow;
3800 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003801
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003802 /* For a shell command more chars need to be escaped. */
3803 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003804 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003805#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003806 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003807#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003808 /* When still after the command name expand executables. */
3809 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003810 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
3813 /* Check for environment variable */
3814 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003815#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 || *xp->xp_pattern == '%'
3817#endif
3818 )
3819 {
3820 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3821 if (!vim_isIDc(*p))
3822 break;
3823 if (*p == NUL)
3824 {
3825 xp->xp_context = EXPAND_ENV_VARS;
3826 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003827#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3828 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003829 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003830 compl = EXPAND_ENV_VARS;
3831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
3833 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003834#if defined(FEAT_CMDL_COMPL)
3835 /* Check for user names */
3836 if (*xp->xp_pattern == '~')
3837 {
3838 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3839 ;
3840 /* Complete ~user only if it partially matches a user name.
3841 * A full match ~user<Tab> will be replaced by user's home
3842 * directory i.e. something like ~user<Tab> -> /home/user/ */
3843 if (*p == NUL && p > xp->xp_pattern + 1
3844 && match_user(xp->xp_pattern + 1) == 1)
3845 {
3846 xp->xp_context = EXPAND_USER;
3847 ++xp->xp_pattern;
3848 }
3849 }
3850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852
3853/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003854 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003856 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003858 case CMD_find:
3859 case CMD_sfind:
3860 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003861 if (xp->xp_context == EXPAND_FILES)
3862 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003863 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 case CMD_cd:
3865 case CMD_chdir:
3866 case CMD_lcd:
3867 case CMD_lchdir:
3868 if (xp->xp_context == EXPAND_FILES)
3869 xp->xp_context = EXPAND_DIRECTORIES;
3870 break;
3871 case CMD_help:
3872 xp->xp_context = EXPAND_HELP;
3873 xp->xp_pattern = arg;
3874 break;
3875
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003876 /* Command modifiers: return the argument.
3877 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003879 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 case CMD_belowright:
3881 case CMD_botright:
3882 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003883 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003884 case CMD_cdo:
3885 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003887 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 case CMD_folddoclosed:
3889 case CMD_folddoopen:
3890 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003891 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 case CMD_keepjumps:
3893 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003894 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003895 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003897 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003899 case CMD_noautocmd:
3900 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003902 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003904 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003905 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 case CMD_topleft:
3907 case CMD_verbose:
3908 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003909 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 return arg;
3911
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003912 case CMD_filter:
3913 if (*arg != NUL)
3914 arg = skip_vimgrep_pat(arg, NULL, NULL);
3915 if (arg == NULL || *arg == NUL)
3916 {
3917 xp->xp_context = EXPAND_NOTHING;
3918 return NULL;
3919 }
3920 return skipwhite(arg);
3921
Bram Moolenaar4f688582007-07-24 12:34:30 +00003922#ifdef FEAT_CMDL_COMPL
3923# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 case CMD_match:
3925 if (*arg == NUL || !ends_excmd(*arg))
3926 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003927 /* also complete "None" */
3928 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 arg = skipwhite(skiptowhite(arg));
3930 if (*arg != NUL)
3931 {
3932 xp->xp_context = EXPAND_NOTHING;
3933 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3934 }
3935 }
3936 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939/*
3940 * All completion for the +cmdline_compl feature goes here.
3941 */
3942
3943# ifdef FEAT_USR_CMDS
3944 case CMD_command:
3945 /* Check for attributes */
3946 while (*arg == '-')
3947 {
3948 arg++; /* Skip "-" */
3949 p = skiptowhite(arg);
3950 if (*p == NUL)
3951 {
3952 /* Cursor is still in the attribute */
3953 p = vim_strchr(arg, '=');
3954 if (p == NULL)
3955 {
3956 /* No "=", so complete attribute names */
3957 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3958 xp->xp_pattern = arg;
3959 return NULL;
3960 }
3961
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003962 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 * their arguments as well.
3964 */
3965 if (STRNICMP(arg, "complete", p - arg) == 0)
3966 {
3967 xp->xp_context = EXPAND_USER_COMPLETE;
3968 xp->xp_pattern = p + 1;
3969 return NULL;
3970 }
3971 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3972 {
3973 xp->xp_context = EXPAND_USER_NARGS;
3974 xp->xp_pattern = p + 1;
3975 return NULL;
3976 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003977 else if (STRNICMP(arg, "addr", p - arg) == 0)
3978 {
3979 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3980 xp->xp_pattern = p + 1;
3981 return NULL;
3982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 return NULL;
3984 }
3985 arg = skipwhite(p);
3986 }
3987
3988 /* After the attributes comes the new command name */
3989 p = skiptowhite(arg);
3990 if (*p == NUL)
3991 {
3992 xp->xp_context = EXPAND_USER_COMMANDS;
3993 xp->xp_pattern = arg;
3994 break;
3995 }
3996
3997 /* And finally comes a normal command */
3998 return skipwhite(p);
3999
4000 case CMD_delcommand:
4001 xp->xp_context = EXPAND_USER_COMMANDS;
4002 xp->xp_pattern = arg;
4003 break;
4004# endif
4005
4006 case CMD_global:
4007 case CMD_vglobal:
4008 delim = *arg; /* get the delimiter */
4009 if (delim)
4010 ++arg; /* skip delimiter if there is one */
4011
4012 while (arg[0] != NUL && arg[0] != delim)
4013 {
4014 if (arg[0] == '\\' && arg[1] != NUL)
4015 ++arg;
4016 ++arg;
4017 }
4018 if (arg[0] != NUL)
4019 return arg + 1;
4020 break;
4021 case CMD_and:
4022 case CMD_substitute:
4023 delim = *arg;
4024 if (delim)
4025 {
4026 /* skip "from" part */
4027 ++arg;
4028 arg = skip_regexp(arg, delim, p_magic, NULL);
4029 }
4030 /* skip "to" part */
4031 while (arg[0] != NUL && arg[0] != delim)
4032 {
4033 if (arg[0] == '\\' && arg[1] != NUL)
4034 ++arg;
4035 ++arg;
4036 }
4037 if (arg[0] != NUL) /* skip delimiter */
4038 ++arg;
4039 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4040 ++arg;
4041 if (arg[0] != NUL)
4042 return arg;
4043 break;
4044 case CMD_isearch:
4045 case CMD_dsearch:
4046 case CMD_ilist:
4047 case CMD_dlist:
4048 case CMD_ijump:
4049 case CMD_psearch:
4050 case CMD_djump:
4051 case CMD_isplit:
4052 case CMD_dsplit:
4053 arg = skipwhite(skipdigits(arg)); /* skip count */
4054 if (*arg == '/') /* Match regexp, not just whole words */
4055 {
4056 for (++arg; *arg && *arg != '/'; arg++)
4057 if (*arg == '\\' && arg[1] != NUL)
4058 arg++;
4059 if (*arg)
4060 {
4061 arg = skipwhite(arg + 1);
4062
4063 /* Check for trailing illegal characters */
4064 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4065 xp->xp_context = EXPAND_NOTHING;
4066 else
4067 return arg;
4068 }
4069 }
4070 break;
4071#ifdef FEAT_AUTOCMD
4072 case CMD_autocmd:
4073 return set_context_in_autocmd(xp, arg, FALSE);
4074
4075 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004076 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 return set_context_in_autocmd(xp, arg, TRUE);
4078#endif
4079 case CMD_set:
4080 set_context_in_set_cmd(xp, arg, 0);
4081 break;
4082 case CMD_setglobal:
4083 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4084 break;
4085 case CMD_setlocal:
4086 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4087 break;
4088 case CMD_tag:
4089 case CMD_stag:
4090 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004091 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 case CMD_tselect:
4093 case CMD_stselect:
4094 case CMD_ptselect:
4095 case CMD_tjump:
4096 case CMD_stjump:
4097 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004098 if (*p_wop != NUL)
4099 xp->xp_context = EXPAND_TAGS_LISTFILES;
4100 else
4101 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 xp->xp_pattern = arg;
4103 break;
4104 case CMD_augroup:
4105 xp->xp_context = EXPAND_AUGROUP;
4106 xp->xp_pattern = arg;
4107 break;
4108#ifdef FEAT_SYN_HL
4109 case CMD_syntax:
4110 set_context_in_syntax_cmd(xp, arg);
4111 break;
4112#endif
4113#ifdef FEAT_EVAL
4114 case CMD_let:
4115 case CMD_if:
4116 case CMD_elseif:
4117 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004118 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 case CMD_echo:
4120 case CMD_echon:
4121 case CMD_execute:
4122 case CMD_echomsg:
4123 case CMD_echoerr:
4124 case CMD_call:
4125 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004126 case CMD_cexpr:
4127 case CMD_caddexpr:
4128 case CMD_cgetexpr:
4129 case CMD_lexpr:
4130 case CMD_laddexpr:
4131 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004132 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 break;
4134
4135 case CMD_unlet:
4136 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4137 arg = xp->xp_pattern + 1;
4138 xp->xp_context = EXPAND_USER_VARS;
4139 xp->xp_pattern = arg;
4140 break;
4141
4142 case CMD_function:
4143 case CMD_delfunction:
4144 xp->xp_context = EXPAND_USER_FUNC;
4145 xp->xp_pattern = arg;
4146 break;
4147
4148 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004149 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 break;
4151#endif
4152 case CMD_highlight:
4153 set_context_in_highlight_cmd(xp, arg);
4154 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004155#ifdef FEAT_CSCOPE
4156 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004157 case CMD_lcscope:
4158 case CMD_scscope:
4159 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004160 break;
4161#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004162#ifdef FEAT_SIGNS
4163 case CMD_sign:
4164 set_context_in_sign_cmd(xp, arg);
4165 break;
4166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167#ifdef FEAT_LISTCMDS
4168 case CMD_bdelete:
4169 case CMD_bwipeout:
4170 case CMD_bunload:
4171 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4172 arg = xp->xp_pattern + 1;
4173 /*FALLTHROUGH*/
4174 case CMD_buffer:
4175 case CMD_sbuffer:
4176 case CMD_checktime:
4177 xp->xp_context = EXPAND_BUFFERS;
4178 xp->xp_pattern = arg;
4179 break;
4180#endif
4181#ifdef FEAT_USR_CMDS
4182 case CMD_USER:
4183 case CMD_USER_BUF:
4184 if (compl != EXPAND_NOTHING)
4185 {
4186 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004187 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
4189# ifdef FEAT_MENU
4190 if (compl == EXPAND_MENUS)
4191 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4192# endif
4193 if (compl == EXPAND_COMMANDS)
4194 return arg;
4195 if (compl == EXPAND_MAPPINGS)
4196 return set_context_in_map_cmd(xp, (char_u *)"map",
4197 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004198 /* Find start of last argument. */
4199 p = arg;
4200 while (*p)
4201 {
4202 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004203 /* argument starts after a space */
4204 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004205 else if (*p == '\\' && *(p + 1) != NUL)
4206 ++p; /* skip over escaped character */
4207 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 xp->xp_pattern = arg;
4210 }
4211 xp->xp_context = compl;
4212 }
4213 break;
4214#endif
4215 case CMD_map: case CMD_noremap:
4216 case CMD_nmap: case CMD_nnoremap:
4217 case CMD_vmap: case CMD_vnoremap:
4218 case CMD_omap: case CMD_onoremap:
4219 case CMD_imap: case CMD_inoremap:
4220 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004221 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004222 case CMD_smap: case CMD_snoremap:
4223 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004225 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 case CMD_unmap:
4227 case CMD_nunmap:
4228 case CMD_vunmap:
4229 case CMD_ounmap:
4230 case CMD_iunmap:
4231 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004232 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004233 case CMD_sunmap:
4234 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004236 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 case CMD_abbreviate: case CMD_noreabbrev:
4238 case CMD_cabbrev: case CMD_cnoreabbrev:
4239 case CMD_iabbrev: case CMD_inoreabbrev:
4240 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004241 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 case CMD_unabbreviate:
4243 case CMD_cunabbrev:
4244 case CMD_iunabbrev:
4245 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004246 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247#ifdef FEAT_MENU
4248 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4249 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4250 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4251 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4252 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4253 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4254 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4255 case CMD_tmenu: case CMD_tunmenu:
4256 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4257 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4258#endif
4259
4260 case CMD_colorscheme:
4261 xp->xp_context = EXPAND_COLORS;
4262 xp->xp_pattern = arg;
4263 break;
4264
4265 case CMD_compiler:
4266 xp->xp_context = EXPAND_COMPILER;
4267 xp->xp_pattern = arg;
4268 break;
4269
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004270 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004271 xp->xp_context = EXPAND_OWNSYNTAX;
4272 xp->xp_pattern = arg;
4273 break;
4274
4275 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004276 xp->xp_context = EXPAND_FILETYPE;
4277 xp->xp_pattern = arg;
4278 break;
4279
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004280 case CMD_packadd:
4281 xp->xp_context = EXPAND_PACKADD;
4282 xp->xp_pattern = arg;
4283 break;
4284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4286 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4287 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004288 p = skiptowhite(arg);
4289 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 {
4291 xp->xp_context = EXPAND_LANGUAGE;
4292 xp->xp_pattern = arg;
4293 }
4294 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004295 {
4296 if ( STRNCMP(arg, "messages", p - arg) == 0
4297 || STRNCMP(arg, "ctype", p - arg) == 0
4298 || STRNCMP(arg, "time", p - arg) == 0)
4299 {
4300 xp->xp_context = EXPAND_LOCALES;
4301 xp->xp_pattern = skipwhite(p);
4302 }
4303 else
4304 xp->xp_context = EXPAND_NOTHING;
4305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 break;
4307#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004308#if defined(FEAT_PROFILE)
4309 case CMD_profile:
4310 set_context_in_profile_cmd(xp, arg);
4311 break;
4312#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004313 case CMD_behave:
4314 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004315 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004316 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004318 case CMD_messages:
4319 xp->xp_context = EXPAND_MESSAGES;
4320 xp->xp_pattern = arg;
4321 break;
4322
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004323#if defined(FEAT_CMDHIST)
4324 case CMD_history:
4325 xp->xp_context = EXPAND_HISTORY;
4326 xp->xp_pattern = arg;
4327 break;
4328#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004329#if defined(FEAT_PROFILE)
4330 case CMD_syntime:
4331 xp->xp_context = EXPAND_SYNTIME;
4332 xp->xp_pattern = arg;
4333 break;
4334#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004335
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336#endif /* FEAT_CMDL_COMPL */
4337
4338 default:
4339 break;
4340 }
4341 return NULL;
4342}
4343
4344/*
4345 * skip a range specifier of the form: addr [,addr] [;addr] ..
4346 *
4347 * Backslashed delimiters after / or ? will be skipped, and commands will
4348 * not be expanded between /'s and ?'s or after "'".
4349 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004350 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 * Returns the "cmd" pointer advanced to beyond the range.
4352 */
4353 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004354skip_range(
4355 char_u *cmd,
4356 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004358 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
Bram Moolenaardf177f62005-02-22 08:39:57 +00004360 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 {
4362 if (*cmd == '\'')
4363 {
4364 if (*++cmd == NUL && ctx != NULL)
4365 *ctx = EXPAND_NOTHING;
4366 }
4367 else if (*cmd == '/' || *cmd == '?')
4368 {
4369 delim = *cmd++;
4370 while (*cmd != NUL && *cmd != delim)
4371 if (*cmd++ == '\\' && *cmd != NUL)
4372 ++cmd;
4373 if (*cmd == NUL && ctx != NULL)
4374 *ctx = EXPAND_NOTHING;
4375 }
4376 if (*cmd != NUL)
4377 ++cmd;
4378 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004379
4380 /* Skip ":" and white space. */
4381 while (*cmd == ':')
4382 cmd = skipwhite(cmd + 1);
4383
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 return cmd;
4385}
4386
4387/*
4388 * get a single EX address
4389 *
4390 * Set ptr to the next character after the part that was interpreted.
4391 * Set ptr to NULL when an error is encountered.
4392 *
4393 * Return MAXLNUM when no Ex address was found.
4394 */
4395 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004396get_address(
4397 exarg_T *eap UNUSED,
4398 char_u **ptr,
4399 int addr_type, /* flag: one of ADDR_LINES, ... */
4400 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004401 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004402 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403{
4404 int c;
4405 int i;
4406 long n;
4407 char_u *cmd;
4408 pos_T pos;
4409 pos_T *fp;
4410 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004411 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412
4413 cmd = skipwhite(*ptr);
4414 lnum = MAXLNUM;
4415 do
4416 {
4417 switch (*cmd)
4418 {
4419 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 ++cmd;
4421 switch (addr_type)
4422 {
4423 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 lnum = curwin->w_cursor.lnum;
4425 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004426 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004427 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004428 break;
4429 case ADDR_ARGUMENTS:
4430 lnum = curwin->w_arg_idx + 1;
4431 break;
4432 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004433 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004434 lnum = curbuf->b_fnum;
4435 break;
4436 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004437 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004438 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004439 case ADDR_TABS_RELATIVE:
4440 EMSG(_(e_invrange));
4441 cmd = NULL;
4442 goto error;
4443 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004444#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004445 case ADDR_QUICKFIX:
4446 lnum = qf_get_cur_valid_idx(eap);
4447 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004448#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004449 }
4450 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451
4452 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004453 ++cmd;
4454 switch (addr_type)
4455 {
4456 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 lnum = curbuf->b_ml.ml_line_count;
4458 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004459 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004460 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004461 break;
4462 case ADDR_ARGUMENTS:
4463 lnum = ARGCOUNT;
4464 break;
4465 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004466 buf = lastbuf;
4467 while (buf->b_ml.ml_mfp == NULL)
4468 {
4469 if (buf->b_prev == NULL)
4470 break;
4471 buf = buf->b_prev;
4472 }
4473 lnum = buf->b_fnum;
4474 break;
4475 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004476 lnum = lastbuf->b_fnum;
4477 break;
4478 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004479 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004480 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004481 case ADDR_TABS_RELATIVE:
4482 EMSG(_(e_invrange));
4483 cmd = NULL;
4484 goto error;
4485 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004486#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004487 case ADDR_QUICKFIX:
4488 lnum = qf_get_size(eap);
4489 if (lnum == 0)
4490 lnum = 1;
4491 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004492#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004493 }
4494 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495
4496 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004497 if (*++cmd == NUL)
4498 {
4499 cmd = NULL;
4500 goto error;
4501 }
4502 if (addr_type != ADDR_LINES)
4503 {
4504 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004505 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004506 goto error;
4507 }
4508 if (skip)
4509 ++cmd;
4510 else
4511 {
4512 /* Only accept a mark in another file when it is
4513 * used by itself: ":'M". */
4514 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4515 ++cmd;
4516 if (fp == (pos_T *)-1)
4517 /* Jumped to another file. */
4518 lnum = curwin->w_cursor.lnum;
4519 else
4520 {
4521 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 {
4523 cmd = NULL;
4524 goto error;
4525 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004526 lnum = fp->lnum;
4527 }
4528 }
4529 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
4531 case '/':
4532 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004533 c = *cmd++;
4534 if (addr_type != ADDR_LINES)
4535 {
4536 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004537 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004538 goto error;
4539 }
4540 if (skip) /* skip "/pat/" */
4541 {
4542 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4543 if (*cmd == c)
4544 ++cmd;
4545 }
4546 else
4547 {
4548 pos = curwin->w_cursor; /* save curwin->w_cursor */
4549 /*
4550 * When '/' or '?' follows another address, start
4551 * from there.
4552 */
4553 if (lnum != MAXLNUM)
4554 curwin->w_cursor.lnum = lnum;
4555 /*
4556 * Start a forward search at the end of the line.
4557 * Start a backward search at the start of the line.
4558 * This makes sure we never match in the current
4559 * line, and can match anywhere in the
4560 * next/previous line.
4561 */
4562 if (c == '/')
4563 curwin->w_cursor.col = MAXCOL;
4564 else
4565 curwin->w_cursor.col = 0;
4566 searchcmdlen = 0;
4567 if (!do_search(NULL, c, cmd, 1L,
4568 SEARCH_HIS | SEARCH_MSG, NULL))
4569 {
4570 curwin->w_cursor = pos;
4571 cmd = NULL;
4572 goto error;
4573 }
4574 lnum = curwin->w_cursor.lnum;
4575 curwin->w_cursor = pos;
4576 /* adjust command string pointer */
4577 cmd += searchcmdlen;
4578 }
4579 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
4581 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004582 ++cmd;
4583 if (addr_type != ADDR_LINES)
4584 {
4585 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004586 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004587 goto error;
4588 }
4589 if (*cmd == '&')
4590 i = RE_SUBST;
4591 else if (*cmd == '?' || *cmd == '/')
4592 i = RE_SEARCH;
4593 else
4594 {
4595 EMSG(_(e_backslash));
4596 cmd = NULL;
4597 goto error;
4598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004600 if (!skip)
4601 {
4602 /*
4603 * When search follows another address, start from
4604 * there.
4605 */
4606 if (lnum != MAXLNUM)
4607 pos.lnum = lnum;
4608 else
4609 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004611 /*
4612 * Start the search just like for the above
4613 * do_search().
4614 */
4615 if (*cmd != '?')
4616 pos.col = MAXCOL;
4617 else
4618 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004619#ifdef FEAT_VIRTUALEDIT
4620 pos.coladd = 0;
4621#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004622 if (searchit(curwin, curbuf, &pos,
4623 *cmd == '?' ? BACKWARD : FORWARD,
4624 (char_u *)"", 1L, SEARCH_MSG,
4625 i, (linenr_T)0, NULL) != FAIL)
4626 lnum = pos.lnum;
4627 else
4628 {
4629 cmd = NULL;
4630 goto error;
4631 }
4632 }
4633 ++cmd;
4634 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635
4636 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004637 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4638 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
4640
4641 for (;;)
4642 {
4643 cmd = skipwhite(cmd);
4644 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4645 break;
4646
4647 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004648 {
4649 switch (addr_type)
4650 {
4651 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004652 /* "+1" is same as ".+1" */
4653 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004654 break;
4655 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004656 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004657 break;
4658 case ADDR_ARGUMENTS:
4659 lnum = curwin->w_arg_idx + 1;
4660 break;
4661 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004662 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004663 lnum = curbuf->b_fnum;
4664 break;
4665 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004666 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004667 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004668 case ADDR_TABS_RELATIVE:
4669 lnum = 1;
4670 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004671#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004672 case ADDR_QUICKFIX:
4673 lnum = qf_get_cur_valid_idx(eap);
4674 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004675#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004676 }
4677 }
4678
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 if (VIM_ISDIGIT(*cmd))
4680 i = '+'; /* "number" is same as "+number" */
4681 else
4682 i = *cmd++;
4683 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4684 n = 1;
4685 else
4686 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004687
4688 if (addr_type == ADDR_TABS_RELATIVE)
4689 {
4690 EMSG(_(e_invrange));
4691 cmd = NULL;
4692 goto error;
4693 }
4694 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004695 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004696 lnum = compute_buffer_local_count(
4697 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004699 {
4700#ifdef FEAT_FOLDING
4701 /* Relative line addressing, need to adjust for folded lines
4702 * now, but only do it after the first address. */
4703 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4704 && address_count >= 2)
4705 (void)hasFolding(lnum, NULL, &lnum);
4706#endif
4707 if (i == '-')
4708 lnum -= n;
4709 else
4710 lnum += n;
4711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 }
4713 } while (*cmd == '/' || *cmd == '?');
4714
4715error:
4716 *ptr = cmd;
4717 return lnum;
4718}
4719
4720/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004721 * Get flags from an Ex command argument.
4722 */
4723 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004724get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004725{
4726 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4727 {
4728 if (*eap->arg == 'l')
4729 eap->flags |= EXFLAG_LIST;
4730 else if (*eap->arg == 'p')
4731 eap->flags |= EXFLAG_PRINT;
4732 else
4733 eap->flags |= EXFLAG_NR;
4734 eap->arg = skipwhite(eap->arg + 1);
4735 }
4736}
4737
4738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 * Function called for command which is Not Implemented. NI!
4740 */
4741 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004742ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
4744 if (!eap->skip)
4745 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4746}
4747
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004748#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749/*
4750 * Function called for script command which is Not Implemented. NI!
4751 * Skips over ":perl <<EOF" constructs.
4752 */
4753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004754ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
4756 if (!eap->skip)
4757 ex_ni(eap);
4758 else
4759 vim_free(script_get(eap, eap->arg));
4760}
4761#endif
4762
4763/*
4764 * Check range in Ex command for validity.
4765 * Return NULL when valid, error message when invalid.
4766 */
4767 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004768invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004770 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 if ( eap->line1 < 0
4772 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004773 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004775
4776 if (eap->argt & RANGE)
4777 {
4778 switch(eap->addr_type)
4779 {
4780 case ADDR_LINES:
4781 if (!(eap->argt & NOTADR)
4782 && eap->line2 > curbuf->b_ml.ml_line_count
4783#ifdef FEAT_DIFF
4784 + (eap->cmdidx == CMD_diffget)
4785#endif
4786 )
4787 return (char_u *)_(e_invrange);
4788 break;
4789 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004790 /* add 1 if ARGCOUNT is 0 */
4791 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004792 return (char_u *)_(e_invrange);
4793 break;
4794 case ADDR_BUFFERS:
4795 if (eap->line1 < firstbuf->b_fnum
4796 || eap->line2 > lastbuf->b_fnum)
4797 return (char_u *)_(e_invrange);
4798 break;
4799 case ADDR_LOADED_BUFFERS:
4800 buf = firstbuf;
4801 while (buf->b_ml.ml_mfp == NULL)
4802 {
4803 if (buf->b_next == NULL)
4804 return (char_u *)_(e_invrange);
4805 buf = buf->b_next;
4806 }
4807 if (eap->line1 < buf->b_fnum)
4808 return (char_u *)_(e_invrange);
4809 buf = lastbuf;
4810 while (buf->b_ml.ml_mfp == NULL)
4811 {
4812 if (buf->b_prev == NULL)
4813 return (char_u *)_(e_invrange);
4814 buf = buf->b_prev;
4815 }
4816 if (eap->line2 > buf->b_fnum)
4817 return (char_u *)_(e_invrange);
4818 break;
4819 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004820 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004821 return (char_u *)_(e_invrange);
4822 break;
4823 case ADDR_TABS:
4824 if (eap->line2 > LAST_TAB_NR)
4825 return (char_u *)_(e_invrange);
4826 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004827 case ADDR_TABS_RELATIVE:
4828 /* Do nothing */
4829 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004830#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004831 case ADDR_QUICKFIX:
4832 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4833 return (char_u *)_(e_invrange);
4834 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004835#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004836 }
4837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 return NULL;
4839}
4840
4841/*
4842 * Correct the range for zero line number, if required.
4843 */
4844 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004845correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846{
4847 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4848 {
4849 if (eap->line1 == 0)
4850 eap->line1 = 1;
4851 if (eap->line2 == 0)
4852 eap->line2 = 1;
4853 }
4854}
4855
Bram Moolenaar748bf032005-02-02 23:04:36 +00004856#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004857static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004858
4859/*
4860 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4861 * pattern. Otherwise return eap->arg.
4862 */
4863 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004864skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004865{
4866 char_u *p = eap->arg;
4867
Bram Moolenaara37420f2006-02-04 22:37:47 +00004868 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4869 || eap->cmdidx == CMD_vimgrepadd
4870 || eap->cmdidx == CMD_lvimgrepadd
4871 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004872 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004873 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004874 if (p == NULL)
4875 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004876 }
4877 return p;
4878}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004879
4880/*
4881 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4882 * in the command line, so that things like % get expanded.
4883 */
4884 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004885replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004886{
4887 char_u *new_cmdline;
4888 char_u *program;
4889 char_u *pos;
4890 char_u *ptr;
4891 int len;
4892 int i;
4893
4894 /*
4895 * Don't do it when ":vimgrep" is used for ":grep".
4896 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004897 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4898 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4899 || eap->cmdidx == CMD_grepadd
4900 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004901 && !grep_internal(eap->cmdidx))
4902 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004903 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4904 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004905 {
4906 if (*curbuf->b_p_gp == NUL)
4907 program = p_gp;
4908 else
4909 program = curbuf->b_p_gp;
4910 }
4911 else
4912 {
4913 if (*curbuf->b_p_mp == NUL)
4914 program = p_mp;
4915 else
4916 program = curbuf->b_p_mp;
4917 }
4918
4919 p = skipwhite(p);
4920
4921 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4922 {
4923 /* replace $* by given arguments */
4924 i = 1;
4925 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4926 ++i;
4927 len = (int)STRLEN(p);
4928 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4929 if (new_cmdline == NULL)
4930 return NULL; /* out of memory */
4931 ptr = new_cmdline;
4932 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4933 {
4934 i = (int)(pos - program);
4935 STRNCPY(ptr, program, i);
4936 STRCPY(ptr += i, p);
4937 ptr += len;
4938 program = pos + 2;
4939 }
4940 STRCPY(ptr, program);
4941 }
4942 else
4943 {
4944 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4945 if (new_cmdline == NULL)
4946 return NULL; /* out of memory */
4947 STRCPY(new_cmdline, program);
4948 STRCAT(new_cmdline, " ");
4949 STRCAT(new_cmdline, p);
4950 }
4951 msg_make(p);
4952
4953 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4954 vim_free(*cmdlinep);
4955 *cmdlinep = new_cmdline;
4956 p = new_cmdline;
4957 }
4958 return p;
4959}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004960#endif
4961
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962/*
4963 * Expand file name in Ex command argument.
4964 * Return FAIL for failure, OK otherwise.
4965 */
4966 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004967expand_filename(
4968 exarg_T *eap,
4969 char_u **cmdlinep,
4970 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971{
4972 int has_wildcards; /* need to expand wildcards */
4973 char_u *repl;
4974 int srclen;
4975 char_u *p;
4976 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004977 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978
Bram Moolenaar748bf032005-02-02 23:04:36 +00004979#ifdef FEAT_QUICKFIX
4980 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4981 p = skip_grep_pat(eap);
4982#else
4983 p = eap->arg;
4984#endif
4985
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 /*
4987 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4988 * the file name contains a wildcard it should not cause expanding.
4989 * (it will be expanded anyway if there is a wildcard before replacing).
4990 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004991 has_wildcards = mch_has_wildcard(p);
4992 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004994#ifdef FEAT_EVAL
4995 /* Skip over `=expr`, wildcards in it are not expanded. */
4996 if (p[0] == '`' && p[1] == '=')
4997 {
4998 p += 2;
4999 (void)skip_expr(&p);
5000 if (*p == '`')
5001 ++p;
5002 continue;
5003 }
5004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 /*
5006 * Quick check if this cannot be the start of a special string.
5007 * Also removes backslash before '%', '#' and '<'.
5008 */
5009 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5010 {
5011 ++p;
5012 continue;
5013 }
5014
5015 /*
5016 * Try to find a match at this position.
5017 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005018 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5019 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 if (*errormsgp != NULL) /* error detected */
5021 return FAIL;
5022 if (repl == NULL) /* no match found */
5023 {
5024 p += srclen;
5025 continue;
5026 }
5027
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005028 /* Wildcards won't be expanded below, the replacement is taken
5029 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5030 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5031 {
5032 char_u *l = repl;
5033
5034 repl = expand_env_save(repl);
5035 vim_free(l);
5036 }
5037
Bram Moolenaar63b92542007-03-27 14:57:09 +00005038 /* Need to escape white space et al. with a backslash.
5039 * Don't do this for:
5040 * - replacement that already has been escaped: "##"
5041 * - shell commands (may have to use quotes instead).
5042 * - non-unix systems when there is a single argument (spaces don't
5043 * separate arguments then).
5044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005046 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 && eap->cmdidx != CMD_bang
5048 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00005049 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00005051 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00005053 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054#ifndef UNIX
5055 && !(eap->argt & NOSPC)
5056#endif
5057 )
5058 {
5059 char_u *l;
5060#ifdef BACKSLASH_IN_FILENAME
5061 /* Don't escape a backslash here, because rem_backslash() doesn't
5062 * remove it later. */
5063 static char_u *nobslash = (char_u *)" \t\"|";
5064# define ESCAPE_CHARS nobslash
5065#else
5066# define ESCAPE_CHARS escape_chars
5067#endif
5068
5069 for (l = repl; *l; ++l)
5070 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5071 {
5072 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5073 if (l != NULL)
5074 {
5075 vim_free(repl);
5076 repl = l;
5077 }
5078 break;
5079 }
5080 }
5081
5082 /* For a shell command a '!' must be escaped. */
5083 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005084 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
5086 char_u *l;
5087
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005088 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 if (l != NULL)
5090 {
5091 vim_free(repl);
5092 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
5094 }
5095
5096 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5097 vim_free(repl);
5098 if (p == NULL)
5099 return FAIL;
5100 }
5101
5102 /*
5103 * One file argument: Expand wildcards.
5104 * Don't do this with ":r !command" or ":w !command".
5105 */
5106 if ((eap->argt & NOSPC) && !eap->usefilter)
5107 {
5108 /*
5109 * May do this twice:
5110 * 1. Replace environment variables.
5111 * 2. Replace any other wildcards, remove backslashes.
5112 */
5113 for (n = 1; n <= 2; ++n)
5114 {
5115 if (n == 2)
5116 {
5117#ifdef UNIX
5118 /*
5119 * Only for Unix we check for more than one file name.
5120 * For other systems spaces are considered to be part
5121 * of the file name.
5122 * Only check here if there is no wildcard, otherwise
5123 * ExpandOne() will check for errors. This allows
5124 * ":e `ls ve*.c`" on Unix.
5125 */
5126 if (!has_wildcards)
5127 for (p = eap->arg; *p; ++p)
5128 {
5129 /* skip escaped characters */
5130 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5131 ++p;
5132 else if (vim_iswhite(*p))
5133 {
5134 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5135 return FAIL;
5136 }
5137 }
5138#endif
5139
5140 /*
5141 * Halve the number of backslashes (this is Vi compatible).
5142 * For Unix and OS/2, when wildcards are expanded, this is
5143 * done by ExpandOne() below.
5144 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005145#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 if (!has_wildcards)
5147#endif
5148 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150
5151 if (has_wildcards)
5152 {
5153 if (n == 1)
5154 {
5155 /*
5156 * First loop: May expand environment variables. This
5157 * can be done much faster with expand_env() than with
5158 * something else (e.g., calling a shell).
5159 * After expanding environment variables, check again
5160 * if there are still wildcards present.
5161 */
5162 if (vim_strchr(eap->arg, '$') != NULL
5163 || vim_strchr(eap->arg, '~') != NULL)
5164 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005165 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005166 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 has_wildcards = mch_has_wildcard(NameBuff);
5168 p = NameBuff;
5169 }
5170 else
5171 p = NULL;
5172 }
5173 else /* n == 2 */
5174 {
5175 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005176 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177
5178 ExpandInit(&xpc);
5179 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005180 if (p_wic)
5181 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005183 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if (p == NULL)
5185 return FAIL;
5186 }
5187 if (p != NULL)
5188 {
5189 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5190 p, cmdlinep);
5191 if (n == 2) /* p came from ExpandOne() */
5192 vim_free(p);
5193 }
5194 }
5195 }
5196 }
5197 return OK;
5198}
5199
5200/*
5201 * Replace part of the command line, keeping eap->cmd, eap->arg and
5202 * eap->nextcmd correct.
5203 * "src" points to the part that is to be replaced, of length "srclen".
5204 * "repl" is the replacement string.
5205 * Returns a pointer to the character after the replaced string.
5206 * Returns NULL for failure.
5207 */
5208 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005209repl_cmdline(
5210 exarg_T *eap,
5211 char_u *src,
5212 int srclen,
5213 char_u *repl,
5214 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215{
5216 int len;
5217 int i;
5218 char_u *new_cmdline;
5219
5220 /*
5221 * The new command line is build in new_cmdline[].
5222 * First allocate it.
5223 * Careful: a "+cmd" argument may have been NUL terminated.
5224 */
5225 len = (int)STRLEN(repl);
5226 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005227 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5229 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5230 return NULL; /* out of memory! */
5231
5232 /*
5233 * Copy the stuff before the expanded part.
5234 * Copy the expanded stuff.
5235 * Copy what came after the expanded part.
5236 * Copy the next commands, if there are any.
5237 */
5238 i = (int)(src - *cmdlinep); /* length of part before match */
5239 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 mch_memmove(new_cmdline + i, repl, (size_t)len);
5242 i += len; /* remember the end of the string */
5243 STRCPY(new_cmdline + i, src + srclen);
5244 src = new_cmdline + i; /* remember where to continue */
5245
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005246 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 {
5248 i = (int)STRLEN(new_cmdline) + 1;
5249 STRCPY(new_cmdline + i, eap->nextcmd);
5250 eap->nextcmd = new_cmdline + i;
5251 }
5252 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5253 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5254 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5255 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5256 vim_free(*cmdlinep);
5257 *cmdlinep = new_cmdline;
5258
5259 return src;
5260}
5261
5262/*
5263 * Check for '|' to separate commands and '"' to start comments.
5264 */
5265 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005266separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267{
5268 char_u *p;
5269
Bram Moolenaar86b68352004-12-27 21:59:20 +00005270#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005271 p = skip_grep_pat(eap);
5272#else
5273 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005274#endif
5275
5276 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
5278 if (*p == Ctrl_V)
5279 {
5280 if (eap->argt & (USECTRLV | XFILE))
5281 ++p; /* skip CTRL-V and next char */
5282 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005283 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005284 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 if (*p == NUL) /* stop at NUL after CTRL-V */
5286 break;
5287 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005288
5289#ifdef FEAT_EVAL
5290 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005291 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005292 {
5293 p += 2;
5294 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005295 }
5296#endif
5297
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 /* Check for '"': start of comment or '|': next command */
5299 /* :@" and :*" do not start a comment!
5300 * :redir @" doesn't either. */
5301 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5302 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5303 || p != eap->arg)
5304 && (eap->cmdidx != CMD_redir
5305 || p != eap->arg + 1 || p[-1] != '@'))
5306 || *p == '|' || *p == '\n')
5307 {
5308 /*
5309 * We remove the '\' before the '|', unless USECTRLV is used
5310 * AND 'b' is present in 'cpoptions'.
5311 */
5312 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5313 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5314 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005315 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 --p;
5317 }
5318 else
5319 {
5320 eap->nextcmd = check_nextcmd(p);
5321 *p = NUL;
5322 break;
5323 }
5324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005326
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5328 del_trailing_spaces(eap->arg);
5329}
5330
5331/*
5332 * get + command from ex argument
5333 */
5334 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005335getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
5337 char_u *arg = *argp;
5338 char_u *command = NULL;
5339
5340 if (*arg == '+') /* +[command] */
5341 {
5342 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005343 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 command = dollar_command;
5345 else
5346 {
5347 command = arg;
5348 arg = skip_cmd_arg(command, TRUE);
5349 if (*arg != NUL)
5350 *arg++ = NUL; /* terminate command with NUL */
5351 }
5352
5353 arg = skipwhite(arg); /* skip over spaces */
5354 *argp = arg;
5355 }
5356 return command;
5357}
5358
5359/*
5360 * Find end of "+command" argument. Skip over "\ " and "\\".
5361 */
5362 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005363skip_cmd_arg(
5364 char_u *p,
5365 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366{
5367 while (*p && !vim_isspace(*p))
5368 {
5369 if (*p == '\\' && p[1] != NUL)
5370 {
5371 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005372 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 else
5374 ++p;
5375 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005376 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 }
5378 return p;
5379}
5380
5381/*
5382 * Get "++opt=arg" argument.
5383 * Return FAIL or OK.
5384 */
5385 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005386getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387{
5388 char_u *arg = eap->arg + 2;
5389 int *pp = NULL;
5390#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005391 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 char_u *p;
5393#endif
5394
5395 /* ":edit ++[no]bin[ary] file" */
5396 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5397 {
5398 if (*arg == 'n')
5399 {
5400 arg += 2;
5401 eap->force_bin = FORCE_NOBIN;
5402 }
5403 else
5404 eap->force_bin = FORCE_BIN;
5405 if (!checkforcmd(&arg, "binary", 3))
5406 return FAIL;
5407 eap->arg = skipwhite(arg);
5408 return OK;
5409 }
5410
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005411 /* ":read ++edit file" */
5412 if (STRNCMP(arg, "edit", 4) == 0)
5413 {
5414 eap->read_edit = TRUE;
5415 eap->arg = skipwhite(arg + 4);
5416 return OK;
5417 }
5418
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 if (STRNCMP(arg, "ff", 2) == 0)
5420 {
5421 arg += 2;
5422 pp = &eap->force_ff;
5423 }
5424 else if (STRNCMP(arg, "fileformat", 10) == 0)
5425 {
5426 arg += 10;
5427 pp = &eap->force_ff;
5428 }
5429#ifdef FEAT_MBYTE
5430 else if (STRNCMP(arg, "enc", 3) == 0)
5431 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005432 if (STRNCMP(arg, "encoding", 8) == 0)
5433 arg += 8;
5434 else
5435 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 pp = &eap->force_enc;
5437 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005438 else if (STRNCMP(arg, "bad", 3) == 0)
5439 {
5440 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005441 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443#endif
5444
5445 if (pp == NULL || *arg != '=')
5446 return FAIL;
5447
5448 ++arg;
5449 *pp = (int)(arg - eap->cmd);
5450 arg = skip_cmd_arg(arg, FALSE);
5451 eap->arg = skipwhite(arg);
5452 *arg = NUL;
5453
5454#ifdef FEAT_MBYTE
5455 if (pp == &eap->force_ff)
5456 {
5457#endif
5458 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5459 return FAIL;
5460#ifdef FEAT_MBYTE
5461 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005462 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 {
5464 /* Make 'fileencoding' lower case. */
5465 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5466 *p = TOLOWER_ASC(*p);
5467 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005468 else
5469 {
5470 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5471 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005472 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005473 if (STRICMP(p, "keep") == 0)
5474 eap->bad_char = BAD_KEEP;
5475 else if (STRICMP(p, "drop") == 0)
5476 eap->bad_char = BAD_DROP;
5477 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5478 eap->bad_char = *p;
5479 else
5480 return FAIL;
5481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482#endif
5483
5484 return OK;
5485}
5486
5487/*
5488 * ":abbreviate" and friends.
5489 */
5490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005491ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
5493 do_exmap(eap, TRUE); /* almost the same as mapping */
5494}
5495
5496/*
5497 * ":map" and friends.
5498 */
5499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005500ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501{
5502 /*
5503 * If we are sourcing .exrc or .vimrc in current directory we
5504 * print the mappings for security reasons.
5505 */
5506 if (secure)
5507 {
5508 secure = 2;
5509 msg_outtrans(eap->cmd);
5510 msg_putchar('\n');
5511 }
5512 do_exmap(eap, FALSE);
5513}
5514
5515/*
5516 * ":unmap" and friends.
5517 */
5518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005519ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
5521 do_exmap(eap, FALSE);
5522}
5523
5524/*
5525 * ":mapclear" and friends.
5526 */
5527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005528ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
5530 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5531}
5532
5533/*
5534 * ":abclear" and friends.
5535 */
5536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005537ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538{
5539 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5540}
5541
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005542#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005544ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
5546 /*
5547 * Disallow auto commands from .exrc and .vimrc in current
5548 * directory for security reasons.
5549 */
5550 if (secure)
5551 {
5552 secure = 2;
5553 eap->errmsg = e_curdir;
5554 }
5555 else if (eap->cmdidx == CMD_autocmd)
5556 do_autocmd(eap->arg, eap->forceit);
5557 else
5558 do_augroup(eap->arg, eap->forceit);
5559}
5560
5561/*
5562 * ":doautocmd": Apply the automatic commands to the current buffer.
5563 */
5564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005565ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005567 char_u *arg = eap->arg;
5568 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005569 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005570
Bram Moolenaar1610d052016-06-09 22:53:01 +02005571 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5572 /* Only when there is no <nomodeline>. */
5573 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005574 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575}
5576#endif
5577
5578#ifdef FEAT_LISTCMDS
5579/*
5580 * :[N]bunload[!] [N] [bufname] unload buffer
5581 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5582 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5583 */
5584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005585ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586{
5587 eap->errmsg = do_bufdel(
5588 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5589 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5590 : DOBUF_UNLOAD, eap->arg,
5591 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5592}
5593
5594/*
5595 * :[N]buffer [N] to buffer N
5596 * :[N]sbuffer [N] to buffer N
5597 */
5598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005599ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600{
5601 if (*eap->arg)
5602 eap->errmsg = e_trailing;
5603 else
5604 {
5605 if (eap->addr_count == 0) /* default is current buffer */
5606 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5607 else
5608 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005609 if (eap->do_ecmd_cmd != NULL)
5610 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 }
5612}
5613
5614/*
5615 * :[N]bmodified [N] to next mod. buffer
5616 * :[N]sbmodified [N] to next mod. buffer
5617 */
5618 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005619ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620{
5621 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005622 if (eap->do_ecmd_cmd != NULL)
5623 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624}
5625
5626/*
5627 * :[N]bnext [N] to next buffer
5628 * :[N]sbnext [N] split and to next buffer
5629 */
5630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005631ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005634 if (eap->do_ecmd_cmd != NULL)
5635 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636}
5637
5638/*
5639 * :[N]bNext [N] to previous buffer
5640 * :[N]bprevious [N] to previous buffer
5641 * :[N]sbNext [N] split and to previous buffer
5642 * :[N]sbprevious [N] split and to previous buffer
5643 */
5644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005645ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
5647 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005648 if (eap->do_ecmd_cmd != NULL)
5649 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650}
5651
5652/*
5653 * :brewind to first buffer
5654 * :bfirst to first buffer
5655 * :sbrewind split and to first buffer
5656 * :sbfirst split and to first buffer
5657 */
5658 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005659ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660{
5661 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005662 if (eap->do_ecmd_cmd != NULL)
5663 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664}
5665
5666/*
5667 * :blast to last buffer
5668 * :sblast split and to last buffer
5669 */
5670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005671ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
5673 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005674 if (eap->do_ecmd_cmd != NULL)
5675 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676}
5677#endif
5678
5679 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005680ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681{
5682 return (c == NUL || c == '|' || c == '"' || c == '\n');
5683}
5684
5685#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5686 || defined(PROTO)
5687/*
5688 * Return the next command, after the first '|' or '\n'.
5689 * Return NULL if not found.
5690 */
5691 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005692find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693{
5694 while (*p != '|' && *p != '\n')
5695 {
5696 if (*p == NUL)
5697 return NULL;
5698 ++p;
5699 }
5700 return (p + 1);
5701}
5702#endif
5703
5704/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005705 * Check if *p is a separator between Ex commands, skipping over white space.
5706 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 */
5708 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005709check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005711 char_u *s = skipwhite(p);
5712
5713 if (*s == '|' || *s == '\n')
5714 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 else
5716 return NULL;
5717}
5718
5719/*
5720 * - if there are more files to edit
5721 * - and this is the last window
5722 * - and forceit not used
5723 * - and not repeated twice on a row
5724 * return FAIL and give error message if 'message' TRUE
5725 * return OK otherwise
5726 */
5727 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005728check_more(
5729 int message, /* when FALSE check only, no messages */
5730 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
5732 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5733
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005734 if (!forceit && only_one_window()
5735 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 {
5737 if (message)
5738 {
5739#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5740 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5741 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005742 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
5744 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005745 vim_strncpy(buff,
5746 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005747 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005749 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 _("%d more files to edit. Quit anyway?"), n);
5751 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5752 return OK;
5753 return FAIL;
5754 }
5755#endif
5756 if (n == 1)
5757 EMSG(_("E173: 1 more file to edit"));
5758 else
5759 EMSGN(_("E173: %ld more files to edit"), n);
5760 quitmore = 2; /* next try to quit is allowed */
5761 }
5762 return FAIL;
5763 }
5764 return OK;
5765}
5766
5767#ifdef FEAT_CMDL_COMPL
5768/*
5769 * Function given to ExpandGeneric() to obtain the list of command names.
5770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005772get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773{
5774 if (idx >= (int)CMD_SIZE)
5775# ifdef FEAT_USR_CMDS
5776 return get_user_command_name(idx);
5777# else
5778 return NULL;
5779# endif
5780 return cmdnames[idx].cmd_name;
5781}
5782#endif
5783
5784#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005785static int uc_add_command(char_u *name, size_t name_len, char_u *rep, long argt, long def, int flags, int compl, char_u *compl_arg, int addr_type, int force);
5786static void uc_list(char_u *name, size_t name_len);
5787static int uc_scan_attr(char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg, int* attr_type_arg);
5788static char_u *uc_split_args(char_u *arg, size_t *lenp);
5789static size_t uc_check_code(char_u *code, size_t len, char_u *buf, ucmd_T *cmd, exarg_T *eap, char_u **split_buf, size_t *split_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
5791 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005792uc_add_command(
5793 char_u *name,
5794 size_t name_len,
5795 char_u *rep,
5796 long argt,
5797 long def,
5798 int flags,
5799 int compl,
5800 char_u *compl_arg,
5801 int addr_type,
5802 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803{
5804 ucmd_T *cmd = NULL;
5805 char_u *p;
5806 int i;
5807 int cmp = 1;
5808 char_u *rep_buf = NULL;
5809 garray_T *gap;
5810
Bram Moolenaar9c102382006-05-03 21:26:49 +00005811 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 if (rep_buf == NULL)
5813 {
5814 /* Can't replace termcodes - try using the string as is */
5815 rep_buf = vim_strsave(rep);
5816
5817 /* Give up if out of memory */
5818 if (rep_buf == NULL)
5819 return FAIL;
5820 }
5821
5822 /* get address of growarray: global or in curbuf */
5823 if (flags & UC_BUFFER)
5824 {
5825 gap = &curbuf->b_ucmds;
5826 if (gap->ga_itemsize == 0)
5827 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5828 }
5829 else
5830 gap = &ucmds;
5831
5832 /* Search for the command in the already defined commands. */
5833 for (i = 0; i < gap->ga_len; ++i)
5834 {
5835 size_t len;
5836
5837 cmd = USER_CMD_GA(gap, i);
5838 len = STRLEN(cmd->uc_name);
5839 cmp = STRNCMP(name, cmd->uc_name, name_len);
5840 if (cmp == 0)
5841 {
5842 if (name_len < len)
5843 cmp = -1;
5844 else if (name_len > len)
5845 cmp = 1;
5846 }
5847
5848 if (cmp == 0)
5849 {
5850 if (!force)
5851 {
5852 EMSG(_("E174: Command already exists: add ! to replace it"));
5853 goto fail;
5854 }
5855
5856 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005857 cmd->uc_rep = NULL;
5858#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5859 vim_free(cmd->uc_compl_arg);
5860 cmd->uc_compl_arg = NULL;
5861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 break;
5863 }
5864
5865 /* Stop as soon as we pass the name to add */
5866 if (cmp < 0)
5867 break;
5868 }
5869
5870 /* Extend the array unless we're replacing an existing command */
5871 if (cmp != 0)
5872 {
5873 if (ga_grow(gap, 1) != OK)
5874 goto fail;
5875 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5876 goto fail;
5877
5878 cmd = USER_CMD_GA(gap, i);
5879 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5880
5881 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882
5883 cmd->uc_name = p;
5884 }
5885
5886 cmd->uc_rep = rep_buf;
5887 cmd->uc_argt = argt;
5888 cmd->uc_def = def;
5889 cmd->uc_compl = compl;
5890#ifdef FEAT_EVAL
5891 cmd->uc_scriptID = current_SID;
5892# ifdef FEAT_CMDL_COMPL
5893 cmd->uc_compl_arg = compl_arg;
5894# endif
5895#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005896 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897
5898 return OK;
5899
5900fail:
5901 vim_free(rep_buf);
5902#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5903 vim_free(compl_arg);
5904#endif
5905 return FAIL;
5906}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005909#if defined(FEAT_USR_CMDS)
5910static struct
5911{
5912 int expand;
5913 char *name;
5914} addr_type_complete[] =
5915{
5916 {ADDR_ARGUMENTS, "arguments"},
5917 {ADDR_LINES, "lines"},
5918 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5919 {ADDR_TABS, "tabs"},
5920 {ADDR_BUFFERS, "buffers"},
5921 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005922 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005923 {-1, NULL}
5924};
5925#endif
5926
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005927#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928/*
5929 * List of names for completion for ":command" with the EXPAND_ flag.
5930 * Must be alphabetical for completion.
5931 */
5932static struct
5933{
5934 int expand;
5935 char *name;
5936} command_complete[] =
5937{
5938 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005939 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005941 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005943 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005944#if defined(FEAT_CSCOPE)
5945 {EXPAND_CSCOPE, "cscope"},
5946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5948 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005949 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950#endif
5951 {EXPAND_DIRECTORIES, "dir"},
5952 {EXPAND_ENV_VARS, "environment"},
5953 {EXPAND_EVENTS, "event"},
5954 {EXPAND_EXPRESSION, "expression"},
5955 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005956 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005957 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 {EXPAND_FUNCTIONS, "function"},
5959 {EXPAND_HELP, "help"},
5960 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005961#if defined(FEAT_CMDHIST)
5962 {EXPAND_HISTORY, "history"},
5963#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005964#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005965 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005966 {EXPAND_LOCALES, "locale"},
5967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 {EXPAND_MAPPINGS, "mapping"},
5969 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005970 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005971 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005972#if defined(FEAT_PROFILE)
5973 {EXPAND_SYNTIME, "syntime"},
5974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005976 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005977 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005978#if defined(FEAT_SIGNS)
5979 {EXPAND_SIGN, "sign"},
5980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 {EXPAND_TAGS, "tag"},
5982 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005983 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 {EXPAND_USER_VARS, "var"},
5985 {0, NULL}
5986};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005989#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005991uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992{
5993 int i, j;
5994 int found = FALSE;
5995 ucmd_T *cmd;
5996 int len;
5997 long a;
5998 garray_T *gap;
5999
6000 gap = &curbuf->b_ucmds;
6001 for (;;)
6002 {
6003 for (i = 0; i < gap->ga_len; ++i)
6004 {
6005 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006006 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007
Bram Moolenaard29459b2016-08-26 22:29:11 +02006008 /* Skip commands which don't match the requested prefix and
6009 * commands filtered out. */
6010 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6011 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 continue;
6013
6014 /* Put out the title first time */
6015 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006016 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 found = TRUE;
6018 msg_putchar('\n');
6019 if (got_int)
6020 break;
6021
6022 /* Special cases */
6023 msg_putchar(a & BANG ? '!' : ' ');
6024 msg_putchar(a & REGSTR ? '"' : ' ');
6025 msg_putchar(gap != &ucmds ? 'b' : ' ');
6026 msg_putchar(' ');
6027
6028 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
6029 len = (int)STRLEN(cmd->uc_name) + 4;
6030
6031 do {
6032 msg_putchar(' ');
6033 ++len;
6034 } while (len < 16);
6035
6036 len = 0;
6037
6038 /* Arguments */
6039 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6040 {
6041 case 0: IObuff[len++] = '0'; break;
6042 case (EXTRA): IObuff[len++] = '*'; break;
6043 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6044 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6045 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6046 }
6047
6048 do {
6049 IObuff[len++] = ' ';
6050 } while (len < 5);
6051
6052 /* Range */
6053 if (a & (RANGE|COUNT))
6054 {
6055 if (a & COUNT)
6056 {
6057 /* -count=N */
6058 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6059 len += (int)STRLEN(IObuff + len);
6060 }
6061 else if (a & DFLALL)
6062 IObuff[len++] = '%';
6063 else if (cmd->uc_def >= 0)
6064 {
6065 /* -range=N */
6066 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6067 len += (int)STRLEN(IObuff + len);
6068 }
6069 else
6070 IObuff[len++] = '.';
6071 }
6072
6073 do {
6074 IObuff[len++] = ' ';
6075 } while (len < 11);
6076
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006077 /* Address Type */
6078 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6079 if (addr_type_complete[j].expand != ADDR_LINES
6080 && addr_type_complete[j].expand == cmd->uc_addr_type)
6081 {
6082 STRCPY(IObuff + len, addr_type_complete[j].name);
6083 len += (int)STRLEN(IObuff + len);
6084 break;
6085 }
6086
6087 do {
6088 IObuff[len++] = ' ';
6089 } while (len < 21);
6090
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 /* Completion */
6092 for (j = 0; command_complete[j].expand != 0; ++j)
6093 if (command_complete[j].expand == cmd->uc_compl)
6094 {
6095 STRCPY(IObuff + len, command_complete[j].name);
6096 len += (int)STRLEN(IObuff + len);
6097 break;
6098 }
6099
6100 do {
6101 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006102 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103
6104 IObuff[len] = '\0';
6105 msg_outtrans(IObuff);
6106
6107 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006108#ifdef FEAT_EVAL
6109 if (p_verbose > 0)
6110 last_set_msg(cmd->uc_scriptID);
6111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 out_flush();
6113 ui_breakcheck();
6114 if (got_int)
6115 break;
6116 }
6117 if (gap == &ucmds || i < gap->ga_len)
6118 break;
6119 gap = &ucmds;
6120 }
6121
6122 if (!found)
6123 MSG(_("No user-defined commands found"));
6124}
6125
6126 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006127uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128{
6129 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6130 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6131 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6132 0xb9, 0x7f, 0};
6133 int i;
6134
6135 for (i = 0; fcmd[i]; ++i)
6136 IObuff[i] = fcmd[i] - 0x40;
6137 IObuff[i] = 0;
6138 return IObuff;
6139}
6140
6141 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006142uc_scan_attr(
6143 char_u *attr,
6144 size_t len,
6145 long *argt,
6146 long *def,
6147 int *flags,
6148 int *compl,
6149 char_u **compl_arg,
6150 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151{
6152 char_u *p;
6153
6154 if (len == 0)
6155 {
6156 EMSG(_("E175: No attribute specified"));
6157 return FAIL;
6158 }
6159
6160 /* First, try the simple attributes (no arguments) */
6161 if (STRNICMP(attr, "bang", len) == 0)
6162 *argt |= BANG;
6163 else if (STRNICMP(attr, "buffer", len) == 0)
6164 *flags |= UC_BUFFER;
6165 else if (STRNICMP(attr, "register", len) == 0)
6166 *argt |= REGSTR;
6167 else if (STRNICMP(attr, "bar", len) == 0)
6168 *argt |= TRLBAR;
6169 else
6170 {
6171 int i;
6172 char_u *val = NULL;
6173 size_t vallen = 0;
6174 size_t attrlen = len;
6175
6176 /* Look for the attribute name - which is the part before any '=' */
6177 for (i = 0; i < (int)len; ++i)
6178 {
6179 if (attr[i] == '=')
6180 {
6181 val = &attr[i + 1];
6182 vallen = len - i - 1;
6183 attrlen = i;
6184 break;
6185 }
6186 }
6187
6188 if (STRNICMP(attr, "nargs", attrlen) == 0)
6189 {
6190 if (vallen == 1)
6191 {
6192 if (*val == '0')
6193 /* Do nothing - this is the default */;
6194 else if (*val == '1')
6195 *argt |= (EXTRA | NOSPC | NEEDARG);
6196 else if (*val == '*')
6197 *argt |= EXTRA;
6198 else if (*val == '?')
6199 *argt |= (EXTRA | NOSPC);
6200 else if (*val == '+')
6201 *argt |= (EXTRA | NEEDARG);
6202 else
6203 goto wrong_nargs;
6204 }
6205 else
6206 {
6207wrong_nargs:
6208 EMSG(_("E176: Invalid number of arguments"));
6209 return FAIL;
6210 }
6211 }
6212 else if (STRNICMP(attr, "range", attrlen) == 0)
6213 {
6214 *argt |= RANGE;
6215 if (vallen == 1 && *val == '%')
6216 *argt |= DFLALL;
6217 else if (val != NULL)
6218 {
6219 p = val;
6220 if (*def >= 0)
6221 {
6222two_count:
6223 EMSG(_("E177: Count cannot be specified twice"));
6224 return FAIL;
6225 }
6226
6227 *def = getdigits(&p);
6228 *argt |= (ZEROR | NOTADR);
6229
6230 if (p != val + vallen || vallen == 0)
6231 {
6232invalid_count:
6233 EMSG(_("E178: Invalid default value for count"));
6234 return FAIL;
6235 }
6236 }
6237 }
6238 else if (STRNICMP(attr, "count", attrlen) == 0)
6239 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006240 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241
6242 if (val != NULL)
6243 {
6244 p = val;
6245 if (*def >= 0)
6246 goto two_count;
6247
6248 *def = getdigits(&p);
6249
6250 if (p != val + vallen)
6251 goto invalid_count;
6252 }
6253
6254 if (*def < 0)
6255 *def = 0;
6256 }
6257 else if (STRNICMP(attr, "complete", attrlen) == 0)
6258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 if (val == NULL)
6260 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006261 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 return FAIL;
6263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006265 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6266 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006269 else if (STRNICMP(attr, "addr", attrlen) == 0)
6270 {
6271 *argt |= RANGE;
6272 if (val == NULL)
6273 {
6274 EMSG(_("E179: argument required for -addr"));
6275 return FAIL;
6276 }
6277 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6278 == FAIL)
6279 return FAIL;
6280 if (addr_type_arg != ADDR_LINES)
6281 *argt |= (ZEROR | NOTADR) ;
6282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 else
6284 {
6285 char_u ch = attr[len];
6286 attr[len] = '\0';
6287 EMSG2(_("E181: Invalid attribute: %s"), attr);
6288 attr[len] = ch;
6289 return FAIL;
6290 }
6291 }
6292
6293 return OK;
6294}
6295
Bram Moolenaara850a712009-01-28 14:42:59 +00006296/*
6297 * ":command ..."
6298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006300ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
6302 char_u *name;
6303 char_u *end;
6304 char_u *p;
6305 long argt = 0;
6306 long def = -1;
6307 int flags = 0;
6308 int compl = EXPAND_NOTHING;
6309 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006310 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006312 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313
6314 p = eap->arg;
6315
6316 /* Check for attributes */
6317 while (*p == '-')
6318 {
6319 ++p;
6320 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006321 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 == FAIL)
6323 return;
6324 p = skipwhite(end);
6325 }
6326
6327 /* Get the name (if any) and skip to the following argument */
6328 name = p;
6329 if (ASCII_ISALPHA(*p))
6330 while (ASCII_ISALNUM(*p))
6331 ++p;
6332 if (!ends_excmd(*p) && !vim_iswhite(*p))
6333 {
6334 EMSG(_("E182: Invalid command name"));
6335 return;
6336 }
6337 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006338 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339
6340 /* If there is nothing after the name, and no attributes were specified,
6341 * we are listing commands
6342 */
6343 p = skipwhite(end);
6344 if (!has_attr && ends_excmd(*p))
6345 {
6346 uc_list(name, end - name);
6347 }
6348 else if (!ASCII_ISUPPER(*name))
6349 {
6350 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6351 return;
6352 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006353 else if ((name_len == 1 && *name == 'X')
6354 || (name_len <= 4
6355 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6356 {
6357 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6358 return;
6359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 else
6361 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006362 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363}
6364
6365/*
6366 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 * Clear all user commands, global and for current buffer.
6368 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006369 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006370ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
6372 uc_clear(&ucmds);
6373 uc_clear(&curbuf->b_ucmds);
6374}
6375
6376/*
6377 * Clear all user commands for "gap".
6378 */
6379 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006380uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381{
6382 int i;
6383 ucmd_T *cmd;
6384
6385 for (i = 0; i < gap->ga_len; ++i)
6386 {
6387 cmd = USER_CMD_GA(gap, i);
6388 vim_free(cmd->uc_name);
6389 vim_free(cmd->uc_rep);
6390# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6391 vim_free(cmd->uc_compl_arg);
6392# endif
6393 }
6394 ga_clear(gap);
6395}
6396
6397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006398ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399{
6400 int i = 0;
6401 ucmd_T *cmd = NULL;
6402 int cmp = -1;
6403 garray_T *gap;
6404
6405 gap = &curbuf->b_ucmds;
6406 for (;;)
6407 {
6408 for (i = 0; i < gap->ga_len; ++i)
6409 {
6410 cmd = USER_CMD_GA(gap, i);
6411 cmp = STRCMP(eap->arg, cmd->uc_name);
6412 if (cmp <= 0)
6413 break;
6414 }
6415 if (gap == &ucmds || cmp == 0)
6416 break;
6417 gap = &ucmds;
6418 }
6419
6420 if (cmp != 0)
6421 {
6422 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6423 return;
6424 }
6425
6426 vim_free(cmd->uc_name);
6427 vim_free(cmd->uc_rep);
6428# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6429 vim_free(cmd->uc_compl_arg);
6430# endif
6431
6432 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433
6434 if (i < gap->ga_len)
6435 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6436}
6437
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006438/*
6439 * split and quote args for <f-args>
6440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006442uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
6444 char_u *buf;
6445 char_u *p;
6446 char_u *q;
6447 int len;
6448
6449 /* Precalculate length */
6450 p = arg;
6451 len = 2; /* Initial and final quotes */
6452
6453 while (*p)
6454 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006455 if (p[0] == '\\' && p[1] == '\\')
6456 {
6457 len += 2;
6458 p += 2;
6459 }
6460 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 {
6462 len += 1;
6463 p += 2;
6464 }
6465 else if (*p == '\\' || *p == '"')
6466 {
6467 len += 2;
6468 p += 1;
6469 }
6470 else if (vim_iswhite(*p))
6471 {
6472 p = skipwhite(p);
6473 if (*p == NUL)
6474 break;
6475 len += 3; /* "," */
6476 }
6477 else
6478 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006479#ifdef FEAT_MBYTE
6480 int charlen = (*mb_ptr2len)(p);
6481 len += charlen;
6482 p += charlen;
6483#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 ++len;
6485 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 }
6488 }
6489
6490 buf = alloc(len + 1);
6491 if (buf == NULL)
6492 {
6493 *lenp = 0;
6494 return buf;
6495 }
6496
6497 p = arg;
6498 q = buf;
6499 *q++ = '"';
6500 while (*p)
6501 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006502 if (p[0] == '\\' && p[1] == '\\')
6503 {
6504 *q++ = '\\';
6505 *q++ = '\\';
6506 p += 2;
6507 }
6508 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 {
6510 *q++ = p[1];
6511 p += 2;
6512 }
6513 else if (*p == '\\' || *p == '"')
6514 {
6515 *q++ = '\\';
6516 *q++ = *p++;
6517 }
6518 else if (vim_iswhite(*p))
6519 {
6520 p = skipwhite(p);
6521 if (*p == NUL)
6522 break;
6523 *q++ = '"';
6524 *q++ = ',';
6525 *q++ = '"';
6526 }
6527 else
6528 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006529 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 }
6531 }
6532 *q++ = '"';
6533 *q = 0;
6534
6535 *lenp = len;
6536 return buf;
6537}
6538
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006539 static size_t
6540add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6541{
6542 size_t result;
6543
6544 result = STRLEN(mod_str);
6545 if (*multi_mods)
6546 result += 1;
6547 if (buf != NULL)
6548 {
6549 if (*multi_mods)
6550 STRCAT(buf, " ");
6551 STRCAT(buf, mod_str);
6552 }
6553
6554 *multi_mods = 1;
6555
6556 return result;
6557}
6558
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559/*
6560 * Check for a <> code in a user command.
6561 * "code" points to the '<'. "len" the length of the <> (inclusive).
6562 * "buf" is where the result is to be added.
6563 * "split_buf" points to a buffer used for splitting, caller should free it.
6564 * "split_len" is the length of what "split_buf" contains.
6565 * Returns the length of the replacement, which has been added to "buf".
6566 * Returns -1 if there was no match, and only the "<" has been copied.
6567 */
6568 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006569uc_check_code(
6570 char_u *code,
6571 size_t len,
6572 char_u *buf,
6573 ucmd_T *cmd, /* the user command we're expanding */
6574 exarg_T *eap, /* ex arguments */
6575 char_u **split_buf,
6576 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577{
6578 size_t result = 0;
6579 char_u *p = code + 1;
6580 size_t l = len - 2;
6581 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006582 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6583 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584
6585 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6586 {
6587 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6588 p += 2;
6589 l -= 2;
6590 }
6591
Bram Moolenaar371d5402006-03-20 21:47:49 +00006592 ++l;
6593 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006595 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006597 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006599 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006601 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006603 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006605 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006607 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006609 else if (STRNICMP(p, "mods>", l) == 0)
6610 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611
6612 switch (type)
6613 {
6614 case ct_ARGS:
6615 /* Simple case first */
6616 if (*eap->arg == NUL)
6617 {
6618 if (quote == 1)
6619 {
6620 result = 2;
6621 if (buf != NULL)
6622 STRCPY(buf, "''");
6623 }
6624 else
6625 result = 0;
6626 break;
6627 }
6628
6629 /* When specified there is a single argument don't split it.
6630 * Works for ":Cmd %" when % is "a b c". */
6631 if ((eap->argt & NOSPC) && quote == 2)
6632 quote = 1;
6633
6634 switch (quote)
6635 {
6636 case 0: /* No quoting, no splitting */
6637 result = STRLEN(eap->arg);
6638 if (buf != NULL)
6639 STRCPY(buf, eap->arg);
6640 break;
6641 case 1: /* Quote, but don't split */
6642 result = STRLEN(eap->arg) + 2;
6643 for (p = eap->arg; *p; ++p)
6644 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006645#ifdef FEAT_MBYTE
6646 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6647 /* DBCS can contain \ in a trail byte, skip the
6648 * double-byte character. */
6649 ++p;
6650 else
6651#endif
6652 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 ++result;
6654 }
6655
6656 if (buf != NULL)
6657 {
6658 *buf++ = '"';
6659 for (p = eap->arg; *p; ++p)
6660 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006661#ifdef FEAT_MBYTE
6662 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6663 /* DBCS can contain \ in a trail byte, copy the
6664 * double-byte character to avoid escaping. */
6665 *buf++ = *p++;
6666 else
6667#endif
6668 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 *buf++ = '\\';
6670 *buf++ = *p;
6671 }
6672 *buf = '"';
6673 }
6674
6675 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006676 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 /* This is hard, so only do it once, and cache the result */
6678 if (*split_buf == NULL)
6679 *split_buf = uc_split_args(eap->arg, split_len);
6680
6681 result = *split_len;
6682 if (buf != NULL && result != 0)
6683 STRCPY(buf, *split_buf);
6684
6685 break;
6686 }
6687 break;
6688
6689 case ct_BANG:
6690 result = eap->forceit ? 1 : 0;
6691 if (quote)
6692 result += 2;
6693 if (buf != NULL)
6694 {
6695 if (quote)
6696 *buf++ = '"';
6697 if (eap->forceit)
6698 *buf++ = '!';
6699 if (quote)
6700 *buf = '"';
6701 }
6702 break;
6703
6704 case ct_LINE1:
6705 case ct_LINE2:
6706 case ct_COUNT:
6707 {
6708 char num_buf[20];
6709 long num = (type == ct_LINE1) ? eap->line1 :
6710 (type == ct_LINE2) ? eap->line2 :
6711 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6712 size_t num_len;
6713
6714 sprintf(num_buf, "%ld", num);
6715 num_len = STRLEN(num_buf);
6716 result = num_len;
6717
6718 if (quote)
6719 result += 2;
6720
6721 if (buf != NULL)
6722 {
6723 if (quote)
6724 *buf++ = '"';
6725 STRCPY(buf, num_buf);
6726 buf += num_len;
6727 if (quote)
6728 *buf = '"';
6729 }
6730
6731 break;
6732 }
6733
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006734 case ct_MODS:
6735 {
6736 int multi_mods = 0;
6737 typedef struct {
6738 int *varp;
6739 char *name;
6740 } mod_entry_T;
6741 static mod_entry_T mod_entries[] = {
6742#ifdef FEAT_BROWSE_CMD
6743 {&cmdmod.browse, "browse"},
6744#endif
6745#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6746 {&cmdmod.confirm, "confirm"},
6747#endif
6748 {&cmdmod.hide, "hide"},
6749 {&cmdmod.keepalt, "keepalt"},
6750 {&cmdmod.keepjumps, "keepjumps"},
6751 {&cmdmod.keepmarks, "keepmarks"},
6752 {&cmdmod.keeppatterns, "keeppatterns"},
6753 {&cmdmod.lockmarks, "lockmarks"},
6754 {&cmdmod.noswapfile, "noswapfile"},
6755 {NULL, NULL}
6756 };
6757 int i;
6758
6759 result = quote ? 2 : 0;
6760 if (buf != NULL)
6761 {
6762 if (quote)
6763 *buf++ = '"';
6764 *buf = '\0';
6765 }
6766
6767#ifdef FEAT_WINDOWS
6768 /* :aboveleft and :leftabove */
6769 if (cmdmod.split & WSP_ABOVE)
6770 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6771 /* :belowright and :rightbelow */
6772 if (cmdmod.split & WSP_BELOW)
6773 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6774 /* :botright */
6775 if (cmdmod.split & WSP_BOT)
6776 result += add_cmd_modifier(buf, "botright", &multi_mods);
6777#endif
6778
6779 /* the modifiers that are simple flags */
6780 for (i = 0; mod_entries[i].varp != NULL; ++i)
6781 if (*mod_entries[i].varp)
6782 result += add_cmd_modifier(buf, mod_entries[i].name,
6783 &multi_mods);
6784
6785 /* TODO: How to support :noautocmd? */
6786#ifdef HAVE_SANDBOX
6787 /* TODO: How to support :sandbox?*/
6788#endif
6789 /* :silent */
6790 if (msg_silent > 0)
6791 result += add_cmd_modifier(buf,
6792 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6793#ifdef FEAT_WINDOWS
6794 /* :tab */
6795 if (cmdmod.tab > 0)
6796 result += add_cmd_modifier(buf, "tab", &multi_mods);
6797 /* :topleft */
6798 if (cmdmod.split & WSP_TOP)
6799 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6800#endif
6801 /* TODO: How to support :unsilent?*/
6802 /* :verbose */
6803 if (p_verbose > 0)
6804 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6805#ifdef FEAT_WINDOWS
6806 /* :vertical */
6807 if (cmdmod.split & WSP_VERT)
6808 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6809#endif
6810 if (quote && buf != NULL)
6811 {
6812 buf += result - 2;
6813 *buf = '"';
6814 }
6815 break;
6816 }
6817
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 case ct_REGISTER:
6819 result = eap->regname ? 1 : 0;
6820 if (quote)
6821 result += 2;
6822 if (buf != NULL)
6823 {
6824 if (quote)
6825 *buf++ = '\'';
6826 if (eap->regname)
6827 *buf++ = eap->regname;
6828 if (quote)
6829 *buf = '\'';
6830 }
6831 break;
6832
6833 case ct_LT:
6834 result = 1;
6835 if (buf != NULL)
6836 *buf = '<';
6837 break;
6838
6839 default:
6840 /* Not recognized: just copy the '<' and return -1. */
6841 result = (size_t)-1;
6842 if (buf != NULL)
6843 *buf = '<';
6844 break;
6845 }
6846
6847 return result;
6848}
6849
6850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006851do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
6853 char_u *buf;
6854 char_u *p;
6855 char_u *q;
6856
6857 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006858 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006859 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 size_t len, totlen;
6861
6862 size_t split_len = 0;
6863 char_u *split_buf = NULL;
6864 ucmd_T *cmd;
6865#ifdef FEAT_EVAL
6866 scid_T save_current_SID = current_SID;
6867#endif
6868
6869 if (eap->cmdidx == CMD_USER)
6870 cmd = USER_CMD(eap->useridx);
6871 else
6872 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6873
6874 /*
6875 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006876 * First round: "buf" is NULL, compute length, allocate "buf".
6877 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 */
6879 buf = NULL;
6880 for (;;)
6881 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006882 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006883 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006885
6886 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006888 start = vim_strchr(p, '<');
6889 if (start != NULL)
6890 end = vim_strchr(start + 1, '>');
6891 if (buf != NULL)
6892 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006893 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6894 ;
6895 if (*ksp == K_SPECIAL
6896 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006897 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6898# ifdef FEAT_GUI
6899 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6900# endif
6901 ))
6902 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006903 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006904 * KS_SPECIAL KE_FILLER, like for mappings, but
6905 * do_cmdline() doesn't handle that, so convert it back.
6906 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6907 len = ksp - p;
6908 if (len > 0)
6909 {
6910 mch_memmove(q, p, len);
6911 q += len;
6912 }
6913 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6914 p = ksp + 3;
6915 continue;
6916 }
6917 }
6918
6919 /* break if there no <item> is found */
6920 if (start == NULL || end == NULL)
6921 break;
6922
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 /* Include the '>' */
6924 ++end;
6925
6926 /* Take everything up to the '<' */
6927 len = start - p;
6928 if (buf == NULL)
6929 totlen += len;
6930 else
6931 {
6932 mch_memmove(q, p, len);
6933 q += len;
6934 }
6935
6936 len = uc_check_code(start, end - start, q, cmd, eap,
6937 &split_buf, &split_len);
6938 if (len == (size_t)-1)
6939 {
6940 /* no match, continue after '<' */
6941 p = start + 1;
6942 len = 1;
6943 }
6944 else
6945 p = end;
6946 if (buf == NULL)
6947 totlen += len;
6948 else
6949 q += len;
6950 }
6951 if (buf != NULL) /* second time here, finished */
6952 {
6953 STRCPY(q, p);
6954 break;
6955 }
6956
6957 totlen += STRLEN(p); /* Add on the trailing characters */
6958 buf = alloc((unsigned)(totlen + 1));
6959 if (buf == NULL)
6960 {
6961 vim_free(split_buf);
6962 return;
6963 }
6964 }
6965
6966#ifdef FEAT_EVAL
6967 current_SID = cmd->uc_scriptID;
6968#endif
6969 (void)do_cmdline(buf, eap->getline, eap->cookie,
6970 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6971#ifdef FEAT_EVAL
6972 current_SID = save_current_SID;
6973#endif
6974 vim_free(buf);
6975 vim_free(split_buf);
6976}
6977
6978# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6979 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006980get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981{
6982 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6983}
6984
6985/*
6986 * Function given to ExpandGeneric() to obtain the list of user command names.
6987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006989get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990{
6991 if (idx < curbuf->b_ucmds.ga_len)
6992 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6993 idx -= curbuf->b_ucmds.ga_len;
6994 if (idx < ucmds.ga_len)
6995 return USER_CMD(idx)->uc_name;
6996 return NULL;
6997}
6998
6999/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007000 * Function given to ExpandGeneric() to obtain the list of user address type names.
7001 */
7002 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007003get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007004{
7005 return (char_u *)addr_type_complete[idx].name;
7006}
7007
7008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 * Function given to ExpandGeneric() to obtain the list of user command
7010 * attributes.
7011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007013get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
7015 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007016 {"addr", "bang", "bar", "buffer", "complete",
7017 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007019 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 return NULL;
7021 return (char_u *)user_cmd_flags[idx];
7022}
7023
7024/*
7025 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007028get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
7030 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7031
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007032 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 return NULL;
7034 return (char_u *)user_cmd_nargs[idx];
7035}
7036
7037/*
7038 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007041get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
7043 return (char_u *)command_complete[idx].name;
7044}
7045# endif /* FEAT_CMDL_COMPL */
7046
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007047/*
7048 * Parse address type argument
7049 */
7050 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007051parse_addr_type_arg(
7052 char_u *value,
7053 int vallen,
7054 long *argt,
7055 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007056{
7057 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007058
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007059 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7060 {
7061 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7062 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7063 if (a && b)
7064 {
7065 *addr_type_arg = addr_type_complete[i].expand;
7066 break;
7067 }
7068 }
7069
7070 if (addr_type_complete[i].expand == -1)
7071 {
7072 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007073
7074 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
7075 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007076 err[i] = NUL;
7077 EMSG2(_("E180: Invalid address type value: %s"), err);
7078 return FAIL;
7079 }
7080
7081 if (*addr_type_arg != ADDR_LINES)
7082 *argt |= NOTADR;
7083
7084 return OK;
7085}
7086
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087#endif /* FEAT_USR_CMDS */
7088
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007089#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7090/*
7091 * Parse a completion argument "value[vallen]".
7092 * The detected completion goes in "*complp", argument type in "*argt".
7093 * When there is an argument, for function and user defined completion, it's
7094 * copied to allocated memory and stored in "*compl_arg".
7095 * Returns FAIL if something is wrong.
7096 */
7097 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007098parse_compl_arg(
7099 char_u *value,
7100 int vallen,
7101 int *complp,
7102 long *argt,
7103 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007104{
7105 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007106# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007107 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007108# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007109 int i;
7110 int valend = vallen;
7111
7112 /* Look for any argument part - which is the part after any ',' */
7113 for (i = 0; i < vallen; ++i)
7114 {
7115 if (value[i] == ',')
7116 {
7117 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007118# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007119 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007120# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007121 valend = i;
7122 break;
7123 }
7124 }
7125
7126 for (i = 0; command_complete[i].expand != 0; ++i)
7127 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007128 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007129 && STRNCMP(value, command_complete[i].name, valend) == 0)
7130 {
7131 *complp = command_complete[i].expand;
7132 if (command_complete[i].expand == EXPAND_BUFFERS)
7133 *argt |= BUFNAME;
7134 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7135 || command_complete[i].expand == EXPAND_FILES)
7136 *argt |= XFILE;
7137 break;
7138 }
7139 }
7140
7141 if (command_complete[i].expand == 0)
7142 {
7143 EMSG2(_("E180: Invalid complete value: %s"), value);
7144 return FAIL;
7145 }
7146
7147# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7148 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7149 && arg != NULL)
7150# else
7151 if (arg != NULL)
7152# endif
7153 {
7154 EMSG(_("E468: Completion argument only allowed for custom completion"));
7155 return FAIL;
7156 }
7157
7158# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7159 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7160 && arg == NULL)
7161 {
7162 EMSG(_("E467: Custom completion requires a function argument"));
7163 return FAIL;
7164 }
7165
7166 if (arg != NULL)
7167 *compl_arg = vim_strnsave(arg, (int)arglen);
7168# endif
7169 return OK;
7170}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007171
7172 int
7173cmdcomplete_str_to_type(char_u *complete_str)
7174{
7175 int i;
7176
7177 for (i = 0; command_complete[i].expand != 0; ++i)
7178 if (STRCMP(complete_str, command_complete[i].name) == 0)
7179 return command_complete[i].expand;
7180
7181 return EXPAND_NOTHING;
7182}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007183#endif
7184
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007186ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187{
Bram Moolenaare6850792010-05-14 15:28:44 +02007188 if (*eap->arg == NUL)
7189 {
7190#ifdef FEAT_EVAL
7191 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7192 char_u *p = NULL;
7193
7194 if (expr != NULL)
7195 {
7196 ++emsg_off;
7197 p = eval_to_string(expr, NULL, FALSE);
7198 --emsg_off;
7199 vim_free(expr);
7200 }
7201 if (p != NULL)
7202 {
7203 MSG(p);
7204 vim_free(p);
7205 }
7206 else
7207 MSG("default");
7208#else
7209 MSG(_("unknown"));
7210#endif
7211 }
7212 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007213 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214}
7215
7216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007217ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218{
7219 if (*eap->arg == NUL && eap->cmd[2] == '!')
7220 MSG(_("Greetings, Vim user!"));
7221 do_highlight(eap->arg, eap->forceit, FALSE);
7222}
7223
7224
7225/*
7226 * Call this function if we thought we were going to exit, but we won't
7227 * (because of an error). May need to restore the terminal mode.
7228 */
7229 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007230not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231{
7232 exiting = FALSE;
7233 settmode(TMODE_RAW);
7234}
7235
7236/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007237 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 */
7239 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007240ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007242#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007243 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007244#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007245
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246#ifdef FEAT_CMDWIN
7247 if (cmdwin_type != 0)
7248 {
7249 cmdwin_result = Ctrl_C;
7250 return;
7251 }
7252#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007253 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007254 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007255 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007256 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007257 return;
7258 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007259#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007260 if (eap->addr_count > 0)
7261 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007262 int wnr = eap->line2;
7263
7264 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7265 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007266 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007267 }
7268 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007269#endif
7270#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007271 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007272#endif
7273
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007274#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007275 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007276 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007277 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007278 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007279 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007280 return;
7281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
7283#ifdef FEAT_NETBEANS_INTG
7284 netbeansForcedQuit = eap->forceit;
7285#endif
7286
7287 /*
7288 * If there are more files or windows we won't exit.
7289 */
7290 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7291 exiting = TRUE;
7292 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007293 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7294 | (eap->forceit ? CCGD_FORCEIT : 0)
7295 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007297 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 {
7299 not_exiting();
7300 }
7301 else
7302 {
7303#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007304 /* quit last window
7305 * Note: only_one_window() returns true, even so a help window is
7306 * still open. In that case only quit, if no address has been
7307 * specified. Example:
7308 * :h|wincmd w|1q - don't quit
7309 * :h|wincmd w|q - quit
7310 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007311 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312#endif
7313 getout(0);
7314#ifdef FEAT_WINDOWS
7315# ifdef FEAT_GUI
7316 need_mouse_correct = TRUE;
7317# endif
7318 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007319 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320#endif
7321 }
7322}
7323
7324/*
7325 * ":cquit".
7326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007328ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329{
7330 getout(1); /* this does not always pass on the exit code to the Manx
7331 compiler. why? */
7332}
7333
7334/*
7335 * ":qall": try to quit all windows
7336 */
7337 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007338ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339{
7340# ifdef FEAT_CMDWIN
7341 if (cmdwin_type != 0)
7342 {
7343 if (eap->forceit)
7344 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7345 else
7346 cmdwin_result = K_XF2;
7347 return;
7348 }
7349# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007350
7351 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007352 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007353 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007354 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007355 return;
7356 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007357#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007358 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7359 /* Refuse to quit when locked or when the buffer in the last window is
7360 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007361 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007362 return;
7363#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007364
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007366 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 getout(0);
7368 not_exiting();
7369}
7370
Bram Moolenaard9967712006-03-11 21:18:15 +00007371#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372/*
7373 * ":close": close current window, unless it is the last one
7374 */
7375 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007376ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007378 win_T *win;
7379 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380# ifdef FEAT_CMDWIN
7381 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007382 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 else
7384# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007385 if (!text_locked()
7386#ifdef FEAT_AUTOCMD
7387 && !curbuf_locked()
7388#endif
7389 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007390 {
7391 if (eap->addr_count == 0)
7392 ex_win_close(eap->forceit, curwin, NULL);
7393 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007394 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007395 {
7396 winnr++;
7397 if (winnr == eap->line2)
7398 break;
7399 }
7400 if (win == NULL)
7401 win = lastwin;
7402 ex_win_close(eap->forceit, win, NULL);
7403 }
7404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405}
7406
Bram Moolenaard9967712006-03-11 21:18:15 +00007407# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007408/*
7409 * ":pclose": Close any preview window.
7410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007412ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007413{
7414 win_T *win;
7415
Bram Moolenaar29323592016-07-24 22:04:11 +02007416 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007417 if (win->w_p_pvw)
7418 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007419 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007420 break;
7421 }
7422}
Bram Moolenaard9967712006-03-11 21:18:15 +00007423# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007424
Bram Moolenaarf740b292006-02-16 22:11:02 +00007425/*
7426 * Close window "win" and take care of handling closing the last window for a
7427 * modified buffer.
7428 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007430ex_win_close(
7431 int forceit,
7432 win_T *win,
7433 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434{
7435 int need_hide;
7436 buf_T *buf = win->w_buffer;
7437
7438 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007439 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007441# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 if ((p_confirm || cmdmod.confirm) && p_write)
7443 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007444 bufref_T bufref;
7445
7446 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007448 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 return;
7450 need_hide = FALSE;
7451 }
7452 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 {
7455 EMSG(_(e_nowrtmsg));
7456 return;
7457 }
7458 }
7459
Bram Moolenaard9967712006-03-11 21:18:15 +00007460# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007462# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007463
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007465 if (tp == NULL)
7466 win_close(win, !need_hide && !P_HID(buf));
7467 else
7468 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469}
7470
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007472 * Handle the argument for a tabpage related ex command.
7473 * Returns a tabpage number.
7474 * When an error is encountered then eap->errmsg is set.
7475 */
7476 static int
7477get_tabpage_arg(exarg_T *eap)
7478{
7479 int tab_number;
7480 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7481
7482 if (eap->arg && *eap->arg != NUL)
7483 {
7484 char_u *p = eap->arg;
7485 char_u *p_save;
7486 int relative = 0; /* argument +N/-N means: go to N places to the
7487 * right/left relative to the current position. */
7488
7489 if (*p == '-')
7490 {
7491 relative = -1;
7492 p++;
7493 }
7494 else if (*p == '+')
7495 {
7496 relative = 1;
7497 p++;
7498 }
7499
7500 p_save = p;
7501 tab_number = getdigits(&p);
7502
7503 if (relative == 0)
7504 {
7505 if (STRCMP(p, "$") == 0)
7506 tab_number = LAST_TAB_NR;
7507 else if (p == p_save || *p_save == '-' || *p != NUL
7508 || tab_number > LAST_TAB_NR)
7509 {
7510 /* No numbers as argument. */
7511 eap->errmsg = e_invarg;
7512 goto theend;
7513 }
7514 }
7515 else
7516 {
7517 if (*p_save == NUL)
7518 tab_number = 1;
7519 else if (p == p_save || *p_save == '-' || *p != NUL
7520 || tab_number == 0)
7521 {
7522 /* No numbers as argument. */
7523 eap->errmsg = e_invarg;
7524 goto theend;
7525 }
7526 tab_number = tab_number * relative + tabpage_index(curtab);
7527 if (!unaccept_arg0 && relative == -1)
7528 --tab_number;
7529 }
7530 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7531 eap->errmsg = e_invarg;
7532 }
7533 else if (eap->addr_count > 0)
7534 {
7535 if (unaccept_arg0 && eap->line2 == 0)
7536 eap->errmsg = e_invrange;
7537 else
7538 {
7539 tab_number = eap->line2;
7540 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7541 {
7542 --tab_number;
7543 if (tab_number < unaccept_arg0)
7544 eap->errmsg = e_invarg;
7545 }
7546 }
7547 }
7548 else
7549 {
7550 switch (eap->cmdidx)
7551 {
7552 case CMD_tabnext:
7553 tab_number = tabpage_index(curtab) + 1;
7554 if (tab_number > LAST_TAB_NR)
7555 tab_number = 1;
7556 break;
7557 case CMD_tabmove:
7558 tab_number = LAST_TAB_NR;
7559 break;
7560 default:
7561 tab_number = tabpage_index(curtab);
7562 }
7563 }
7564
7565theend:
7566 return tab_number;
7567}
7568
7569/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007570 * ":tabclose": close current tab page, unless it is the last one.
7571 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 */
7573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007574ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007576 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007577 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007578
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007579# ifdef FEAT_CMDWIN
7580 if (cmdwin_type != 0)
7581 cmdwin_result = K_IGNORE;
7582 else
7583# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007584 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007585 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007586 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007588 tab_number = get_tabpage_arg(eap);
7589 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007590 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007591 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007592 if (tp == NULL)
7593 {
7594 beep_flush();
7595 return;
7596 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007597 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007598 {
7599 tabpage_close_other(tp, eap->forceit);
7600 return;
7601 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007602 else if (!text_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007603#ifdef FEAT_AUTOCMD
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007604 && !curbuf_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007605#endif
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007606 )
7607 tabpage_close(eap->forceit);
7608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007610}
7611
7612/*
7613 * ":tabonly": close all tab pages except the current one
7614 */
7615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007616ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007617{
7618 tabpage_T *tp;
7619 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007620 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007621
7622# ifdef FEAT_CMDWIN
7623 if (cmdwin_type != 0)
7624 cmdwin_result = K_IGNORE;
7625 else
7626# endif
7627 if (first_tabpage->tp_next == NULL)
7628 MSG(_("Already only one tab page"));
7629 else
7630 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007631 tab_number = get_tabpage_arg(eap);
7632 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007633 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007634 goto_tabpage(tab_number);
7635 /* Repeat this up to a 1000 times, because autocommands may
7636 * mess up the lists. */
7637 for (done = 0; done < 1000; ++done)
7638 {
7639 FOR_ALL_TABPAGES(tp)
7640 if (tp->tp_topframe != topframe)
7641 {
7642 tabpage_close_other(tp, eap->forceit);
7643 /* if we failed to close it quit */
7644 if (valid_tabpage(tp))
7645 done = 1000;
7646 /* start over, "tp" is now invalid */
7647 break;
7648 }
7649 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007650 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007651 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007652 }
7653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655
7656/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007657 * Close the current tab page.
7658 */
7659 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007660tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007661{
7662 /* First close all the windows but the current one. If that worked then
7663 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007664 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007665 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007666 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007667 ex_win_close(forceit, curwin, NULL);
7668# ifdef FEAT_GUI
7669 need_mouse_correct = TRUE;
7670# endif
7671}
7672
7673/*
7674 * Close tab page "tp", which is not the current tab page.
7675 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007676 * Also takes care of the tab pages line disappearing when closing the
7677 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007678 */
7679 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007680tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007681{
7682 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007683 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007684 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007685
7686 /* Limit to 1000 windows, autocommands may add a window while we close
7687 * one. OK, so I'm paranoid... */
7688 while (++done < 1000)
7689 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007690 wp = tp->tp_firstwin;
7691 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007692
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007693 /* Autocommands may delete the tab page under our fingers and we may
7694 * fail to close a window with a modified buffer. */
7695 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007696 break;
7697 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007698
Bram Moolenaar29323592016-07-24 22:04:11 +02007699#ifdef FEAT_AUTOCMD
7700 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7701#endif
7702
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007703 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007704 if (h != tabline_height())
7705 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007706}
7707
7708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 * ":only".
7710 */
7711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007712ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007714 win_T *wp;
7715 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716# ifdef FEAT_GUI
7717 need_mouse_correct = TRUE;
7718# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007719 if (eap->addr_count > 0)
7720 {
7721 wnr = eap->line2;
7722 for (wp = firstwin; --wnr > 0; )
7723 {
7724 if (wp->w_next == NULL)
7725 break;
7726 else
7727 wp = wp->w_next;
7728 }
7729 win_goto(wp);
7730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 close_others(TRUE, eap->forceit);
7732}
7733
7734/*
7735 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007736 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007738 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007739ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740{
7741 if (eap->addr_count == 0)
7742 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007743 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744}
7745#endif /* FEAT_WINDOWS */
7746
7747 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007748ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007750 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751#ifdef FEAT_WINDOWS
Bram Moolenaar2256c992016-11-15 21:17:07 +01007752 if (!eap->skip)
7753 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754# ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007755 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756# endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007757 if (eap->addr_count == 0)
7758 win_close(curwin, FALSE); /* don't free buffer */
7759 else
7760 {
7761 int winnr = 0;
7762 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007763
Bram Moolenaar2256c992016-11-15 21:17:07 +01007764 FOR_ALL_WINDOWS(win)
7765 {
7766 winnr++;
7767 if (winnr == eap->line2)
7768 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007769 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007770 if (win == NULL)
7771 win = lastwin;
7772 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776}
7777
7778/*
7779 * ":stop" and ":suspend": Suspend Vim.
7780 */
7781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007782ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783{
7784 /*
7785 * Disallow suspending for "rvim".
7786 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007787 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 {
7789 if (!eap->forceit)
7790 autowrite_all();
7791 windgoto((int)Rows - 1, 0);
7792 out_char('\n');
7793 out_flush();
7794 stoptermcap();
7795 out_flush(); /* needed for SUN to restore xterm buffer */
7796#ifdef FEAT_TITLE
7797 mch_restore_title(3); /* restore window titles */
7798#endif
7799 ui_suspend(); /* call machine specific function */
7800#ifdef FEAT_TITLE
7801 maketitle();
7802 resettitle(); /* force updating the title */
7803#endif
7804 starttermcap();
7805 scroll_start(); /* scroll screen before redrawing */
7806 redraw_later_clear();
7807 shell_resized(); /* may have resized window */
7808 }
7809}
7810
7811/*
7812 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7813 */
7814 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007815ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816{
7817#ifdef FEAT_CMDWIN
7818 if (cmdwin_type != 0)
7819 {
7820 cmdwin_result = Ctrl_C;
7821 return;
7822 }
7823#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007824 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007825 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007826 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007827 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007828 return;
7829 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007830#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007831 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7832 /* Refuse to quit when locked or when the buffer in the last window is
7833 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007834 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007835 return;
7836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837
7838 /*
7839 * if more files or windows we won't exit
7840 */
7841 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7842 exiting = TRUE;
7843 if ( ((eap->cmdidx == CMD_wq
7844 || curbufIsChanged())
7845 && do_write(eap) == FAIL)
7846 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007847 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 {
7849 not_exiting();
7850 }
7851 else
7852 {
7853#ifdef FEAT_WINDOWS
7854 if (only_one_window()) /* quit last window, exit Vim */
7855#endif
7856 getout(0);
7857#ifdef FEAT_WINDOWS
7858# ifdef FEAT_GUI
7859 need_mouse_correct = TRUE;
7860# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007861 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 win_close(curwin, !P_HID(curwin->w_buffer));
7863#endif
7864 }
7865}
7866
7867/*
7868 * ":print", ":list", ":number".
7869 */
7870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007871ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007873 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7874 EMSG(_(e_emptybuf));
7875 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007877 for ( ;!got_int; ui_breakcheck())
7878 {
7879 print_line(eap->line1,
7880 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7881 || (eap->flags & EXFLAG_NR)),
7882 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7883 if (++eap->line1 > eap->line2)
7884 break;
7885 out_flush(); /* show one line at a time */
7886 }
7887 setpcmark();
7888 /* put cursor at last line */
7889 curwin->w_cursor.lnum = eap->line2;
7890 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 }
7892
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894}
7895
7896#ifdef FEAT_BYTEOFF
7897 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007898ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899{
7900 goto_byte(eap->line2);
7901}
7902#endif
7903
7904/*
7905 * ":shell".
7906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007908ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909{
7910 do_shell(NULL, 0);
7911}
7912
7913#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7914 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007915 || defined(FEAT_GUI_MSWIN) \
7916 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 || defined(PROTO)
7918
7919/*
7920 * Handle a file drop. The code is here because a drop is *nearly* like an
7921 * :args command, but not quite (we have a list of exact filenames, so we
7922 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7923 * code is very similar to :args and hence needs access to a lot of the static
7924 * functions in this file.
7925 *
7926 * The list should be allocated using alloc(), as should each item in the
7927 * list. This function takes over responsibility for freeing the list.
7928 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007929 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007930 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 */
7932 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007933handle_drop(
7934 int filec, /* the number of files dropped */
7935 char_u **filev, /* the list of files dropped */
7936 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
7938 exarg_T ea;
7939 int save_msg_scroll = msg_scroll;
7940
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007941 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007942 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007944#ifdef FEAT_AUTOCMD
7945 if (curbuf_locked())
7946 return;
7947#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007948 /* When the screen is being updated we should not change buffers and
7949 * windows structures, it may cause freed memory to be used. */
7950 if (updating_screen)
7951 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952
7953 /* Check whether the current buffer is changed. If so, we will need
7954 * to split the current window or data could be lost.
7955 * We don't need to check if the 'hidden' option is set, as in this
7956 * case the buffer won't be lost.
7957 */
7958 if (!P_HID(curbuf) && !split)
7959 {
7960 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007961 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 --emsg_off;
7963 }
7964 if (split)
7965 {
7966# ifdef FEAT_WINDOWS
7967 if (win_split(0, 0) == FAIL)
7968 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007969 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970
7971 /* When splitting the window, create a new alist. Otherwise the
7972 * existing one is overwritten. */
7973 alist_unlink(curwin->w_alist);
7974 alist_new();
7975# else
7976 return; /* can't split, always fail */
7977# endif
7978 }
7979
7980 /*
7981 * Set up the new argument list.
7982 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007983 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984
7985 /*
7986 * Move to the first file.
7987 */
7988 /* Fake up a minimal "next" command for do_argfile() */
7989 vim_memset(&ea, 0, sizeof(ea));
7990 ea.cmd = (char_u *)"next";
7991 do_argfile(&ea, 0);
7992
7993 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7994 * mode that is not needed here. */
7995 need_start_insertmode = FALSE;
7996
7997 /* Restore msg_scroll, otherwise a following command may cause scrolling
7998 * unexpectedly. The screen will be redrawn by the caller, thus
7999 * msg_scroll being set by displaying a message is irrelevant. */
8000 msg_scroll = save_msg_scroll;
8001}
8002#endif
8003
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004/*
8005 * Clear an argument list: free all file names and reset it to zero entries.
8006 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008007 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008008alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009{
8010 while (--al->al_ga.ga_len >= 0)
8011 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8012 ga_clear(&al->al_ga);
8013}
8014
8015/*
8016 * Init an argument list.
8017 */
8018 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008019alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020{
8021 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8022}
8023
8024#if defined(FEAT_WINDOWS) || defined(PROTO)
8025
8026/*
8027 * Remove a reference from an argument list.
8028 * Ignored when the argument list is the global one.
8029 * If the argument list is no longer used by any window, free it.
8030 */
8031 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008032alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033{
8034 if (al != &global_alist && --al->al_refcount <= 0)
8035 {
8036 alist_clear(al);
8037 vim_free(al);
8038 }
8039}
8040
8041# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
8042/*
8043 * Create a new argument list and use it for the current window.
8044 */
8045 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008046alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047{
8048 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8049 if (curwin->w_alist == NULL)
8050 {
8051 curwin->w_alist = &global_alist;
8052 ++global_alist.al_refcount;
8053 }
8054 else
8055 {
8056 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008057 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 alist_init(curwin->w_alist);
8059 }
8060}
8061# endif
8062#endif
8063
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008064#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065/*
8066 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008067 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8068 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 */
8070 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008071alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072{
8073 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008074 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 char_u **new_arg_files;
8076 int new_arg_file_count;
8077 char_u *save_p_su = p_su;
8078 int i;
8079
8080 /* Don't use 'suffixes' here. This should work like the shell did the
8081 * expansion. Also, the vimrc file isn't read yet, thus the user
8082 * can't set the options. */
8083 p_su = empty_option;
8084 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8085 if (old_arg_files != NULL)
8086 {
8087 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008088 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8089 old_arg_count = GARGCOUNT;
8090 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008092 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 && new_arg_file_count > 0)
8094 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008095 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8096 TRUE, fnum_list, fnum_len);
8097 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 }
8100 p_su = save_p_su;
8101}
8102#endif
8103
8104/*
8105 * Set the argument list for the current window.
8106 * Takes over the allocated files[] and the allocated fnames in it.
8107 */
8108 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008109alist_set(
8110 alist_T *al,
8111 int count,
8112 char_u **files,
8113 int use_curbuf,
8114 int *fnum_list,
8115 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116{
8117 int i;
8118
8119 alist_clear(al);
8120 if (ga_grow(&al->al_ga, count) == OK)
8121 {
8122 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008123 {
8124 if (got_int)
8125 {
8126 /* When adding many buffers this can take a long time. Allow
8127 * interrupting here. */
8128 while (i < count)
8129 vim_free(files[i++]);
8130 break;
8131 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008132
8133 /* May set buffer name of a buffer previously used for the
8134 * argument list, so that it's re-used by alist_add. */
8135 if (fnum_list != NULL && i < fnum_len)
8136 buf_set_name(fnum_list[i], files[i]);
8137
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008139 ui_breakcheck();
8140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 vim_free(files);
8142 }
8143 else
8144 FreeWild(count, files);
8145#ifdef FEAT_WINDOWS
8146 if (al == &global_alist)
8147#endif
8148 arg_had_last = FALSE;
8149}
8150
8151/*
8152 * Add file "fname" to argument list "al".
8153 * "fname" must have been allocated and "al" must have been checked for room.
8154 */
8155 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008156alist_add(
8157 alist_T *al,
8158 char_u *fname,
8159 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160{
8161 if (fname == NULL) /* don't add NULL file names */
8162 return;
8163#ifdef BACKSLASH_IN_FILENAME
8164 slash_adjust(fname);
8165#endif
8166 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8167 if (set_fnum > 0)
8168 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8169 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8170 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171}
8172
8173#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8174/*
8175 * Adjust slashes in file names. Called after 'shellslash' was set.
8176 */
8177 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008178alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179{
8180 int i;
8181# ifdef FEAT_WINDOWS
8182 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008183 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184# endif
8185
8186 for (i = 0; i < GARGCOUNT; ++i)
8187 if (GARGLIST[i].ae_fname != NULL)
8188 slash_adjust(GARGLIST[i].ae_fname);
8189# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008190 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 if (wp->w_alist != &global_alist)
8192 for (i = 0; i < WARGCOUNT(wp); ++i)
8193 if (WARGLIST(wp)[i].ae_fname != NULL)
8194 slash_adjust(WARGLIST(wp)[i].ae_fname);
8195# endif
8196}
8197#endif
8198
8199/*
8200 * ":preserve".
8201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008203ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008205 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 ml_preserve(curbuf, TRUE);
8207}
8208
8209/*
8210 * ":recover".
8211 */
8212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008213ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214{
8215 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8216 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008217 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8218 | CCGD_MULTWIN
8219 | (eap->forceit ? CCGD_FORCEIT : 0)
8220 | CCGD_EXCMD)
8221
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 && (*eap->arg == NUL
8223 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8224 ml_recover();
8225 recoverymode = FALSE;
8226}
8227
8228/*
8229 * Command modifier used in a wrong way.
8230 */
8231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008232ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233{
8234 eap->errmsg = e_invcmd;
8235}
8236
8237#ifdef FEAT_WINDOWS
8238/*
8239 * :sview [+command] file split window with new file, read-only
8240 * :split [[+command] file] split window with current or new file
8241 * :vsplit [[+command] file] split window vertically with current or new file
8242 * :new [[+command] file] split window with no or new file
8243 * :vnew [[+command] file] split vertically window with no or new file
8244 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008245 *
8246 * :tabedit open new Tab page with empty window
8247 * :tabedit [+command] file open new Tab page and edit "file"
8248 * :tabnew [[+command] file] just like :tabedit
8249 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 */
8251 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008252ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008254 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008255# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008257# endif
8258# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008262# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008266# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008268 * quickfix windows. But it's OK when doing ":tab split". */
8269 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {
8271 if (eap->cmdidx == CMD_split)
8272 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 if (eap->cmdidx == CMD_vsplit)
8274 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008278# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008279 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {
8281 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8282 FNAME_MESS, TRUE, curbuf->b_ffname);
8283 if (fname == NULL)
8284 goto theend;
8285 eap->arg = fname;
8286 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008287# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008291# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 && eap->cmdidx != CMD_new)
8295 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008296# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008297 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008298# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008299 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008300# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008301 au_has_group((char_u *)"FileExplorer"))
8302 {
8303 /* No browsing supported but we do have the file explorer:
8304 * Edit the directory. */
8305 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8306 eap->arg = (char_u *)".";
8307 }
8308 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008309# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008310 {
8311 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008313 if (fname == NULL)
8314 goto theend;
8315 eap->arg = fname;
8316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 }
8318 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008321 /*
8322 * Either open new tab page or split the window.
8323 */
8324 if (eap->cmdidx == CMD_tabedit
8325 || eap->cmdidx == CMD_tabfind
8326 || eap->cmdidx == CMD_tabnew)
8327 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008328 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8329 : eap->addr_count == 0 ? 0
8330 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008331 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008332 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008333
8334 /* set the alternate buffer for the window we came from */
8335 if (curwin != old_curwin
8336 && win_valid(old_curwin)
8337 && old_curwin->w_buffer != curbuf
8338 && !cmdmod.keepalt)
8339 old_curwin->w_alt_fnum = curbuf->b_fnum;
8340 }
8341 }
8342 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8344 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008345# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 /* Reset 'scrollbind' when editing another file, but keep it when
8347 * doing ":split" without arguments. */
8348 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008349# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008351# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008353 {
8354 RESET_BINDING(curwin);
8355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 else
8357 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 do_exedit(eap, old_curwin);
8360 }
8361
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008362# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008366# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367theend:
8368 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008369# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008371
8372/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008373 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008374 */
8375 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008376tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008377{
8378 exarg_T ea;
8379
8380 vim_memset(&ea, 0, sizeof(ea));
8381 ea.cmdidx = CMD_tabnew;
8382 ea.cmd = (char_u *)"tabn";
8383 ea.arg = (char_u *)"";
8384 ex_splitview(&ea);
8385}
8386
8387/*
8388 * :tabnext command
8389 */
8390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008391ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008392{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008393 int tab_number;
8394
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008395 switch (eap->cmdidx)
8396 {
8397 case CMD_tabfirst:
8398 case CMD_tabrewind:
8399 goto_tabpage(1);
8400 break;
8401 case CMD_tablast:
8402 goto_tabpage(9999);
8403 break;
8404 case CMD_tabprevious:
8405 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008406 if (eap->arg && *eap->arg != NUL)
8407 {
8408 char_u *p = eap->arg;
8409 char_u *p_save = p;
8410
8411 tab_number = getdigits(&p);
8412 if (p == p_save || *p_save == '-' || *p != NUL
8413 || tab_number == 0)
8414 {
8415 /* No numbers as argument. */
8416 eap->errmsg = e_invarg;
8417 return;
8418 }
8419 }
8420 else
8421 {
8422 if (eap->addr_count == 0)
8423 tab_number = 1;
8424 else
8425 {
8426 tab_number = eap->line2;
8427 if (tab_number < 1)
8428 {
8429 eap->errmsg = e_invrange;
8430 return;
8431 }
8432 }
8433 }
8434 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008435 break;
8436 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008437 tab_number = get_tabpage_arg(eap);
8438 if (eap->errmsg == NULL)
8439 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008440 break;
8441 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008442}
8443
8444/*
8445 * :tabmove command
8446 */
8447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008448ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008449{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008450 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008451
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008452 tab_number = get_tabpage_arg(eap);
8453 if (eap->errmsg == NULL)
8454 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008455}
8456
8457/*
8458 * :tabs command: List tabs and their contents.
8459 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008461ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008462{
8463 tabpage_T *tp;
8464 win_T *wp;
8465 int tabcount = 1;
8466
8467 msg_start();
8468 msg_scroll = TRUE;
8469 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8470 {
8471 msg_putchar('\n');
8472 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8473 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8474 out_flush(); /* output one line at a time */
8475 ui_breakcheck();
8476
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008477 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008478 wp = firstwin;
8479 else
8480 wp = tp->tp_firstwin;
8481 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8482 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008483 msg_putchar('\n');
8484 msg_putchar(wp == curwin ? '>' : ' ');
8485 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008486 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8487 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008488 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008489 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008490 else
8491 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8492 IObuff, IOSIZE, TRUE);
8493 msg_outtrans(IObuff);
8494 out_flush(); /* output one line at a time */
8495 ui_breakcheck();
8496 }
8497 }
8498}
8499
8500#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501
8502/*
8503 * ":mode": Set screen mode.
8504 * If no argument given, just get the screen size and redraw.
8505 */
8506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008507ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508{
8509 if (*eap->arg == NUL)
8510 shell_resized();
8511 else
8512 mch_screenmode(eap->arg);
8513}
8514
8515#ifdef FEAT_WINDOWS
8516/*
8517 * ":resize".
8518 * set, increment or decrement current window height
8519 */
8520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008521ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
8523 int n;
8524 win_T *wp = curwin;
8525
8526 if (eap->addr_count > 0)
8527 {
8528 n = eap->line2;
8529 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8530 ;
8531 }
8532
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008533# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008535# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 if (cmdmod.split & WSP_VERT)
8538 {
8539 if (*eap->arg == '-' || *eap->arg == '+')
8540 n += W_WIDTH(curwin);
8541 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8542 n = 9999;
8543 win_setwidth_win((int)n, wp);
8544 }
8545 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 {
8547 if (*eap->arg == '-' || *eap->arg == '+')
8548 n += curwin->w_height;
8549 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8550 n = 9999;
8551 win_setheight_win((int)n, wp);
8552 }
8553}
8554#endif
8555
8556/*
8557 * ":find [+command] <file>" command.
8558 */
8559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008560ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561{
8562#ifdef FEAT_SEARCHPATH
8563 char_u *fname;
8564 int count;
8565
8566 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8567 TRUE, curbuf->b_ffname);
8568 if (eap->addr_count > 0)
8569 {
8570 /* Repeat finding the file "count" times. This matters when it
8571 * appears several times in the path. */
8572 count = eap->line2;
8573 while (fname != NULL && --count > 0)
8574 {
8575 vim_free(fname);
8576 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8577 FALSE, curbuf->b_ffname);
8578 }
8579 }
8580
8581 if (fname != NULL)
8582 {
8583 eap->arg = fname;
8584#endif
8585 do_exedit(eap, NULL);
8586#ifdef FEAT_SEARCHPATH
8587 vim_free(fname);
8588 }
8589#endif
8590}
8591
8592/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008593 * ":open" simulation: for now just work like ":visual".
8594 */
8595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008596ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008597{
8598 regmatch_T regmatch;
8599 char_u *p;
8600
8601 curwin->w_cursor.lnum = eap->line2;
8602 beginline(BL_SOL | BL_FIX);
8603 if (*eap->arg == '/')
8604 {
8605 /* ":open /pattern/": put cursor in column found with pattern */
8606 ++eap->arg;
8607 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8608 *p = NUL;
8609 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8610 if (regmatch.regprog != NULL)
8611 {
8612 regmatch.rm_ic = p_ic;
8613 p = ml_get_curline();
8614 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008615 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008616 else
8617 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008618 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008619 }
8620 /* Move to the NUL, ignore any other arguments. */
8621 eap->arg += STRLEN(eap->arg);
8622 }
8623 check_cursor();
8624
8625 eap->cmdidx = CMD_visual;
8626 do_exedit(eap, NULL);
8627}
8628
8629/*
8630 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 */
8632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008633ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634{
8635 do_exedit(eap, NULL);
8636}
8637
8638/*
8639 * ":edit <file>" command and alikes.
8640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008642do_exedit(
8643 exarg_T *eap,
8644 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645{
8646 int n;
8647#ifdef FEAT_WINDOWS
8648 int need_hide;
8649#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008650 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651
8652 /*
8653 * ":vi" command ends Ex mode.
8654 */
8655 if (exmode_active && (eap->cmdidx == CMD_visual
8656 || eap->cmdidx == CMD_view))
8657 {
8658 exmode_active = FALSE;
8659 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008660 {
8661 /* Special case: ":global/pat/visual\NLvi-commands" */
8662 if (global_busy)
8663 {
8664 int rd = RedrawingDisabled;
8665 int nwr = no_wait_return;
8666 int ms = msg_scroll;
8667#ifdef FEAT_GUI
8668 int he = hold_gui_events;
8669#endif
8670
8671 if (eap->nextcmd != NULL)
8672 {
8673 stuffReadbuff(eap->nextcmd);
8674 eap->nextcmd = NULL;
8675 }
8676
8677 if (exmode_was != EXMODE_VIM)
8678 settmode(TMODE_RAW);
8679 RedrawingDisabled = 0;
8680 no_wait_return = 0;
8681 need_wait_return = FALSE;
8682 msg_scroll = 0;
8683#ifdef FEAT_GUI
8684 hold_gui_events = 0;
8685#endif
8686 must_redraw = CLEAR;
8687
8688 main_loop(FALSE, TRUE);
8689
8690 RedrawingDisabled = rd;
8691 no_wait_return = nwr;
8692 msg_scroll = ms;
8693#ifdef FEAT_GUI
8694 hold_gui_events = he;
8695#endif
8696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 }
8700
8701 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008702 || eap->cmdidx == CMD_tabnew
8703 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008704#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 || eap->cmdidx == CMD_vnew
8706#endif
8707 ) && *eap->arg == NUL)
8708 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008709 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 setpcmark();
8711 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008712 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8713 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 }
8715 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008716#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 && eap->cmdidx != CMD_vsplit
8718#endif
8719 )
8720 || *eap->arg != NUL
8721#ifdef FEAT_BROWSE
8722 || cmdmod.browse
8723#endif
8724 )
8725 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008726#ifdef FEAT_AUTOCMD
8727 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8728 * can bring us here, others are stopped earlier. */
8729 if (*eap->arg != NUL && curbuf_locked())
8730 return;
8731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 n = readonlymode;
8733 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8734 readonlymode = TRUE;
8735 else if (eap->cmdidx == CMD_enew)
8736 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8737 empty buffer */
8738 setpcmark();
8739 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8740 NULL, eap,
8741 /* ":edit" goes to first line if Vi compatible */
8742 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8743 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8744 ? ECMD_ONE : eap->do_ecmd_lnum,
8745 (P_HID(curbuf) ? ECMD_HIDE : 0)
8746 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008747 /* after a split we can use an existing buffer */
8748 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749#ifdef FEAT_LISTCMDS
8750 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8751#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008752 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 {
8754 /* Editing the file failed. If the window was split, close it. */
8755#ifdef FEAT_WINDOWS
8756 if (old_curwin != NULL)
8757 {
8758 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8759 if (!need_hide || P_HID(curbuf))
8760 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008761# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8762 cleanup_T cs;
8763
8764 /* Reset the error/interrupt/exception state here so that
8765 * aborting() returns FALSE when closing a window. */
8766 enter_cleanup(&cs);
8767# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768# ifdef FEAT_GUI
8769 need_mouse_correct = TRUE;
8770# endif
8771 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008772
8773# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8774 /* Restore the error/interrupt/exception state if not
8775 * discarded by a new aborting error, interrupt, or
8776 * uncaught exception. */
8777 leave_cleanup(&cs);
8778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 }
8780 }
8781#endif
8782 }
8783 else if (readonlymode && curbuf->b_nwindows == 1)
8784 {
8785 /* When editing an already visited buffer, 'readonly' won't be set
8786 * but the previous value is kept. With ":view" and ":sview" we
8787 * want the file to be readonly, except when another window is
8788 * editing the same buffer. */
8789 curbuf->b_p_ro = TRUE;
8790 }
8791 readonlymode = n;
8792 }
8793 else
8794 {
8795 if (eap->do_ecmd_cmd != NULL)
8796 do_cmdline_cmd(eap->do_ecmd_cmd);
8797#ifdef FEAT_TITLE
8798 n = curwin->w_arg_idx_invalid;
8799#endif
8800 check_arg_idx(curwin);
8801#ifdef FEAT_TITLE
8802 if (n != curwin->w_arg_idx_invalid)
8803 maketitle();
8804#endif
8805 }
8806
8807#ifdef FEAT_WINDOWS
8808 /*
8809 * if ":split file" worked, set alternate file name in old window to new
8810 * file
8811 */
8812 if (old_curwin != NULL
8813 && *eap->arg != NUL
8814 && curwin != old_curwin
8815 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008816 && old_curwin->w_buffer != curbuf
8817 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 old_curwin->w_alt_fnum = curbuf->b_fnum;
8819#endif
8820
8821 ex_no_reprint = TRUE;
8822}
8823
8824#ifndef FEAT_GUI
8825/*
8826 * ":gui" and ":gvim" when there is no GUI.
8827 */
8828 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008829ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830{
8831 eap->errmsg = e_nogvim;
8832}
8833#endif
8834
8835#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008837ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838{
8839 gui_make_tearoff(eap->arg);
8840}
8841#endif
8842
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008843#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008845ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008847 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848}
8849#endif
8850
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008852ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853{
8854 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8855 MSG(_("No swap file"));
8856 else
8857 msg(curbuf->b_ml.ml_mfp->mf_fname);
8858}
8859
8860/*
8861 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8862 * offset.
8863 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008866ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867{
8868#ifdef FEAT_SCROLLBIND
8869 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008870 win_T *save_curwin = curwin;
8871 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 long topline;
8873 long y;
8874 linenr_T old_linenr = curwin->w_cursor.lnum;
8875
8876 setpcmark();
8877
8878 /*
8879 * determine max topline
8880 */
8881 if (curwin->w_p_scb)
8882 {
8883 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008884 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 {
8886 if (wp->w_p_scb && wp->w_buffer)
8887 {
8888 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8889 if (topline > y)
8890 topline = y;
8891 }
8892 }
8893 if (topline < 1)
8894 topline = 1;
8895 }
8896 else
8897 {
8898 topline = 1;
8899 }
8900
8901
8902 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008903 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008905 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 {
8907 if (curwin->w_p_scb)
8908 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008909 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 y = topline - curwin->w_topline;
8911 if (y > 0)
8912 scrollup(y, TRUE);
8913 else
8914 scrolldown(-y, TRUE);
8915 curwin->w_scbind_pos = topline;
8916 redraw_later(VALID);
8917 cursor_correct();
8918#ifdef FEAT_WINDOWS
8919 curwin->w_redr_status = TRUE;
8920#endif
8921 }
8922 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008923 curwin = save_curwin;
8924 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 if (curwin->w_p_scb)
8926 {
8927 did_syncbind = TRUE;
8928 checkpcmark();
8929 if (old_linenr != curwin->w_cursor.lnum)
8930 {
8931 char_u ctrl_o[2];
8932
8933 ctrl_o[0] = Ctrl_O;
8934 ctrl_o[1] = 0;
8935 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8936 }
8937 }
8938#endif
8939}
8940
8941
8942 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008943ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008945 int i;
8946 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8947 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
8949 if (eap->usefilter) /* :r!cmd */
8950 do_bang(1, eap, FALSE, FALSE, TRUE);
8951 else
8952 {
8953 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8954 return;
8955
8956#ifdef FEAT_BROWSE
8957 if (cmdmod.browse)
8958 {
8959 char_u *browseFile;
8960
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008961 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 NULL, NULL, NULL, curbuf);
8963 if (browseFile != NULL)
8964 {
8965 i = readfile(browseFile, NULL,
8966 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8967 vim_free(browseFile);
8968 }
8969 else
8970 i = OK;
8971 }
8972 else
8973#endif
8974 if (*eap->arg == NUL)
8975 {
8976 if (check_fname() == FAIL) /* check for no file name */
8977 return;
8978 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8979 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8980 }
8981 else
8982 {
8983 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8984 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8985 i = readfile(eap->arg, NULL,
8986 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8987
8988 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008989 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 {
8991#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8992 if (!aborting())
8993#endif
8994 EMSG2(_(e_notopen), eap->arg);
8995 }
8996 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008997 {
8998 if (empty && exmode_active)
8999 {
9000 /* Delete the empty line that remains. Historically ex does
9001 * this but vi doesn't. */
9002 if (eap->line2 == 0)
9003 lnum = curbuf->b_ml.ml_line_count;
9004 else
9005 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009006 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009007 {
9008 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009009 if (curwin->w_cursor.lnum > 1
9010 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009011 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009012 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009013 }
9014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 }
9018}
9019
Bram Moolenaarea408852005-06-25 22:49:46 +00009020static char_u *prev_dir = NULL;
9021
9022#if defined(EXITFREE) || defined(PROTO)
9023 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009024free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009025{
9026 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00009027 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00009028
9029 vim_free(globaldir);
9030 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00009031}
9032#endif
9033
Bram Moolenaarf4258302013-06-02 18:20:17 +02009034/*
9035 * Deal with the side effects of changing the current directory.
9036 * When "local" is TRUE then this was after an ":lcd" command.
9037 */
9038 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009039post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009040{
9041 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01009042 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009043 if (local)
9044 {
9045 /* If still in global directory, need to remember current
9046 * directory as global directory. */
9047 if (globaldir == NULL && prev_dir != NULL)
9048 globaldir = vim_strsave(prev_dir);
9049 /* Remember this local directory for the window. */
9050 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9051 curwin->w_localdir = vim_strsave(NameBuff);
9052 }
9053 else
9054 {
9055 /* We are now in the global directory, no need to remember its
9056 * name. */
9057 vim_free(globaldir);
9058 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009059 }
9060
9061 shorten_fnames(TRUE);
9062}
9063
Bram Moolenaarea408852005-06-25 22:49:46 +00009064
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065/*
9066 * ":cd", ":lcd", ":chdir" and ":lchdir".
9067 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009068 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009069ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 char_u *new_dir;
9072 char_u *tofree;
9073
9074 new_dir = eap->arg;
9075#if !defined(UNIX) && !defined(VMS)
9076 /* for non-UNIX ":cd" means: print current directory */
9077 if (*new_dir == NUL)
9078 ex_pwd(NULL);
9079 else
9080#endif
9081 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009082#ifdef FEAT_AUTOCMD
9083 if (allbuf_locked())
9084 return;
9085#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009086 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9087 && !eap->forceit)
9088 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009089 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009090 return;
9091 }
9092
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 /* ":cd -": Change to previous directory */
9094 if (STRCMP(new_dir, "-") == 0)
9095 {
9096 if (prev_dir == NULL)
9097 {
9098 EMSG(_("E186: No previous directory"));
9099 return;
9100 }
9101 new_dir = prev_dir;
9102 }
9103
9104 /* Save current directory for next ":cd -" */
9105 tofree = prev_dir;
9106 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9107 prev_dir = vim_strsave(NameBuff);
9108 else
9109 prev_dir = NULL;
9110
9111#if defined(UNIX) || defined(VMS)
9112 /* for UNIX ":cd" means: go to home directory */
9113 if (*new_dir == NUL)
9114 {
9115 /* use NameBuff for home directory name */
9116# ifdef VMS
9117 char_u *p;
9118
9119 p = mch_getenv((char_u *)"SYS$LOGIN");
9120 if (p == NULL || *p == NUL) /* empty is the same as not set */
9121 NameBuff[0] = NUL;
9122 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009123 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124# else
9125 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9126# endif
9127 new_dir = NameBuff;
9128 }
9129#endif
9130 if (new_dir == NULL || vim_chdir(new_dir))
9131 EMSG(_(e_failed));
9132 else
9133 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02009134 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135
9136 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009137 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 ex_pwd(eap);
9139 }
9140 vim_free(tofree);
9141 }
9142}
9143
9144/*
9145 * ":pwd".
9146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009148ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
9150 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9151 {
9152#ifdef BACKSLASH_IN_FILENAME
9153 slash_adjust(NameBuff);
9154#endif
9155 msg(NameBuff);
9156 }
9157 else
9158 EMSG(_("E187: Unknown"));
9159}
9160
9161/*
9162 * ":=".
9163 */
9164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009165ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166{
9167 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009168 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169}
9170
9171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009172ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009174 int n;
9175 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176
9177 if (cursor_valid())
9178 {
9179 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9180 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009181 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009183
9184 len = eap->line2;
9185 switch (*eap->arg)
9186 {
9187 case 'm': break;
9188 case NUL: len *= 1000L; break;
9189 default: EMSG2(_(e_invarg2), eap->arg); return;
9190 }
9191 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192}
9193
9194/*
9195 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9196 */
9197 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009198do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199{
9200 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009201 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202
9203 cursor_on();
9204 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009205 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009207 wait_now = msec - done > 1000L ? 1000L : msec - done;
9208#ifdef FEAT_TIMERS
9209 {
9210 long due_time = check_due_timer();
9211
9212 if (due_time > 0 && due_time < wait_now)
9213 wait_now = due_time;
9214 }
9215#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009216#ifdef FEAT_JOB_CHANNEL
9217 if (has_any_channel() && wait_now > 100L)
9218 wait_now = 100L;
9219#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009220 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009221#ifdef FEAT_JOB_CHANNEL
9222 if (has_any_channel())
9223 ui_breakcheck_force(TRUE);
9224 else
9225#endif
9226 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009227#ifdef MESSAGE_QUEUE
9228 /* Process the netbeans and clientserver messages that may have been
9229 * received in the call to ui_breakcheck() when the GUI is in use. This
9230 * may occur when running a test case. */
9231 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 }
9234}
9235
9236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009237do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238{
9239 int mode;
9240 char_u *cmdp;
9241
9242 cmdp = eap->cmd;
9243 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9244
9245 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9246 eap->arg, mode, isabbrev))
9247 {
9248 case 1: EMSG(_(e_invarg));
9249 break;
9250 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9251 break;
9252 }
9253}
9254
9255/*
9256 * ":winsize" command (obsolete).
9257 */
9258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009259ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260{
9261 int w, h;
9262 char_u *arg = eap->arg;
9263 char_u *p;
9264
9265 w = getdigits(&arg);
9266 arg = skipwhite(arg);
9267 p = arg;
9268 h = getdigits(&arg);
9269 if (*p != NUL && *arg == NUL)
9270 set_shellsize(w, h, TRUE);
9271 else
9272 EMSG(_("E465: :winsize requires two number arguments"));
9273}
9274
9275#ifdef FEAT_WINDOWS
9276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009277ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278{
9279 int xchar = NUL;
9280 char_u *p;
9281
9282 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9283 {
9284 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9285 if (eap->arg[1] == NUL)
9286 {
9287 EMSG(_(e_invarg));
9288 return;
9289 }
9290 xchar = eap->arg[1];
9291 p = eap->arg + 2;
9292 }
9293 else
9294 p = eap->arg + 1;
9295
9296 eap->nextcmd = check_nextcmd(p);
9297 p = skipwhite(p);
9298 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9299 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009300 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 {
9302 /* Pass flags on for ":vertical wincmd ]". */
9303 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009304 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9306 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009307 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 }
9309}
9310#endif
9311
Bram Moolenaar843ee412004-06-30 16:16:41 +00009312#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313/*
9314 * ":winpos".
9315 */
9316 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009317ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318{
9319 int x, y;
9320 char_u *arg = eap->arg;
9321 char_u *p;
9322
9323 if (*arg == NUL)
9324 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009325# if defined(FEAT_GUI) || defined(MSWIN)
9326# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009328# else
9329 if (mch_get_winpos(&x, &y) != FAIL)
9330# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 {
9332 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9333 msg(IObuff);
9334 }
9335 else
9336# endif
9337 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9338 }
9339 else
9340 {
9341 x = getdigits(&arg);
9342 arg = skipwhite(arg);
9343 p = arg;
9344 y = getdigits(&arg);
9345 if (*p == NUL || *arg != NUL)
9346 {
9347 EMSG(_("E466: :winpos requires two number arguments"));
9348 return;
9349 }
9350# ifdef FEAT_GUI
9351 if (gui.in_use)
9352 gui_mch_set_winpos(x, y);
9353 else if (gui.starting)
9354 {
9355 /* Remember the coordinates for when the window is opened. */
9356 gui_win_x = x;
9357 gui_win_y = y;
9358 }
9359# ifdef HAVE_TGETENT
9360 else
9361# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009362# else
9363# ifdef MSWIN
9364 mch_set_winpos(x, y);
9365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366# endif
9367# ifdef HAVE_TGETENT
9368 if (*T_CWP)
9369 term_set_winpos(x, y);
9370# endif
9371 }
9372}
9373#endif
9374
9375/*
9376 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9377 */
9378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009379ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380{
9381 oparg_T oa;
9382
9383 clear_oparg(&oa);
9384 oa.regname = eap->regname;
9385 oa.start.lnum = eap->line1;
9386 oa.end.lnum = eap->line2;
9387 oa.line_count = eap->line2 - eap->line1 + 1;
9388 oa.motion_type = MLINE;
9389#ifdef FEAT_VIRTUALEDIT
9390 virtual_op = FALSE;
9391#endif
9392 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9393 {
9394 setpcmark();
9395 curwin->w_cursor.lnum = eap->line1;
9396 beginline(BL_SOL | BL_FIX);
9397 }
9398
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009399 if (VIsual_active)
9400 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009401
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 switch (eap->cmdidx)
9403 {
9404 case CMD_delete:
9405 oa.op_type = OP_DELETE;
9406 op_delete(&oa);
9407 break;
9408
9409 case CMD_yank:
9410 oa.op_type = OP_YANK;
9411 (void)op_yank(&oa, FALSE, TRUE);
9412 break;
9413
9414 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009415 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009417 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9418#else
9419 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009421 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 oa.op_type = OP_RSHIFT;
9423 else
9424 oa.op_type = OP_LSHIFT;
9425 op_shift(&oa, FALSE, eap->amount);
9426 break;
9427 }
9428#ifdef FEAT_VIRTUALEDIT
9429 virtual_op = MAYBE;
9430#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009431 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432}
9433
9434/*
9435 * ":put".
9436 */
9437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009438ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439{
9440 /* ":0put" works like ":1put!". */
9441 if (eap->line2 == 0)
9442 {
9443 eap->line2 = 1;
9444 eap->forceit = TRUE;
9445 }
9446 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009447 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9448 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449}
9450
9451/*
9452 * Handle ":copy" and ":move".
9453 */
9454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009455ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456{
9457 long n;
9458
Bram Moolenaarded27822017-01-02 14:27:34 +01009459 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 if (eap->arg == NULL) /* error detected */
9461 {
9462 eap->nextcmd = NULL;
9463 return;
9464 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009465 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466
9467 /*
9468 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9469 */
9470 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9471 {
9472 EMSG(_(e_invaddr));
9473 return;
9474 }
9475
9476 if (eap->cmdidx == CMD_move)
9477 {
9478 if (do_move(eap->line1, eap->line2, n) == FAIL)
9479 return;
9480 }
9481 else
9482 ex_copy(eap->line1, eap->line2, n);
9483 u_clearline();
9484 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009485 ex_may_print(eap);
9486}
9487
9488/*
9489 * Print the current line if flags were given to the Ex command.
9490 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009491 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009492ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009493{
9494 if (eap->flags != 0)
9495 {
9496 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9497 (eap->flags & EXFLAG_LIST));
9498 ex_no_reprint = TRUE;
9499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500}
9501
9502/*
9503 * ":smagic" and ":snomagic".
9504 */
9505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009506ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507{
9508 int magic_save = p_magic;
9509
9510 p_magic = (eap->cmdidx == CMD_smagic);
9511 do_sub(eap);
9512 p_magic = magic_save;
9513}
9514
9515/*
9516 * ":join".
9517 */
9518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009519ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
9521 curwin->w_cursor.lnum = eap->line1;
9522 if (eap->line1 == eap->line2)
9523 {
9524 if (eap->addr_count >= 2) /* :2,2join does nothing */
9525 return;
9526 if (eap->line2 == curbuf->b_ml.ml_line_count)
9527 {
9528 beep_flush();
9529 return;
9530 }
9531 ++eap->line2;
9532 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009533 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009535 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536}
9537
9538/*
9539 * ":[addr]@r" or ":[addr]*r": execute register
9540 */
9541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009542ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543{
9544 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009545 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546
9547 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009548 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549
9550#ifdef USE_ON_FLY_SCROLL
9551 dont_scroll = TRUE; /* disallow scrolling here */
9552#endif
9553
9554 /* get the register name. No name means to use the previous one */
9555 c = *eap->arg;
9556 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9557 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009558 /* Put the register in the typeahead buffer with the "silent" flag. */
9559 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9560 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 else
9565 {
9566 int save_efr = exec_from_reg;
9567
9568 exec_from_reg = TRUE;
9569
9570 /*
9571 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009572 * Continue until the stuff buffer is empty and all added characters
9573 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009575 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9577
9578 exec_from_reg = save_efr;
9579 }
9580}
9581
9582/*
9583 * ":!".
9584 */
9585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009586ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
9588 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9589}
9590
9591/*
9592 * ":undo".
9593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009595ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009597 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009598 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009599 else
9600 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601}
9602
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009603#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009605ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009606{
9607 char_u hash[UNDO_HASH_SIZE];
9608
9609 u_compute_hash(hash);
9610 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9611}
9612
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009614ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009615{
9616 char_u hash[UNDO_HASH_SIZE];
9617
9618 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009619 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009620}
9621#endif
9622
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623/*
9624 * ":redo".
9625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009627ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628{
9629 u_redo(1);
9630}
9631
9632/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009633 * ":earlier" and ":later".
9634 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009636ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009637{
9638 long count = 0;
9639 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009640 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009641 char_u *p = eap->arg;
9642
9643 if (*p == NUL)
9644 count = 1;
9645 else if (isdigit(*p))
9646 {
9647 count = getdigits(&p);
9648 switch (*p)
9649 {
9650 case 's': ++p; sec = TRUE; break;
9651 case 'm': ++p; sec = TRUE; count *= 60; break;
9652 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009653 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9654 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009655 }
9656 }
9657
9658 if (*p != NUL)
9659 EMSG2(_(e_invarg2), eap->arg);
9660 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009661 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9662 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009663}
9664
9665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 * ":redir": start/stop redirection.
9667 */
9668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009669ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670{
9671 char *mode;
9672 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009673 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674
Bram Moolenaarba768492016-07-08 15:32:54 +02009675#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009676 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009677 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009678 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009679 return;
9680 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009681#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009682
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 if (STRICMP(eap->arg, "END") == 0)
9684 close_redir();
9685 else
9686 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009687 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009689 ++arg;
9690 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009692 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 mode = "a";
9694 }
9695 else
9696 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009697 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698
9699 close_redir();
9700
9701 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009702 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 if (fname == NULL)
9704 return;
9705#ifdef FEAT_BROWSE
9706 if (cmdmod.browse)
9707 {
9708 char_u *browseFile;
9709
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009710 browseFile = do_browse(BROWSE_SAVE,
9711 (char_u *)_("Save Redirection"),
9712 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 if (browseFile == NULL)
9714 return; /* operation cancelled */
9715 vim_free(fname);
9716 fname = browseFile;
9717 eap->forceit = TRUE; /* since dialog already asked */
9718 }
9719#endif
9720
9721 redir_fd = open_exfile(fname, eap->forceit, mode);
9722 vim_free(fname);
9723 }
9724#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009725 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 {
9727 /* redirect to a register a-z (resp. A-Z for appending) */
9728 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009729 ++arg;
9730 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009732 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009733 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009735 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009737 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009738 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009739 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009740 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009742 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009743 if (*arg == '>')
9744 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009745 /* Make register empty when not using @A-@Z and the
9746 * command is valid. */
9747 if (*arg == NUL && !isupper(redir_reg))
9748 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009750 }
9751 if (*arg != NUL)
9752 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009753 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009754 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009755 }
9756 }
9757 else if (*arg == '=' && arg[1] == '>')
9758 {
9759 int append;
9760
9761 /* redirect to a variable */
9762 close_redir();
9763 arg += 2;
9764
9765 if (*arg == '>')
9766 {
9767 ++arg;
9768 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 }
9770 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009771 append = FALSE;
9772
9773 if (var_redir_start(skipwhite(arg), append) == OK)
9774 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 }
9776#endif
9777
9778 /* TODO: redirect to a buffer */
9779
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009781 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009783
9784 /* Make sure redirection is not off. Can happen for cmdline completion
9785 * that indirectly invokes a command to catch its output. */
9786 if (redir_fd != NULL
9787#ifdef FEAT_EVAL
9788 || redir_reg || redir_vname
9789#endif
9790 )
9791 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792}
9793
9794/*
9795 * ":redraw": force redraw
9796 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009797 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009798ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799{
9800 int r = RedrawingDisabled;
9801 int p = p_lz;
9802
9803 RedrawingDisabled = 0;
9804 p_lz = FALSE;
9805 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009806 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807#ifdef FEAT_TITLE
9808 if (need_maketitle)
9809 maketitle();
9810#endif
9811 RedrawingDisabled = r;
9812 p_lz = p;
9813
9814 /* Reset msg_didout, so that a message that's there is overwritten. */
9815 msg_didout = FALSE;
9816 msg_col = 0;
9817
Bram Moolenaar56667a52013-07-17 11:54:28 +02009818 /* No need to wait after an intentional redraw. */
9819 need_wait_return = FALSE;
9820
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 out_flush();
9822}
9823
9824/*
9825 * ":redrawstatus": force redraw of status line(s)
9826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009828ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829{
9830#if defined(FEAT_WINDOWS)
9831 int r = RedrawingDisabled;
9832 int p = p_lz;
9833
9834 RedrawingDisabled = 0;
9835 p_lz = FALSE;
9836 if (eap->forceit)
9837 status_redraw_all();
9838 else
9839 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009840 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 RedrawingDisabled = r;
9842 p_lz = p;
9843 out_flush();
9844#endif
9845}
9846
9847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009848close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849{
9850 if (redir_fd != NULL)
9851 {
9852 fclose(redir_fd);
9853 redir_fd = NULL;
9854 }
9855#ifdef FEAT_EVAL
9856 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009857 if (redir_vname)
9858 {
9859 var_redir_stop();
9860 redir_vname = 0;
9861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862#endif
9863}
9864
9865#if defined(FEAT_SESSION) && defined(USE_CRNL)
9866# define MKSESSION_NL
9867static int mksession_nl = FALSE; /* use NL only in put_eol() */
9868#endif
9869
9870/*
9871 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9872 */
9873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009874ex_mkrc(
9875 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876{
9877 FILE *fd;
9878 int failed = FALSE;
9879 char_u *fname;
9880#ifdef FEAT_BROWSE
9881 char_u *browseFile = NULL;
9882#endif
9883#ifdef FEAT_SESSION
9884 int view_session = FALSE;
9885 int using_vdir = FALSE; /* using 'viewdir'? */
9886 char_u *viewFile = NULL;
9887 unsigned *flagp;
9888#endif
9889
9890 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9891 {
9892#ifdef FEAT_SESSION
9893 view_session = TRUE;
9894#else
9895 ex_ni(eap);
9896 return;
9897#endif
9898 }
9899
9900#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009901 /* Use the short file name until ":lcd" is used. We also don't use the
9902 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009903 did_lcd = FALSE;
9904
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9906 if (eap->cmdidx == CMD_mkview
9907 && (*eap->arg == NUL
9908 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9909 {
9910 eap->forceit = TRUE;
9911 fname = get_view_file(*eap->arg);
9912 if (fname == NULL)
9913 return;
9914 viewFile = fname;
9915 using_vdir = TRUE;
9916 }
9917 else
9918#endif
9919 if (*eap->arg != NUL)
9920 fname = eap->arg;
9921 else if (eap->cmdidx == CMD_mkvimrc)
9922 fname = (char_u *)VIMRC_FILE;
9923#ifdef FEAT_SESSION
9924 else if (eap->cmdidx == CMD_mksession)
9925 fname = (char_u *)SESSION_FILE;
9926#endif
9927 else
9928 fname = (char_u *)EXRC_FILE;
9929
9930#ifdef FEAT_BROWSE
9931 if (cmdmod.browse)
9932 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009933 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934# ifdef FEAT_SESSION
9935 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9936 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9937# endif
9938 (char_u *)_("Save Setup"),
9939 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9940 if (browseFile == NULL)
9941 goto theend;
9942 fname = browseFile;
9943 eap->forceit = TRUE; /* since dialog already asked */
9944 }
9945#endif
9946
9947#if defined(FEAT_SESSION) && defined(vim_mkdir)
9948 /* When using 'viewdir' may have to create the directory. */
9949 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009950 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951#endif
9952
9953 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9954 if (fd != NULL)
9955 {
9956#ifdef FEAT_SESSION
9957 if (eap->cmdidx == CMD_mkview)
9958 flagp = &vop_flags;
9959 else
9960 flagp = &ssop_flags;
9961#endif
9962
9963#ifdef MKSESSION_NL
9964 /* "unix" in 'sessionoptions': use NL line separator */
9965 if (view_session && (*flagp & SSOP_UNIX))
9966 mksession_nl = TRUE;
9967#endif
9968
9969 /* Write the version command for :mkvimrc */
9970 if (eap->cmdidx == CMD_mkvimrc)
9971 (void)put_line(fd, "version 6.0");
9972
9973#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009974 if (eap->cmdidx == CMD_mksession)
9975 {
9976 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9977 failed = TRUE;
9978 }
9979
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 if (eap->cmdidx != CMD_mkview)
9981#endif
9982 {
9983 /* Write setting 'compatible' first, because it has side effects.
9984 * For that same reason only do it when needed. */
9985 if (p_cp)
9986 (void)put_line(fd, "if !&cp | set cp | endif");
9987 else
9988 (void)put_line(fd, "if &cp | set nocp | endif");
9989 }
9990
9991#ifdef FEAT_SESSION
9992 if (!view_session
9993 || (eap->cmdidx == CMD_mksession
9994 && (*flagp & SSOP_OPTIONS)))
9995#endif
9996 failed |= (makemap(fd, NULL) == FAIL
9997 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9998
9999#ifdef FEAT_SESSION
10000 if (!failed && view_session)
10001 {
10002 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10003 failed = TRUE;
10004 if (eap->cmdidx == CMD_mksession)
10005 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010006 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007
Bram Moolenaard9462e32011-04-11 21:35:11 +020010008 dirnow = alloc(MAXPATHL);
10009 if (dirnow == NULL)
10010 failed = TRUE;
10011 else
10012 {
10013 /*
10014 * Change to session file's dir.
10015 */
10016 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010018 *dirnow = NUL;
10019 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10020 {
10021 if (vim_chdirfile(fname) == OK)
10022 shorten_fnames(TRUE);
10023 }
10024 else if (*dirnow != NUL
10025 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10026 {
10027 if (mch_chdir((char *)globaldir) == 0)
10028 shorten_fnames(TRUE);
10029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030
Bram Moolenaard9462e32011-04-11 21:35:11 +020010031 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032
Bram Moolenaard9462e32011-04-11 21:35:11 +020010033 /* restore original dir */
10034 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010036 {
10037 if (mch_chdir((char *)dirnow) != 0)
10038 EMSG(_(e_prev_dir));
10039 shorten_fnames(TRUE);
10040 }
10041 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 }
10043 }
10044 else
10045 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010046 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10047 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 }
10049 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10050 == FAIL)
10051 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010052 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10053 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010054 if (eap->cmdidx == CMD_mksession)
10055 {
10056 if (put_line(fd, "unlet SessionLoad") == FAIL)
10057 failed = TRUE;
10058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 }
10060#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010061 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10062 failed = TRUE;
10063
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 failed |= fclose(fd);
10065
10066 if (failed)
10067 EMSG(_(e_write));
10068#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10069 else if (eap->cmdidx == CMD_mksession)
10070 {
10071 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010072 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073
Bram Moolenaard9462e32011-04-11 21:35:11 +020010074 tbuf = alloc(MAXPATHL);
10075 if (tbuf != NULL)
10076 {
10077 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10078 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10079 vim_free(tbuf);
10080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 }
10082#endif
10083#ifdef MKSESSION_NL
10084 mksession_nl = FALSE;
10085#endif
10086 }
10087
10088#ifdef FEAT_BROWSE
10089theend:
10090 vim_free(browseFile);
10091#endif
10092#ifdef FEAT_SESSION
10093 vim_free(viewFile);
10094#endif
10095}
10096
Bram Moolenaardf177f62005-02-22 08:39:57 +000010097#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10098 || defined(PROTO)
10099 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010100vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010101{
10102 if (vim_mkdir(name, prot) != 0)
10103 {
10104 EMSG2(_("E739: Cannot create directory: %s"), name);
10105 return FAIL;
10106 }
10107 return OK;
10108}
10109#endif
10110
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111/*
10112 * Open a file for writing for an Ex command, with some checks.
10113 * Return file descriptor, or NULL on failure.
10114 */
10115 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010116open_exfile(
10117 char_u *fname,
10118 int forceit,
10119 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120{
10121 FILE *fd;
10122
10123#ifdef UNIX
10124 /* with Unix it is possible to open a directory */
10125 if (mch_isdir(fname))
10126 {
10127 EMSG2(_(e_isadir2), fname);
10128 return NULL;
10129 }
10130#endif
10131 if (!forceit && *mode != 'a' && vim_fexists(fname))
10132 {
10133 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10134 return NULL;
10135 }
10136
10137 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10138 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10139
10140 return fd;
10141}
10142
10143/*
10144 * ":mark" and ":k".
10145 */
10146 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010147ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148{
10149 pos_T pos;
10150
10151 if (*eap->arg == NUL) /* No argument? */
10152 EMSG(_(e_argreq));
10153 else if (eap->arg[1] != NUL) /* more than one character? */
10154 EMSG(_(e_trailing));
10155 else
10156 {
10157 pos = curwin->w_cursor; /* save curwin->w_cursor */
10158 curwin->w_cursor.lnum = eap->line2;
10159 beginline(BL_WHITE | BL_FIX);
10160 if (setmark(*eap->arg) == FAIL) /* set mark */
10161 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10162 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10163 }
10164}
10165
10166/*
10167 * Update w_topline, w_leftcol and the cursor position.
10168 */
10169 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010170update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171{
10172 check_cursor(); /* put cursor on valid line */
10173 update_topline();
10174 if (!curwin->w_p_wrap)
10175 validate_cursor();
10176 update_curswant();
10177}
10178
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179/*
10180 * ":normal[!] {commands}": Execute normal mode commands.
10181 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010182 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010183ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 int save_msg_scroll = msg_scroll;
10186 int save_restart_edit = restart_edit;
10187 int save_msg_didout = msg_didout;
10188 int save_State = State;
10189 tasave_T tabuf;
10190 int save_insertmode = p_im;
10191 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010192 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193#ifdef FEAT_MBYTE
10194 char_u *arg = NULL;
10195 int l;
10196 char_u *p;
10197#endif
10198
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010199 if (ex_normal_lock > 0)
10200 {
10201 EMSG(_(e_secure));
10202 return;
10203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 if (ex_normal_busy >= p_mmd)
10205 {
10206 EMSG(_("E192: Recursive use of :normal too deep"));
10207 return;
10208 }
10209 ++ex_normal_busy;
10210
10211 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10212 restart_edit = 0; /* don't go to Insert mode */
10213 p_im = FALSE; /* don't use 'insertmode' */
10214
10215#ifdef FEAT_MBYTE
10216 /*
10217 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10218 * this for the K_SPECIAL leading byte, otherwise special keys will not
10219 * work.
10220 */
10221 if (has_mbyte)
10222 {
10223 int len = 0;
10224
10225 /* Count the number of characters to be escaped. */
10226 for (p = eap->arg; *p != NUL; ++p)
10227 {
10228# ifdef FEAT_GUI
10229 if (*p == CSI) /* leadbyte CSI */
10230 len += 2;
10231# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010232 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10234# ifdef FEAT_GUI
10235 || *p == CSI
10236# endif
10237 )
10238 len += 2;
10239 }
10240 if (len > 0)
10241 {
10242 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10243 if (arg != NULL)
10244 {
10245 len = 0;
10246 for (p = eap->arg; *p != NUL; ++p)
10247 {
10248 arg[len++] = *p;
10249# ifdef FEAT_GUI
10250 if (*p == CSI)
10251 {
10252 arg[len++] = KS_EXTRA;
10253 arg[len++] = (int)KE_CSI;
10254 }
10255# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010256 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257 {
10258 arg[len++] = *++p;
10259 if (*p == K_SPECIAL)
10260 {
10261 arg[len++] = KS_SPECIAL;
10262 arg[len++] = KE_FILLER;
10263 }
10264# ifdef FEAT_GUI
10265 else if (*p == CSI)
10266 {
10267 arg[len++] = KS_EXTRA;
10268 arg[len++] = (int)KE_CSI;
10269 }
10270# endif
10271 }
10272 arg[len] = NUL;
10273 }
10274 }
10275 }
10276 }
10277#endif
10278
10279 /*
10280 * Save the current typeahead. This is required to allow using ":normal"
10281 * from an event handler and makes sure we don't hang when the argument
10282 * ends with half a command.
10283 */
10284 save_typeahead(&tabuf);
10285 if (tabuf.typebuf_valid)
10286 {
10287 /*
10288 * Repeat the :normal command for each line in the range. When no
10289 * range given, execute it just once, without positioning the cursor
10290 * first.
10291 */
10292 do
10293 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 if (eap->addr_count != 0)
10295 {
10296 curwin->w_cursor.lnum = eap->line1++;
10297 curwin->w_cursor.col = 0;
10298 }
10299
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010300 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301#ifdef FEAT_MBYTE
10302 arg != NULL ? arg :
10303#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010304 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 }
10306 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10307 }
10308
10309 /* Might not return to the main loop when in an event handler. */
10310 update_topline_cursor();
10311
10312 /* Restore the previous typeahead. */
10313 restore_typeahead(&tabuf);
10314
10315 --ex_normal_busy;
10316 msg_scroll = save_msg_scroll;
10317 restart_edit = save_restart_edit;
10318 p_im = save_insertmode;
10319 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010320 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10322
10323 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010324 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010326#ifdef FEAT_MOUSE
10327 setmouse();
10328#endif
10329#ifdef CURSOR_SHAPE
10330 ui_cursor_shape(); /* may show different cursor shape */
10331#endif
10332
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333#ifdef FEAT_MBYTE
10334 vim_free(arg);
10335#endif
10336}
10337
10338/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010339 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 */
10341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010342ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010344 if (eap->forceit)
10345 {
10346 coladvance((colnr_T)MAXCOL);
10347 curwin->w_curswant = MAXCOL;
10348 curwin->w_set_curswant = FALSE;
10349 }
10350
Bram Moolenaara40c5002005-01-09 21:16:21 +000010351 /* Ignore the command when already in Insert mode. Inserting an
10352 * expression register that invokes a function can do this. */
10353 if (State & INSERT)
10354 return;
10355
Bram Moolenaar64969662005-12-14 21:59:55 +000010356 if (eap->cmdidx == CMD_startinsert)
10357 restart_edit = 'a';
10358 else if (eap->cmdidx == CMD_startreplace)
10359 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010361 restart_edit = 'V';
10362
10363 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010365 if (eap->cmdidx == CMD_startinsert)
10366 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 curwin->w_curswant = 0; /* avoid MAXCOL */
10368 }
10369}
10370
10371/*
10372 * ":stopinsert"
10373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010375ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376{
10377 restart_edit = 0;
10378 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010379 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010382/*
10383 * Execute normal mode command "cmd".
10384 * "remap" can be REMAP_NONE or REMAP_YES.
10385 */
10386 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010387exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010388{
Bram Moolenaar25281632016-01-21 23:32:32 +010010389 /* Stuff the argument into the typeahead buffer. */
10390 ins_typebuf(cmd, remap, 0, TRUE, silent);
10391 exec_normal(FALSE);
10392}
Bram Moolenaar25281632016-01-21 23:32:32 +010010393
Bram Moolenaar25281632016-01-21 23:32:32 +010010394/*
10395 * Execute normal_cmd() until there is no typeahead left.
10396 */
10397 void
10398exec_normal(int was_typed)
10399{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010400 oparg_T oa;
10401
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010402 clear_oparg(&oa);
10403 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010404 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10405 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010406 {
10407 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010408 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010409 }
10410}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010411
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412#ifdef FEAT_FIND_ID
10413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010414ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415{
10416 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10417 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10418 (linenr_T)1, (linenr_T)MAXLNUM);
10419}
10420
10421#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10422/*
10423 * ":psearch"
10424 */
10425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010426ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427{
10428 g_do_tagpreview = p_pvh;
10429 ex_findpat(eap);
10430 g_do_tagpreview = 0;
10431}
10432#endif
10433
10434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010435ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436{
10437 int whole = TRUE;
10438 long n;
10439 char_u *p;
10440 int action;
10441
10442 switch (cmdnames[eap->cmdidx].cmd_name[2])
10443 {
10444 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10445 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10446 action = ACTION_GOTO;
10447 else
10448 action = ACTION_SHOW;
10449 break;
10450 case 'i': /* ":ilist" and ":dlist" */
10451 action = ACTION_SHOW_ALL;
10452 break;
10453 case 'u': /* ":ijump" and ":djump" */
10454 action = ACTION_GOTO;
10455 break;
10456 default: /* ":isplit" and ":dsplit" */
10457 action = ACTION_SPLIT;
10458 break;
10459 }
10460
10461 n = 1;
10462 if (vim_isdigit(*eap->arg)) /* get count */
10463 {
10464 n = getdigits(&eap->arg);
10465 eap->arg = skipwhite(eap->arg);
10466 }
10467 if (*eap->arg == '/') /* Match regexp, not just whole words */
10468 {
10469 whole = FALSE;
10470 ++eap->arg;
10471 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10472 if (*p)
10473 {
10474 *p++ = NUL;
10475 p = skipwhite(p);
10476
10477 /* Check for trailing illegal characters */
10478 if (!ends_excmd(*p))
10479 eap->errmsg = e_trailing;
10480 else
10481 eap->nextcmd = check_nextcmd(p);
10482 }
10483 }
10484 if (!eap->skip)
10485 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10486 whole, !eap->forceit,
10487 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10488 n, action, eap->line1, eap->line2);
10489}
10490#endif
10491
10492#ifdef FEAT_WINDOWS
10493
10494# ifdef FEAT_QUICKFIX
10495/*
10496 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10497 */
10498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010499ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010501 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10503}
10504
10505/*
10506 * ":pedit"
10507 */
10508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010509ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510{
10511 win_T *curwin_save = curwin;
10512
10513 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010514 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 keep_help_flag = curwin_save->w_buffer->b_help;
10516 do_exedit(eap, NULL);
10517 keep_help_flag = FALSE;
10518 if (curwin != curwin_save && win_valid(curwin_save))
10519 {
10520 /* Return cursor to where we were */
10521 validate_cursor();
10522 redraw_later(VALID);
10523 win_enter(curwin_save, TRUE);
10524 }
10525 g_do_tagpreview = 0;
10526}
10527# endif
10528
10529/*
10530 * ":stag", ":stselect" and ":stjump".
10531 */
10532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010533ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534{
10535 postponed_split = -1;
10536 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010537 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10539 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010540 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541}
10542#endif
10543
10544/*
10545 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10546 */
10547 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010548ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549{
10550 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10551}
10552
10553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010554ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555{
10556 int cmd;
10557
10558 switch (name[1])
10559 {
10560 case 'j': cmd = DT_JUMP; /* ":tjump" */
10561 break;
10562 case 's': cmd = DT_SELECT; /* ":tselect" */
10563 break;
10564 case 'p': cmd = DT_PREV; /* ":tprevious" */
10565 break;
10566 case 'N': cmd = DT_PREV; /* ":tNext" */
10567 break;
10568 case 'n': cmd = DT_NEXT; /* ":tnext" */
10569 break;
10570 case 'o': cmd = DT_POP; /* ":pop" */
10571 break;
10572 case 'f': /* ":tfirst" */
10573 case 'r': cmd = DT_FIRST; /* ":trewind" */
10574 break;
10575 case 'l': cmd = DT_LAST; /* ":tlast" */
10576 break;
10577 default: /* ":tag" */
10578#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010579 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010581 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 return;
10583 }
10584#endif
10585 cmd = DT_TAG;
10586 break;
10587 }
10588
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010589 if (name[0] == 'l')
10590 {
10591#ifndef FEAT_QUICKFIX
10592 ex_ni(eap);
10593 return;
10594#else
10595 cmd = DT_LTAG;
10596#endif
10597 }
10598
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10600 eap->forceit, TRUE);
10601}
10602
10603/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010604 * Check "str" for starting with a special cmdline variable.
10605 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10606 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10607 */
10608 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010609find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010610{
10611 int len;
10612 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010613 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010614 "%",
10615#define SPEC_PERC 0
10616 "#",
10617#define SPEC_HASH 1
10618 "<cword>", /* cursor word */
10619#define SPEC_CWORD 2
10620 "<cWORD>", /* cursor WORD */
10621#define SPEC_CCWORD 3
10622 "<cfile>", /* cursor path name */
10623#define SPEC_CFILE 4
10624 "<sfile>", /* ":so" file name */
10625#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010626 "<slnum>", /* ":so" file line number */
10627#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010628#ifdef FEAT_AUTOCMD
10629 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010630# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010631 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010632# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010633 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010634# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010635#endif
10636#ifdef FEAT_CLIENTSERVER
10637 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010638# ifdef FEAT_AUTOCMD
10639# define SPEC_CLIENT 10
10640# else
10641# define SPEC_CLIENT 7
10642# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010643#endif
10644 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010645
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010646 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010647 {
10648 len = (int)STRLEN(spec_str[i]);
10649 if (STRNCMP(src, spec_str[i], len) == 0)
10650 {
10651 *usedlen = len;
10652 return i;
10653 }
10654 }
10655 return -1;
10656}
10657
10658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 * Evaluate cmdline variables.
10660 *
10661 * change '%' to curbuf->b_ffname
10662 * '#' to curwin->w_altfile
10663 * '<cword>' to word under the cursor
10664 * '<cWORD>' to WORD under the cursor
10665 * '<cfile>' to path name under the cursor
10666 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010667 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 * '<afile>' to file name for autocommand
10669 * '<abuf>' to buffer number for autocommand
10670 * '<amatch>' to matching name for autocommand
10671 *
10672 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10673 * "" for error without a message) and NULL is returned.
10674 * Returns an allocated string if a valid match was found.
10675 * Returns NULL if no match was found. "usedlen" then still contains the
10676 * number of characters to skip.
10677 */
10678 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010679eval_vars(
10680 char_u *src, /* pointer into commandline */
10681 char_u *srcstart, /* beginning of valid memory for src */
10682 int *usedlen, /* characters after src that are used */
10683 linenr_T *lnump, /* line number for :e command, or NULL */
10684 char_u **errormsg, /* pointer to error message */
10685 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010686 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687{
10688 int i;
10689 char_u *s;
10690 char_u *result;
10691 char_u *resultbuf = NULL;
10692 int resultlen;
10693 buf_T *buf;
10694 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10695 int spec_idx;
10696#ifdef FEAT_MODIFY_FNAME
10697 int skip_mod = FALSE;
10698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700
10701 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010702 if (escaped != NULL)
10703 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704
10705 /*
10706 * Check if there is something to do.
10707 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010708 spec_idx = find_cmdline_var(src, usedlen);
10709 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 {
10711 *usedlen = 1;
10712 return NULL;
10713 }
10714
10715 /*
10716 * Skip when preceded with a backslash "\%" and "\#".
10717 * Note: In "\\%" the % is also not recognized!
10718 */
10719 if (src > srcstart && src[-1] == '\\')
10720 {
10721 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010722 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 return NULL;
10724 }
10725
10726 /*
10727 * word or WORD under cursor
10728 */
10729 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10730 {
10731 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10732 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10733 if (resultlen == 0)
10734 {
10735 *errormsg = (char_u *)"";
10736 return NULL;
10737 }
10738 }
10739
10740 /*
10741 * '#': Alternate file name
10742 * '%': Current file name
10743 * File name under the cursor
10744 * File name for autocommand
10745 * and following modifiers
10746 */
10747 else
10748 {
10749 switch (spec_idx)
10750 {
10751 case SPEC_PERC: /* '%': current file */
10752 if (curbuf->b_fname == NULL)
10753 {
10754 result = (char_u *)"";
10755 valid = 0; /* Must have ":p:h" to be valid */
10756 }
10757 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 break;
10760
10761 case SPEC_HASH: /* '#' or "#99": alternate file */
10762 if (src[1] == '#') /* "##": the argument list */
10763 {
10764 result = arg_all();
10765 resultbuf = result;
10766 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010767 if (escaped != NULL)
10768 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769#ifdef FEAT_MODIFY_FNAME
10770 skip_mod = TRUE;
10771#endif
10772 break;
10773 }
10774 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010775 if (*s == '<') /* "#<99" uses v:oldfiles */
10776 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 i = (int)getdigits(&s);
10778 *usedlen = (int)(s - src); /* length of what we expand */
10779
Bram Moolenaard812df62008-11-09 12:46:09 +000010780 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010782 if (*usedlen < 2)
10783 {
10784 /* Should we give an error message for #<text? */
10785 *usedlen = 1;
10786 return NULL;
10787 }
10788#ifdef FEAT_EVAL
10789 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10790 (long)i);
10791 if (result == NULL)
10792 {
10793 *errormsg = (char_u *)"";
10794 return NULL;
10795 }
10796#else
10797 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 }
10801 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010802 {
10803 buf = buflist_findnr(i);
10804 if (buf == NULL)
10805 {
10806 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10807 return NULL;
10808 }
10809 if (lnump != NULL)
10810 *lnump = ECMD_LAST;
10811 if (buf->b_fname == NULL)
10812 {
10813 result = (char_u *)"";
10814 valid = 0; /* Must have ":p:h" to be valid */
10815 }
10816 else
10817 result = buf->b_fname;
10818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 break;
10820
10821#ifdef FEAT_SEARCHPATH
10822 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010823 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 if (result == NULL)
10825 {
10826 *errormsg = (char_u *)"";
10827 return NULL;
10828 }
10829 resultbuf = result; /* remember allocated string */
10830 break;
10831#endif
10832
10833#ifdef FEAT_AUTOCMD
10834 case SPEC_AFILE: /* file name for autocommand */
10835 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010836 if (result != NULL && !autocmd_fname_full)
10837 {
10838 /* Still need to turn the fname into a full path. It is
10839 * postponed to avoid a delay when <afile> is not used. */
10840 autocmd_fname_full = TRUE;
10841 result = FullName_save(autocmd_fname, FALSE);
10842 vim_free(autocmd_fname);
10843 autocmd_fname = result;
10844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 if (result == NULL)
10846 {
10847 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10848 return NULL;
10849 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010850 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 break;
10852
10853 case SPEC_ABUF: /* buffer number for autocommand */
10854 if (autocmd_bufnr <= 0)
10855 {
10856 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10857 return NULL;
10858 }
10859 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10860 result = strbuf;
10861 break;
10862
10863 case SPEC_AMATCH: /* match name for autocommand */
10864 result = autocmd_match;
10865 if (result == NULL)
10866 {
10867 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10868 return NULL;
10869 }
10870 break;
10871
10872#endif
10873 case SPEC_SFILE: /* file name for ":so" command */
10874 result = sourcing_name;
10875 if (result == NULL)
10876 {
10877 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10878 return NULL;
10879 }
10880 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010881 case SPEC_SLNUM: /* line in file for ":so" command */
10882 if (sourcing_name == NULL || sourcing_lnum == 0)
10883 {
10884 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10885 return NULL;
10886 }
10887 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10888 result = strbuf;
10889 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890#if defined(FEAT_CLIENTSERVER)
10891 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010892 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10893 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 result = strbuf;
10895 break;
10896#endif
10897 }
10898
10899 resultlen = (int)STRLEN(result); /* length of new string */
10900 if (src[*usedlen] == '<') /* remove the file name extension */
10901 {
10902 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 resultlen = (int)(s - result);
10905 }
10906#ifdef FEAT_MODIFY_FNAME
10907 else if (!skip_mod)
10908 {
10909 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10910 &resultlen);
10911 if (result == NULL)
10912 {
10913 *errormsg = (char_u *)"";
10914 return NULL;
10915 }
10916 }
10917#endif
10918 }
10919
10920 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10921 {
10922 if (valid != VALID_HEAD + VALID_PATH)
10923 /* xgettext:no-c-format */
10924 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10925 else
10926 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10927 result = NULL;
10928 }
10929 else
10930 result = vim_strnsave(result, resultlen);
10931 vim_free(resultbuf);
10932 return result;
10933}
10934
10935/*
10936 * Concatenate all files in the argument list, separated by spaces, and return
10937 * it in one allocated string.
10938 * Spaces and backslashes in the file names are escaped with a backslash.
10939 * Returns NULL when out of memory.
10940 */
10941 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010942arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943{
10944 int len;
10945 int idx;
10946 char_u *retval = NULL;
10947 char_u *p;
10948
10949 /*
10950 * Do this loop two times:
10951 * first time: compute the total length
10952 * second time: concatenate the names
10953 */
10954 for (;;)
10955 {
10956 len = 0;
10957 for (idx = 0; idx < ARGCOUNT; ++idx)
10958 {
10959 p = alist_name(&ARGLIST[idx]);
10960 if (p != NULL)
10961 {
10962 if (len > 0)
10963 {
10964 /* insert a space in between names */
10965 if (retval != NULL)
10966 retval[len] = ' ';
10967 ++len;
10968 }
10969 for ( ; *p != NUL; ++p)
10970 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010971 if (*p == ' '
10972#ifndef BACKSLASH_IN_FILENAME
10973 || *p == '\\'
10974#endif
10975 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 {
10977 /* insert a backslash */
10978 if (retval != NULL)
10979 retval[len] = '\\';
10980 ++len;
10981 }
10982 if (retval != NULL)
10983 retval[len] = *p;
10984 ++len;
10985 }
10986 }
10987 }
10988
10989 /* second time: break here */
10990 if (retval != NULL)
10991 {
10992 retval[len] = NUL;
10993 break;
10994 }
10995
10996 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010997 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 if (retval == NULL)
10999 break;
11000 }
11001
11002 return retval;
11003}
11004
11005#if defined(FEAT_AUTOCMD) || defined(PROTO)
11006/*
11007 * Expand the <sfile> string in "arg".
11008 *
11009 * Returns an allocated string, or NULL for any error.
11010 */
11011 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011012expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013{
11014 char_u *errormsg;
11015 int len;
11016 char_u *result;
11017 char_u *newres;
11018 char_u *repl;
11019 int srclen;
11020 char_u *p;
11021
11022 result = vim_strsave(arg);
11023 if (result == NULL)
11024 return NULL;
11025
11026 for (p = result; *p; )
11027 {
11028 if (STRNCMP(p, "<sfile>", 7) != 0)
11029 ++p;
11030 else
11031 {
11032 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011033 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 if (errormsg != NULL)
11035 {
11036 if (*errormsg)
11037 emsg(errormsg);
11038 vim_free(result);
11039 return NULL;
11040 }
11041 if (repl == NULL) /* no match (cannot happen) */
11042 {
11043 p += srclen;
11044 continue;
11045 }
11046 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11047 newres = alloc(len);
11048 if (newres == NULL)
11049 {
11050 vim_free(repl);
11051 vim_free(result);
11052 return NULL;
11053 }
11054 mch_memmove(newres, result, (size_t)(p - result));
11055 STRCPY(newres + (p - result), repl);
11056 len = (int)STRLEN(newres);
11057 STRCAT(newres, p + srclen);
11058 vim_free(repl);
11059 vim_free(result);
11060 result = newres;
11061 p = newres + len; /* continue after the match */
11062 }
11063 }
11064
11065 return result;
11066}
11067#endif
11068
11069#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011070static int ses_winsizes(FILE *fd, int restore_size,
11071 win_T *tab_firstwin);
11072static int ses_win_rec(FILE *fd, frame_T *fr);
11073static frame_T *ses_skipframe(frame_T *fr);
11074static int ses_do_frame(frame_T *fr);
11075static int ses_do_win(win_T *wp);
11076static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11077static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
11078static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079
11080/*
11081 * Write openfile commands for the current buffers to an .exrc file.
11082 * Return FAIL on error, OK otherwise.
11083 */
11084 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011085makeopens(
11086 FILE *fd,
11087 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088{
11089 buf_T *buf;
11090 int only_save_windows = TRUE;
11091 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 int restore_size = TRUE;
11093 win_T *wp;
11094 char_u *sname;
11095 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011096 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011097 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011098 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011099 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011100 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011101 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102
11103 if (ssop_flags & SSOP_BUFFERS)
11104 only_save_windows = FALSE; /* Save ALL buffers */
11105
11106 /*
11107 * Begin by setting the this_session variable, and then other
11108 * sessionable variables.
11109 */
11110#ifdef FEAT_EVAL
11111 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11112 return FAIL;
11113 if (ssop_flags & SSOP_GLOBALS)
11114 if (store_session_globals(fd) == FAIL)
11115 return FAIL;
11116#endif
11117
11118 /*
11119 * Close all windows but one.
11120 */
11121 if (put_line(fd, "silent only") == FAIL)
11122 return FAIL;
11123
11124 /*
11125 * Now a :cd command to the session directory or the current directory
11126 */
11127 if (ssop_flags & SSOP_SESDIR)
11128 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011129 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11130 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 return FAIL;
11132 }
11133 else if (ssop_flags & SSOP_CURDIR)
11134 {
11135 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11136 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011137 || fputs("cd ", fd) < 0
11138 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11139 || put_eol(fd) == FAIL)
11140 {
11141 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 vim_free(sname);
11145 }
11146
11147 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011148 * If there is an empty, unnamed buffer we will wipe it out later.
11149 * Remember the buffer number.
11150 */
11151 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11152 return FAIL;
11153 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11154 return FAIL;
11155 if (put_line(fd, "endif") == FAIL)
11156 return FAIL;
11157
11158 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 * Now save the current files, current buffer first.
11160 */
11161 if (put_line(fd, "set shortmess=aoO") == FAIL)
11162 return FAIL;
11163
11164 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011165 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 {
11167 if (!(only_save_windows && buf->b_nwindows == 0)
11168 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11169 && buf->b_fname != NULL
11170 && buf->b_p_bl)
11171 {
11172 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11173 : buf->b_wininfo->wi_fpos.lnum) < 0
11174 || ses_fname(fd, buf, &ssop_flags) == FAIL)
11175 return FAIL;
11176 }
11177 }
11178
11179 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011180 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11182 return FAIL;
11183
11184 if (ssop_flags & SSOP_RESIZE)
11185 {
11186 /* Note: after the restore we still check it worked!*/
11187 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11188 || put_eol(fd) == FAIL)
11189 return FAIL;
11190 }
11191
11192#ifdef FEAT_GUI
11193 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11194 {
11195 int x, y;
11196
11197 if (gui_mch_get_winpos(&x, &y) == OK)
11198 {
11199 /* Note: after the restore we still check it worked!*/
11200 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11201 return FAIL;
11202 }
11203 }
11204#endif
11205
11206 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011207 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11208 * will be displayed when creating the next tab. That resizes the windows
11209 * in the first tab, which may cause problems. Set 'showtabline' to 2
11210 * temporarily to avoid that.
11211 */
11212 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11213 {
11214 if (put_line(fd, "set stal=2") == FAIL)
11215 return FAIL;
11216 restore_stal = TRUE;
11217 }
11218
11219 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011220 * May repeat putting Windows for each tab, when "tabpages" is in
11221 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011222 * Don't use goto_tabpage(), it may change directory and trigger
11223 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011225 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011226 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011227 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011229 int need_tabnew = FALSE;
11230 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011231
Bram Moolenaar18144c82006-04-12 21:52:12 +000011232 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011234 tabpage_T *tp = find_tabpage(tabnr);
11235
11236 if (tp == NULL)
11237 break; /* done all tab pages */
11238 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011239 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011240 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011241 tab_topframe = topframe;
11242 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011243 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011244 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011245 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011246 tab_topframe = tp->tp_topframe;
11247 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011248 if (tabnr > 1)
11249 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251
Bram Moolenaar18144c82006-04-12 21:52:12 +000011252 /*
11253 * Before creating the window layout, try loading one file. If this
11254 * is aborted we don't end up with a number of useless windows.
11255 * This may have side effects! (e.g., compressed or network file).
11256 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011257 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011258 {
11259 if (ses_do_win(wp)
11260 && wp->w_buffer->b_ffname != NULL
11261 && !wp->w_buffer->b_help
11262#ifdef FEAT_QUICKFIX
11263 && !bt_nofile(wp->w_buffer)
11264#endif
11265 )
11266 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011267 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011268 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11269 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011270 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011271 if (!wp->w_arg_idx_invalid)
11272 edited_win = wp;
11273 break;
11274 }
11275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011277 /* If no file got edited create an empty tab page. */
11278 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11279 return FAIL;
11280
Bram Moolenaar18144c82006-04-12 21:52:12 +000011281 /*
11282 * Save current window layout.
11283 */
11284 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011286 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011288 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11289 return FAIL;
11290 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11291 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292
Bram Moolenaar18144c82006-04-12 21:52:12 +000011293 /*
11294 * Check if window sizes can be restored (no windows omitted).
11295 * Remember the window number of the current window after restoring.
11296 */
11297 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011298 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011299 {
11300 if (ses_do_win(wp))
11301 ++nr;
11302 else
11303 restore_size = FALSE;
11304 if (curwin == wp)
11305 cnr = nr;
11306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307
Bram Moolenaar18144c82006-04-12 21:52:12 +000011308 /* Go to the first window. */
11309 if (put_line(fd, "wincmd t") == FAIL)
11310 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011311
Bram Moolenaar18144c82006-04-12 21:52:12 +000011312 /*
11313 * If more than one window, see if sizes can be restored.
11314 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11315 * resized when moving between windows.
11316 * Do this before restoring the view, so that the topline and the
11317 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011318 * winminheight and winminwidth need to be set to avoid an error if the
11319 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011320 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011321 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011322 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011323 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011324 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325
Bram Moolenaar18144c82006-04-12 21:52:12 +000011326 /*
11327 * Restore the view of the window (options, file, cursor, etc.).
11328 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011329 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011330 {
11331 if (!ses_do_win(wp))
11332 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011333 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11334 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011335 return FAIL;
11336 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11337 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011338 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011339 }
11340
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011341 /* The argument index in the first tab page is zero, need to set it in
11342 * each window. For further tab pages it's the window where we do
11343 * "tabedit". */
11344 cur_arg_idx = next_arg_idx;
11345
Bram Moolenaar18144c82006-04-12 21:52:12 +000011346 /*
11347 * Restore cursor to the current window if it's not the first one.
11348 */
11349 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11350 || put_eol(fd) == FAIL))
11351 return FAIL;
11352
11353 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011354 * Restore window sizes again after jumping around in windows, because
11355 * the current window has a minimum size while others may not.
11356 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011357 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011358 return FAIL;
11359
Bram Moolenaar18144c82006-04-12 21:52:12 +000011360 /* Don't continue in another tab page when doing only the current one
11361 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011362 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011363 break;
11364 }
11365
11366 if (ssop_flags & SSOP_TABPAGES)
11367 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011368 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11369 || put_eol(fd) == FAIL)
11370 return FAIL;
11371 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011372 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11373 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011374
Bram Moolenaar9c102382006-05-03 21:26:49 +000011375 /*
11376 * Wipe out an empty unnamed buffer we started in.
11377 */
11378 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11379 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011380 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011381 return FAIL;
11382 if (put_line(fd, "endif") == FAIL)
11383 return FAIL;
11384 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11385 return FAIL;
11386
11387 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11388 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11389 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11390 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011391 /* Re-apply 'winminheight' and 'winminwidth'. */
11392 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11393 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11394 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395
11396 /*
11397 * Lastly, execute the x.vim file if it exists.
11398 */
11399 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11400 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011401 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 || put_line(fd, "endif") == FAIL)
11403 return FAIL;
11404
11405 return OK;
11406}
11407
11408 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011409ses_winsizes(
11410 FILE *fd,
11411 int restore_size,
11412 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413{
11414 int n = 0;
11415 win_T *wp;
11416
11417 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11418 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011419 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 {
11421 if (!ses_do_win(wp))
11422 continue;
11423 ++n;
11424
11425 /* restore height when not full height */
11426 if (wp->w_height + wp->w_status_height < topframe->fr_height
11427 && (fprintf(fd,
11428 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11429 n, (long)wp->w_height, Rows / 2, Rows) < 0
11430 || put_eol(fd) == FAIL))
11431 return FAIL;
11432
11433 /* restore width when not full width */
11434 if (wp->w_width < Columns && (fprintf(fd,
11435 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11436 n, (long)wp->w_width, Columns / 2, Columns) < 0
11437 || put_eol(fd) == FAIL))
11438 return FAIL;
11439 }
11440 }
11441 else
11442 {
11443 /* Just equalise window sizes */
11444 if (put_line(fd, "wincmd =") == FAIL)
11445 return FAIL;
11446 }
11447 return OK;
11448}
11449
11450/*
11451 * Write commands to "fd" to recursively create windows for frame "fr",
11452 * horizontally and vertically split.
11453 * After the commands the last window in the frame is the current window.
11454 * Returns FAIL when writing the commands to "fd" fails.
11455 */
11456 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011457ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458{
11459 frame_T *frc;
11460 int count = 0;
11461
11462 if (fr->fr_layout != FR_LEAF)
11463 {
11464 /* Find first frame that's not skipped and then create a window for
11465 * each following one (first frame is already there). */
11466 frc = ses_skipframe(fr->fr_child);
11467 if (frc != NULL)
11468 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11469 {
11470 /* Make window as big as possible so that we have lots of room
11471 * to split. */
11472 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11473 || put_line(fd, fr->fr_layout == FR_COL
11474 ? "split" : "vsplit") == FAIL)
11475 return FAIL;
11476 ++count;
11477 }
11478
11479 /* Go back to the first window. */
11480 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11481 ? "%dwincmd k" : "%dwincmd h", count) < 0
11482 || put_eol(fd) == FAIL))
11483 return FAIL;
11484
11485 /* Recursively create frames/windows in each window of this column or
11486 * row. */
11487 frc = ses_skipframe(fr->fr_child);
11488 while (frc != NULL)
11489 {
11490 ses_win_rec(fd, frc);
11491 frc = ses_skipframe(frc->fr_next);
11492 /* Go to next window. */
11493 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11494 return FAIL;
11495 }
11496 }
11497 return OK;
11498}
11499
11500/*
11501 * Skip frames that don't contain windows we want to save in the Session.
11502 * Returns NULL when there none.
11503 */
11504 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011505ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506{
11507 frame_T *frc;
11508
11509 for (frc = fr; frc != NULL; frc = frc->fr_next)
11510 if (ses_do_frame(frc))
11511 break;
11512 return frc;
11513}
11514
11515/*
11516 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11517 * the Session.
11518 */
11519 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011520ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521{
11522 frame_T *frc;
11523
11524 if (fr->fr_layout == FR_LEAF)
11525 return ses_do_win(fr->fr_win);
11526 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11527 if (ses_do_frame(frc))
11528 return TRUE;
11529 return FALSE;
11530}
11531
11532/*
11533 * Return non-zero if window "wp" is to be stored in the Session.
11534 */
11535 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011536ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537{
11538 if (wp->w_buffer->b_fname == NULL
11539#ifdef FEAT_QUICKFIX
11540 /* When 'buftype' is "nofile" can't restore the window contents. */
11541 || bt_nofile(wp->w_buffer)
11542#endif
11543 )
11544 return (ssop_flags & SSOP_BLANK);
11545 if (wp->w_buffer->b_help)
11546 return (ssop_flags & SSOP_HELP);
11547 return TRUE;
11548}
11549
11550/*
11551 * Write commands to "fd" to restore the view of a window.
11552 * Caller must make sure 'scrolloff' is zero.
11553 */
11554 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011555put_view(
11556 FILE *fd,
11557 win_T *wp,
11558 int add_edit, /* add ":edit" command to view */
11559 unsigned *flagp, /* vop_flags or ssop_flags */
11560 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011561 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562{
11563 win_T *save_curwin;
11564 int f;
11565 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011566 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567
11568 /* Always restore cursor position for ":mksession". For ":mkview" only
11569 * when 'viewoptions' contains "cursor". */
11570 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11571
11572 /*
11573 * Local argument list.
11574 */
11575 if (wp->w_alist == &global_alist)
11576 {
11577 if (put_line(fd, "argglobal") == FAIL)
11578 return FAIL;
11579 }
11580 else
11581 {
11582 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11583 flagp == &vop_flags
11584 || !(*flagp & SSOP_CURDIR)
11585 || wp->w_localdir != NULL, flagp) == FAIL)
11586 return FAIL;
11587 }
11588
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011589 /* Only when part of a session: restore the argument index. Some
11590 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011591 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011592 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011594 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595 || put_eol(fd) == FAIL)
11596 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011597 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598 }
11599
11600 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011601 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602 {
11603 /*
11604 * Load the file.
11605 */
11606 if (wp->w_buffer->b_ffname != NULL
11607#ifdef FEAT_QUICKFIX
11608 && !bt_nofile(wp->w_buffer)
11609#endif
11610 )
11611 {
11612 /*
11613 * Editing a file in this buffer: use ":edit file".
11614 * This may have side effects! (e.g., compressed or network file).
11615 */
11616 if (fputs("edit ", fd) < 0
11617 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11618 return FAIL;
11619 }
11620 else
11621 {
11622 /* No file in this buffer, just make it empty. */
11623 if (put_line(fd, "enew") == FAIL)
11624 return FAIL;
11625#ifdef FEAT_QUICKFIX
11626 if (wp->w_buffer->b_ffname != NULL)
11627 {
11628 /* The buffer does have a name, but it's not a file name. */
11629 if (fputs("file ", fd) < 0
11630 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11631 return FAIL;
11632 }
11633#endif
11634 do_cursor = FALSE;
11635 }
11636 }
11637
11638 /*
11639 * Local mappings and abbreviations.
11640 */
11641 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11642 && makemap(fd, wp->w_buffer) == FAIL)
11643 return FAIL;
11644
11645 /*
11646 * Local options. Need to go to the window temporarily.
11647 * Store only local values when using ":mkview" and when ":mksession" is
11648 * used and 'sessionoptions' doesn't include "options".
11649 * Some folding options are always stored when "folds" is included,
11650 * otherwise the folds would not be restored correctly.
11651 */
11652 save_curwin = curwin;
11653 curwin = wp;
11654 curbuf = curwin->w_buffer;
11655 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11656 f = makeset(fd, OPT_LOCAL,
11657 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11658#ifdef FEAT_FOLDING
11659 else if (*flagp & SSOP_FOLDS)
11660 f = makefoldset(fd);
11661#endif
11662 else
11663 f = OK;
11664 curwin = save_curwin;
11665 curbuf = curwin->w_buffer;
11666 if (f == FAIL)
11667 return FAIL;
11668
11669#ifdef FEAT_FOLDING
11670 /*
11671 * Save Folds when 'buftype' is empty and for help files.
11672 */
11673 if ((*flagp & SSOP_FOLDS)
11674 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011675# ifdef FEAT_QUICKFIX
11676 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11677# endif
11678 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 {
11680 if (put_folds(fd, wp) == FAIL)
11681 return FAIL;
11682 }
11683#endif
11684
11685 /*
11686 * Set the cursor after creating folds, since that moves the cursor.
11687 */
11688 if (do_cursor)
11689 {
11690
11691 /* Restore the cursor line in the file and relatively in the
11692 * window. Don't use "G", it changes the jumplist. */
11693 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11694 (long)wp->w_cursor.lnum,
11695 (long)(wp->w_cursor.lnum - wp->w_topline),
11696 (long)wp->w_height / 2, (long)wp->w_height) < 0
11697 || put_eol(fd) == FAIL
11698 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11699 || put_line(fd, "exe s:l") == FAIL
11700 || put_line(fd, "normal! zt") == FAIL
11701 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11702 || put_eol(fd) == FAIL)
11703 return FAIL;
11704 /* Restore the cursor column and left offset when not wrapping. */
11705 if (wp->w_cursor.col == 0)
11706 {
11707 if (put_line(fd, "normal! 0") == FAIL)
11708 return FAIL;
11709 }
11710 else
11711 {
11712 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11713 {
11714 if (fprintf(fd,
11715 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011716 (long)wp->w_virtcol + 1,
11717 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 (long)wp->w_width / 2, (long)wp->w_width) < 0
11719 || put_eol(fd) == FAIL
11720 || put_line(fd, "if s:c > 0") == FAIL
11721 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011722 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11723 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724 || put_eol(fd) == FAIL
11725 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011726 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 || put_eol(fd) == FAIL
11728 || put_line(fd, "endif") == FAIL)
11729 return FAIL;
11730 }
11731 else
11732 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011733 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734 || put_eol(fd) == FAIL)
11735 return FAIL;
11736 }
11737 }
11738 }
11739
11740 /*
11741 * Local directory.
11742 */
11743 if (wp->w_localdir != NULL)
11744 {
11745 if (fputs("lcd ", fd) < 0
11746 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11747 || put_eol(fd) == FAIL)
11748 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011749 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750 }
11751
11752 return OK;
11753}
11754
11755/*
11756 * Write an argument list to the session file.
11757 * Returns FAIL if writing fails.
11758 */
11759 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011760ses_arglist(
11761 FILE *fd,
11762 char *cmd,
11763 garray_T *gap,
11764 int fullname, /* TRUE: use full path name */
11765 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766{
11767 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011768 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 char_u *s;
11770
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011771 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11772 return FAIL;
11773 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 return FAIL;
11775 for (i = 0; i < gap->ga_len; ++i)
11776 {
11777 /* NULL file names are skipped (only happens when out of memory). */
11778 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11779 if (s != NULL)
11780 {
11781 if (fullname)
11782 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011783 buf = alloc(MAXPATHL);
11784 if (buf != NULL)
11785 {
11786 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11787 s = buf;
11788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011790 if (fputs("argadd ", fd) < 0
11791 || ses_put_fname(fd, s, flagp) == FAIL
11792 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011793 {
11794 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011796 }
11797 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798 }
11799 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011800 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801}
11802
11803/*
11804 * Write a buffer name to the session file.
11805 * Also ends the line.
11806 * Returns FAIL if writing fails.
11807 */
11808 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011809ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810{
11811 char_u *name;
11812
11813 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011814 * the session file will be sourced.
11815 * Don't do this for ":mkview", we don't know the current directory.
11816 * Don't do this after ":lcd", we don't keep track of what the current
11817 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818 if (buf->b_sfname != NULL
11819 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011820 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011821#ifdef FEAT_AUTOCHDIR
11822 && !p_acd
11823#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011824 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 name = buf->b_sfname;
11826 else
11827 name = buf->b_ffname;
11828 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11829 return FAIL;
11830 return OK;
11831}
11832
11833/*
11834 * Write a file name to the session file.
11835 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11836 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011837 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838 */
11839 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011840ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841{
11842 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011843 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845
11846 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011847 if (sname == NULL)
11848 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011850 if (*flagp & SSOP_SLASH)
11851 {
11852 /* change all backslashes to forward slashes */
11853 for (p = sname; *p != NUL; mb_ptr_adv(p))
11854 if (*p == '\\')
11855 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011857
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011858 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011859 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011861 if (p == NULL)
11862 return FAIL;
11863
11864 /* write the result */
11865 if (fputs((char *)p, fd) < 0)
11866 retval = FAIL;
11867
11868 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869 return retval;
11870}
11871
11872/*
11873 * ":loadview [nr]"
11874 */
11875 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011876ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877{
11878 char_u *fname;
11879
11880 fname = get_view_file(*eap->arg);
11881 if (fname != NULL)
11882 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011883 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884 vim_free(fname);
11885 }
11886}
11887
11888/*
11889 * Get the name of the view file for the current buffer.
11890 */
11891 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011892get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893{
11894 int len = 0;
11895 char_u *p, *s;
11896 char_u *retval;
11897 char_u *sname;
11898
11899 if (curbuf->b_ffname == NULL)
11900 {
11901 EMSG(_(e_noname));
11902 return NULL;
11903 }
11904 sname = home_replace_save(NULL, curbuf->b_ffname);
11905 if (sname == NULL)
11906 return NULL;
11907
11908 /*
11909 * We want a file name without separators, because we're not going to make
11910 * a directory.
11911 * "normal" path separator -> "=+"
11912 * "=" -> "=="
11913 * ":" path separator -> "=-"
11914 */
11915 for (p = sname; *p; ++p)
11916 if (*p == '=' || vim_ispathsep(*p))
11917 ++len;
11918 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11919 if (retval != NULL)
11920 {
11921 STRCPY(retval, p_vdir);
11922 add_pathsep(retval);
11923 s = retval + STRLEN(retval);
11924 for (p = sname; *p; ++p)
11925 {
11926 if (*p == '=')
11927 {
11928 *s++ = '=';
11929 *s++ = '=';
11930 }
11931 else if (vim_ispathsep(*p))
11932 {
11933 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011934#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935 if (*p == ':')
11936 *s++ = '-';
11937 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011939 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940 }
11941 else
11942 *s++ = *p;
11943 }
11944 *s++ = '=';
11945 *s++ = c;
11946 STRCPY(s, ".vim");
11947 }
11948
11949 vim_free(sname);
11950 return retval;
11951}
11952
11953#endif /* FEAT_SESSION */
11954
11955/*
11956 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11957 * Return FAIL for a write error.
11958 */
11959 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011960put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961{
11962 if (
11963#ifdef USE_CRNL
11964 (
11965# ifdef MKSESSION_NL
11966 !mksession_nl &&
11967# endif
11968 (putc('\r', fd) < 0)) ||
11969#endif
11970 (putc('\n', fd) < 0))
11971 return FAIL;
11972 return OK;
11973}
11974
11975/*
11976 * Write a line to "fd".
11977 * Return FAIL for a write error.
11978 */
11979 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011980put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981{
11982 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11983 return FAIL;
11984 return OK;
11985}
11986
11987#ifdef FEAT_VIMINFO
11988/*
11989 * ":rviminfo" and ":wviminfo".
11990 */
11991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011992ex_viminfo(
11993 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994{
11995 char_u *save_viminfo;
11996
11997 save_viminfo = p_viminfo;
11998 if (*p_viminfo == NUL)
11999 p_viminfo = (char_u *)"'100";
12000 if (eap->cmdidx == CMD_rviminfo)
12001 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012002 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12003 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 EMSG(_("E195: Cannot open viminfo file for reading"));
12005 }
12006 else
12007 write_viminfo(eap->arg, eap->forceit);
12008 p_viminfo = save_viminfo;
12009}
12010#endif
12011
12012#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012013/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012014 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012015 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012018dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 if (fname == NULL)
12021 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012022 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023}
12024#endif
12025
12026/*
12027 * ":behave {mswin,xterm}"
12028 */
12029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012030ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031{
12032 if (STRCMP(eap->arg, "mswin") == 0)
12033 {
12034 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12035 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12036 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12037 set_option_value((char_u *)"keymodel", 0L,
12038 (char_u *)"startsel,stopsel", 0);
12039 }
12040 else if (STRCMP(eap->arg, "xterm") == 0)
12041 {
12042 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12043 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12044 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12045 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12046 }
12047 else
12048 EMSG2(_(e_invarg2), eap->arg);
12049}
12050
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012051#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12052/*
12053 * Function given to ExpandGeneric() to obtain the possible arguments of the
12054 * ":behave {mswin,xterm}" command.
12055 */
12056 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012057get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012058{
12059 if (idx == 0)
12060 return (char_u *)"mswin";
12061 if (idx == 1)
12062 return (char_u *)"xterm";
12063 return NULL;
12064}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012065
12066/*
12067 * Function given to ExpandGeneric() to obtain the possible arguments of the
12068 * ":messages {clear}" command.
12069 */
12070 char_u *
12071get_messages_arg(expand_T *xp UNUSED, int idx)
12072{
12073 if (idx == 0)
12074 return (char_u *)"clear";
12075 return NULL;
12076}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012077#endif
12078
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079#ifdef FEAT_AUTOCMD
12080static int filetype_detect = FALSE;
12081static int filetype_plugin = FALSE;
12082static int filetype_indent = FALSE;
12083
12084/*
12085 * ":filetype [plugin] [indent] {on,off,detect}"
12086 * on: Load the filetype.vim file to install autocommands for file types.
12087 * off: Load the ftoff.vim file to remove all autocommands for file types.
12088 * plugin on: load filetype.vim and ftplugin.vim
12089 * plugin off: load ftplugof.vim
12090 * indent on: load filetype.vim and indent.vim
12091 * indent off: load indoff.vim
12092 */
12093 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012094ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095{
12096 char_u *arg = eap->arg;
12097 int plugin = FALSE;
12098 int indent = FALSE;
12099
12100 if (*eap->arg == NUL)
12101 {
12102 /* Print current status. */
12103 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12104 filetype_detect ? "ON" : "OFF",
12105 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12106 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12107 return;
12108 }
12109
12110 /* Accept "plugin" and "indent" in any order. */
12111 for (;;)
12112 {
12113 if (STRNCMP(arg, "plugin", 6) == 0)
12114 {
12115 plugin = TRUE;
12116 arg = skipwhite(arg + 6);
12117 continue;
12118 }
12119 if (STRNCMP(arg, "indent", 6) == 0)
12120 {
12121 indent = TRUE;
12122 arg = skipwhite(arg + 6);
12123 continue;
12124 }
12125 break;
12126 }
12127 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12128 {
12129 if (*arg == 'o' || !filetype_detect)
12130 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012131 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 filetype_detect = TRUE;
12133 if (plugin)
12134 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012135 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136 filetype_plugin = TRUE;
12137 }
12138 if (indent)
12139 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012140 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141 filetype_indent = TRUE;
12142 }
12143 }
12144 if (*arg == 'd')
12145 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012146 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012147 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148 }
12149 }
12150 else if (STRCMP(arg, "off") == 0)
12151 {
12152 if (plugin || indent)
12153 {
12154 if (plugin)
12155 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012156 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 filetype_plugin = FALSE;
12158 }
12159 if (indent)
12160 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012161 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162 filetype_indent = FALSE;
12163 }
12164 }
12165 else
12166 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012167 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 filetype_detect = FALSE;
12169 }
12170 }
12171 else
12172 EMSG2(_(e_invarg2), arg);
12173}
12174
12175/*
12176 * ":setfiletype {name}"
12177 */
12178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012179ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180{
12181 if (!did_filetype)
12182 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
12183}
12184#endif
12185
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012187ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188{
12189#ifdef FEAT_DIGRAPHS
12190 if (*eap->arg != NUL)
12191 putdigraph(eap->arg);
12192 else
12193 listdigraphs();
12194#else
12195 EMSG(_("E196: No digraphs in this version"));
12196#endif
12197}
12198
12199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012200ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201{
12202 int flags = 0;
12203
12204 if (eap->cmdidx == CMD_setlocal)
12205 flags = OPT_LOCAL;
12206 else if (eap->cmdidx == CMD_setglobal)
12207 flags = OPT_GLOBAL;
12208#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12209 if (cmdmod.browse && flags == 0)
12210 ex_options(eap);
12211 else
12212#endif
12213 (void)do_set(eap->arg, flags);
12214}
12215
12216#ifdef FEAT_SEARCH_EXTRA
12217/*
12218 * ":nohlsearch"
12219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012221ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012223 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012224 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225}
12226
12227/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012228 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 * Sets nextcmd to the start of the next command, if any. Also called when
12230 * skipping commands to find the next command.
12231 */
12232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012233ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234{
12235 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012236 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237 char_u *end;
12238 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012239 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012240
12241 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012242 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012243 else
12244 {
12245 EMSG(e_invcmd);
12246 return;
12247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248
12249 /* First clear any old pattern. */
12250 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012251 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252
12253 if (ends_excmd(*eap->arg))
12254 end = eap->arg;
12255 else if ((STRNICMP(eap->arg, "none", 4) == 0
12256 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12257 end = eap->arg + 4;
12258 else
12259 {
12260 p = skiptowhite(eap->arg);
12261 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012262 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263 p = skipwhite(p);
12264 if (*p == NUL)
12265 {
12266 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012267 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268 EMSG2(_(e_invarg2), eap->arg);
12269 return;
12270 }
12271 end = skip_regexp(p + 1, *p, TRUE, NULL);
12272 if (!eap->skip)
12273 {
12274 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12275 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012276 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277 eap->errmsg = e_trailing;
12278 return;
12279 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012280 if (*end != *p)
12281 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012282 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012283 EMSG2(_(e_invarg2), p);
12284 return;
12285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286
12287 c = *end;
12288 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012289 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012290 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012291 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 }
12293 }
12294 eap->nextcmd = find_nextcmd(end);
12295}
12296#endif
12297
12298#ifdef FEAT_CRYPT
12299/*
12300 * ":X": Get crypt key
12301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012303ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012305 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012306 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307}
12308#endif
12309
12310#ifdef FEAT_FOLDING
12311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012312ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313{
12314 if (foldManualAllowed(TRUE))
12315 foldCreate(eap->line1, eap->line2);
12316}
12317
12318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012319ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320{
12321 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12322 eap->forceit, FALSE);
12323}
12324
12325 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012326ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327{
12328 linenr_T lnum;
12329
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012330#ifdef FEAT_CLIPBOARD
12331 start_global_changes();
12332#endif
12333
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334 /* First set the marks for all lines closed/open. */
12335 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12336 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12337 ml_setmarked(lnum);
12338
12339 /* Execute the command on the marked lines. */
12340 global_exe(eap->arg);
12341 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012342#ifdef FEAT_CLIPBOARD
12343 end_global_changes();
12344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345}
12346#endif