blob: b923980e354dfeae0a4394d859b6dbf373e54f4e [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
Bram Moolenaar4033c552017-09-16 20:54:51 +0200111#if !defined(FEAT_LISTCMDS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112# 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
Bram Moolenaar4033c552017-09-16 20:54:51 +0200129#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130# 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 Moolenaarf28dbce2016-01-29 22:03:47 +0100162static void ex_close(exarg_T *eap);
163static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
164static void ex_only(exarg_T *eap);
165static void ex_resize(exarg_T *eap);
166static void ex_stag(exarg_T *eap);
167static void ex_tabclose(exarg_T *eap);
168static void ex_tabonly(exarg_T *eap);
169static void ex_tabnext(exarg_T *eap);
170static void ex_tabmove(exarg_T *eap);
171static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200172#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100173static void ex_pclose(exarg_T *eap);
174static void ex_ptag(exarg_T *eap);
175static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#else
177# define ex_pclose ex_ni
178# define ex_ptag ex_ni
179# define ex_pedit ex_ni
180#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100181static void ex_hide(exarg_T *eap);
182static void ex_stop(exarg_T *eap);
183static void ex_exit(exarg_T *eap);
184static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100186static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#else
188# define ex_goto ex_ni
189#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static void ex_shell(exarg_T *eap);
191static void ex_preserve(exarg_T *eap);
192static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193#ifndef FEAT_LISTCMDS
194# define ex_argedit ex_ni
195# define ex_argadd ex_ni
196# define ex_argdelete ex_ni
197# define ex_listdo ex_ni
198#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100199static void ex_mode(exarg_T *eap);
200static void ex_wrongmodifier(exarg_T *eap);
201static void ex_find(exarg_T *eap);
202static void ex_open(exarg_T *eap);
203static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#ifndef FEAT_GUI
205# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100206static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#endif
208#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210#else
211# define ex_tearoff ex_ni
212#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000213#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100214static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215#else
216# define ex_popup ex_ni
217#endif
218#ifndef FEAT_GUI_MSWIN
219# define ex_simalt ex_ni
220#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000221#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222# define gui_mch_find_dialog ex_ni
223# define gui_mch_replace_dialog ex_ni
224#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000225#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226# define ex_helpfind ex_ni
227#endif
228#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100229# define ex_cscope ex_ni
230# define ex_scscope ex_ni
231# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#endif
233#ifndef FEAT_SYN_HL
234# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200235# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000236#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200237#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200238# define ex_syntime ex_ni
239#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000240#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000241# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000242# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000243# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000244# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000245# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000246#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200247#ifndef FEAT_PERSISTENT_UNDO
248# define ex_rundo ex_ni
249# define ex_wundo ex_ni
250#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200251#ifndef FEAT_LUA
252# define ex_lua ex_script_ni
253# define ex_luado ex_ni
254# define ex_luafile ex_ni
255#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000256#ifndef FEAT_MZSCHEME
257# define ex_mzscheme ex_script_ni
258# define ex_mzfile ex_ni
259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#ifndef FEAT_PERL
261# define ex_perl ex_script_ni
262# define ex_perldo ex_ni
263#endif
264#ifndef FEAT_PYTHON
265# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200266# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267# define ex_pyfile ex_ni
268#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200269#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200270# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200271# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200272# define ex_py3file ex_ni
273#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100274#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
275# define ex_pyx ex_script_ni
276# define ex_pyxdo ex_ni
277# define ex_pyxfile ex_ni
278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279#ifndef FEAT_TCL
280# define ex_tcl ex_script_ni
281# define ex_tcldo ex_ni
282# define ex_tclfile ex_ni
283#endif
284#ifndef FEAT_RUBY
285# define ex_ruby ex_script_ni
286# define ex_rubydo ex_ni
287# define ex_rubyfile ex_ni
288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289#ifndef FEAT_KEYMAP
290# define ex_loadkeymap ex_ni
291#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100292static void ex_swapname(exarg_T *eap);
293static void ex_syncbind(exarg_T *eap);
294static void ex_read(exarg_T *eap);
295static void ex_pwd(exarg_T *eap);
296static void ex_equal(exarg_T *eap);
297static void ex_sleep(exarg_T *eap);
298static void do_exmap(exarg_T *eap, int isabbrev);
299static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100300static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000301#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100302static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303#else
304# define ex_winpos ex_ni
305#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100306static void ex_operators(exarg_T *eap);
307static void ex_put(exarg_T *eap);
308static void ex_copymove(exarg_T *eap);
309static void ex_submagic(exarg_T *eap);
310static void ex_join(exarg_T *eap);
311static void ex_at(exarg_T *eap);
312static void ex_bang(exarg_T *eap);
313static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200314#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100315static void ex_wundo(exarg_T *eap);
316static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200317#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_redo(exarg_T *eap);
319static void ex_later(exarg_T *eap);
320static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100321static void ex_redrawstatus(exarg_T *eap);
322static void close_redir(void);
323static void ex_mkrc(exarg_T *eap);
324static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100326static char_u *uc_fun_cmd(void);
327static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_startinsert(exarg_T *eap);
330static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100332static void ex_checkpath(exarg_T *eap);
333static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#else
335# define ex_findpat ex_ni
336# define ex_checkpath ex_ni
337#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200338#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100339static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340#else
341# define ex_psearch ex_ni
342#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100343static void ex_tag(exarg_T *eap);
344static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#ifndef FEAT_EVAL
346# define ex_scriptnames ex_ni
347# define ex_finish ex_ni
348# define ex_echo ex_ni
349# define ex_echohl ex_ni
350# define ex_execute ex_ni
351# define ex_call ex_ni
352# define ex_if ex_ni
353# define ex_endif ex_ni
354# define ex_else ex_ni
355# define ex_while ex_ni
356# define ex_continue ex_ni
357# define ex_break ex_ni
358# define ex_endwhile ex_ni
359# define ex_throw ex_ni
360# define ex_try ex_ni
361# define ex_catch ex_ni
362# define ex_finally ex_ni
363# define ex_endtry ex_ni
364# define ex_endfunction ex_ni
365# define ex_let ex_ni
366# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000367# define ex_lockvar ex_ni
368# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369# define ex_function ex_ni
370# define ex_delfunction ex_ni
371# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000372# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100374static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100376static int makeopens(FILE *fd, char_u *dirnow);
377static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
378static void ex_loadview(exarg_T *eap);
379static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000380static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381#else
382# define ex_loadview ex_ni
383#endif
384#ifndef FEAT_EVAL
385# define ex_compiler ex_ni
386#endif
387#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100388static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#else
390# define ex_viminfo ex_ni
391#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100392static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100394static void ex_filetype(exarg_T *eap);
395static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#else
397# define ex_filetype ex_ni
398# define ex_setfiletype ex_ni
399#endif
400#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000401# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402# define ex_diffpatch ex_ni
403# define ex_diffgetput ex_ni
404# define ex_diffsplit ex_ni
405# define ex_diffthis ex_ni
406# define ex_diffupdate ex_ni
407#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100408static void ex_digraphs(exarg_T *eap);
409static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
411# define ex_options ex_ni
412#endif
413#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100414static void ex_nohlsearch(exarg_T *eap);
415static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416#else
417# define ex_nohlsearch ex_ni
418# define ex_match ex_ni
419#endif
420#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100421static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422#else
423# define ex_X ex_ni
424#endif
425#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100426static void ex_fold(exarg_T *eap);
427static void ex_foldopen(exarg_T *eap);
428static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429#else
430# define ex_fold ex_ni
431# define ex_foldopen ex_ni
432# define ex_folddo ex_ni
433#endif
434#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
435 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
436# define ex_language ex_ni
437#endif
438#ifndef FEAT_SIGNS
439# define ex_sign ex_ni
440#endif
441#ifndef FEAT_SUN_WORKSHOP
442# define ex_wsverb ex_ni
443#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000444#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200445# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000446# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200447# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449
450#ifndef FEAT_EVAL
451# define ex_debug ex_ni
452# define ex_breakadd ex_ni
453# define ex_debuggreedy ex_ni
454# define ex_breakdel ex_ni
455# define ex_breaklist ex_ni
456#endif
457
458#ifndef FEAT_CMDHIST
459# define ex_history ex_ni
460#endif
461#ifndef FEAT_JUMPLIST
462# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200463# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464# define ex_changes ex_ni
465#endif
466
Bram Moolenaar05159a02005-02-26 23:04:13 +0000467#ifndef FEAT_PROFILE
468# define ex_profile ex_ni
469#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200470#ifndef FEAT_TERMINAL
471# define ex_terminal ex_ni
472#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000473
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474/*
475 * Declare cmdnames[].
476 */
477#define DO_DECLARE_EXCMD
478#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200479#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100480
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481static char_u dollar_command[2] = {'$', 0};
482
483
484#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000485/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486typedef struct
487{
488 char_u *line; /* command line */
489 linenr_T lnum; /* sourcing_lnum of the line */
490} wcmd_T;
491
492/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000493 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000495 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000497struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499 garray_T *lines_gap; /* growarray with line info */
500 int current_line; /* last read line from growarray */
501 int repeating; /* TRUE when looping a second time */
502 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100503 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 void *cookie;
505};
506
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100507static char_u *get_loop_line(int c, void *cookie, int indent);
508static int store_loop_line(garray_T *gap, char_u *line);
509static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000510
511/* Struct to save a few things while debugging. Used in do_cmdline() only. */
512struct dbg_stuff
513{
514 int trylevel;
515 int force_abort;
516 except_T *caught_stack;
517 char_u *vv_exception;
518 char_u *vv_throwpoint;
519 int did_emsg;
520 int got_int;
521 int did_throw;
522 int need_rethrow;
523 int check_cstack;
524 except_T *current_exception;
525};
526
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100527static void save_dbg_stuff(struct dbg_stuff *dsp);
528static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000529
530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100531save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000532{
533 dsp->trylevel = trylevel; trylevel = 0;
534 dsp->force_abort = force_abort; force_abort = FALSE;
535 dsp->caught_stack = caught_stack; caught_stack = NULL;
536 dsp->vv_exception = v_exception(NULL);
537 dsp->vv_throwpoint = v_throwpoint(NULL);
538
539 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
540 dsp->did_emsg = did_emsg; did_emsg = FALSE;
541 dsp->got_int = got_int; got_int = FALSE;
542 dsp->did_throw = did_throw; did_throw = FALSE;
543 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
544 dsp->check_cstack = check_cstack; check_cstack = FALSE;
545 dsp->current_exception = current_exception; current_exception = NULL;
546}
547
548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100549restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000550{
551 suppress_errthrow = FALSE;
552 trylevel = dsp->trylevel;
553 force_abort = dsp->force_abort;
554 caught_stack = dsp->caught_stack;
555 (void)v_exception(dsp->vv_exception);
556 (void)v_throwpoint(dsp->vv_throwpoint);
557 did_emsg = dsp->did_emsg;
558 got_int = dsp->got_int;
559 did_throw = dsp->did_throw;
560 need_rethrow = dsp->need_rethrow;
561 check_cstack = dsp->check_cstack;
562 current_exception = dsp->current_exception;
563}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564#endif
565
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566/*
567 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
568 * command is given.
569 */
570 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100571do_exmode(
572 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573{
574 int save_msg_scroll;
575 int prev_msg_row;
576 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100577 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000578
579 if (improved)
580 exmode_active = EXMODE_VIM;
581 else
582 exmode_active = EXMODE_NORMAL;
583 State = NORMAL;
584
585 /* When using ":global /pat/ visual" and then "Q" we return to continue
586 * the :global command. */
587 if (global_busy)
588 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
590 save_msg_scroll = msg_scroll;
591 ++RedrawingDisabled; /* don't redisplay the window */
592 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593#ifdef FEAT_GUI
594 /* Ignore scrollbar and mouse events in Ex mode */
595 ++hold_gui_events;
596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
598 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
599 while (exmode_active)
600 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000601 /* Check for a ":normal" command and no more characters left. */
602 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
603 {
604 exmode_active = FALSE;
605 break;
606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 msg_scroll = TRUE;
608 need_wait_return = FALSE;
609 ex_pressedreturn = FALSE;
610 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100611 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 prev_msg_row = msg_row;
613 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 if (improved)
615 {
616 cmdline_row = msg_row;
617 do_cmdline(NULL, getexline, NULL, 0);
618 }
619 else
620 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
621 lines_left = Rows - 1;
622
Bram Moolenaardf177f62005-02-22 08:39:57 +0000623 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100624 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000626 if (curbuf->b_ml.ml_flags & ML_EMPTY)
627 EMSG(_(e_emptybuf));
628 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000630 if (ex_pressedreturn)
631 {
632 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000633 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000634 msg_row = prev_msg_row;
635 if (prev_msg_row == Rows - 1)
636 msg_row--;
637 }
638 msg_col = 0;
639 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
640 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000643 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
644 {
645 if (curbuf->b_ml.ml_flags & ML_EMPTY)
646 EMSG(_(e_emptybuf));
647 else
648 EMSG(_("E501: At end-of-file"));
649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 }
651
652#ifdef FEAT_GUI
653 --hold_gui_events;
654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 --RedrawingDisabled;
656 --no_wait_return;
657 update_screen(CLEAR);
658 need_wait_return = FALSE;
659 msg_scroll = save_msg_scroll;
660}
661
662/*
663 * Execute a simple command line. Used for translated commands like "*".
664 */
665 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100666do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
668 return do_cmdline(cmd, NULL, NULL,
669 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
670}
671
672/*
673 * do_cmdline(): execute one Ex command line
674 *
675 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100676 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 * 2. Split up in parts separated with '|'.
678 *
679 * This function can be called recursively!
680 *
681 * flags:
682 * DOCMD_VERBOSE - The command will be included in the error message.
683 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100684 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 * DOCMD_KEYTYPED - Don't reset KeyTyped.
686 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
687 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
688 *
689 * return FAIL if cmdline could not be executed, OK otherwise
690 */
691 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100692do_cmdline(
693 char_u *cmdline,
694 char_u *(*fgetline)(int, void *, int),
695 void *cookie, /* argument for fgetline() */
696 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697{
698 char_u *next_cmdline; /* next cmd to execute */
699 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100700 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 static int recursive = 0; /* recursive depth */
702 int msg_didout_before_start = 0;
703 int count = 0; /* line number count */
704 int did_inc = FALSE; /* incremented RedrawingDisabled */
705 int retval = OK;
706#ifdef FEAT_EVAL
707 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000708 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 int current_line = 0; /* active line in lines_ga */
710 char_u *fname = NULL; /* function or script name */
711 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
712 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000713 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 int initial_trylevel;
715 struct msglist **saved_msg_list = NULL;
716 struct msglist *private_msg_list;
717
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100718 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100719 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000721 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000723 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100725# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726# define cmd_cookie cookie
727#endif
728 static int call_depth = 0; /* recursiveness */
729
730#ifdef FEAT_EVAL
731 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
732 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000733 * This ensures that the do_errthrow() call in do_one_cmd() does not
734 * combine the messages stored by an earlier invocation of do_one_cmd()
735 * with the command name of the later one. This would happen when
736 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 saved_msg_list = msg_list;
738 msg_list = &private_msg_list;
739 private_msg_list = NULL;
740#endif
741
742 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100743 * here. The value of 200 allows nested function calls, ":source", etc.
744 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100745 if (call_depth >= 200
746#ifdef FEAT_EVAL
747 && call_depth >= p_mfd
748#endif
749 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {
751 EMSG(_("E169: Command too recursive"));
752#ifdef FEAT_EVAL
753 /* When converting to an exception, we do not include the command name
754 * since this is not an error of the specific command. */
755 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
756 msg_list = saved_msg_list;
757#endif
758 return FAIL;
759 }
760 ++call_depth;
761
762#ifdef FEAT_EVAL
763 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000764 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 cstack.cs_trylevel = 0;
766 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000767 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
769
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100770 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
772 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100773 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000774 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 ++ex_nesting_level;
776
777 /* Get the function or script name and the address where the next breakpoint
778 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 {
781 fname = func_name(real_cookie);
782 breakpoint = func_breakpoint(real_cookie);
783 dbg_tick = func_dbg_tick(real_cookie);
784 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100785 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {
787 fname = sourcing_name;
788 breakpoint = source_breakpoint(real_cookie);
789 dbg_tick = source_dbg_tick(real_cookie);
790 }
791
792 /*
793 * Initialize "force_abort" and "suppress_errthrow" at the top level.
794 */
795 if (!recursive)
796 {
797 force_abort = FALSE;
798 suppress_errthrow = FALSE;
799 }
800
801 /*
802 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000803 * exception handling (used when debugging). Otherwise clear it to avoid
804 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000806 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000807 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000808 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200809 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811 initial_trylevel = trylevel;
812
813 /*
814 * "did_throw" will be set to TRUE when an exception is being thrown.
815 */
816 did_throw = FALSE;
817#endif
818 /*
819 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000820 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 * If force_abort is set, we cancel everything.
822 */
823 did_emsg = FALSE;
824
825 /*
826 * KeyTyped is only set when calling vgetc(). Reset it here when not
827 * calling vgetc() (sourced command lines).
828 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100829 if (!(flags & DOCMD_KEYTYPED)
830 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 KeyTyped = FALSE;
832
833 /*
834 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000835 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 * - for multiple commands on one line, separated with '|'
837 * - when repeating until there are no more lines (for ":source")
838 */
839 next_cmdline = cmdline;
840 do
841 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000842#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100843 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844#endif
845
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000846 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 if (next_cmdline == NULL
848#ifdef FEAT_EVAL
849 && !force_abort
850 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852#endif
853 )
854 did_emsg = FALSE;
855
856 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000857 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100858 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 * 3. If a line is given: Make a copy, so we can mess with it.
860 */
861
862#ifdef FEAT_EVAL
863 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000864 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 {
866 /* Each '|' separated command is stored separately in lines_ga, to
867 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100868 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
870 /* Check if a function has returned or, unless it has an unclosed
871 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000872 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000874# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000875 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000876 func_line_end(real_cookie);
877# endif
878 if (func_has_ended(real_cookie))
879 {
880 retval = FAIL;
881 break;
882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000884#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000885 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100886 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000887 script_line_end();
888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
890 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100891 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
893 retval = FAIL;
894 break;
895 }
896
897 /* If breakpoints have been added/deleted need to check for it. */
898 if (breakpoint != NULL && dbg_tick != NULL
899 && *dbg_tick != debug_tick)
900 {
901 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100902 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 fname, sourcing_lnum);
904 *dbg_tick = debug_tick;
905 }
906
907 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
908 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
909
910 /* Did we encounter a breakpoint? */
911 if (breakpoint != NULL && *breakpoint != 0
912 && *breakpoint <= sourcing_lnum)
913 {
914 dbg_breakpoint(fname, sourcing_lnum);
915 /* Find next breakpoint. */
916 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100917 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 fname, sourcing_lnum);
919 *dbg_tick = debug_tick;
920 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000921# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000922 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000923 {
924 if (getline_is_func)
925 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100926 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 script_line_start();
928 }
929# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 }
931
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000932 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000934 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100935 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 * below, so that it stores lines in or reads them from
937 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000938 * while/for loop. */
939 cmd_getline = get_loop_line;
940 cmd_cookie = (void *)&cmd_loop_cookie;
941 cmd_loop_cookie.lines_gap = &lines_ga;
942 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100943 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000944 cmd_loop_cookie.cookie = cookie;
945 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947 else
948 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100949 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 cmd_cookie = cookie;
951 }
952#endif
953
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100954 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 if (next_cmdline == NULL)
956 {
957 /*
958 * Need to set msg_didout for the first line after an ":if",
959 * otherwise the ":if" will be overwritten.
960 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100961 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100963 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964#ifdef FEAT_EVAL
965 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
966#else
967 0
968#endif
969 )) == NULL)
970 {
971 /* Don't call wait_return for aborted command line. The NULL
972 * returned for the end of a sourced file or executed function
973 * doesn't do this. */
974 if (KeyTyped && !(flags & DOCMD_REPEAT))
975 need_wait_return = FALSE;
976 retval = FAIL;
977 break;
978 }
979 used_getline = TRUE;
980
981 /*
982 * Keep the first typed line. Clear it when more lines are typed.
983 */
984 if (flags & DOCMD_KEEPLINE)
985 {
986 vim_free(repeat_cmdline);
987 if (count == 0)
988 repeat_cmdline = vim_strsave(next_cmdline);
989 else
990 repeat_cmdline = NULL;
991 }
992 }
993
994 /* 3. Make a copy of the command so we can mess with it. */
995 else if (cmdline_copy == NULL)
996 {
997 next_cmdline = vim_strsave(next_cmdline);
998 if (next_cmdline == NULL)
999 {
1000 EMSG(_(e_outofmem));
1001 retval = FAIL;
1002 break;
1003 }
1004 }
1005 cmdline_copy = next_cmdline;
1006
1007#ifdef FEAT_EVAL
1008 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001009 * Save the current line when inside a ":while" or ":for", and when
1010 * the command looks like a ":while" or ":for", because we may need it
1011 * later. When there is a '|' and another command, it is stored
1012 * separately, because we need to be able to jump back to it from an
1013 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001015 if (current_line == lines_ga.ga_len
1016 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001018 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 {
1020 retval = FAIL;
1021 break;
1022 }
1023 }
1024 did_endif = FALSE;
1025#endif
1026
1027 if (count++ == 0)
1028 {
1029 /*
1030 * All output from the commands is put below each other, without
1031 * waiting for a return. Don't do this when executing commands
1032 * from a script or when being called recursive (e.g. for ":e
1033 * +command file").
1034 */
1035 if (!(flags & DOCMD_NOWAIT) && !recursive)
1036 {
1037 msg_didout_before_start = msg_didout;
1038 msg_didany = FALSE; /* no output yet */
1039 msg_start();
1040 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001041 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 ++RedrawingDisabled;
1043 did_inc = TRUE;
1044 }
1045 }
1046
1047 if (p_verbose >= 15 && sourcing_name != NULL)
1048 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001050 verbose_enter_scroll();
1051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 smsg((char_u *)_("line %ld: %s"),
1053 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001054 if (msg_silent == 0)
1055 msg_puts((char_u *)"\n"); /* don't overwrite this */
1056
1057 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 --no_wait_return;
1059 }
1060
1061 /*
1062 * 2. Execute one '|' separated command.
1063 * do_one_cmd() will return NULL if there is no trailing '|'.
1064 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1065 */
1066 ++recursive;
1067 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1068#ifdef FEAT_EVAL
1069 &cstack,
1070#endif
1071 cmd_getline, cmd_cookie);
1072 --recursive;
1073
1074#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 if (cmd_cookie == (void *)&cmd_loop_cookie)
1076 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079#endif
1080
1081 if (next_cmdline == NULL)
1082 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001083 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084#ifdef FEAT_CMDHIST
1085 /*
1086 * If the command was typed, remember it for the ':' register.
1087 * Do this AFTER executing the command to make :@: work.
1088 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001089 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 && new_last_cmdline != NULL)
1091 {
1092 vim_free(last_cmdline);
1093 last_cmdline = new_last_cmdline;
1094 new_last_cmdline = NULL;
1095 }
1096#endif
1097 }
1098 else
1099 {
1100 /* need to copy the command after the '|' to cmdline_copy, for the
1101 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001102 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 next_cmdline = cmdline_copy;
1104 }
1105
1106
1107#ifdef FEAT_EVAL
1108 /* reset did_emsg for a function that is not aborted by an error */
1109 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001110 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 && !func_has_abort(real_cookie))
1112 did_emsg = FALSE;
1113
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001114 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 {
1116 ++current_line;
1117
1118 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 * An ":endwhile", ":endfor" and ":continue" is handled here.
1120 * If we were executing commands, jump back to the ":while" or
1121 * ":for".
1122 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001126 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001128 /* Jump back to the matching ":while" or ":for". Be careful
1129 * not to use a cs_line[] from an entry that isn't a ":while"
1130 * or ":for": It would make "current_line" invalid and can
1131 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 if (!did_emsg && !got_int && !did_throw
1133 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 && (cstack.cs_flags[cstack.cs_idx]
1135 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 && cstack.cs_line[cstack.cs_idx] >= 0
1137 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1138 {
1139 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001140 /* remember we jumped there */
1141 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 line_breakcheck(); /* check if CTRL-C typed */
1143
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001144 /* Check for the next breakpoint at or after the ":while"
1145 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 if (breakpoint != NULL)
1147 {
1148 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001149 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 fname,
1151 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1152 *dbg_tick = debug_tick;
1153 }
1154 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001155 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001159 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1160 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 }
1162 }
1163
1164 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001169 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1171 }
1172 }
1173
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001174 /* Check for the next breakpoint after a watchexpression */
1175 if (breakpoint != NULL && has_watchexpr())
1176 {
1177 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1178 *dbg_tick = debug_tick;
1179 }
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 /*
1182 * When not inside any ":while" loop, clear remembered lines.
1183 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 {
1186 if (lines_ga.ga_len > 0)
1187 {
1188 sourcing_lnum =
1189 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1190 free_cmdlines(&lines_ga);
1191 }
1192 current_line = 0;
1193 }
1194
1195 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1197 * being restored at the ":endtry". Reset them here and set the
1198 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1199 * This includes the case where a missing ":endif", ":endwhile" or
1200 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001202 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001204 cstack.cs_lflags &= ~CSL_HAD_FINA;
1205 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1206 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 did_throw ? (void *)current_exception : NULL);
1208 did_emsg = got_int = did_throw = FALSE;
1209 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1210 }
1211
1212 /* Update global "trylevel" for recursive calls to do_cmdline() from
1213 * within this loop. */
1214 trylevel = initial_trylevel + cstack.cs_trylevel;
1215
1216 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001217 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 * files) is aborted because of an error, an interrupt, or an uncaught
1219 * exception, cancel everything. If it is left normally, reset
1220 * force_abort to get the non-EH compatible abortion behavior for
1221 * the rest of the script.
1222 */
1223 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1224 force_abort = FALSE;
1225
1226 /* Convert an interrupt to an exception if appropriate. */
1227 (void)do_intthrow(&cstack);
1228#endif /* FEAT_EVAL */
1229
1230 }
1231 /*
1232 * Continue executing command lines when:
1233 * - no CTRL-C typed, no aborting error, no exception thrown or try
1234 * conditionals need to be checked for executing finally clauses or
1235 * catching an interrupt exception
1236 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001237 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 * looping for ":source" command or function call.
1239 */
1240 while (!((got_int
1241#ifdef FEAT_EVAL
1242 || (did_emsg && force_abort) || did_throw
1243#endif
1244 )
1245#ifdef FEAT_EVAL
1246 && cstack.cs_trylevel == 0
1247#endif
1248 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001249 && !(did_emsg
1250#ifdef FEAT_EVAL
1251 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001252 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001253 * the :endtry to be missed. */
1254 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1255#endif
1256 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001257 && (getline_equal(fgetline, cookie, getexmodeline)
1258 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 && (next_cmdline != NULL
1260#ifdef FEAT_EVAL
1261 || cstack.cs_idx >= 0
1262#endif
1263 || (flags & DOCMD_REPEAT)));
1264
1265 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001266 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267#ifdef FEAT_EVAL
1268 free_cmdlines(&lines_ga);
1269 ga_clear(&lines_ga);
1270
1271 if (cstack.cs_idx >= 0)
1272 {
1273 /*
1274 * If a sourced file or executed function ran to its end, report the
1275 * unclosed conditional.
1276 */
1277 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001278 && ((getline_equal(fgetline, cookie, getsourceline)
1279 && !source_finished(fgetline, cookie))
1280 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 && !func_has_ended(real_cookie))))
1282 {
1283 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1284 EMSG(_(e_endtry));
1285 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1286 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001287 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1288 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 else
1290 EMSG(_(e_endif));
1291 }
1292
1293 /*
1294 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1295 * ":endtry" in a sourced file or executed function. If the try
1296 * conditional is in its finally clause, ignore anything pending.
1297 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001298 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 */
1300 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001301 {
1302 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1303
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001304 if (idx >= 0)
1305 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001306 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1307 &cstack.cs_looplevel);
1308 }
1309 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 trylevel = initial_trylevel;
1311 }
1312
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001313 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1314 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001316 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 ? (char_u *)"endfunction" : (char_u *)NULL);
1318
1319 if (trylevel == 0)
1320 {
1321 /*
1322 * When an exception is being thrown out of the outermost try
1323 * conditional, discard the uncaught exception, disable the conversion
1324 * of interrupts or errors to exceptions, and ensure that no more
1325 * commands are executed.
1326 */
1327 if (did_throw)
1328 {
1329 void *p = NULL;
1330 char_u *saved_sourcing_name;
1331 int saved_sourcing_lnum;
1332 struct msglist *messages = NULL, *next;
1333
1334 /*
1335 * If the uncaught exception is a user exception, report it as an
1336 * error. If it is an error exception, display the saved error
1337 * message now. For an interrupt exception, do nothing; the
1338 * interrupt message is given elsewhere.
1339 */
1340 switch (current_exception->type)
1341 {
1342 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001343 vim_snprintf((char *)IObuff, IOSIZE,
1344 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 current_exception->value);
1346 p = vim_strsave(IObuff);
1347 break;
1348 case ET_ERROR:
1349 messages = current_exception->messages;
1350 current_exception->messages = NULL;
1351 break;
1352 case ET_INTERRUPT:
1353 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 }
1355
1356 saved_sourcing_name = sourcing_name;
1357 saved_sourcing_lnum = sourcing_lnum;
1358 sourcing_name = current_exception->throw_name;
1359 sourcing_lnum = current_exception->throw_lnum;
1360 current_exception->throw_name = NULL;
1361
1362 discard_current_exception(); /* uses IObuff if 'verbose' */
1363 suppress_errthrow = TRUE;
1364 force_abort = TRUE;
1365
1366 if (messages != NULL)
1367 {
1368 do
1369 {
1370 next = messages->next;
1371 emsg(messages->msg);
1372 vim_free(messages->msg);
1373 vim_free(messages);
1374 messages = next;
1375 }
1376 while (messages != NULL);
1377 }
1378 else if (p != NULL)
1379 {
1380 emsg(p);
1381 vim_free(p);
1382 }
1383 vim_free(sourcing_name);
1384 sourcing_name = saved_sourcing_name;
1385 sourcing_lnum = saved_sourcing_lnum;
1386 }
1387
1388 /*
1389 * On an interrupt or an aborting error not converted to an exception,
1390 * disable the conversion of errors to exceptions. (Interrupts are not
1391 * converted any more, here.) This enables also the interrupt message
1392 * when force_abort is set and did_emsg unset in case of an interrupt
1393 * from a finally clause after an error.
1394 */
1395 else if (got_int || (did_emsg && force_abort))
1396 suppress_errthrow = TRUE;
1397 }
1398
1399 /*
1400 * The current cstack will be freed when do_cmdline() returns. An uncaught
1401 * exception will have to be rethrown in the previous cstack. If a function
1402 * has just returned or a script file was just finished and the previous
1403 * cstack belongs to the same function or, respectively, script file, it
1404 * will have to be checked for finally clauses to be executed due to the
1405 * ":return" or ":finish". This is done in do_one_cmd().
1406 */
1407 if (did_throw)
1408 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001409 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001411 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 && ex_nesting_level > func_level(real_cookie) + 1))
1413 {
1414 if (!did_throw)
1415 check_cstack = TRUE;
1416 }
1417 else
1418 {
1419 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001420 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 --ex_nesting_level;
1422 /*
1423 * Go to debug mode when returning from a function in which we are
1424 * single-stepping.
1425 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001426 if ((getline_equal(fgetline, cookie, getsourceline)
1427 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001429 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 ? (char_u *)_("End of sourced file")
1431 : (char_u *)_("End of function"));
1432 }
1433
1434 /*
1435 * Restore the exception environment (done after returning from the
1436 * debugger).
1437 */
1438 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001439 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
1441 msg_list = saved_msg_list;
1442#endif /* FEAT_EVAL */
1443
1444 /*
1445 * If there was too much output to fit on the command line, ask the user to
1446 * hit return before redrawing the screen. With the ":global" command we do
1447 * this only once after the command is finished.
1448 */
1449 if (did_inc)
1450 {
1451 --RedrawingDisabled;
1452 --no_wait_return;
1453 msg_scroll = FALSE;
1454
1455 /*
1456 * When just finished an ":if"-":else" which was typed, no need to
1457 * wait for hit-return. Also for an error situation.
1458 */
1459 if (retval == FAIL
1460#ifdef FEAT_EVAL
1461 || (did_endif && KeyTyped && !did_emsg)
1462#endif
1463 )
1464 {
1465 need_wait_return = FALSE;
1466 msg_didany = FALSE; /* don't wait when restarting edit */
1467 }
1468 else if (need_wait_return)
1469 {
1470 /*
1471 * The msg_start() above clears msg_didout. The wait_return we do
1472 * here should not overwrite the command that may be shown before
1473 * doing that.
1474 */
1475 msg_didout |= msg_didout_before_start;
1476 wait_return(FALSE);
1477 }
1478 }
1479
Bram Moolenaar2a942252012-11-28 23:03:07 +01001480#ifdef FEAT_EVAL
1481 did_endif = FALSE; /* in case do_cmdline used recursively */
1482#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 /*
1484 * Reset if_level, in case a sourced script file contains more ":if" than
1485 * ":endif" (could be ":if x | foo | endif").
1486 */
1487 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001488#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 --call_depth;
1491 return retval;
1492}
1493
1494#ifdef FEAT_EVAL
1495/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001496 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 */
1498 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001499get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001501 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 wcmd_T *wp;
1503 char_u *line;
1504
1505 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1506 {
1507 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001508 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001510 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 if (cp->getline == NULL)
1512 line = getcmdline(c, 0L, indent);
1513 else
1514 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001515 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 ++cp->current_line;
1517
1518 return line;
1519 }
1520
1521 KeyTyped = FALSE;
1522 ++cp->current_line;
1523 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1524 sourcing_lnum = wp->lnum;
1525 return vim_strsave(wp->line);
1526}
1527
1528/*
1529 * Store a line in "gap" so that a ":while" loop can execute it again.
1530 */
1531 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001532store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533{
1534 if (ga_grow(gap, 1) == FAIL)
1535 return FAIL;
1536 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1537 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1538 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 return OK;
1540}
1541
1542/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001543 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 */
1545 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001546free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 while (gap->ga_len > 0)
1549 {
1550 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1551 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 }
1553}
1554#endif
1555
1556/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001557 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1558 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001561getline_equal(
1562 char_u *(*fgetline)(int, void *, int),
1563 void *cookie UNUSED, /* argument for fgetline() */
1564 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
1566#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001567 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001568 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaar89d40322006-08-29 15:30:07 +00001570 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001571 * function that's originally used to obtain the lines. This may be
1572 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001573 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001574 cp = (struct loop_cookie *)cookie;
1575 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 {
1577 gp = cp->getline;
1578 cp = cp->cookie;
1579 }
1580 return gp == func;
1581#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001582 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583#endif
1584}
1585
1586#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1587/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001588 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 * getline function. Otherwise return "cookie".
1590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001592getline_cookie(
1593 char_u *(*fgetline)(int, void *, int) UNUSED,
1594 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
1596# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001597 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001598 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
Bram Moolenaar89d40322006-08-29 15:30:07 +00001600 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001601 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001603 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001604 cp = (struct loop_cookie *)cookie;
1605 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 {
1607 gp = cp->getline;
1608 cp = cp->cookie;
1609 }
1610 return cp;
1611# else
1612 return cookie;
1613# endif
1614}
1615#endif
1616
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001617
1618/*
1619 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1620 * ":bwipeout", etc.
1621 * Returns the buffer number.
1622 */
1623 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001624compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001625{
1626 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001627 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 int count = offset;
1629
1630 buf = firstbuf;
1631 while (buf->b_next != NULL && buf->b_fnum < lnum)
1632 buf = buf->b_next;
1633 while (count != 0)
1634 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001635 count += (offset < 0) ? 1 : -1;
1636 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1637 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001638 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001639 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001640 if (addr_type == ADDR_LOADED_BUFFERS)
1641 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001642 while (buf->b_ml.ml_mfp == NULL)
1643 {
1644 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1645 if (nextbuf == NULL)
1646 break;
1647 buf = nextbuf;
1648 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001649 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001650 /* we might have gone too far, last buffer is not loadedd */
1651 if (addr_type == ADDR_LOADED_BUFFERS)
1652 while (buf->b_ml.ml_mfp == NULL)
1653 {
1654 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1655 if (nextbuf == NULL)
1656 break;
1657 buf = nextbuf;
1658 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001659 return buf->b_fnum;
1660}
1661
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001662static int current_win_nr(win_T *win);
1663static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001664
1665 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001666current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001667{
1668 win_T *wp;
1669 int nr = 0;
1670
Bram Moolenaar29323592016-07-24 22:04:11 +02001671 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001672 {
1673 ++nr;
1674 if (wp == win)
1675 break;
1676 }
1677 return nr;
1678}
1679
1680 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001681current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001682{
1683 tabpage_T *tp;
1684 int nr = 0;
1685
Bram Moolenaar29323592016-07-24 22:04:11 +02001686 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001687 {
1688 ++nr;
1689 if (tp == tab)
1690 break;
1691 }
1692 return nr;
1693}
1694
1695# define CURRENT_WIN_NR current_win_nr(curwin)
1696# define LAST_WIN_NR current_win_nr(NULL)
1697# define CURRENT_TAB_NR current_tab_nr(curtab)
1698# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700/*
1701 * Execute one Ex command.
1702 *
1703 * If 'sourcing' is TRUE, the command will be included in the error message.
1704 *
1705 * 1. skip comment lines and leading space
1706 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001707 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001708 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001709 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001710 * 6. parse arguments
1711 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001713 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 *
1715 * This function may be called recursively!
1716 */
1717#if (_MSC_VER == 1200)
1718/*
Bram Moolenaared203462004-06-16 11:19:22 +00001719 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001721 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722#endif
1723 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001724do_one_cmd(
1725 char_u **cmdlinep,
1726 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001728 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001730 char_u *(*fgetline)(int, void *, int),
1731 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
1733 char_u *p;
1734 linenr_T lnum;
1735 long n;
1736 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001737 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 exarg_T ea; /* Ex command arguments */
1739 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001740 int save_msg_scroll = msg_scroll;
1741 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001743#ifdef HAVE_SANDBOX
1744 int did_sandbox = FALSE;
1745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 cmdmod_T save_cmdmod;
1747 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001748 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001749 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750
1751 vim_memset(&ea, 0, sizeof(ea));
1752 ea.line1 = 1;
1753 ea.line2 = 1;
1754#ifdef FEAT_EVAL
1755 ++ex_nesting_level;
1756#endif
1757
Bram Moolenaar21691f82012-12-05 19:13:18 +01001758 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 if (quitmore
1760#ifdef FEAT_EVAL
1761 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001762 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001763#endif
1764#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001765 /* avoid that an autocommand, e.g. QuitPre, does this */
1766 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#endif
1768 )
1769 --quitmore;
1770
1771 /*
1772 * Reset browse, confirm, etc.. They are restored when returning, for
1773 * recursive calls.
1774 */
1775 save_cmdmod = cmdmod;
1776 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1777
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001778 /* "#!anything" is handled like a comment. */
1779 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1780 goto doend;
1781
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 /*
1783 * Repeat until no more command modifiers are found.
1784 */
1785 ea.cmd = *cmdlinep;
1786 for (;;)
1787 {
1788/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001789 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 */
1791 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1792 ++ea.cmd;
1793
1794 /* in ex mode, an empty line works like :+ */
1795 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001796 && (getline_equal(fgetline, cookie, getexmodeline)
1797 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001798 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 {
1800 ea.cmd = (char_u *)"+";
1801 ex_pressedreturn = TRUE;
1802 }
1803
1804 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001805 if (*ea.cmd == '"')
1806 goto doend;
1807 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001808 {
1809 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
1813/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001814 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001816 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 switch (*p)
1818 {
1819 /* When adding an entry, also modify cmd_exists(). */
1820 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1821 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 continue;
1824
1825 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1826 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 continue;
1829 }
1830 if (checkforcmd(&ea.cmd, "browse", 3))
1831 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001832#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 cmdmod.browse = TRUE;
1834#endif
1835 continue;
1836 }
1837 if (!checkforcmd(&ea.cmd, "botright", 2))
1838 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 cmdmod.split |= WSP_BOT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 continue;
1841
1842 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1843 break;
1844#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1845 cmdmod.confirm = TRUE;
1846#endif
1847 continue;
1848
1849 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1850 {
1851 cmdmod.keepmarks = TRUE;
1852 continue;
1853 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001854 if (checkforcmd(&ea.cmd, "keepalt", 5))
1855 {
1856 cmdmod.keepalt = TRUE;
1857 continue;
1858 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001859 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1860 {
1861 cmdmod.keeppatterns = TRUE;
1862 continue;
1863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1865 break;
1866 cmdmod.keepjumps = TRUE;
1867 continue;
1868
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001869 case 'f': /* only accept ":filter {pat} cmd" */
1870 {
1871 char_u *reg_pat;
1872
1873 if (!checkforcmd(&p, "filter", 4)
1874 || *p == NUL || ends_excmd(*p))
1875 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001876 if (*p == '!')
1877 {
1878 cmdmod.filter_force = TRUE;
1879 p = skipwhite(p + 1);
1880 if (*p == NUL || ends_excmd(*p))
1881 break;
1882 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001883 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1884 if (p == NULL || *p == NUL)
1885 break;
1886 cmdmod.filter_regmatch.regprog =
1887 vim_regcomp(reg_pat, RE_MAGIC);
1888 if (cmdmod.filter_regmatch.regprog == NULL)
1889 break;
1890 ea.cmd = p;
1891 continue;
1892 }
1893
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 /* ":hide" and ":hide | cmd" are not modifiers */
1895 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1896 || *p == NUL || ends_excmd(*p))
1897 break;
1898 ea.cmd = p;
1899 cmdmod.hide = TRUE;
1900 continue;
1901
1902 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1903 {
1904 cmdmod.lockmarks = TRUE;
1905 continue;
1906 }
1907
1908 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1909 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 continue;
1912
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001913 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001914 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001915#ifdef FEAT_AUTOCMD
1916 if (cmdmod.save_ei == NULL)
1917 {
1918 /* Set 'eventignore' to "all". Restore the
1919 * existing option value later. */
1920 cmdmod.save_ei = vim_strsave(p_ei);
1921 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001922 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001923 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001924#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001925 continue;
1926 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001927 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001928 break;
1929 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001930 continue;
1931
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 continue;
1936
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001937 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1938 {
1939#ifdef HAVE_SANDBOX
1940 if (!did_sandbox)
1941 ++sandbox;
1942 did_sandbox = TRUE;
1943#endif
1944 continue;
1945 }
1946 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001948 if (save_msg_silent == -1)
1949 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 ++msg_silent;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001951 if (*ea.cmd == '!' && !VIM_ISWHITE(ea.cmd[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 {
1953 /* ":silent!", but not "silent !cmd" */
1954 ea.cmd = skipwhite(ea.cmd + 1);
1955 ++emsg_silent;
1956 ++did_esilent;
1957 }
1958 continue;
1959
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001960 case 't': if (checkforcmd(&p, "tab", 3))
1961 {
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001962 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01001963 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001964 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001965 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001966 else
1967 {
1968 if (tabnr < 0 || tabnr > LAST_TAB_NR)
1969 {
1970 errormsg = (char_u *)_(e_invrange);
1971 goto doend;
1972 }
1973 cmdmod.tab = tabnr + 1;
1974 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001975 ea.cmd = p;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001976 continue;
1977 }
1978 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 cmdmod.split |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 continue;
1982
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001983 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1984 break;
1985 if (save_msg_silent == -1)
1986 save_msg_silent = msg_silent;
1987 msg_silent = 0;
1988 continue;
1989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1991 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 cmdmod.split |= WSP_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 continue;
1994 }
1995 if (!checkforcmd(&p, "verbose", 4))
1996 break;
1997 if (verbose_save < 0)
1998 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001999 if (vim_isdigit(*ea.cmd))
2000 p_verbose = atoi((char *)ea.cmd);
2001 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 p_verbose = 1;
2003 ea.cmd = p;
2004 continue;
2005 }
2006 break;
2007 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002008 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
2010#ifdef FEAT_EVAL
2011 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2012 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2013#else
2014 ea.skip = (if_level > 0);
2015#endif
2016
2017#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002018# ifdef FEAT_PROFILE
2019 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002020 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002021 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002022 if (getline_equal(fgetline, cookie, get_func_line))
2023 func_line_exec(getline_cookie(fgetline, cookie));
2024 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002025 script_line_exec();
2026 }
2027#endif
2028
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 /* May go to debug mode. If this happens and the ">quit" debug command is
2030 * used, throw an interrupt exception and skip the next command. */
2031 dbg_check_breakpoint(&ea);
2032 if (!ea.skip && got_int)
2033 {
2034 ea.skip = TRUE;
2035 (void)do_intthrow(cstack);
2036 }
2037#endif
2038
2039/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002040 * 3. Skip over the range to find the command. Let "p" point to after it.
2041 *
2042 * We need the command to know what kind of range it uses.
2043 */
2044 cmd = ea.cmd;
2045 ea.cmd = skip_range(ea.cmd, NULL);
2046 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2047 ea.cmd = skipwhite(ea.cmd + 1);
2048 p = find_command(&ea, NULL);
2049
2050/*
2051 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 *
2053 * where 'addr' is:
2054 *
2055 * % (entire file)
2056 * $ [+-NUM]
2057 * 'x [+-NUM] (where x denotes a currently defined mark)
2058 * . [+-NUM]
2059 * [+-NUM]..
2060 * NUM
2061 *
2062 * The ea.cmd pointer is updated to point to the first character following the
2063 * range spec. If an initial address is found, but no second, the upper bound
2064 * is equal to the lower.
2065 */
2066
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002067 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002068 if (!IS_USER_CMDIDX(ea.cmdidx))
2069 {
2070 if (ea.cmdidx != CMD_SIZE)
2071 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2072 else
2073 ea.addr_type = ADDR_LINES;
2074
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002075 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002076 if (ea.cmdidx == CMD_wincmd && p != NULL)
2077 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002078 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002081 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 for (;;)
2083 {
2084 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002085 switch (ea.addr_type)
2086 {
2087 case ADDR_LINES:
2088 /* default is current line number */
2089 ea.line2 = curwin->w_cursor.lnum;
2090 break;
2091 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002092 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002093 break;
2094 case ADDR_ARGUMENTS:
2095 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002096 if (ea.line2 > ARGCOUNT)
2097 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002098 break;
2099 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002100 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002101 ea.line2 = curbuf->b_fnum;
2102 break;
2103 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002104 ea.line2 = CURRENT_TAB_NR;
2105 break;
2106 case ADDR_TABS_RELATIVE:
2107 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002108 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002109#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002110 case ADDR_QUICKFIX:
2111 ea.line2 = qf_get_cur_valid_idx(&ea);
2112 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002113#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002116 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002117 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 if (ea.cmd == NULL) /* error detected */
2119 goto doend;
2120 if (lnum == MAXLNUM)
2121 {
2122 if (*ea.cmd == '%') /* '%' - all lines */
2123 {
2124 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002125 switch (ea.addr_type)
2126 {
2127 case ADDR_LINES:
2128 ea.line1 = 1;
2129 ea.line2 = curbuf->b_ml.ml_line_count;
2130 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002131 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002132 {
2133 buf_T *buf = firstbuf;
2134
2135 while (buf->b_next != NULL
2136 && buf->b_ml.ml_mfp == NULL)
2137 buf = buf->b_next;
2138 ea.line1 = buf->b_fnum;
2139 buf = lastbuf;
2140 while (buf->b_prev != NULL
2141 && buf->b_ml.ml_mfp == NULL)
2142 buf = buf->b_prev;
2143 ea.line2 = buf->b_fnum;
2144 break;
2145 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002146 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002147 ea.line1 = firstbuf->b_fnum;
2148 ea.line2 = lastbuf->b_fnum;
2149 break;
2150 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002151 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002152 if (IS_USER_CMDIDX(ea.cmdidx))
2153 {
2154 ea.line1 = 1;
2155 ea.line2 = ea.addr_type == ADDR_WINDOWS
2156 ? LAST_WIN_NR : LAST_TAB_NR;
2157 }
2158 else
2159 {
2160 /* there is no Vim command which uses '%' and
2161 * ADDR_WINDOWS or ADDR_TABS */
2162 errormsg = (char_u *)_(e_invrange);
2163 goto doend;
2164 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002165 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002166 case ADDR_TABS_RELATIVE:
2167 errormsg = (char_u *)_(e_invrange);
2168 goto doend;
2169 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002170 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002171 if (ARGCOUNT == 0)
2172 ea.line1 = ea.line2 = 0;
2173 else
2174 {
2175 ea.line1 = 1;
2176 ea.line2 = ARGCOUNT;
2177 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002178 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002179#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002180 case ADDR_QUICKFIX:
2181 ea.line1 = 1;
2182 ea.line2 = qf_get_size(&ea);
2183 if (ea.line2 == 0)
2184 ea.line2 = 1;
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.addr_count;
2189 }
2190 /* '*' - visual area */
2191 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2192 {
2193 pos_T *fp;
2194
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002195 if (ea.addr_type != ADDR_LINES)
2196 {
2197 errormsg = (char_u *)_(e_invrange);
2198 goto doend;
2199 }
2200
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 ++ea.cmd;
2202 if (!ea.skip)
2203 {
2204 fp = getmark('<', FALSE);
2205 if (check_mark(fp) == FAIL)
2206 goto doend;
2207 ea.line1 = fp->lnum;
2208 fp = getmark('>', FALSE);
2209 if (check_mark(fp) == FAIL)
2210 goto doend;
2211 ea.line2 = fp->lnum;
2212 ++ea.addr_count;
2213 }
2214 }
2215 }
2216 else
2217 ea.line2 = lnum;
2218 ea.addr_count++;
2219
2220 if (*ea.cmd == ';')
2221 {
2222 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +01002225 /* don't leave the cursor on an illegal line or column */
2226 check_cursor();
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 }
2229 else if (*ea.cmd != ',')
2230 break;
2231 ++ea.cmd;
2232 }
2233
2234 /* One address given: set start and end lines */
2235 if (ea.addr_count == 1)
2236 {
2237 ea.line1 = ea.line2;
2238 /* ... but only implicit: really no address given */
2239 if (lnum == MAXLNUM)
2240 ea.addr_count = 0;
2241 }
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002244 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 */
2246
2247 /*
2248 * Skip ':' and any white space
2249 */
2250 ea.cmd = skipwhite(ea.cmd);
2251 while (*ea.cmd == ':')
2252 ea.cmd = skipwhite(ea.cmd + 1);
2253
2254 /*
2255 * If we got a line, but no command, then go to the line.
2256 * If we find a '|' or '\n' we set ea.nextcmd.
2257 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002258 if (*ea.cmd == NUL || *ea.cmd == '"'
2259 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
2261 /*
2262 * strange vi behaviour:
2263 * ":3" jumps to line 3
2264 * ":3|..." prints line 3
2265 * ":|" prints current line
2266 */
2267 if (ea.skip) /* skip this if inside :if */
2268 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002269 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
2271 ea.cmdidx = CMD_print;
2272 ea.argt = RANGE+COUNT+TRLBAR;
2273 if ((errormsg = invalid_range(&ea)) == NULL)
2274 {
2275 correct_range(&ea);
2276 ex_print(&ea);
2277 }
2278 }
2279 else if (ea.addr_count != 0)
2280 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002281 if (ea.line2 > curbuf->b_ml.ml_line_count)
2282 {
2283 /* With '-' in 'cpoptions' a line number past the file is an
2284 * error, otherwise put it at the end of the file. */
2285 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2286 ea.line2 = -1;
2287 else
2288 ea.line2 = curbuf->b_ml.ml_line_count;
2289 }
2290
2291 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002292 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 else
2294 {
2295 if (ea.line2 == 0)
2296 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 else
2298 curwin->w_cursor.lnum = ea.line2;
2299 beginline(BL_SOL | BL_FIX);
2300 }
2301 }
2302 goto doend;
2303 }
2304
Bram Moolenaard5005162014-08-22 23:05:54 +02002305#ifdef FEAT_AUTOCMD
2306 /* If this looks like an undefined user command and there are CmdUndefined
2307 * autocommands defined, trigger the matching autocommands. */
2308 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2309 && ASCII_ISUPPER(*ea.cmd)
2310 && has_cmdundefined())
2311 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002312 int ret;
2313
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002314 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002315 while (ASCII_ISALNUM(*p))
2316 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002317 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002318 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2319 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002320 /* If the autocommands did something and didn't cause an error, try
2321 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002322 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002323 }
2324#endif
2325
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326#ifdef FEAT_USR_CMDS
2327 if (p == NULL)
2328 {
2329 if (!ea.skip)
2330 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2331 goto doend;
2332 }
2333 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002334 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2335 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
2337 errormsg = uc_fun_cmd();
2338 goto doend;
2339 }
2340#endif
2341 if (ea.cmdidx == CMD_SIZE)
2342 {
2343 if (!ea.skip)
2344 {
2345 STRCPY(IObuff, _("E492: Not an editor command"));
2346 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002347 {
2348 /* If the modifier was parsed OK the error must be in the
2349 * following command */
2350 if (after_modifier != NULL)
2351 append_command(after_modifier);
2352 else
2353 append_command(*cmdlinep);
2354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002356 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
2358 goto doend;
2359 }
2360
Bram Moolenaar958636c2014-10-21 20:01:58 +02002361 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2362 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002363#ifdef HAVE_EX_SCRIPT_NI
2364 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2365#endif
2366 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
2368#ifndef FEAT_EVAL
2369 /*
2370 * When the expression evaluation is disabled, recognize the ":if" and
2371 * ":endif" commands and ignore everything in between it.
2372 */
2373 if (ea.cmdidx == CMD_if)
2374 ++if_level;
2375 if (if_level)
2376 {
2377 if (ea.cmdidx == CMD_endif)
2378 --if_level;
2379 goto doend;
2380 }
2381
2382#endif
2383
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002384 /* forced commands */
2385 if (*p == '!' && ea.cmdidx != CMD_substitute
2386 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
2388 ++p;
2389 ea.forceit = TRUE;
2390 }
2391 else
2392 ea.forceit = FALSE;
2393
2394/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002395 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002397 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002398 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
2400 if (!ea.skip)
2401 {
2402#ifdef HAVE_SANDBOX
2403 if (sandbox != 0 && !(ea.argt & SBOXOK))
2404 {
2405 /* Command not allowed in sandbox. */
2406 errormsg = (char_u *)_(e_sandbox);
2407 goto doend;
2408 }
2409#endif
2410 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2411 {
2412 /* Command not allowed in non-'modifiable' buffer */
2413 errormsg = (char_u *)_(e_modifiable);
2414 goto doend;
2415 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002416
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002417 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002418 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002420 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002421 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 goto doend;
2423 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002424#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002425 /* Disallow editing another buffer when "curbuf_lock" is set.
2426 * Do allow ":edit" (check for argument later).
2427 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002428 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002429 && ea.cmdidx != CMD_edit
2430 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002431 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002432 && curbuf_locked())
2433 goto doend;
2434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
2436 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2437 {
2438 /* no range allowed */
2439 errormsg = (char_u *)_(e_norange);
2440 goto doend;
2441 }
2442 }
2443
2444 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2445 {
2446 errormsg = (char_u *)_(e_nobang);
2447 goto doend;
2448 }
2449
2450 /*
2451 * Don't complain about the range if it is not used
2452 * (could happen if line_count is accidentally set to 0).
2453 */
2454 if (!ea.skip && !ni)
2455 {
2456 /*
2457 * If the range is backwards, ask for confirmation and, if given, swap
2458 * ea.line1 & ea.line2 so it's forwards again.
2459 * When global command is busy, don't ask, will fail below.
2460 */
2461 if (!global_busy && ea.line1 > ea.line2)
2462 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002463 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002465 if (sourcing || exmode_active)
2466 {
2467 errormsg = (char_u *)_("E493: Backwards range given");
2468 goto doend;
2469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 if (ask_yesno((char_u *)
2471 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002472 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 }
2474 lnum = ea.line1;
2475 ea.line1 = ea.line2;
2476 ea.line2 = lnum;
2477 }
2478 if ((errormsg = invalid_range(&ea)) != NULL)
2479 goto doend;
2480 }
2481
2482 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2483 ea.line2 = 1;
2484
2485 correct_range(&ea);
2486
2487#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002488 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2489 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
2491 /* Put the first line at the start of a closed fold, put the last line
2492 * at the end of a closed fold. */
2493 (void)hasFolding(ea.line1, &ea.line1, NULL);
2494 (void)hasFolding(ea.line2, NULL, &ea.line2);
2495 }
2496#endif
2497
2498#ifdef FEAT_QUICKFIX
2499 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002500 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 * option here, so things like % get expanded.
2502 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002503 p = replace_makeprg(&ea, p, cmdlinep);
2504 if (p == NULL)
2505 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506#endif
2507
2508 /*
2509 * Skip to start of argument.
2510 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2511 */
2512 if (ea.cmdidx == CMD_bang)
2513 ea.arg = p;
2514 else
2515 ea.arg = skipwhite(p);
2516
2517 /*
2518 * Check for "++opt=val" argument.
2519 * Must be first, allow ":w ++enc=utf8 !cmd"
2520 */
2521 if (ea.argt & ARGOPT)
2522 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2523 if (getargopt(&ea) == FAIL && !ni)
2524 {
2525 errormsg = (char_u *)_(e_invarg);
2526 goto doend;
2527 }
2528
2529 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2530 {
2531 if (*ea.arg == '>') /* append */
2532 {
2533 if (*++ea.arg != '>') /* typed wrong */
2534 {
2535 errormsg = (char_u *)_("E494: Use w or w>>");
2536 goto doend;
2537 }
2538 ea.arg = skipwhite(ea.arg + 1);
2539 ea.append = TRUE;
2540 }
2541 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2542 {
2543 ++ea.arg;
2544 ea.usefilter = TRUE;
2545 }
2546 }
2547
2548 if (ea.cmdidx == CMD_read)
2549 {
2550 if (ea.forceit)
2551 {
2552 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2553 ea.forceit = FALSE;
2554 }
2555 else if (*ea.arg == '!') /* :r !filter */
2556 {
2557 ++ea.arg;
2558 ea.usefilter = TRUE;
2559 }
2560 }
2561
2562 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2563 {
2564 ea.amount = 1;
2565 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2566 {
2567 ++ea.arg;
2568 ++ea.amount;
2569 }
2570 ea.arg = skipwhite(ea.arg);
2571 }
2572
2573 /*
2574 * Check for "+command" argument, before checking for next command.
2575 * Don't do this for ":read !cmd" and ":write !cmd".
2576 */
2577 if ((ea.argt & EDITCMD) && !ea.usefilter)
2578 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2579
2580 /*
2581 * Check for '|' to separate commands and '"' to start comments.
2582 * Don't do this for ":read !cmd" and ":write !cmd".
2583 */
2584 if ((ea.argt & TRLBAR) && !ea.usefilter)
2585 separate_nextcmd(&ea);
2586
2587 /*
2588 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002589 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2590 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002592 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002593 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002594 || ea.cmdidx == CMD_global
2595 || ea.cmdidx == CMD_vglobal
2596 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 {
2598 for (p = ea.arg; *p; ++p)
2599 {
2600 /* Remove one backslash before a newline, so that it's possible to
2601 * pass a newline to the shell and also a newline that is preceded
2602 * with a backslash. This makes it impossible to end a shell
2603 * command in a backslash, but that doesn't appear useful.
2604 * Halving the number of backslashes is incompatible with previous
2605 * versions. */
2606 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002607 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 else if (*p == '\n')
2609 {
2610 ea.nextcmd = p + 1;
2611 *p = NUL;
2612 break;
2613 }
2614 }
2615 }
2616
2617 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2618 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002619 buf_T *buf;
2620
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002622 switch (ea.addr_type)
2623 {
2624 case ADDR_LINES:
2625 ea.line2 = curbuf->b_ml.ml_line_count;
2626 break;
2627 case ADDR_LOADED_BUFFERS:
2628 buf = firstbuf;
2629 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2630 buf = buf->b_next;
2631 ea.line1 = buf->b_fnum;
2632 buf = lastbuf;
2633 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2634 buf = buf->b_prev;
2635 ea.line2 = buf->b_fnum;
2636 break;
2637 case ADDR_BUFFERS:
2638 ea.line1 = firstbuf->b_fnum;
2639 ea.line2 = lastbuf->b_fnum;
2640 break;
2641 case ADDR_WINDOWS:
2642 ea.line2 = LAST_WIN_NR;
2643 break;
2644 case ADDR_TABS:
2645 ea.line2 = LAST_TAB_NR;
2646 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002647 case ADDR_TABS_RELATIVE:
2648 ea.line2 = 1;
2649 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002650 case ADDR_ARGUMENTS:
2651 if (ARGCOUNT == 0)
2652 ea.line1 = ea.line2 = 0;
2653 else
2654 ea.line2 = ARGCOUNT;
2655 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002656#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002657 case ADDR_QUICKFIX:
2658 ea.line2 = qf_get_size(&ea);
2659 if (ea.line2 == 0)
2660 ea.line2 = 1;
2661 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002662#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 }
2665
2666 /* accept numbered register only when no count allowed (:put) */
2667 if ( (ea.argt & REGSTR)
2668 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002669 /* Do not allow register = for user commands */
2670 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2672 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002673#ifndef FEAT_CLIPBOARD
2674 /* check these explicitly for a more specific error message */
2675 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002677 errormsg = (char_u *)_(e_invalidreg);
2678 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 }
2680#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002681 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2682 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002683 {
2684 ea.regname = *ea.arg++;
2685#ifdef FEAT_EVAL
2686 /* for '=' register: accept the rest of the line as an expression */
2687 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2688 {
2689 set_expr_line(vim_strsave(ea.arg));
2690 ea.arg += STRLEN(ea.arg);
2691 }
2692#endif
2693 ea.arg = skipwhite(ea.arg);
2694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
2696
2697 /*
2698 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2699 * count, it's a buffer name.
2700 */
2701 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2702 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002703 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 {
2705 n = getdigits(&ea.arg);
2706 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002707 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
2709 errormsg = (char_u *)_(e_zerocount);
2710 goto doend;
2711 }
2712 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2713 {
2714 ea.line2 = n;
2715 if (ea.addr_count == 0)
2716 ea.addr_count = 1;
2717 }
2718 else
2719 {
2720 ea.line1 = ea.line2;
2721 ea.line2 += n - 1;
2722 ++ea.addr_count;
2723 /*
2724 * Be vi compatible: no error message for out of range.
2725 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002726 if (ea.addr_type == ADDR_LINES
2727 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 ea.line2 = curbuf->b_ml.ml_line_count;
2729 }
2730 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002731
2732 /*
2733 * Check for flags: 'l', 'p' and '#'.
2734 */
2735 if (ea.argt & EXFLAGS)
2736 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002738 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002739 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
2741 errormsg = (char_u *)_(e_trailing);
2742 goto doend;
2743 }
2744
2745 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2746 {
2747 errormsg = (char_u *)_(e_argreq);
2748 goto doend;
2749 }
2750
2751#ifdef FEAT_EVAL
2752 /*
2753 * Skip the command when it's not going to be executed.
2754 * The commands like :if, :endif, etc. always need to be executed.
2755 * Also make an exception for commands that handle a trailing command
2756 * themselves.
2757 */
2758 if (ea.skip)
2759 {
2760 switch (ea.cmdidx)
2761 {
2762 /* commands that need evaluation */
2763 case CMD_while:
2764 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002765 case CMD_for:
2766 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 case CMD_if:
2768 case CMD_elseif:
2769 case CMD_else:
2770 case CMD_endif:
2771 case CMD_try:
2772 case CMD_catch:
2773 case CMD_finally:
2774 case CMD_endtry:
2775 case CMD_function:
2776 break;
2777
2778 /* Commands that handle '|' themselves. Check: A command should
2779 * either have the TRLBAR flag, appear in this list or appear in
2780 * the list at ":help :bar". */
2781 case CMD_aboveleft:
2782 case CMD_and:
2783 case CMD_belowright:
2784 case CMD_botright:
2785 case CMD_browse:
2786 case CMD_call:
2787 case CMD_confirm:
2788 case CMD_delfunction:
2789 case CMD_djump:
2790 case CMD_dlist:
2791 case CMD_dsearch:
2792 case CMD_dsplit:
2793 case CMD_echo:
2794 case CMD_echoerr:
2795 case CMD_echomsg:
2796 case CMD_echon:
2797 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002798 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 case CMD_help:
2800 case CMD_hide:
2801 case CMD_ijump:
2802 case CMD_ilist:
2803 case CMD_isearch:
2804 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002805 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 case CMD_keepjumps:
2807 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002808 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 case CMD_leftabove:
2810 case CMD_let:
2811 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002812 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002814 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002815 case CMD_noautocmd:
2816 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 case CMD_perl:
2818 case CMD_psearch:
2819 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002820 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002821 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 case CMD_return:
2823 case CMD_rightbelow:
2824 case CMD_ruby:
2825 case CMD_silent:
2826 case CMD_smagic:
2827 case CMD_snomagic:
2828 case CMD_substitute:
2829 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002830 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 case CMD_tcl:
2832 case CMD_throw:
2833 case CMD_tilde:
2834 case CMD_topleft:
2835 case CMD_unlet:
2836 case CMD_verbose:
2837 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002838 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 break;
2840
2841 default: goto doend;
2842 }
2843 }
2844#endif
2845
2846 if (ea.argt & XFILE)
2847 {
2848 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2849 goto doend;
2850 }
2851
2852#ifdef FEAT_LISTCMDS
2853 /*
2854 * Accept buffer name. Cannot be used at the same time with a buffer
2855 * number. Don't do this for a user command.
2856 */
2857 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002858 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
2860 /*
2861 * :bdelete, :bwipeout and :bunload take several arguments, separated
2862 * by spaces: find next space (skipping over escaped characters).
2863 * The others take one argument: ignore trailing spaces.
2864 */
2865 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2866 || ea.cmdidx == CMD_bunload)
2867 p = skiptowhite_esc(ea.arg);
2868 else
2869 {
2870 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002871 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 --p;
2873 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002874 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2875 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 if (ea.line2 < 0) /* failed */
2877 goto doend;
2878 ea.addr_count = 1;
2879 ea.arg = skipwhite(p);
2880 }
2881#endif
2882
Bram Moolenaar2be57332018-02-13 18:05:18 +01002883 /* The :try command saves the emsg_silent flag, reset it here when
2884 * ":silent! try" was used, it should only apply to :try itself. */
2885 if (ea.cmdidx == CMD_try && did_esilent > 0)
2886 {
2887 emsg_silent -= did_esilent;
2888 if (emsg_silent < 0)
2889 emsg_silent = 0;
2890 did_esilent = 0;
2891 }
2892
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002894 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 *
2896 * The "ea" structure holds the arguments that can be used.
2897 */
2898 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002899 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 ea.cookie = cookie;
2901#ifdef FEAT_EVAL
2902 ea.cstack = cstack;
2903#endif
2904
2905#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002906 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
2908 /*
2909 * Execute a user-defined command.
2910 */
2911 do_ucmd(&ea);
2912 }
2913 else
2914#endif
2915 {
2916 /*
2917 * Call the function to execute the command.
2918 */
2919 ea.errmsg = NULL;
2920 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2921 if (ea.errmsg != NULL)
2922 errormsg = (char_u *)_(ea.errmsg);
2923 }
2924
2925#ifdef FEAT_EVAL
2926 /*
2927 * If the command just executed called do_cmdline(), any throw or ":return"
2928 * or ":finish" encountered there must also check the cstack of the still
2929 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2930 * exception, or reanimate a returned function or finished script file and
2931 * return or finish it again.
2932 */
2933 if (need_rethrow)
2934 do_throw(cstack);
2935 else if (check_cstack)
2936 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002937 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002939 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 && current_func_returned())
2941 do_return(&ea, TRUE, FALSE, NULL);
2942 }
2943 need_rethrow = check_cstack = FALSE;
2944#endif
2945
2946doend:
2947 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002948 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002950 curwin->w_cursor.col = 0;
2951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952
2953 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2954 {
2955 if (sourcing)
2956 {
2957 if (errormsg != IObuff)
2958 {
2959 STRCPY(IObuff, errormsg);
2960 errormsg = IObuff;
2961 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002962 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 }
2964 emsg(errormsg);
2965 }
2966#ifdef FEAT_EVAL
2967 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002968 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2969 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970#endif
2971
2972 if (verbose_save >= 0)
2973 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002974#ifdef FEAT_AUTOCMD
2975 if (cmdmod.save_ei != NULL)
2976 {
2977 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002978 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2979 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002980 free_string_option(cmdmod.save_ei);
2981 }
2982#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002983 if (cmdmod.filter_regmatch.regprog != NULL)
2984 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
2986 cmdmod = save_cmdmod;
2987
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002988 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 {
2990 /* messages could be enabled for a serious error, need to check if the
2991 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002992 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002993 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 emsg_silent -= did_esilent;
2995 if (emsg_silent < 0)
2996 emsg_silent = 0;
2997 /* Restore msg_scroll, it's set by file I/O commands, even when no
2998 * message is actually displayed. */
2999 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003000
3001 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3002 * somewhere in the line. Put it back in the first column. */
3003 if (redirecting())
3004 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 }
3006
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003007#ifdef HAVE_SANDBOX
3008 if (did_sandbox)
3009 --sandbox;
3010#endif
3011
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3013 ea.nextcmd = NULL;
3014
3015#ifdef FEAT_EVAL
3016 --ex_nesting_level;
3017#endif
3018
3019 return ea.nextcmd;
3020}
3021#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003022 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023#endif
3024
3025/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003026 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 * If there is a match advance "pp" to the argument and return TRUE.
3028 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003029 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003030checkforcmd(
3031 char_u **pp, /* start of command */
3032 char *cmd, /* name of command */
3033 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034{
3035 int i;
3036
3037 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003038 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 break;
3040 if (i >= len && !isalpha((*pp)[i]))
3041 {
3042 *pp = skipwhite(*pp + i);
3043 return TRUE;
3044 }
3045 return FALSE;
3046}
3047
3048/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003049 * Append "cmd" to the error message in IObuff.
3050 * Takes care of limiting the length and handling 0xa0, which would be
3051 * invisible otherwise.
3052 */
3053 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003054append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003055{
3056 char_u *s = cmd;
3057 char_u *d;
3058
3059 STRCAT(IObuff, ": ");
3060 d = IObuff + STRLEN(IObuff);
3061 while (*s != NUL && d - IObuff < IOSIZE - 7)
3062 {
3063 if (
3064#ifdef FEAT_MBYTE
3065 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3066#endif
3067 *s == 0xa0)
3068 {
3069 s +=
3070#ifdef FEAT_MBYTE
3071 enc_utf8 ? 2 :
3072#endif
3073 1;
3074 STRCPY(d, "<a0>");
3075 d += 4;
3076 }
3077 else
3078 MB_COPY_CHAR(s, d);
3079 }
3080 *d = NUL;
3081}
3082
3083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003085 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003087 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 * Returns NULL for an ambiguous user command.
3089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003091find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 int len;
3094 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003095 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096
3097 /*
3098 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003099 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 * - the 'k' command can directly be followed by any character.
3101 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003102 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003104 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 */
3106 p = eap->cmd;
3107 if (*p == 'k')
3108 {
3109 eap->cmdidx = CMD_k;
3110 ++p;
3111 }
3112 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003113 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3114 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 || p[1] == 'g'
3116 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3117 || p[1] == 'I'
3118 || (p[1] == 'r' && p[2] != 'e')))
3119 {
3120 eap->cmdidx = CMD_substitute;
3121 ++p;
3122 }
3123 else
3124 {
3125 while (ASCII_ISALPHA(*p))
3126 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003127 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003128 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003129 while (ASCII_ISALNUM(*p))
3130 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 /* check for non-alpha command */
3133 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3134 ++p;
3135 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003136 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3137 {
3138 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3139 * :delete with the 'l' flag. Same for 'p'. */
3140 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003141 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003142 break;
3143 if (i == len - 1)
3144 {
3145 --len;
3146 if (p[-1] == 'l')
3147 eap->flags |= EXFLAG_LIST;
3148 else
3149 eap->flags |= EXFLAG_PRINT;
3150 }
3151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003153 if (ASCII_ISLOWER(eap->cmd[0]))
3154 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003155 int c1 = eap->cmd[0];
3156 int c2 = eap->cmd[1];
3157
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003158 if (command_count != (int)CMD_SIZE)
3159 {
3160 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3161 getout(1);
3162 }
3163
3164 /* Use a precomputed index for fast look-up in cmdnames[]
3165 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003166 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3167 if (ASCII_ISLOWER(c2))
3168 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003171 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
3173 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3174 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3175 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3176 (size_t)len) == 0)
3177 {
3178#ifdef FEAT_EVAL
3179 if (full != NULL
3180 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3181 *full = TRUE;
3182#endif
3183 break;
3184 }
3185
3186#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003187 /* Look for a user defined command as a last resort. Let ":Print" be
3188 * overruled by a user defined command. */
3189 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3190 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003192 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 while (ASCII_ISALNUM(*p))
3194 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003195 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 }
3197#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003198 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 eap->cmdidx = CMD_SIZE;
3200 }
3201
3202 return p;
3203}
3204
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003205#ifdef FEAT_USR_CMDS
3206/*
3207 * Search for a user command that matches "eap->cmd".
3208 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3209 * Return a pointer to just after the command.
3210 * Return NULL if there is no matching command.
3211 */
3212 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003213find_ucmd(
3214 exarg_T *eap,
3215 char_u *p, /* end of the command (possibly including count) */
3216 int *full, /* set to TRUE for a full match */
3217 expand_T *xp, /* used for completion, NULL otherwise */
3218 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003219{
3220 int len = (int)(p - eap->cmd);
3221 int j, k, matchlen = 0;
3222 ucmd_T *uc;
3223 int found = FALSE;
3224 int possible = FALSE;
3225 char_u *cp, *np; /* Point into typed cmd and test name */
3226 garray_T *gap;
3227 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3228 only full match global is accepted. */
3229
3230 /*
3231 * Look for buffer-local user commands first, then global ones.
3232 */
3233 gap = &curbuf->b_ucmds;
3234 for (;;)
3235 {
3236 for (j = 0; j < gap->ga_len; ++j)
3237 {
3238 uc = USER_CMD_GA(gap, j);
3239 cp = eap->cmd;
3240 np = uc->uc_name;
3241 k = 0;
3242 while (k < len && *np != NUL && *cp++ == *np++)
3243 k++;
3244 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3245 {
3246 /* If finding a second match, the command is ambiguous. But
3247 * not if a buffer-local command wasn't a full match and a
3248 * global command is a full match. */
3249 if (k == len && found && *np != NUL)
3250 {
3251 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003252 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003253 amb_local = TRUE;
3254 }
3255
3256 if (!found || (k == len && *np == NUL))
3257 {
3258 /* If we matched up to a digit, then there could
3259 * be another command including the digit that we
3260 * should use instead.
3261 */
3262 if (k == len)
3263 found = TRUE;
3264 else
3265 possible = TRUE;
3266
3267 if (gap == &ucmds)
3268 eap->cmdidx = CMD_USER;
3269 else
3270 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003271 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003272 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003273 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003274
3275# ifdef FEAT_CMDL_COMPL
3276 if (compl != NULL)
3277 *compl = uc->uc_compl;
3278# ifdef FEAT_EVAL
3279 if (xp != NULL)
3280 {
3281 xp->xp_arg = uc->uc_compl_arg;
3282 xp->xp_scriptID = uc->uc_scriptID;
3283 }
3284# endif
3285# endif
3286 /* Do not search for further abbreviations
3287 * if this is an exact match. */
3288 matchlen = k;
3289 if (k == len && *np == NUL)
3290 {
3291 if (full != NULL)
3292 *full = TRUE;
3293 amb_local = FALSE;
3294 break;
3295 }
3296 }
3297 }
3298 }
3299
3300 /* Stop if we found a full match or searched all. */
3301 if (j < gap->ga_len || gap == &ucmds)
3302 break;
3303 gap = &ucmds;
3304 }
3305
3306 /* Only found ambiguous matches. */
3307 if (amb_local)
3308 {
3309 if (xp != NULL)
3310 xp->xp_context = EXPAND_UNSUCCESSFUL;
3311 return NULL;
3312 }
3313
3314 /* The match we found may be followed immediately by a number. Move "p"
3315 * back to point to it. */
3316 if (found || possible)
3317 return p + (matchlen - len);
3318 return p;
3319}
3320#endif
3321
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003323static struct cmdmod
3324{
3325 char *name;
3326 int minlen;
3327 int has_count; /* :123verbose :3tab */
3328} cmdmods[] = {
3329 {"aboveleft", 3, FALSE},
3330 {"belowright", 3, FALSE},
3331 {"botright", 2, FALSE},
3332 {"browse", 3, FALSE},
3333 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003334 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003335 {"hide", 3, FALSE},
3336 {"keepalt", 5, FALSE},
3337 {"keepjumps", 5, FALSE},
3338 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003339 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003340 {"leftabove", 5, FALSE},
3341 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003342 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003343 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003344 {"rightbelow", 6, FALSE},
3345 {"sandbox", 3, FALSE},
3346 {"silent", 3, FALSE},
3347 {"tab", 3, TRUE},
3348 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003349 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003350 {"verbose", 4, TRUE},
3351 {"vertical", 4, FALSE},
3352};
3353
3354/*
3355 * Return length of a command modifier (including optional count).
3356 * Return zero when it's not a modifier.
3357 */
3358 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003359modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003360{
3361 int i, j;
3362 char_u *p = cmd;
3363
3364 if (VIM_ISDIGIT(*cmd))
3365 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003366 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003367 {
3368 for (j = 0; p[j] != NUL; ++j)
3369 if (p[j] != cmdmods[i].name[j])
3370 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003371 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003372 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003373 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003374 }
3375 return 0;
3376}
3377
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378/*
3379 * Return > 0 if an Ex command "name" exists.
3380 * Return 2 if there is an exact match.
3381 * Return 3 if there is an ambiguous match.
3382 */
3383 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003384cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
3386 exarg_T ea;
3387 int full = FALSE;
3388 int i;
3389 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003390 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
3392 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003393 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
3395 for (j = 0; name[j] != NUL; ++j)
3396 if (name[j] != cmdmods[i].name[j])
3397 break;
3398 if (name[j] == NUL && j >= cmdmods[i].minlen)
3399 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3400 }
3401
Bram Moolenaara9587612006-05-04 21:47:50 +00003402 /* Check built-in commands and user defined commands.
3403 * For ":2match" and ":3match" we need to skip the number. */
3404 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003406 p = find_command(&ea, &full);
3407 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003409 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3410 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003411 if (*skipwhite(p) != NUL)
3412 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3414}
3415#endif
3416
3417/*
3418 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3419 * we don't need/want deleted. Maybe this could be done better if we didn't
3420 * repeat all this stuff. The only problem is that they may not stay
3421 * perfectly compatible with each other, but then the command line syntax
3422 * probably won't change that much -- webb.
3423 */
3424 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003425set_one_cmd_context(
3426 expand_T *xp,
3427 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
3429 char_u *p;
3430 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003431 int len = 0;
3432 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3434 int compl = EXPAND_NOTHING;
3435#endif
3436#ifdef FEAT_CMDL_COMPL
3437 int delim;
3438#endif
3439 int forceit = FALSE;
3440 int usefilter = FALSE; /* filter instead of file name */
3441
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003442 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 xp->xp_pattern = buff;
3444 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003445 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003448 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 */
3450 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3451 ;
3452 xp->xp_pattern = cmd;
3453
3454 if (*cmd == NUL)
3455 return NULL;
3456 if (*cmd == '"') /* ignore comment lines */
3457 {
3458 xp->xp_context = EXPAND_NOTHING;
3459 return NULL;
3460 }
3461
3462/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003463 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 */
3465 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 xp->xp_pattern = cmd;
3467 if (*cmd == NUL)
3468 return NULL;
3469 if (*cmd == '"')
3470 {
3471 xp->xp_context = EXPAND_NOTHING;
3472 return NULL;
3473 }
3474
3475 if (*cmd == '|' || *cmd == '\n')
3476 return cmd + 1; /* There's another command */
3477
3478 /*
3479 * Isolate the command and search for it in the command table.
3480 * Exceptions:
3481 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003482 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3484 */
3485 if (*cmd == 'k' && cmd[1] != 'e')
3486 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003487 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 p = cmd + 1;
3489 }
3490 else
3491 {
3492 p = cmd;
3493 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3494 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003495 /* a user command may contain digits */
3496 if (ASCII_ISUPPER(cmd[0]))
3497 while (ASCII_ISALNUM(*p) || *p == '*')
3498 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003499 /* for python 3.x: ":py3*" commands completion */
3500 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003501 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003502 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003503 while (ASCII_ISALPHA(*p) || *p == '*')
3504 ++p;
3505 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003506 /* check for non-alpha command */
3507 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3508 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003509 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003511 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
3513 xp->xp_context = EXPAND_UNSUCCESSFUL;
3514 return NULL;
3515 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003516 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003517 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3518 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3519 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 break;
3521
3522#ifdef FEAT_USR_CMDS
3523 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3525 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526#endif
3527 }
3528
3529 /*
3530 * If the cursor is touching the command, and it ends in an alpha-numeric
3531 * character, complete the command name.
3532 */
3533 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3534 return NULL;
3535
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003536 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 {
3538 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3539 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003540 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 p = cmd + 1;
3542 }
3543#ifdef FEAT_USR_CMDS
3544 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3545 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003546 ea.cmd = cmd;
3547 p = find_ucmd(&ea, p, NULL, xp,
3548# if defined(FEAT_CMDL_COMPL)
3549 &compl
3550# else
3551 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003553 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003554 if (p == NULL)
3555 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557#endif
3558 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003559 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
3561 /* Not still touching the command and it was an illegal one */
3562 xp->xp_context = EXPAND_UNSUCCESSFUL;
3563 return NULL;
3564 }
3565
3566 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3567
3568 if (*p == '!') /* forced commands */
3569 {
3570 forceit = TRUE;
3571 ++p;
3572 }
3573
3574/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003575 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003577 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003578 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
3580 arg = skipwhite(p);
3581
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003582 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 if (*arg == '>') /* append */
3585 {
3586 if (*++arg == '>')
3587 ++arg;
3588 arg = skipwhite(arg);
3589 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003590 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
3592 ++arg;
3593 usefilter = TRUE;
3594 }
3595 }
3596
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003597 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
3599 usefilter = forceit; /* :r! filter if forced */
3600 if (*arg == '!') /* :r !filter */
3601 {
3602 ++arg;
3603 usefilter = TRUE;
3604 }
3605 }
3606
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003607 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
3609 while (*arg == *cmd) /* allow any number of '>' or '<' */
3610 ++arg;
3611 arg = skipwhite(arg);
3612 }
3613
3614 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003615 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
3617 /* Check if we're in the +command */
3618 p = arg + 1;
3619 arg = skip_cmd_arg(arg, FALSE);
3620
3621 /* Still touching the command after '+'? */
3622 if (*arg == NUL)
3623 return p;
3624
3625 /* Skip space(s) after +command to get to the real argument */
3626 arg = skipwhite(arg);
3627 }
3628
3629 /*
3630 * Check for '|' to separate commands and '"' to start comments.
3631 * Don't do this for ":read !cmd" and ":write !cmd".
3632 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003633 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
3635 p = arg;
3636 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003637 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 p += 2;
3639 while (*p)
3640 {
3641 if (*p == Ctrl_V)
3642 {
3643 if (p[1] != NUL)
3644 ++p;
3645 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003646 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 || *p == '|' || *p == '\n')
3648 {
3649 if (*(p - 1) != '\\')
3650 {
3651 if (*p == '|' || *p == '\n')
3652 return p + 1;
3653 return NULL; /* It's a comment */
3654 }
3655 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003656 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
3658 }
3659
3660 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003661 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 vim_strchr((char_u *)"|\"", *arg) == NULL)
3663 return NULL;
3664
3665 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003666 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003668 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003669 while (*p && p < buff + len)
3670 {
3671 if (*p == ' ' || *p == TAB)
3672 {
3673 /* argument starts after a space */
3674 xp->xp_pattern = ++p;
3675 }
3676 else
3677 {
3678 if (*p == '\\' && *(p + 1) != NUL)
3679 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003680 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003681 }
3682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003684 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003686 int c;
3687 int in_quote = FALSE;
3688 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689
3690 /*
3691 * Allow spaces within back-quotes to count as part of the argument
3692 * being expanded.
3693 */
3694 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003695 p = xp->xp_pattern;
3696 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003698#ifdef FEAT_MBYTE
3699 if (has_mbyte)
3700 c = mb_ptr2char(p);
3701 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003703 c = *p;
3704 if (c == '\\' && p[1] != NUL)
3705 ++p;
3706 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 {
3708 if (!in_quote)
3709 {
3710 xp->xp_pattern = p;
3711 bow = p + 1;
3712 }
3713 in_quote = !in_quote;
3714 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003715 /* An argument can contain just about everything, except
3716 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003717 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003718#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003719 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003720#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003721 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003722 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003723 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003724 while (*p != NUL)
3725 {
3726#ifdef FEAT_MBYTE
3727 if (has_mbyte)
3728 c = mb_ptr2char(p);
3729 else
3730#endif
3731 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003732 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003733 break;
3734#ifdef FEAT_MBYTE
3735 if (has_mbyte)
3736 len = (*mb_ptr2len)(p);
3737 else
3738#endif
3739 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003740 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003741 }
3742 if (in_quote)
3743 bow = p;
3744 else
3745 xp->xp_pattern = p;
3746 p -= len;
3747 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003748 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
3750
3751 /*
3752 * If we are still inside the quotes, and we passed a space, just
3753 * expand from there.
3754 */
3755 if (bow != NULL && in_quote)
3756 xp->xp_pattern = bow;
3757 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003758
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003759 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003760 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003761 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003762#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003763 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003764#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003765 /* When still after the command name expand executables. */
3766 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003767 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
3770 /* Check for environment variable */
3771 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003772#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 || *xp->xp_pattern == '%'
3774#endif
3775 )
3776 {
3777 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3778 if (!vim_isIDc(*p))
3779 break;
3780 if (*p == NUL)
3781 {
3782 xp->xp_context = EXPAND_ENV_VARS;
3783 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003784#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3785 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003786 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003787 compl = EXPAND_ENV_VARS;
3788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003791#if defined(FEAT_CMDL_COMPL)
3792 /* Check for user names */
3793 if (*xp->xp_pattern == '~')
3794 {
3795 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3796 ;
3797 /* Complete ~user only if it partially matches a user name.
3798 * A full match ~user<Tab> will be replaced by user's home
3799 * directory i.e. something like ~user<Tab> -> /home/user/ */
3800 if (*p == NUL && p > xp->xp_pattern + 1
3801 && match_user(xp->xp_pattern + 1) == 1)
3802 {
3803 xp->xp_context = EXPAND_USER;
3804 ++xp->xp_pattern;
3805 }
3806 }
3807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809
3810/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003811 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003813 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003815 case CMD_find:
3816 case CMD_sfind:
3817 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003818 if (xp->xp_context == EXPAND_FILES)
3819 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003820 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 case CMD_cd:
3822 case CMD_chdir:
3823 case CMD_lcd:
3824 case CMD_lchdir:
3825 if (xp->xp_context == EXPAND_FILES)
3826 xp->xp_context = EXPAND_DIRECTORIES;
3827 break;
3828 case CMD_help:
3829 xp->xp_context = EXPAND_HELP;
3830 xp->xp_pattern = arg;
3831 break;
3832
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003833 /* Command modifiers: return the argument.
3834 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003836 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 case CMD_belowright:
3838 case CMD_botright:
3839 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003840 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003841 case CMD_cdo:
3842 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003844 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 case CMD_folddoclosed:
3846 case CMD_folddoopen:
3847 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003848 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 case CMD_keepjumps:
3850 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003851 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003852 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003854 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003856 case CMD_noautocmd:
3857 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003859 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003861 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003862 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 case CMD_topleft:
3864 case CMD_verbose:
3865 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003866 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 return arg;
3868
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003869 case CMD_filter:
3870 if (*arg != NUL)
3871 arg = skip_vimgrep_pat(arg, NULL, NULL);
3872 if (arg == NULL || *arg == NUL)
3873 {
3874 xp->xp_context = EXPAND_NOTHING;
3875 return NULL;
3876 }
3877 return skipwhite(arg);
3878
Bram Moolenaar4f688582007-07-24 12:34:30 +00003879#ifdef FEAT_CMDL_COMPL
3880# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 case CMD_match:
3882 if (*arg == NUL || !ends_excmd(*arg))
3883 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003884 /* also complete "None" */
3885 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 arg = skipwhite(skiptowhite(arg));
3887 if (*arg != NUL)
3888 {
3889 xp->xp_context = EXPAND_NOTHING;
3890 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3891 }
3892 }
3893 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003894# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896/*
3897 * All completion for the +cmdline_compl feature goes here.
3898 */
3899
3900# ifdef FEAT_USR_CMDS
3901 case CMD_command:
3902 /* Check for attributes */
3903 while (*arg == '-')
3904 {
3905 arg++; /* Skip "-" */
3906 p = skiptowhite(arg);
3907 if (*p == NUL)
3908 {
3909 /* Cursor is still in the attribute */
3910 p = vim_strchr(arg, '=');
3911 if (p == NULL)
3912 {
3913 /* No "=", so complete attribute names */
3914 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3915 xp->xp_pattern = arg;
3916 return NULL;
3917 }
3918
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003919 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 * their arguments as well.
3921 */
3922 if (STRNICMP(arg, "complete", p - arg) == 0)
3923 {
3924 xp->xp_context = EXPAND_USER_COMPLETE;
3925 xp->xp_pattern = p + 1;
3926 return NULL;
3927 }
3928 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3929 {
3930 xp->xp_context = EXPAND_USER_NARGS;
3931 xp->xp_pattern = p + 1;
3932 return NULL;
3933 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003934 else if (STRNICMP(arg, "addr", p - arg) == 0)
3935 {
3936 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3937 xp->xp_pattern = p + 1;
3938 return NULL;
3939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 return NULL;
3941 }
3942 arg = skipwhite(p);
3943 }
3944
3945 /* After the attributes comes the new command name */
3946 p = skiptowhite(arg);
3947 if (*p == NUL)
3948 {
3949 xp->xp_context = EXPAND_USER_COMMANDS;
3950 xp->xp_pattern = arg;
3951 break;
3952 }
3953
3954 /* And finally comes a normal command */
3955 return skipwhite(p);
3956
3957 case CMD_delcommand:
3958 xp->xp_context = EXPAND_USER_COMMANDS;
3959 xp->xp_pattern = arg;
3960 break;
3961# endif
3962
3963 case CMD_global:
3964 case CMD_vglobal:
3965 delim = *arg; /* get the delimiter */
3966 if (delim)
3967 ++arg; /* skip delimiter if there is one */
3968
3969 while (arg[0] != NUL && arg[0] != delim)
3970 {
3971 if (arg[0] == '\\' && arg[1] != NUL)
3972 ++arg;
3973 ++arg;
3974 }
3975 if (arg[0] != NUL)
3976 return arg + 1;
3977 break;
3978 case CMD_and:
3979 case CMD_substitute:
3980 delim = *arg;
3981 if (delim)
3982 {
3983 /* skip "from" part */
3984 ++arg;
3985 arg = skip_regexp(arg, delim, p_magic, NULL);
3986 }
3987 /* skip "to" part */
3988 while (arg[0] != NUL && arg[0] != delim)
3989 {
3990 if (arg[0] == '\\' && arg[1] != NUL)
3991 ++arg;
3992 ++arg;
3993 }
3994 if (arg[0] != NUL) /* skip delimiter */
3995 ++arg;
3996 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3997 ++arg;
3998 if (arg[0] != NUL)
3999 return arg;
4000 break;
4001 case CMD_isearch:
4002 case CMD_dsearch:
4003 case CMD_ilist:
4004 case CMD_dlist:
4005 case CMD_ijump:
4006 case CMD_psearch:
4007 case CMD_djump:
4008 case CMD_isplit:
4009 case CMD_dsplit:
4010 arg = skipwhite(skipdigits(arg)); /* skip count */
4011 if (*arg == '/') /* Match regexp, not just whole words */
4012 {
4013 for (++arg; *arg && *arg != '/'; arg++)
4014 if (*arg == '\\' && arg[1] != NUL)
4015 arg++;
4016 if (*arg)
4017 {
4018 arg = skipwhite(arg + 1);
4019
4020 /* Check for trailing illegal characters */
4021 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4022 xp->xp_context = EXPAND_NOTHING;
4023 else
4024 return arg;
4025 }
4026 }
4027 break;
4028#ifdef FEAT_AUTOCMD
4029 case CMD_autocmd:
4030 return set_context_in_autocmd(xp, arg, FALSE);
4031
4032 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004033 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return set_context_in_autocmd(xp, arg, TRUE);
4035#endif
4036 case CMD_set:
4037 set_context_in_set_cmd(xp, arg, 0);
4038 break;
4039 case CMD_setglobal:
4040 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4041 break;
4042 case CMD_setlocal:
4043 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4044 break;
4045 case CMD_tag:
4046 case CMD_stag:
4047 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004048 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 case CMD_tselect:
4050 case CMD_stselect:
4051 case CMD_ptselect:
4052 case CMD_tjump:
4053 case CMD_stjump:
4054 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004055 if (*p_wop != NUL)
4056 xp->xp_context = EXPAND_TAGS_LISTFILES;
4057 else
4058 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 xp->xp_pattern = arg;
4060 break;
4061 case CMD_augroup:
4062 xp->xp_context = EXPAND_AUGROUP;
4063 xp->xp_pattern = arg;
4064 break;
4065#ifdef FEAT_SYN_HL
4066 case CMD_syntax:
4067 set_context_in_syntax_cmd(xp, arg);
4068 break;
4069#endif
4070#ifdef FEAT_EVAL
4071 case CMD_let:
4072 case CMD_if:
4073 case CMD_elseif:
4074 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004075 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 case CMD_echo:
4077 case CMD_echon:
4078 case CMD_execute:
4079 case CMD_echomsg:
4080 case CMD_echoerr:
4081 case CMD_call:
4082 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004083 case CMD_cexpr:
4084 case CMD_caddexpr:
4085 case CMD_cgetexpr:
4086 case CMD_lexpr:
4087 case CMD_laddexpr:
4088 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004089 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 break;
4091
4092 case CMD_unlet:
4093 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4094 arg = xp->xp_pattern + 1;
4095 xp->xp_context = EXPAND_USER_VARS;
4096 xp->xp_pattern = arg;
4097 break;
4098
4099 case CMD_function:
4100 case CMD_delfunction:
4101 xp->xp_context = EXPAND_USER_FUNC;
4102 xp->xp_pattern = arg;
4103 break;
4104
4105 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004106 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 break;
4108#endif
4109 case CMD_highlight:
4110 set_context_in_highlight_cmd(xp, arg);
4111 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004112#ifdef FEAT_CSCOPE
4113 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004114 case CMD_lcscope:
4115 case CMD_scscope:
4116 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004117 break;
4118#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004119#ifdef FEAT_SIGNS
4120 case CMD_sign:
4121 set_context_in_sign_cmd(xp, arg);
4122 break;
4123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124#ifdef FEAT_LISTCMDS
4125 case CMD_bdelete:
4126 case CMD_bwipeout:
4127 case CMD_bunload:
4128 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4129 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004130 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 case CMD_buffer:
4132 case CMD_sbuffer:
4133 case CMD_checktime:
4134 xp->xp_context = EXPAND_BUFFERS;
4135 xp->xp_pattern = arg;
4136 break;
4137#endif
4138#ifdef FEAT_USR_CMDS
4139 case CMD_USER:
4140 case CMD_USER_BUF:
4141 if (compl != EXPAND_NOTHING)
4142 {
4143 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004144 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
4146# ifdef FEAT_MENU
4147 if (compl == EXPAND_MENUS)
4148 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4149# endif
4150 if (compl == EXPAND_COMMANDS)
4151 return arg;
4152 if (compl == EXPAND_MAPPINGS)
4153 return set_context_in_map_cmd(xp, (char_u *)"map",
4154 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004155 /* Find start of last argument. */
4156 p = arg;
4157 while (*p)
4158 {
4159 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004160 /* argument starts after a space */
4161 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004162 else if (*p == '\\' && *(p + 1) != NUL)
4163 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004164 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 xp->xp_pattern = arg;
4167 }
4168 xp->xp_context = compl;
4169 }
4170 break;
4171#endif
4172 case CMD_map: case CMD_noremap:
4173 case CMD_nmap: case CMD_nnoremap:
4174 case CMD_vmap: case CMD_vnoremap:
4175 case CMD_omap: case CMD_onoremap:
4176 case CMD_imap: case CMD_inoremap:
4177 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004178 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004179 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004180 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004181 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004183 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 case CMD_unmap:
4185 case CMD_nunmap:
4186 case CMD_vunmap:
4187 case CMD_ounmap:
4188 case CMD_iunmap:
4189 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004190 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004191 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004192 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004193 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004195 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004196 case CMD_mapclear:
4197 case CMD_nmapclear:
4198 case CMD_vmapclear:
4199 case CMD_omapclear:
4200 case CMD_imapclear:
4201 case CMD_cmapclear:
4202 case CMD_lmapclear:
4203 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004204 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004205 case CMD_xmapclear:
4206 xp->xp_context = EXPAND_MAPCLEAR;
4207 xp->xp_pattern = arg;
4208 break;
4209
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 case CMD_abbreviate: case CMD_noreabbrev:
4211 case CMD_cabbrev: case CMD_cnoreabbrev:
4212 case CMD_iabbrev: case CMD_inoreabbrev:
4213 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004214 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 case CMD_unabbreviate:
4216 case CMD_cunabbrev:
4217 case CMD_iunabbrev:
4218 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004219 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220#ifdef FEAT_MENU
4221 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4222 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4223 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4224 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4225 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4226 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4227 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4228 case CMD_tmenu: case CMD_tunmenu:
4229 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4230 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4231#endif
4232
4233 case CMD_colorscheme:
4234 xp->xp_context = EXPAND_COLORS;
4235 xp->xp_pattern = arg;
4236 break;
4237
4238 case CMD_compiler:
4239 xp->xp_context = EXPAND_COMPILER;
4240 xp->xp_pattern = arg;
4241 break;
4242
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004243 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004244 xp->xp_context = EXPAND_OWNSYNTAX;
4245 xp->xp_pattern = arg;
4246 break;
4247
4248 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004249 xp->xp_context = EXPAND_FILETYPE;
4250 xp->xp_pattern = arg;
4251 break;
4252
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004253 case CMD_packadd:
4254 xp->xp_context = EXPAND_PACKADD;
4255 xp->xp_pattern = arg;
4256 break;
4257
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4259 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4260 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004261 p = skiptowhite(arg);
4262 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 {
4264 xp->xp_context = EXPAND_LANGUAGE;
4265 xp->xp_pattern = arg;
4266 }
4267 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004268 {
4269 if ( STRNCMP(arg, "messages", p - arg) == 0
4270 || STRNCMP(arg, "ctype", p - arg) == 0
4271 || STRNCMP(arg, "time", p - arg) == 0)
4272 {
4273 xp->xp_context = EXPAND_LOCALES;
4274 xp->xp_pattern = skipwhite(p);
4275 }
4276 else
4277 xp->xp_context = EXPAND_NOTHING;
4278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 break;
4280#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004281#if defined(FEAT_PROFILE)
4282 case CMD_profile:
4283 set_context_in_profile_cmd(xp, arg);
4284 break;
4285#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004286 case CMD_behave:
4287 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004288 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004289 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004291 case CMD_messages:
4292 xp->xp_context = EXPAND_MESSAGES;
4293 xp->xp_pattern = arg;
4294 break;
4295
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004296#if defined(FEAT_CMDHIST)
4297 case CMD_history:
4298 xp->xp_context = EXPAND_HISTORY;
4299 xp->xp_pattern = arg;
4300 break;
4301#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004302#if defined(FEAT_PROFILE)
4303 case CMD_syntime:
4304 xp->xp_context = EXPAND_SYNTIME;
4305 xp->xp_pattern = arg;
4306 break;
4307#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004308
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309#endif /* FEAT_CMDL_COMPL */
4310
4311 default:
4312 break;
4313 }
4314 return NULL;
4315}
4316
4317/*
4318 * skip a range specifier of the form: addr [,addr] [;addr] ..
4319 *
4320 * Backslashed delimiters after / or ? will be skipped, and commands will
4321 * not be expanded between /'s and ?'s or after "'".
4322 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004323 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 * Returns the "cmd" pointer advanced to beyond the range.
4325 */
4326 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004327skip_range(
4328 char_u *cmd,
4329 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004331 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004333 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004335 if (*cmd == '\\')
4336 {
4337 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4338 ++cmd;
4339 else
4340 break;
4341 }
4342 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 {
4344 if (*++cmd == NUL && ctx != NULL)
4345 *ctx = EXPAND_NOTHING;
4346 }
4347 else if (*cmd == '/' || *cmd == '?')
4348 {
4349 delim = *cmd++;
4350 while (*cmd != NUL && *cmd != delim)
4351 if (*cmd++ == '\\' && *cmd != NUL)
4352 ++cmd;
4353 if (*cmd == NUL && ctx != NULL)
4354 *ctx = EXPAND_NOTHING;
4355 }
4356 if (*cmd != NUL)
4357 ++cmd;
4358 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004359
4360 /* Skip ":" and white space. */
4361 while (*cmd == ':')
4362 cmd = skipwhite(cmd + 1);
4363
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 return cmd;
4365}
4366
4367/*
4368 * get a single EX address
4369 *
4370 * Set ptr to the next character after the part that was interpreted.
4371 * Set ptr to NULL when an error is encountered.
4372 *
4373 * Return MAXLNUM when no Ex address was found.
4374 */
4375 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004376get_address(
4377 exarg_T *eap UNUSED,
4378 char_u **ptr,
4379 int addr_type, /* flag: one of ADDR_LINES, ... */
4380 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004381 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004382 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383{
4384 int c;
4385 int i;
4386 long n;
4387 char_u *cmd;
4388 pos_T pos;
4389 pos_T *fp;
4390 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004391 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 cmd = skipwhite(*ptr);
4394 lnum = MAXLNUM;
4395 do
4396 {
4397 switch (*cmd)
4398 {
4399 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004400 ++cmd;
4401 switch (addr_type)
4402 {
4403 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 lnum = curwin->w_cursor.lnum;
4405 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004406 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004407 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004408 break;
4409 case ADDR_ARGUMENTS:
4410 lnum = curwin->w_arg_idx + 1;
4411 break;
4412 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004413 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004414 lnum = curbuf->b_fnum;
4415 break;
4416 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004417 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004418 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004419 case ADDR_TABS_RELATIVE:
4420 EMSG(_(e_invrange));
4421 cmd = NULL;
4422 goto error;
4423 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004424#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004425 case ADDR_QUICKFIX:
4426 lnum = qf_get_cur_valid_idx(eap);
4427 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004428#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 }
4430 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
4432 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 ++cmd;
4434 switch (addr_type)
4435 {
4436 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 lnum = curbuf->b_ml.ml_line_count;
4438 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004439 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004440 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004441 break;
4442 case ADDR_ARGUMENTS:
4443 lnum = ARGCOUNT;
4444 break;
4445 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004446 buf = lastbuf;
4447 while (buf->b_ml.ml_mfp == NULL)
4448 {
4449 if (buf->b_prev == NULL)
4450 break;
4451 buf = buf->b_prev;
4452 }
4453 lnum = buf->b_fnum;
4454 break;
4455 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004456 lnum = lastbuf->b_fnum;
4457 break;
4458 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004459 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004460 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004461 case ADDR_TABS_RELATIVE:
4462 EMSG(_(e_invrange));
4463 cmd = NULL;
4464 goto error;
4465 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004466#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004467 case ADDR_QUICKFIX:
4468 lnum = qf_get_size(eap);
4469 if (lnum == 0)
4470 lnum = 1;
4471 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004472#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004473 }
4474 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475
4476 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004477 if (*++cmd == NUL)
4478 {
4479 cmd = NULL;
4480 goto error;
4481 }
4482 if (addr_type != ADDR_LINES)
4483 {
4484 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004485 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004486 goto error;
4487 }
4488 if (skip)
4489 ++cmd;
4490 else
4491 {
4492 /* Only accept a mark in another file when it is
4493 * used by itself: ":'M". */
4494 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4495 ++cmd;
4496 if (fp == (pos_T *)-1)
4497 /* Jumped to another file. */
4498 lnum = curwin->w_cursor.lnum;
4499 else
4500 {
4501 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 {
4503 cmd = NULL;
4504 goto error;
4505 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004506 lnum = fp->lnum;
4507 }
4508 }
4509 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
4511 case '/':
4512 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004513 c = *cmd++;
4514 if (addr_type != ADDR_LINES)
4515 {
4516 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004517 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004518 goto error;
4519 }
4520 if (skip) /* skip "/pat/" */
4521 {
4522 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4523 if (*cmd == c)
4524 ++cmd;
4525 }
4526 else
4527 {
4528 pos = curwin->w_cursor; /* save curwin->w_cursor */
4529 /*
4530 * When '/' or '?' follows another address, start
4531 * from there.
4532 */
4533 if (lnum != MAXLNUM)
4534 curwin->w_cursor.lnum = lnum;
4535 /*
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004536 * Start a forward search at the end of the line (unless
4537 * before the first line).
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004538 * Start a backward search at the start of the line.
4539 * This makes sure we never match in the current
4540 * line, and can match anywhere in the
4541 * next/previous line.
4542 */
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004543 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004544 curwin->w_cursor.col = MAXCOL;
4545 else
4546 curwin->w_cursor.col = 0;
4547 searchcmdlen = 0;
4548 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004549 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004550 {
4551 curwin->w_cursor = pos;
4552 cmd = NULL;
4553 goto error;
4554 }
4555 lnum = curwin->w_cursor.lnum;
4556 curwin->w_cursor = pos;
4557 /* adjust command string pointer */
4558 cmd += searchcmdlen;
4559 }
4560 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
4562 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004563 ++cmd;
4564 if (addr_type != ADDR_LINES)
4565 {
4566 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004567 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004568 goto error;
4569 }
4570 if (*cmd == '&')
4571 i = RE_SUBST;
4572 else if (*cmd == '?' || *cmd == '/')
4573 i = RE_SEARCH;
4574 else
4575 {
4576 EMSG(_(e_backslash));
4577 cmd = NULL;
4578 goto error;
4579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004581 if (!skip)
4582 {
4583 /*
4584 * When search follows another address, start from
4585 * there.
4586 */
4587 if (lnum != MAXLNUM)
4588 pos.lnum = lnum;
4589 else
4590 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004592 /*
4593 * Start the search just like for the above
4594 * do_search().
4595 */
4596 if (*cmd != '?')
4597 pos.col = MAXCOL;
4598 else
4599 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004600#ifdef FEAT_VIRTUALEDIT
4601 pos.coladd = 0;
4602#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004603 if (searchit(curwin, curbuf, &pos,
4604 *cmd == '?' ? BACKWARD : FORWARD,
4605 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004606 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004607 lnum = pos.lnum;
4608 else
4609 {
4610 cmd = NULL;
4611 goto error;
4612 }
4613 }
4614 ++cmd;
4615 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616
4617 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004618 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4619 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
4621
4622 for (;;)
4623 {
4624 cmd = skipwhite(cmd);
4625 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4626 break;
4627
4628 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004629 {
4630 switch (addr_type)
4631 {
4632 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004633 /* "+1" is same as ".+1" */
4634 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004635 break;
4636 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004637 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004638 break;
4639 case ADDR_ARGUMENTS:
4640 lnum = curwin->w_arg_idx + 1;
4641 break;
4642 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004643 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004644 lnum = curbuf->b_fnum;
4645 break;
4646 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004647 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004648 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004649 case ADDR_TABS_RELATIVE:
4650 lnum = 1;
4651 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004652#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004653 case ADDR_QUICKFIX:
4654 lnum = qf_get_cur_valid_idx(eap);
4655 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004656#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004657 }
4658 }
4659
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (VIM_ISDIGIT(*cmd))
4661 i = '+'; /* "number" is same as "+number" */
4662 else
4663 i = *cmd++;
4664 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4665 n = 1;
4666 else
4667 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004668
4669 if (addr_type == ADDR_TABS_RELATIVE)
4670 {
4671 EMSG(_(e_invrange));
4672 cmd = NULL;
4673 goto error;
4674 }
4675 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004676 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004677 lnum = compute_buffer_local_count(
4678 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004680 {
4681#ifdef FEAT_FOLDING
4682 /* Relative line addressing, need to adjust for folded lines
4683 * now, but only do it after the first address. */
4684 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4685 && address_count >= 2)
4686 (void)hasFolding(lnum, NULL, &lnum);
4687#endif
4688 if (i == '-')
4689 lnum -= n;
4690 else
4691 lnum += n;
4692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
4694 } while (*cmd == '/' || *cmd == '?');
4695
4696error:
4697 *ptr = cmd;
4698 return lnum;
4699}
4700
4701/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004702 * Get flags from an Ex command argument.
4703 */
4704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004705get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004706{
4707 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4708 {
4709 if (*eap->arg == 'l')
4710 eap->flags |= EXFLAG_LIST;
4711 else if (*eap->arg == 'p')
4712 eap->flags |= EXFLAG_PRINT;
4713 else
4714 eap->flags |= EXFLAG_NR;
4715 eap->arg = skipwhite(eap->arg + 1);
4716 }
4717}
4718
4719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 * Function called for command which is Not Implemented. NI!
4721 */
4722 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004723ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724{
4725 if (!eap->skip)
4726 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4727}
4728
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004729#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730/*
4731 * Function called for script command which is Not Implemented. NI!
4732 * Skips over ":perl <<EOF" constructs.
4733 */
4734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004735ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
4737 if (!eap->skip)
4738 ex_ni(eap);
4739 else
4740 vim_free(script_get(eap, eap->arg));
4741}
4742#endif
4743
4744/*
4745 * Check range in Ex command for validity.
4746 * Return NULL when valid, error message when invalid.
4747 */
4748 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004749invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004751 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 if ( eap->line1 < 0
4753 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004754 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004756
4757 if (eap->argt & RANGE)
4758 {
4759 switch(eap->addr_type)
4760 {
4761 case ADDR_LINES:
4762 if (!(eap->argt & NOTADR)
4763 && eap->line2 > curbuf->b_ml.ml_line_count
4764#ifdef FEAT_DIFF
4765 + (eap->cmdidx == CMD_diffget)
4766#endif
4767 )
4768 return (char_u *)_(e_invrange);
4769 break;
4770 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004771 /* add 1 if ARGCOUNT is 0 */
4772 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004773 return (char_u *)_(e_invrange);
4774 break;
4775 case ADDR_BUFFERS:
4776 if (eap->line1 < firstbuf->b_fnum
4777 || eap->line2 > lastbuf->b_fnum)
4778 return (char_u *)_(e_invrange);
4779 break;
4780 case ADDR_LOADED_BUFFERS:
4781 buf = firstbuf;
4782 while (buf->b_ml.ml_mfp == NULL)
4783 {
4784 if (buf->b_next == NULL)
4785 return (char_u *)_(e_invrange);
4786 buf = buf->b_next;
4787 }
4788 if (eap->line1 < buf->b_fnum)
4789 return (char_u *)_(e_invrange);
4790 buf = lastbuf;
4791 while (buf->b_ml.ml_mfp == NULL)
4792 {
4793 if (buf->b_prev == NULL)
4794 return (char_u *)_(e_invrange);
4795 buf = buf->b_prev;
4796 }
4797 if (eap->line2 > buf->b_fnum)
4798 return (char_u *)_(e_invrange);
4799 break;
4800 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004801 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004802 return (char_u *)_(e_invrange);
4803 break;
4804 case ADDR_TABS:
4805 if (eap->line2 > LAST_TAB_NR)
4806 return (char_u *)_(e_invrange);
4807 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004808 case ADDR_TABS_RELATIVE:
4809 /* Do nothing */
4810 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004811#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004812 case ADDR_QUICKFIX:
4813 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4814 return (char_u *)_(e_invrange);
4815 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004816#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004817 }
4818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 return NULL;
4820}
4821
4822/*
4823 * Correct the range for zero line number, if required.
4824 */
4825 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004826correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827{
4828 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4829 {
4830 if (eap->line1 == 0)
4831 eap->line1 = 1;
4832 if (eap->line2 == 0)
4833 eap->line2 = 1;
4834 }
4835}
4836
Bram Moolenaar748bf032005-02-02 23:04:36 +00004837#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004838static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004839
4840/*
4841 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4842 * pattern. Otherwise return eap->arg.
4843 */
4844 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004845skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004846{
4847 char_u *p = eap->arg;
4848
Bram Moolenaara37420f2006-02-04 22:37:47 +00004849 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4850 || eap->cmdidx == CMD_vimgrepadd
4851 || eap->cmdidx == CMD_lvimgrepadd
4852 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004853 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004854 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004855 if (p == NULL)
4856 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004857 }
4858 return p;
4859}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004860
4861/*
4862 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4863 * in the command line, so that things like % get expanded.
4864 */
4865 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004866replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004867{
4868 char_u *new_cmdline;
4869 char_u *program;
4870 char_u *pos;
4871 char_u *ptr;
4872 int len;
4873 int i;
4874
4875 /*
4876 * Don't do it when ":vimgrep" is used for ":grep".
4877 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004878 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4879 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4880 || eap->cmdidx == CMD_grepadd
4881 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004882 && !grep_internal(eap->cmdidx))
4883 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004884 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4885 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004886 {
4887 if (*curbuf->b_p_gp == NUL)
4888 program = p_gp;
4889 else
4890 program = curbuf->b_p_gp;
4891 }
4892 else
4893 {
4894 if (*curbuf->b_p_mp == NUL)
4895 program = p_mp;
4896 else
4897 program = curbuf->b_p_mp;
4898 }
4899
4900 p = skipwhite(p);
4901
4902 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4903 {
4904 /* replace $* by given arguments */
4905 i = 1;
4906 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4907 ++i;
4908 len = (int)STRLEN(p);
4909 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4910 if (new_cmdline == NULL)
4911 return NULL; /* out of memory */
4912 ptr = new_cmdline;
4913 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4914 {
4915 i = (int)(pos - program);
4916 STRNCPY(ptr, program, i);
4917 STRCPY(ptr += i, p);
4918 ptr += len;
4919 program = pos + 2;
4920 }
4921 STRCPY(ptr, program);
4922 }
4923 else
4924 {
4925 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4926 if (new_cmdline == NULL)
4927 return NULL; /* out of memory */
4928 STRCPY(new_cmdline, program);
4929 STRCAT(new_cmdline, " ");
4930 STRCAT(new_cmdline, p);
4931 }
4932 msg_make(p);
4933
4934 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4935 vim_free(*cmdlinep);
4936 *cmdlinep = new_cmdline;
4937 p = new_cmdline;
4938 }
4939 return p;
4940}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004941#endif
4942
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943/*
4944 * Expand file name in Ex command argument.
4945 * Return FAIL for failure, OK otherwise.
4946 */
4947 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004948expand_filename(
4949 exarg_T *eap,
4950 char_u **cmdlinep,
4951 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952{
4953 int has_wildcards; /* need to expand wildcards */
4954 char_u *repl;
4955 int srclen;
4956 char_u *p;
4957 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004958 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959
Bram Moolenaar748bf032005-02-02 23:04:36 +00004960#ifdef FEAT_QUICKFIX
4961 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4962 p = skip_grep_pat(eap);
4963#else
4964 p = eap->arg;
4965#endif
4966
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 /*
4968 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4969 * the file name contains a wildcard it should not cause expanding.
4970 * (it will be expanded anyway if there is a wildcard before replacing).
4971 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004972 has_wildcards = mch_has_wildcard(p);
4973 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004975#ifdef FEAT_EVAL
4976 /* Skip over `=expr`, wildcards in it are not expanded. */
4977 if (p[0] == '`' && p[1] == '=')
4978 {
4979 p += 2;
4980 (void)skip_expr(&p);
4981 if (*p == '`')
4982 ++p;
4983 continue;
4984 }
4985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 /*
4987 * Quick check if this cannot be the start of a special string.
4988 * Also removes backslash before '%', '#' and '<'.
4989 */
4990 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4991 {
4992 ++p;
4993 continue;
4994 }
4995
4996 /*
4997 * Try to find a match at this position.
4998 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004999 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5000 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 if (*errormsgp != NULL) /* error detected */
5002 return FAIL;
5003 if (repl == NULL) /* no match found */
5004 {
5005 p += srclen;
5006 continue;
5007 }
5008
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005009 /* Wildcards won't be expanded below, the replacement is taken
5010 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5011 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5012 {
5013 char_u *l = repl;
5014
5015 repl = expand_env_save(repl);
5016 vim_free(l);
5017 }
5018
Bram Moolenaar63b92542007-03-27 14:57:09 +00005019 /* Need to escape white space et al. with a backslash.
5020 * Don't do this for:
5021 * - replacement that already has been escaped: "##"
5022 * - shell commands (may have to use quotes instead).
5023 * - non-unix systems when there is a single argument (spaces don't
5024 * separate arguments then).
5025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005027 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 && eap->cmdidx != CMD_grep
5030 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005031 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005032 && eap->cmdidx != CMD_lgrep
5033 && eap->cmdidx != CMD_lgrepadd
5034 && eap->cmdidx != CMD_lmake
5035 && eap->cmdidx != CMD_make
5036 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037#ifndef UNIX
5038 && !(eap->argt & NOSPC)
5039#endif
5040 )
5041 {
5042 char_u *l;
5043#ifdef BACKSLASH_IN_FILENAME
5044 /* Don't escape a backslash here, because rem_backslash() doesn't
5045 * remove it later. */
5046 static char_u *nobslash = (char_u *)" \t\"|";
5047# define ESCAPE_CHARS nobslash
5048#else
5049# define ESCAPE_CHARS escape_chars
5050#endif
5051
5052 for (l = repl; *l; ++l)
5053 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5054 {
5055 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5056 if (l != NULL)
5057 {
5058 vim_free(repl);
5059 repl = l;
5060 }
5061 break;
5062 }
5063 }
5064
5065 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005066 if ((eap->usefilter || eap->cmdidx == CMD_bang
5067 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005068 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
5070 char_u *l;
5071
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005072 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 if (l != NULL)
5074 {
5075 vim_free(repl);
5076 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 }
5078 }
5079
5080 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5081 vim_free(repl);
5082 if (p == NULL)
5083 return FAIL;
5084 }
5085
5086 /*
5087 * One file argument: Expand wildcards.
5088 * Don't do this with ":r !command" or ":w !command".
5089 */
5090 if ((eap->argt & NOSPC) && !eap->usefilter)
5091 {
5092 /*
5093 * May do this twice:
5094 * 1. Replace environment variables.
5095 * 2. Replace any other wildcards, remove backslashes.
5096 */
5097 for (n = 1; n <= 2; ++n)
5098 {
5099 if (n == 2)
5100 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 /*
5102 * Halve the number of backslashes (this is Vi compatible).
5103 * For Unix and OS/2, when wildcards are expanded, this is
5104 * done by ExpandOne() below.
5105 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005106#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 if (!has_wildcards)
5108#endif
5109 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 }
5111
5112 if (has_wildcards)
5113 {
5114 if (n == 1)
5115 {
5116 /*
5117 * First loop: May expand environment variables. This
5118 * can be done much faster with expand_env() than with
5119 * something else (e.g., calling a shell).
5120 * After expanding environment variables, check again
5121 * if there are still wildcards present.
5122 */
5123 if (vim_strchr(eap->arg, '$') != NULL
5124 || vim_strchr(eap->arg, '~') != NULL)
5125 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005126 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005127 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 has_wildcards = mch_has_wildcard(NameBuff);
5129 p = NameBuff;
5130 }
5131 else
5132 p = NULL;
5133 }
5134 else /* n == 2 */
5135 {
5136 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005137 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
5139 ExpandInit(&xpc);
5140 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005141 if (p_wic)
5142 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005144 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 if (p == NULL)
5146 return FAIL;
5147 }
5148 if (p != NULL)
5149 {
5150 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5151 p, cmdlinep);
5152 if (n == 2) /* p came from ExpandOne() */
5153 vim_free(p);
5154 }
5155 }
5156 }
5157 }
5158 return OK;
5159}
5160
5161/*
5162 * Replace part of the command line, keeping eap->cmd, eap->arg and
5163 * eap->nextcmd correct.
5164 * "src" points to the part that is to be replaced, of length "srclen".
5165 * "repl" is the replacement string.
5166 * Returns a pointer to the character after the replaced string.
5167 * Returns NULL for failure.
5168 */
5169 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005170repl_cmdline(
5171 exarg_T *eap,
5172 char_u *src,
5173 int srclen,
5174 char_u *repl,
5175 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176{
5177 int len;
5178 int i;
5179 char_u *new_cmdline;
5180
5181 /*
5182 * The new command line is build in new_cmdline[].
5183 * First allocate it.
5184 * Careful: a "+cmd" argument may have been NUL terminated.
5185 */
5186 len = (int)STRLEN(repl);
5187 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005188 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5190 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5191 return NULL; /* out of memory! */
5192
5193 /*
5194 * Copy the stuff before the expanded part.
5195 * Copy the expanded stuff.
5196 * Copy what came after the expanded part.
5197 * Copy the next commands, if there are any.
5198 */
5199 i = (int)(src - *cmdlinep); /* length of part before match */
5200 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005201
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 mch_memmove(new_cmdline + i, repl, (size_t)len);
5203 i += len; /* remember the end of the string */
5204 STRCPY(new_cmdline + i, src + srclen);
5205 src = new_cmdline + i; /* remember where to continue */
5206
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005207 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 {
5209 i = (int)STRLEN(new_cmdline) + 1;
5210 STRCPY(new_cmdline + i, eap->nextcmd);
5211 eap->nextcmd = new_cmdline + i;
5212 }
5213 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5214 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5215 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5216 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5217 vim_free(*cmdlinep);
5218 *cmdlinep = new_cmdline;
5219
5220 return src;
5221}
5222
5223/*
5224 * Check for '|' to separate commands and '"' to start comments.
5225 */
5226 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005227separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228{
5229 char_u *p;
5230
Bram Moolenaar86b68352004-12-27 21:59:20 +00005231#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005232 p = skip_grep_pat(eap);
5233#else
5234 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005235#endif
5236
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005237 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 {
5239 if (*p == Ctrl_V)
5240 {
5241 if (eap->argt & (USECTRLV | XFILE))
5242 ++p; /* skip CTRL-V and next char */
5243 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005244 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005245 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 if (*p == NUL) /* stop at NUL after CTRL-V */
5247 break;
5248 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005249
5250#ifdef FEAT_EVAL
5251 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005252 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005253 {
5254 p += 2;
5255 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005256 }
5257#endif
5258
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 /* Check for '"': start of comment or '|': next command */
5260 /* :@" and :*" do not start a comment!
5261 * :redir @" doesn't either. */
5262 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5263 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5264 || p != eap->arg)
5265 && (eap->cmdidx != CMD_redir
5266 || p != eap->arg + 1 || p[-1] != '@'))
5267 || *p == '|' || *p == '\n')
5268 {
5269 /*
5270 * We remove the '\' before the '|', unless USECTRLV is used
5271 * AND 'b' is present in 'cpoptions'.
5272 */
5273 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5274 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5275 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005276 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 --p;
5278 }
5279 else
5280 {
5281 eap->nextcmd = check_nextcmd(p);
5282 *p = NUL;
5283 break;
5284 }
5285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5289 del_trailing_spaces(eap->arg);
5290}
5291
5292/*
5293 * get + command from ex argument
5294 */
5295 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005296getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297{
5298 char_u *arg = *argp;
5299 char_u *command = NULL;
5300
5301 if (*arg == '+') /* +[command] */
5302 {
5303 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005304 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 command = dollar_command;
5306 else
5307 {
5308 command = arg;
5309 arg = skip_cmd_arg(command, TRUE);
5310 if (*arg != NUL)
5311 *arg++ = NUL; /* terminate command with NUL */
5312 }
5313
5314 arg = skipwhite(arg); /* skip over spaces */
5315 *argp = arg;
5316 }
5317 return command;
5318}
5319
5320/*
5321 * Find end of "+command" argument. Skip over "\ " and "\\".
5322 */
5323 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005324skip_cmd_arg(
5325 char_u *p,
5326 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327{
5328 while (*p && !vim_isspace(*p))
5329 {
5330 if (*p == '\\' && p[1] != NUL)
5331 {
5332 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005333 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 else
5335 ++p;
5336 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005337 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
5339 return p;
5340}
5341
5342/*
5343 * Get "++opt=arg" argument.
5344 * Return FAIL or OK.
5345 */
5346 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005347getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348{
5349 char_u *arg = eap->arg + 2;
5350 int *pp = NULL;
5351#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005352 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 char_u *p;
5354#endif
5355
5356 /* ":edit ++[no]bin[ary] file" */
5357 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5358 {
5359 if (*arg == 'n')
5360 {
5361 arg += 2;
5362 eap->force_bin = FORCE_NOBIN;
5363 }
5364 else
5365 eap->force_bin = FORCE_BIN;
5366 if (!checkforcmd(&arg, "binary", 3))
5367 return FAIL;
5368 eap->arg = skipwhite(arg);
5369 return OK;
5370 }
5371
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005372 /* ":read ++edit file" */
5373 if (STRNCMP(arg, "edit", 4) == 0)
5374 {
5375 eap->read_edit = TRUE;
5376 eap->arg = skipwhite(arg + 4);
5377 return OK;
5378 }
5379
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 if (STRNCMP(arg, "ff", 2) == 0)
5381 {
5382 arg += 2;
5383 pp = &eap->force_ff;
5384 }
5385 else if (STRNCMP(arg, "fileformat", 10) == 0)
5386 {
5387 arg += 10;
5388 pp = &eap->force_ff;
5389 }
5390#ifdef FEAT_MBYTE
5391 else if (STRNCMP(arg, "enc", 3) == 0)
5392 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005393 if (STRNCMP(arg, "encoding", 8) == 0)
5394 arg += 8;
5395 else
5396 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 pp = &eap->force_enc;
5398 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005399 else if (STRNCMP(arg, "bad", 3) == 0)
5400 {
5401 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005402 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404#endif
5405
5406 if (pp == NULL || *arg != '=')
5407 return FAIL;
5408
5409 ++arg;
5410 *pp = (int)(arg - eap->cmd);
5411 arg = skip_cmd_arg(arg, FALSE);
5412 eap->arg = skipwhite(arg);
5413 *arg = NUL;
5414
5415#ifdef FEAT_MBYTE
5416 if (pp == &eap->force_ff)
5417 {
5418#endif
5419 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5420 return FAIL;
5421#ifdef FEAT_MBYTE
5422 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005423 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 {
5425 /* Make 'fileencoding' lower case. */
5426 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5427 *p = TOLOWER_ASC(*p);
5428 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005429 else
5430 {
5431 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5432 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005433 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005434 if (STRICMP(p, "keep") == 0)
5435 eap->bad_char = BAD_KEEP;
5436 else if (STRICMP(p, "drop") == 0)
5437 eap->bad_char = BAD_DROP;
5438 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5439 eap->bad_char = *p;
5440 else
5441 return FAIL;
5442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443#endif
5444
5445 return OK;
5446}
5447
5448/*
5449 * ":abbreviate" and friends.
5450 */
5451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005452ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453{
5454 do_exmap(eap, TRUE); /* almost the same as mapping */
5455}
5456
5457/*
5458 * ":map" and friends.
5459 */
5460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005461ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462{
5463 /*
5464 * If we are sourcing .exrc or .vimrc in current directory we
5465 * print the mappings for security reasons.
5466 */
5467 if (secure)
5468 {
5469 secure = 2;
5470 msg_outtrans(eap->cmd);
5471 msg_putchar('\n');
5472 }
5473 do_exmap(eap, FALSE);
5474}
5475
5476/*
5477 * ":unmap" and friends.
5478 */
5479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005480ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481{
5482 do_exmap(eap, FALSE);
5483}
5484
5485/*
5486 * ":mapclear" and friends.
5487 */
5488 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005489ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490{
5491 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5492}
5493
5494/*
5495 * ":abclear" and friends.
5496 */
5497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005498ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
5500 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5501}
5502
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005503#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005505ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506{
5507 /*
5508 * Disallow auto commands from .exrc and .vimrc in current
5509 * directory for security reasons.
5510 */
5511 if (secure)
5512 {
5513 secure = 2;
5514 eap->errmsg = e_curdir;
5515 }
5516 else if (eap->cmdidx == CMD_autocmd)
5517 do_autocmd(eap->arg, eap->forceit);
5518 else
5519 do_augroup(eap->arg, eap->forceit);
5520}
5521
5522/*
5523 * ":doautocmd": Apply the automatic commands to the current buffer.
5524 */
5525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005526ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005528 char_u *arg = eap->arg;
5529 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005530 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005531
Bram Moolenaar1610d052016-06-09 22:53:01 +02005532 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5533 /* Only when there is no <nomodeline>. */
5534 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005535 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536}
5537#endif
5538
5539#ifdef FEAT_LISTCMDS
5540/*
5541 * :[N]bunload[!] [N] [bufname] unload buffer
5542 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5543 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5544 */
5545 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005546ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
5548 eap->errmsg = do_bufdel(
5549 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5550 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5551 : DOBUF_UNLOAD, eap->arg,
5552 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5553}
5554
5555/*
5556 * :[N]buffer [N] to buffer N
5557 * :[N]sbuffer [N] to buffer N
5558 */
5559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005560ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
5562 if (*eap->arg)
5563 eap->errmsg = e_trailing;
5564 else
5565 {
5566 if (eap->addr_count == 0) /* default is current buffer */
5567 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5568 else
5569 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005570 if (eap->do_ecmd_cmd != NULL)
5571 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
5573}
5574
5575/*
5576 * :[N]bmodified [N] to next mod. buffer
5577 * :[N]sbmodified [N] to next mod. buffer
5578 */
5579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005580ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005583 if (eap->do_ecmd_cmd != NULL)
5584 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585}
5586
5587/*
5588 * :[N]bnext [N] to next buffer
5589 * :[N]sbnext [N] split and to next buffer
5590 */
5591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005592ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
5594 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005595 if (eap->do_ecmd_cmd != NULL)
5596 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597}
5598
5599/*
5600 * :[N]bNext [N] to previous buffer
5601 * :[N]bprevious [N] to previous buffer
5602 * :[N]sbNext [N] split and to previous buffer
5603 * :[N]sbprevious [N] split and to previous buffer
5604 */
5605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005606ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
5608 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (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 * :brewind to first buffer
5615 * :bfirst to first buffer
5616 * :sbrewind split and to first buffer
5617 * :sbfirst split and to first buffer
5618 */
5619 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005620ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621{
5622 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005623 if (eap->do_ecmd_cmd != NULL)
5624 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625}
5626
5627/*
5628 * :blast to last buffer
5629 * :sblast split and to last buffer
5630 */
5631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005632ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633{
5634 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005635 if (eap->do_ecmd_cmd != NULL)
5636 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637}
5638#endif
5639
5640 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005641ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
5643 return (c == NUL || c == '|' || c == '"' || c == '\n');
5644}
5645
5646#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5647 || defined(PROTO)
5648/*
5649 * Return the next command, after the first '|' or '\n'.
5650 * Return NULL if not found.
5651 */
5652 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005653find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654{
5655 while (*p != '|' && *p != '\n')
5656 {
5657 if (*p == NUL)
5658 return NULL;
5659 ++p;
5660 }
5661 return (p + 1);
5662}
5663#endif
5664
5665/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005666 * Check if *p is a separator between Ex commands, skipping over white space.
5667 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 */
5669 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005670check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005672 char_u *s = skipwhite(p);
5673
5674 if (*s == '|' || *s == '\n')
5675 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 else
5677 return NULL;
5678}
5679
5680/*
5681 * - if there are more files to edit
5682 * - and this is the last window
5683 * - and forceit not used
5684 * - and not repeated twice on a row
5685 * return FAIL and give error message if 'message' TRUE
5686 * return OK otherwise
5687 */
5688 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005689check_more(
5690 int message, /* when FALSE check only, no messages */
5691 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692{
5693 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5694
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005695 if (!forceit && only_one_window()
5696 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
5698 if (message)
5699 {
5700#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5701 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5702 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005703 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704
5705 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005706 vim_strncpy(buff,
5707 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005708 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005710 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 _("%d more files to edit. Quit anyway?"), n);
5712 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5713 return OK;
5714 return FAIL;
5715 }
5716#endif
5717 if (n == 1)
5718 EMSG(_("E173: 1 more file to edit"));
5719 else
5720 EMSGN(_("E173: %ld more files to edit"), n);
5721 quitmore = 2; /* next try to quit is allowed */
5722 }
5723 return FAIL;
5724 }
5725 return OK;
5726}
5727
5728#ifdef FEAT_CMDL_COMPL
5729/*
5730 * Function given to ExpandGeneric() to obtain the list of command names.
5731 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005733get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734{
5735 if (idx >= (int)CMD_SIZE)
5736# ifdef FEAT_USR_CMDS
5737 return get_user_command_name(idx);
5738# else
5739 return NULL;
5740# endif
5741 return cmdnames[idx].cmd_name;
5742}
5743#endif
5744
5745#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005746static 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);
5747static void uc_list(char_u *name, size_t name_len);
5748static 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);
5749static char_u *uc_split_args(char_u *arg, size_t *lenp);
5750static 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 +00005751
5752 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005753uc_add_command(
5754 char_u *name,
5755 size_t name_len,
5756 char_u *rep,
5757 long argt,
5758 long def,
5759 int flags,
5760 int compl,
5761 char_u *compl_arg,
5762 int addr_type,
5763 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
5765 ucmd_T *cmd = NULL;
5766 char_u *p;
5767 int i;
5768 int cmp = 1;
5769 char_u *rep_buf = NULL;
5770 garray_T *gap;
5771
Bram Moolenaar9c102382006-05-03 21:26:49 +00005772 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 if (rep_buf == NULL)
5774 {
5775 /* Can't replace termcodes - try using the string as is */
5776 rep_buf = vim_strsave(rep);
5777
5778 /* Give up if out of memory */
5779 if (rep_buf == NULL)
5780 return FAIL;
5781 }
5782
5783 /* get address of growarray: global or in curbuf */
5784 if (flags & UC_BUFFER)
5785 {
5786 gap = &curbuf->b_ucmds;
5787 if (gap->ga_itemsize == 0)
5788 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5789 }
5790 else
5791 gap = &ucmds;
5792
5793 /* Search for the command in the already defined commands. */
5794 for (i = 0; i < gap->ga_len; ++i)
5795 {
5796 size_t len;
5797
5798 cmd = USER_CMD_GA(gap, i);
5799 len = STRLEN(cmd->uc_name);
5800 cmp = STRNCMP(name, cmd->uc_name, name_len);
5801 if (cmp == 0)
5802 {
5803 if (name_len < len)
5804 cmp = -1;
5805 else if (name_len > len)
5806 cmp = 1;
5807 }
5808
5809 if (cmp == 0)
5810 {
5811 if (!force)
5812 {
5813 EMSG(_("E174: Command already exists: add ! to replace it"));
5814 goto fail;
5815 }
5816
Bram Moolenaard23a8232018-02-10 18:45:26 +01005817 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005818#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005819 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 break;
5822 }
5823
5824 /* Stop as soon as we pass the name to add */
5825 if (cmp < 0)
5826 break;
5827 }
5828
5829 /* Extend the array unless we're replacing an existing command */
5830 if (cmp != 0)
5831 {
5832 if (ga_grow(gap, 1) != OK)
5833 goto fail;
5834 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5835 goto fail;
5836
5837 cmd = USER_CMD_GA(gap, i);
5838 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5839
5840 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841
5842 cmd->uc_name = p;
5843 }
5844
5845 cmd->uc_rep = rep_buf;
5846 cmd->uc_argt = argt;
5847 cmd->uc_def = def;
5848 cmd->uc_compl = compl;
5849#ifdef FEAT_EVAL
5850 cmd->uc_scriptID = current_SID;
5851# ifdef FEAT_CMDL_COMPL
5852 cmd->uc_compl_arg = compl_arg;
5853# endif
5854#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005855 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856
5857 return OK;
5858
5859fail:
5860 vim_free(rep_buf);
5861#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5862 vim_free(compl_arg);
5863#endif
5864 return FAIL;
5865}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005868#if defined(FEAT_USR_CMDS)
5869static struct
5870{
5871 int expand;
5872 char *name;
5873} addr_type_complete[] =
5874{
5875 {ADDR_ARGUMENTS, "arguments"},
5876 {ADDR_LINES, "lines"},
5877 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5878 {ADDR_TABS, "tabs"},
5879 {ADDR_BUFFERS, "buffers"},
5880 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005881 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005882 {-1, NULL}
5883};
5884#endif
5885
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005886#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887/*
5888 * List of names for completion for ":command" with the EXPAND_ flag.
5889 * Must be alphabetical for completion.
5890 */
5891static struct
5892{
5893 int expand;
5894 char *name;
5895} command_complete[] =
5896{
5897 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005898 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005900 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005902 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005903#if defined(FEAT_CSCOPE)
5904 {EXPAND_CSCOPE, "cscope"},
5905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5907 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005908 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909#endif
5910 {EXPAND_DIRECTORIES, "dir"},
5911 {EXPAND_ENV_VARS, "environment"},
5912 {EXPAND_EVENTS, "event"},
5913 {EXPAND_EXPRESSION, "expression"},
5914 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005915 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005916 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 {EXPAND_FUNCTIONS, "function"},
5918 {EXPAND_HELP, "help"},
5919 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005920#if defined(FEAT_CMDHIST)
5921 {EXPAND_HISTORY, "history"},
5922#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005923#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005924 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005925 {EXPAND_LOCALES, "locale"},
5926#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005927 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {EXPAND_MAPPINGS, "mapping"},
5929 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005930 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005931 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005932#if defined(FEAT_PROFILE)
5933 {EXPAND_SYNTIME, "syntime"},
5934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005936 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005937 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005938#if defined(FEAT_SIGNS)
5939 {EXPAND_SIGN, "sign"},
5940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 {EXPAND_TAGS, "tag"},
5942 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005943 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 {EXPAND_USER_VARS, "var"},
5945 {0, NULL}
5946};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005949#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005951uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
5953 int i, j;
5954 int found = FALSE;
5955 ucmd_T *cmd;
5956 int len;
5957 long a;
5958 garray_T *gap;
5959
5960 gap = &curbuf->b_ucmds;
5961 for (;;)
5962 {
5963 for (i = 0; i < gap->ga_len; ++i)
5964 {
5965 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005966 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967
Bram Moolenaard29459b2016-08-26 22:29:11 +02005968 /* Skip commands which don't match the requested prefix and
5969 * commands filtered out. */
5970 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5971 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 continue;
5973
5974 /* Put out the title first time */
5975 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005976 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 found = TRUE;
5978 msg_putchar('\n');
5979 if (got_int)
5980 break;
5981
5982 /* Special cases */
5983 msg_putchar(a & BANG ? '!' : ' ');
5984 msg_putchar(a & REGSTR ? '"' : ' ');
5985 msg_putchar(gap != &ucmds ? 'b' : ' ');
5986 msg_putchar(' ');
5987
Bram Moolenaar8820b482017-03-16 17:23:31 +01005988 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 len = (int)STRLEN(cmd->uc_name) + 4;
5990
5991 do {
5992 msg_putchar(' ');
5993 ++len;
5994 } while (len < 16);
5995
5996 len = 0;
5997
5998 /* Arguments */
5999 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6000 {
6001 case 0: IObuff[len++] = '0'; break;
6002 case (EXTRA): IObuff[len++] = '*'; break;
6003 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6004 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6005 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6006 }
6007
6008 do {
6009 IObuff[len++] = ' ';
6010 } while (len < 5);
6011
6012 /* Range */
6013 if (a & (RANGE|COUNT))
6014 {
6015 if (a & COUNT)
6016 {
6017 /* -count=N */
6018 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6019 len += (int)STRLEN(IObuff + len);
6020 }
6021 else if (a & DFLALL)
6022 IObuff[len++] = '%';
6023 else if (cmd->uc_def >= 0)
6024 {
6025 /* -range=N */
6026 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6027 len += (int)STRLEN(IObuff + len);
6028 }
6029 else
6030 IObuff[len++] = '.';
6031 }
6032
6033 do {
6034 IObuff[len++] = ' ';
6035 } while (len < 11);
6036
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006037 /* Address Type */
6038 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6039 if (addr_type_complete[j].expand != ADDR_LINES
6040 && addr_type_complete[j].expand == cmd->uc_addr_type)
6041 {
6042 STRCPY(IObuff + len, addr_type_complete[j].name);
6043 len += (int)STRLEN(IObuff + len);
6044 break;
6045 }
6046
6047 do {
6048 IObuff[len++] = ' ';
6049 } while (len < 21);
6050
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 /* Completion */
6052 for (j = 0; command_complete[j].expand != 0; ++j)
6053 if (command_complete[j].expand == cmd->uc_compl)
6054 {
6055 STRCPY(IObuff + len, command_complete[j].name);
6056 len += (int)STRLEN(IObuff + len);
6057 break;
6058 }
6059
6060 do {
6061 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006062 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063
6064 IObuff[len] = '\0';
6065 msg_outtrans(IObuff);
6066
6067 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006068#ifdef FEAT_EVAL
6069 if (p_verbose > 0)
6070 last_set_msg(cmd->uc_scriptID);
6071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 out_flush();
6073 ui_breakcheck();
6074 if (got_int)
6075 break;
6076 }
6077 if (gap == &ucmds || i < gap->ga_len)
6078 break;
6079 gap = &ucmds;
6080 }
6081
6082 if (!found)
6083 MSG(_("No user-defined commands found"));
6084}
6085
6086 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006087uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088{
6089 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6090 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6091 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6092 0xb9, 0x7f, 0};
6093 int i;
6094
6095 for (i = 0; fcmd[i]; ++i)
6096 IObuff[i] = fcmd[i] - 0x40;
6097 IObuff[i] = 0;
6098 return IObuff;
6099}
6100
6101 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006102uc_scan_attr(
6103 char_u *attr,
6104 size_t len,
6105 long *argt,
6106 long *def,
6107 int *flags,
6108 int *compl,
6109 char_u **compl_arg,
6110 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111{
6112 char_u *p;
6113
6114 if (len == 0)
6115 {
6116 EMSG(_("E175: No attribute specified"));
6117 return FAIL;
6118 }
6119
6120 /* First, try the simple attributes (no arguments) */
6121 if (STRNICMP(attr, "bang", len) == 0)
6122 *argt |= BANG;
6123 else if (STRNICMP(attr, "buffer", len) == 0)
6124 *flags |= UC_BUFFER;
6125 else if (STRNICMP(attr, "register", len) == 0)
6126 *argt |= REGSTR;
6127 else if (STRNICMP(attr, "bar", len) == 0)
6128 *argt |= TRLBAR;
6129 else
6130 {
6131 int i;
6132 char_u *val = NULL;
6133 size_t vallen = 0;
6134 size_t attrlen = len;
6135
6136 /* Look for the attribute name - which is the part before any '=' */
6137 for (i = 0; i < (int)len; ++i)
6138 {
6139 if (attr[i] == '=')
6140 {
6141 val = &attr[i + 1];
6142 vallen = len - i - 1;
6143 attrlen = i;
6144 break;
6145 }
6146 }
6147
6148 if (STRNICMP(attr, "nargs", attrlen) == 0)
6149 {
6150 if (vallen == 1)
6151 {
6152 if (*val == '0')
6153 /* Do nothing - this is the default */;
6154 else if (*val == '1')
6155 *argt |= (EXTRA | NOSPC | NEEDARG);
6156 else if (*val == '*')
6157 *argt |= EXTRA;
6158 else if (*val == '?')
6159 *argt |= (EXTRA | NOSPC);
6160 else if (*val == '+')
6161 *argt |= (EXTRA | NEEDARG);
6162 else
6163 goto wrong_nargs;
6164 }
6165 else
6166 {
6167wrong_nargs:
6168 EMSG(_("E176: Invalid number of arguments"));
6169 return FAIL;
6170 }
6171 }
6172 else if (STRNICMP(attr, "range", attrlen) == 0)
6173 {
6174 *argt |= RANGE;
6175 if (vallen == 1 && *val == '%')
6176 *argt |= DFLALL;
6177 else if (val != NULL)
6178 {
6179 p = val;
6180 if (*def >= 0)
6181 {
6182two_count:
6183 EMSG(_("E177: Count cannot be specified twice"));
6184 return FAIL;
6185 }
6186
6187 *def = getdigits(&p);
6188 *argt |= (ZEROR | NOTADR);
6189
6190 if (p != val + vallen || vallen == 0)
6191 {
6192invalid_count:
6193 EMSG(_("E178: Invalid default value for count"));
6194 return FAIL;
6195 }
6196 }
6197 }
6198 else if (STRNICMP(attr, "count", attrlen) == 0)
6199 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006200 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201
6202 if (val != NULL)
6203 {
6204 p = val;
6205 if (*def >= 0)
6206 goto two_count;
6207
6208 *def = getdigits(&p);
6209
6210 if (p != val + vallen)
6211 goto invalid_count;
6212 }
6213
6214 if (*def < 0)
6215 *def = 0;
6216 }
6217 else if (STRNICMP(attr, "complete", attrlen) == 0)
6218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 if (val == NULL)
6220 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006221 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 return FAIL;
6223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006225 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6226 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006229 else if (STRNICMP(attr, "addr", attrlen) == 0)
6230 {
6231 *argt |= RANGE;
6232 if (val == NULL)
6233 {
6234 EMSG(_("E179: argument required for -addr"));
6235 return FAIL;
6236 }
6237 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6238 == FAIL)
6239 return FAIL;
6240 if (addr_type_arg != ADDR_LINES)
6241 *argt |= (ZEROR | NOTADR) ;
6242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 else
6244 {
6245 char_u ch = attr[len];
6246 attr[len] = '\0';
6247 EMSG2(_("E181: Invalid attribute: %s"), attr);
6248 attr[len] = ch;
6249 return FAIL;
6250 }
6251 }
6252
6253 return OK;
6254}
6255
Bram Moolenaara850a712009-01-28 14:42:59 +00006256/*
6257 * ":command ..."
6258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006260ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
6262 char_u *name;
6263 char_u *end;
6264 char_u *p;
6265 long argt = 0;
6266 long def = -1;
6267 int flags = 0;
6268 int compl = EXPAND_NOTHING;
6269 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006270 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006272 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273
6274 p = eap->arg;
6275
6276 /* Check for attributes */
6277 while (*p == '-')
6278 {
6279 ++p;
6280 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006281 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6282 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 == FAIL)
6284 return;
6285 p = skipwhite(end);
6286 }
6287
6288 /* Get the name (if any) and skip to the following argument */
6289 name = p;
6290 if (ASCII_ISALPHA(*p))
6291 while (ASCII_ISALNUM(*p))
6292 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006293 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 {
6295 EMSG(_("E182: Invalid command name"));
6296 return;
6297 }
6298 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006299 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300
6301 /* If there is nothing after the name, and no attributes were specified,
6302 * we are listing commands
6303 */
6304 p = skipwhite(end);
6305 if (!has_attr && ends_excmd(*p))
6306 {
6307 uc_list(name, end - name);
6308 }
6309 else if (!ASCII_ISUPPER(*name))
6310 {
6311 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6312 return;
6313 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006314 else if ((name_len == 1 && *name == 'X')
6315 || (name_len <= 4
6316 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6317 {
6318 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6319 return;
6320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 else
6322 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006323 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324}
6325
6326/*
6327 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 * Clear all user commands, global and for current buffer.
6329 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006330 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006331ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332{
6333 uc_clear(&ucmds);
6334 uc_clear(&curbuf->b_ucmds);
6335}
6336
6337/*
6338 * Clear all user commands for "gap".
6339 */
6340 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006341uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
6343 int i;
6344 ucmd_T *cmd;
6345
6346 for (i = 0; i < gap->ga_len; ++i)
6347 {
6348 cmd = USER_CMD_GA(gap, i);
6349 vim_free(cmd->uc_name);
6350 vim_free(cmd->uc_rep);
6351# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6352 vim_free(cmd->uc_compl_arg);
6353# endif
6354 }
6355 ga_clear(gap);
6356}
6357
6358 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006359ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360{
6361 int i = 0;
6362 ucmd_T *cmd = NULL;
6363 int cmp = -1;
6364 garray_T *gap;
6365
6366 gap = &curbuf->b_ucmds;
6367 for (;;)
6368 {
6369 for (i = 0; i < gap->ga_len; ++i)
6370 {
6371 cmd = USER_CMD_GA(gap, i);
6372 cmp = STRCMP(eap->arg, cmd->uc_name);
6373 if (cmp <= 0)
6374 break;
6375 }
6376 if (gap == &ucmds || cmp == 0)
6377 break;
6378 gap = &ucmds;
6379 }
6380
6381 if (cmp != 0)
6382 {
6383 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6384 return;
6385 }
6386
6387 vim_free(cmd->uc_name);
6388 vim_free(cmd->uc_rep);
6389# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6390 vim_free(cmd->uc_compl_arg);
6391# endif
6392
6393 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394
6395 if (i < gap->ga_len)
6396 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6397}
6398
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006399/*
6400 * split and quote args for <f-args>
6401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006403uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
6405 char_u *buf;
6406 char_u *p;
6407 char_u *q;
6408 int len;
6409
6410 /* Precalculate length */
6411 p = arg;
6412 len = 2; /* Initial and final quotes */
6413
6414 while (*p)
6415 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006416 if (p[0] == '\\' && p[1] == '\\')
6417 {
6418 len += 2;
6419 p += 2;
6420 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006421 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 {
6423 len += 1;
6424 p += 2;
6425 }
6426 else if (*p == '\\' || *p == '"')
6427 {
6428 len += 2;
6429 p += 1;
6430 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006431 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 {
6433 p = skipwhite(p);
6434 if (*p == NUL)
6435 break;
6436 len += 3; /* "," */
6437 }
6438 else
6439 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006440#ifdef FEAT_MBYTE
6441 int charlen = (*mb_ptr2len)(p);
6442 len += charlen;
6443 p += charlen;
6444#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 ++len;
6446 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 }
6449 }
6450
6451 buf = alloc(len + 1);
6452 if (buf == NULL)
6453 {
6454 *lenp = 0;
6455 return buf;
6456 }
6457
6458 p = arg;
6459 q = buf;
6460 *q++ = '"';
6461 while (*p)
6462 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006463 if (p[0] == '\\' && p[1] == '\\')
6464 {
6465 *q++ = '\\';
6466 *q++ = '\\';
6467 p += 2;
6468 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006469 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 {
6471 *q++ = p[1];
6472 p += 2;
6473 }
6474 else if (*p == '\\' || *p == '"')
6475 {
6476 *q++ = '\\';
6477 *q++ = *p++;
6478 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006479 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 {
6481 p = skipwhite(p);
6482 if (*p == NUL)
6483 break;
6484 *q++ = '"';
6485 *q++ = ',';
6486 *q++ = '"';
6487 }
6488 else
6489 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006490 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 }
6492 }
6493 *q++ = '"';
6494 *q = 0;
6495
6496 *lenp = len;
6497 return buf;
6498}
6499
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006500 static size_t
6501add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6502{
6503 size_t result;
6504
6505 result = STRLEN(mod_str);
6506 if (*multi_mods)
6507 result += 1;
6508 if (buf != NULL)
6509 {
6510 if (*multi_mods)
6511 STRCAT(buf, " ");
6512 STRCAT(buf, mod_str);
6513 }
6514
6515 *multi_mods = 1;
6516
6517 return result;
6518}
6519
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520/*
6521 * Check for a <> code in a user command.
6522 * "code" points to the '<'. "len" the length of the <> (inclusive).
6523 * "buf" is where the result is to be added.
6524 * "split_buf" points to a buffer used for splitting, caller should free it.
6525 * "split_len" is the length of what "split_buf" contains.
6526 * Returns the length of the replacement, which has been added to "buf".
6527 * Returns -1 if there was no match, and only the "<" has been copied.
6528 */
6529 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006530uc_check_code(
6531 char_u *code,
6532 size_t len,
6533 char_u *buf,
6534 ucmd_T *cmd, /* the user command we're expanding */
6535 exarg_T *eap, /* ex arguments */
6536 char_u **split_buf,
6537 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538{
6539 size_t result = 0;
6540 char_u *p = code + 1;
6541 size_t l = len - 2;
6542 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006543 enum {
6544 ct_ARGS,
6545 ct_BANG,
6546 ct_COUNT,
6547 ct_LINE1,
6548 ct_LINE2,
6549 ct_RANGE,
6550 ct_MODS,
6551 ct_REGISTER,
6552 ct_LT,
6553 ct_NONE
6554 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
6556 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6557 {
6558 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6559 p += 2;
6560 l -= 2;
6561 }
6562
Bram Moolenaar371d5402006-03-20 21:47:49 +00006563 ++l;
6564 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006566 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006568 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006570 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006572 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006574 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006576 else if (STRNICMP(p, "range>", l) == 0)
6577 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006578 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006580 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006582 else if (STRNICMP(p, "mods>", l) == 0)
6583 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584
6585 switch (type)
6586 {
6587 case ct_ARGS:
6588 /* Simple case first */
6589 if (*eap->arg == NUL)
6590 {
6591 if (quote == 1)
6592 {
6593 result = 2;
6594 if (buf != NULL)
6595 STRCPY(buf, "''");
6596 }
6597 else
6598 result = 0;
6599 break;
6600 }
6601
6602 /* When specified there is a single argument don't split it.
6603 * Works for ":Cmd %" when % is "a b c". */
6604 if ((eap->argt & NOSPC) && quote == 2)
6605 quote = 1;
6606
6607 switch (quote)
6608 {
6609 case 0: /* No quoting, no splitting */
6610 result = STRLEN(eap->arg);
6611 if (buf != NULL)
6612 STRCPY(buf, eap->arg);
6613 break;
6614 case 1: /* Quote, but don't split */
6615 result = STRLEN(eap->arg) + 2;
6616 for (p = eap->arg; *p; ++p)
6617 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006618#ifdef FEAT_MBYTE
6619 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6620 /* DBCS can contain \ in a trail byte, skip the
6621 * double-byte character. */
6622 ++p;
6623 else
6624#endif
6625 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 ++result;
6627 }
6628
6629 if (buf != NULL)
6630 {
6631 *buf++ = '"';
6632 for (p = eap->arg; *p; ++p)
6633 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006634#ifdef FEAT_MBYTE
6635 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6636 /* DBCS can contain \ in a trail byte, copy the
6637 * double-byte character to avoid escaping. */
6638 *buf++ = *p++;
6639 else
6640#endif
6641 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 *buf++ = '\\';
6643 *buf++ = *p;
6644 }
6645 *buf = '"';
6646 }
6647
6648 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006649 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 /* This is hard, so only do it once, and cache the result */
6651 if (*split_buf == NULL)
6652 *split_buf = uc_split_args(eap->arg, split_len);
6653
6654 result = *split_len;
6655 if (buf != NULL && result != 0)
6656 STRCPY(buf, *split_buf);
6657
6658 break;
6659 }
6660 break;
6661
6662 case ct_BANG:
6663 result = eap->forceit ? 1 : 0;
6664 if (quote)
6665 result += 2;
6666 if (buf != NULL)
6667 {
6668 if (quote)
6669 *buf++ = '"';
6670 if (eap->forceit)
6671 *buf++ = '!';
6672 if (quote)
6673 *buf = '"';
6674 }
6675 break;
6676
6677 case ct_LINE1:
6678 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006679 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 case ct_COUNT:
6681 {
6682 char num_buf[20];
6683 long num = (type == ct_LINE1) ? eap->line1 :
6684 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006685 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6687 size_t num_len;
6688
6689 sprintf(num_buf, "%ld", num);
6690 num_len = STRLEN(num_buf);
6691 result = num_len;
6692
6693 if (quote)
6694 result += 2;
6695
6696 if (buf != NULL)
6697 {
6698 if (quote)
6699 *buf++ = '"';
6700 STRCPY(buf, num_buf);
6701 buf += num_len;
6702 if (quote)
6703 *buf = '"';
6704 }
6705
6706 break;
6707 }
6708
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006709 case ct_MODS:
6710 {
6711 int multi_mods = 0;
6712 typedef struct {
6713 int *varp;
6714 char *name;
6715 } mod_entry_T;
6716 static mod_entry_T mod_entries[] = {
6717#ifdef FEAT_BROWSE_CMD
6718 {&cmdmod.browse, "browse"},
6719#endif
6720#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6721 {&cmdmod.confirm, "confirm"},
6722#endif
6723 {&cmdmod.hide, "hide"},
6724 {&cmdmod.keepalt, "keepalt"},
6725 {&cmdmod.keepjumps, "keepjumps"},
6726 {&cmdmod.keepmarks, "keepmarks"},
6727 {&cmdmod.keeppatterns, "keeppatterns"},
6728 {&cmdmod.lockmarks, "lockmarks"},
6729 {&cmdmod.noswapfile, "noswapfile"},
6730 {NULL, NULL}
6731 };
6732 int i;
6733
6734 result = quote ? 2 : 0;
6735 if (buf != NULL)
6736 {
6737 if (quote)
6738 *buf++ = '"';
6739 *buf = '\0';
6740 }
6741
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006742 /* :aboveleft and :leftabove */
6743 if (cmdmod.split & WSP_ABOVE)
6744 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6745 /* :belowright and :rightbelow */
6746 if (cmdmod.split & WSP_BELOW)
6747 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6748 /* :botright */
6749 if (cmdmod.split & WSP_BOT)
6750 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006751
6752 /* the modifiers that are simple flags */
6753 for (i = 0; mod_entries[i].varp != NULL; ++i)
6754 if (*mod_entries[i].varp)
6755 result += add_cmd_modifier(buf, mod_entries[i].name,
6756 &multi_mods);
6757
6758 /* TODO: How to support :noautocmd? */
6759#ifdef HAVE_SANDBOX
6760 /* TODO: How to support :sandbox?*/
6761#endif
6762 /* :silent */
6763 if (msg_silent > 0)
6764 result += add_cmd_modifier(buf,
6765 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006766 /* :tab */
6767 if (cmdmod.tab > 0)
6768 result += add_cmd_modifier(buf, "tab", &multi_mods);
6769 /* :topleft */
6770 if (cmdmod.split & WSP_TOP)
6771 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006772 /* TODO: How to support :unsilent?*/
6773 /* :verbose */
6774 if (p_verbose > 0)
6775 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006776 /* :vertical */
6777 if (cmdmod.split & WSP_VERT)
6778 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006779 if (quote && buf != NULL)
6780 {
6781 buf += result - 2;
6782 *buf = '"';
6783 }
6784 break;
6785 }
6786
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 case ct_REGISTER:
6788 result = eap->regname ? 1 : 0;
6789 if (quote)
6790 result += 2;
6791 if (buf != NULL)
6792 {
6793 if (quote)
6794 *buf++ = '\'';
6795 if (eap->regname)
6796 *buf++ = eap->regname;
6797 if (quote)
6798 *buf = '\'';
6799 }
6800 break;
6801
6802 case ct_LT:
6803 result = 1;
6804 if (buf != NULL)
6805 *buf = '<';
6806 break;
6807
6808 default:
6809 /* Not recognized: just copy the '<' and return -1. */
6810 result = (size_t)-1;
6811 if (buf != NULL)
6812 *buf = '<';
6813 break;
6814 }
6815
6816 return result;
6817}
6818
6819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006820do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821{
6822 char_u *buf;
6823 char_u *p;
6824 char_u *q;
6825
6826 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006827 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006828 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 size_t len, totlen;
6830
6831 size_t split_len = 0;
6832 char_u *split_buf = NULL;
6833 ucmd_T *cmd;
6834#ifdef FEAT_EVAL
6835 scid_T save_current_SID = current_SID;
6836#endif
6837
6838 if (eap->cmdidx == CMD_USER)
6839 cmd = USER_CMD(eap->useridx);
6840 else
6841 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6842
6843 /*
6844 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006845 * First round: "buf" is NULL, compute length, allocate "buf".
6846 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 */
6848 buf = NULL;
6849 for (;;)
6850 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006851 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006852 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006854
6855 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006857 start = vim_strchr(p, '<');
6858 if (start != NULL)
6859 end = vim_strchr(start + 1, '>');
6860 if (buf != NULL)
6861 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006862 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6863 ;
6864 if (*ksp == K_SPECIAL
6865 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006866 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6867# ifdef FEAT_GUI
6868 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6869# endif
6870 ))
6871 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006872 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006873 * KS_SPECIAL KE_FILLER, like for mappings, but
6874 * do_cmdline() doesn't handle that, so convert it back.
6875 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6876 len = ksp - p;
6877 if (len > 0)
6878 {
6879 mch_memmove(q, p, len);
6880 q += len;
6881 }
6882 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6883 p = ksp + 3;
6884 continue;
6885 }
6886 }
6887
6888 /* break if there no <item> is found */
6889 if (start == NULL || end == NULL)
6890 break;
6891
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 /* Include the '>' */
6893 ++end;
6894
6895 /* Take everything up to the '<' */
6896 len = start - p;
6897 if (buf == NULL)
6898 totlen += len;
6899 else
6900 {
6901 mch_memmove(q, p, len);
6902 q += len;
6903 }
6904
6905 len = uc_check_code(start, end - start, q, cmd, eap,
6906 &split_buf, &split_len);
6907 if (len == (size_t)-1)
6908 {
6909 /* no match, continue after '<' */
6910 p = start + 1;
6911 len = 1;
6912 }
6913 else
6914 p = end;
6915 if (buf == NULL)
6916 totlen += len;
6917 else
6918 q += len;
6919 }
6920 if (buf != NULL) /* second time here, finished */
6921 {
6922 STRCPY(q, p);
6923 break;
6924 }
6925
6926 totlen += STRLEN(p); /* Add on the trailing characters */
6927 buf = alloc((unsigned)(totlen + 1));
6928 if (buf == NULL)
6929 {
6930 vim_free(split_buf);
6931 return;
6932 }
6933 }
6934
6935#ifdef FEAT_EVAL
6936 current_SID = cmd->uc_scriptID;
6937#endif
6938 (void)do_cmdline(buf, eap->getline, eap->cookie,
6939 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6940#ifdef FEAT_EVAL
6941 current_SID = save_current_SID;
6942#endif
6943 vim_free(buf);
6944 vim_free(split_buf);
6945}
6946
6947# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6948 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006949get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950{
6951 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6952}
6953
6954/*
6955 * Function given to ExpandGeneric() to obtain the list of user command names.
6956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006958get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959{
6960 if (idx < curbuf->b_ucmds.ga_len)
6961 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6962 idx -= curbuf->b_ucmds.ga_len;
6963 if (idx < ucmds.ga_len)
6964 return USER_CMD(idx)->uc_name;
6965 return NULL;
6966}
6967
6968/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006969 * Function given to ExpandGeneric() to obtain the list of user address type names.
6970 */
6971 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006972get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006973{
6974 return (char_u *)addr_type_complete[idx].name;
6975}
6976
6977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 * Function given to ExpandGeneric() to obtain the list of user command
6979 * attributes.
6980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006982get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983{
6984 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006985 {"addr", "bang", "bar", "buffer", "complete",
6986 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006988 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 return NULL;
6990 return (char_u *)user_cmd_flags[idx];
6991}
6992
6993/*
6994 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006997get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998{
6999 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7000
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007001 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 return NULL;
7003 return (char_u *)user_cmd_nargs[idx];
7004}
7005
7006/*
7007 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007010get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011{
7012 return (char_u *)command_complete[idx].name;
7013}
7014# endif /* FEAT_CMDL_COMPL */
7015
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007016/*
7017 * Parse address type argument
7018 */
7019 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007020parse_addr_type_arg(
7021 char_u *value,
7022 int vallen,
7023 long *argt,
7024 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007025{
7026 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007027
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007028 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7029 {
7030 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7031 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7032 if (a && b)
7033 {
7034 *addr_type_arg = addr_type_complete[i].expand;
7035 break;
7036 }
7037 }
7038
7039 if (addr_type_complete[i].expand == -1)
7040 {
7041 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007042
Bram Moolenaar1c465442017-03-12 20:10:05 +01007043 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007044 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007045 err[i] = NUL;
7046 EMSG2(_("E180: Invalid address type value: %s"), err);
7047 return FAIL;
7048 }
7049
7050 if (*addr_type_arg != ADDR_LINES)
7051 *argt |= NOTADR;
7052
7053 return OK;
7054}
7055
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056#endif /* FEAT_USR_CMDS */
7057
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007058#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7059/*
7060 * Parse a completion argument "value[vallen]".
7061 * The detected completion goes in "*complp", argument type in "*argt".
7062 * When there is an argument, for function and user defined completion, it's
7063 * copied to allocated memory and stored in "*compl_arg".
7064 * Returns FAIL if something is wrong.
7065 */
7066 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007067parse_compl_arg(
7068 char_u *value,
7069 int vallen,
7070 int *complp,
7071 long *argt,
7072 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007073{
7074 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007075# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007076 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007077# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007078 int i;
7079 int valend = vallen;
7080
7081 /* Look for any argument part - which is the part after any ',' */
7082 for (i = 0; i < vallen; ++i)
7083 {
7084 if (value[i] == ',')
7085 {
7086 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007087# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007088 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007089# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007090 valend = i;
7091 break;
7092 }
7093 }
7094
7095 for (i = 0; command_complete[i].expand != 0; ++i)
7096 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007097 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007098 && STRNCMP(value, command_complete[i].name, valend) == 0)
7099 {
7100 *complp = command_complete[i].expand;
7101 if (command_complete[i].expand == EXPAND_BUFFERS)
7102 *argt |= BUFNAME;
7103 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7104 || command_complete[i].expand == EXPAND_FILES)
7105 *argt |= XFILE;
7106 break;
7107 }
7108 }
7109
7110 if (command_complete[i].expand == 0)
7111 {
7112 EMSG2(_("E180: Invalid complete value: %s"), value);
7113 return FAIL;
7114 }
7115
7116# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7117 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7118 && arg != NULL)
7119# else
7120 if (arg != NULL)
7121# endif
7122 {
7123 EMSG(_("E468: Completion argument only allowed for custom completion"));
7124 return FAIL;
7125 }
7126
7127# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7128 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7129 && arg == NULL)
7130 {
7131 EMSG(_("E467: Custom completion requires a function argument"));
7132 return FAIL;
7133 }
7134
7135 if (arg != NULL)
7136 *compl_arg = vim_strnsave(arg, (int)arglen);
7137# endif
7138 return OK;
7139}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007140
7141 int
7142cmdcomplete_str_to_type(char_u *complete_str)
7143{
7144 int i;
7145
7146 for (i = 0; command_complete[i].expand != 0; ++i)
7147 if (STRCMP(complete_str, command_complete[i].name) == 0)
7148 return command_complete[i].expand;
7149
7150 return EXPAND_NOTHING;
7151}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007152#endif
7153
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007155ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156{
Bram Moolenaare6850792010-05-14 15:28:44 +02007157 if (*eap->arg == NUL)
7158 {
7159#ifdef FEAT_EVAL
7160 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7161 char_u *p = NULL;
7162
7163 if (expr != NULL)
7164 {
7165 ++emsg_off;
7166 p = eval_to_string(expr, NULL, FALSE);
7167 --emsg_off;
7168 vim_free(expr);
7169 }
7170 if (p != NULL)
7171 {
7172 MSG(p);
7173 vim_free(p);
7174 }
7175 else
7176 MSG("default");
7177#else
7178 MSG(_("unknown"));
7179#endif
7180 }
7181 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007182 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183}
7184
7185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007186ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187{
7188 if (*eap->arg == NUL && eap->cmd[2] == '!')
7189 MSG(_("Greetings, Vim user!"));
7190 do_highlight(eap->arg, eap->forceit, FALSE);
7191}
7192
7193
7194/*
7195 * Call this function if we thought we were going to exit, but we won't
7196 * (because of an error). May need to restore the terminal mode.
7197 */
7198 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007199not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 exiting = FALSE;
7202 settmode(TMODE_RAW);
7203}
7204
7205/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007206 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 */
7208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007209ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007211 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007212
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213#ifdef FEAT_CMDWIN
7214 if (cmdwin_type != 0)
7215 {
7216 cmdwin_result = Ctrl_C;
7217 return;
7218 }
7219#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007220 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007221 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007222 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007223 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007224 return;
7225 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007226 if (eap->addr_count > 0)
7227 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007228 int wnr = eap->line2;
7229
7230 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7231 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007232 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007233 }
7234 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007235 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007236
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007237#ifdef FEAT_AUTOCMD
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007238 /* Refuse to quit when locked. */
7239 if (curbuf_locked())
7240 return;
7241 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7242 /* Bail out when autocommands closed the window.
7243 * Refuse to quit when the buffer in the last window is being closed (can
7244 * only happen in autocommands). */
7245 if (!win_valid(wp)
Bram Moolenaar0ea50702017-07-08 14:44:50 +02007246 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007247 return;
7248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249
7250#ifdef FEAT_NETBEANS_INTG
7251 netbeansForcedQuit = eap->forceit;
7252#endif
7253
7254 /*
7255 * If there are more files or windows we won't exit.
7256 */
7257 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7258 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007259 if ((!buf_hide(wp->w_buffer)
7260 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007261 | (eap->forceit ? CCGD_FORCEIT : 0)
7262 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007264 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {
7266 not_exiting();
7267 }
7268 else
7269 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007270 /* quit last window
7271 * Note: only_one_window() returns true, even so a help window is
7272 * still open. In that case only quit, if no address has been
7273 * specified. Example:
7274 * :h|wincmd w|1q - don't quit
7275 * :h|wincmd w|q - quit
7276 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007277 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007279 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007280#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007284 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 }
7286}
7287
7288/*
7289 * ":cquit".
7290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007292ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293{
7294 getout(1); /* this does not always pass on the exit code to the Manx
7295 compiler. why? */
7296}
7297
7298/*
7299 * ":qall": try to quit all windows
7300 */
7301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007302ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
7304# ifdef FEAT_CMDWIN
7305 if (cmdwin_type != 0)
7306 {
7307 if (eap->forceit)
7308 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7309 else
7310 cmdwin_result = K_XF2;
7311 return;
7312 }
7313# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007314
7315 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007316 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007317 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007318 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007319 return;
7320 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007321#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007322 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7323 /* Refuse to quit when locked or when the buffer in the last window is
7324 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007325 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007326 return;
7327#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007328
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007330 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 getout(0);
7332 not_exiting();
7333}
7334
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335/*
7336 * ":close": close current window, unless it is the last one
7337 */
7338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007339ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007341 win_T *win;
7342 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007343#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007345 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007347#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007348 if (!text_locked()
7349#ifdef FEAT_AUTOCMD
7350 && !curbuf_locked()
7351#endif
7352 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007353 {
7354 if (eap->addr_count == 0)
7355 ex_win_close(eap->forceit, curwin, NULL);
7356 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007357 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007358 {
7359 winnr++;
7360 if (winnr == eap->line2)
7361 break;
7362 }
7363 if (win == NULL)
7364 win = lastwin;
7365 ex_win_close(eap->forceit, win, NULL);
7366 }
7367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368}
7369
Bram Moolenaar4033c552017-09-16 20:54:51 +02007370#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007371/*
7372 * ":pclose": Close any preview window.
7373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007375ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007376{
7377 win_T *win;
7378
Bram Moolenaar29323592016-07-24 22:04:11 +02007379 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007380 if (win->w_p_pvw)
7381 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007382 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007383 break;
7384 }
7385}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007386#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007387
Bram Moolenaarf740b292006-02-16 22:11:02 +00007388/*
7389 * Close window "win" and take care of handling closing the last window for a
7390 * modified buffer.
7391 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007392 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007393ex_win_close(
7394 int forceit,
7395 win_T *win,
7396 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397{
7398 int need_hide;
7399 buf_T *buf = win->w_buffer;
7400
7401 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007402 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007404#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 if ((p_confirm || cmdmod.confirm) && p_write)
7406 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007407 bufref_T bufref;
7408
7409 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007411 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 return;
7413 need_hide = FALSE;
7414 }
7415 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007418 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 return;
7420 }
7421 }
7422
Bram Moolenaar4033c552017-09-16 20:54:51 +02007423#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007425#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007426
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007428 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007429 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007430 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007431 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432}
7433
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007435 * Handle the argument for a tabpage related ex command.
7436 * Returns a tabpage number.
7437 * When an error is encountered then eap->errmsg is set.
7438 */
7439 static int
7440get_tabpage_arg(exarg_T *eap)
7441{
7442 int tab_number;
7443 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7444
7445 if (eap->arg && *eap->arg != NUL)
7446 {
7447 char_u *p = eap->arg;
7448 char_u *p_save;
7449 int relative = 0; /* argument +N/-N means: go to N places to the
7450 * right/left relative to the current position. */
7451
7452 if (*p == '-')
7453 {
7454 relative = -1;
7455 p++;
7456 }
7457 else if (*p == '+')
7458 {
7459 relative = 1;
7460 p++;
7461 }
7462
7463 p_save = p;
7464 tab_number = getdigits(&p);
7465
7466 if (relative == 0)
7467 {
7468 if (STRCMP(p, "$") == 0)
7469 tab_number = LAST_TAB_NR;
7470 else if (p == p_save || *p_save == '-' || *p != NUL
7471 || tab_number > LAST_TAB_NR)
7472 {
7473 /* No numbers as argument. */
7474 eap->errmsg = e_invarg;
7475 goto theend;
7476 }
7477 }
7478 else
7479 {
7480 if (*p_save == NUL)
7481 tab_number = 1;
7482 else if (p == p_save || *p_save == '-' || *p != NUL
7483 || tab_number == 0)
7484 {
7485 /* No numbers as argument. */
7486 eap->errmsg = e_invarg;
7487 goto theend;
7488 }
7489 tab_number = tab_number * relative + tabpage_index(curtab);
7490 if (!unaccept_arg0 && relative == -1)
7491 --tab_number;
7492 }
7493 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7494 eap->errmsg = e_invarg;
7495 }
7496 else if (eap->addr_count > 0)
7497 {
7498 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007499 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007500 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007501 tab_number = 0;
7502 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007503 else
7504 {
7505 tab_number = eap->line2;
7506 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7507 {
7508 --tab_number;
7509 if (tab_number < unaccept_arg0)
7510 eap->errmsg = e_invarg;
7511 }
7512 }
7513 }
7514 else
7515 {
7516 switch (eap->cmdidx)
7517 {
7518 case CMD_tabnext:
7519 tab_number = tabpage_index(curtab) + 1;
7520 if (tab_number > LAST_TAB_NR)
7521 tab_number = 1;
7522 break;
7523 case CMD_tabmove:
7524 tab_number = LAST_TAB_NR;
7525 break;
7526 default:
7527 tab_number = tabpage_index(curtab);
7528 }
7529 }
7530
7531theend:
7532 return tab_number;
7533}
7534
7535/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007536 * ":tabclose": close current tab page, unless it is the last one.
7537 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 */
7539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007540ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007542 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007543 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007544
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007545# ifdef FEAT_CMDWIN
7546 if (cmdwin_type != 0)
7547 cmdwin_result = K_IGNORE;
7548 else
7549# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007550 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007551 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007552 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007554 tab_number = get_tabpage_arg(eap);
7555 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007556 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007557 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007558 if (tp == NULL)
7559 {
7560 beep_flush();
7561 return;
7562 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007563 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007564 {
7565 tabpage_close_other(tp, eap->forceit);
7566 return;
7567 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007568 else if (!text_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007569#ifdef FEAT_AUTOCMD
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007570 && !curbuf_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007571#endif
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007572 )
7573 tabpage_close(eap->forceit);
7574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007576}
7577
7578/*
7579 * ":tabonly": close all tab pages except the current one
7580 */
7581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007582ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007583{
7584 tabpage_T *tp;
7585 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007586 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007587
7588# ifdef FEAT_CMDWIN
7589 if (cmdwin_type != 0)
7590 cmdwin_result = K_IGNORE;
7591 else
7592# endif
7593 if (first_tabpage->tp_next == NULL)
7594 MSG(_("Already only one tab page"));
7595 else
7596 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007597 tab_number = get_tabpage_arg(eap);
7598 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007599 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007600 goto_tabpage(tab_number);
7601 /* Repeat this up to a 1000 times, because autocommands may
7602 * mess up the lists. */
7603 for (done = 0; done < 1000; ++done)
7604 {
7605 FOR_ALL_TABPAGES(tp)
7606 if (tp->tp_topframe != topframe)
7607 {
7608 tabpage_close_other(tp, eap->forceit);
7609 /* if we failed to close it quit */
7610 if (valid_tabpage(tp))
7611 done = 1000;
7612 /* start over, "tp" is now invalid */
7613 break;
7614 }
7615 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007616 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007617 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007618 }
7619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621
7622/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007623 * Close the current tab page.
7624 */
7625 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007626tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007627{
7628 /* First close all the windows but the current one. If that worked then
7629 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007630 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007631 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007632 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007633 ex_win_close(forceit, curwin, NULL);
7634# ifdef FEAT_GUI
7635 need_mouse_correct = TRUE;
7636# endif
7637}
7638
7639/*
7640 * Close tab page "tp", which is not the current tab page.
7641 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007642 * Also takes care of the tab pages line disappearing when closing the
7643 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007644 */
7645 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007646tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007647{
7648 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007649 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007650 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007651
7652 /* Limit to 1000 windows, autocommands may add a window while we close
7653 * one. OK, so I'm paranoid... */
7654 while (++done < 1000)
7655 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007656 wp = tp->tp_firstwin;
7657 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007658
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007659 /* Autocommands may delete the tab page under our fingers and we may
7660 * fail to close a window with a modified buffer. */
7661 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007662 break;
7663 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007664
Bram Moolenaar29323592016-07-24 22:04:11 +02007665#ifdef FEAT_AUTOCMD
7666 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7667#endif
7668
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007669 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007670 if (h != tabline_height())
7671 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007672}
7673
7674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 * ":only".
7676 */
7677 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007678ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007680 win_T *wp;
7681 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682# ifdef FEAT_GUI
7683 need_mouse_correct = TRUE;
7684# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007685 if (eap->addr_count > 0)
7686 {
7687 wnr = eap->line2;
7688 for (wp = firstwin; --wnr > 0; )
7689 {
7690 if (wp->w_next == NULL)
7691 break;
7692 else
7693 wp = wp->w_next;
7694 }
7695 win_goto(wp);
7696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 close_others(TRUE, eap->forceit);
7698}
7699
7700/*
7701 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007702 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007704 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007705ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706{
7707 if (eap->addr_count == 0)
7708 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007709 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711
7712 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007713ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007715 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007716 if (!eap->skip)
7717 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007718#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007719 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007720#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007721 if (eap->addr_count == 0)
7722 win_close(curwin, FALSE); /* don't free buffer */
7723 else
7724 {
7725 int winnr = 0;
7726 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007727
Bram Moolenaar2256c992016-11-15 21:17:07 +01007728 FOR_ALL_WINDOWS(win)
7729 {
7730 winnr++;
7731 if (winnr == eap->line2)
7732 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007733 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007734 if (win == NULL)
7735 win = lastwin;
7736 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 }
7739}
7740
7741/*
7742 * ":stop" and ":suspend": Suspend Vim.
7743 */
7744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007745ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746{
7747 /*
7748 * Disallow suspending for "rvim".
7749 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007750 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {
7752 if (!eap->forceit)
7753 autowrite_all();
7754 windgoto((int)Rows - 1, 0);
7755 out_char('\n');
7756 out_flush();
7757 stoptermcap();
7758 out_flush(); /* needed for SUN to restore xterm buffer */
7759#ifdef FEAT_TITLE
7760 mch_restore_title(3); /* restore window titles */
7761#endif
7762 ui_suspend(); /* call machine specific function */
7763#ifdef FEAT_TITLE
7764 maketitle();
7765 resettitle(); /* force updating the title */
7766#endif
7767 starttermcap();
7768 scroll_start(); /* scroll screen before redrawing */
7769 redraw_later_clear();
7770 shell_resized(); /* may have resized window */
7771 }
7772}
7773
7774/*
7775 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7776 */
7777 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007778ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
7780#ifdef FEAT_CMDWIN
7781 if (cmdwin_type != 0)
7782 {
7783 cmdwin_result = Ctrl_C;
7784 return;
7785 }
7786#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007787 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007788 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007789 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007790 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007791 return;
7792 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007793#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007794 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7795 /* Refuse to quit when locked or when the buffer in the last window is
7796 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007797 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007798 return;
7799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800
7801 /*
7802 * if more files or windows we won't exit
7803 */
7804 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7805 exiting = TRUE;
7806 if ( ((eap->cmdidx == CMD_wq
7807 || curbufIsChanged())
7808 && do_write(eap) == FAIL)
7809 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007810 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 {
7812 not_exiting();
7813 }
7814 else
7815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007818 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819# ifdef FEAT_GUI
7820 need_mouse_correct = TRUE;
7821# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007822 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007823 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 }
7825}
7826
7827/*
7828 * ":print", ":list", ":number".
7829 */
7830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007831ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007833 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7834 EMSG(_(e_emptybuf));
7835 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007837 for ( ;!got_int; ui_breakcheck())
7838 {
7839 print_line(eap->line1,
7840 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7841 || (eap->flags & EXFLAG_NR)),
7842 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7843 if (++eap->line1 > eap->line2)
7844 break;
7845 out_flush(); /* show one line at a time */
7846 }
7847 setpcmark();
7848 /* put cursor at last line */
7849 curwin->w_cursor.lnum = eap->line2;
7850 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 }
7852
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854}
7855
7856#ifdef FEAT_BYTEOFF
7857 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007858ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
7860 goto_byte(eap->line2);
7861}
7862#endif
7863
7864/*
7865 * ":shell".
7866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007868ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869{
7870 do_shell(NULL, 0);
7871}
7872
Bram Moolenaar4033c552017-09-16 20:54:51 +02007873#if defined(HAVE_DROP_FILE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007875 || defined(FEAT_GUI_MSWIN) \
7876 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 || defined(PROTO)
7878
7879/*
7880 * Handle a file drop. The code is here because a drop is *nearly* like an
7881 * :args command, but not quite (we have a list of exact filenames, so we
7882 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7883 * code is very similar to :args and hence needs access to a lot of the static
7884 * functions in this file.
7885 *
7886 * The list should be allocated using alloc(), as should each item in the
7887 * list. This function takes over responsibility for freeing the list.
7888 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007889 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007890 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 */
7892 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007893handle_drop(
7894 int filec, /* the number of files dropped */
7895 char_u **filev, /* the list of files dropped */
7896 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897{
7898 exarg_T ea;
7899 int save_msg_scroll = msg_scroll;
7900
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007901 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007902 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007904#ifdef FEAT_AUTOCMD
7905 if (curbuf_locked())
7906 return;
7907#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007908 /* When the screen is being updated we should not change buffers and
7909 * windows structures, it may cause freed memory to be used. */
7910 if (updating_screen)
7911 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912
7913 /* Check whether the current buffer is changed. If so, we will need
7914 * to split the current window or data could be lost.
7915 * We don't need to check if the 'hidden' option is set, as in this
7916 * case the buffer won't be lost.
7917 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007918 if (!buf_hide(curbuf) && !split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {
7920 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007921 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 --emsg_off;
7923 }
7924 if (split)
7925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 if (win_split(0, 0) == FAIL)
7927 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007928 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929
7930 /* When splitting the window, create a new alist. Otherwise the
7931 * existing one is overwritten. */
7932 alist_unlink(curwin->w_alist);
7933 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 }
7935
7936 /*
7937 * Set up the new argument list.
7938 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007939 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940
7941 /*
7942 * Move to the first file.
7943 */
7944 /* Fake up a minimal "next" command for do_argfile() */
7945 vim_memset(&ea, 0, sizeof(ea));
7946 ea.cmd = (char_u *)"next";
7947 do_argfile(&ea, 0);
7948
7949 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7950 * mode that is not needed here. */
7951 need_start_insertmode = FALSE;
7952
7953 /* Restore msg_scroll, otherwise a following command may cause scrolling
7954 * unexpectedly. The screen will be redrawn by the caller, thus
7955 * msg_scroll being set by displaying a message is irrelevant. */
7956 msg_scroll = save_msg_scroll;
7957}
7958#endif
7959
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960/*
7961 * Clear an argument list: free all file names and reset it to zero entries.
7962 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007963 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007964alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965{
7966 while (--al->al_ga.ga_len >= 0)
7967 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7968 ga_clear(&al->al_ga);
7969}
7970
7971/*
7972 * Init an argument list.
7973 */
7974 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007975alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7978}
7979
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980/*
7981 * Remove a reference from an argument list.
7982 * Ignored when the argument list is the global one.
7983 * If the argument list is no longer used by any window, free it.
7984 */
7985 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007986alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
7988 if (al != &global_alist && --al->al_refcount <= 0)
7989 {
7990 alist_clear(al);
7991 vim_free(al);
7992 }
7993}
7994
Bram Moolenaar4033c552017-09-16 20:54:51 +02007995#if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996/*
7997 * Create a new argument list and use it for the current window.
7998 */
7999 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008000alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001{
8002 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8003 if (curwin->w_alist == NULL)
8004 {
8005 curwin->w_alist = &global_alist;
8006 ++global_alist.al_refcount;
8007 }
8008 else
8009 {
8010 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008011 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 alist_init(curwin->w_alist);
8013 }
8014}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015#endif
8016
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008017#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018/*
8019 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008020 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8021 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 */
8023 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008024alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025{
8026 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008027 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 char_u **new_arg_files;
8029 int new_arg_file_count;
8030 char_u *save_p_su = p_su;
8031 int i;
8032
8033 /* Don't use 'suffixes' here. This should work like the shell did the
8034 * expansion. Also, the vimrc file isn't read yet, thus the user
8035 * can't set the options. */
8036 p_su = empty_option;
8037 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8038 if (old_arg_files != NULL)
8039 {
8040 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008041 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8042 old_arg_count = GARGCOUNT;
8043 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008045 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 && new_arg_file_count > 0)
8047 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008048 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8049 TRUE, fnum_list, fnum_len);
8050 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 }
8053 p_su = save_p_su;
8054}
8055#endif
8056
8057/*
8058 * Set the argument list for the current window.
8059 * Takes over the allocated files[] and the allocated fnames in it.
8060 */
8061 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008062alist_set(
8063 alist_T *al,
8064 int count,
8065 char_u **files,
8066 int use_curbuf,
8067 int *fnum_list,
8068 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069{
8070 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008071 static int recursive = 0;
8072
8073 if (recursive)
8074 {
8075#ifdef FEAT_AUTOCMD
8076 EMSG(_(e_au_recursive));
8077#endif
8078 return;
8079 }
8080 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081
8082 alist_clear(al);
8083 if (ga_grow(&al->al_ga, count) == OK)
8084 {
8085 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008086 {
8087 if (got_int)
8088 {
8089 /* When adding many buffers this can take a long time. Allow
8090 * interrupting here. */
8091 while (i < count)
8092 vim_free(files[i++]);
8093 break;
8094 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008095
8096 /* May set buffer name of a buffer previously used for the
8097 * argument list, so that it's re-used by alist_add. */
8098 if (fnum_list != NULL && i < fnum_len)
8099 buf_set_name(fnum_list[i], files[i]);
8100
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008102 ui_breakcheck();
8103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 vim_free(files);
8105 }
8106 else
8107 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008110
8111 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112}
8113
8114/*
8115 * Add file "fname" to argument list "al".
8116 * "fname" must have been allocated and "al" must have been checked for room.
8117 */
8118 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008119alist_add(
8120 alist_T *al,
8121 char_u *fname,
8122 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123{
8124 if (fname == NULL) /* don't add NULL file names */
8125 return;
8126#ifdef BACKSLASH_IN_FILENAME
8127 slash_adjust(fname);
8128#endif
8129 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8130 if (set_fnum > 0)
8131 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8132 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8133 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134}
8135
8136#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8137/*
8138 * Adjust slashes in file names. Called after 'shellslash' was set.
8139 */
8140 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008141alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142{
8143 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008145 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146
8147 for (i = 0; i < GARGCOUNT; ++i)
8148 if (GARGLIST[i].ae_fname != NULL)
8149 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008150 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 if (wp->w_alist != &global_alist)
8152 for (i = 0; i < WARGCOUNT(wp); ++i)
8153 if (WARGLIST(wp)[i].ae_fname != NULL)
8154 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155}
8156#endif
8157
8158/*
8159 * ":preserve".
8160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008162ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008164 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 ml_preserve(curbuf, TRUE);
8166}
8167
8168/*
8169 * ":recover".
8170 */
8171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008172ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173{
8174 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8175 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008176 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8177 | CCGD_MULTWIN
8178 | (eap->forceit ? CCGD_FORCEIT : 0)
8179 | CCGD_EXCMD)
8180
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 && (*eap->arg == NUL
8182 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8183 ml_recover();
8184 recoverymode = FALSE;
8185}
8186
8187/*
8188 * Command modifier used in a wrong way.
8189 */
8190 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008191ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192{
8193 eap->errmsg = e_invcmd;
8194}
8195
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196/*
8197 * :sview [+command] file split window with new file, read-only
8198 * :split [[+command] file] split window with current or new file
8199 * :vsplit [[+command] file] split window vertically with current or new file
8200 * :new [[+command] file] split window with no or new file
8201 * :vnew [[+command] file] split vertically window with no or new file
8202 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008203 *
8204 * :tabedit open new Tab page with empty window
8205 * :tabedit [+command] file open new Tab page and edit "file"
8206 * :tabnew [[+command] file] just like :tabedit
8207 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 */
8209 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008210ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008212 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008213#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008215#endif
8216#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219
Bram Moolenaar4033c552017-09-16 20:54:51 +02008220#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223
Bram Moolenaar4033c552017-09-16 20:54:51 +02008224#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008226 * quickfix windows. But it's OK when doing ":tab split". */
8227 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {
8229 if (eap->cmdidx == CMD_split)
8230 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 if (eap->cmdidx == CMD_vsplit)
8232 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235
Bram Moolenaar4033c552017-09-16 20:54:51 +02008236#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008237 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {
8239 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8240 FNAME_MESS, TRUE, curbuf->b_ffname);
8241 if (fname == NULL)
8242 goto theend;
8243 eap->arg = fname;
8244 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008245# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008246 else
8247# endif
8248#endif
8249#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 && eap->cmdidx != CMD_new)
8253 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008254# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008255 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008256# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008257 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008258# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008259 au_has_group((char_u *)"FileExplorer"))
8260 {
8261 /* No browsing supported but we do have the file explorer:
8262 * Edit the directory. */
8263 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8264 eap->arg = (char_u *)".";
8265 }
8266 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008267# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008268 {
8269 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008271 if (fname == NULL)
8272 goto theend;
8273 eap->arg = fname;
8274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 }
8276 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008279 /*
8280 * Either open new tab page or split the window.
8281 */
8282 if (eap->cmdidx == CMD_tabedit
8283 || eap->cmdidx == CMD_tabfind
8284 || eap->cmdidx == CMD_tabnew)
8285 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008286 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8287 : eap->addr_count == 0 ? 0
8288 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008289 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008290 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008291
8292 /* set the alternate buffer for the window we came from */
8293 if (curwin != old_curwin
8294 && win_valid(old_curwin)
8295 && old_curwin->w_buffer != curbuf
8296 && !cmdmod.keepalt)
8297 old_curwin->w_alt_fnum = curbuf->b_fnum;
8298 }
8299 }
8300 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8302 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008303# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 /* Reset 'scrollbind' when editing another file, but keep it when
8305 * doing ":split" without arguments. */
8306 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008307# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008309# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008311 {
8312 RESET_BINDING(curwin);
8313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 else
8315 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008316# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 do_exedit(eap, old_curwin);
8318 }
8319
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008320# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008324# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325theend:
8326 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008327# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008329
8330/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008331 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008332 */
8333 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008334tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008335{
8336 exarg_T ea;
8337
8338 vim_memset(&ea, 0, sizeof(ea));
8339 ea.cmdidx = CMD_tabnew;
8340 ea.cmd = (char_u *)"tabn";
8341 ea.arg = (char_u *)"";
8342 ex_splitview(&ea);
8343}
8344
8345/*
8346 * :tabnext command
8347 */
8348 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008349ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008350{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008351 int tab_number;
8352
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008353 switch (eap->cmdidx)
8354 {
8355 case CMD_tabfirst:
8356 case CMD_tabrewind:
8357 goto_tabpage(1);
8358 break;
8359 case CMD_tablast:
8360 goto_tabpage(9999);
8361 break;
8362 case CMD_tabprevious:
8363 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008364 if (eap->arg && *eap->arg != NUL)
8365 {
8366 char_u *p = eap->arg;
8367 char_u *p_save = p;
8368
8369 tab_number = getdigits(&p);
8370 if (p == p_save || *p_save == '-' || *p != NUL
8371 || tab_number == 0)
8372 {
8373 /* No numbers as argument. */
8374 eap->errmsg = e_invarg;
8375 return;
8376 }
8377 }
8378 else
8379 {
8380 if (eap->addr_count == 0)
8381 tab_number = 1;
8382 else
8383 {
8384 tab_number = eap->line2;
8385 if (tab_number < 1)
8386 {
8387 eap->errmsg = e_invrange;
8388 return;
8389 }
8390 }
8391 }
8392 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008393 break;
8394 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008395 tab_number = get_tabpage_arg(eap);
8396 if (eap->errmsg == NULL)
8397 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008398 break;
8399 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008400}
8401
8402/*
8403 * :tabmove command
8404 */
8405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008406ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008407{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008408 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008409
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008410 tab_number = get_tabpage_arg(eap);
8411 if (eap->errmsg == NULL)
8412 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008413}
8414
8415/*
8416 * :tabs command: List tabs and their contents.
8417 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008419ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008420{
8421 tabpage_T *tp;
8422 win_T *wp;
8423 int tabcount = 1;
8424
8425 msg_start();
8426 msg_scroll = TRUE;
8427 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8428 {
8429 msg_putchar('\n');
8430 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008431 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008432 out_flush(); /* output one line at a time */
8433 ui_breakcheck();
8434
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008435 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008436 wp = firstwin;
8437 else
8438 wp = tp->tp_firstwin;
8439 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8440 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008441 msg_putchar('\n');
8442 msg_putchar(wp == curwin ? '>' : ' ');
8443 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008444 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8445 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008446 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008447 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008448 else
8449 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8450 IObuff, IOSIZE, TRUE);
8451 msg_outtrans(IObuff);
8452 out_flush(); /* output one line at a time */
8453 ui_breakcheck();
8454 }
8455 }
8456}
8457
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458/*
8459 * ":mode": Set screen mode.
8460 * If no argument given, just get the screen size and redraw.
8461 */
8462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008463ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464{
8465 if (*eap->arg == NUL)
8466 shell_resized();
8467 else
8468 mch_screenmode(eap->arg);
8469}
8470
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471/*
8472 * ":resize".
8473 * set, increment or decrement current window height
8474 */
8475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008476ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477{
8478 int n;
8479 win_T *wp = curwin;
8480
8481 if (eap->addr_count > 0)
8482 {
8483 n = eap->line2;
8484 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8485 ;
8486 }
8487
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008488# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008490# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 if (cmdmod.split & WSP_VERT)
8493 {
8494 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008495 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8497 n = 9999;
8498 win_setwidth_win((int)n, wp);
8499 }
8500 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 {
8502 if (*eap->arg == '-' || *eap->arg == '+')
8503 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008504 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 n = 9999;
8506 win_setheight_win((int)n, wp);
8507 }
8508}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509
8510/*
8511 * ":find [+command] <file>" command.
8512 */
8513 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008514ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515{
8516#ifdef FEAT_SEARCHPATH
8517 char_u *fname;
8518 int count;
8519
8520 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8521 TRUE, curbuf->b_ffname);
8522 if (eap->addr_count > 0)
8523 {
8524 /* Repeat finding the file "count" times. This matters when it
8525 * appears several times in the path. */
8526 count = eap->line2;
8527 while (fname != NULL && --count > 0)
8528 {
8529 vim_free(fname);
8530 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8531 FALSE, curbuf->b_ffname);
8532 }
8533 }
8534
8535 if (fname != NULL)
8536 {
8537 eap->arg = fname;
8538#endif
8539 do_exedit(eap, NULL);
8540#ifdef FEAT_SEARCHPATH
8541 vim_free(fname);
8542 }
8543#endif
8544}
8545
8546/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008547 * ":open" simulation: for now just work like ":visual".
8548 */
8549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008550ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008551{
8552 regmatch_T regmatch;
8553 char_u *p;
8554
8555 curwin->w_cursor.lnum = eap->line2;
8556 beginline(BL_SOL | BL_FIX);
8557 if (*eap->arg == '/')
8558 {
8559 /* ":open /pattern/": put cursor in column found with pattern */
8560 ++eap->arg;
8561 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8562 *p = NUL;
8563 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8564 if (regmatch.regprog != NULL)
8565 {
8566 regmatch.rm_ic = p_ic;
8567 p = ml_get_curline();
8568 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008569 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008570 else
8571 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008572 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008573 }
8574 /* Move to the NUL, ignore any other arguments. */
8575 eap->arg += STRLEN(eap->arg);
8576 }
8577 check_cursor();
8578
8579 eap->cmdidx = CMD_visual;
8580 do_exedit(eap, NULL);
8581}
8582
8583/*
8584 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 */
8586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008587ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588{
8589 do_exedit(eap, NULL);
8590}
8591
8592/*
8593 * ":edit <file>" command and alikes.
8594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008596do_exedit(
8597 exarg_T *eap,
8598 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599{
8600 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008602 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603
8604 /*
8605 * ":vi" command ends Ex mode.
8606 */
8607 if (exmode_active && (eap->cmdidx == CMD_visual
8608 || eap->cmdidx == CMD_view))
8609 {
8610 exmode_active = FALSE;
8611 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008612 {
8613 /* Special case: ":global/pat/visual\NLvi-commands" */
8614 if (global_busy)
8615 {
8616 int rd = RedrawingDisabled;
8617 int nwr = no_wait_return;
8618 int ms = msg_scroll;
8619#ifdef FEAT_GUI
8620 int he = hold_gui_events;
8621#endif
8622
8623 if (eap->nextcmd != NULL)
8624 {
8625 stuffReadbuff(eap->nextcmd);
8626 eap->nextcmd = NULL;
8627 }
8628
8629 if (exmode_was != EXMODE_VIM)
8630 settmode(TMODE_RAW);
8631 RedrawingDisabled = 0;
8632 no_wait_return = 0;
8633 need_wait_return = FALSE;
8634 msg_scroll = 0;
8635#ifdef FEAT_GUI
8636 hold_gui_events = 0;
8637#endif
8638 must_redraw = CLEAR;
8639
8640 main_loop(FALSE, TRUE);
8641
8642 RedrawingDisabled = rd;
8643 no_wait_return = nwr;
8644 msg_scroll = ms;
8645#ifdef FEAT_GUI
8646 hold_gui_events = he;
8647#endif
8648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 }
8652
8653 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008654 || eap->cmdidx == CMD_tabnew
8655 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008656 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008658 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 setpcmark();
8660 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008661 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8662 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008664 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 || *eap->arg != NUL
8666#ifdef FEAT_BROWSE
8667 || cmdmod.browse
8668#endif
8669 )
8670 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008671#ifdef FEAT_AUTOCMD
8672 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8673 * can bring us here, others are stopped earlier. */
8674 if (*eap->arg != NUL && curbuf_locked())
8675 return;
8676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 n = readonlymode;
8678 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8679 readonlymode = TRUE;
8680 else if (eap->cmdidx == CMD_enew)
8681 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8682 empty buffer */
8683 setpcmark();
8684 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8685 NULL, eap,
8686 /* ":edit" goes to first line if Vi compatible */
8687 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8688 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8689 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008690 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008692 /* after a split we can use an existing buffer */
8693 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694#ifdef FEAT_LISTCMDS
8695 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8696#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008697 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 {
8699 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 if (old_curwin != NULL)
8701 {
8702 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008703 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02008705#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008706 cleanup_T cs;
8707
8708 /* Reset the error/interrupt/exception state here so that
8709 * aborting() returns FALSE when closing a window. */
8710 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008711#endif
8712#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008714#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008715 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008716
Bram Moolenaar4033c552017-09-16 20:54:51 +02008717#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008718 /* Restore the error/interrupt/exception state if not
8719 * discarded by a new aborting error, interrupt, or
8720 * uncaught exception. */
8721 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 }
8724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 }
8726 else if (readonlymode && curbuf->b_nwindows == 1)
8727 {
8728 /* When editing an already visited buffer, 'readonly' won't be set
8729 * but the previous value is kept. With ":view" and ":sview" we
8730 * want the file to be readonly, except when another window is
8731 * editing the same buffer. */
8732 curbuf->b_p_ro = TRUE;
8733 }
8734 readonlymode = n;
8735 }
8736 else
8737 {
8738 if (eap->do_ecmd_cmd != NULL)
8739 do_cmdline_cmd(eap->do_ecmd_cmd);
8740#ifdef FEAT_TITLE
8741 n = curwin->w_arg_idx_invalid;
8742#endif
8743 check_arg_idx(curwin);
8744#ifdef FEAT_TITLE
8745 if (n != curwin->w_arg_idx_invalid)
8746 maketitle();
8747#endif
8748 }
8749
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 /*
8751 * if ":split file" worked, set alternate file name in old window to new
8752 * file
8753 */
8754 if (old_curwin != NULL
8755 && *eap->arg != NUL
8756 && curwin != old_curwin
8757 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008758 && old_curwin->w_buffer != curbuf
8759 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761
8762 ex_no_reprint = TRUE;
8763}
8764
8765#ifndef FEAT_GUI
8766/*
8767 * ":gui" and ":gvim" when there is no GUI.
8768 */
8769 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008770ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771{
8772 eap->errmsg = e_nogvim;
8773}
8774#endif
8775
8776#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8777 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008778ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779{
8780 gui_make_tearoff(eap->arg);
8781}
8782#endif
8783
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008784#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008786ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008788 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789}
8790#endif
8791
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008793ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
8795 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8796 MSG(_("No swap file"));
8797 else
8798 msg(curbuf->b_ml.ml_mfp->mf_fname);
8799}
8800
8801/*
8802 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8803 * offset.
8804 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008807ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808{
8809#ifdef FEAT_SCROLLBIND
8810 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008811 win_T *save_curwin = curwin;
8812 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 long topline;
8814 long y;
8815 linenr_T old_linenr = curwin->w_cursor.lnum;
8816
8817 setpcmark();
8818
8819 /*
8820 * determine max topline
8821 */
8822 if (curwin->w_p_scb)
8823 {
8824 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008825 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 {
8827 if (wp->w_p_scb && wp->w_buffer)
8828 {
8829 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8830 if (topline > y)
8831 topline = y;
8832 }
8833 }
8834 if (topline < 1)
8835 topline = 1;
8836 }
8837 else
8838 {
8839 topline = 1;
8840 }
8841
8842
8843 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008844 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008846 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 {
8848 if (curwin->w_p_scb)
8849 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008850 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 y = topline - curwin->w_topline;
8852 if (y > 0)
8853 scrollup(y, TRUE);
8854 else
8855 scrolldown(-y, TRUE);
8856 curwin->w_scbind_pos = topline;
8857 redraw_later(VALID);
8858 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 }
8861 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008862 curwin = save_curwin;
8863 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 if (curwin->w_p_scb)
8865 {
8866 did_syncbind = TRUE;
8867 checkpcmark();
8868 if (old_linenr != curwin->w_cursor.lnum)
8869 {
8870 char_u ctrl_o[2];
8871
8872 ctrl_o[0] = Ctrl_O;
8873 ctrl_o[1] = 0;
8874 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8875 }
8876 }
8877#endif
8878}
8879
8880
8881 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008882ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008884 int i;
8885 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8886 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887
8888 if (eap->usefilter) /* :r!cmd */
8889 do_bang(1, eap, FALSE, FALSE, TRUE);
8890 else
8891 {
8892 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8893 return;
8894
8895#ifdef FEAT_BROWSE
8896 if (cmdmod.browse)
8897 {
8898 char_u *browseFile;
8899
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008900 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 NULL, NULL, NULL, curbuf);
8902 if (browseFile != NULL)
8903 {
8904 i = readfile(browseFile, NULL,
8905 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8906 vim_free(browseFile);
8907 }
8908 else
8909 i = OK;
8910 }
8911 else
8912#endif
8913 if (*eap->arg == NUL)
8914 {
8915 if (check_fname() == FAIL) /* check for no file name */
8916 return;
8917 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8918 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8919 }
8920 else
8921 {
8922 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8923 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8924 i = readfile(eap->arg, NULL,
8925 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8926
8927 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008928 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 {
8930#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8931 if (!aborting())
8932#endif
8933 EMSG2(_(e_notopen), eap->arg);
8934 }
8935 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008936 {
8937 if (empty && exmode_active)
8938 {
8939 /* Delete the empty line that remains. Historically ex does
8940 * this but vi doesn't. */
8941 if (eap->line2 == 0)
8942 lnum = curbuf->b_ml.ml_line_count;
8943 else
8944 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008945 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008946 {
8947 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008948 if (curwin->w_cursor.lnum > 1
8949 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008950 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008951 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008952 }
8953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 }
8957}
8958
Bram Moolenaarea408852005-06-25 22:49:46 +00008959static char_u *prev_dir = NULL;
8960
8961#if defined(EXITFREE) || defined(PROTO)
8962 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008963free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008964{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008965 VIM_CLEAR(prev_dir);
8966 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00008967}
8968#endif
8969
Bram Moolenaarf4258302013-06-02 18:20:17 +02008970/*
8971 * Deal with the side effects of changing the current directory.
8972 * When "local" is TRUE then this was after an ":lcd" command.
8973 */
8974 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008975post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008976{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008977 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008978 if (local)
8979 {
8980 /* If still in global directory, need to remember current
8981 * directory as global directory. */
8982 if (globaldir == NULL && prev_dir != NULL)
8983 globaldir = vim_strsave(prev_dir);
8984 /* Remember this local directory for the window. */
8985 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8986 curwin->w_localdir = vim_strsave(NameBuff);
8987 }
8988 else
8989 {
8990 /* We are now in the global directory, no need to remember its
8991 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01008992 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008993 }
8994
8995 shorten_fnames(TRUE);
8996}
8997
Bram Moolenaarea408852005-06-25 22:49:46 +00008998
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999/*
9000 * ":cd", ":lcd", ":chdir" and ":lchdir".
9001 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009002 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009003ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 char_u *new_dir;
9006 char_u *tofree;
9007
9008 new_dir = eap->arg;
9009#if !defined(UNIX) && !defined(VMS)
9010 /* for non-UNIX ":cd" means: print current directory */
9011 if (*new_dir == NUL)
9012 ex_pwd(NULL);
9013 else
9014#endif
9015 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009016#ifdef FEAT_AUTOCMD
9017 if (allbuf_locked())
9018 return;
9019#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009020 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9021 && !eap->forceit)
9022 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009023 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009024 return;
9025 }
9026
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 /* ":cd -": Change to previous directory */
9028 if (STRCMP(new_dir, "-") == 0)
9029 {
9030 if (prev_dir == NULL)
9031 {
9032 EMSG(_("E186: No previous directory"));
9033 return;
9034 }
9035 new_dir = prev_dir;
9036 }
9037
9038 /* Save current directory for next ":cd -" */
9039 tofree = prev_dir;
9040 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9041 prev_dir = vim_strsave(NameBuff);
9042 else
9043 prev_dir = NULL;
9044
9045#if defined(UNIX) || defined(VMS)
9046 /* for UNIX ":cd" means: go to home directory */
9047 if (*new_dir == NUL)
9048 {
9049 /* use NameBuff for home directory name */
9050# ifdef VMS
9051 char_u *p;
9052
9053 p = mch_getenv((char_u *)"SYS$LOGIN");
9054 if (p == NULL || *p == NUL) /* empty is the same as not set */
9055 NameBuff[0] = NUL;
9056 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009057 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058# else
9059 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9060# endif
9061 new_dir = NameBuff;
9062 }
9063#endif
9064 if (new_dir == NULL || vim_chdir(new_dir))
9065 EMSG(_(e_failed));
9066 else
9067 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009068 int is_local_chdir = eap->cmdidx == CMD_lcd
9069 || eap->cmdidx == CMD_lchdir;
9070
9071 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072
9073 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009074 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009076#ifdef FEAT_AUTOCMD
9077 apply_autocmds(EVENT_DIRCHANGED,
9078 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9079 new_dir, FALSE, curbuf);
9080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 }
9082 vim_free(tofree);
9083 }
9084}
9085
9086/*
9087 * ":pwd".
9088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009090ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091{
9092 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9093 {
9094#ifdef BACKSLASH_IN_FILENAME
9095 slash_adjust(NameBuff);
9096#endif
9097 msg(NameBuff);
9098 }
9099 else
9100 EMSG(_("E187: Unknown"));
9101}
9102
9103/*
9104 * ":=".
9105 */
9106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009107ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
9109 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009110 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111}
9112
9113 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009114ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009116 int n;
9117 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118
9119 if (cursor_valid())
9120 {
9121 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9122 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009123 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009125
9126 len = eap->line2;
9127 switch (*eap->arg)
9128 {
9129 case 'm': break;
9130 case NUL: len *= 1000L; break;
9131 default: EMSG2(_(e_invarg2), eap->arg); return;
9132 }
9133 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134}
9135
9136/*
9137 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9138 */
9139 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009140do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141{
9142 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009143 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144
9145 cursor_on();
9146 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009147 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009149 wait_now = msec - done > 1000L ? 1000L : msec - done;
9150#ifdef FEAT_TIMERS
9151 {
9152 long due_time = check_due_timer();
9153
9154 if (due_time > 0 && due_time < wait_now)
9155 wait_now = due_time;
9156 }
9157#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009158#ifdef FEAT_JOB_CHANNEL
9159 if (has_any_channel() && wait_now > 100L)
9160 wait_now = 100L;
9161#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009162 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009163#ifdef FEAT_JOB_CHANNEL
9164 if (has_any_channel())
9165 ui_breakcheck_force(TRUE);
9166 else
9167#endif
9168 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009169#ifdef MESSAGE_QUEUE
9170 /* Process the netbeans and clientserver messages that may have been
9171 * received in the call to ui_breakcheck() when the GUI is in use. This
9172 * may occur when running a test case. */
9173 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 }
9176}
9177
9178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009179do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180{
9181 int mode;
9182 char_u *cmdp;
9183
9184 cmdp = eap->cmd;
9185 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9186
9187 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9188 eap->arg, mode, isabbrev))
9189 {
9190 case 1: EMSG(_(e_invarg));
9191 break;
9192 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9193 break;
9194 }
9195}
9196
9197/*
9198 * ":winsize" command (obsolete).
9199 */
9200 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009201ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202{
9203 int w, h;
9204 char_u *arg = eap->arg;
9205 char_u *p;
9206
9207 w = getdigits(&arg);
9208 arg = skipwhite(arg);
9209 p = arg;
9210 h = getdigits(&arg);
9211 if (*p != NUL && *arg == NUL)
9212 set_shellsize(w, h, TRUE);
9213 else
9214 EMSG(_("E465: :winsize requires two number arguments"));
9215}
9216
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009218ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219{
9220 int xchar = NUL;
9221 char_u *p;
9222
9223 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9224 {
9225 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9226 if (eap->arg[1] == NUL)
9227 {
9228 EMSG(_(e_invarg));
9229 return;
9230 }
9231 xchar = eap->arg[1];
9232 p = eap->arg + 2;
9233 }
9234 else
9235 p = eap->arg + 1;
9236
9237 eap->nextcmd = check_nextcmd(p);
9238 p = skipwhite(p);
9239 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9240 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009241 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 {
9243 /* Pass flags on for ":vertical wincmd ]". */
9244 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009245 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9247 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009248 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 }
9250}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251
Bram Moolenaar843ee412004-06-30 16:16:41 +00009252#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253/*
9254 * ":winpos".
9255 */
9256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009257ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
9259 int x, y;
9260 char_u *arg = eap->arg;
9261 char_u *p;
9262
9263 if (*arg == NUL)
9264 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009265# if defined(FEAT_GUI) || defined(MSWIN)
9266# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009268# else
9269 if (mch_get_winpos(&x, &y) != FAIL)
9270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 {
9272 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9273 msg(IObuff);
9274 }
9275 else
9276# endif
9277 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9278 }
9279 else
9280 {
9281 x = getdigits(&arg);
9282 arg = skipwhite(arg);
9283 p = arg;
9284 y = getdigits(&arg);
9285 if (*p == NUL || *arg != NUL)
9286 {
9287 EMSG(_("E466: :winpos requires two number arguments"));
9288 return;
9289 }
9290# ifdef FEAT_GUI
9291 if (gui.in_use)
9292 gui_mch_set_winpos(x, y);
9293 else if (gui.starting)
9294 {
9295 /* Remember the coordinates for when the window is opened. */
9296 gui_win_x = x;
9297 gui_win_y = y;
9298 }
9299# ifdef HAVE_TGETENT
9300 else
9301# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009302# else
9303# ifdef MSWIN
9304 mch_set_winpos(x, y);
9305# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306# endif
9307# ifdef HAVE_TGETENT
9308 if (*T_CWP)
9309 term_set_winpos(x, y);
9310# endif
9311 }
9312}
9313#endif
9314
9315/*
9316 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9317 */
9318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009319ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320{
9321 oparg_T oa;
9322
9323 clear_oparg(&oa);
9324 oa.regname = eap->regname;
9325 oa.start.lnum = eap->line1;
9326 oa.end.lnum = eap->line2;
9327 oa.line_count = eap->line2 - eap->line1 + 1;
9328 oa.motion_type = MLINE;
9329#ifdef FEAT_VIRTUALEDIT
9330 virtual_op = FALSE;
9331#endif
9332 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9333 {
9334 setpcmark();
9335 curwin->w_cursor.lnum = eap->line1;
9336 beginline(BL_SOL | BL_FIX);
9337 }
9338
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009339 if (VIsual_active)
9340 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009341
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 switch (eap->cmdidx)
9343 {
9344 case CMD_delete:
9345 oa.op_type = OP_DELETE;
9346 op_delete(&oa);
9347 break;
9348
9349 case CMD_yank:
9350 oa.op_type = OP_YANK;
9351 (void)op_yank(&oa, FALSE, TRUE);
9352 break;
9353
9354 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009355 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009357 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9358#else
9359 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009361 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 oa.op_type = OP_RSHIFT;
9363 else
9364 oa.op_type = OP_LSHIFT;
9365 op_shift(&oa, FALSE, eap->amount);
9366 break;
9367 }
9368#ifdef FEAT_VIRTUALEDIT
9369 virtual_op = MAYBE;
9370#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009371 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372}
9373
9374/*
9375 * ":put".
9376 */
9377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009378ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379{
9380 /* ":0put" works like ":1put!". */
9381 if (eap->line2 == 0)
9382 {
9383 eap->line2 = 1;
9384 eap->forceit = TRUE;
9385 }
9386 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009387 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9388 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389}
9390
9391/*
9392 * Handle ":copy" and ":move".
9393 */
9394 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009395ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396{
9397 long n;
9398
Bram Moolenaarded27822017-01-02 14:27:34 +01009399 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 if (eap->arg == NULL) /* error detected */
9401 {
9402 eap->nextcmd = NULL;
9403 return;
9404 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009405 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406
9407 /*
9408 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9409 */
9410 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9411 {
9412 EMSG(_(e_invaddr));
9413 return;
9414 }
9415
9416 if (eap->cmdidx == CMD_move)
9417 {
9418 if (do_move(eap->line1, eap->line2, n) == FAIL)
9419 return;
9420 }
9421 else
9422 ex_copy(eap->line1, eap->line2, n);
9423 u_clearline();
9424 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009425 ex_may_print(eap);
9426}
9427
9428/*
9429 * Print the current line if flags were given to the Ex command.
9430 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009431 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009432ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009433{
9434 if (eap->flags != 0)
9435 {
9436 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9437 (eap->flags & EXFLAG_LIST));
9438 ex_no_reprint = TRUE;
9439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440}
9441
9442/*
9443 * ":smagic" and ":snomagic".
9444 */
9445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009446ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447{
9448 int magic_save = p_magic;
9449
9450 p_magic = (eap->cmdidx == CMD_smagic);
9451 do_sub(eap);
9452 p_magic = magic_save;
9453}
9454
9455/*
9456 * ":join".
9457 */
9458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009459ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460{
9461 curwin->w_cursor.lnum = eap->line1;
9462 if (eap->line1 == eap->line2)
9463 {
9464 if (eap->addr_count >= 2) /* :2,2join does nothing */
9465 return;
9466 if (eap->line2 == curbuf->b_ml.ml_line_count)
9467 {
9468 beep_flush();
9469 return;
9470 }
9471 ++eap->line2;
9472 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009473 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009475 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476}
9477
9478/*
9479 * ":[addr]@r" or ":[addr]*r": execute register
9480 */
9481 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009482ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
9484 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009485 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486
9487 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009488 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489
9490#ifdef USE_ON_FLY_SCROLL
9491 dont_scroll = TRUE; /* disallow scrolling here */
9492#endif
9493
9494 /* get the register name. No name means to use the previous one */
9495 c = *eap->arg;
9496 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9497 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009498 /* Put the register in the typeahead buffer with the "silent" flag. */
9499 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9500 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 else
9505 {
9506 int save_efr = exec_from_reg;
9507
9508 exec_from_reg = TRUE;
9509
9510 /*
9511 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009512 * Continue until the stuff buffer is empty and all added characters
9513 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009515 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9517
9518 exec_from_reg = save_efr;
9519 }
9520}
9521
9522/*
9523 * ":!".
9524 */
9525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009526ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527{
9528 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9529}
9530
9531/*
9532 * ":undo".
9533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009535ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009537 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009538 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009539 else
9540 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541}
9542
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009543#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009545ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009546{
9547 char_u hash[UNDO_HASH_SIZE];
9548
9549 u_compute_hash(hash);
9550 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9551}
9552
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009554ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009555{
9556 char_u hash[UNDO_HASH_SIZE];
9557
9558 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009559 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009560}
9561#endif
9562
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563/*
9564 * ":redo".
9565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009567ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568{
9569 u_redo(1);
9570}
9571
9572/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009573 * ":earlier" and ":later".
9574 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009576ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009577{
9578 long count = 0;
9579 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009580 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009581 char_u *p = eap->arg;
9582
9583 if (*p == NUL)
9584 count = 1;
9585 else if (isdigit(*p))
9586 {
9587 count = getdigits(&p);
9588 switch (*p)
9589 {
9590 case 's': ++p; sec = TRUE; break;
9591 case 'm': ++p; sec = TRUE; count *= 60; break;
9592 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009593 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9594 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009595 }
9596 }
9597
9598 if (*p != NUL)
9599 EMSG2(_(e_invarg2), eap->arg);
9600 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009601 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9602 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009603}
9604
9605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 * ":redir": start/stop redirection.
9607 */
9608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009609ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
9611 char *mode;
9612 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009613 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614
Bram Moolenaarba768492016-07-08 15:32:54 +02009615#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009616 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009617 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009618 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009619 return;
9620 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009621#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009622
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 if (STRICMP(eap->arg, "END") == 0)
9624 close_redir();
9625 else
9626 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009627 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009629 ++arg;
9630 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009632 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 mode = "a";
9634 }
9635 else
9636 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009637 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638
9639 close_redir();
9640
9641 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009642 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 if (fname == NULL)
9644 return;
9645#ifdef FEAT_BROWSE
9646 if (cmdmod.browse)
9647 {
9648 char_u *browseFile;
9649
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009650 browseFile = do_browse(BROWSE_SAVE,
9651 (char_u *)_("Save Redirection"),
9652 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 if (browseFile == NULL)
9654 return; /* operation cancelled */
9655 vim_free(fname);
9656 fname = browseFile;
9657 eap->forceit = TRUE; /* since dialog already asked */
9658 }
9659#endif
9660
9661 redir_fd = open_exfile(fname, eap->forceit, mode);
9662 vim_free(fname);
9663 }
9664#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009665 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 {
9667 /* redirect to a register a-z (resp. A-Z for appending) */
9668 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009669 ++arg;
9670 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009672 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009673 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009675 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009677 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009678 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009679 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009680 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009682 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009683 if (*arg == '>')
9684 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009685 /* Make register empty when not using @A-@Z and the
9686 * command is valid. */
9687 if (*arg == NUL && !isupper(redir_reg))
9688 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009690 }
9691 if (*arg != NUL)
9692 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009693 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009694 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009695 }
9696 }
9697 else if (*arg == '=' && arg[1] == '>')
9698 {
9699 int append;
9700
9701 /* redirect to a variable */
9702 close_redir();
9703 arg += 2;
9704
9705 if (*arg == '>')
9706 {
9707 ++arg;
9708 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 }
9710 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009711 append = FALSE;
9712
9713 if (var_redir_start(skipwhite(arg), append) == OK)
9714 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 }
9716#endif
9717
9718 /* TODO: redirect to a buffer */
9719
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009721 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009723
9724 /* Make sure redirection is not off. Can happen for cmdline completion
9725 * that indirectly invokes a command to catch its output. */
9726 if (redir_fd != NULL
9727#ifdef FEAT_EVAL
9728 || redir_reg || redir_vname
9729#endif
9730 )
9731 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732}
9733
9734/*
9735 * ":redraw": force redraw
9736 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009737 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009738ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739{
9740 int r = RedrawingDisabled;
9741 int p = p_lz;
9742
9743 RedrawingDisabled = 0;
9744 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009745 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009747 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748#ifdef FEAT_TITLE
9749 if (need_maketitle)
9750 maketitle();
9751#endif
9752 RedrawingDisabled = r;
9753 p_lz = p;
9754
9755 /* Reset msg_didout, so that a message that's there is overwritten. */
9756 msg_didout = FALSE;
9757 msg_col = 0;
9758
Bram Moolenaar56667a52013-07-17 11:54:28 +02009759 /* No need to wait after an intentional redraw. */
9760 need_wait_return = FALSE;
9761
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 out_flush();
9763}
9764
9765/*
9766 * ":redrawstatus": force redraw of status line(s)
9767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009769ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 int r = RedrawingDisabled;
9772 int p = p_lz;
9773
9774 RedrawingDisabled = 0;
9775 p_lz = FALSE;
9776 if (eap->forceit)
9777 status_redraw_all();
9778 else
9779 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009780 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 RedrawingDisabled = r;
9782 p_lz = p;
9783 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784}
9785
9786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009787close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788{
9789 if (redir_fd != NULL)
9790 {
9791 fclose(redir_fd);
9792 redir_fd = NULL;
9793 }
9794#ifdef FEAT_EVAL
9795 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009796 if (redir_vname)
9797 {
9798 var_redir_stop();
9799 redir_vname = 0;
9800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801#endif
9802}
9803
9804#if defined(FEAT_SESSION) && defined(USE_CRNL)
9805# define MKSESSION_NL
9806static int mksession_nl = FALSE; /* use NL only in put_eol() */
9807#endif
9808
9809/*
9810 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9811 */
9812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009813ex_mkrc(
9814 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815{
9816 FILE *fd;
9817 int failed = FALSE;
9818 char_u *fname;
9819#ifdef FEAT_BROWSE
9820 char_u *browseFile = NULL;
9821#endif
9822#ifdef FEAT_SESSION
9823 int view_session = FALSE;
9824 int using_vdir = FALSE; /* using 'viewdir'? */
9825 char_u *viewFile = NULL;
9826 unsigned *flagp;
9827#endif
9828
9829 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9830 {
9831#ifdef FEAT_SESSION
9832 view_session = TRUE;
9833#else
9834 ex_ni(eap);
9835 return;
9836#endif
9837 }
9838
9839#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009840 /* Use the short file name until ":lcd" is used. We also don't use the
9841 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009842 did_lcd = FALSE;
9843
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9845 if (eap->cmdidx == CMD_mkview
9846 && (*eap->arg == NUL
9847 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9848 {
9849 eap->forceit = TRUE;
9850 fname = get_view_file(*eap->arg);
9851 if (fname == NULL)
9852 return;
9853 viewFile = fname;
9854 using_vdir = TRUE;
9855 }
9856 else
9857#endif
9858 if (*eap->arg != NUL)
9859 fname = eap->arg;
9860 else if (eap->cmdidx == CMD_mkvimrc)
9861 fname = (char_u *)VIMRC_FILE;
9862#ifdef FEAT_SESSION
9863 else if (eap->cmdidx == CMD_mksession)
9864 fname = (char_u *)SESSION_FILE;
9865#endif
9866 else
9867 fname = (char_u *)EXRC_FILE;
9868
9869#ifdef FEAT_BROWSE
9870 if (cmdmod.browse)
9871 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009872 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873# ifdef FEAT_SESSION
9874 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9875 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9876# endif
9877 (char_u *)_("Save Setup"),
9878 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9879 if (browseFile == NULL)
9880 goto theend;
9881 fname = browseFile;
9882 eap->forceit = TRUE; /* since dialog already asked */
9883 }
9884#endif
9885
9886#if defined(FEAT_SESSION) && defined(vim_mkdir)
9887 /* When using 'viewdir' may have to create the directory. */
9888 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009889 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890#endif
9891
9892 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9893 if (fd != NULL)
9894 {
9895#ifdef FEAT_SESSION
9896 if (eap->cmdidx == CMD_mkview)
9897 flagp = &vop_flags;
9898 else
9899 flagp = &ssop_flags;
9900#endif
9901
9902#ifdef MKSESSION_NL
9903 /* "unix" in 'sessionoptions': use NL line separator */
9904 if (view_session && (*flagp & SSOP_UNIX))
9905 mksession_nl = TRUE;
9906#endif
9907
9908 /* Write the version command for :mkvimrc */
9909 if (eap->cmdidx == CMD_mkvimrc)
9910 (void)put_line(fd, "version 6.0");
9911
9912#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009913 if (eap->cmdidx == CMD_mksession)
9914 {
9915 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9916 failed = TRUE;
9917 }
9918
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 if (eap->cmdidx != CMD_mkview)
9920#endif
9921 {
9922 /* Write setting 'compatible' first, because it has side effects.
9923 * For that same reason only do it when needed. */
9924 if (p_cp)
9925 (void)put_line(fd, "if !&cp | set cp | endif");
9926 else
9927 (void)put_line(fd, "if &cp | set nocp | endif");
9928 }
9929
9930#ifdef FEAT_SESSION
9931 if (!view_session
9932 || (eap->cmdidx == CMD_mksession
9933 && (*flagp & SSOP_OPTIONS)))
9934#endif
9935 failed |= (makemap(fd, NULL) == FAIL
9936 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9937
9938#ifdef FEAT_SESSION
9939 if (!failed && view_session)
9940 {
9941 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9942 failed = TRUE;
9943 if (eap->cmdidx == CMD_mksession)
9944 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009945 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946
Bram Moolenaard9462e32011-04-11 21:35:11 +02009947 dirnow = alloc(MAXPATHL);
9948 if (dirnow == NULL)
9949 failed = TRUE;
9950 else
9951 {
9952 /*
9953 * Change to session file's dir.
9954 */
9955 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009957 *dirnow = NUL;
9958 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9959 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009960 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009961 shorten_fnames(TRUE);
9962 }
9963 else if (*dirnow != NUL
9964 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9965 {
9966 if (mch_chdir((char *)globaldir) == 0)
9967 shorten_fnames(TRUE);
9968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969
Bram Moolenaard9462e32011-04-11 21:35:11 +02009970 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971
Bram Moolenaard9462e32011-04-11 21:35:11 +02009972 /* restore original dir */
9973 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009975 {
9976 if (mch_chdir((char *)dirnow) != 0)
9977 EMSG(_(e_prev_dir));
9978 shorten_fnames(TRUE);
9979 }
9980 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 }
9982 }
9983 else
9984 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009985 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9986 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 }
9988 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9989 == FAIL)
9990 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009991 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9992 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009993 if (eap->cmdidx == CMD_mksession)
9994 {
9995 if (put_line(fd, "unlet SessionLoad") == FAIL)
9996 failed = TRUE;
9997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 }
9999#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010000 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10001 failed = TRUE;
10002
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 failed |= fclose(fd);
10004
10005 if (failed)
10006 EMSG(_(e_write));
10007#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10008 else if (eap->cmdidx == CMD_mksession)
10009 {
10010 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010011 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012
Bram Moolenaard9462e32011-04-11 21:35:11 +020010013 tbuf = alloc(MAXPATHL);
10014 if (tbuf != NULL)
10015 {
10016 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10017 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10018 vim_free(tbuf);
10019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 }
10021#endif
10022#ifdef MKSESSION_NL
10023 mksession_nl = FALSE;
10024#endif
10025 }
10026
10027#ifdef FEAT_BROWSE
10028theend:
10029 vim_free(browseFile);
10030#endif
10031#ifdef FEAT_SESSION
10032 vim_free(viewFile);
10033#endif
10034}
10035
Bram Moolenaardf177f62005-02-22 08:39:57 +000010036#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10037 || defined(PROTO)
10038 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010039vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010040{
10041 if (vim_mkdir(name, prot) != 0)
10042 {
10043 EMSG2(_("E739: Cannot create directory: %s"), name);
10044 return FAIL;
10045 }
10046 return OK;
10047}
10048#endif
10049
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050/*
10051 * Open a file for writing for an Ex command, with some checks.
10052 * Return file descriptor, or NULL on failure.
10053 */
10054 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010055open_exfile(
10056 char_u *fname,
10057 int forceit,
10058 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059{
10060 FILE *fd;
10061
10062#ifdef UNIX
10063 /* with Unix it is possible to open a directory */
10064 if (mch_isdir(fname))
10065 {
10066 EMSG2(_(e_isadir2), fname);
10067 return NULL;
10068 }
10069#endif
10070 if (!forceit && *mode != 'a' && vim_fexists(fname))
10071 {
10072 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10073 return NULL;
10074 }
10075
10076 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10077 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10078
10079 return fd;
10080}
10081
10082/*
10083 * ":mark" and ":k".
10084 */
10085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010086ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087{
10088 pos_T pos;
10089
10090 if (*eap->arg == NUL) /* No argument? */
10091 EMSG(_(e_argreq));
10092 else if (eap->arg[1] != NUL) /* more than one character? */
10093 EMSG(_(e_trailing));
10094 else
10095 {
10096 pos = curwin->w_cursor; /* save curwin->w_cursor */
10097 curwin->w_cursor.lnum = eap->line2;
10098 beginline(BL_WHITE | BL_FIX);
10099 if (setmark(*eap->arg) == FAIL) /* set mark */
10100 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10101 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10102 }
10103}
10104
10105/*
10106 * Update w_topline, w_leftcol and the cursor position.
10107 */
10108 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010109update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110{
10111 check_cursor(); /* put cursor on valid line */
10112 update_topline();
10113 if (!curwin->w_p_wrap)
10114 validate_cursor();
10115 update_curswant();
10116}
10117
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010119 * Save the current State and go to Normal mode.
10120 * Return TRUE if the typeahead could be saved.
10121 */
10122 int
10123save_current_state(save_state_T *sst)
10124{
10125 sst->save_msg_scroll = msg_scroll;
10126 sst->save_restart_edit = restart_edit;
10127 sst->save_msg_didout = msg_didout;
10128 sst->save_State = State;
10129 sst->save_insertmode = p_im;
10130 sst->save_finish_op = finish_op;
10131 sst->save_opcount = opcount;
10132
10133 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10134 restart_edit = 0; /* don't go to Insert mode */
10135 p_im = FALSE; /* don't use 'insertmode' */
10136
10137 /*
10138 * Save the current typeahead. This is required to allow using ":normal"
10139 * from an event handler and makes sure we don't hang when the argument
10140 * ends with half a command.
10141 */
10142 save_typeahead(&sst->tabuf);
10143 return sst->tabuf.typebuf_valid;
10144}
10145
10146 void
10147restore_current_state(save_state_T *sst)
10148{
10149 /* Restore the previous typeahead. */
10150 restore_typeahead(&sst->tabuf);
10151
10152 msg_scroll = sst->save_msg_scroll;
10153 restart_edit = sst->save_restart_edit;
10154 p_im = sst->save_insertmode;
10155 finish_op = sst->save_finish_op;
10156 opcount = sst->save_opcount;
10157 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10158
10159 /* Restore the state (needed when called from a function executed for
10160 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10161 State = sst->save_State;
10162#ifdef CURSOR_SHAPE
10163 ui_cursor_shape(); /* may show different cursor shape */
10164#endif
10165}
10166
10167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 * ":normal[!] {commands}": Execute normal mode commands.
10169 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010170 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010171ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010173 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174#ifdef FEAT_MBYTE
10175 char_u *arg = NULL;
10176 int l;
10177 char_u *p;
10178#endif
10179
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010180 if (ex_normal_lock > 0)
10181 {
10182 EMSG(_(e_secure));
10183 return;
10184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 if (ex_normal_busy >= p_mmd)
10186 {
10187 EMSG(_("E192: Recursive use of :normal too deep"));
10188 return;
10189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190
10191#ifdef FEAT_MBYTE
10192 /*
10193 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10194 * this for the K_SPECIAL leading byte, otherwise special keys will not
10195 * work.
10196 */
10197 if (has_mbyte)
10198 {
10199 int len = 0;
10200
10201 /* Count the number of characters to be escaped. */
10202 for (p = eap->arg; *p != NUL; ++p)
10203 {
10204# ifdef FEAT_GUI
10205 if (*p == CSI) /* leadbyte CSI */
10206 len += 2;
10207# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010208 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10210# ifdef FEAT_GUI
10211 || *p == CSI
10212# endif
10213 )
10214 len += 2;
10215 }
10216 if (len > 0)
10217 {
10218 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10219 if (arg != NULL)
10220 {
10221 len = 0;
10222 for (p = eap->arg; *p != NUL; ++p)
10223 {
10224 arg[len++] = *p;
10225# ifdef FEAT_GUI
10226 if (*p == CSI)
10227 {
10228 arg[len++] = KS_EXTRA;
10229 arg[len++] = (int)KE_CSI;
10230 }
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 {
10234 arg[len++] = *++p;
10235 if (*p == K_SPECIAL)
10236 {
10237 arg[len++] = KS_SPECIAL;
10238 arg[len++] = KE_FILLER;
10239 }
10240# ifdef FEAT_GUI
10241 else if (*p == CSI)
10242 {
10243 arg[len++] = KS_EXTRA;
10244 arg[len++] = (int)KE_CSI;
10245 }
10246# endif
10247 }
10248 arg[len] = NUL;
10249 }
10250 }
10251 }
10252 }
10253#endif
10254
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010255 ++ex_normal_busy;
10256 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257 {
10258 /*
10259 * Repeat the :normal command for each line in the range. When no
10260 * range given, execute it just once, without positioning the cursor
10261 * first.
10262 */
10263 do
10264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 if (eap->addr_count != 0)
10266 {
10267 curwin->w_cursor.lnum = eap->line1++;
10268 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010269 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 }
10271
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010272 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273#ifdef FEAT_MBYTE
10274 arg != NULL ? arg :
10275#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010276 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 }
10278 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10279 }
10280
10281 /* Might not return to the main loop when in an event handler. */
10282 update_topline_cursor();
10283
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010284 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010286#ifdef FEAT_MOUSE
10287 setmouse();
10288#endif
10289#ifdef CURSOR_SHAPE
10290 ui_cursor_shape(); /* may show different cursor shape */
10291#endif
10292
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293#ifdef FEAT_MBYTE
10294 vim_free(arg);
10295#endif
10296}
10297
10298/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010299 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 */
10301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010302ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010304 if (eap->forceit)
10305 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010306 /* cursor line can be zero on startup */
10307 if (!curwin->w_cursor.lnum)
10308 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010309 coladvance((colnr_T)MAXCOL);
10310 curwin->w_curswant = MAXCOL;
10311 curwin->w_set_curswant = FALSE;
10312 }
10313
Bram Moolenaara40c5002005-01-09 21:16:21 +000010314 /* Ignore the command when already in Insert mode. Inserting an
10315 * expression register that invokes a function can do this. */
10316 if (State & INSERT)
10317 return;
10318
Bram Moolenaar64969662005-12-14 21:59:55 +000010319 if (eap->cmdidx == CMD_startinsert)
10320 restart_edit = 'a';
10321 else if (eap->cmdidx == CMD_startreplace)
10322 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010324 restart_edit = 'V';
10325
10326 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010328 if (eap->cmdidx == CMD_startinsert)
10329 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 curwin->w_curswant = 0; /* avoid MAXCOL */
10331 }
10332}
10333
10334/*
10335 * ":stopinsert"
10336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010338ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339{
10340 restart_edit = 0;
10341 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010342 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010345/*
10346 * Execute normal mode command "cmd".
10347 * "remap" can be REMAP_NONE or REMAP_YES.
10348 */
10349 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010350exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010351{
Bram Moolenaar25281632016-01-21 23:32:32 +010010352 /* Stuff the argument into the typeahead buffer. */
10353 ins_typebuf(cmd, remap, 0, TRUE, silent);
10354 exec_normal(FALSE);
10355}
Bram Moolenaar25281632016-01-21 23:32:32 +010010356
Bram Moolenaar25281632016-01-21 23:32:32 +010010357/*
10358 * Execute normal_cmd() until there is no typeahead left.
10359 */
10360 void
10361exec_normal(int was_typed)
10362{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010363 oparg_T oa;
10364
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010365 clear_oparg(&oa);
10366 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010367 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10368 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010369 {
10370 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010371 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010372 }
10373}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010374
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375#ifdef FEAT_FIND_ID
10376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010377ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378{
10379 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10380 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10381 (linenr_T)1, (linenr_T)MAXLNUM);
10382}
10383
Bram Moolenaar4033c552017-09-16 20:54:51 +020010384#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385/*
10386 * ":psearch"
10387 */
10388 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010389ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390{
10391 g_do_tagpreview = p_pvh;
10392 ex_findpat(eap);
10393 g_do_tagpreview = 0;
10394}
10395#endif
10396
10397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010398ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399{
10400 int whole = TRUE;
10401 long n;
10402 char_u *p;
10403 int action;
10404
10405 switch (cmdnames[eap->cmdidx].cmd_name[2])
10406 {
10407 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10408 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10409 action = ACTION_GOTO;
10410 else
10411 action = ACTION_SHOW;
10412 break;
10413 case 'i': /* ":ilist" and ":dlist" */
10414 action = ACTION_SHOW_ALL;
10415 break;
10416 case 'u': /* ":ijump" and ":djump" */
10417 action = ACTION_GOTO;
10418 break;
10419 default: /* ":isplit" and ":dsplit" */
10420 action = ACTION_SPLIT;
10421 break;
10422 }
10423
10424 n = 1;
10425 if (vim_isdigit(*eap->arg)) /* get count */
10426 {
10427 n = getdigits(&eap->arg);
10428 eap->arg = skipwhite(eap->arg);
10429 }
10430 if (*eap->arg == '/') /* Match regexp, not just whole words */
10431 {
10432 whole = FALSE;
10433 ++eap->arg;
10434 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10435 if (*p)
10436 {
10437 *p++ = NUL;
10438 p = skipwhite(p);
10439
10440 /* Check for trailing illegal characters */
10441 if (!ends_excmd(*p))
10442 eap->errmsg = e_trailing;
10443 else
10444 eap->nextcmd = check_nextcmd(p);
10445 }
10446 }
10447 if (!eap->skip)
10448 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10449 whole, !eap->forceit,
10450 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10451 n, action, eap->line1, eap->line2);
10452}
10453#endif
10454
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455
Bram Moolenaar4033c552017-09-16 20:54:51 +020010456#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457/*
10458 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10459 */
10460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010461ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010463 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10465}
10466
10467/*
10468 * ":pedit"
10469 */
10470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010471ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472{
10473 win_T *curwin_save = curwin;
10474
10475 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010476 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010477 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 do_exedit(eap, NULL);
10479 keep_help_flag = FALSE;
10480 if (curwin != curwin_save && win_valid(curwin_save))
10481 {
10482 /* Return cursor to where we were */
10483 validate_cursor();
10484 redraw_later(VALID);
10485 win_enter(curwin_save, TRUE);
10486 }
10487 g_do_tagpreview = 0;
10488}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490
10491/*
10492 * ":stag", ":stselect" and ":stjump".
10493 */
10494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010495ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496{
10497 postponed_split = -1;
10498 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010499 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10501 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010502 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504
10505/*
10506 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10507 */
10508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010509ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510{
10511 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10512}
10513
10514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010515ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516{
10517 int cmd;
10518
10519 switch (name[1])
10520 {
10521 case 'j': cmd = DT_JUMP; /* ":tjump" */
10522 break;
10523 case 's': cmd = DT_SELECT; /* ":tselect" */
10524 break;
10525 case 'p': cmd = DT_PREV; /* ":tprevious" */
10526 break;
10527 case 'N': cmd = DT_PREV; /* ":tNext" */
10528 break;
10529 case 'n': cmd = DT_NEXT; /* ":tnext" */
10530 break;
10531 case 'o': cmd = DT_POP; /* ":pop" */
10532 break;
10533 case 'f': /* ":tfirst" */
10534 case 'r': cmd = DT_FIRST; /* ":trewind" */
10535 break;
10536 case 'l': cmd = DT_LAST; /* ":tlast" */
10537 break;
10538 default: /* ":tag" */
10539#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010540 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010542 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 return;
10544 }
10545#endif
10546 cmd = DT_TAG;
10547 break;
10548 }
10549
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010550 if (name[0] == 'l')
10551 {
10552#ifndef FEAT_QUICKFIX
10553 ex_ni(eap);
10554 return;
10555#else
10556 cmd = DT_LTAG;
10557#endif
10558 }
10559
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10561 eap->forceit, TRUE);
10562}
10563
10564/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010565 * Check "str" for starting with a special cmdline variable.
10566 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10567 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10568 */
10569 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010570find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010571{
10572 int len;
10573 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010574 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010575 "%",
10576#define SPEC_PERC 0
10577 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010578#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010579 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010580#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010581 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010582#define SPEC_CCWORD (SPEC_CWORD + 1)
10583 "<cexpr>", /* expr under cursor */
10584#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010585 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010586#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010587 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010588#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010589 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010590#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010591#ifdef FEAT_AUTOCMD
10592 "<afile>", /* autocommand file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010593# define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010594 "<abuf>", /* autocommand buffer number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010595# define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010596 "<amatch>", /* autocommand match name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010597# define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010598#endif
10599#ifdef FEAT_CLIENTSERVER
10600 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010601# ifdef FEAT_AUTOCMD
Bram Moolenaar65f08472017-09-10 18:16:20 +020010602# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010603# else
Bram Moolenaar65f08472017-09-10 18:16:20 +020010604# define SPEC_CLIENT (SPEC_SLNUM + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010605# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010606#endif
10607 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010608
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010609 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010610 {
10611 len = (int)STRLEN(spec_str[i]);
10612 if (STRNCMP(src, spec_str[i], len) == 0)
10613 {
10614 *usedlen = len;
10615 return i;
10616 }
10617 }
10618 return -1;
10619}
10620
10621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 * Evaluate cmdline variables.
10623 *
10624 * change '%' to curbuf->b_ffname
10625 * '#' to curwin->w_altfile
10626 * '<cword>' to word under the cursor
10627 * '<cWORD>' to WORD under the cursor
10628 * '<cfile>' to path name under the cursor
10629 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010630 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 * '<afile>' to file name for autocommand
10632 * '<abuf>' to buffer number for autocommand
10633 * '<amatch>' to matching name for autocommand
10634 *
10635 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10636 * "" for error without a message) and NULL is returned.
10637 * Returns an allocated string if a valid match was found.
10638 * Returns NULL if no match was found. "usedlen" then still contains the
10639 * number of characters to skip.
10640 */
10641 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010642eval_vars(
10643 char_u *src, /* pointer into commandline */
10644 char_u *srcstart, /* beginning of valid memory for src */
10645 int *usedlen, /* characters after src that are used */
10646 linenr_T *lnump, /* line number for :e command, or NULL */
10647 char_u **errormsg, /* pointer to error message */
10648 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010649 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650{
10651 int i;
10652 char_u *s;
10653 char_u *result;
10654 char_u *resultbuf = NULL;
10655 int resultlen;
10656 buf_T *buf;
10657 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10658 int spec_idx;
10659#ifdef FEAT_MODIFY_FNAME
10660 int skip_mod = FALSE;
10661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663
10664 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010665 if (escaped != NULL)
10666 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667
10668 /*
10669 * Check if there is something to do.
10670 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010671 spec_idx = find_cmdline_var(src, usedlen);
10672 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 {
10674 *usedlen = 1;
10675 return NULL;
10676 }
10677
10678 /*
10679 * Skip when preceded with a backslash "\%" and "\#".
10680 * Note: In "\\%" the % is also not recognized!
10681 */
10682 if (src > srcstart && src[-1] == '\\')
10683 {
10684 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010685 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 return NULL;
10687 }
10688
10689 /*
10690 * word or WORD under cursor
10691 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010692 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10693 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010695 resultlen = find_ident_under_cursor(&result,
10696 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10697 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10698 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 if (resultlen == 0)
10700 {
10701 *errormsg = (char_u *)"";
10702 return NULL;
10703 }
10704 }
10705
10706 /*
10707 * '#': Alternate file name
10708 * '%': Current file name
10709 * File name under the cursor
10710 * File name for autocommand
10711 * and following modifiers
10712 */
10713 else
10714 {
10715 switch (spec_idx)
10716 {
10717 case SPEC_PERC: /* '%': current file */
10718 if (curbuf->b_fname == NULL)
10719 {
10720 result = (char_u *)"";
10721 valid = 0; /* Must have ":p:h" to be valid */
10722 }
10723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 break;
10726
10727 case SPEC_HASH: /* '#' or "#99": alternate file */
10728 if (src[1] == '#') /* "##": the argument list */
10729 {
10730 result = arg_all();
10731 resultbuf = result;
10732 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010733 if (escaped != NULL)
10734 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735#ifdef FEAT_MODIFY_FNAME
10736 skip_mod = TRUE;
10737#endif
10738 break;
10739 }
10740 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010741 if (*s == '<') /* "#<99" uses v:oldfiles */
10742 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010744 if (s == src + 2 && src[1] == '-')
10745 /* just a minus sign, don't skip over it */
10746 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 *usedlen = (int)(s - src); /* length of what we expand */
10748
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010749 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010751 if (*usedlen < 2)
10752 {
10753 /* Should we give an error message for #<text? */
10754 *usedlen = 1;
10755 return NULL;
10756 }
10757#ifdef FEAT_EVAL
10758 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10759 (long)i);
10760 if (result == NULL)
10761 {
10762 *errormsg = (char_u *)"";
10763 return NULL;
10764 }
10765#else
10766 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 }
10770 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010771 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010772 if (i == 0 && src[1] == '<' && *usedlen > 1)
10773 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010774 buf = buflist_findnr(i);
10775 if (buf == NULL)
10776 {
10777 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10778 return NULL;
10779 }
10780 if (lnump != NULL)
10781 *lnump = ECMD_LAST;
10782 if (buf->b_fname == NULL)
10783 {
10784 result = (char_u *)"";
10785 valid = 0; /* Must have ":p:h" to be valid */
10786 }
10787 else
10788 result = buf->b_fname;
10789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 break;
10791
10792#ifdef FEAT_SEARCHPATH
10793 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010794 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 if (result == NULL)
10796 {
10797 *errormsg = (char_u *)"";
10798 return NULL;
10799 }
10800 resultbuf = result; /* remember allocated string */
10801 break;
10802#endif
10803
10804#ifdef FEAT_AUTOCMD
10805 case SPEC_AFILE: /* file name for autocommand */
10806 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010807 if (result != NULL && !autocmd_fname_full)
10808 {
10809 /* Still need to turn the fname into a full path. It is
10810 * postponed to avoid a delay when <afile> is not used. */
10811 autocmd_fname_full = TRUE;
10812 result = FullName_save(autocmd_fname, FALSE);
10813 vim_free(autocmd_fname);
10814 autocmd_fname = result;
10815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 if (result == NULL)
10817 {
10818 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10819 return NULL;
10820 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010821 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 break;
10823
10824 case SPEC_ABUF: /* buffer number for autocommand */
10825 if (autocmd_bufnr <= 0)
10826 {
10827 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10828 return NULL;
10829 }
10830 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10831 result = strbuf;
10832 break;
10833
10834 case SPEC_AMATCH: /* match name for autocommand */
10835 result = autocmd_match;
10836 if (result == NULL)
10837 {
10838 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10839 return NULL;
10840 }
10841 break;
10842
10843#endif
10844 case SPEC_SFILE: /* file name for ":so" command */
10845 result = sourcing_name;
10846 if (result == NULL)
10847 {
10848 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10849 return NULL;
10850 }
10851 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010852 case SPEC_SLNUM: /* line in file for ":so" command */
10853 if (sourcing_name == NULL || sourcing_lnum == 0)
10854 {
10855 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10856 return NULL;
10857 }
10858 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10859 result = strbuf;
10860 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861#if defined(FEAT_CLIENTSERVER)
10862 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010863 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10864 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 result = strbuf;
10866 break;
10867#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010868 default:
10869 result = (char_u *)""; /* avoid gcc warning */
10870 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 }
10872
10873 resultlen = (int)STRLEN(result); /* length of new string */
10874 if (src[*usedlen] == '<') /* remove the file name extension */
10875 {
10876 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 resultlen = (int)(s - result);
10879 }
10880#ifdef FEAT_MODIFY_FNAME
10881 else if (!skip_mod)
10882 {
10883 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10884 &resultlen);
10885 if (result == NULL)
10886 {
10887 *errormsg = (char_u *)"";
10888 return NULL;
10889 }
10890 }
10891#endif
10892 }
10893
10894 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10895 {
10896 if (valid != VALID_HEAD + VALID_PATH)
10897 /* xgettext:no-c-format */
10898 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10899 else
10900 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10901 result = NULL;
10902 }
10903 else
10904 result = vim_strnsave(result, resultlen);
10905 vim_free(resultbuf);
10906 return result;
10907}
10908
10909/*
10910 * Concatenate all files in the argument list, separated by spaces, and return
10911 * it in one allocated string.
10912 * Spaces and backslashes in the file names are escaped with a backslash.
10913 * Returns NULL when out of memory.
10914 */
10915 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010916arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917{
10918 int len;
10919 int idx;
10920 char_u *retval = NULL;
10921 char_u *p;
10922
10923 /*
10924 * Do this loop two times:
10925 * first time: compute the total length
10926 * second time: concatenate the names
10927 */
10928 for (;;)
10929 {
10930 len = 0;
10931 for (idx = 0; idx < ARGCOUNT; ++idx)
10932 {
10933 p = alist_name(&ARGLIST[idx]);
10934 if (p != NULL)
10935 {
10936 if (len > 0)
10937 {
10938 /* insert a space in between names */
10939 if (retval != NULL)
10940 retval[len] = ' ';
10941 ++len;
10942 }
10943 for ( ; *p != NUL; ++p)
10944 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010945 if (*p == ' '
10946#ifndef BACKSLASH_IN_FILENAME
10947 || *p == '\\'
10948#endif
10949 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 {
10951 /* insert a backslash */
10952 if (retval != NULL)
10953 retval[len] = '\\';
10954 ++len;
10955 }
10956 if (retval != NULL)
10957 retval[len] = *p;
10958 ++len;
10959 }
10960 }
10961 }
10962
10963 /* second time: break here */
10964 if (retval != NULL)
10965 {
10966 retval[len] = NUL;
10967 break;
10968 }
10969
10970 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010971 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 if (retval == NULL)
10973 break;
10974 }
10975
10976 return retval;
10977}
10978
10979#if defined(FEAT_AUTOCMD) || defined(PROTO)
10980/*
10981 * Expand the <sfile> string in "arg".
10982 *
10983 * Returns an allocated string, or NULL for any error.
10984 */
10985 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010986expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987{
10988 char_u *errormsg;
10989 int len;
10990 char_u *result;
10991 char_u *newres;
10992 char_u *repl;
10993 int srclen;
10994 char_u *p;
10995
10996 result = vim_strsave(arg);
10997 if (result == NULL)
10998 return NULL;
10999
11000 for (p = result; *p; )
11001 {
11002 if (STRNCMP(p, "<sfile>", 7) != 0)
11003 ++p;
11004 else
11005 {
11006 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011007 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 if (errormsg != NULL)
11009 {
11010 if (*errormsg)
11011 emsg(errormsg);
11012 vim_free(result);
11013 return NULL;
11014 }
11015 if (repl == NULL) /* no match (cannot happen) */
11016 {
11017 p += srclen;
11018 continue;
11019 }
11020 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11021 newres = alloc(len);
11022 if (newres == NULL)
11023 {
11024 vim_free(repl);
11025 vim_free(result);
11026 return NULL;
11027 }
11028 mch_memmove(newres, result, (size_t)(p - result));
11029 STRCPY(newres + (p - result), repl);
11030 len = (int)STRLEN(newres);
11031 STRCAT(newres, p + srclen);
11032 vim_free(repl);
11033 vim_free(result);
11034 result = newres;
11035 p = newres + len; /* continue after the match */
11036 }
11037 }
11038
11039 return result;
11040}
11041#endif
11042
11043#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011044static int ses_winsizes(FILE *fd, int restore_size,
11045 win_T *tab_firstwin);
11046static int ses_win_rec(FILE *fd, frame_T *fr);
11047static frame_T *ses_skipframe(frame_T *fr);
11048static int ses_do_frame(frame_T *fr);
11049static int ses_do_win(win_T *wp);
11050static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11051static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011052static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053
11054/*
11055 * Write openfile commands for the current buffers to an .exrc file.
11056 * Return FAIL on error, OK otherwise.
11057 */
11058 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011059makeopens(
11060 FILE *fd,
11061 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062{
11063 buf_T *buf;
11064 int only_save_windows = TRUE;
11065 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 int restore_size = TRUE;
11067 win_T *wp;
11068 char_u *sname;
11069 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011070 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011071 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011072 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011073 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011074 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011075 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076
11077 if (ssop_flags & SSOP_BUFFERS)
11078 only_save_windows = FALSE; /* Save ALL buffers */
11079
11080 /*
11081 * Begin by setting the this_session variable, and then other
11082 * sessionable variables.
11083 */
11084#ifdef FEAT_EVAL
11085 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11086 return FAIL;
11087 if (ssop_flags & SSOP_GLOBALS)
11088 if (store_session_globals(fd) == FAIL)
11089 return FAIL;
11090#endif
11091
11092 /*
11093 * Close all windows but one.
11094 */
11095 if (put_line(fd, "silent only") == FAIL)
11096 return FAIL;
11097
11098 /*
11099 * Now a :cd command to the session directory or the current directory
11100 */
11101 if (ssop_flags & SSOP_SESDIR)
11102 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011103 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11104 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 return FAIL;
11106 }
11107 else if (ssop_flags & SSOP_CURDIR)
11108 {
11109 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11110 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011111 || fputs("cd ", fd) < 0
11112 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11113 || put_eol(fd) == FAIL)
11114 {
11115 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 vim_free(sname);
11119 }
11120
11121 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011122 * If there is an empty, unnamed buffer we will wipe it out later.
11123 * Remember the buffer number.
11124 */
11125 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11126 return FAIL;
11127 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11128 return FAIL;
11129 if (put_line(fd, "endif") == FAIL)
11130 return FAIL;
11131
11132 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 * Now save the current files, current buffer first.
11134 */
11135 if (put_line(fd, "set shortmess=aoO") == FAIL)
11136 return FAIL;
11137
11138 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011139 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 {
11141 if (!(only_save_windows && buf->b_nwindows == 0)
11142 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11143 && buf->b_fname != NULL
11144 && buf->b_p_bl)
11145 {
11146 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11147 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011148 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 return FAIL;
11150 }
11151 }
11152
11153 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011154 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11156 return FAIL;
11157
11158 if (ssop_flags & SSOP_RESIZE)
11159 {
11160 /* Note: after the restore we still check it worked!*/
11161 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11162 || put_eol(fd) == FAIL)
11163 return FAIL;
11164 }
11165
11166#ifdef FEAT_GUI
11167 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11168 {
11169 int x, y;
11170
11171 if (gui_mch_get_winpos(&x, &y) == OK)
11172 {
11173 /* Note: after the restore we still check it worked!*/
11174 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11175 return FAIL;
11176 }
11177 }
11178#endif
11179
11180 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011181 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11182 * will be displayed when creating the next tab. That resizes the windows
11183 * in the first tab, which may cause problems. Set 'showtabline' to 2
11184 * temporarily to avoid that.
11185 */
11186 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11187 {
11188 if (put_line(fd, "set stal=2") == FAIL)
11189 return FAIL;
11190 restore_stal = TRUE;
11191 }
11192
11193 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011194 * May repeat putting Windows for each tab, when "tabpages" is in
11195 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011196 * Don't use goto_tabpage(), it may change directory and trigger
11197 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011199 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011200 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011201 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011203 int need_tabnew = FALSE;
11204 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011205
Bram Moolenaar18144c82006-04-12 21:52:12 +000011206 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011208 tabpage_T *tp = find_tabpage(tabnr);
11209
11210 if (tp == NULL)
11211 break; /* done all tab pages */
11212 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011213 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011214 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011215 tab_topframe = topframe;
11216 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011217 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011218 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011219 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011220 tab_topframe = tp->tp_topframe;
11221 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011222 if (tabnr > 1)
11223 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225
Bram Moolenaar18144c82006-04-12 21:52:12 +000011226 /*
11227 * Before creating the window layout, try loading one file. If this
11228 * is aborted we don't end up with a number of useless windows.
11229 * This may have side effects! (e.g., compressed or network file).
11230 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011231 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011232 {
11233 if (ses_do_win(wp)
11234 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011235 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011236#ifdef FEAT_QUICKFIX
11237 && !bt_nofile(wp->w_buffer)
11238#endif
11239 )
11240 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011241 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011242 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11243 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011244 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011245 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011246 if (!wp->w_arg_idx_invalid)
11247 edited_win = wp;
11248 break;
11249 }
11250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011252 /* If no file got edited create an empty tab page. */
11253 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11254 return FAIL;
11255
Bram Moolenaar18144c82006-04-12 21:52:12 +000011256 /*
11257 * Save current window layout.
11258 */
11259 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011261 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011263 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11264 return FAIL;
11265 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11266 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267
Bram Moolenaar18144c82006-04-12 21:52:12 +000011268 /*
11269 * Check if window sizes can be restored (no windows omitted).
11270 * Remember the window number of the current window after restoring.
11271 */
11272 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011273 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011274 {
11275 if (ses_do_win(wp))
11276 ++nr;
11277 else
11278 restore_size = FALSE;
11279 if (curwin == wp)
11280 cnr = nr;
11281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282
Bram Moolenaar18144c82006-04-12 21:52:12 +000011283 /* Go to the first window. */
11284 if (put_line(fd, "wincmd t") == FAIL)
11285 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011286
Bram Moolenaar18144c82006-04-12 21:52:12 +000011287 /*
11288 * If more than one window, see if sizes can be restored.
11289 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11290 * resized when moving between windows.
11291 * Do this before restoring the view, so that the topline and the
11292 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011293 * winminheight and winminwidth need to be set to avoid an error if the
11294 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011295 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011296 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011297 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011298 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011299 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300
Bram Moolenaar18144c82006-04-12 21:52:12 +000011301 /*
11302 * Restore the view of the window (options, file, cursor, etc.).
11303 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011304 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011305 {
11306 if (!ses_do_win(wp))
11307 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011308 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11309 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011310 return FAIL;
11311 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11312 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011313 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011314 }
11315
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011316 /* The argument index in the first tab page is zero, need to set it in
11317 * each window. For further tab pages it's the window where we do
11318 * "tabedit". */
11319 cur_arg_idx = next_arg_idx;
11320
Bram Moolenaar18144c82006-04-12 21:52:12 +000011321 /*
11322 * Restore cursor to the current window if it's not the first one.
11323 */
11324 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11325 || put_eol(fd) == FAIL))
11326 return FAIL;
11327
11328 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011329 * Restore window sizes again after jumping around in windows, because
11330 * the current window has a minimum size while others may not.
11331 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011332 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011333 return FAIL;
11334
Bram Moolenaar18144c82006-04-12 21:52:12 +000011335 /* Don't continue in another tab page when doing only the current one
11336 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011337 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011338 break;
11339 }
11340
11341 if (ssop_flags & SSOP_TABPAGES)
11342 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011343 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11344 || put_eol(fd) == FAIL)
11345 return FAIL;
11346 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011347 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11348 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011349
Bram Moolenaar9c102382006-05-03 21:26:49 +000011350 /*
11351 * Wipe out an empty unnamed buffer we started in.
11352 */
11353 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11354 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011355 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011356 return FAIL;
11357 if (put_line(fd, "endif") == FAIL)
11358 return FAIL;
11359 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11360 return FAIL;
11361
11362 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11363 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11364 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11365 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011366 /* Re-apply 'winminheight' and 'winminwidth'. */
11367 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11368 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11369 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370
11371 /*
11372 * Lastly, execute the x.vim file if it exists.
11373 */
11374 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11375 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011376 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377 || put_line(fd, "endif") == FAIL)
11378 return FAIL;
11379
11380 return OK;
11381}
11382
11383 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011384ses_winsizes(
11385 FILE *fd,
11386 int restore_size,
11387 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388{
11389 int n = 0;
11390 win_T *wp;
11391
11392 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11393 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011394 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 {
11396 if (!ses_do_win(wp))
11397 continue;
11398 ++n;
11399
11400 /* restore height when not full height */
11401 if (wp->w_height + wp->w_status_height < topframe->fr_height
11402 && (fprintf(fd,
11403 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11404 n, (long)wp->w_height, Rows / 2, Rows) < 0
11405 || put_eol(fd) == FAIL))
11406 return FAIL;
11407
11408 /* restore width when not full width */
11409 if (wp->w_width < Columns && (fprintf(fd,
11410 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11411 n, (long)wp->w_width, Columns / 2, Columns) < 0
11412 || put_eol(fd) == FAIL))
11413 return FAIL;
11414 }
11415 }
11416 else
11417 {
11418 /* Just equalise window sizes */
11419 if (put_line(fd, "wincmd =") == FAIL)
11420 return FAIL;
11421 }
11422 return OK;
11423}
11424
11425/*
11426 * Write commands to "fd" to recursively create windows for frame "fr",
11427 * horizontally and vertically split.
11428 * After the commands the last window in the frame is the current window.
11429 * Returns FAIL when writing the commands to "fd" fails.
11430 */
11431 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011432ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433{
11434 frame_T *frc;
11435 int count = 0;
11436
11437 if (fr->fr_layout != FR_LEAF)
11438 {
11439 /* Find first frame that's not skipped and then create a window for
11440 * each following one (first frame is already there). */
11441 frc = ses_skipframe(fr->fr_child);
11442 if (frc != NULL)
11443 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11444 {
11445 /* Make window as big as possible so that we have lots of room
11446 * to split. */
11447 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11448 || put_line(fd, fr->fr_layout == FR_COL
11449 ? "split" : "vsplit") == FAIL)
11450 return FAIL;
11451 ++count;
11452 }
11453
11454 /* Go back to the first window. */
11455 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11456 ? "%dwincmd k" : "%dwincmd h", count) < 0
11457 || put_eol(fd) == FAIL))
11458 return FAIL;
11459
11460 /* Recursively create frames/windows in each window of this column or
11461 * row. */
11462 frc = ses_skipframe(fr->fr_child);
11463 while (frc != NULL)
11464 {
11465 ses_win_rec(fd, frc);
11466 frc = ses_skipframe(frc->fr_next);
11467 /* Go to next window. */
11468 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11469 return FAIL;
11470 }
11471 }
11472 return OK;
11473}
11474
11475/*
11476 * Skip frames that don't contain windows we want to save in the Session.
11477 * Returns NULL when there none.
11478 */
11479 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011480ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481{
11482 frame_T *frc;
11483
11484 for (frc = fr; frc != NULL; frc = frc->fr_next)
11485 if (ses_do_frame(frc))
11486 break;
11487 return frc;
11488}
11489
11490/*
11491 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11492 * the Session.
11493 */
11494 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011495ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496{
11497 frame_T *frc;
11498
11499 if (fr->fr_layout == FR_LEAF)
11500 return ses_do_win(fr->fr_win);
11501 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11502 if (ses_do_frame(frc))
11503 return TRUE;
11504 return FALSE;
11505}
11506
11507/*
11508 * Return non-zero if window "wp" is to be stored in the Session.
11509 */
11510 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011511ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512{
11513 if (wp->w_buffer->b_fname == NULL
11514#ifdef FEAT_QUICKFIX
11515 /* When 'buftype' is "nofile" can't restore the window contents. */
11516 || bt_nofile(wp->w_buffer)
11517#endif
11518 )
11519 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011520 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 return (ssop_flags & SSOP_HELP);
11522 return TRUE;
11523}
11524
11525/*
11526 * Write commands to "fd" to restore the view of a window.
11527 * Caller must make sure 'scrolloff' is zero.
11528 */
11529 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011530put_view(
11531 FILE *fd,
11532 win_T *wp,
11533 int add_edit, /* add ":edit" command to view */
11534 unsigned *flagp, /* vop_flags or ssop_flags */
11535 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011536 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537{
11538 win_T *save_curwin;
11539 int f;
11540 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011541 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542
11543 /* Always restore cursor position for ":mksession". For ":mkview" only
11544 * when 'viewoptions' contains "cursor". */
11545 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11546
11547 /*
11548 * Local argument list.
11549 */
11550 if (wp->w_alist == &global_alist)
11551 {
11552 if (put_line(fd, "argglobal") == FAIL)
11553 return FAIL;
11554 }
11555 else
11556 {
11557 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11558 flagp == &vop_flags
11559 || !(*flagp & SSOP_CURDIR)
11560 || wp->w_localdir != NULL, flagp) == FAIL)
11561 return FAIL;
11562 }
11563
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011564 /* Only when part of a session: restore the argument index. Some
11565 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011566 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011567 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011569 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570 || put_eol(fd) == FAIL)
11571 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011572 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573 }
11574
11575 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011576 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 {
11578 /*
11579 * Load the file.
11580 */
11581 if (wp->w_buffer->b_ffname != NULL
11582#ifdef FEAT_QUICKFIX
11583 && !bt_nofile(wp->w_buffer)
11584#endif
11585 )
11586 {
11587 /*
11588 * Editing a file in this buffer: use ":edit file".
11589 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011590 *
11591 * Note, if a buffer for that file already exists, use :badd to
11592 * edit that buffer, to not lose folding information (:edit resets
11593 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011595 if (fputs("if bufexists('", fd) < 0
11596 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11597 || fputs("') | buffer ", fd) < 0
11598 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11599 || fputs(" | else | edit ", fd) < 0
11600 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11601 || fputs(" | endif", fd) < 0
11602 ||
11603 put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 return FAIL;
11605 }
11606 else
11607 {
11608 /* No file in this buffer, just make it empty. */
11609 if (put_line(fd, "enew") == FAIL)
11610 return FAIL;
11611#ifdef FEAT_QUICKFIX
11612 if (wp->w_buffer->b_ffname != NULL)
11613 {
11614 /* The buffer does have a name, but it's not a file name. */
11615 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011616 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 return FAIL;
11618 }
11619#endif
11620 do_cursor = FALSE;
11621 }
11622 }
11623
11624 /*
11625 * Local mappings and abbreviations.
11626 */
11627 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11628 && makemap(fd, wp->w_buffer) == FAIL)
11629 return FAIL;
11630
11631 /*
11632 * Local options. Need to go to the window temporarily.
11633 * Store only local values when using ":mkview" and when ":mksession" is
11634 * used and 'sessionoptions' doesn't include "options".
11635 * Some folding options are always stored when "folds" is included,
11636 * otherwise the folds would not be restored correctly.
11637 */
11638 save_curwin = curwin;
11639 curwin = wp;
11640 curbuf = curwin->w_buffer;
11641 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11642 f = makeset(fd, OPT_LOCAL,
11643 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11644#ifdef FEAT_FOLDING
11645 else if (*flagp & SSOP_FOLDS)
11646 f = makefoldset(fd);
11647#endif
11648 else
11649 f = OK;
11650 curwin = save_curwin;
11651 curbuf = curwin->w_buffer;
11652 if (f == FAIL)
11653 return FAIL;
11654
11655#ifdef FEAT_FOLDING
11656 /*
11657 * Save Folds when 'buftype' is empty and for help files.
11658 */
11659 if ((*flagp & SSOP_FOLDS)
11660 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011661 && (*wp->w_buffer->b_p_bt == NUL || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 {
11663 if (put_folds(fd, wp) == FAIL)
11664 return FAIL;
11665 }
11666#endif
11667
11668 /*
11669 * Set the cursor after creating folds, since that moves the cursor.
11670 */
11671 if (do_cursor)
11672 {
11673
11674 /* Restore the cursor line in the file and relatively in the
11675 * window. Don't use "G", it changes the jumplist. */
11676 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11677 (long)wp->w_cursor.lnum,
11678 (long)(wp->w_cursor.lnum - wp->w_topline),
11679 (long)wp->w_height / 2, (long)wp->w_height) < 0
11680 || put_eol(fd) == FAIL
11681 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11682 || put_line(fd, "exe s:l") == FAIL
11683 || put_line(fd, "normal! zt") == FAIL
11684 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11685 || put_eol(fd) == FAIL)
11686 return FAIL;
11687 /* Restore the cursor column and left offset when not wrapping. */
11688 if (wp->w_cursor.col == 0)
11689 {
11690 if (put_line(fd, "normal! 0") == FAIL)
11691 return FAIL;
11692 }
11693 else
11694 {
11695 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11696 {
11697 if (fprintf(fd,
11698 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011699 (long)wp->w_virtcol + 1,
11700 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701 (long)wp->w_width / 2, (long)wp->w_width) < 0
11702 || put_eol(fd) == FAIL
11703 || put_line(fd, "if s:c > 0") == FAIL
11704 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011705 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11706 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707 || put_eol(fd) == FAIL
11708 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011709 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 || put_eol(fd) == FAIL
11711 || put_line(fd, "endif") == FAIL)
11712 return FAIL;
11713 }
11714 else
11715 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011716 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717 || put_eol(fd) == FAIL)
11718 return FAIL;
11719 }
11720 }
11721 }
11722
11723 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011724 * Local directory, if the current flag is not view options or the "curdir"
11725 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011727 if (wp->w_localdir != NULL
11728 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729 {
11730 if (fputs("lcd ", fd) < 0
11731 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11732 || put_eol(fd) == FAIL)
11733 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011734 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 }
11736
11737 return OK;
11738}
11739
11740/*
11741 * Write an argument list to the session file.
11742 * Returns FAIL if writing fails.
11743 */
11744 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011745ses_arglist(
11746 FILE *fd,
11747 char *cmd,
11748 garray_T *gap,
11749 int fullname, /* TRUE: use full path name */
11750 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751{
11752 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011753 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 char_u *s;
11755
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011756 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11757 return FAIL;
11758 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 return FAIL;
11760 for (i = 0; i < gap->ga_len; ++i)
11761 {
11762 /* NULL file names are skipped (only happens when out of memory). */
11763 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11764 if (s != NULL)
11765 {
11766 if (fullname)
11767 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011768 buf = alloc(MAXPATHL);
11769 if (buf != NULL)
11770 {
11771 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11772 s = buf;
11773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011775 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011776 || ses_put_fname(fd, s, flagp) == FAIL
11777 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011778 {
11779 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011781 }
11782 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783 }
11784 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011785 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786}
11787
11788/*
11789 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011790 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 * Returns FAIL if writing fails.
11792 */
11793 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011794ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795{
11796 char_u *name;
11797
11798 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011799 * the session file will be sourced.
11800 * Don't do this for ":mkview", we don't know the current directory.
11801 * Don't do this after ":lcd", we don't keep track of what the current
11802 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 if (buf->b_sfname != NULL
11804 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011805 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011806#ifdef FEAT_AUTOCHDIR
11807 && !p_acd
11808#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011809 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 name = buf->b_sfname;
11811 else
11812 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011813 if (ses_put_fname(fd, name, flagp) == FAIL
11814 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 return FAIL;
11816 return OK;
11817}
11818
11819/*
11820 * Write a file name to the session file.
11821 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11822 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011823 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 */
11825 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011826ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827{
11828 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011829 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831
11832 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011833 if (sname == NULL)
11834 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011836 if (*flagp & SSOP_SLASH)
11837 {
11838 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011839 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011840 if (*p == '\\')
11841 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011843
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011844 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011845 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011847 if (p == NULL)
11848 return FAIL;
11849
11850 /* write the result */
11851 if (fputs((char *)p, fd) < 0)
11852 retval = FAIL;
11853
11854 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 return retval;
11856}
11857
11858/*
11859 * ":loadview [nr]"
11860 */
11861 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011862ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863{
11864 char_u *fname;
11865
11866 fname = get_view_file(*eap->arg);
11867 if (fname != NULL)
11868 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011869 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870 vim_free(fname);
11871 }
11872}
11873
11874/*
11875 * Get the name of the view file for the current buffer.
11876 */
11877 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011878get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879{
11880 int len = 0;
11881 char_u *p, *s;
11882 char_u *retval;
11883 char_u *sname;
11884
11885 if (curbuf->b_ffname == NULL)
11886 {
11887 EMSG(_(e_noname));
11888 return NULL;
11889 }
11890 sname = home_replace_save(NULL, curbuf->b_ffname);
11891 if (sname == NULL)
11892 return NULL;
11893
11894 /*
11895 * We want a file name without separators, because we're not going to make
11896 * a directory.
11897 * "normal" path separator -> "=+"
11898 * "=" -> "=="
11899 * ":" path separator -> "=-"
11900 */
11901 for (p = sname; *p; ++p)
11902 if (*p == '=' || vim_ispathsep(*p))
11903 ++len;
11904 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11905 if (retval != NULL)
11906 {
11907 STRCPY(retval, p_vdir);
11908 add_pathsep(retval);
11909 s = retval + STRLEN(retval);
11910 for (p = sname; *p; ++p)
11911 {
11912 if (*p == '=')
11913 {
11914 *s++ = '=';
11915 *s++ = '=';
11916 }
11917 else if (vim_ispathsep(*p))
11918 {
11919 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011920#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 if (*p == ':')
11922 *s++ = '-';
11923 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011925 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926 }
11927 else
11928 *s++ = *p;
11929 }
11930 *s++ = '=';
11931 *s++ = c;
11932 STRCPY(s, ".vim");
11933 }
11934
11935 vim_free(sname);
11936 return retval;
11937}
11938
11939#endif /* FEAT_SESSION */
11940
11941/*
11942 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11943 * Return FAIL for a write error.
11944 */
11945 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011946put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947{
11948 if (
11949#ifdef USE_CRNL
11950 (
11951# ifdef MKSESSION_NL
11952 !mksession_nl &&
11953# endif
11954 (putc('\r', fd) < 0)) ||
11955#endif
11956 (putc('\n', fd) < 0))
11957 return FAIL;
11958 return OK;
11959}
11960
11961/*
11962 * Write a line to "fd".
11963 * Return FAIL for a write error.
11964 */
11965 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011966put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967{
11968 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11969 return FAIL;
11970 return OK;
11971}
11972
11973#ifdef FEAT_VIMINFO
11974/*
11975 * ":rviminfo" and ":wviminfo".
11976 */
11977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011978ex_viminfo(
11979 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980{
11981 char_u *save_viminfo;
11982
11983 save_viminfo = p_viminfo;
11984 if (*p_viminfo == NUL)
11985 p_viminfo = (char_u *)"'100";
11986 if (eap->cmdidx == CMD_rviminfo)
11987 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011988 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11989 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990 EMSG(_("E195: Cannot open viminfo file for reading"));
11991 }
11992 else
11993 write_viminfo(eap->arg, eap->forceit);
11994 p_viminfo = save_viminfo;
11995}
11996#endif
11997
11998#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011999/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012000 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012001 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012004dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006 if (fname == NULL)
12007 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012008 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009}
12010#endif
12011
12012/*
12013 * ":behave {mswin,xterm}"
12014 */
12015 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012016ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017{
12018 if (STRCMP(eap->arg, "mswin") == 0)
12019 {
12020 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12021 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12022 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12023 set_option_value((char_u *)"keymodel", 0L,
12024 (char_u *)"startsel,stopsel", 0);
12025 }
12026 else if (STRCMP(eap->arg, "xterm") == 0)
12027 {
12028 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12029 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12030 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12031 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12032 }
12033 else
12034 EMSG2(_(e_invarg2), eap->arg);
12035}
12036
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012037#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12038/*
12039 * Function given to ExpandGeneric() to obtain the possible arguments of the
12040 * ":behave {mswin,xterm}" command.
12041 */
12042 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012043get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012044{
12045 if (idx == 0)
12046 return (char_u *)"mswin";
12047 if (idx == 1)
12048 return (char_u *)"xterm";
12049 return NULL;
12050}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012051
12052/*
12053 * Function given to ExpandGeneric() to obtain the possible arguments of the
12054 * ":messages {clear}" command.
12055 */
12056 char_u *
12057get_messages_arg(expand_T *xp UNUSED, int idx)
12058{
12059 if (idx == 0)
12060 return (char_u *)"clear";
12061 return NULL;
12062}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012063#endif
12064
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012065 char_u *
12066get_mapclear_arg(expand_T *xp UNUSED, int idx)
12067{
12068 if (idx == 0)
12069 return (char_u *)"<buffer>";
12070 return NULL;
12071}
12072
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073#ifdef FEAT_AUTOCMD
12074static int filetype_detect = FALSE;
12075static int filetype_plugin = FALSE;
12076static int filetype_indent = FALSE;
12077
12078/*
12079 * ":filetype [plugin] [indent] {on,off,detect}"
12080 * on: Load the filetype.vim file to install autocommands for file types.
12081 * off: Load the ftoff.vim file to remove all autocommands for file types.
12082 * plugin on: load filetype.vim and ftplugin.vim
12083 * plugin off: load ftplugof.vim
12084 * indent on: load filetype.vim and indent.vim
12085 * indent off: load indoff.vim
12086 */
12087 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012088ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089{
12090 char_u *arg = eap->arg;
12091 int plugin = FALSE;
12092 int indent = FALSE;
12093
12094 if (*eap->arg == NUL)
12095 {
12096 /* Print current status. */
12097 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12098 filetype_detect ? "ON" : "OFF",
12099 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12100 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12101 return;
12102 }
12103
12104 /* Accept "plugin" and "indent" in any order. */
12105 for (;;)
12106 {
12107 if (STRNCMP(arg, "plugin", 6) == 0)
12108 {
12109 plugin = TRUE;
12110 arg = skipwhite(arg + 6);
12111 continue;
12112 }
12113 if (STRNCMP(arg, "indent", 6) == 0)
12114 {
12115 indent = TRUE;
12116 arg = skipwhite(arg + 6);
12117 continue;
12118 }
12119 break;
12120 }
12121 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12122 {
12123 if (*arg == 'o' || !filetype_detect)
12124 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012125 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126 filetype_detect = TRUE;
12127 if (plugin)
12128 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012129 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130 filetype_plugin = TRUE;
12131 }
12132 if (indent)
12133 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012134 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135 filetype_indent = TRUE;
12136 }
12137 }
12138 if (*arg == 'd')
12139 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012140 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012141 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142 }
12143 }
12144 else if (STRCMP(arg, "off") == 0)
12145 {
12146 if (plugin || indent)
12147 {
12148 if (plugin)
12149 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012150 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 filetype_plugin = FALSE;
12152 }
12153 if (indent)
12154 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012155 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156 filetype_indent = FALSE;
12157 }
12158 }
12159 else
12160 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012161 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162 filetype_detect = FALSE;
12163 }
12164 }
12165 else
12166 EMSG2(_(e_invarg2), arg);
12167}
12168
12169/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012170 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171 */
12172 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012173ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174{
12175 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012176 {
12177 char_u *arg = eap->arg;
12178
12179 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12180 arg += 9;
12181
12182 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12183 if (arg != eap->arg)
12184 did_filetype = FALSE;
12185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186}
12187#endif
12188
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012190ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191{
12192#ifdef FEAT_DIGRAPHS
12193 if (*eap->arg != NUL)
12194 putdigraph(eap->arg);
12195 else
12196 listdigraphs();
12197#else
12198 EMSG(_("E196: No digraphs in this version"));
12199#endif
12200}
12201
12202 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012203ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204{
12205 int flags = 0;
12206
12207 if (eap->cmdidx == CMD_setlocal)
12208 flags = OPT_LOCAL;
12209 else if (eap->cmdidx == CMD_setglobal)
12210 flags = OPT_GLOBAL;
12211#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12212 if (cmdmod.browse && flags == 0)
12213 ex_options(eap);
12214 else
12215#endif
12216 (void)do_set(eap->arg, flags);
12217}
12218
12219#ifdef FEAT_SEARCH_EXTRA
12220/*
12221 * ":nohlsearch"
12222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012224ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012226 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012227 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228}
12229
12230/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012231 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232 * Sets nextcmd to the start of the next command, if any. Also called when
12233 * skipping commands to find the next command.
12234 */
12235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012236ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237{
12238 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012239 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 char_u *end;
12241 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012242 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012243
12244 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012245 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012246 else
12247 {
12248 EMSG(e_invcmd);
12249 return;
12250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251
12252 /* First clear any old pattern. */
12253 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012254 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255
12256 if (ends_excmd(*eap->arg))
12257 end = eap->arg;
12258 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012259 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 end = eap->arg + 4;
12261 else
12262 {
12263 p = skiptowhite(eap->arg);
12264 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012265 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266 p = skipwhite(p);
12267 if (*p == NUL)
12268 {
12269 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012270 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271 EMSG2(_(e_invarg2), eap->arg);
12272 return;
12273 }
12274 end = skip_regexp(p + 1, *p, TRUE, NULL);
12275 if (!eap->skip)
12276 {
12277 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12278 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012279 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 eap->errmsg = e_trailing;
12281 return;
12282 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012283 if (*end != *p)
12284 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012285 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012286 EMSG2(_(e_invarg2), p);
12287 return;
12288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289
12290 c = *end;
12291 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012292 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012293 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012294 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 }
12296 }
12297 eap->nextcmd = find_nextcmd(end);
12298}
12299#endif
12300
12301#ifdef FEAT_CRYPT
12302/*
12303 * ":X": Get crypt key
12304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012306ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012308 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012309 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310}
12311#endif
12312
12313#ifdef FEAT_FOLDING
12314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012315ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316{
12317 if (foldManualAllowed(TRUE))
12318 foldCreate(eap->line1, eap->line2);
12319}
12320
12321 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012322ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323{
12324 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12325 eap->forceit, FALSE);
12326}
12327
12328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012329ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330{
12331 linenr_T lnum;
12332
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012333#ifdef FEAT_CLIPBOARD
12334 start_global_changes();
12335#endif
12336
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 /* First set the marks for all lines closed/open. */
12338 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12339 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12340 ml_setmarked(lnum);
12341
12342 /* Execute the command on the marked lines. */
12343 global_exe(eap->arg);
12344 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012345#ifdef FEAT_CLIPBOARD
12346 end_global_changes();
12347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348}
12349#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012350
12351# if defined(FEAT_TIMERS) || defined(PROTO)
12352 int
12353get_pressedreturn(void)
12354{
12355 return ex_pressedreturn;
12356}
12357
12358 void
12359set_pressedreturn(int val)
12360{
12361 ex_pressedreturn = val;
12362}
12363#endif