blob: 0539a5114b98ec077ee606da4c2b1e341ef9e0ca [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
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static void ex_autocmd(exarg_T *eap);
85static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static void ex_bunload(exarg_T *eap);
87static void ex_buffer(exarg_T *eap);
88static void ex_bmodified(exarg_T *eap);
89static void ex_bnext(exarg_T *eap);
90static void ex_bprevious(exarg_T *eap);
91static void ex_brewind(exarg_T *eap);
92static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static char_u *getargcmd(char_u **);
94static char_u *skip_cmd_arg(char_u *p, int rembs);
95static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#ifndef FEAT_QUICKFIX
97# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000098# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000099# define ex_cc ex_ni
100# define ex_cnext ex_ni
101# define ex_cfile ex_ni
102# define qf_list ex_ni
103# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200104# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000106# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200108#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109# define ex_cclose ex_ni
110# define ex_copen ex_ni
111# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200112# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000114#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
115# define ex_cexpr ex_ni
116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100118static int check_more(int, int);
Bram Moolenaarded27822017-01-02 14:27:34 +0100119static 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 +0100120static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200121#if !defined(FEAT_PERL) \
122 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
123 || !defined(FEAT_TCL) \
124 || !defined(FEAT_RUBY) \
125 || !defined(FEAT_LUA) \
126 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000127# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100128static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static char_u *invalid_range(exarg_T *eap);
131static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000132#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100133static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000134#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100135static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
136static void ex_highlight(exarg_T *eap);
137static void ex_colorscheme(exarg_T *eap);
138static void ex_quit(exarg_T *eap);
139static void ex_cquit(exarg_T *eap);
140static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100141static void ex_close(exarg_T *eap);
142static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
143static void ex_only(exarg_T *eap);
144static void ex_resize(exarg_T *eap);
145static void ex_stag(exarg_T *eap);
146static void ex_tabclose(exarg_T *eap);
147static void ex_tabonly(exarg_T *eap);
148static void ex_tabnext(exarg_T *eap);
149static void ex_tabmove(exarg_T *eap);
150static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200151#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100152static void ex_pclose(exarg_T *eap);
153static void ex_ptag(exarg_T *eap);
154static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#else
156# define ex_pclose ex_ni
157# define ex_ptag ex_ni
158# define ex_pedit ex_ni
159#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100160static void ex_hide(exarg_T *eap);
161static void ex_stop(exarg_T *eap);
162static void ex_exit(exarg_T *eap);
163static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100165static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#else
167# define ex_goto ex_ni
168#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100169static void ex_shell(exarg_T *eap);
170static void ex_preserve(exarg_T *eap);
171static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100172static void ex_mode(exarg_T *eap);
173static void ex_wrongmodifier(exarg_T *eap);
174static void ex_find(exarg_T *eap);
175static void ex_open(exarg_T *eap);
176static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177#ifndef FEAT_GUI
178# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100179static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180#endif
181#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100182static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#else
184# define ex_tearoff ex_ni
185#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100186#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
187 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100188static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#else
190# define ex_popup ex_ni
191#endif
192#ifndef FEAT_GUI_MSWIN
193# define ex_simalt ex_ni
194#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000195#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196# define gui_mch_find_dialog ex_ni
197# define gui_mch_replace_dialog ex_ni
198#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000199#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200# define ex_helpfind ex_ni
201#endif
202#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100203# define ex_cscope ex_ni
204# define ex_scscope ex_ni
205# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#endif
207#ifndef FEAT_SYN_HL
208# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200209# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000210#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100211#ifndef FEAT_EVAL
212# define ex_packadd ex_ni
213# define ex_packloadall ex_ni
214#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200215#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200216# define ex_syntime ex_ni
217#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000218#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000219# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000220# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000221# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000222# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000223# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000224#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200225#ifndef FEAT_PERSISTENT_UNDO
226# define ex_rundo ex_ni
227# define ex_wundo ex_ni
228#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200229#ifndef FEAT_LUA
230# define ex_lua ex_script_ni
231# define ex_luado ex_ni
232# define ex_luafile ex_ni
233#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000234#ifndef FEAT_MZSCHEME
235# define ex_mzscheme ex_script_ni
236# define ex_mzfile ex_ni
237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238#ifndef FEAT_PERL
239# define ex_perl ex_script_ni
240# define ex_perldo ex_ni
241#endif
242#ifndef FEAT_PYTHON
243# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200244# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245# define ex_pyfile ex_ni
246#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200247#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200248# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200249# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200250# define ex_py3file ex_ni
251#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100252#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
253# define ex_pyx ex_script_ni
254# define ex_pyxdo ex_ni
255# define ex_pyxfile ex_ni
256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#ifndef FEAT_TCL
258# define ex_tcl ex_script_ni
259# define ex_tcldo ex_ni
260# define ex_tclfile ex_ni
261#endif
262#ifndef FEAT_RUBY
263# define ex_ruby ex_script_ni
264# define ex_rubydo ex_ni
265# define ex_rubyfile ex_ni
266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267#ifndef FEAT_KEYMAP
268# define ex_loadkeymap ex_ni
269#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100270static void ex_swapname(exarg_T *eap);
271static void ex_syncbind(exarg_T *eap);
272static void ex_read(exarg_T *eap);
273static void ex_pwd(exarg_T *eap);
274static void ex_equal(exarg_T *eap);
275static void ex_sleep(exarg_T *eap);
276static void do_exmap(exarg_T *eap, int isabbrev);
277static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100278static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000279#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100280static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281#else
282# define ex_winpos ex_ni
283#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100284static void ex_operators(exarg_T *eap);
285static void ex_put(exarg_T *eap);
286static void ex_copymove(exarg_T *eap);
287static void ex_submagic(exarg_T *eap);
288static void ex_join(exarg_T *eap);
289static void ex_at(exarg_T *eap);
290static void ex_bang(exarg_T *eap);
291static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200292#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100293static void ex_wundo(exarg_T *eap);
294static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200295#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100296static void ex_redo(exarg_T *eap);
297static void ex_later(exarg_T *eap);
298static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100299static void ex_redrawstatus(exarg_T *eap);
300static void close_redir(void);
301static void ex_mkrc(exarg_T *eap);
302static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100304static char_u *uc_fun_cmd(void);
305static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100307static void ex_startinsert(exarg_T *eap);
308static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100310static void ex_checkpath(exarg_T *eap);
311static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#else
313# define ex_findpat ex_ni
314# define ex_checkpath ex_ni
315#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200316#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318#else
319# define ex_psearch ex_ni
320#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100321static void ex_tag(exarg_T *eap);
322static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#ifndef FEAT_EVAL
324# define ex_scriptnames ex_ni
325# define ex_finish ex_ni
326# define ex_echo ex_ni
327# define ex_echohl ex_ni
328# define ex_execute ex_ni
329# define ex_call ex_ni
330# define ex_if ex_ni
331# define ex_endif ex_ni
332# define ex_else ex_ni
333# define ex_while ex_ni
334# define ex_continue ex_ni
335# define ex_break ex_ni
336# define ex_endwhile ex_ni
337# define ex_throw ex_ni
338# define ex_try ex_ni
339# define ex_catch ex_ni
340# define ex_finally ex_ni
341# define ex_endtry ex_ni
342# define ex_endfunction ex_ni
343# define ex_let ex_ni
344# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000345# define ex_lockvar ex_ni
346# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_function ex_ni
348# define ex_delfunction ex_ni
349# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000350# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100352static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100354static int makeopens(FILE *fd, char_u *dirnow);
355static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
356static void ex_loadview(exarg_T *eap);
357static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000358static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#else
360# define ex_loadview ex_ni
361#endif
362#ifndef FEAT_EVAL
363# define ex_compiler ex_ni
364#endif
365#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100366static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367#else
368# define ex_viminfo ex_ni
369#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100370static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100371static void ex_filetype(exarg_T *eap);
372static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000374# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375# define ex_diffpatch ex_ni
376# define ex_diffgetput ex_ni
377# define ex_diffsplit ex_ni
378# define ex_diffthis ex_ni
379# define ex_diffupdate ex_ni
380#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100381static void ex_digraphs(exarg_T *eap);
382static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100383#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384# define ex_options ex_ni
385#endif
386#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100387static void ex_nohlsearch(exarg_T *eap);
388static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#else
390# define ex_nohlsearch ex_ni
391# define ex_match ex_ni
392#endif
393#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100394static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395#else
396# define ex_X ex_ni
397#endif
398#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100399static void ex_fold(exarg_T *eap);
400static void ex_foldopen(exarg_T *eap);
401static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#else
403# define ex_fold ex_ni
404# define ex_foldopen ex_ni
405# define ex_folddo ex_ni
406#endif
407#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
408 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
409# define ex_language ex_ni
410#endif
411#ifndef FEAT_SIGNS
412# define ex_sign ex_ni
413#endif
414#ifndef FEAT_SUN_WORKSHOP
415# define ex_wsverb ex_ni
416#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000417#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200418# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000419# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200420# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422
423#ifndef FEAT_EVAL
424# define ex_debug ex_ni
425# define ex_breakadd ex_ni
426# define ex_debuggreedy ex_ni
427# define ex_breakdel ex_ni
428# define ex_breaklist ex_ni
429#endif
430
431#ifndef FEAT_CMDHIST
432# define ex_history ex_ni
433#endif
434#ifndef FEAT_JUMPLIST
435# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200436# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437# define ex_changes ex_ni
438#endif
439
Bram Moolenaar05159a02005-02-26 23:04:13 +0000440#ifndef FEAT_PROFILE
441# define ex_profile ex_ni
442#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200443#ifndef FEAT_TERMINAL
444# define ex_terminal ex_ni
445#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000446
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447/*
448 * Declare cmdnames[].
449 */
450#define DO_DECLARE_EXCMD
451#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200452#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100453
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454static char_u dollar_command[2] = {'$', 0};
455
456
457#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000458/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459typedef struct
460{
461 char_u *line; /* command line */
462 linenr_T lnum; /* sourcing_lnum of the line */
463} wcmd_T;
464
465/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000466 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000468 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000470struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471{
472 garray_T *lines_gap; /* growarray with line info */
473 int current_line; /* last read line from growarray */
474 int repeating; /* TRUE when looping a second time */
475 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100476 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 void *cookie;
478};
479
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100480static char_u *get_loop_line(int c, void *cookie, int indent);
481static int store_loop_line(garray_T *gap, char_u *line);
482static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000483
484/* Struct to save a few things while debugging. Used in do_cmdline() only. */
485struct dbg_stuff
486{
487 int trylevel;
488 int force_abort;
489 except_T *caught_stack;
490 char_u *vv_exception;
491 char_u *vv_throwpoint;
492 int did_emsg;
493 int got_int;
494 int did_throw;
495 int need_rethrow;
496 int check_cstack;
497 except_T *current_exception;
498};
499
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100500static void save_dbg_stuff(struct dbg_stuff *dsp);
501static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000502
503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100504save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000505{
506 dsp->trylevel = trylevel; trylevel = 0;
507 dsp->force_abort = force_abort; force_abort = FALSE;
508 dsp->caught_stack = caught_stack; caught_stack = NULL;
509 dsp->vv_exception = v_exception(NULL);
510 dsp->vv_throwpoint = v_throwpoint(NULL);
511
512 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
513 dsp->did_emsg = did_emsg; did_emsg = FALSE;
514 dsp->got_int = got_int; got_int = FALSE;
515 dsp->did_throw = did_throw; did_throw = FALSE;
516 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
517 dsp->check_cstack = check_cstack; check_cstack = FALSE;
518 dsp->current_exception = current_exception; current_exception = NULL;
519}
520
521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100522restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000523{
524 suppress_errthrow = FALSE;
525 trylevel = dsp->trylevel;
526 force_abort = dsp->force_abort;
527 caught_stack = dsp->caught_stack;
528 (void)v_exception(dsp->vv_exception);
529 (void)v_throwpoint(dsp->vv_throwpoint);
530 did_emsg = dsp->did_emsg;
531 got_int = dsp->got_int;
532 did_throw = dsp->did_throw;
533 need_rethrow = dsp->need_rethrow;
534 check_cstack = dsp->check_cstack;
535 current_exception = dsp->current_exception;
536}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537#endif
538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539/*
540 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
541 * command is given.
542 */
543 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100544do_exmode(
545 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
547 int save_msg_scroll;
548 int prev_msg_row;
549 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100550 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000551
552 if (improved)
553 exmode_active = EXMODE_VIM;
554 else
555 exmode_active = EXMODE_NORMAL;
556 State = NORMAL;
557
558 /* When using ":global /pat/ visual" and then "Q" we return to continue
559 * the :global command. */
560 if (global_busy)
561 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
563 save_msg_scroll = msg_scroll;
564 ++RedrawingDisabled; /* don't redisplay the window */
565 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566#ifdef FEAT_GUI
567 /* Ignore scrollbar and mouse events in Ex mode */
568 ++hold_gui_events;
569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
571 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
572 while (exmode_active)
573 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000574 /* Check for a ":normal" command and no more characters left. */
575 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
576 {
577 exmode_active = FALSE;
578 break;
579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 msg_scroll = TRUE;
581 need_wait_return = FALSE;
582 ex_pressedreturn = FALSE;
583 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100584 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 prev_msg_row = msg_row;
586 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (improved)
588 {
589 cmdline_row = msg_row;
590 do_cmdline(NULL, getexline, NULL, 0);
591 }
592 else
593 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
594 lines_left = Rows - 1;
595
Bram Moolenaardf177f62005-02-22 08:39:57 +0000596 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100597 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000599 if (curbuf->b_ml.ml_flags & ML_EMPTY)
600 EMSG(_(e_emptybuf));
601 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000603 if (ex_pressedreturn)
604 {
605 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000606 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000607 msg_row = prev_msg_row;
608 if (prev_msg_row == Rows - 1)
609 msg_row--;
610 }
611 msg_col = 0;
612 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
613 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000616 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
617 {
618 if (curbuf->b_ml.ml_flags & ML_EMPTY)
619 EMSG(_(e_emptybuf));
620 else
621 EMSG(_("E501: At end-of-file"));
622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 }
624
625#ifdef FEAT_GUI
626 --hold_gui_events;
627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 --RedrawingDisabled;
629 --no_wait_return;
630 update_screen(CLEAR);
631 need_wait_return = FALSE;
632 msg_scroll = save_msg_scroll;
633}
634
635/*
636 * Execute a simple command line. Used for translated commands like "*".
637 */
638 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100639do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640{
641 return do_cmdline(cmd, NULL, NULL,
642 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
643}
644
645/*
646 * do_cmdline(): execute one Ex command line
647 *
648 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100649 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 * 2. Split up in parts separated with '|'.
651 *
652 * This function can be called recursively!
653 *
654 * flags:
655 * DOCMD_VERBOSE - The command will be included in the error message.
656 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100657 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 * DOCMD_KEYTYPED - Don't reset KeyTyped.
659 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
660 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
661 *
662 * return FAIL if cmdline could not be executed, OK otherwise
663 */
664 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100665do_cmdline(
666 char_u *cmdline,
667 char_u *(*fgetline)(int, void *, int),
668 void *cookie, /* argument for fgetline() */
669 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
671 char_u *next_cmdline; /* next cmd to execute */
672 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100673 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 static int recursive = 0; /* recursive depth */
675 int msg_didout_before_start = 0;
676 int count = 0; /* line number count */
677 int did_inc = FALSE; /* incremented RedrawingDisabled */
678 int retval = OK;
679#ifdef FEAT_EVAL
680 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000681 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 int current_line = 0; /* active line in lines_ga */
683 char_u *fname = NULL; /* function or script name */
684 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
685 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000686 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 int initial_trylevel;
688 struct msglist **saved_msg_list = NULL;
689 struct msglist *private_msg_list;
690
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100691 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100692 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000694 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000696 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100698# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699# define cmd_cookie cookie
700#endif
701 static int call_depth = 0; /* recursiveness */
702
703#ifdef FEAT_EVAL
704 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
705 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000706 * This ensures that the do_errthrow() call in do_one_cmd() does not
707 * combine the messages stored by an earlier invocation of do_one_cmd()
708 * with the command name of the later one. This would happen when
709 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 saved_msg_list = msg_list;
711 msg_list = &private_msg_list;
712 private_msg_list = NULL;
713#endif
714
715 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100716 * here. The value of 200 allows nested function calls, ":source", etc.
717 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100718 if (call_depth >= 200
719#ifdef FEAT_EVAL
720 && call_depth >= p_mfd
721#endif
722 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 {
724 EMSG(_("E169: Command too recursive"));
725#ifdef FEAT_EVAL
726 /* When converting to an exception, we do not include the command name
727 * since this is not an error of the specific command. */
728 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
729 msg_list = saved_msg_list;
730#endif
731 return FAIL;
732 }
733 ++call_depth;
734
735#ifdef FEAT_EVAL
736 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000737 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 cstack.cs_trylevel = 0;
739 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000740 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
742
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100743 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744
745 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100746 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000747 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 ++ex_nesting_level;
749
750 /* Get the function or script name and the address where the next breakpoint
751 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000752 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
754 fname = func_name(real_cookie);
755 breakpoint = func_breakpoint(real_cookie);
756 dbg_tick = func_dbg_tick(real_cookie);
757 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100758 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 {
760 fname = sourcing_name;
761 breakpoint = source_breakpoint(real_cookie);
762 dbg_tick = source_dbg_tick(real_cookie);
763 }
764
765 /*
766 * Initialize "force_abort" and "suppress_errthrow" at the top level.
767 */
768 if (!recursive)
769 {
770 force_abort = FALSE;
771 suppress_errthrow = FALSE;
772 }
773
774 /*
775 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000776 * exception handling (used when debugging). Otherwise clear it to avoid
777 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000779 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000780 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000781 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200782 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
784 initial_trylevel = trylevel;
785
786 /*
787 * "did_throw" will be set to TRUE when an exception is being thrown.
788 */
789 did_throw = FALSE;
790#endif
791 /*
792 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000793 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 * If force_abort is set, we cancel everything.
795 */
796 did_emsg = FALSE;
797
798 /*
799 * KeyTyped is only set when calling vgetc(). Reset it here when not
800 * calling vgetc() (sourced command lines).
801 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100802 if (!(flags & DOCMD_KEYTYPED)
803 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 KeyTyped = FALSE;
805
806 /*
807 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000808 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 * - for multiple commands on one line, separated with '|'
810 * - when repeating until there are no more lines (for ":source")
811 */
812 next_cmdline = cmdline;
813 do
814 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100816 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817#endif
818
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000819 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 if (next_cmdline == NULL
821#ifdef FEAT_EVAL
822 && !force_abort
823 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000824 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825#endif
826 )
827 did_emsg = FALSE;
828
829 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000830 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100831 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 * 3. If a line is given: Make a copy, so we can mess with it.
833 */
834
835#ifdef FEAT_EVAL
836 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000837 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
839 /* Each '|' separated command is stored separately in lines_ga, to
840 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100841 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
843 /* Check if a function has returned or, unless it has an unclosed
844 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000848 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849 func_line_end(real_cookie);
850# endif
851 if (func_has_ended(real_cookie))
852 {
853 retval = FAIL;
854 break;
855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000857#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000858 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100859 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000860 script_line_end();
861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
863 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100864 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 {
866 retval = FAIL;
867 break;
868 }
869
870 /* If breakpoints have been added/deleted need to check for it. */
871 if (breakpoint != NULL && dbg_tick != NULL
872 && *dbg_tick != debug_tick)
873 {
874 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100875 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 fname, sourcing_lnum);
877 *dbg_tick = debug_tick;
878 }
879
880 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
881 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
882
883 /* Did we encounter a breakpoint? */
884 if (breakpoint != NULL && *breakpoint != 0
885 && *breakpoint <= sourcing_lnum)
886 {
887 dbg_breakpoint(fname, sourcing_lnum);
888 /* Find next breakpoint. */
889 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100890 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 fname, sourcing_lnum);
892 *dbg_tick = debug_tick;
893 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000894# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000895 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896 {
897 if (getline_is_func)
898 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100899 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000900 script_line_start();
901 }
902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 }
904
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000905 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000907 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100908 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 * below, so that it stores lines in or reads them from
910 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000911 * while/for loop. */
912 cmd_getline = get_loop_line;
913 cmd_cookie = (void *)&cmd_loop_cookie;
914 cmd_loop_cookie.lines_gap = &lines_ga;
915 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100916 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000917 cmd_loop_cookie.cookie = cookie;
918 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920 else
921 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100922 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 cmd_cookie = cookie;
924 }
925#endif
926
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100927 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 if (next_cmdline == NULL)
929 {
930 /*
931 * Need to set msg_didout for the first line after an ":if",
932 * otherwise the ":if" will be overwritten.
933 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100934 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100936 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937#ifdef FEAT_EVAL
938 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
939#else
940 0
941#endif
942 )) == NULL)
943 {
944 /* Don't call wait_return for aborted command line. The NULL
945 * returned for the end of a sourced file or executed function
946 * doesn't do this. */
947 if (KeyTyped && !(flags & DOCMD_REPEAT))
948 need_wait_return = FALSE;
949 retval = FAIL;
950 break;
951 }
952 used_getline = TRUE;
953
954 /*
955 * Keep the first typed line. Clear it when more lines are typed.
956 */
957 if (flags & DOCMD_KEEPLINE)
958 {
959 vim_free(repeat_cmdline);
960 if (count == 0)
961 repeat_cmdline = vim_strsave(next_cmdline);
962 else
963 repeat_cmdline = NULL;
964 }
965 }
966
967 /* 3. Make a copy of the command so we can mess with it. */
968 else if (cmdline_copy == NULL)
969 {
970 next_cmdline = vim_strsave(next_cmdline);
971 if (next_cmdline == NULL)
972 {
973 EMSG(_(e_outofmem));
974 retval = FAIL;
975 break;
976 }
977 }
978 cmdline_copy = next_cmdline;
979
980#ifdef FEAT_EVAL
981 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000982 * Save the current line when inside a ":while" or ":for", and when
983 * the command looks like a ":while" or ":for", because we may need it
984 * later. When there is a '|' and another command, it is stored
985 * separately, because we need to be able to jump back to it from an
986 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000988 if (current_line == lines_ga.ga_len
989 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000991 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 {
993 retval = FAIL;
994 break;
995 }
996 }
997 did_endif = FALSE;
998#endif
999
1000 if (count++ == 0)
1001 {
1002 /*
1003 * All output from the commands is put below each other, without
1004 * waiting for a return. Don't do this when executing commands
1005 * from a script or when being called recursive (e.g. for ":e
1006 * +command file").
1007 */
1008 if (!(flags & DOCMD_NOWAIT) && !recursive)
1009 {
1010 msg_didout_before_start = msg_didout;
1011 msg_didany = FALSE; /* no output yet */
1012 msg_start();
1013 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001014 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 ++RedrawingDisabled;
1016 did_inc = TRUE;
1017 }
1018 }
1019
1020 if (p_verbose >= 15 && sourcing_name != NULL)
1021 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001023 verbose_enter_scroll();
1024
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 smsg((char_u *)_("line %ld: %s"),
1026 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001027 if (msg_silent == 0)
1028 msg_puts((char_u *)"\n"); /* don't overwrite this */
1029
1030 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 --no_wait_return;
1032 }
1033
1034 /*
1035 * 2. Execute one '|' separated command.
1036 * do_one_cmd() will return NULL if there is no trailing '|'.
1037 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1038 */
1039 ++recursive;
1040 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1041#ifdef FEAT_EVAL
1042 &cstack,
1043#endif
1044 cmd_getline, cmd_cookie);
1045 --recursive;
1046
1047#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 if (cmd_cookie == (void *)&cmd_loop_cookie)
1049 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052#endif
1053
1054 if (next_cmdline == NULL)
1055 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001056 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057#ifdef FEAT_CMDHIST
1058 /*
1059 * If the command was typed, remember it for the ':' register.
1060 * Do this AFTER executing the command to make :@: work.
1061 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001062 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 && new_last_cmdline != NULL)
1064 {
1065 vim_free(last_cmdline);
1066 last_cmdline = new_last_cmdline;
1067 new_last_cmdline = NULL;
1068 }
1069#endif
1070 }
1071 else
1072 {
1073 /* need to copy the command after the '|' to cmdline_copy, for the
1074 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001075 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 next_cmdline = cmdline_copy;
1077 }
1078
1079
1080#ifdef FEAT_EVAL
1081 /* reset did_emsg for a function that is not aborted by an error */
1082 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001083 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 && !func_has_abort(real_cookie))
1085 did_emsg = FALSE;
1086
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001087 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
1089 ++current_line;
1090
1091 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 * An ":endwhile", ":endfor" and ":continue" is handled here.
1093 * If we were executing commands, jump back to the ":while" or
1094 * ":for".
1095 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001099 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001101 /* Jump back to the matching ":while" or ":for". Be careful
1102 * not to use a cs_line[] from an entry that isn't a ":while"
1103 * or ":for": It would make "current_line" invalid and can
1104 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 if (!did_emsg && !got_int && !did_throw
1106 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001107 && (cstack.cs_flags[cstack.cs_idx]
1108 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 && cstack.cs_line[cstack.cs_idx] >= 0
1110 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1111 {
1112 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001113 /* remember we jumped there */
1114 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 line_breakcheck(); /* check if CTRL-C typed */
1116
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 /* Check for the next breakpoint at or after the ":while"
1118 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (breakpoint != NULL)
1120 {
1121 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001122 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 fname,
1124 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1125 *dbg_tick = debug_tick;
1126 }
1127 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001128 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001132 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1133 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 }
1136
1137 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001140 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001142 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1144 }
1145 }
1146
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001147 /* Check for the next breakpoint after a watchexpression */
1148 if (breakpoint != NULL && has_watchexpr())
1149 {
1150 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1151 *dbg_tick = debug_tick;
1152 }
1153
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 /*
1155 * When not inside any ":while" loop, clear remembered lines.
1156 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {
1159 if (lines_ga.ga_len > 0)
1160 {
1161 sourcing_lnum =
1162 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1163 free_cmdlines(&lines_ga);
1164 }
1165 current_line = 0;
1166 }
1167
1168 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001169 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1170 * being restored at the ":endtry". Reset them here and set the
1171 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1172 * This includes the case where a missing ":endif", ":endwhile" or
1173 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001177 cstack.cs_lflags &= ~CSL_HAD_FINA;
1178 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1179 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 did_throw ? (void *)current_exception : NULL);
1181 did_emsg = got_int = did_throw = FALSE;
1182 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1183 }
1184
1185 /* Update global "trylevel" for recursive calls to do_cmdline() from
1186 * within this loop. */
1187 trylevel = initial_trylevel + cstack.cs_trylevel;
1188
1189 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001190 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 * files) is aborted because of an error, an interrupt, or an uncaught
1192 * exception, cancel everything. If it is left normally, reset
1193 * force_abort to get the non-EH compatible abortion behavior for
1194 * the rest of the script.
1195 */
1196 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1197 force_abort = FALSE;
1198
1199 /* Convert an interrupt to an exception if appropriate. */
1200 (void)do_intthrow(&cstack);
1201#endif /* FEAT_EVAL */
1202
1203 }
1204 /*
1205 * Continue executing command lines when:
1206 * - no CTRL-C typed, no aborting error, no exception thrown or try
1207 * conditionals need to be checked for executing finally clauses or
1208 * catching an interrupt exception
1209 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001210 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 * looping for ":source" command or function call.
1212 */
1213 while (!((got_int
1214#ifdef FEAT_EVAL
1215 || (did_emsg && force_abort) || did_throw
1216#endif
1217 )
1218#ifdef FEAT_EVAL
1219 && cstack.cs_trylevel == 0
1220#endif
1221 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001222 && !(did_emsg
1223#ifdef FEAT_EVAL
1224 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001225 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001226 * the :endtry to be missed. */
1227 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1228#endif
1229 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001230 && (getline_equal(fgetline, cookie, getexmodeline)
1231 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 && (next_cmdline != NULL
1233#ifdef FEAT_EVAL
1234 || cstack.cs_idx >= 0
1235#endif
1236 || (flags & DOCMD_REPEAT)));
1237
1238 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001239 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240#ifdef FEAT_EVAL
1241 free_cmdlines(&lines_ga);
1242 ga_clear(&lines_ga);
1243
1244 if (cstack.cs_idx >= 0)
1245 {
1246 /*
1247 * If a sourced file or executed function ran to its end, report the
1248 * unclosed conditional.
1249 */
1250 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001251 && ((getline_equal(fgetline, cookie, getsourceline)
1252 && !source_finished(fgetline, cookie))
1253 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 && !func_has_ended(real_cookie))))
1255 {
1256 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1257 EMSG(_(e_endtry));
1258 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1259 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001260 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1261 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 else
1263 EMSG(_(e_endif));
1264 }
1265
1266 /*
1267 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1268 * ":endtry" in a sourced file or executed function. If the try
1269 * conditional is in its finally clause, ignore anything pending.
1270 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001271 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 */
1273 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001274 {
1275 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1276
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001277 if (idx >= 0)
1278 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001279 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1280 &cstack.cs_looplevel);
1281 }
1282 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 trylevel = initial_trylevel;
1284 }
1285
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001286 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1287 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001289 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 ? (char_u *)"endfunction" : (char_u *)NULL);
1291
1292 if (trylevel == 0)
1293 {
1294 /*
1295 * When an exception is being thrown out of the outermost try
1296 * conditional, discard the uncaught exception, disable the conversion
1297 * of interrupts or errors to exceptions, and ensure that no more
1298 * commands are executed.
1299 */
1300 if (did_throw)
1301 {
1302 void *p = NULL;
1303 char_u *saved_sourcing_name;
1304 int saved_sourcing_lnum;
1305 struct msglist *messages = NULL, *next;
1306
1307 /*
1308 * If the uncaught exception is a user exception, report it as an
1309 * error. If it is an error exception, display the saved error
1310 * message now. For an interrupt exception, do nothing; the
1311 * interrupt message is given elsewhere.
1312 */
1313 switch (current_exception->type)
1314 {
1315 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001316 vim_snprintf((char *)IObuff, IOSIZE,
1317 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 current_exception->value);
1319 p = vim_strsave(IObuff);
1320 break;
1321 case ET_ERROR:
1322 messages = current_exception->messages;
1323 current_exception->messages = NULL;
1324 break;
1325 case ET_INTERRUPT:
1326 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328
1329 saved_sourcing_name = sourcing_name;
1330 saved_sourcing_lnum = sourcing_lnum;
1331 sourcing_name = current_exception->throw_name;
1332 sourcing_lnum = current_exception->throw_lnum;
1333 current_exception->throw_name = NULL;
1334
1335 discard_current_exception(); /* uses IObuff if 'verbose' */
1336 suppress_errthrow = TRUE;
1337 force_abort = TRUE;
1338
1339 if (messages != NULL)
1340 {
1341 do
1342 {
1343 next = messages->next;
1344 emsg(messages->msg);
1345 vim_free(messages->msg);
1346 vim_free(messages);
1347 messages = next;
1348 }
1349 while (messages != NULL);
1350 }
1351 else if (p != NULL)
1352 {
1353 emsg(p);
1354 vim_free(p);
1355 }
1356 vim_free(sourcing_name);
1357 sourcing_name = saved_sourcing_name;
1358 sourcing_lnum = saved_sourcing_lnum;
1359 }
1360
1361 /*
1362 * On an interrupt or an aborting error not converted to an exception,
1363 * disable the conversion of errors to exceptions. (Interrupts are not
1364 * converted any more, here.) This enables also the interrupt message
1365 * when force_abort is set and did_emsg unset in case of an interrupt
1366 * from a finally clause after an error.
1367 */
1368 else if (got_int || (did_emsg && force_abort))
1369 suppress_errthrow = TRUE;
1370 }
1371
1372 /*
1373 * The current cstack will be freed when do_cmdline() returns. An uncaught
1374 * exception will have to be rethrown in the previous cstack. If a function
1375 * has just returned or a script file was just finished and the previous
1376 * cstack belongs to the same function or, respectively, script file, it
1377 * will have to be checked for finally clauses to be executed due to the
1378 * ":return" or ":finish". This is done in do_one_cmd().
1379 */
1380 if (did_throw)
1381 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001382 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001384 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 && ex_nesting_level > func_level(real_cookie) + 1))
1386 {
1387 if (!did_throw)
1388 check_cstack = TRUE;
1389 }
1390 else
1391 {
1392 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001393 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 --ex_nesting_level;
1395 /*
1396 * Go to debug mode when returning from a function in which we are
1397 * single-stepping.
1398 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001399 if ((getline_equal(fgetline, cookie, getsourceline)
1400 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001402 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 ? (char_u *)_("End of sourced file")
1404 : (char_u *)_("End of function"));
1405 }
1406
1407 /*
1408 * Restore the exception environment (done after returning from the
1409 * debugger).
1410 */
1411 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001412 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413
1414 msg_list = saved_msg_list;
1415#endif /* FEAT_EVAL */
1416
1417 /*
1418 * If there was too much output to fit on the command line, ask the user to
1419 * hit return before redrawing the screen. With the ":global" command we do
1420 * this only once after the command is finished.
1421 */
1422 if (did_inc)
1423 {
1424 --RedrawingDisabled;
1425 --no_wait_return;
1426 msg_scroll = FALSE;
1427
1428 /*
1429 * When just finished an ":if"-":else" which was typed, no need to
1430 * wait for hit-return. Also for an error situation.
1431 */
1432 if (retval == FAIL
1433#ifdef FEAT_EVAL
1434 || (did_endif && KeyTyped && !did_emsg)
1435#endif
1436 )
1437 {
1438 need_wait_return = FALSE;
1439 msg_didany = FALSE; /* don't wait when restarting edit */
1440 }
1441 else if (need_wait_return)
1442 {
1443 /*
1444 * The msg_start() above clears msg_didout. The wait_return we do
1445 * here should not overwrite the command that may be shown before
1446 * doing that.
1447 */
1448 msg_didout |= msg_didout_before_start;
1449 wait_return(FALSE);
1450 }
1451 }
1452
Bram Moolenaar2a942252012-11-28 23:03:07 +01001453#ifdef FEAT_EVAL
1454 did_endif = FALSE; /* in case do_cmdline used recursively */
1455#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 /*
1457 * Reset if_level, in case a sourced script file contains more ":if" than
1458 * ":endif" (could be ":if x | foo | endif").
1459 */
1460 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001461#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001462
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 --call_depth;
1464 return retval;
1465}
1466
1467#ifdef FEAT_EVAL
1468/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001469 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 */
1471 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001472get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001474 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 wcmd_T *wp;
1476 char_u *line;
1477
1478 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1479 {
1480 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001481 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001483 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (cp->getline == NULL)
1485 line = getcmdline(c, 0L, indent);
1486 else
1487 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001488 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 ++cp->current_line;
1490
1491 return line;
1492 }
1493
1494 KeyTyped = FALSE;
1495 ++cp->current_line;
1496 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1497 sourcing_lnum = wp->lnum;
1498 return vim_strsave(wp->line);
1499}
1500
1501/*
1502 * Store a line in "gap" so that a ":while" loop can execute it again.
1503 */
1504 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001505store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506{
1507 if (ga_grow(gap, 1) == FAIL)
1508 return FAIL;
1509 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1510 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1511 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 return OK;
1513}
1514
1515/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001516 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 */
1518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001519free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
1521 while (gap->ga_len > 0)
1522 {
1523 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1524 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 }
1526}
1527#endif
1528
1529/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001530 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1531 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001534getline_equal(
1535 char_u *(*fgetline)(int, void *, int),
1536 void *cookie UNUSED, /* argument for fgetline() */
1537 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001540 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001541 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542
Bram Moolenaar89d40322006-08-29 15:30:07 +00001543 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001544 * function that's originally used to obtain the lines. This may be
1545 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001546 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547 cp = (struct loop_cookie *)cookie;
1548 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 {
1550 gp = cp->getline;
1551 cp = cp->cookie;
1552 }
1553 return gp == func;
1554#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001555 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556#endif
1557}
1558
1559#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1560/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001561 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 * getline function. Otherwise return "cookie".
1563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001565getline_cookie(
1566 char_u *(*fgetline)(int, void *, int) UNUSED,
1567 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
1569# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001570 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001571 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
Bram Moolenaar89d40322006-08-29 15:30:07 +00001573 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001574 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001576 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001577 cp = (struct loop_cookie *)cookie;
1578 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 {
1580 gp = cp->getline;
1581 cp = cp->cookie;
1582 }
1583 return cp;
1584# else
1585 return cookie;
1586# endif
1587}
1588#endif
1589
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001590
1591/*
1592 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1593 * ":bwipeout", etc.
1594 * Returns the buffer number.
1595 */
1596 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001597compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001598{
1599 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001600 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001601 int count = offset;
1602
1603 buf = firstbuf;
1604 while (buf->b_next != NULL && buf->b_fnum < lnum)
1605 buf = buf->b_next;
1606 while (count != 0)
1607 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001608 count += (offset < 0) ? 1 : -1;
1609 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1610 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001611 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001612 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613 if (addr_type == ADDR_LOADED_BUFFERS)
1614 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001615 while (buf->b_ml.ml_mfp == NULL)
1616 {
1617 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1618 if (nextbuf == NULL)
1619 break;
1620 buf = nextbuf;
1621 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001622 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001623 /* we might have gone too far, last buffer is not loadedd */
1624 if (addr_type == ADDR_LOADED_BUFFERS)
1625 while (buf->b_ml.ml_mfp == NULL)
1626 {
1627 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1628 if (nextbuf == NULL)
1629 break;
1630 buf = nextbuf;
1631 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001632 return buf->b_fnum;
1633}
1634
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001635static int current_win_nr(win_T *win);
1636static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001637
1638 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001639current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001640{
1641 win_T *wp;
1642 int nr = 0;
1643
Bram Moolenaar29323592016-07-24 22:04:11 +02001644 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001645 {
1646 ++nr;
1647 if (wp == win)
1648 break;
1649 }
1650 return nr;
1651}
1652
1653 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001654current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001655{
1656 tabpage_T *tp;
1657 int nr = 0;
1658
Bram Moolenaar29323592016-07-24 22:04:11 +02001659 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001660 {
1661 ++nr;
1662 if (tp == tab)
1663 break;
1664 }
1665 return nr;
1666}
1667
1668# define CURRENT_WIN_NR current_win_nr(curwin)
1669# define LAST_WIN_NR current_win_nr(NULL)
1670# define CURRENT_TAB_NR current_tab_nr(curtab)
1671# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001672
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673/*
1674 * Execute one Ex command.
1675 *
1676 * If 'sourcing' is TRUE, the command will be included in the error message.
1677 *
1678 * 1. skip comment lines and leading space
1679 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001680 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001681 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001682 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001683 * 6. parse arguments
1684 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001686 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 *
1688 * This function may be called recursively!
1689 */
1690#if (_MSC_VER == 1200)
1691/*
Bram Moolenaared203462004-06-16 11:19:22 +00001692 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001694 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695#endif
1696 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001697do_one_cmd(
1698 char_u **cmdlinep,
1699 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001701 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001703 char_u *(*fgetline)(int, void *, int),
1704 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705{
1706 char_u *p;
1707 linenr_T lnum;
1708 long n;
1709 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001710 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 exarg_T ea; /* Ex command arguments */
1712 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001713 int save_msg_scroll = msg_scroll;
1714 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001716#ifdef HAVE_SANDBOX
1717 int did_sandbox = FALSE;
1718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 cmdmod_T save_cmdmod;
1720 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001721 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001722 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
1724 vim_memset(&ea, 0, sizeof(ea));
1725 ea.line1 = 1;
1726 ea.line2 = 1;
1727#ifdef FEAT_EVAL
1728 ++ex_nesting_level;
1729#endif
1730
Bram Moolenaar21691f82012-12-05 19:13:18 +01001731 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (quitmore
1733#ifdef FEAT_EVAL
1734 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001735 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001736#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001737 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001738 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 --quitmore;
1740
1741 /*
1742 * Reset browse, confirm, etc.. They are restored when returning, for
1743 * recursive calls.
1744 */
1745 save_cmdmod = cmdmod;
1746 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1747
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001748 /* "#!anything" is handled like a comment. */
1749 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1750 goto doend;
1751
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 /*
1753 * Repeat until no more command modifiers are found.
1754 */
1755 ea.cmd = *cmdlinep;
1756 for (;;)
1757 {
1758/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001759 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 */
1761 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1762 ++ea.cmd;
1763
1764 /* in ex mode, an empty line works like :+ */
1765 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001766 && (getline_equal(fgetline, cookie, getexmodeline)
1767 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001768 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 {
1770 ea.cmd = (char_u *)"+";
1771 ex_pressedreturn = TRUE;
1772 }
1773
1774 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001775 if (*ea.cmd == '"')
1776 goto doend;
1777 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001778 {
1779 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001784 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001786 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 switch (*p)
1788 {
1789 /* When adding an entry, also modify cmd_exists(). */
1790 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1791 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 continue;
1794
1795 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1796 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 continue;
1799 }
1800 if (checkforcmd(&ea.cmd, "browse", 3))
1801 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001802#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 cmdmod.browse = TRUE;
1804#endif
1805 continue;
1806 }
1807 if (!checkforcmd(&ea.cmd, "botright", 2))
1808 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 cmdmod.split |= WSP_BOT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 continue;
1811
1812 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1813 break;
1814#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1815 cmdmod.confirm = TRUE;
1816#endif
1817 continue;
1818
1819 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1820 {
1821 cmdmod.keepmarks = TRUE;
1822 continue;
1823 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001824 if (checkforcmd(&ea.cmd, "keepalt", 5))
1825 {
1826 cmdmod.keepalt = TRUE;
1827 continue;
1828 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001829 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1830 {
1831 cmdmod.keeppatterns = TRUE;
1832 continue;
1833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1835 break;
1836 cmdmod.keepjumps = TRUE;
1837 continue;
1838
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001839 case 'f': /* only accept ":filter {pat} cmd" */
1840 {
1841 char_u *reg_pat;
1842
1843 if (!checkforcmd(&p, "filter", 4)
1844 || *p == NUL || ends_excmd(*p))
1845 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001846 if (*p == '!')
1847 {
1848 cmdmod.filter_force = TRUE;
1849 p = skipwhite(p + 1);
1850 if (*p == NUL || ends_excmd(*p))
1851 break;
1852 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001853 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1854 if (p == NULL || *p == NUL)
1855 break;
1856 cmdmod.filter_regmatch.regprog =
1857 vim_regcomp(reg_pat, RE_MAGIC);
1858 if (cmdmod.filter_regmatch.regprog == NULL)
1859 break;
1860 ea.cmd = p;
1861 continue;
1862 }
1863
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 /* ":hide" and ":hide | cmd" are not modifiers */
1865 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1866 || *p == NUL || ends_excmd(*p))
1867 break;
1868 ea.cmd = p;
1869 cmdmod.hide = TRUE;
1870 continue;
1871
1872 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1873 {
1874 cmdmod.lockmarks = TRUE;
1875 continue;
1876 }
1877
1878 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1879 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 continue;
1882
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001883 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001884 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001885 if (cmdmod.save_ei == NULL)
1886 {
1887 /* Set 'eventignore' to "all". Restore the
1888 * existing option value later. */
1889 cmdmod.save_ei = vim_strsave(p_ei);
1890 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001891 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001892 }
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001893 continue;
1894 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001895 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001896 break;
1897 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001898 continue;
1899
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1901 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 continue;
1904
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001905 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1906 {
1907#ifdef HAVE_SANDBOX
1908 if (!did_sandbox)
1909 ++sandbox;
1910 did_sandbox = TRUE;
1911#endif
1912 continue;
1913 }
1914 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001916 if (save_msg_silent == -1)
1917 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 ++msg_silent;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001919 if (*ea.cmd == '!' && !VIM_ISWHITE(ea.cmd[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
1921 /* ":silent!", but not "silent !cmd" */
1922 ea.cmd = skipwhite(ea.cmd + 1);
1923 ++emsg_silent;
1924 ++did_esilent;
1925 }
1926 continue;
1927
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001928 case 't': if (checkforcmd(&p, "tab", 3))
1929 {
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001930 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01001931 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001932 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001933 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001934 else
1935 {
1936 if (tabnr < 0 || tabnr > LAST_TAB_NR)
1937 {
1938 errormsg = (char_u *)_(e_invrange);
1939 goto doend;
1940 }
1941 cmdmod.tab = tabnr + 1;
1942 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001943 ea.cmd = p;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001944 continue;
1945 }
1946 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 cmdmod.split |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 continue;
1950
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001951 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1952 break;
1953 if (save_msg_silent == -1)
1954 save_msg_silent = msg_silent;
1955 msg_silent = 0;
1956 continue;
1957
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1959 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 cmdmod.split |= WSP_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 continue;
1962 }
1963 if (!checkforcmd(&p, "verbose", 4))
1964 break;
1965 if (verbose_save < 0)
1966 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001967 if (vim_isdigit(*ea.cmd))
1968 p_verbose = atoi((char *)ea.cmd);
1969 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 p_verbose = 1;
1971 ea.cmd = p;
1972 continue;
1973 }
1974 break;
1975 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001976 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977
1978#ifdef FEAT_EVAL
1979 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1980 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1981#else
1982 ea.skip = (if_level > 0);
1983#endif
1984
1985#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001986# ifdef FEAT_PROFILE
1987 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001988 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001989 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001990 if (getline_equal(fgetline, cookie, get_func_line))
1991 func_line_exec(getline_cookie(fgetline, cookie));
1992 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001993 script_line_exec();
1994 }
1995#endif
1996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 /* May go to debug mode. If this happens and the ">quit" debug command is
1998 * used, throw an interrupt exception and skip the next command. */
1999 dbg_check_breakpoint(&ea);
2000 if (!ea.skip && got_int)
2001 {
2002 ea.skip = TRUE;
2003 (void)do_intthrow(cstack);
2004 }
2005#endif
2006
2007/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002008 * 3. Skip over the range to find the command. Let "p" point to after it.
2009 *
2010 * We need the command to know what kind of range it uses.
2011 */
2012 cmd = ea.cmd;
2013 ea.cmd = skip_range(ea.cmd, NULL);
2014 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2015 ea.cmd = skipwhite(ea.cmd + 1);
2016 p = find_command(&ea, NULL);
2017
2018/*
2019 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 *
2021 * where 'addr' is:
2022 *
2023 * % (entire file)
2024 * $ [+-NUM]
2025 * 'x [+-NUM] (where x denotes a currently defined mark)
2026 * . [+-NUM]
2027 * [+-NUM]..
2028 * NUM
2029 *
2030 * The ea.cmd pointer is updated to point to the first character following the
2031 * range spec. If an initial address is found, but no second, the upper bound
2032 * is equal to the lower.
2033 */
2034
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002035 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002036 if (!IS_USER_CMDIDX(ea.cmdidx))
2037 {
2038 if (ea.cmdidx != CMD_SIZE)
2039 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2040 else
2041 ea.addr_type = ADDR_LINES;
2042
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002043 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002044 if (ea.cmdidx == CMD_wincmd && p != NULL)
2045 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002046 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002049 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 for (;;)
2051 {
2052 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002053 switch (ea.addr_type)
2054 {
2055 case ADDR_LINES:
2056 /* default is current line number */
2057 ea.line2 = curwin->w_cursor.lnum;
2058 break;
2059 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002060 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002061 break;
2062 case ADDR_ARGUMENTS:
2063 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002064 if (ea.line2 > ARGCOUNT)
2065 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002066 break;
2067 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002068 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002069 ea.line2 = curbuf->b_fnum;
2070 break;
2071 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002072 ea.line2 = CURRENT_TAB_NR;
2073 break;
2074 case ADDR_TABS_RELATIVE:
2075 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002076 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002077#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002078 case ADDR_QUICKFIX:
2079 ea.line2 = qf_get_cur_valid_idx(&ea);
2080 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002081#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002084 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002085 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 if (ea.cmd == NULL) /* error detected */
2087 goto doend;
2088 if (lnum == MAXLNUM)
2089 {
2090 if (*ea.cmd == '%') /* '%' - all lines */
2091 {
2092 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002093 switch (ea.addr_type)
2094 {
2095 case ADDR_LINES:
2096 ea.line1 = 1;
2097 ea.line2 = curbuf->b_ml.ml_line_count;
2098 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002099 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002100 {
2101 buf_T *buf = firstbuf;
2102
2103 while (buf->b_next != NULL
2104 && buf->b_ml.ml_mfp == NULL)
2105 buf = buf->b_next;
2106 ea.line1 = buf->b_fnum;
2107 buf = lastbuf;
2108 while (buf->b_prev != NULL
2109 && buf->b_ml.ml_mfp == NULL)
2110 buf = buf->b_prev;
2111 ea.line2 = buf->b_fnum;
2112 break;
2113 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002114 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002115 ea.line1 = firstbuf->b_fnum;
2116 ea.line2 = lastbuf->b_fnum;
2117 break;
2118 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002119 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002120 if (IS_USER_CMDIDX(ea.cmdidx))
2121 {
2122 ea.line1 = 1;
2123 ea.line2 = ea.addr_type == ADDR_WINDOWS
2124 ? LAST_WIN_NR : LAST_TAB_NR;
2125 }
2126 else
2127 {
2128 /* there is no Vim command which uses '%' and
2129 * ADDR_WINDOWS or ADDR_TABS */
2130 errormsg = (char_u *)_(e_invrange);
2131 goto doend;
2132 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002133 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002134 case ADDR_TABS_RELATIVE:
2135 errormsg = (char_u *)_(e_invrange);
2136 goto doend;
2137 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002138 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002139 if (ARGCOUNT == 0)
2140 ea.line1 = ea.line2 = 0;
2141 else
2142 {
2143 ea.line1 = 1;
2144 ea.line2 = ARGCOUNT;
2145 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002146 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002147#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002148 case ADDR_QUICKFIX:
2149 ea.line1 = 1;
2150 ea.line2 = qf_get_size(&ea);
2151 if (ea.line2 == 0)
2152 ea.line2 = 1;
2153 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002154#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 ++ea.addr_count;
2157 }
2158 /* '*' - visual area */
2159 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2160 {
2161 pos_T *fp;
2162
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002163 if (ea.addr_type != ADDR_LINES)
2164 {
2165 errormsg = (char_u *)_(e_invrange);
2166 goto doend;
2167 }
2168
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 ++ea.cmd;
2170 if (!ea.skip)
2171 {
2172 fp = getmark('<', FALSE);
2173 if (check_mark(fp) == FAIL)
2174 goto doend;
2175 ea.line1 = fp->lnum;
2176 fp = getmark('>', FALSE);
2177 if (check_mark(fp) == FAIL)
2178 goto doend;
2179 ea.line2 = fp->lnum;
2180 ++ea.addr_count;
2181 }
2182 }
2183 }
2184 else
2185 ea.line2 = lnum;
2186 ea.addr_count++;
2187
2188 if (*ea.cmd == ';')
2189 {
2190 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002191 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +01002193 /* don't leave the cursor on an illegal line or column */
2194 check_cursor();
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 }
2197 else if (*ea.cmd != ',')
2198 break;
2199 ++ea.cmd;
2200 }
2201
2202 /* One address given: set start and end lines */
2203 if (ea.addr_count == 1)
2204 {
2205 ea.line1 = ea.line2;
2206 /* ... but only implicit: really no address given */
2207 if (lnum == MAXLNUM)
2208 ea.addr_count = 0;
2209 }
2210
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002212 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 */
2214
2215 /*
2216 * Skip ':' and any white space
2217 */
2218 ea.cmd = skipwhite(ea.cmd);
2219 while (*ea.cmd == ':')
2220 ea.cmd = skipwhite(ea.cmd + 1);
2221
2222 /*
2223 * If we got a line, but no command, then go to the line.
2224 * If we find a '|' or '\n' we set ea.nextcmd.
2225 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002226 if (*ea.cmd == NUL || *ea.cmd == '"'
2227 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 {
2229 /*
2230 * strange vi behaviour:
2231 * ":3" jumps to line 3
2232 * ":3|..." prints line 3
2233 * ":|" prints current line
2234 */
2235 if (ea.skip) /* skip this if inside :if */
2236 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002237 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
2239 ea.cmdidx = CMD_print;
2240 ea.argt = RANGE+COUNT+TRLBAR;
2241 if ((errormsg = invalid_range(&ea)) == NULL)
2242 {
2243 correct_range(&ea);
2244 ex_print(&ea);
2245 }
2246 }
2247 else if (ea.addr_count != 0)
2248 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002249 if (ea.line2 > curbuf->b_ml.ml_line_count)
2250 {
2251 /* With '-' in 'cpoptions' a line number past the file is an
2252 * error, otherwise put it at the end of the file. */
2253 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2254 ea.line2 = -1;
2255 else
2256 ea.line2 = curbuf->b_ml.ml_line_count;
2257 }
2258
2259 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002260 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 else
2262 {
2263 if (ea.line2 == 0)
2264 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 else
2266 curwin->w_cursor.lnum = ea.line2;
2267 beginline(BL_SOL | BL_FIX);
2268 }
2269 }
2270 goto doend;
2271 }
2272
Bram Moolenaard5005162014-08-22 23:05:54 +02002273 /* If this looks like an undefined user command and there are CmdUndefined
2274 * autocommands defined, trigger the matching autocommands. */
2275 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2276 && ASCII_ISUPPER(*ea.cmd)
2277 && has_cmdundefined())
2278 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002279 int ret;
2280
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002281 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002282 while (ASCII_ISALNUM(*p))
2283 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002284 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002285 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2286 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002287 /* If the autocommands did something and didn't cause an error, try
2288 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002289 p = (ret
2290#ifdef FEAT_EVAL
2291 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002292#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002293 ) ? find_command(&ea, NULL) : ea.cmd;
2294 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002295
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296#ifdef FEAT_USR_CMDS
2297 if (p == NULL)
2298 {
2299 if (!ea.skip)
2300 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2301 goto doend;
2302 }
2303 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002304 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2305 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
2307 errormsg = uc_fun_cmd();
2308 goto doend;
2309 }
2310#endif
2311 if (ea.cmdidx == CMD_SIZE)
2312 {
2313 if (!ea.skip)
2314 {
2315 STRCPY(IObuff, _("E492: Not an editor command"));
2316 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002317 {
2318 /* If the modifier was parsed OK the error must be in the
2319 * following command */
2320 if (after_modifier != NULL)
2321 append_command(after_modifier);
2322 else
2323 append_command(*cmdlinep);
2324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002326 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 }
2328 goto doend;
2329 }
2330
Bram Moolenaar958636c2014-10-21 20:01:58 +02002331 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2332 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002333#ifdef HAVE_EX_SCRIPT_NI
2334 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2335#endif
2336 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338#ifndef FEAT_EVAL
2339 /*
2340 * When the expression evaluation is disabled, recognize the ":if" and
2341 * ":endif" commands and ignore everything in between it.
2342 */
2343 if (ea.cmdidx == CMD_if)
2344 ++if_level;
2345 if (if_level)
2346 {
2347 if (ea.cmdidx == CMD_endif)
2348 --if_level;
2349 goto doend;
2350 }
2351
2352#endif
2353
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002354 /* forced commands */
2355 if (*p == '!' && ea.cmdidx != CMD_substitute
2356 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
2358 ++p;
2359 ea.forceit = TRUE;
2360 }
2361 else
2362 ea.forceit = FALSE;
2363
2364/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002365 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002367 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002368 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 if (!ea.skip)
2371 {
2372#ifdef HAVE_SANDBOX
2373 if (sandbox != 0 && !(ea.argt & SBOXOK))
2374 {
2375 /* Command not allowed in sandbox. */
2376 errormsg = (char_u *)_(e_sandbox);
2377 goto doend;
2378 }
2379#endif
2380 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2381 {
2382 /* Command not allowed in non-'modifiable' buffer */
2383 errormsg = (char_u *)_(e_modifiable);
2384 goto doend;
2385 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002386
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002387 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002388 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002390 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002391 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 goto doend;
2393 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002394 /* Disallow editing another buffer when "curbuf_lock" is set.
2395 * Do allow ":edit" (check for argument later).
2396 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002397 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002398 && ea.cmdidx != CMD_edit
2399 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002400 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002401 && curbuf_locked())
2402 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
2404 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2405 {
2406 /* no range allowed */
2407 errormsg = (char_u *)_(e_norange);
2408 goto doend;
2409 }
2410 }
2411
2412 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2413 {
2414 errormsg = (char_u *)_(e_nobang);
2415 goto doend;
2416 }
2417
2418 /*
2419 * Don't complain about the range if it is not used
2420 * (could happen if line_count is accidentally set to 0).
2421 */
2422 if (!ea.skip && !ni)
2423 {
2424 /*
2425 * If the range is backwards, ask for confirmation and, if given, swap
2426 * ea.line1 & ea.line2 so it's forwards again.
2427 * When global command is busy, don't ask, will fail below.
2428 */
2429 if (!global_busy && ea.line1 > ea.line2)
2430 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002431 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002433 if (sourcing || exmode_active)
2434 {
2435 errormsg = (char_u *)_("E493: Backwards range given");
2436 goto doend;
2437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 if (ask_yesno((char_u *)
2439 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002440 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 }
2442 lnum = ea.line1;
2443 ea.line1 = ea.line2;
2444 ea.line2 = lnum;
2445 }
2446 if ((errormsg = invalid_range(&ea)) != NULL)
2447 goto doend;
2448 }
2449
2450 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2451 ea.line2 = 1;
2452
2453 correct_range(&ea);
2454
2455#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002456 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2457 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 {
2459 /* Put the first line at the start of a closed fold, put the last line
2460 * at the end of a closed fold. */
2461 (void)hasFolding(ea.line1, &ea.line1, NULL);
2462 (void)hasFolding(ea.line2, NULL, &ea.line2);
2463 }
2464#endif
2465
2466#ifdef FEAT_QUICKFIX
2467 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002468 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 * option here, so things like % get expanded.
2470 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002471 p = replace_makeprg(&ea, p, cmdlinep);
2472 if (p == NULL)
2473 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474#endif
2475
2476 /*
2477 * Skip to start of argument.
2478 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2479 */
2480 if (ea.cmdidx == CMD_bang)
2481 ea.arg = p;
2482 else
2483 ea.arg = skipwhite(p);
2484
2485 /*
2486 * Check for "++opt=val" argument.
2487 * Must be first, allow ":w ++enc=utf8 !cmd"
2488 */
2489 if (ea.argt & ARGOPT)
2490 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2491 if (getargopt(&ea) == FAIL && !ni)
2492 {
2493 errormsg = (char_u *)_(e_invarg);
2494 goto doend;
2495 }
2496
2497 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2498 {
2499 if (*ea.arg == '>') /* append */
2500 {
2501 if (*++ea.arg != '>') /* typed wrong */
2502 {
2503 errormsg = (char_u *)_("E494: Use w or w>>");
2504 goto doend;
2505 }
2506 ea.arg = skipwhite(ea.arg + 1);
2507 ea.append = TRUE;
2508 }
2509 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2510 {
2511 ++ea.arg;
2512 ea.usefilter = TRUE;
2513 }
2514 }
2515
2516 if (ea.cmdidx == CMD_read)
2517 {
2518 if (ea.forceit)
2519 {
2520 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2521 ea.forceit = FALSE;
2522 }
2523 else if (*ea.arg == '!') /* :r !filter */
2524 {
2525 ++ea.arg;
2526 ea.usefilter = TRUE;
2527 }
2528 }
2529
2530 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2531 {
2532 ea.amount = 1;
2533 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2534 {
2535 ++ea.arg;
2536 ++ea.amount;
2537 }
2538 ea.arg = skipwhite(ea.arg);
2539 }
2540
2541 /*
2542 * Check for "+command" argument, before checking for next command.
2543 * Don't do this for ":read !cmd" and ":write !cmd".
2544 */
2545 if ((ea.argt & EDITCMD) && !ea.usefilter)
2546 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2547
2548 /*
2549 * Check for '|' to separate commands and '"' to start comments.
2550 * Don't do this for ":read !cmd" and ":write !cmd".
2551 */
2552 if ((ea.argt & TRLBAR) && !ea.usefilter)
2553 separate_nextcmd(&ea);
2554
2555 /*
2556 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002557 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2558 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002560 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002561 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002562 || ea.cmdidx == CMD_global
2563 || ea.cmdidx == CMD_vglobal
2564 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
2566 for (p = ea.arg; *p; ++p)
2567 {
2568 /* Remove one backslash before a newline, so that it's possible to
2569 * pass a newline to the shell and also a newline that is preceded
2570 * with a backslash. This makes it impossible to end a shell
2571 * command in a backslash, but that doesn't appear useful.
2572 * Halving the number of backslashes is incompatible with previous
2573 * versions. */
2574 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002575 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 else if (*p == '\n')
2577 {
2578 ea.nextcmd = p + 1;
2579 *p = NUL;
2580 break;
2581 }
2582 }
2583 }
2584
2585 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2586 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002587 buf_T *buf;
2588
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002590 switch (ea.addr_type)
2591 {
2592 case ADDR_LINES:
2593 ea.line2 = curbuf->b_ml.ml_line_count;
2594 break;
2595 case ADDR_LOADED_BUFFERS:
2596 buf = firstbuf;
2597 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2598 buf = buf->b_next;
2599 ea.line1 = buf->b_fnum;
2600 buf = lastbuf;
2601 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2602 buf = buf->b_prev;
2603 ea.line2 = buf->b_fnum;
2604 break;
2605 case ADDR_BUFFERS:
2606 ea.line1 = firstbuf->b_fnum;
2607 ea.line2 = lastbuf->b_fnum;
2608 break;
2609 case ADDR_WINDOWS:
2610 ea.line2 = LAST_WIN_NR;
2611 break;
2612 case ADDR_TABS:
2613 ea.line2 = LAST_TAB_NR;
2614 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002615 case ADDR_TABS_RELATIVE:
2616 ea.line2 = 1;
2617 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002618 case ADDR_ARGUMENTS:
2619 if (ARGCOUNT == 0)
2620 ea.line1 = ea.line2 = 0;
2621 else
2622 ea.line2 = ARGCOUNT;
2623 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002624#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002625 case ADDR_QUICKFIX:
2626 ea.line2 = qf_get_size(&ea);
2627 if (ea.line2 == 0)
2628 ea.line2 = 1;
2629 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002630#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 }
2633
2634 /* accept numbered register only when no count allowed (:put) */
2635 if ( (ea.argt & REGSTR)
2636 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002637 /* Do not allow register = for user commands */
2638 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2640 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002641#ifndef FEAT_CLIPBOARD
2642 /* check these explicitly for a more specific error message */
2643 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002645 errormsg = (char_u *)_(e_invalidreg);
2646 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 }
2648#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002649 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2650 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002651 {
2652 ea.regname = *ea.arg++;
2653#ifdef FEAT_EVAL
2654 /* for '=' register: accept the rest of the line as an expression */
2655 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2656 {
2657 set_expr_line(vim_strsave(ea.arg));
2658 ea.arg += STRLEN(ea.arg);
2659 }
2660#endif
2661 ea.arg = skipwhite(ea.arg);
2662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 }
2664
2665 /*
2666 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2667 * count, it's a buffer name.
2668 */
2669 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2670 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002671 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
2673 n = getdigits(&ea.arg);
2674 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002675 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
2677 errormsg = (char_u *)_(e_zerocount);
2678 goto doend;
2679 }
2680 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2681 {
2682 ea.line2 = n;
2683 if (ea.addr_count == 0)
2684 ea.addr_count = 1;
2685 }
2686 else
2687 {
2688 ea.line1 = ea.line2;
2689 ea.line2 += n - 1;
2690 ++ea.addr_count;
2691 /*
2692 * Be vi compatible: no error message for out of range.
2693 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002694 if (ea.addr_type == ADDR_LINES
2695 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 ea.line2 = curbuf->b_ml.ml_line_count;
2697 }
2698 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002699
2700 /*
2701 * Check for flags: 'l', 'p' and '#'.
2702 */
2703 if (ea.argt & EXFLAGS)
2704 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002706 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002707 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
2709 errormsg = (char_u *)_(e_trailing);
2710 goto doend;
2711 }
2712
2713 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2714 {
2715 errormsg = (char_u *)_(e_argreq);
2716 goto doend;
2717 }
2718
2719#ifdef FEAT_EVAL
2720 /*
2721 * Skip the command when it's not going to be executed.
2722 * The commands like :if, :endif, etc. always need to be executed.
2723 * Also make an exception for commands that handle a trailing command
2724 * themselves.
2725 */
2726 if (ea.skip)
2727 {
2728 switch (ea.cmdidx)
2729 {
2730 /* commands that need evaluation */
2731 case CMD_while:
2732 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002733 case CMD_for:
2734 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 case CMD_if:
2736 case CMD_elseif:
2737 case CMD_else:
2738 case CMD_endif:
2739 case CMD_try:
2740 case CMD_catch:
2741 case CMD_finally:
2742 case CMD_endtry:
2743 case CMD_function:
2744 break;
2745
2746 /* Commands that handle '|' themselves. Check: A command should
2747 * either have the TRLBAR flag, appear in this list or appear in
2748 * the list at ":help :bar". */
2749 case CMD_aboveleft:
2750 case CMD_and:
2751 case CMD_belowright:
2752 case CMD_botright:
2753 case CMD_browse:
2754 case CMD_call:
2755 case CMD_confirm:
2756 case CMD_delfunction:
2757 case CMD_djump:
2758 case CMD_dlist:
2759 case CMD_dsearch:
2760 case CMD_dsplit:
2761 case CMD_echo:
2762 case CMD_echoerr:
2763 case CMD_echomsg:
2764 case CMD_echon:
2765 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002766 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 case CMD_help:
2768 case CMD_hide:
2769 case CMD_ijump:
2770 case CMD_ilist:
2771 case CMD_isearch:
2772 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002773 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 case CMD_keepjumps:
2775 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002776 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 case CMD_leftabove:
2778 case CMD_let:
2779 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002780 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002782 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002783 case CMD_noautocmd:
2784 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 case CMD_perl:
2786 case CMD_psearch:
2787 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002788 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002789 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 case CMD_return:
2791 case CMD_rightbelow:
2792 case CMD_ruby:
2793 case CMD_silent:
2794 case CMD_smagic:
2795 case CMD_snomagic:
2796 case CMD_substitute:
2797 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002798 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 case CMD_tcl:
2800 case CMD_throw:
2801 case CMD_tilde:
2802 case CMD_topleft:
2803 case CMD_unlet:
2804 case CMD_verbose:
2805 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002806 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 break;
2808
2809 default: goto doend;
2810 }
2811 }
2812#endif
2813
2814 if (ea.argt & XFILE)
2815 {
2816 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2817 goto doend;
2818 }
2819
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 /*
2821 * Accept buffer name. Cannot be used at the same time with a buffer
2822 * number. Don't do this for a user command.
2823 */
2824 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002825 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 {
2827 /*
2828 * :bdelete, :bwipeout and :bunload take several arguments, separated
2829 * by spaces: find next space (skipping over escaped characters).
2830 * The others take one argument: ignore trailing spaces.
2831 */
2832 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2833 || ea.cmdidx == CMD_bunload)
2834 p = skiptowhite_esc(ea.arg);
2835 else
2836 {
2837 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002838 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 --p;
2840 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002841 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2842 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 if (ea.line2 < 0) /* failed */
2844 goto doend;
2845 ea.addr_count = 1;
2846 ea.arg = skipwhite(p);
2847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848
Bram Moolenaar2be57332018-02-13 18:05:18 +01002849 /* The :try command saves the emsg_silent flag, reset it here when
2850 * ":silent! try" was used, it should only apply to :try itself. */
2851 if (ea.cmdidx == CMD_try && did_esilent > 0)
2852 {
2853 emsg_silent -= did_esilent;
2854 if (emsg_silent < 0)
2855 emsg_silent = 0;
2856 did_esilent = 0;
2857 }
2858
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002860 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 *
2862 * The "ea" structure holds the arguments that can be used.
2863 */
2864 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002865 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 ea.cookie = cookie;
2867#ifdef FEAT_EVAL
2868 ea.cstack = cstack;
2869#endif
2870
2871#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002872 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 {
2874 /*
2875 * Execute a user-defined command.
2876 */
2877 do_ucmd(&ea);
2878 }
2879 else
2880#endif
2881 {
2882 /*
2883 * Call the function to execute the command.
2884 */
2885 ea.errmsg = NULL;
2886 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2887 if (ea.errmsg != NULL)
2888 errormsg = (char_u *)_(ea.errmsg);
2889 }
2890
2891#ifdef FEAT_EVAL
2892 /*
2893 * If the command just executed called do_cmdline(), any throw or ":return"
2894 * or ":finish" encountered there must also check the cstack of the still
2895 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2896 * exception, or reanimate a returned function or finished script file and
2897 * return or finish it again.
2898 */
2899 if (need_rethrow)
2900 do_throw(cstack);
2901 else if (check_cstack)
2902 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002903 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002905 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 && current_func_returned())
2907 do_return(&ea, TRUE, FALSE, NULL);
2908 }
2909 need_rethrow = check_cstack = FALSE;
2910#endif
2911
2912doend:
2913 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002914 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002916 curwin->w_cursor.col = 0;
2917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918
2919 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2920 {
2921 if (sourcing)
2922 {
2923 if (errormsg != IObuff)
2924 {
2925 STRCPY(IObuff, errormsg);
2926 errormsg = IObuff;
2927 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002928 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 }
2930 emsg(errormsg);
2931 }
2932#ifdef FEAT_EVAL
2933 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002934 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2935 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936#endif
2937
2938 if (verbose_save >= 0)
2939 p_verbose = verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002940
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002941 if (cmdmod.save_ei != NULL)
2942 {
2943 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002944 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2945 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002946 free_string_option(cmdmod.save_ei);
2947 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002948
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002949 if (cmdmod.filter_regmatch.regprog != NULL)
2950 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951
2952 cmdmod = save_cmdmod;
2953
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002954 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 {
2956 /* messages could be enabled for a serious error, need to check if the
2957 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002958 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002959 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 emsg_silent -= did_esilent;
2961 if (emsg_silent < 0)
2962 emsg_silent = 0;
2963 /* Restore msg_scroll, it's set by file I/O commands, even when no
2964 * message is actually displayed. */
2965 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002966
2967 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2968 * somewhere in the line. Put it back in the first column. */
2969 if (redirecting())
2970 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 }
2972
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002973#ifdef HAVE_SANDBOX
2974 if (did_sandbox)
2975 --sandbox;
2976#endif
2977
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2979 ea.nextcmd = NULL;
2980
2981#ifdef FEAT_EVAL
2982 --ex_nesting_level;
2983#endif
2984
2985 return ea.nextcmd;
2986}
2987#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002988 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989#endif
2990
2991/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002992 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 * If there is a match advance "pp" to the argument and return TRUE.
2994 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002995 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002996checkforcmd(
2997 char_u **pp, /* start of command */
2998 char *cmd, /* name of command */
2999 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
3001 int i;
3002
3003 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003004 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 break;
3006 if (i >= len && !isalpha((*pp)[i]))
3007 {
3008 *pp = skipwhite(*pp + i);
3009 return TRUE;
3010 }
3011 return FALSE;
3012}
3013
3014/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003015 * Append "cmd" to the error message in IObuff.
3016 * Takes care of limiting the length and handling 0xa0, which would be
3017 * invisible otherwise.
3018 */
3019 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003020append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003021{
3022 char_u *s = cmd;
3023 char_u *d;
3024
3025 STRCAT(IObuff, ": ");
3026 d = IObuff + STRLEN(IObuff);
3027 while (*s != NUL && d - IObuff < IOSIZE - 7)
3028 {
3029 if (
3030#ifdef FEAT_MBYTE
3031 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3032#endif
3033 *s == 0xa0)
3034 {
3035 s +=
3036#ifdef FEAT_MBYTE
3037 enc_utf8 ? 2 :
3038#endif
3039 1;
3040 STRCPY(d, "<a0>");
3041 d += 4;
3042 }
3043 else
3044 MB_COPY_CHAR(s, d);
3045 }
3046 *d = NUL;
3047}
3048
3049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003051 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003053 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 * Returns NULL for an ambiguous user command.
3055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003057find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
3059 int len;
3060 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003061 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
3063 /*
3064 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003065 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 * - the 'k' command can directly be followed by any character.
3067 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003068 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003070 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 */
3072 p = eap->cmd;
3073 if (*p == 'k')
3074 {
3075 eap->cmdidx = CMD_k;
3076 ++p;
3077 }
3078 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003079 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3080 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 || p[1] == 'g'
3082 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3083 || p[1] == 'I'
3084 || (p[1] == 'r' && p[2] != 'e')))
3085 {
3086 eap->cmdidx = CMD_substitute;
3087 ++p;
3088 }
3089 else
3090 {
3091 while (ASCII_ISALPHA(*p))
3092 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003093 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003094 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003095 while (ASCII_ISALNUM(*p))
3096 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003097
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 /* check for non-alpha command */
3099 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3100 ++p;
3101 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003102 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3103 {
3104 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3105 * :delete with the 'l' flag. Same for 'p'. */
3106 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003107 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003108 break;
3109 if (i == len - 1)
3110 {
3111 --len;
3112 if (p[-1] == 'l')
3113 eap->flags |= EXFLAG_LIST;
3114 else
3115 eap->flags |= EXFLAG_PRINT;
3116 }
3117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003119 if (ASCII_ISLOWER(eap->cmd[0]))
3120 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003121 int c1 = eap->cmd[0];
3122 int c2 = eap->cmd[1];
3123
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003124 if (command_count != (int)CMD_SIZE)
3125 {
3126 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3127 getout(1);
3128 }
3129
3130 /* Use a precomputed index for fast look-up in cmdnames[]
3131 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003132 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3133 if (ASCII_ISLOWER(c2))
3134 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003137 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
3139 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3140 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3141 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3142 (size_t)len) == 0)
3143 {
3144#ifdef FEAT_EVAL
3145 if (full != NULL
3146 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3147 *full = TRUE;
3148#endif
3149 break;
3150 }
3151
3152#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003153 /* Look for a user defined command as a last resort. Let ":Print" be
3154 * overruled by a user defined command. */
3155 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3156 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003158 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 while (ASCII_ISALNUM(*p))
3160 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003161 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 }
3163#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003164 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 eap->cmdidx = CMD_SIZE;
3166 }
3167
3168 return p;
3169}
3170
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003171#ifdef FEAT_USR_CMDS
3172/*
3173 * Search for a user command that matches "eap->cmd".
3174 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3175 * Return a pointer to just after the command.
3176 * Return NULL if there is no matching command.
3177 */
3178 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003179find_ucmd(
3180 exarg_T *eap,
3181 char_u *p, /* end of the command (possibly including count) */
3182 int *full, /* set to TRUE for a full match */
3183 expand_T *xp, /* used for completion, NULL otherwise */
3184 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003185{
3186 int len = (int)(p - eap->cmd);
3187 int j, k, matchlen = 0;
3188 ucmd_T *uc;
3189 int found = FALSE;
3190 int possible = FALSE;
3191 char_u *cp, *np; /* Point into typed cmd and test name */
3192 garray_T *gap;
3193 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3194 only full match global is accepted. */
3195
3196 /*
3197 * Look for buffer-local user commands first, then global ones.
3198 */
3199 gap = &curbuf->b_ucmds;
3200 for (;;)
3201 {
3202 for (j = 0; j < gap->ga_len; ++j)
3203 {
3204 uc = USER_CMD_GA(gap, j);
3205 cp = eap->cmd;
3206 np = uc->uc_name;
3207 k = 0;
3208 while (k < len && *np != NUL && *cp++ == *np++)
3209 k++;
3210 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3211 {
3212 /* If finding a second match, the command is ambiguous. But
3213 * not if a buffer-local command wasn't a full match and a
3214 * global command is a full match. */
3215 if (k == len && found && *np != NUL)
3216 {
3217 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003218 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003219 amb_local = TRUE;
3220 }
3221
3222 if (!found || (k == len && *np == NUL))
3223 {
3224 /* If we matched up to a digit, then there could
3225 * be another command including the digit that we
3226 * should use instead.
3227 */
3228 if (k == len)
3229 found = TRUE;
3230 else
3231 possible = TRUE;
3232
3233 if (gap == &ucmds)
3234 eap->cmdidx = CMD_USER;
3235 else
3236 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003237 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003238 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003239 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003240
3241# ifdef FEAT_CMDL_COMPL
3242 if (compl != NULL)
3243 *compl = uc->uc_compl;
3244# ifdef FEAT_EVAL
3245 if (xp != NULL)
3246 {
3247 xp->xp_arg = uc->uc_compl_arg;
3248 xp->xp_scriptID = uc->uc_scriptID;
3249 }
3250# endif
3251# endif
3252 /* Do not search for further abbreviations
3253 * if this is an exact match. */
3254 matchlen = k;
3255 if (k == len && *np == NUL)
3256 {
3257 if (full != NULL)
3258 *full = TRUE;
3259 amb_local = FALSE;
3260 break;
3261 }
3262 }
3263 }
3264 }
3265
3266 /* Stop if we found a full match or searched all. */
3267 if (j < gap->ga_len || gap == &ucmds)
3268 break;
3269 gap = &ucmds;
3270 }
3271
3272 /* Only found ambiguous matches. */
3273 if (amb_local)
3274 {
3275 if (xp != NULL)
3276 xp->xp_context = EXPAND_UNSUCCESSFUL;
3277 return NULL;
3278 }
3279
3280 /* The match we found may be followed immediately by a number. Move "p"
3281 * back to point to it. */
3282 if (found || possible)
3283 return p + (matchlen - len);
3284 return p;
3285}
3286#endif
3287
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003289static struct cmdmod
3290{
3291 char *name;
3292 int minlen;
3293 int has_count; /* :123verbose :3tab */
3294} cmdmods[] = {
3295 {"aboveleft", 3, FALSE},
3296 {"belowright", 3, FALSE},
3297 {"botright", 2, FALSE},
3298 {"browse", 3, FALSE},
3299 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003300 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003301 {"hide", 3, FALSE},
3302 {"keepalt", 5, FALSE},
3303 {"keepjumps", 5, FALSE},
3304 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003305 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003306 {"leftabove", 5, FALSE},
3307 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003308 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003309 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003310 {"rightbelow", 6, FALSE},
3311 {"sandbox", 3, FALSE},
3312 {"silent", 3, FALSE},
3313 {"tab", 3, TRUE},
3314 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003315 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003316 {"verbose", 4, TRUE},
3317 {"vertical", 4, FALSE},
3318};
3319
3320/*
3321 * Return length of a command modifier (including optional count).
3322 * Return zero when it's not a modifier.
3323 */
3324 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003325modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003326{
3327 int i, j;
3328 char_u *p = cmd;
3329
3330 if (VIM_ISDIGIT(*cmd))
3331 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003332 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003333 {
3334 for (j = 0; p[j] != NUL; ++j)
3335 if (p[j] != cmdmods[i].name[j])
3336 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003337 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003338 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003339 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003340 }
3341 return 0;
3342}
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344/*
3345 * Return > 0 if an Ex command "name" exists.
3346 * Return 2 if there is an exact match.
3347 * Return 3 if there is an ambiguous match.
3348 */
3349 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003350cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
3352 exarg_T ea;
3353 int full = FALSE;
3354 int i;
3355 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003356 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
3358 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003359 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
3361 for (j = 0; name[j] != NUL; ++j)
3362 if (name[j] != cmdmods[i].name[j])
3363 break;
3364 if (name[j] == NUL && j >= cmdmods[i].minlen)
3365 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3366 }
3367
Bram Moolenaara9587612006-05-04 21:47:50 +00003368 /* Check built-in commands and user defined commands.
3369 * For ":2match" and ":3match" we need to skip the number. */
3370 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003372 p = find_command(&ea, &full);
3373 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003375 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3376 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003377 if (*skipwhite(p) != NUL)
3378 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3380}
3381#endif
3382
3383/*
3384 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3385 * we don't need/want deleted. Maybe this could be done better if we didn't
3386 * repeat all this stuff. The only problem is that they may not stay
3387 * perfectly compatible with each other, but then the command line syntax
3388 * probably won't change that much -- webb.
3389 */
3390 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003391set_one_cmd_context(
3392 expand_T *xp,
3393 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
3395 char_u *p;
3396 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003397 int len = 0;
3398 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3400 int compl = EXPAND_NOTHING;
3401#endif
3402#ifdef FEAT_CMDL_COMPL
3403 int delim;
3404#endif
3405 int forceit = FALSE;
3406 int usefilter = FALSE; /* filter instead of file name */
3407
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003408 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 xp->xp_pattern = buff;
3410 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003411 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
3413/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003414 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 */
3416 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3417 ;
3418 xp->xp_pattern = cmd;
3419
3420 if (*cmd == NUL)
3421 return NULL;
3422 if (*cmd == '"') /* ignore comment lines */
3423 {
3424 xp->xp_context = EXPAND_NOTHING;
3425 return NULL;
3426 }
3427
3428/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003429 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 */
3431 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 xp->xp_pattern = cmd;
3433 if (*cmd == NUL)
3434 return NULL;
3435 if (*cmd == '"')
3436 {
3437 xp->xp_context = EXPAND_NOTHING;
3438 return NULL;
3439 }
3440
3441 if (*cmd == '|' || *cmd == '\n')
3442 return cmd + 1; /* There's another command */
3443
3444 /*
3445 * Isolate the command and search for it in the command table.
3446 * Exceptions:
3447 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003448 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3450 */
3451 if (*cmd == 'k' && cmd[1] != 'e')
3452 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003453 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 p = cmd + 1;
3455 }
3456 else
3457 {
3458 p = cmd;
3459 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3460 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003461 /* a user command may contain digits */
3462 if (ASCII_ISUPPER(cmd[0]))
3463 while (ASCII_ISALNUM(*p) || *p == '*')
3464 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003465 /* for python 3.x: ":py3*" commands completion */
3466 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003467 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003468 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003469 while (ASCII_ISALPHA(*p) || *p == '*')
3470 ++p;
3471 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003472 /* check for non-alpha command */
3473 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3474 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003475 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003477 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 {
3479 xp->xp_context = EXPAND_UNSUCCESSFUL;
3480 return NULL;
3481 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003482 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003483 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3484 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3485 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 break;
3487
3488#ifdef FEAT_USR_CMDS
3489 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3491 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492#endif
3493 }
3494
3495 /*
3496 * If the cursor is touching the command, and it ends in an alpha-numeric
3497 * character, complete the command name.
3498 */
3499 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3500 return NULL;
3501
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003502 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
3504 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3505 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003506 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 p = cmd + 1;
3508 }
3509#ifdef FEAT_USR_CMDS
3510 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3511 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003512 ea.cmd = cmd;
3513 p = find_ucmd(&ea, p, NULL, xp,
3514# if defined(FEAT_CMDL_COMPL)
3515 &compl
3516# else
3517 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003519 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003520 if (p == NULL)
3521 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523#endif
3524 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003525 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
3527 /* Not still touching the command and it was an illegal one */
3528 xp->xp_context = EXPAND_UNSUCCESSFUL;
3529 return NULL;
3530 }
3531
3532 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3533
3534 if (*p == '!') /* forced commands */
3535 {
3536 forceit = TRUE;
3537 ++p;
3538 }
3539
3540/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003541 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003543 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003544 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
3546 arg = skipwhite(p);
3547
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003548 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
3550 if (*arg == '>') /* append */
3551 {
3552 if (*++arg == '>')
3553 ++arg;
3554 arg = skipwhite(arg);
3555 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003556 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
3558 ++arg;
3559 usefilter = TRUE;
3560 }
3561 }
3562
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003563 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
3565 usefilter = forceit; /* :r! filter if forced */
3566 if (*arg == '!') /* :r !filter */
3567 {
3568 ++arg;
3569 usefilter = TRUE;
3570 }
3571 }
3572
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003573 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 {
3575 while (*arg == *cmd) /* allow any number of '>' or '<' */
3576 ++arg;
3577 arg = skipwhite(arg);
3578 }
3579
3580 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003581 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
3583 /* Check if we're in the +command */
3584 p = arg + 1;
3585 arg = skip_cmd_arg(arg, FALSE);
3586
3587 /* Still touching the command after '+'? */
3588 if (*arg == NUL)
3589 return p;
3590
3591 /* Skip space(s) after +command to get to the real argument */
3592 arg = skipwhite(arg);
3593 }
3594
3595 /*
3596 * Check for '|' to separate commands and '"' to start comments.
3597 * Don't do this for ":read !cmd" and ":write !cmd".
3598 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003599 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
3601 p = arg;
3602 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003603 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 p += 2;
3605 while (*p)
3606 {
3607 if (*p == Ctrl_V)
3608 {
3609 if (p[1] != NUL)
3610 ++p;
3611 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003612 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 || *p == '|' || *p == '\n')
3614 {
3615 if (*(p - 1) != '\\')
3616 {
3617 if (*p == '|' || *p == '\n')
3618 return p + 1;
3619 return NULL; /* It's a comment */
3620 }
3621 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003622 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624 }
3625
3626 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003627 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 vim_strchr((char_u *)"|\"", *arg) == NULL)
3629 return NULL;
3630
3631 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003632 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003634 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003635 while (*p && p < buff + len)
3636 {
3637 if (*p == ' ' || *p == TAB)
3638 {
3639 /* argument starts after a space */
3640 xp->xp_pattern = ++p;
3641 }
3642 else
3643 {
3644 if (*p == '\\' && *(p + 1) != NUL)
3645 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003646 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003647 }
3648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003650 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003652 int c;
3653 int in_quote = FALSE;
3654 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
3656 /*
3657 * Allow spaces within back-quotes to count as part of the argument
3658 * being expanded.
3659 */
3660 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003661 p = xp->xp_pattern;
3662 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003664#ifdef FEAT_MBYTE
3665 if (has_mbyte)
3666 c = mb_ptr2char(p);
3667 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003669 c = *p;
3670 if (c == '\\' && p[1] != NUL)
3671 ++p;
3672 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
3674 if (!in_quote)
3675 {
3676 xp->xp_pattern = p;
3677 bow = p + 1;
3678 }
3679 in_quote = !in_quote;
3680 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003681 /* An argument can contain just about everything, except
3682 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003683 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003684#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003685 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003686#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003687 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003688 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003689 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003690 while (*p != NUL)
3691 {
3692#ifdef FEAT_MBYTE
3693 if (has_mbyte)
3694 c = mb_ptr2char(p);
3695 else
3696#endif
3697 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003698 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003699 break;
3700#ifdef FEAT_MBYTE
3701 if (has_mbyte)
3702 len = (*mb_ptr2len)(p);
3703 else
3704#endif
3705 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003706 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003707 }
3708 if (in_quote)
3709 bow = p;
3710 else
3711 xp->xp_pattern = p;
3712 p -= len;
3713 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003714 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
3716
3717 /*
3718 * If we are still inside the quotes, and we passed a space, just
3719 * expand from there.
3720 */
3721 if (bow != NULL && in_quote)
3722 xp->xp_pattern = bow;
3723 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003724
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003725 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003726 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003727 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003728#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003729 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003730#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003731 /* When still after the command name expand executables. */
3732 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003733 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
3736 /* Check for environment variable */
3737 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003738#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 || *xp->xp_pattern == '%'
3740#endif
3741 )
3742 {
3743 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3744 if (!vim_isIDc(*p))
3745 break;
3746 if (*p == NUL)
3747 {
3748 xp->xp_context = EXPAND_ENV_VARS;
3749 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003750#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3751 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003752 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003753 compl = EXPAND_ENV_VARS;
3754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003757#if defined(FEAT_CMDL_COMPL)
3758 /* Check for user names */
3759 if (*xp->xp_pattern == '~')
3760 {
3761 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3762 ;
3763 /* Complete ~user only if it partially matches a user name.
3764 * A full match ~user<Tab> will be replaced by user's home
3765 * directory i.e. something like ~user<Tab> -> /home/user/ */
3766 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003767 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003768 {
3769 xp->xp_context = EXPAND_USER;
3770 ++xp->xp_pattern;
3771 }
3772 }
3773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
3775
3776/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003777 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003779 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003781 case CMD_find:
3782 case CMD_sfind:
3783 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003784 if (xp->xp_context == EXPAND_FILES)
3785 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003786 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 case CMD_cd:
3788 case CMD_chdir:
3789 case CMD_lcd:
3790 case CMD_lchdir:
3791 if (xp->xp_context == EXPAND_FILES)
3792 xp->xp_context = EXPAND_DIRECTORIES;
3793 break;
3794 case CMD_help:
3795 xp->xp_context = EXPAND_HELP;
3796 xp->xp_pattern = arg;
3797 break;
3798
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003799 /* Command modifiers: return the argument.
3800 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003802 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 case CMD_belowright:
3804 case CMD_botright:
3805 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003806 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003807 case CMD_cdo:
3808 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003810 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 case CMD_folddoclosed:
3812 case CMD_folddoopen:
3813 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003814 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 case CMD_keepjumps:
3816 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003817 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003818 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003820 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003822 case CMD_noautocmd:
3823 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003825 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003827 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003828 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 case CMD_topleft:
3830 case CMD_verbose:
3831 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003832 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 return arg;
3834
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003835 case CMD_filter:
3836 if (*arg != NUL)
3837 arg = skip_vimgrep_pat(arg, NULL, NULL);
3838 if (arg == NULL || *arg == NUL)
3839 {
3840 xp->xp_context = EXPAND_NOTHING;
3841 return NULL;
3842 }
3843 return skipwhite(arg);
3844
Bram Moolenaar4f688582007-07-24 12:34:30 +00003845#ifdef FEAT_CMDL_COMPL
3846# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 case CMD_match:
3848 if (*arg == NUL || !ends_excmd(*arg))
3849 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003850 /* also complete "None" */
3851 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 arg = skipwhite(skiptowhite(arg));
3853 if (*arg != NUL)
3854 {
3855 xp->xp_context = EXPAND_NOTHING;
3856 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3857 }
3858 }
3859 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003860# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862/*
3863 * All completion for the +cmdline_compl feature goes here.
3864 */
3865
3866# ifdef FEAT_USR_CMDS
3867 case CMD_command:
3868 /* Check for attributes */
3869 while (*arg == '-')
3870 {
3871 arg++; /* Skip "-" */
3872 p = skiptowhite(arg);
3873 if (*p == NUL)
3874 {
3875 /* Cursor is still in the attribute */
3876 p = vim_strchr(arg, '=');
3877 if (p == NULL)
3878 {
3879 /* No "=", so complete attribute names */
3880 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3881 xp->xp_pattern = arg;
3882 return NULL;
3883 }
3884
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003885 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 * their arguments as well.
3887 */
3888 if (STRNICMP(arg, "complete", p - arg) == 0)
3889 {
3890 xp->xp_context = EXPAND_USER_COMPLETE;
3891 xp->xp_pattern = p + 1;
3892 return NULL;
3893 }
3894 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3895 {
3896 xp->xp_context = EXPAND_USER_NARGS;
3897 xp->xp_pattern = p + 1;
3898 return NULL;
3899 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003900 else if (STRNICMP(arg, "addr", p - arg) == 0)
3901 {
3902 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3903 xp->xp_pattern = p + 1;
3904 return NULL;
3905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 return NULL;
3907 }
3908 arg = skipwhite(p);
3909 }
3910
3911 /* After the attributes comes the new command name */
3912 p = skiptowhite(arg);
3913 if (*p == NUL)
3914 {
3915 xp->xp_context = EXPAND_USER_COMMANDS;
3916 xp->xp_pattern = arg;
3917 break;
3918 }
3919
3920 /* And finally comes a normal command */
3921 return skipwhite(p);
3922
3923 case CMD_delcommand:
3924 xp->xp_context = EXPAND_USER_COMMANDS;
3925 xp->xp_pattern = arg;
3926 break;
3927# endif
3928
3929 case CMD_global:
3930 case CMD_vglobal:
3931 delim = *arg; /* get the delimiter */
3932 if (delim)
3933 ++arg; /* skip delimiter if there is one */
3934
3935 while (arg[0] != NUL && arg[0] != delim)
3936 {
3937 if (arg[0] == '\\' && arg[1] != NUL)
3938 ++arg;
3939 ++arg;
3940 }
3941 if (arg[0] != NUL)
3942 return arg + 1;
3943 break;
3944 case CMD_and:
3945 case CMD_substitute:
3946 delim = *arg;
3947 if (delim)
3948 {
3949 /* skip "from" part */
3950 ++arg;
3951 arg = skip_regexp(arg, delim, p_magic, NULL);
3952 }
3953 /* skip "to" part */
3954 while (arg[0] != NUL && arg[0] != delim)
3955 {
3956 if (arg[0] == '\\' && arg[1] != NUL)
3957 ++arg;
3958 ++arg;
3959 }
3960 if (arg[0] != NUL) /* skip delimiter */
3961 ++arg;
3962 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3963 ++arg;
3964 if (arg[0] != NUL)
3965 return arg;
3966 break;
3967 case CMD_isearch:
3968 case CMD_dsearch:
3969 case CMD_ilist:
3970 case CMD_dlist:
3971 case CMD_ijump:
3972 case CMD_psearch:
3973 case CMD_djump:
3974 case CMD_isplit:
3975 case CMD_dsplit:
3976 arg = skipwhite(skipdigits(arg)); /* skip count */
3977 if (*arg == '/') /* Match regexp, not just whole words */
3978 {
3979 for (++arg; *arg && *arg != '/'; arg++)
3980 if (*arg == '\\' && arg[1] != NUL)
3981 arg++;
3982 if (*arg)
3983 {
3984 arg = skipwhite(arg + 1);
3985
3986 /* Check for trailing illegal characters */
3987 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3988 xp->xp_context = EXPAND_NOTHING;
3989 else
3990 return arg;
3991 }
3992 }
3993 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 case CMD_autocmd:
3996 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003998 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 case CMD_set:
4001 set_context_in_set_cmd(xp, arg, 0);
4002 break;
4003 case CMD_setglobal:
4004 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4005 break;
4006 case CMD_setlocal:
4007 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4008 break;
4009 case CMD_tag:
4010 case CMD_stag:
4011 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004012 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 case CMD_tselect:
4014 case CMD_stselect:
4015 case CMD_ptselect:
4016 case CMD_tjump:
4017 case CMD_stjump:
4018 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004019 if (*p_wop != NUL)
4020 xp->xp_context = EXPAND_TAGS_LISTFILES;
4021 else
4022 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 xp->xp_pattern = arg;
4024 break;
4025 case CMD_augroup:
4026 xp->xp_context = EXPAND_AUGROUP;
4027 xp->xp_pattern = arg;
4028 break;
4029#ifdef FEAT_SYN_HL
4030 case CMD_syntax:
4031 set_context_in_syntax_cmd(xp, arg);
4032 break;
4033#endif
4034#ifdef FEAT_EVAL
4035 case CMD_let:
4036 case CMD_if:
4037 case CMD_elseif:
4038 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004039 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 case CMD_echo:
4041 case CMD_echon:
4042 case CMD_execute:
4043 case CMD_echomsg:
4044 case CMD_echoerr:
4045 case CMD_call:
4046 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004047 case CMD_cexpr:
4048 case CMD_caddexpr:
4049 case CMD_cgetexpr:
4050 case CMD_lexpr:
4051 case CMD_laddexpr:
4052 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004053 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 break;
4055
4056 case CMD_unlet:
4057 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4058 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02004059
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 xp->xp_context = EXPAND_USER_VARS;
4061 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02004062
4063 if (*xp->xp_pattern == '$')
4064 {
4065 xp->xp_context = EXPAND_ENV_VARS;
4066 ++xp->xp_pattern;
4067 }
4068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 break;
4070
4071 case CMD_function:
4072 case CMD_delfunction:
4073 xp->xp_context = EXPAND_USER_FUNC;
4074 xp->xp_pattern = arg;
4075 break;
4076
4077 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004078 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 break;
4080#endif
4081 case CMD_highlight:
4082 set_context_in_highlight_cmd(xp, arg);
4083 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004084#ifdef FEAT_CSCOPE
4085 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004086 case CMD_lcscope:
4087 case CMD_scscope:
4088 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004089 break;
4090#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004091#ifdef FEAT_SIGNS
4092 case CMD_sign:
4093 set_context_in_sign_cmd(xp, arg);
4094 break;
4095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 case CMD_bdelete:
4097 case CMD_bwipeout:
4098 case CMD_bunload:
4099 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4100 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004101 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 case CMD_buffer:
4103 case CMD_sbuffer:
4104 case CMD_checktime:
4105 xp->xp_context = EXPAND_BUFFERS;
4106 xp->xp_pattern = arg;
4107 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108#ifdef FEAT_USR_CMDS
4109 case CMD_USER:
4110 case CMD_USER_BUF:
4111 if (compl != EXPAND_NOTHING)
4112 {
4113 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004114 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 {
4116# ifdef FEAT_MENU
4117 if (compl == EXPAND_MENUS)
4118 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4119# endif
4120 if (compl == EXPAND_COMMANDS)
4121 return arg;
4122 if (compl == EXPAND_MAPPINGS)
4123 return set_context_in_map_cmd(xp, (char_u *)"map",
4124 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004125 /* Find start of last argument. */
4126 p = arg;
4127 while (*p)
4128 {
4129 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004130 /* argument starts after a space */
4131 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004132 else if (*p == '\\' && *(p + 1) != NUL)
4133 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004134 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 xp->xp_pattern = arg;
4137 }
4138 xp->xp_context = compl;
4139 }
4140 break;
4141#endif
4142 case CMD_map: case CMD_noremap:
4143 case CMD_nmap: case CMD_nnoremap:
4144 case CMD_vmap: case CMD_vnoremap:
4145 case CMD_omap: case CMD_onoremap:
4146 case CMD_imap: case CMD_inoremap:
4147 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004148 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004149 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004150 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004151 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004153 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 case CMD_unmap:
4155 case CMD_nunmap:
4156 case CMD_vunmap:
4157 case CMD_ounmap:
4158 case CMD_iunmap:
4159 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004160 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004161 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004162 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004163 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004165 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004166 case CMD_mapclear:
4167 case CMD_nmapclear:
4168 case CMD_vmapclear:
4169 case CMD_omapclear:
4170 case CMD_imapclear:
4171 case CMD_cmapclear:
4172 case CMD_lmapclear:
4173 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004174 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004175 case CMD_xmapclear:
4176 xp->xp_context = EXPAND_MAPCLEAR;
4177 xp->xp_pattern = arg;
4178 break;
4179
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 case CMD_abbreviate: case CMD_noreabbrev:
4181 case CMD_cabbrev: case CMD_cnoreabbrev:
4182 case CMD_iabbrev: case CMD_inoreabbrev:
4183 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004184 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 case CMD_unabbreviate:
4186 case CMD_cunabbrev:
4187 case CMD_iunabbrev:
4188 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004189 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190#ifdef FEAT_MENU
4191 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4192 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4193 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4194 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4195 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4196 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4197 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4198 case CMD_tmenu: case CMD_tunmenu:
4199 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4200 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4201#endif
4202
4203 case CMD_colorscheme:
4204 xp->xp_context = EXPAND_COLORS;
4205 xp->xp_pattern = arg;
4206 break;
4207
4208 case CMD_compiler:
4209 xp->xp_context = EXPAND_COMPILER;
4210 xp->xp_pattern = arg;
4211 break;
4212
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004213 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004214 xp->xp_context = EXPAND_OWNSYNTAX;
4215 xp->xp_pattern = arg;
4216 break;
4217
4218 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004219 xp->xp_context = EXPAND_FILETYPE;
4220 xp->xp_pattern = arg;
4221 break;
4222
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004223 case CMD_packadd:
4224 xp->xp_context = EXPAND_PACKADD;
4225 xp->xp_pattern = arg;
4226 break;
4227
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4229 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4230 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004231 p = skiptowhite(arg);
4232 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 {
4234 xp->xp_context = EXPAND_LANGUAGE;
4235 xp->xp_pattern = arg;
4236 }
4237 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004238 {
4239 if ( STRNCMP(arg, "messages", p - arg) == 0
4240 || STRNCMP(arg, "ctype", p - arg) == 0
4241 || STRNCMP(arg, "time", p - arg) == 0)
4242 {
4243 xp->xp_context = EXPAND_LOCALES;
4244 xp->xp_pattern = skipwhite(p);
4245 }
4246 else
4247 xp->xp_context = EXPAND_NOTHING;
4248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 break;
4250#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004251#if defined(FEAT_PROFILE)
4252 case CMD_profile:
4253 set_context_in_profile_cmd(xp, arg);
4254 break;
4255#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004256 case CMD_behave:
4257 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004258 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004259 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004261 case CMD_messages:
4262 xp->xp_context = EXPAND_MESSAGES;
4263 xp->xp_pattern = arg;
4264 break;
4265
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004266#if defined(FEAT_CMDHIST)
4267 case CMD_history:
4268 xp->xp_context = EXPAND_HISTORY;
4269 xp->xp_pattern = arg;
4270 break;
4271#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004272#if defined(FEAT_PROFILE)
4273 case CMD_syntime:
4274 xp->xp_context = EXPAND_SYNTIME;
4275 xp->xp_pattern = arg;
4276 break;
4277#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004278
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004279 case CMD_argdelete:
4280 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4281 arg = xp->xp_pattern + 1;
4282 xp->xp_context = EXPAND_ARGLIST;
4283 xp->xp_pattern = arg;
4284 break;
4285
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286#endif /* FEAT_CMDL_COMPL */
4287
4288 default:
4289 break;
4290 }
4291 return NULL;
4292}
4293
4294/*
4295 * skip a range specifier of the form: addr [,addr] [;addr] ..
4296 *
4297 * Backslashed delimiters after / or ? will be skipped, and commands will
4298 * not be expanded between /'s and ?'s or after "'".
4299 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004300 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 * Returns the "cmd" pointer advanced to beyond the range.
4302 */
4303 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004304skip_range(
4305 char_u *cmd,
4306 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004308 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004310 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004312 if (*cmd == '\\')
4313 {
4314 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4315 ++cmd;
4316 else
4317 break;
4318 }
4319 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
4321 if (*++cmd == NUL && ctx != NULL)
4322 *ctx = EXPAND_NOTHING;
4323 }
4324 else if (*cmd == '/' || *cmd == '?')
4325 {
4326 delim = *cmd++;
4327 while (*cmd != NUL && *cmd != delim)
4328 if (*cmd++ == '\\' && *cmd != NUL)
4329 ++cmd;
4330 if (*cmd == NUL && ctx != NULL)
4331 *ctx = EXPAND_NOTHING;
4332 }
4333 if (*cmd != NUL)
4334 ++cmd;
4335 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004336
4337 /* Skip ":" and white space. */
4338 while (*cmd == ':')
4339 cmd = skipwhite(cmd + 1);
4340
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 return cmd;
4342}
4343
4344/*
4345 * get a single EX address
4346 *
4347 * Set ptr to the next character after the part that was interpreted.
4348 * Set ptr to NULL when an error is encountered.
4349 *
4350 * Return MAXLNUM when no Ex address was found.
4351 */
4352 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004353get_address(
4354 exarg_T *eap UNUSED,
4355 char_u **ptr,
4356 int addr_type, /* flag: one of ADDR_LINES, ... */
4357 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004358 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004359 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360{
4361 int c;
4362 int i;
4363 long n;
4364 char_u *cmd;
4365 pos_T pos;
4366 pos_T *fp;
4367 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004368 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369
4370 cmd = skipwhite(*ptr);
4371 lnum = MAXLNUM;
4372 do
4373 {
4374 switch (*cmd)
4375 {
4376 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004377 ++cmd;
4378 switch (addr_type)
4379 {
4380 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 lnum = curwin->w_cursor.lnum;
4382 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004383 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004384 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004385 break;
4386 case ADDR_ARGUMENTS:
4387 lnum = curwin->w_arg_idx + 1;
4388 break;
4389 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004390 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004391 lnum = curbuf->b_fnum;
4392 break;
4393 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004394 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004395 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004396 case ADDR_TABS_RELATIVE:
4397 EMSG(_(e_invrange));
4398 cmd = NULL;
4399 goto error;
4400 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004401#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004402 case ADDR_QUICKFIX:
4403 lnum = qf_get_cur_valid_idx(eap);
4404 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004405#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004406 }
4407 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408
4409 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004410 ++cmd;
4411 switch (addr_type)
4412 {
4413 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 lnum = curbuf->b_ml.ml_line_count;
4415 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004416 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004417 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004418 break;
4419 case ADDR_ARGUMENTS:
4420 lnum = ARGCOUNT;
4421 break;
4422 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004423 buf = lastbuf;
4424 while (buf->b_ml.ml_mfp == NULL)
4425 {
4426 if (buf->b_prev == NULL)
4427 break;
4428 buf = buf->b_prev;
4429 }
4430 lnum = buf->b_fnum;
4431 break;
4432 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 lnum = lastbuf->b_fnum;
4434 break;
4435 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004436 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004437 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004438 case ADDR_TABS_RELATIVE:
4439 EMSG(_(e_invrange));
4440 cmd = NULL;
4441 goto error;
4442 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004443#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004444 case ADDR_QUICKFIX:
4445 lnum = qf_get_size(eap);
4446 if (lnum == 0)
4447 lnum = 1;
4448 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004449#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 }
4451 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
4453 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004454 if (*++cmd == NUL)
4455 {
4456 cmd = NULL;
4457 goto error;
4458 }
4459 if (addr_type != ADDR_LINES)
4460 {
4461 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004462 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004463 goto error;
4464 }
4465 if (skip)
4466 ++cmd;
4467 else
4468 {
4469 /* Only accept a mark in another file when it is
4470 * used by itself: ":'M". */
4471 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4472 ++cmd;
4473 if (fp == (pos_T *)-1)
4474 /* Jumped to another file. */
4475 lnum = curwin->w_cursor.lnum;
4476 else
4477 {
4478 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
4480 cmd = NULL;
4481 goto error;
4482 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004483 lnum = fp->lnum;
4484 }
4485 }
4486 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487
4488 case '/':
4489 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004490 c = *cmd++;
4491 if (addr_type != ADDR_LINES)
4492 {
4493 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004494 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 goto error;
4496 }
4497 if (skip) /* skip "/pat/" */
4498 {
4499 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4500 if (*cmd == c)
4501 ++cmd;
4502 }
4503 else
4504 {
4505 pos = curwin->w_cursor; /* save curwin->w_cursor */
4506 /*
4507 * When '/' or '?' follows another address, start
4508 * from there.
4509 */
4510 if (lnum != MAXLNUM)
4511 curwin->w_cursor.lnum = lnum;
4512 /*
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004513 * Start a forward search at the end of the line (unless
4514 * before the first line).
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004515 * Start a backward search at the start of the line.
4516 * This makes sure we never match in the current
4517 * line, and can match anywhere in the
4518 * next/previous line.
4519 */
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004520 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004521 curwin->w_cursor.col = MAXCOL;
4522 else
4523 curwin->w_cursor.col = 0;
4524 searchcmdlen = 0;
4525 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004526 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004527 {
4528 curwin->w_cursor = pos;
4529 cmd = NULL;
4530 goto error;
4531 }
4532 lnum = curwin->w_cursor.lnum;
4533 curwin->w_cursor = pos;
4534 /* adjust command string pointer */
4535 cmd += searchcmdlen;
4536 }
4537 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538
4539 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004540 ++cmd;
4541 if (addr_type != ADDR_LINES)
4542 {
4543 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004544 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004545 goto error;
4546 }
4547 if (*cmd == '&')
4548 i = RE_SUBST;
4549 else if (*cmd == '?' || *cmd == '/')
4550 i = RE_SEARCH;
4551 else
4552 {
4553 EMSG(_(e_backslash));
4554 cmd = NULL;
4555 goto error;
4556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004558 if (!skip)
4559 {
4560 /*
4561 * When search follows another address, start from
4562 * there.
4563 */
4564 if (lnum != MAXLNUM)
4565 pos.lnum = lnum;
4566 else
4567 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004569 /*
4570 * Start the search just like for the above
4571 * do_search().
4572 */
4573 if (*cmd != '?')
4574 pos.col = MAXCOL;
4575 else
4576 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004577#ifdef FEAT_VIRTUALEDIT
4578 pos.coladd = 0;
4579#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004580 if (searchit(curwin, curbuf, &pos,
4581 *cmd == '?' ? BACKWARD : FORWARD,
4582 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004583 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004584 lnum = pos.lnum;
4585 else
4586 {
4587 cmd = NULL;
4588 goto error;
4589 }
4590 }
4591 ++cmd;
4592 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
4594 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004595 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4596 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
4598
4599 for (;;)
4600 {
4601 cmd = skipwhite(cmd);
4602 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4603 break;
4604
4605 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004606 {
4607 switch (addr_type)
4608 {
4609 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004610 /* "+1" is same as ".+1" */
4611 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004612 break;
4613 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004614 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004615 break;
4616 case ADDR_ARGUMENTS:
4617 lnum = curwin->w_arg_idx + 1;
4618 break;
4619 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004620 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004621 lnum = curbuf->b_fnum;
4622 break;
4623 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004624 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004625 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004626 case ADDR_TABS_RELATIVE:
4627 lnum = 1;
4628 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004629#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004630 case ADDR_QUICKFIX:
4631 lnum = qf_get_cur_valid_idx(eap);
4632 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004633#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004634 }
4635 }
4636
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 if (VIM_ISDIGIT(*cmd))
4638 i = '+'; /* "number" is same as "+number" */
4639 else
4640 i = *cmd++;
4641 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4642 n = 1;
4643 else
4644 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004645
4646 if (addr_type == ADDR_TABS_RELATIVE)
4647 {
4648 EMSG(_(e_invrange));
4649 cmd = NULL;
4650 goto error;
4651 }
4652 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004653 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004654 lnum = compute_buffer_local_count(
4655 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004657 {
4658#ifdef FEAT_FOLDING
4659 /* Relative line addressing, need to adjust for folded lines
4660 * now, but only do it after the first address. */
4661 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4662 && address_count >= 2)
4663 (void)hasFolding(lnum, NULL, &lnum);
4664#endif
4665 if (i == '-')
4666 lnum -= n;
4667 else
4668 lnum += n;
4669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 }
4671 } while (*cmd == '/' || *cmd == '?');
4672
4673error:
4674 *ptr = cmd;
4675 return lnum;
4676}
4677
4678/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004679 * Get flags from an Ex command argument.
4680 */
4681 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004682get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004683{
4684 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4685 {
4686 if (*eap->arg == 'l')
4687 eap->flags |= EXFLAG_LIST;
4688 else if (*eap->arg == 'p')
4689 eap->flags |= EXFLAG_PRINT;
4690 else
4691 eap->flags |= EXFLAG_NR;
4692 eap->arg = skipwhite(eap->arg + 1);
4693 }
4694}
4695
4696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 * Function called for command which is Not Implemented. NI!
4698 */
4699 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004700ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
4702 if (!eap->skip)
4703 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4704}
4705
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004706#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707/*
4708 * Function called for script command which is Not Implemented. NI!
4709 * Skips over ":perl <<EOF" constructs.
4710 */
4711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004712ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713{
4714 if (!eap->skip)
4715 ex_ni(eap);
4716 else
4717 vim_free(script_get(eap, eap->arg));
4718}
4719#endif
4720
4721/*
4722 * Check range in Ex command for validity.
4723 * Return NULL when valid, error message when invalid.
4724 */
4725 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004726invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004728 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 if ( eap->line1 < 0
4730 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004731 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004733
4734 if (eap->argt & RANGE)
4735 {
4736 switch(eap->addr_type)
4737 {
4738 case ADDR_LINES:
4739 if (!(eap->argt & NOTADR)
4740 && eap->line2 > curbuf->b_ml.ml_line_count
4741#ifdef FEAT_DIFF
4742 + (eap->cmdidx == CMD_diffget)
4743#endif
4744 )
4745 return (char_u *)_(e_invrange);
4746 break;
4747 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004748 /* add 1 if ARGCOUNT is 0 */
4749 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004750 return (char_u *)_(e_invrange);
4751 break;
4752 case ADDR_BUFFERS:
4753 if (eap->line1 < firstbuf->b_fnum
4754 || eap->line2 > lastbuf->b_fnum)
4755 return (char_u *)_(e_invrange);
4756 break;
4757 case ADDR_LOADED_BUFFERS:
4758 buf = firstbuf;
4759 while (buf->b_ml.ml_mfp == NULL)
4760 {
4761 if (buf->b_next == NULL)
4762 return (char_u *)_(e_invrange);
4763 buf = buf->b_next;
4764 }
4765 if (eap->line1 < buf->b_fnum)
4766 return (char_u *)_(e_invrange);
4767 buf = lastbuf;
4768 while (buf->b_ml.ml_mfp == NULL)
4769 {
4770 if (buf->b_prev == NULL)
4771 return (char_u *)_(e_invrange);
4772 buf = buf->b_prev;
4773 }
4774 if (eap->line2 > buf->b_fnum)
4775 return (char_u *)_(e_invrange);
4776 break;
4777 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004778 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004779 return (char_u *)_(e_invrange);
4780 break;
4781 case ADDR_TABS:
4782 if (eap->line2 > LAST_TAB_NR)
4783 return (char_u *)_(e_invrange);
4784 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004785 case ADDR_TABS_RELATIVE:
4786 /* Do nothing */
4787 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004788#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004789 case ADDR_QUICKFIX:
4790 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4791 return (char_u *)_(e_invrange);
4792 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004793#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004794 }
4795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 return NULL;
4797}
4798
4799/*
4800 * Correct the range for zero line number, if required.
4801 */
4802 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004803correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804{
4805 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4806 {
4807 if (eap->line1 == 0)
4808 eap->line1 = 1;
4809 if (eap->line2 == 0)
4810 eap->line2 = 1;
4811 }
4812}
4813
Bram Moolenaar748bf032005-02-02 23:04:36 +00004814#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004815static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004816
4817/*
4818 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4819 * pattern. Otherwise return eap->arg.
4820 */
4821 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004822skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004823{
4824 char_u *p = eap->arg;
4825
Bram Moolenaara37420f2006-02-04 22:37:47 +00004826 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4827 || eap->cmdidx == CMD_vimgrepadd
4828 || eap->cmdidx == CMD_lvimgrepadd
4829 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004830 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004831 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004832 if (p == NULL)
4833 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004834 }
4835 return p;
4836}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004837
4838/*
4839 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4840 * in the command line, so that things like % get expanded.
4841 */
4842 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004843replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004844{
4845 char_u *new_cmdline;
4846 char_u *program;
4847 char_u *pos;
4848 char_u *ptr;
4849 int len;
4850 int i;
4851
4852 /*
4853 * Don't do it when ":vimgrep" is used for ":grep".
4854 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004855 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4856 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4857 || eap->cmdidx == CMD_grepadd
4858 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004859 && !grep_internal(eap->cmdidx))
4860 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004861 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4862 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004863 {
4864 if (*curbuf->b_p_gp == NUL)
4865 program = p_gp;
4866 else
4867 program = curbuf->b_p_gp;
4868 }
4869 else
4870 {
4871 if (*curbuf->b_p_mp == NUL)
4872 program = p_mp;
4873 else
4874 program = curbuf->b_p_mp;
4875 }
4876
4877 p = skipwhite(p);
4878
4879 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4880 {
4881 /* replace $* by given arguments */
4882 i = 1;
4883 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4884 ++i;
4885 len = (int)STRLEN(p);
4886 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4887 if (new_cmdline == NULL)
4888 return NULL; /* out of memory */
4889 ptr = new_cmdline;
4890 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4891 {
4892 i = (int)(pos - program);
4893 STRNCPY(ptr, program, i);
4894 STRCPY(ptr += i, p);
4895 ptr += len;
4896 program = pos + 2;
4897 }
4898 STRCPY(ptr, program);
4899 }
4900 else
4901 {
4902 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4903 if (new_cmdline == NULL)
4904 return NULL; /* out of memory */
4905 STRCPY(new_cmdline, program);
4906 STRCAT(new_cmdline, " ");
4907 STRCAT(new_cmdline, p);
4908 }
4909 msg_make(p);
4910
4911 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4912 vim_free(*cmdlinep);
4913 *cmdlinep = new_cmdline;
4914 p = new_cmdline;
4915 }
4916 return p;
4917}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004918#endif
4919
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920/*
4921 * Expand file name in Ex command argument.
4922 * Return FAIL for failure, OK otherwise.
4923 */
4924 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004925expand_filename(
4926 exarg_T *eap,
4927 char_u **cmdlinep,
4928 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929{
4930 int has_wildcards; /* need to expand wildcards */
4931 char_u *repl;
4932 int srclen;
4933 char_u *p;
4934 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004935 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936
Bram Moolenaar748bf032005-02-02 23:04:36 +00004937#ifdef FEAT_QUICKFIX
4938 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4939 p = skip_grep_pat(eap);
4940#else
4941 p = eap->arg;
4942#endif
4943
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 /*
4945 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4946 * the file name contains a wildcard it should not cause expanding.
4947 * (it will be expanded anyway if there is a wildcard before replacing).
4948 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004949 has_wildcards = mch_has_wildcard(p);
4950 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004952#ifdef FEAT_EVAL
4953 /* Skip over `=expr`, wildcards in it are not expanded. */
4954 if (p[0] == '`' && p[1] == '=')
4955 {
4956 p += 2;
4957 (void)skip_expr(&p);
4958 if (*p == '`')
4959 ++p;
4960 continue;
4961 }
4962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 /*
4964 * Quick check if this cannot be the start of a special string.
4965 * Also removes backslash before '%', '#' and '<'.
4966 */
4967 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4968 {
4969 ++p;
4970 continue;
4971 }
4972
4973 /*
4974 * Try to find a match at this position.
4975 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004976 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4977 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 if (*errormsgp != NULL) /* error detected */
4979 return FAIL;
4980 if (repl == NULL) /* no match found */
4981 {
4982 p += srclen;
4983 continue;
4984 }
4985
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004986 /* Wildcards won't be expanded below, the replacement is taken
4987 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4988 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4989 {
4990 char_u *l = repl;
4991
4992 repl = expand_env_save(repl);
4993 vim_free(l);
4994 }
4995
Bram Moolenaar63b92542007-03-27 14:57:09 +00004996 /* Need to escape white space et al. with a backslash.
4997 * Don't do this for:
4998 * - replacement that already has been escaped: "##"
4999 * - shell commands (may have to use quotes instead).
5000 * - non-unix systems when there is a single argument (spaces don't
5001 * separate arguments then).
5002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005004 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 && eap->cmdidx != CMD_grep
5007 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005008 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005009 && eap->cmdidx != CMD_lgrep
5010 && eap->cmdidx != CMD_lgrepadd
5011 && eap->cmdidx != CMD_lmake
5012 && eap->cmdidx != CMD_make
5013 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014#ifndef UNIX
5015 && !(eap->argt & NOSPC)
5016#endif
5017 )
5018 {
5019 char_u *l;
5020#ifdef BACKSLASH_IN_FILENAME
5021 /* Don't escape a backslash here, because rem_backslash() doesn't
5022 * remove it later. */
5023 static char_u *nobslash = (char_u *)" \t\"|";
5024# define ESCAPE_CHARS nobslash
5025#else
5026# define ESCAPE_CHARS escape_chars
5027#endif
5028
5029 for (l = repl; *l; ++l)
5030 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5031 {
5032 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5033 if (l != NULL)
5034 {
5035 vim_free(repl);
5036 repl = l;
5037 }
5038 break;
5039 }
5040 }
5041
5042 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005043 if ((eap->usefilter || eap->cmdidx == CMD_bang
5044 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005045 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
5047 char_u *l;
5048
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005049 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 if (l != NULL)
5051 {
5052 vim_free(repl);
5053 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
5055 }
5056
5057 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5058 vim_free(repl);
5059 if (p == NULL)
5060 return FAIL;
5061 }
5062
5063 /*
5064 * One file argument: Expand wildcards.
5065 * Don't do this with ":r !command" or ":w !command".
5066 */
5067 if ((eap->argt & NOSPC) && !eap->usefilter)
5068 {
5069 /*
5070 * May do this twice:
5071 * 1. Replace environment variables.
5072 * 2. Replace any other wildcards, remove backslashes.
5073 */
5074 for (n = 1; n <= 2; ++n)
5075 {
5076 if (n == 2)
5077 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 /*
5079 * Halve the number of backslashes (this is Vi compatible).
5080 * For Unix and OS/2, when wildcards are expanded, this is
5081 * done by ExpandOne() below.
5082 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005083#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 if (!has_wildcards)
5085#endif
5086 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 }
5088
5089 if (has_wildcards)
5090 {
5091 if (n == 1)
5092 {
5093 /*
5094 * First loop: May expand environment variables. This
5095 * can be done much faster with expand_env() than with
5096 * something else (e.g., calling a shell).
5097 * After expanding environment variables, check again
5098 * if there are still wildcards present.
5099 */
5100 if (vim_strchr(eap->arg, '$') != NULL
5101 || vim_strchr(eap->arg, '~') != NULL)
5102 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005103 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005104 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 has_wildcards = mch_has_wildcard(NameBuff);
5106 p = NameBuff;
5107 }
5108 else
5109 p = NULL;
5110 }
5111 else /* n == 2 */
5112 {
5113 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005114 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115
5116 ExpandInit(&xpc);
5117 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005118 if (p_wic)
5119 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005121 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (p == NULL)
5123 return FAIL;
5124 }
5125 if (p != NULL)
5126 {
5127 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5128 p, cmdlinep);
5129 if (n == 2) /* p came from ExpandOne() */
5130 vim_free(p);
5131 }
5132 }
5133 }
5134 }
5135 return OK;
5136}
5137
5138/*
5139 * Replace part of the command line, keeping eap->cmd, eap->arg and
5140 * eap->nextcmd correct.
5141 * "src" points to the part that is to be replaced, of length "srclen".
5142 * "repl" is the replacement string.
5143 * Returns a pointer to the character after the replaced string.
5144 * Returns NULL for failure.
5145 */
5146 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005147repl_cmdline(
5148 exarg_T *eap,
5149 char_u *src,
5150 int srclen,
5151 char_u *repl,
5152 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153{
5154 int len;
5155 int i;
5156 char_u *new_cmdline;
5157
5158 /*
5159 * The new command line is build in new_cmdline[].
5160 * First allocate it.
5161 * Careful: a "+cmd" argument may have been NUL terminated.
5162 */
5163 len = (int)STRLEN(repl);
5164 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005165 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5167 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5168 return NULL; /* out of memory! */
5169
5170 /*
5171 * Copy the stuff before the expanded part.
5172 * Copy the expanded stuff.
5173 * Copy what came after the expanded part.
5174 * Copy the next commands, if there are any.
5175 */
5176 i = (int)(src - *cmdlinep); /* length of part before match */
5177 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005178
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 mch_memmove(new_cmdline + i, repl, (size_t)len);
5180 i += len; /* remember the end of the string */
5181 STRCPY(new_cmdline + i, src + srclen);
5182 src = new_cmdline + i; /* remember where to continue */
5183
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005184 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
5186 i = (int)STRLEN(new_cmdline) + 1;
5187 STRCPY(new_cmdline + i, eap->nextcmd);
5188 eap->nextcmd = new_cmdline + i;
5189 }
5190 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5191 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5192 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5193 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5194 vim_free(*cmdlinep);
5195 *cmdlinep = new_cmdline;
5196
5197 return src;
5198}
5199
5200/*
5201 * Check for '|' to separate commands and '"' to start comments.
5202 */
5203 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005204separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205{
5206 char_u *p;
5207
Bram Moolenaar86b68352004-12-27 21:59:20 +00005208#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005209 p = skip_grep_pat(eap);
5210#else
5211 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005212#endif
5213
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005214 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
5216 if (*p == Ctrl_V)
5217 {
5218 if (eap->argt & (USECTRLV | XFILE))
5219 ++p; /* skip CTRL-V and next char */
5220 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005221 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005222 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 if (*p == NUL) /* stop at NUL after CTRL-V */
5224 break;
5225 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005226
5227#ifdef FEAT_EVAL
5228 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005229 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005230 {
5231 p += 2;
5232 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005233 }
5234#endif
5235
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 /* Check for '"': start of comment or '|': next command */
5237 /* :@" and :*" do not start a comment!
5238 * :redir @" doesn't either. */
5239 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5240 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5241 || p != eap->arg)
5242 && (eap->cmdidx != CMD_redir
5243 || p != eap->arg + 1 || p[-1] != '@'))
5244 || *p == '|' || *p == '\n')
5245 {
5246 /*
5247 * We remove the '\' before the '|', unless USECTRLV is used
5248 * AND 'b' is present in 'cpoptions'.
5249 */
5250 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5251 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5252 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005253 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 --p;
5255 }
5256 else
5257 {
5258 eap->nextcmd = check_nextcmd(p);
5259 *p = NUL;
5260 break;
5261 }
5262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005264
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5266 del_trailing_spaces(eap->arg);
5267}
5268
5269/*
5270 * get + command from ex argument
5271 */
5272 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005273getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
5275 char_u *arg = *argp;
5276 char_u *command = NULL;
5277
5278 if (*arg == '+') /* +[command] */
5279 {
5280 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005281 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 command = dollar_command;
5283 else
5284 {
5285 command = arg;
5286 arg = skip_cmd_arg(command, TRUE);
5287 if (*arg != NUL)
5288 *arg++ = NUL; /* terminate command with NUL */
5289 }
5290
5291 arg = skipwhite(arg); /* skip over spaces */
5292 *argp = arg;
5293 }
5294 return command;
5295}
5296
5297/*
5298 * Find end of "+command" argument. Skip over "\ " and "\\".
5299 */
5300 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005301skip_cmd_arg(
5302 char_u *p,
5303 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304{
5305 while (*p && !vim_isspace(*p))
5306 {
5307 if (*p == '\\' && p[1] != NUL)
5308 {
5309 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005310 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 else
5312 ++p;
5313 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005314 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 }
5316 return p;
5317}
5318
Bram Moolenaar86676c92018-04-05 18:56:48 +02005319#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005320 int
5321get_bad_opt(char_u *p, exarg_T *eap)
5322{
5323 if (STRICMP(p, "keep") == 0)
5324 eap->bad_char = BAD_KEEP;
5325 else if (STRICMP(p, "drop") == 0)
5326 eap->bad_char = BAD_DROP;
5327 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5328 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005329 else
5330 return FAIL;
5331 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005332}
Bram Moolenaar86676c92018-04-05 18:56:48 +02005333#endif
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005334
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335/*
5336 * Get "++opt=arg" argument.
5337 * Return FAIL or OK.
5338 */
5339 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005340getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341{
5342 char_u *arg = eap->arg + 2;
5343 int *pp = NULL;
5344#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005345 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 char_u *p;
5347#endif
5348
5349 /* ":edit ++[no]bin[ary] file" */
5350 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5351 {
5352 if (*arg == 'n')
5353 {
5354 arg += 2;
5355 eap->force_bin = FORCE_NOBIN;
5356 }
5357 else
5358 eap->force_bin = FORCE_BIN;
5359 if (!checkforcmd(&arg, "binary", 3))
5360 return FAIL;
5361 eap->arg = skipwhite(arg);
5362 return OK;
5363 }
5364
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005365 /* ":read ++edit file" */
5366 if (STRNCMP(arg, "edit", 4) == 0)
5367 {
5368 eap->read_edit = TRUE;
5369 eap->arg = skipwhite(arg + 4);
5370 return OK;
5371 }
5372
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 if (STRNCMP(arg, "ff", 2) == 0)
5374 {
5375 arg += 2;
5376 pp = &eap->force_ff;
5377 }
5378 else if (STRNCMP(arg, "fileformat", 10) == 0)
5379 {
5380 arg += 10;
5381 pp = &eap->force_ff;
5382 }
5383#ifdef FEAT_MBYTE
5384 else if (STRNCMP(arg, "enc", 3) == 0)
5385 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005386 if (STRNCMP(arg, "encoding", 8) == 0)
5387 arg += 8;
5388 else
5389 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 pp = &eap->force_enc;
5391 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005392 else if (STRNCMP(arg, "bad", 3) == 0)
5393 {
5394 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005395 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397#endif
5398
5399 if (pp == NULL || *arg != '=')
5400 return FAIL;
5401
5402 ++arg;
5403 *pp = (int)(arg - eap->cmd);
5404 arg = skip_cmd_arg(arg, FALSE);
5405 eap->arg = skipwhite(arg);
5406 *arg = NUL;
5407
5408#ifdef FEAT_MBYTE
5409 if (pp == &eap->force_ff)
5410 {
5411#endif
5412 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5413 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005414 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415#ifdef FEAT_MBYTE
5416 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005417 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {
5419 /* Make 'fileencoding' lower case. */
5420 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5421 *p = TOLOWER_ASC(*p);
5422 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005423 else
5424 {
5425 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5426 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005427 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005428 return FAIL;
5429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430#endif
5431
5432 return OK;
5433}
5434
5435/*
5436 * ":abbreviate" and friends.
5437 */
5438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005439ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440{
5441 do_exmap(eap, TRUE); /* almost the same as mapping */
5442}
5443
5444/*
5445 * ":map" and friends.
5446 */
5447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005448ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449{
5450 /*
5451 * If we are sourcing .exrc or .vimrc in current directory we
5452 * print the mappings for security reasons.
5453 */
5454 if (secure)
5455 {
5456 secure = 2;
5457 msg_outtrans(eap->cmd);
5458 msg_putchar('\n');
5459 }
5460 do_exmap(eap, FALSE);
5461}
5462
5463/*
5464 * ":unmap" and friends.
5465 */
5466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005467ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468{
5469 do_exmap(eap, FALSE);
5470}
5471
5472/*
5473 * ":mapclear" and friends.
5474 */
5475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005476ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
5478 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5479}
5480
5481/*
5482 * ":abclear" and friends.
5483 */
5484 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005485ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486{
5487 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5488}
5489
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005491ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
5493 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005494 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 * directory for security reasons.
5496 */
5497 if (secure)
5498 {
5499 secure = 2;
5500 eap->errmsg = e_curdir;
5501 }
5502 else if (eap->cmdidx == CMD_autocmd)
5503 do_autocmd(eap->arg, eap->forceit);
5504 else
5505 do_augroup(eap->arg, eap->forceit);
5506}
5507
5508/*
5509 * ":doautocmd": Apply the automatic commands to the current buffer.
5510 */
5511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005512ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005514 char_u *arg = eap->arg;
5515 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005516 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005517
Bram Moolenaar1610d052016-06-09 22:53:01 +02005518 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5519 /* Only when there is no <nomodeline>. */
5520 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005521 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524/*
5525 * :[N]bunload[!] [N] [bufname] unload buffer
5526 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5527 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5528 */
5529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005530ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
5532 eap->errmsg = do_bufdel(
5533 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5534 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5535 : DOBUF_UNLOAD, eap->arg,
5536 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5537}
5538
5539/*
5540 * :[N]buffer [N] to buffer N
5541 * :[N]sbuffer [N] to buffer N
5542 */
5543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005544ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
5546 if (*eap->arg)
5547 eap->errmsg = e_trailing;
5548 else
5549 {
5550 if (eap->addr_count == 0) /* default is current buffer */
5551 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5552 else
5553 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005554 if (eap->do_ecmd_cmd != NULL)
5555 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 }
5557}
5558
5559/*
5560 * :[N]bmodified [N] to next mod. buffer
5561 * :[N]sbmodified [N] to next mod. buffer
5562 */
5563 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005564ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565{
5566 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005567 if (eap->do_ecmd_cmd != NULL)
5568 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569}
5570
5571/*
5572 * :[N]bnext [N] to next buffer
5573 * :[N]sbnext [N] split and to next buffer
5574 */
5575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005576ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005579 if (eap->do_ecmd_cmd != NULL)
5580 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581}
5582
5583/*
5584 * :[N]bNext [N] to previous buffer
5585 * :[N]bprevious [N] to previous buffer
5586 * :[N]sbNext [N] split and to previous buffer
5587 * :[N]sbprevious [N] split and to previous buffer
5588 */
5589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005590ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005593 if (eap->do_ecmd_cmd != NULL)
5594 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595}
5596
5597/*
5598 * :brewind to first buffer
5599 * :bfirst to first buffer
5600 * :sbrewind split and to first buffer
5601 * :sbfirst split and to first buffer
5602 */
5603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005604ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605{
5606 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005607 if (eap->do_ecmd_cmd != NULL)
5608 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609}
5610
5611/*
5612 * :blast to last buffer
5613 * :sblast split and to last buffer
5614 */
5615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005616ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617{
5618 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005619 if (eap->do_ecmd_cmd != NULL)
5620 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622
5623 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005624ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625{
5626 return (c == NUL || c == '|' || c == '"' || c == '\n');
5627}
5628
5629#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5630 || defined(PROTO)
5631/*
5632 * Return the next command, after the first '|' or '\n'.
5633 * Return NULL if not found.
5634 */
5635 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005636find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 while (*p != '|' && *p != '\n')
5639 {
5640 if (*p == NUL)
5641 return NULL;
5642 ++p;
5643 }
5644 return (p + 1);
5645}
5646#endif
5647
5648/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005649 * Check if *p is a separator between Ex commands, skipping over white space.
5650 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 */
5652 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005653check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005655 char_u *s = skipwhite(p);
5656
5657 if (*s == '|' || *s == '\n')
5658 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 else
5660 return NULL;
5661}
5662
5663/*
5664 * - if there are more files to edit
5665 * - and this is the last window
5666 * - and forceit not used
5667 * - and not repeated twice on a row
5668 * return FAIL and give error message if 'message' TRUE
5669 * return OK otherwise
5670 */
5671 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005672check_more(
5673 int message, /* when FALSE check only, no messages */
5674 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675{
5676 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5677
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005678 if (!forceit && only_one_window()
5679 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 {
5681 if (message)
5682 {
5683#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5684 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5685 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005686 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687
5688 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005689 vim_strncpy(buff,
5690 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005691 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005693 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 _("%d more files to edit. Quit anyway?"), n);
5695 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5696 return OK;
5697 return FAIL;
5698 }
5699#endif
5700 if (n == 1)
5701 EMSG(_("E173: 1 more file to edit"));
5702 else
5703 EMSGN(_("E173: %ld more files to edit"), n);
5704 quitmore = 2; /* next try to quit is allowed */
5705 }
5706 return FAIL;
5707 }
5708 return OK;
5709}
5710
5711#ifdef FEAT_CMDL_COMPL
5712/*
5713 * Function given to ExpandGeneric() to obtain the list of command names.
5714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005716get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
5718 if (idx >= (int)CMD_SIZE)
5719# ifdef FEAT_USR_CMDS
5720 return get_user_command_name(idx);
5721# else
5722 return NULL;
5723# endif
5724 return cmdnames[idx].cmd_name;
5725}
5726#endif
5727
5728#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005729static 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);
5730static void uc_list(char_u *name, size_t name_len);
5731static 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);
5732static char_u *uc_split_args(char_u *arg, size_t *lenp);
5733static 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 +00005734
5735 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005736uc_add_command(
5737 char_u *name,
5738 size_t name_len,
5739 char_u *rep,
5740 long argt,
5741 long def,
5742 int flags,
5743 int compl,
5744 char_u *compl_arg,
5745 int addr_type,
5746 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
5748 ucmd_T *cmd = NULL;
5749 char_u *p;
5750 int i;
5751 int cmp = 1;
5752 char_u *rep_buf = NULL;
5753 garray_T *gap;
5754
Bram Moolenaar9c102382006-05-03 21:26:49 +00005755 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 if (rep_buf == NULL)
5757 {
5758 /* Can't replace termcodes - try using the string as is */
5759 rep_buf = vim_strsave(rep);
5760
5761 /* Give up if out of memory */
5762 if (rep_buf == NULL)
5763 return FAIL;
5764 }
5765
5766 /* get address of growarray: global or in curbuf */
5767 if (flags & UC_BUFFER)
5768 {
5769 gap = &curbuf->b_ucmds;
5770 if (gap->ga_itemsize == 0)
5771 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5772 }
5773 else
5774 gap = &ucmds;
5775
5776 /* Search for the command in the already defined commands. */
5777 for (i = 0; i < gap->ga_len; ++i)
5778 {
5779 size_t len;
5780
5781 cmd = USER_CMD_GA(gap, i);
5782 len = STRLEN(cmd->uc_name);
5783 cmp = STRNCMP(name, cmd->uc_name, name_len);
5784 if (cmp == 0)
5785 {
5786 if (name_len < len)
5787 cmp = -1;
5788 else if (name_len > len)
5789 cmp = 1;
5790 }
5791
5792 if (cmp == 0)
5793 {
5794 if (!force)
5795 {
5796 EMSG(_("E174: Command already exists: add ! to replace it"));
5797 goto fail;
5798 }
5799
Bram Moolenaard23a8232018-02-10 18:45:26 +01005800 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005801#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005802 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 break;
5805 }
5806
5807 /* Stop as soon as we pass the name to add */
5808 if (cmp < 0)
5809 break;
5810 }
5811
5812 /* Extend the array unless we're replacing an existing command */
5813 if (cmp != 0)
5814 {
5815 if (ga_grow(gap, 1) != OK)
5816 goto fail;
5817 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5818 goto fail;
5819
5820 cmd = USER_CMD_GA(gap, i);
5821 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5822
5823 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824
5825 cmd->uc_name = p;
5826 }
5827
5828 cmd->uc_rep = rep_buf;
5829 cmd->uc_argt = argt;
5830 cmd->uc_def = def;
5831 cmd->uc_compl = compl;
5832#ifdef FEAT_EVAL
5833 cmd->uc_scriptID = current_SID;
5834# ifdef FEAT_CMDL_COMPL
5835 cmd->uc_compl_arg = compl_arg;
5836# endif
5837#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005838 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839
5840 return OK;
5841
5842fail:
5843 vim_free(rep_buf);
5844#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5845 vim_free(compl_arg);
5846#endif
5847 return FAIL;
5848}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005851#if defined(FEAT_USR_CMDS)
5852static struct
5853{
5854 int expand;
5855 char *name;
5856} addr_type_complete[] =
5857{
5858 {ADDR_ARGUMENTS, "arguments"},
5859 {ADDR_LINES, "lines"},
5860 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5861 {ADDR_TABS, "tabs"},
5862 {ADDR_BUFFERS, "buffers"},
5863 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005864 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005865 {-1, NULL}
5866};
5867#endif
5868
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005869#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870/*
5871 * List of names for completion for ":command" with the EXPAND_ flag.
5872 * Must be alphabetical for completion.
5873 */
5874static struct
5875{
5876 int expand;
5877 char *name;
5878} command_complete[] =
5879{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005880 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005882 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005884 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005886 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005887#if defined(FEAT_CSCOPE)
5888 {EXPAND_CSCOPE, "cscope"},
5889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5891 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005892 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893#endif
5894 {EXPAND_DIRECTORIES, "dir"},
5895 {EXPAND_ENV_VARS, "environment"},
5896 {EXPAND_EVENTS, "event"},
5897 {EXPAND_EXPRESSION, "expression"},
5898 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005899 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005900 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 {EXPAND_FUNCTIONS, "function"},
5902 {EXPAND_HELP, "help"},
5903 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005904#if defined(FEAT_CMDHIST)
5905 {EXPAND_HISTORY, "history"},
5906#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005907#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005908 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005909 {EXPAND_LOCALES, "locale"},
5910#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005911 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 {EXPAND_MAPPINGS, "mapping"},
5913 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005914 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005915 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005916#if defined(FEAT_PROFILE)
5917 {EXPAND_SYNTIME, "syntime"},
5918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005920 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005921 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005922#if defined(FEAT_SIGNS)
5923 {EXPAND_SIGN, "sign"},
5924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {EXPAND_TAGS, "tag"},
5926 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005927 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {EXPAND_USER_VARS, "var"},
5929 {0, NULL}
5930};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005933#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005935uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
5937 int i, j;
5938 int found = FALSE;
5939 ucmd_T *cmd;
5940 int len;
5941 long a;
5942 garray_T *gap;
5943
5944 gap = &curbuf->b_ucmds;
5945 for (;;)
5946 {
5947 for (i = 0; i < gap->ga_len; ++i)
5948 {
5949 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005950 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951
Bram Moolenaard29459b2016-08-26 22:29:11 +02005952 /* Skip commands which don't match the requested prefix and
5953 * commands filtered out. */
5954 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5955 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 continue;
5957
5958 /* Put out the title first time */
5959 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005960 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 found = TRUE;
5962 msg_putchar('\n');
5963 if (got_int)
5964 break;
5965
5966 /* Special cases */
5967 msg_putchar(a & BANG ? '!' : ' ');
5968 msg_putchar(a & REGSTR ? '"' : ' ');
5969 msg_putchar(gap != &ucmds ? 'b' : ' ');
5970 msg_putchar(' ');
5971
Bram Moolenaar8820b482017-03-16 17:23:31 +01005972 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 len = (int)STRLEN(cmd->uc_name) + 4;
5974
5975 do {
5976 msg_putchar(' ');
5977 ++len;
5978 } while (len < 16);
5979
5980 len = 0;
5981
5982 /* Arguments */
5983 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5984 {
5985 case 0: IObuff[len++] = '0'; break;
5986 case (EXTRA): IObuff[len++] = '*'; break;
5987 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5988 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5989 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5990 }
5991
5992 do {
5993 IObuff[len++] = ' ';
5994 } while (len < 5);
5995
5996 /* Range */
5997 if (a & (RANGE|COUNT))
5998 {
5999 if (a & COUNT)
6000 {
6001 /* -count=N */
6002 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6003 len += (int)STRLEN(IObuff + len);
6004 }
6005 else if (a & DFLALL)
6006 IObuff[len++] = '%';
6007 else if (cmd->uc_def >= 0)
6008 {
6009 /* -range=N */
6010 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6011 len += (int)STRLEN(IObuff + len);
6012 }
6013 else
6014 IObuff[len++] = '.';
6015 }
6016
6017 do {
6018 IObuff[len++] = ' ';
6019 } while (len < 11);
6020
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006021 /* Address Type */
6022 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6023 if (addr_type_complete[j].expand != ADDR_LINES
6024 && addr_type_complete[j].expand == cmd->uc_addr_type)
6025 {
6026 STRCPY(IObuff + len, addr_type_complete[j].name);
6027 len += (int)STRLEN(IObuff + len);
6028 break;
6029 }
6030
6031 do {
6032 IObuff[len++] = ' ';
6033 } while (len < 21);
6034
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 /* Completion */
6036 for (j = 0; command_complete[j].expand != 0; ++j)
6037 if (command_complete[j].expand == cmd->uc_compl)
6038 {
6039 STRCPY(IObuff + len, command_complete[j].name);
6040 len += (int)STRLEN(IObuff + len);
6041 break;
6042 }
6043
6044 do {
6045 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006046 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047
6048 IObuff[len] = '\0';
6049 msg_outtrans(IObuff);
6050
6051 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006052#ifdef FEAT_EVAL
6053 if (p_verbose > 0)
6054 last_set_msg(cmd->uc_scriptID);
6055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 out_flush();
6057 ui_breakcheck();
6058 if (got_int)
6059 break;
6060 }
6061 if (gap == &ucmds || i < gap->ga_len)
6062 break;
6063 gap = &ucmds;
6064 }
6065
6066 if (!found)
6067 MSG(_("No user-defined commands found"));
6068}
6069
6070 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006071uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072{
6073 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6074 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6075 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6076 0xb9, 0x7f, 0};
6077 int i;
6078
6079 for (i = 0; fcmd[i]; ++i)
6080 IObuff[i] = fcmd[i] - 0x40;
6081 IObuff[i] = 0;
6082 return IObuff;
6083}
6084
6085 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006086uc_scan_attr(
6087 char_u *attr,
6088 size_t len,
6089 long *argt,
6090 long *def,
6091 int *flags,
6092 int *compl,
6093 char_u **compl_arg,
6094 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095{
6096 char_u *p;
6097
6098 if (len == 0)
6099 {
6100 EMSG(_("E175: No attribute specified"));
6101 return FAIL;
6102 }
6103
6104 /* First, try the simple attributes (no arguments) */
6105 if (STRNICMP(attr, "bang", len) == 0)
6106 *argt |= BANG;
6107 else if (STRNICMP(attr, "buffer", len) == 0)
6108 *flags |= UC_BUFFER;
6109 else if (STRNICMP(attr, "register", len) == 0)
6110 *argt |= REGSTR;
6111 else if (STRNICMP(attr, "bar", len) == 0)
6112 *argt |= TRLBAR;
6113 else
6114 {
6115 int i;
6116 char_u *val = NULL;
6117 size_t vallen = 0;
6118 size_t attrlen = len;
6119
6120 /* Look for the attribute name - which is the part before any '=' */
6121 for (i = 0; i < (int)len; ++i)
6122 {
6123 if (attr[i] == '=')
6124 {
6125 val = &attr[i + 1];
6126 vallen = len - i - 1;
6127 attrlen = i;
6128 break;
6129 }
6130 }
6131
6132 if (STRNICMP(attr, "nargs", attrlen) == 0)
6133 {
6134 if (vallen == 1)
6135 {
6136 if (*val == '0')
6137 /* Do nothing - this is the default */;
6138 else if (*val == '1')
6139 *argt |= (EXTRA | NOSPC | NEEDARG);
6140 else if (*val == '*')
6141 *argt |= EXTRA;
6142 else if (*val == '?')
6143 *argt |= (EXTRA | NOSPC);
6144 else if (*val == '+')
6145 *argt |= (EXTRA | NEEDARG);
6146 else
6147 goto wrong_nargs;
6148 }
6149 else
6150 {
6151wrong_nargs:
6152 EMSG(_("E176: Invalid number of arguments"));
6153 return FAIL;
6154 }
6155 }
6156 else if (STRNICMP(attr, "range", attrlen) == 0)
6157 {
6158 *argt |= RANGE;
6159 if (vallen == 1 && *val == '%')
6160 *argt |= DFLALL;
6161 else if (val != NULL)
6162 {
6163 p = val;
6164 if (*def >= 0)
6165 {
6166two_count:
6167 EMSG(_("E177: Count cannot be specified twice"));
6168 return FAIL;
6169 }
6170
6171 *def = getdigits(&p);
6172 *argt |= (ZEROR | NOTADR);
6173
6174 if (p != val + vallen || vallen == 0)
6175 {
6176invalid_count:
6177 EMSG(_("E178: Invalid default value for count"));
6178 return FAIL;
6179 }
6180 }
6181 }
6182 else if (STRNICMP(attr, "count", attrlen) == 0)
6183 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006184 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
6186 if (val != NULL)
6187 {
6188 p = val;
6189 if (*def >= 0)
6190 goto two_count;
6191
6192 *def = getdigits(&p);
6193
6194 if (p != val + vallen)
6195 goto invalid_count;
6196 }
6197
6198 if (*def < 0)
6199 *def = 0;
6200 }
6201 else if (STRNICMP(attr, "complete", attrlen) == 0)
6202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 if (val == NULL)
6204 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006205 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 return FAIL;
6207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006209 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6210 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006213 else if (STRNICMP(attr, "addr", attrlen) == 0)
6214 {
6215 *argt |= RANGE;
6216 if (val == NULL)
6217 {
6218 EMSG(_("E179: argument required for -addr"));
6219 return FAIL;
6220 }
6221 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6222 == FAIL)
6223 return FAIL;
6224 if (addr_type_arg != ADDR_LINES)
6225 *argt |= (ZEROR | NOTADR) ;
6226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 else
6228 {
6229 char_u ch = attr[len];
6230 attr[len] = '\0';
6231 EMSG2(_("E181: Invalid attribute: %s"), attr);
6232 attr[len] = ch;
6233 return FAIL;
6234 }
6235 }
6236
6237 return OK;
6238}
6239
Bram Moolenaara850a712009-01-28 14:42:59 +00006240/*
6241 * ":command ..."
6242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006244ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245{
6246 char_u *name;
6247 char_u *end;
6248 char_u *p;
6249 long argt = 0;
6250 long def = -1;
6251 int flags = 0;
6252 int compl = EXPAND_NOTHING;
6253 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006254 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006256 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
6258 p = eap->arg;
6259
6260 /* Check for attributes */
6261 while (*p == '-')
6262 {
6263 ++p;
6264 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006265 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6266 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 == FAIL)
6268 return;
6269 p = skipwhite(end);
6270 }
6271
6272 /* Get the name (if any) and skip to the following argument */
6273 name = p;
6274 if (ASCII_ISALPHA(*p))
6275 while (ASCII_ISALNUM(*p))
6276 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006277 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 {
6279 EMSG(_("E182: Invalid command name"));
6280 return;
6281 }
6282 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006283 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284
6285 /* If there is nothing after the name, and no attributes were specified,
6286 * we are listing commands
6287 */
6288 p = skipwhite(end);
6289 if (!has_attr && ends_excmd(*p))
6290 {
6291 uc_list(name, end - name);
6292 }
6293 else if (!ASCII_ISUPPER(*name))
6294 {
6295 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6296 return;
6297 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006298 else if ((name_len == 1 && *name == 'X')
6299 || (name_len <= 4
6300 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6301 {
6302 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6303 return;
6304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 else
6306 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006307 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308}
6309
6310/*
6311 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 * Clear all user commands, global and for current buffer.
6313 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006314 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006315ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 uc_clear(&ucmds);
6318 uc_clear(&curbuf->b_ucmds);
6319}
6320
6321/*
6322 * Clear all user commands for "gap".
6323 */
6324 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006325uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 int i;
6328 ucmd_T *cmd;
6329
6330 for (i = 0; i < gap->ga_len; ++i)
6331 {
6332 cmd = USER_CMD_GA(gap, i);
6333 vim_free(cmd->uc_name);
6334 vim_free(cmd->uc_rep);
6335# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6336 vim_free(cmd->uc_compl_arg);
6337# endif
6338 }
6339 ga_clear(gap);
6340}
6341
6342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006343ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344{
6345 int i = 0;
6346 ucmd_T *cmd = NULL;
6347 int cmp = -1;
6348 garray_T *gap;
6349
6350 gap = &curbuf->b_ucmds;
6351 for (;;)
6352 {
6353 for (i = 0; i < gap->ga_len; ++i)
6354 {
6355 cmd = USER_CMD_GA(gap, i);
6356 cmp = STRCMP(eap->arg, cmd->uc_name);
6357 if (cmp <= 0)
6358 break;
6359 }
6360 if (gap == &ucmds || cmp == 0)
6361 break;
6362 gap = &ucmds;
6363 }
6364
6365 if (cmp != 0)
6366 {
6367 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6368 return;
6369 }
6370
6371 vim_free(cmd->uc_name);
6372 vim_free(cmd->uc_rep);
6373# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6374 vim_free(cmd->uc_compl_arg);
6375# endif
6376
6377 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378
6379 if (i < gap->ga_len)
6380 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6381}
6382
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006383/*
6384 * split and quote args for <f-args>
6385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006387uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 char_u *buf;
6390 char_u *p;
6391 char_u *q;
6392 int len;
6393
6394 /* Precalculate length */
6395 p = arg;
6396 len = 2; /* Initial and final quotes */
6397
6398 while (*p)
6399 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006400 if (p[0] == '\\' && p[1] == '\\')
6401 {
6402 len += 2;
6403 p += 2;
6404 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006405 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 {
6407 len += 1;
6408 p += 2;
6409 }
6410 else if (*p == '\\' || *p == '"')
6411 {
6412 len += 2;
6413 p += 1;
6414 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006415 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 {
6417 p = skipwhite(p);
6418 if (*p == NUL)
6419 break;
6420 len += 3; /* "," */
6421 }
6422 else
6423 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006424#ifdef FEAT_MBYTE
6425 int charlen = (*mb_ptr2len)(p);
6426 len += charlen;
6427 p += charlen;
6428#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 ++len;
6430 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 }
6433 }
6434
6435 buf = alloc(len + 1);
6436 if (buf == NULL)
6437 {
6438 *lenp = 0;
6439 return buf;
6440 }
6441
6442 p = arg;
6443 q = buf;
6444 *q++ = '"';
6445 while (*p)
6446 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006447 if (p[0] == '\\' && p[1] == '\\')
6448 {
6449 *q++ = '\\';
6450 *q++ = '\\';
6451 p += 2;
6452 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006453 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 {
6455 *q++ = p[1];
6456 p += 2;
6457 }
6458 else if (*p == '\\' || *p == '"')
6459 {
6460 *q++ = '\\';
6461 *q++ = *p++;
6462 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006463 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 {
6465 p = skipwhite(p);
6466 if (*p == NUL)
6467 break;
6468 *q++ = '"';
6469 *q++ = ',';
6470 *q++ = '"';
6471 }
6472 else
6473 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006474 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 }
6476 }
6477 *q++ = '"';
6478 *q = 0;
6479
6480 *lenp = len;
6481 return buf;
6482}
6483
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006484 static size_t
6485add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6486{
6487 size_t result;
6488
6489 result = STRLEN(mod_str);
6490 if (*multi_mods)
6491 result += 1;
6492 if (buf != NULL)
6493 {
6494 if (*multi_mods)
6495 STRCAT(buf, " ");
6496 STRCAT(buf, mod_str);
6497 }
6498
6499 *multi_mods = 1;
6500
6501 return result;
6502}
6503
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504/*
6505 * Check for a <> code in a user command.
6506 * "code" points to the '<'. "len" the length of the <> (inclusive).
6507 * "buf" is where the result is to be added.
6508 * "split_buf" points to a buffer used for splitting, caller should free it.
6509 * "split_len" is the length of what "split_buf" contains.
6510 * Returns the length of the replacement, which has been added to "buf".
6511 * Returns -1 if there was no match, and only the "<" has been copied.
6512 */
6513 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006514uc_check_code(
6515 char_u *code,
6516 size_t len,
6517 char_u *buf,
6518 ucmd_T *cmd, /* the user command we're expanding */
6519 exarg_T *eap, /* ex arguments */
6520 char_u **split_buf,
6521 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522{
6523 size_t result = 0;
6524 char_u *p = code + 1;
6525 size_t l = len - 2;
6526 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006527 enum {
6528 ct_ARGS,
6529 ct_BANG,
6530 ct_COUNT,
6531 ct_LINE1,
6532 ct_LINE2,
6533 ct_RANGE,
6534 ct_MODS,
6535 ct_REGISTER,
6536 ct_LT,
6537 ct_NONE
6538 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
6540 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6541 {
6542 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6543 p += 2;
6544 l -= 2;
6545 }
6546
Bram Moolenaar371d5402006-03-20 21:47:49 +00006547 ++l;
6548 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006550 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006552 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006554 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006556 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006558 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006560 else if (STRNICMP(p, "range>", l) == 0)
6561 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006562 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006564 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006566 else if (STRNICMP(p, "mods>", l) == 0)
6567 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568
6569 switch (type)
6570 {
6571 case ct_ARGS:
6572 /* Simple case first */
6573 if (*eap->arg == NUL)
6574 {
6575 if (quote == 1)
6576 {
6577 result = 2;
6578 if (buf != NULL)
6579 STRCPY(buf, "''");
6580 }
6581 else
6582 result = 0;
6583 break;
6584 }
6585
6586 /* When specified there is a single argument don't split it.
6587 * Works for ":Cmd %" when % is "a b c". */
6588 if ((eap->argt & NOSPC) && quote == 2)
6589 quote = 1;
6590
6591 switch (quote)
6592 {
6593 case 0: /* No quoting, no splitting */
6594 result = STRLEN(eap->arg);
6595 if (buf != NULL)
6596 STRCPY(buf, eap->arg);
6597 break;
6598 case 1: /* Quote, but don't split */
6599 result = STRLEN(eap->arg) + 2;
6600 for (p = eap->arg; *p; ++p)
6601 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006602#ifdef FEAT_MBYTE
6603 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6604 /* DBCS can contain \ in a trail byte, skip the
6605 * double-byte character. */
6606 ++p;
6607 else
6608#endif
6609 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 ++result;
6611 }
6612
6613 if (buf != NULL)
6614 {
6615 *buf++ = '"';
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, copy the
6621 * double-byte character to avoid escaping. */
6622 *buf++ = *p++;
6623 else
6624#endif
6625 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 *buf++ = '\\';
6627 *buf++ = *p;
6628 }
6629 *buf = '"';
6630 }
6631
6632 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006633 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 /* This is hard, so only do it once, and cache the result */
6635 if (*split_buf == NULL)
6636 *split_buf = uc_split_args(eap->arg, split_len);
6637
6638 result = *split_len;
6639 if (buf != NULL && result != 0)
6640 STRCPY(buf, *split_buf);
6641
6642 break;
6643 }
6644 break;
6645
6646 case ct_BANG:
6647 result = eap->forceit ? 1 : 0;
6648 if (quote)
6649 result += 2;
6650 if (buf != NULL)
6651 {
6652 if (quote)
6653 *buf++ = '"';
6654 if (eap->forceit)
6655 *buf++ = '!';
6656 if (quote)
6657 *buf = '"';
6658 }
6659 break;
6660
6661 case ct_LINE1:
6662 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006663 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 case ct_COUNT:
6665 {
6666 char num_buf[20];
6667 long num = (type == ct_LINE1) ? eap->line1 :
6668 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006669 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6671 size_t num_len;
6672
6673 sprintf(num_buf, "%ld", num);
6674 num_len = STRLEN(num_buf);
6675 result = num_len;
6676
6677 if (quote)
6678 result += 2;
6679
6680 if (buf != NULL)
6681 {
6682 if (quote)
6683 *buf++ = '"';
6684 STRCPY(buf, num_buf);
6685 buf += num_len;
6686 if (quote)
6687 *buf = '"';
6688 }
6689
6690 break;
6691 }
6692
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006693 case ct_MODS:
6694 {
6695 int multi_mods = 0;
6696 typedef struct {
6697 int *varp;
6698 char *name;
6699 } mod_entry_T;
6700 static mod_entry_T mod_entries[] = {
6701#ifdef FEAT_BROWSE_CMD
6702 {&cmdmod.browse, "browse"},
6703#endif
6704#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6705 {&cmdmod.confirm, "confirm"},
6706#endif
6707 {&cmdmod.hide, "hide"},
6708 {&cmdmod.keepalt, "keepalt"},
6709 {&cmdmod.keepjumps, "keepjumps"},
6710 {&cmdmod.keepmarks, "keepmarks"},
6711 {&cmdmod.keeppatterns, "keeppatterns"},
6712 {&cmdmod.lockmarks, "lockmarks"},
6713 {&cmdmod.noswapfile, "noswapfile"},
6714 {NULL, NULL}
6715 };
6716 int i;
6717
6718 result = quote ? 2 : 0;
6719 if (buf != NULL)
6720 {
6721 if (quote)
6722 *buf++ = '"';
6723 *buf = '\0';
6724 }
6725
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006726 /* :aboveleft and :leftabove */
6727 if (cmdmod.split & WSP_ABOVE)
6728 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6729 /* :belowright and :rightbelow */
6730 if (cmdmod.split & WSP_BELOW)
6731 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6732 /* :botright */
6733 if (cmdmod.split & WSP_BOT)
6734 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006735
6736 /* the modifiers that are simple flags */
6737 for (i = 0; mod_entries[i].varp != NULL; ++i)
6738 if (*mod_entries[i].varp)
6739 result += add_cmd_modifier(buf, mod_entries[i].name,
6740 &multi_mods);
6741
6742 /* TODO: How to support :noautocmd? */
6743#ifdef HAVE_SANDBOX
6744 /* TODO: How to support :sandbox?*/
6745#endif
6746 /* :silent */
6747 if (msg_silent > 0)
6748 result += add_cmd_modifier(buf,
6749 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006750 /* :tab */
6751 if (cmdmod.tab > 0)
6752 result += add_cmd_modifier(buf, "tab", &multi_mods);
6753 /* :topleft */
6754 if (cmdmod.split & WSP_TOP)
6755 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006756 /* TODO: How to support :unsilent?*/
6757 /* :verbose */
6758 if (p_verbose > 0)
6759 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006760 /* :vertical */
6761 if (cmdmod.split & WSP_VERT)
6762 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006763 if (quote && buf != NULL)
6764 {
6765 buf += result - 2;
6766 *buf = '"';
6767 }
6768 break;
6769 }
6770
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 case ct_REGISTER:
6772 result = eap->regname ? 1 : 0;
6773 if (quote)
6774 result += 2;
6775 if (buf != NULL)
6776 {
6777 if (quote)
6778 *buf++ = '\'';
6779 if (eap->regname)
6780 *buf++ = eap->regname;
6781 if (quote)
6782 *buf = '\'';
6783 }
6784 break;
6785
6786 case ct_LT:
6787 result = 1;
6788 if (buf != NULL)
6789 *buf = '<';
6790 break;
6791
6792 default:
6793 /* Not recognized: just copy the '<' and return -1. */
6794 result = (size_t)-1;
6795 if (buf != NULL)
6796 *buf = '<';
6797 break;
6798 }
6799
6800 return result;
6801}
6802
6803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006804do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805{
6806 char_u *buf;
6807 char_u *p;
6808 char_u *q;
6809
6810 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006811 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006812 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 size_t len, totlen;
6814
6815 size_t split_len = 0;
6816 char_u *split_buf = NULL;
6817 ucmd_T *cmd;
6818#ifdef FEAT_EVAL
6819 scid_T save_current_SID = current_SID;
6820#endif
6821
6822 if (eap->cmdidx == CMD_USER)
6823 cmd = USER_CMD(eap->useridx);
6824 else
6825 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6826
6827 /*
6828 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006829 * First round: "buf" is NULL, compute length, allocate "buf".
6830 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 */
6832 buf = NULL;
6833 for (;;)
6834 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006835 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006836 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006838
6839 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006841 start = vim_strchr(p, '<');
6842 if (start != NULL)
6843 end = vim_strchr(start + 1, '>');
6844 if (buf != NULL)
6845 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006846 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6847 ;
6848 if (*ksp == K_SPECIAL
6849 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006850 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6851# ifdef FEAT_GUI
6852 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6853# endif
6854 ))
6855 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006856 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006857 * KS_SPECIAL KE_FILLER, like for mappings, but
6858 * do_cmdline() doesn't handle that, so convert it back.
6859 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6860 len = ksp - p;
6861 if (len > 0)
6862 {
6863 mch_memmove(q, p, len);
6864 q += len;
6865 }
6866 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6867 p = ksp + 3;
6868 continue;
6869 }
6870 }
6871
6872 /* break if there no <item> is found */
6873 if (start == NULL || end == NULL)
6874 break;
6875
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 /* Include the '>' */
6877 ++end;
6878
6879 /* Take everything up to the '<' */
6880 len = start - p;
6881 if (buf == NULL)
6882 totlen += len;
6883 else
6884 {
6885 mch_memmove(q, p, len);
6886 q += len;
6887 }
6888
6889 len = uc_check_code(start, end - start, q, cmd, eap,
6890 &split_buf, &split_len);
6891 if (len == (size_t)-1)
6892 {
6893 /* no match, continue after '<' */
6894 p = start + 1;
6895 len = 1;
6896 }
6897 else
6898 p = end;
6899 if (buf == NULL)
6900 totlen += len;
6901 else
6902 q += len;
6903 }
6904 if (buf != NULL) /* second time here, finished */
6905 {
6906 STRCPY(q, p);
6907 break;
6908 }
6909
6910 totlen += STRLEN(p); /* Add on the trailing characters */
6911 buf = alloc((unsigned)(totlen + 1));
6912 if (buf == NULL)
6913 {
6914 vim_free(split_buf);
6915 return;
6916 }
6917 }
6918
6919#ifdef FEAT_EVAL
6920 current_SID = cmd->uc_scriptID;
6921#endif
6922 (void)do_cmdline(buf, eap->getline, eap->cookie,
6923 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6924#ifdef FEAT_EVAL
6925 current_SID = save_current_SID;
6926#endif
6927 vim_free(buf);
6928 vim_free(split_buf);
6929}
6930
6931# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6932 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006933get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934{
6935 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6936}
6937
6938/*
6939 * Function given to ExpandGeneric() to obtain the list of user command names.
6940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006942get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943{
6944 if (idx < curbuf->b_ucmds.ga_len)
6945 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6946 idx -= curbuf->b_ucmds.ga_len;
6947 if (idx < ucmds.ga_len)
6948 return USER_CMD(idx)->uc_name;
6949 return NULL;
6950}
6951
6952/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006953 * Function given to ExpandGeneric() to obtain the list of user address type names.
6954 */
6955 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006956get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006957{
6958 return (char_u *)addr_type_complete[idx].name;
6959}
6960
6961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 * Function given to ExpandGeneric() to obtain the list of user command
6963 * attributes.
6964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006966get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
6968 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006969 {"addr", "bang", "bar", "buffer", "complete",
6970 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006972 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 return NULL;
6974 return (char_u *)user_cmd_flags[idx];
6975}
6976
6977/*
6978 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006981get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982{
6983 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6984
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006985 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 return NULL;
6987 return (char_u *)user_cmd_nargs[idx];
6988}
6989
6990/*
6991 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006994get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
6996 return (char_u *)command_complete[idx].name;
6997}
6998# endif /* FEAT_CMDL_COMPL */
6999
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007000/*
7001 * Parse address type argument
7002 */
7003 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007004parse_addr_type_arg(
7005 char_u *value,
7006 int vallen,
7007 long *argt,
7008 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007009{
7010 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007011
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007012 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7013 {
7014 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7015 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7016 if (a && b)
7017 {
7018 *addr_type_arg = addr_type_complete[i].expand;
7019 break;
7020 }
7021 }
7022
7023 if (addr_type_complete[i].expand == -1)
7024 {
7025 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007026
Bram Moolenaar1c465442017-03-12 20:10:05 +01007027 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007028 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007029 err[i] = NUL;
7030 EMSG2(_("E180: Invalid address type value: %s"), err);
7031 return FAIL;
7032 }
7033
7034 if (*addr_type_arg != ADDR_LINES)
7035 *argt |= NOTADR;
7036
7037 return OK;
7038}
7039
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040#endif /* FEAT_USR_CMDS */
7041
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007042#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7043/*
7044 * Parse a completion argument "value[vallen]".
7045 * The detected completion goes in "*complp", argument type in "*argt".
7046 * When there is an argument, for function and user defined completion, it's
7047 * copied to allocated memory and stored in "*compl_arg".
7048 * Returns FAIL if something is wrong.
7049 */
7050 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007051parse_compl_arg(
7052 char_u *value,
7053 int vallen,
7054 int *complp,
7055 long *argt,
7056 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007057{
7058 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007059# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007060 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007061# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007062 int i;
7063 int valend = vallen;
7064
7065 /* Look for any argument part - which is the part after any ',' */
7066 for (i = 0; i < vallen; ++i)
7067 {
7068 if (value[i] == ',')
7069 {
7070 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007071# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007072 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007073# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007074 valend = i;
7075 break;
7076 }
7077 }
7078
7079 for (i = 0; command_complete[i].expand != 0; ++i)
7080 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007081 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007082 && STRNCMP(value, command_complete[i].name, valend) == 0)
7083 {
7084 *complp = command_complete[i].expand;
7085 if (command_complete[i].expand == EXPAND_BUFFERS)
7086 *argt |= BUFNAME;
7087 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7088 || command_complete[i].expand == EXPAND_FILES)
7089 *argt |= XFILE;
7090 break;
7091 }
7092 }
7093
7094 if (command_complete[i].expand == 0)
7095 {
7096 EMSG2(_("E180: Invalid complete value: %s"), value);
7097 return FAIL;
7098 }
7099
7100# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7101 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7102 && arg != NULL)
7103# else
7104 if (arg != NULL)
7105# endif
7106 {
7107 EMSG(_("E468: Completion argument only allowed for custom completion"));
7108 return FAIL;
7109 }
7110
7111# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7112 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7113 && arg == NULL)
7114 {
7115 EMSG(_("E467: Custom completion requires a function argument"));
7116 return FAIL;
7117 }
7118
7119 if (arg != NULL)
7120 *compl_arg = vim_strnsave(arg, (int)arglen);
7121# endif
7122 return OK;
7123}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007124
7125 int
7126cmdcomplete_str_to_type(char_u *complete_str)
7127{
7128 int i;
7129
7130 for (i = 0; command_complete[i].expand != 0; ++i)
7131 if (STRCMP(complete_str, command_complete[i].name) == 0)
7132 return command_complete[i].expand;
7133
7134 return EXPAND_NOTHING;
7135}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007136#endif
7137
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007139ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140{
Bram Moolenaare6850792010-05-14 15:28:44 +02007141 if (*eap->arg == NUL)
7142 {
7143#ifdef FEAT_EVAL
7144 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7145 char_u *p = NULL;
7146
7147 if (expr != NULL)
7148 {
7149 ++emsg_off;
7150 p = eval_to_string(expr, NULL, FALSE);
7151 --emsg_off;
7152 vim_free(expr);
7153 }
7154 if (p != NULL)
7155 {
7156 MSG(p);
7157 vim_free(p);
7158 }
7159 else
7160 MSG("default");
7161#else
7162 MSG(_("unknown"));
7163#endif
7164 }
7165 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007166 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167}
7168
7169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007170ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171{
7172 if (*eap->arg == NUL && eap->cmd[2] == '!')
7173 MSG(_("Greetings, Vim user!"));
7174 do_highlight(eap->arg, eap->forceit, FALSE);
7175}
7176
7177
7178/*
7179 * Call this function if we thought we were going to exit, but we won't
7180 * (because of an error). May need to restore the terminal mode.
7181 */
7182 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007183not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
7185 exiting = FALSE;
7186 settmode(TMODE_RAW);
7187}
7188
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007189 static int
7190before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7191{
7192 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7193
7194 /* Bail out when autocommands closed the window.
7195 * Refuse to quit when the buffer in the last window is being closed (can
7196 * only happen in autocommands). */
7197 if (!win_valid(wp)
7198 || curbuf_locked()
7199 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7200 return TRUE;
7201
7202 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7203 {
7204 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7205 /* Refuse to quit when locked or when the buffer in the last window is
7206 * being closed (can only happen in autocommands). */
7207 if (curbuf_locked()
7208 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7209 return TRUE;
7210 }
7211
7212 return FALSE;
7213}
7214
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007216 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007217 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 */
7219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007220ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007222 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007223
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224#ifdef FEAT_CMDWIN
7225 if (cmdwin_type != 0)
7226 {
7227 cmdwin_result = Ctrl_C;
7228 return;
7229 }
7230#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007231 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007232 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007233 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007234 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007235 return;
7236 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007237 if (eap->addr_count > 0)
7238 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007239 int wnr = eap->line2;
7240
7241 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7242 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007243 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007244 }
7245 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007246 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007247
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007248 /* Refuse to quit when locked. */
7249 if (curbuf_locked())
7250 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007251
7252 /* Trigger QuitPre and maybe ExitPre */
7253 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007254 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255
7256#ifdef FEAT_NETBEANS_INTG
7257 netbeansForcedQuit = eap->forceit;
7258#endif
7259
7260 /*
7261 * If there are more files or windows we won't exit.
7262 */
7263 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7264 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007265 if ((!buf_hide(wp->w_buffer)
7266 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007267 | (eap->forceit ? CCGD_FORCEIT : 0)
7268 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007270 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 {
7272 not_exiting();
7273 }
7274 else
7275 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007276 /* quit last window
7277 * Note: only_one_window() returns true, even so a help window is
7278 * still open. In that case only quit, if no address has been
7279 * specified. Example:
7280 * :h|wincmd w|1q - don't quit
7281 * :h|wincmd w|q - quit
7282 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007283 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007285 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007286#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007290 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 }
7292}
7293
7294/*
7295 * ":cquit".
7296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007298ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299{
7300 getout(1); /* this does not always pass on the exit code to the Manx
7301 compiler. why? */
7302}
7303
7304/*
7305 * ":qall": try to quit all windows
7306 */
7307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007308ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309{
7310# ifdef FEAT_CMDWIN
7311 if (cmdwin_type != 0)
7312 {
7313 if (eap->forceit)
7314 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7315 else
7316 cmdwin_result = K_XF2;
7317 return;
7318 }
7319# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007320
7321 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007322 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007323 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007324 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007325 return;
7326 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007327
7328 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007329 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007332 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 getout(0);
7334 not_exiting();
7335}
7336
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337/*
7338 * ":close": close current window, unless it is the last one
7339 */
7340 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007341ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007343 win_T *win;
7344 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007345#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007347 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007349#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007350 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007351 {
7352 if (eap->addr_count == 0)
7353 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007354 else
7355 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007356 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007357 {
7358 winnr++;
7359 if (winnr == eap->line2)
7360 break;
7361 }
7362 if (win == NULL)
7363 win = lastwin;
7364 ex_win_close(eap->forceit, win, NULL);
7365 }
7366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367}
7368
Bram Moolenaar4033c552017-09-16 20:54:51 +02007369#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007370/*
7371 * ":pclose": Close any preview window.
7372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007374ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007375{
7376 win_T *win;
7377
Bram Moolenaar29323592016-07-24 22:04:11 +02007378 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007379 if (win->w_p_pvw)
7380 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007381 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007382 break;
7383 }
7384}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007385#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007386
Bram Moolenaarf740b292006-02-16 22:11:02 +00007387/*
7388 * Close window "win" and take care of handling closing the last window for a
7389 * modified buffer.
7390 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007392ex_win_close(
7393 int forceit,
7394 win_T *win,
7395 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396{
7397 int need_hide;
7398 buf_T *buf = win->w_buffer;
7399
7400 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007401 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007403#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 if ((p_confirm || cmdmod.confirm) && p_write)
7405 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007406 bufref_T bufref;
7407
7408 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007410 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 return;
7412 need_hide = FALSE;
7413 }
7414 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007417 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 return;
7419 }
7420 }
7421
Bram Moolenaar4033c552017-09-16 20:54:51 +02007422#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007424#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007425
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007427 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007428 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007429 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007430 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431}
7432
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007434 * Handle the argument for a tabpage related ex command.
7435 * Returns a tabpage number.
7436 * When an error is encountered then eap->errmsg is set.
7437 */
7438 static int
7439get_tabpage_arg(exarg_T *eap)
7440{
7441 int tab_number;
7442 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7443
7444 if (eap->arg && *eap->arg != NUL)
7445 {
7446 char_u *p = eap->arg;
7447 char_u *p_save;
7448 int relative = 0; /* argument +N/-N means: go to N places to the
7449 * right/left relative to the current position. */
7450
7451 if (*p == '-')
7452 {
7453 relative = -1;
7454 p++;
7455 }
7456 else if (*p == '+')
7457 {
7458 relative = 1;
7459 p++;
7460 }
7461
7462 p_save = p;
7463 tab_number = getdigits(&p);
7464
7465 if (relative == 0)
7466 {
7467 if (STRCMP(p, "$") == 0)
7468 tab_number = LAST_TAB_NR;
7469 else if (p == p_save || *p_save == '-' || *p != NUL
7470 || tab_number > LAST_TAB_NR)
7471 {
7472 /* No numbers as argument. */
7473 eap->errmsg = e_invarg;
7474 goto theend;
7475 }
7476 }
7477 else
7478 {
7479 if (*p_save == NUL)
7480 tab_number = 1;
7481 else if (p == p_save || *p_save == '-' || *p != NUL
7482 || tab_number == 0)
7483 {
7484 /* No numbers as argument. */
7485 eap->errmsg = e_invarg;
7486 goto theend;
7487 }
7488 tab_number = tab_number * relative + tabpage_index(curtab);
7489 if (!unaccept_arg0 && relative == -1)
7490 --tab_number;
7491 }
7492 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7493 eap->errmsg = e_invarg;
7494 }
7495 else if (eap->addr_count > 0)
7496 {
7497 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007498 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007499 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007500 tab_number = 0;
7501 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007502 else
7503 {
7504 tab_number = eap->line2;
7505 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7506 {
7507 --tab_number;
7508 if (tab_number < unaccept_arg0)
7509 eap->errmsg = e_invarg;
7510 }
7511 }
7512 }
7513 else
7514 {
7515 switch (eap->cmdidx)
7516 {
7517 case CMD_tabnext:
7518 tab_number = tabpage_index(curtab) + 1;
7519 if (tab_number > LAST_TAB_NR)
7520 tab_number = 1;
7521 break;
7522 case CMD_tabmove:
7523 tab_number = LAST_TAB_NR;
7524 break;
7525 default:
7526 tab_number = tabpage_index(curtab);
7527 }
7528 }
7529
7530theend:
7531 return tab_number;
7532}
7533
7534/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007535 * ":tabclose": close current tab page, unless it is the last one.
7536 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 */
7538 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007539ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007541 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007542 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007543
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007544# ifdef FEAT_CMDWIN
7545 if (cmdwin_type != 0)
7546 cmdwin_result = K_IGNORE;
7547 else
7548# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007549 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007550 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007551 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007553 tab_number = get_tabpage_arg(eap);
7554 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007555 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007556 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007557 if (tp == NULL)
7558 {
7559 beep_flush();
7560 return;
7561 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007562 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007563 {
7564 tabpage_close_other(tp, eap->forceit);
7565 return;
7566 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007567 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007568 tabpage_close(eap->forceit);
7569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007571}
7572
7573/*
7574 * ":tabonly": close all tab pages except the current one
7575 */
7576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007577ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007578{
7579 tabpage_T *tp;
7580 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007581 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007582
7583# ifdef FEAT_CMDWIN
7584 if (cmdwin_type != 0)
7585 cmdwin_result = K_IGNORE;
7586 else
7587# endif
7588 if (first_tabpage->tp_next == NULL)
7589 MSG(_("Already only one tab page"));
7590 else
7591 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007592 tab_number = get_tabpage_arg(eap);
7593 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007594 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007595 goto_tabpage(tab_number);
7596 /* Repeat this up to a 1000 times, because autocommands may
7597 * mess up the lists. */
7598 for (done = 0; done < 1000; ++done)
7599 {
7600 FOR_ALL_TABPAGES(tp)
7601 if (tp->tp_topframe != topframe)
7602 {
7603 tabpage_close_other(tp, eap->forceit);
7604 /* if we failed to close it quit */
7605 if (valid_tabpage(tp))
7606 done = 1000;
7607 /* start over, "tp" is now invalid */
7608 break;
7609 }
7610 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007611 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007612 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007613 }
7614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616
7617/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007618 * Close the current tab page.
7619 */
7620 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007621tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007622{
7623 /* First close all the windows but the current one. If that worked then
7624 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007625 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007626 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007627 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007628 ex_win_close(forceit, curwin, NULL);
7629# ifdef FEAT_GUI
7630 need_mouse_correct = TRUE;
7631# endif
7632}
7633
7634/*
7635 * Close tab page "tp", which is not the current tab page.
7636 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007637 * Also takes care of the tab pages line disappearing when closing the
7638 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007639 */
7640 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007641tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007642{
7643 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007644 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007645 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007646
7647 /* Limit to 1000 windows, autocommands may add a window while we close
7648 * one. OK, so I'm paranoid... */
7649 while (++done < 1000)
7650 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007651 wp = tp->tp_firstwin;
7652 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007653
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007654 /* Autocommands may delete the tab page under our fingers and we may
7655 * fail to close a window with a modified buffer. */
7656 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007657 break;
7658 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007659
Bram Moolenaar29323592016-07-24 22:04:11 +02007660 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007661
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007662 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007663 if (h != tabline_height())
7664 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007665}
7666
7667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 * ":only".
7669 */
7670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007671ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007673 win_T *wp;
7674 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675# ifdef FEAT_GUI
7676 need_mouse_correct = TRUE;
7677# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007678 if (eap->addr_count > 0)
7679 {
7680 wnr = eap->line2;
7681 for (wp = firstwin; --wnr > 0; )
7682 {
7683 if (wp->w_next == NULL)
7684 break;
7685 else
7686 wp = wp->w_next;
7687 }
7688 win_goto(wp);
7689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 close_others(TRUE, eap->forceit);
7691}
7692
7693/*
7694 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007695 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007697 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007698ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
7700 if (eap->addr_count == 0)
7701 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007702 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704
7705 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007706ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007708 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007709 if (!eap->skip)
7710 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007711#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007712 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007713#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007714 if (eap->addr_count == 0)
7715 win_close(curwin, FALSE); /* don't free buffer */
7716 else
7717 {
7718 int winnr = 0;
7719 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007720
Bram Moolenaar2256c992016-11-15 21:17:07 +01007721 FOR_ALL_WINDOWS(win)
7722 {
7723 winnr++;
7724 if (winnr == eap->line2)
7725 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007726 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007727 if (win == NULL)
7728 win = lastwin;
7729 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 }
7732}
7733
7734/*
7735 * ":stop" and ":suspend": Suspend Vim.
7736 */
7737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007738ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
7740 /*
7741 * Disallow suspending for "rvim".
7742 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007743 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 {
7745 if (!eap->forceit)
7746 autowrite_all();
7747 windgoto((int)Rows - 1, 0);
7748 out_char('\n');
7749 out_flush();
7750 stoptermcap();
7751 out_flush(); /* needed for SUN to restore xterm buffer */
7752#ifdef FEAT_TITLE
7753 mch_restore_title(3); /* restore window titles */
7754#endif
7755 ui_suspend(); /* call machine specific function */
7756#ifdef FEAT_TITLE
7757 maketitle();
7758 resettitle(); /* force updating the title */
7759#endif
7760 starttermcap();
7761 scroll_start(); /* scroll screen before redrawing */
7762 redraw_later_clear();
7763 shell_resized(); /* may have resized window */
7764 }
7765}
7766
7767/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007768 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 */
7770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007771ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
7773#ifdef FEAT_CMDWIN
7774 if (cmdwin_type != 0)
7775 {
7776 cmdwin_result = Ctrl_C;
7777 return;
7778 }
7779#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007780 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007781 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007782 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007783 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007784 return;
7785 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007786
7787 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007788 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789
7790 /*
7791 * if more files or windows we won't exit
7792 */
7793 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7794 exiting = TRUE;
7795 if ( ((eap->cmdidx == CMD_wq
7796 || curbufIsChanged())
7797 && do_write(eap) == FAIL)
7798 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007799 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 {
7801 not_exiting();
7802 }
7803 else
7804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007807 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808# ifdef FEAT_GUI
7809 need_mouse_correct = TRUE;
7810# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007811 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007812 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 }
7814}
7815
7816/*
7817 * ":print", ":list", ":number".
7818 */
7819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007820ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007822 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7823 EMSG(_(e_emptybuf));
7824 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007826 for ( ;!got_int; ui_breakcheck())
7827 {
7828 print_line(eap->line1,
7829 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7830 || (eap->flags & EXFLAG_NR)),
7831 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7832 if (++eap->line1 > eap->line2)
7833 break;
7834 out_flush(); /* show one line at a time */
7835 }
7836 setpcmark();
7837 /* put cursor at last line */
7838 curwin->w_cursor.lnum = eap->line2;
7839 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 }
7841
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843}
7844
7845#ifdef FEAT_BYTEOFF
7846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007847ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848{
7849 goto_byte(eap->line2);
7850}
7851#endif
7852
7853/*
7854 * ":shell".
7855 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007857ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
7859 do_shell(NULL, 0);
7860}
7861
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007862#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007864static int drop_busy = FALSE;
7865static int drop_filec;
7866static char_u **drop_filev = NULL;
7867static int drop_split;
7868static void (*drop_callback)(void *);
7869static void *drop_cookie;
7870
7871 static void
7872handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 exarg_T ea;
7875 int save_msg_scroll = msg_scroll;
7876
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007877 // Setting the argument list may cause screen updates and being called
7878 // recursively. Avoid that by setting drop_busy.
7879 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880
7881 /* Check whether the current buffer is changed. If so, we will need
7882 * to split the current window or data could be lost.
7883 * We don't need to check if the 'hidden' option is set, as in this
7884 * case the buffer won't be lost.
7885 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007886 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {
7888 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007889 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 --emsg_off;
7891 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007892 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 if (win_split(0, 0) == FAIL)
7895 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007896 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897
7898 /* When splitting the window, create a new alist. Otherwise the
7899 * existing one is overwritten. */
7900 alist_unlink(curwin->w_alist);
7901 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 }
7903
7904 /*
7905 * Set up the new argument list.
7906 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007907 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908
7909 /*
7910 * Move to the first file.
7911 */
7912 /* Fake up a minimal "next" command for do_argfile() */
7913 vim_memset(&ea, 0, sizeof(ea));
7914 ea.cmd = (char_u *)"next";
7915 do_argfile(&ea, 0);
7916
7917 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7918 * mode that is not needed here. */
7919 need_start_insertmode = FALSE;
7920
7921 /* Restore msg_scroll, otherwise a following command may cause scrolling
7922 * unexpectedly. The screen will be redrawn by the caller, thus
7923 * msg_scroll being set by displaying a message is irrelevant. */
7924 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007925
7926 if (drop_callback != NULL)
7927 drop_callback(drop_cookie);
7928
7929 drop_filev = NULL;
7930 drop_busy = FALSE;
7931}
7932
7933/*
7934 * Handle a file drop. The code is here because a drop is *nearly* like an
7935 * :args command, but not quite (we have a list of exact filenames, so we
7936 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7937 * code is very similar to :args and hence needs access to a lot of the static
7938 * functions in this file.
7939 *
7940 * The "filev" list must have been allocated using alloc(), as should each item
7941 * in the list. This function takes over responsibility for freeing the "filev"
7942 * list.
7943 */
7944 void
7945handle_drop(
7946 int filec, // the number of files dropped
7947 char_u **filev, // the list of files dropped
7948 int split, // force splitting the window
7949 void (*callback)(void *), // to be called after setting the argument
7950 // list
7951 void *cookie) // argument for "callback" (allocated)
7952{
7953 // Cannot handle recursive drops, finish the pending one.
7954 if (drop_busy)
7955 {
7956 FreeWild(filec, filev);
7957 vim_free(cookie);
7958 return;
7959 }
7960
7961 // When calling handle_drop() more than once in a row we only use the last
7962 // one.
7963 if (drop_filev != NULL)
7964 {
7965 FreeWild(drop_filec, drop_filev);
7966 vim_free(drop_cookie);
7967 }
7968
7969 drop_filec = filec;
7970 drop_filev = filev;
7971 drop_split = split;
7972 drop_callback = callback;
7973 drop_cookie = cookie;
7974
7975 // Postpone this when:
7976 // - editing the command line
7977 // - not possible to change the current buffer
7978 // - updating the screen
7979 // As it may change buffers and window structures that are in use and cause
7980 // freed memory to be used.
7981 if (text_locked() || curbuf_locked() || updating_screen)
7982 return;
7983
7984 handle_drop_internal();
7985}
7986
7987/*
7988 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
7989 * reset: Handle a postponed drop.
7990 */
7991 void
7992handle_any_postponed_drop(void)
7993{
7994 if (!drop_busy && drop_filev != NULL
7995 && !text_locked() && !curbuf_locked() && !updating_screen)
7996 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997}
7998#endif
7999
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000/*
8001 * Clear an argument list: free all file names and reset it to zero entries.
8002 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008003 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008004alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005{
8006 while (--al->al_ga.ga_len >= 0)
8007 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8008 ga_clear(&al->al_ga);
8009}
8010
8011/*
8012 * Init an argument list.
8013 */
8014 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008015alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8018}
8019
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020/*
8021 * Remove a reference from an argument list.
8022 * Ignored when the argument list is the global one.
8023 * If the argument list is no longer used by any window, free it.
8024 */
8025 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008026alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027{
8028 if (al != &global_alist && --al->al_refcount <= 0)
8029 {
8030 alist_clear(al);
8031 vim_free(al);
8032 }
8033}
8034
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035/*
8036 * Create a new argument list and use it for the current window.
8037 */
8038 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008039alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040{
8041 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8042 if (curwin->w_alist == NULL)
8043 {
8044 curwin->w_alist = &global_alist;
8045 ++global_alist.al_refcount;
8046 }
8047 else
8048 {
8049 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008050 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 alist_init(curwin->w_alist);
8052 }
8053}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008055#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056/*
8057 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008058 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8059 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 */
8061 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008062alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063{
8064 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008065 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 char_u **new_arg_files;
8067 int new_arg_file_count;
8068 char_u *save_p_su = p_su;
8069 int i;
8070
8071 /* Don't use 'suffixes' here. This should work like the shell did the
8072 * expansion. Also, the vimrc file isn't read yet, thus the user
8073 * can't set the options. */
8074 p_su = empty_option;
8075 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8076 if (old_arg_files != NULL)
8077 {
8078 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008079 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8080 old_arg_count = GARGCOUNT;
8081 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008083 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 && new_arg_file_count > 0)
8085 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008086 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8087 TRUE, fnum_list, fnum_len);
8088 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 }
8091 p_su = save_p_su;
8092}
8093#endif
8094
8095/*
8096 * Set the argument list for the current window.
8097 * Takes over the allocated files[] and the allocated fnames in it.
8098 */
8099 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008100alist_set(
8101 alist_T *al,
8102 int count,
8103 char_u **files,
8104 int use_curbuf,
8105 int *fnum_list,
8106 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
8108 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008109 static int recursive = 0;
8110
8111 if (recursive)
8112 {
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008113 EMSG(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008114 return;
8115 }
8116 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117
8118 alist_clear(al);
8119 if (ga_grow(&al->al_ga, count) == OK)
8120 {
8121 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008122 {
8123 if (got_int)
8124 {
8125 /* When adding many buffers this can take a long time. Allow
8126 * interrupting here. */
8127 while (i < count)
8128 vim_free(files[i++]);
8129 break;
8130 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008131
8132 /* May set buffer name of a buffer previously used for the
8133 * argument list, so that it's re-used by alist_add. */
8134 if (fnum_list != NULL && i < fnum_len)
8135 buf_set_name(fnum_list[i], files[i]);
8136
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008138 ui_breakcheck();
8139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 vim_free(files);
8141 }
8142 else
8143 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008146
8147 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148}
8149
8150/*
8151 * Add file "fname" to argument list "al".
8152 * "fname" must have been allocated and "al" must have been checked for room.
8153 */
8154 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008155alist_add(
8156 alist_T *al,
8157 char_u *fname,
8158 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159{
8160 if (fname == NULL) /* don't add NULL file names */
8161 return;
8162#ifdef BACKSLASH_IN_FILENAME
8163 slash_adjust(fname);
8164#endif
8165 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8166 if (set_fnum > 0)
8167 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8168 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8169 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170}
8171
8172#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8173/*
8174 * Adjust slashes in file names. Called after 'shellslash' was set.
8175 */
8176 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008177alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178{
8179 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008181 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182
8183 for (i = 0; i < GARGCOUNT; ++i)
8184 if (GARGLIST[i].ae_fname != NULL)
8185 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008186 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 if (wp->w_alist != &global_alist)
8188 for (i = 0; i < WARGCOUNT(wp); ++i)
8189 if (WARGLIST(wp)[i].ae_fname != NULL)
8190 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191}
8192#endif
8193
8194/*
8195 * ":preserve".
8196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008198ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008200 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 ml_preserve(curbuf, TRUE);
8202}
8203
8204/*
8205 * ":recover".
8206 */
8207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008208ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209{
8210 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8211 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008212 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8213 | CCGD_MULTWIN
8214 | (eap->forceit ? CCGD_FORCEIT : 0)
8215 | CCGD_EXCMD)
8216
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 && (*eap->arg == NUL
8218 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8219 ml_recover();
8220 recoverymode = FALSE;
8221}
8222
8223/*
8224 * Command modifier used in a wrong way.
8225 */
8226 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008227ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228{
8229 eap->errmsg = e_invcmd;
8230}
8231
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232/*
8233 * :sview [+command] file split window with new file, read-only
8234 * :split [[+command] file] split window with current or new file
8235 * :vsplit [[+command] file] split window vertically with current or new file
8236 * :new [[+command] file] split window with no or new file
8237 * :vnew [[+command] file] split vertically window with no or new file
8238 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008239 *
8240 * :tabedit open new Tab page with empty window
8241 * :tabedit [+command] file open new Tab page and edit "file"
8242 * :tabnew [[+command] file] just like :tabedit
8243 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 */
8245 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008246ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008248 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008249#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008251#endif
8252#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008254#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008255 int use_tab = eap->cmdidx == CMD_tabedit
8256 || eap->cmdidx == CMD_tabfind
8257 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258
Bram Moolenaar4033c552017-09-16 20:54:51 +02008259#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262
Bram Moolenaar4033c552017-09-16 20:54:51 +02008263#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008265 * quickfix windows. But it's OK when doing ":tab split". */
8266 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {
8268 if (eap->cmdidx == CMD_split)
8269 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 if (eap->cmdidx == CMD_vsplit)
8271 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274
Bram Moolenaar4033c552017-09-16 20:54:51 +02008275#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008276 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 {
8278 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8279 FNAME_MESS, TRUE, curbuf->b_ffname);
8280 if (fname == NULL)
8281 goto theend;
8282 eap->arg = fname;
8283 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008284# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008285 else
8286# endif
8287#endif
8288#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 && eap->cmdidx != CMD_new)
8292 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008293 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008294# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008295 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008296# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008297 au_has_group((char_u *)"FileExplorer"))
8298 {
8299 /* No browsing supported but we do have the file explorer:
8300 * Edit the directory. */
8301 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8302 eap->arg = (char_u *)".";
8303 }
8304 else
8305 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008306 fname = do_browse(0, (char_u *)(use_tab
8307 ? _("Edit File in new tab page")
8308 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008310 if (fname == NULL)
8311 goto theend;
8312 eap->arg = fname;
8313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 }
8315 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008318 /*
8319 * Either open new tab page or split the window.
8320 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008321 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008322 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008323 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8324 : eap->addr_count == 0 ? 0
8325 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008326 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008327 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008328
8329 /* set the alternate buffer for the window we came from */
8330 if (curwin != old_curwin
8331 && win_valid(old_curwin)
8332 && old_curwin->w_buffer != curbuf
8333 && !cmdmod.keepalt)
8334 old_curwin->w_alt_fnum = curbuf->b_fnum;
8335 }
8336 }
8337 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8339 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 /* Reset 'scrollbind' when editing another file, but keep it when
8341 * doing ":split" without arguments. */
8342 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008343# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008345# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008347 {
8348 RESET_BINDING(curwin);
8349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 else
8351 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 do_exedit(eap, old_curwin);
8353 }
8354
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008355# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008357# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008359# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360theend:
8361 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008364
8365/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008366 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008367 */
8368 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008369tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008370{
8371 exarg_T ea;
8372
8373 vim_memset(&ea, 0, sizeof(ea));
8374 ea.cmdidx = CMD_tabnew;
8375 ea.cmd = (char_u *)"tabn";
8376 ea.arg = (char_u *)"";
8377 ex_splitview(&ea);
8378}
8379
8380/*
8381 * :tabnext command
8382 */
8383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008384ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008385{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008386 int tab_number;
8387
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008388 switch (eap->cmdidx)
8389 {
8390 case CMD_tabfirst:
8391 case CMD_tabrewind:
8392 goto_tabpage(1);
8393 break;
8394 case CMD_tablast:
8395 goto_tabpage(9999);
8396 break;
8397 case CMD_tabprevious:
8398 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008399 if (eap->arg && *eap->arg != NUL)
8400 {
8401 char_u *p = eap->arg;
8402 char_u *p_save = p;
8403
8404 tab_number = getdigits(&p);
8405 if (p == p_save || *p_save == '-' || *p != NUL
8406 || tab_number == 0)
8407 {
8408 /* No numbers as argument. */
8409 eap->errmsg = e_invarg;
8410 return;
8411 }
8412 }
8413 else
8414 {
8415 if (eap->addr_count == 0)
8416 tab_number = 1;
8417 else
8418 {
8419 tab_number = eap->line2;
8420 if (tab_number < 1)
8421 {
8422 eap->errmsg = e_invrange;
8423 return;
8424 }
8425 }
8426 }
8427 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008428 break;
8429 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008430 tab_number = get_tabpage_arg(eap);
8431 if (eap->errmsg == NULL)
8432 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008433 break;
8434 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008435}
8436
8437/*
8438 * :tabmove command
8439 */
8440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008441ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008442{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008443 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008444
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008445 tab_number = get_tabpage_arg(eap);
8446 if (eap->errmsg == NULL)
8447 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008448}
8449
8450/*
8451 * :tabs command: List tabs and their contents.
8452 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008454ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008455{
8456 tabpage_T *tp;
8457 win_T *wp;
8458 int tabcount = 1;
8459
8460 msg_start();
8461 msg_scroll = TRUE;
8462 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8463 {
8464 msg_putchar('\n');
8465 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008466 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008467 out_flush(); /* output one line at a time */
8468 ui_breakcheck();
8469
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008470 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008471 wp = firstwin;
8472 else
8473 wp = tp->tp_firstwin;
8474 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8475 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008476 msg_putchar('\n');
8477 msg_putchar(wp == curwin ? '>' : ' ');
8478 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008479 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8480 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008481 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008482 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008483 else
8484 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8485 IObuff, IOSIZE, TRUE);
8486 msg_outtrans(IObuff);
8487 out_flush(); /* output one line at a time */
8488 ui_breakcheck();
8489 }
8490 }
8491}
8492
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493/*
8494 * ":mode": Set screen mode.
8495 * If no argument given, just get the screen size and redraw.
8496 */
8497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008498ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499{
8500 if (*eap->arg == NUL)
8501 shell_resized();
8502 else
8503 mch_screenmode(eap->arg);
8504}
8505
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506/*
8507 * ":resize".
8508 * set, increment or decrement current window height
8509 */
8510 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008511ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512{
8513 int n;
8514 win_T *wp = curwin;
8515
8516 if (eap->addr_count > 0)
8517 {
8518 n = eap->line2;
8519 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8520 ;
8521 }
8522
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008523# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 if (cmdmod.split & WSP_VERT)
8528 {
8529 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008530 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8532 n = 9999;
8533 win_setwidth_win((int)n, wp);
8534 }
8535 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 {
8537 if (*eap->arg == '-' || *eap->arg == '+')
8538 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008539 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 n = 9999;
8541 win_setheight_win((int)n, wp);
8542 }
8543}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544
8545/*
8546 * ":find [+command] <file>" command.
8547 */
8548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008549ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551#ifdef FEAT_SEARCHPATH
8552 char_u *fname;
8553 int count;
8554
8555 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8556 TRUE, curbuf->b_ffname);
8557 if (eap->addr_count > 0)
8558 {
8559 /* Repeat finding the file "count" times. This matters when it
8560 * appears several times in the path. */
8561 count = eap->line2;
8562 while (fname != NULL && --count > 0)
8563 {
8564 vim_free(fname);
8565 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8566 FALSE, curbuf->b_ffname);
8567 }
8568 }
8569
8570 if (fname != NULL)
8571 {
8572 eap->arg = fname;
8573#endif
8574 do_exedit(eap, NULL);
8575#ifdef FEAT_SEARCHPATH
8576 vim_free(fname);
8577 }
8578#endif
8579}
8580
8581/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008582 * ":open" simulation: for now just work like ":visual".
8583 */
8584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008585ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008586{
8587 regmatch_T regmatch;
8588 char_u *p;
8589
8590 curwin->w_cursor.lnum = eap->line2;
8591 beginline(BL_SOL | BL_FIX);
8592 if (*eap->arg == '/')
8593 {
8594 /* ":open /pattern/": put cursor in column found with pattern */
8595 ++eap->arg;
8596 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8597 *p = NUL;
8598 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8599 if (regmatch.regprog != NULL)
8600 {
8601 regmatch.rm_ic = p_ic;
8602 p = ml_get_curline();
8603 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008604 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008605 else
8606 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008607 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008608 }
8609 /* Move to the NUL, ignore any other arguments. */
8610 eap->arg += STRLEN(eap->arg);
8611 }
8612 check_cursor();
8613
8614 eap->cmdidx = CMD_visual;
8615 do_exedit(eap, NULL);
8616}
8617
8618/*
8619 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 */
8621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008622ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623{
8624 do_exedit(eap, NULL);
8625}
8626
8627/*
8628 * ":edit <file>" command and alikes.
8629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008631do_exedit(
8632 exarg_T *eap,
8633 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634{
8635 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008637 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638
8639 /*
8640 * ":vi" command ends Ex mode.
8641 */
8642 if (exmode_active && (eap->cmdidx == CMD_visual
8643 || eap->cmdidx == CMD_view))
8644 {
8645 exmode_active = FALSE;
8646 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008647 {
8648 /* Special case: ":global/pat/visual\NLvi-commands" */
8649 if (global_busy)
8650 {
8651 int rd = RedrawingDisabled;
8652 int nwr = no_wait_return;
8653 int ms = msg_scroll;
8654#ifdef FEAT_GUI
8655 int he = hold_gui_events;
8656#endif
8657
8658 if (eap->nextcmd != NULL)
8659 {
8660 stuffReadbuff(eap->nextcmd);
8661 eap->nextcmd = NULL;
8662 }
8663
8664 if (exmode_was != EXMODE_VIM)
8665 settmode(TMODE_RAW);
8666 RedrawingDisabled = 0;
8667 no_wait_return = 0;
8668 need_wait_return = FALSE;
8669 msg_scroll = 0;
8670#ifdef FEAT_GUI
8671 hold_gui_events = 0;
8672#endif
8673 must_redraw = CLEAR;
8674
8675 main_loop(FALSE, TRUE);
8676
8677 RedrawingDisabled = rd;
8678 no_wait_return = nwr;
8679 msg_scroll = ms;
8680#ifdef FEAT_GUI
8681 hold_gui_events = he;
8682#endif
8683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 }
8687
8688 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008689 || eap->cmdidx == CMD_tabnew
8690 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008691 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008693 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 setpcmark();
8695 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008696 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8697 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008699 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 || *eap->arg != NUL
8701#ifdef FEAT_BROWSE
8702 || cmdmod.browse
8703#endif
8704 )
8705 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008706 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8707 * can bring us here, others are stopped earlier. */
8708 if (*eap->arg != NUL && curbuf_locked())
8709 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008710
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 n = readonlymode;
8712 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8713 readonlymode = TRUE;
8714 else if (eap->cmdidx == CMD_enew)
8715 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8716 empty buffer */
8717 setpcmark();
8718 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8719 NULL, eap,
8720 /* ":edit" goes to first line if Vi compatible */
8721 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8722 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8723 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008724 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008726 /* after a split we can use an existing buffer */
8727 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008729 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 {
8731 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 if (old_curwin != NULL)
8733 {
8734 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008735 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008737#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008738 cleanup_T cs;
8739
8740 /* Reset the error/interrupt/exception state here so that
8741 * aborting() returns FALSE when closing a window. */
8742 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008743#endif
8744#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008746#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008747 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008748
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008749#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008750 /* Restore the error/interrupt/exception state if not
8751 * discarded by a new aborting error, interrupt, or
8752 * uncaught exception. */
8753 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 }
8756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 }
8758 else if (readonlymode && curbuf->b_nwindows == 1)
8759 {
8760 /* When editing an already visited buffer, 'readonly' won't be set
8761 * but the previous value is kept. With ":view" and ":sview" we
8762 * want the file to be readonly, except when another window is
8763 * editing the same buffer. */
8764 curbuf->b_p_ro = TRUE;
8765 }
8766 readonlymode = n;
8767 }
8768 else
8769 {
8770 if (eap->do_ecmd_cmd != NULL)
8771 do_cmdline_cmd(eap->do_ecmd_cmd);
8772#ifdef FEAT_TITLE
8773 n = curwin->w_arg_idx_invalid;
8774#endif
8775 check_arg_idx(curwin);
8776#ifdef FEAT_TITLE
8777 if (n != curwin->w_arg_idx_invalid)
8778 maketitle();
8779#endif
8780 }
8781
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 /*
8783 * if ":split file" worked, set alternate file name in old window to new
8784 * file
8785 */
8786 if (old_curwin != NULL
8787 && *eap->arg != NUL
8788 && curwin != old_curwin
8789 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008790 && old_curwin->w_buffer != curbuf
8791 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793
8794 ex_no_reprint = TRUE;
8795}
8796
8797#ifndef FEAT_GUI
8798/*
8799 * ":gui" and ":gvim" when there is no GUI.
8800 */
8801 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008802ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803{
8804 eap->errmsg = e_nogvim;
8805}
8806#endif
8807
8808#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008810ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811{
8812 gui_make_tearoff(eap->arg);
8813}
8814#endif
8815
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008816#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8817 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008819ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008821# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8822 if (gui.in_use)
8823 gui_make_popup(eap->arg, eap->forceit);
8824# ifdef FEAT_TERM_POPUP_MENU
8825 else
8826# endif
8827# endif
8828# ifdef FEAT_TERM_POPUP_MENU
8829 pum_make_popup(eap->arg, eap->forceit);
8830# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831}
8832#endif
8833
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008835ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836{
8837 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8838 MSG(_("No swap file"));
8839 else
8840 msg(curbuf->b_ml.ml_mfp->mf_fname);
8841}
8842
8843/*
8844 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8845 * offset.
8846 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008849ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008852 win_T *save_curwin = curwin;
8853 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 long topline;
8855 long y;
8856 linenr_T old_linenr = curwin->w_cursor.lnum;
8857
8858 setpcmark();
8859
8860 /*
8861 * determine max topline
8862 */
8863 if (curwin->w_p_scb)
8864 {
8865 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008866 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 {
8868 if (wp->w_p_scb && wp->w_buffer)
8869 {
8870 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8871 if (topline > y)
8872 topline = y;
8873 }
8874 }
8875 if (topline < 1)
8876 topline = 1;
8877 }
8878 else
8879 {
8880 topline = 1;
8881 }
8882
8883
8884 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008885 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008887 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 {
8889 if (curwin->w_p_scb)
8890 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008891 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 y = topline - curwin->w_topline;
8893 if (y > 0)
8894 scrollup(y, TRUE);
8895 else
8896 scrolldown(-y, TRUE);
8897 curwin->w_scbind_pos = topline;
8898 redraw_later(VALID);
8899 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 }
8902 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008903 curwin = save_curwin;
8904 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 if (curwin->w_p_scb)
8906 {
8907 did_syncbind = TRUE;
8908 checkpcmark();
8909 if (old_linenr != curwin->w_cursor.lnum)
8910 {
8911 char_u ctrl_o[2];
8912
8913 ctrl_o[0] = Ctrl_O;
8914 ctrl_o[1] = 0;
8915 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8916 }
8917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918}
8919
8920
8921 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008922ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008924 int i;
8925 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8926 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927
8928 if (eap->usefilter) /* :r!cmd */
8929 do_bang(1, eap, FALSE, FALSE, TRUE);
8930 else
8931 {
8932 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8933 return;
8934
8935#ifdef FEAT_BROWSE
8936 if (cmdmod.browse)
8937 {
8938 char_u *browseFile;
8939
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008940 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 NULL, NULL, NULL, curbuf);
8942 if (browseFile != NULL)
8943 {
8944 i = readfile(browseFile, NULL,
8945 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8946 vim_free(browseFile);
8947 }
8948 else
8949 i = OK;
8950 }
8951 else
8952#endif
8953 if (*eap->arg == NUL)
8954 {
8955 if (check_fname() == FAIL) /* check for no file name */
8956 return;
8957 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8958 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8959 }
8960 else
8961 {
8962 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8963 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8964 i = readfile(eap->arg, NULL,
8965 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8966
8967 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008968 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008970#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 if (!aborting())
8972#endif
8973 EMSG2(_(e_notopen), eap->arg);
8974 }
8975 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008976 {
8977 if (empty && exmode_active)
8978 {
8979 /* Delete the empty line that remains. Historically ex does
8980 * this but vi doesn't. */
8981 if (eap->line2 == 0)
8982 lnum = curbuf->b_ml.ml_line_count;
8983 else
8984 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008985 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008986 {
8987 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008988 if (curwin->w_cursor.lnum > 1
8989 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008990 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008991 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008992 }
8993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 }
8997}
8998
Bram Moolenaarea408852005-06-25 22:49:46 +00008999static char_u *prev_dir = NULL;
9000
9001#if defined(EXITFREE) || defined(PROTO)
9002 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009003free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009004{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009005 VIM_CLEAR(prev_dir);
9006 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009007}
9008#endif
9009
Bram Moolenaarf4258302013-06-02 18:20:17 +02009010/*
9011 * Deal with the side effects of changing the current directory.
9012 * When "local" is TRUE then this was after an ":lcd" command.
9013 */
9014 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009015post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009016{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009017 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009018 if (local)
9019 {
9020 /* If still in global directory, need to remember current
9021 * directory as global directory. */
9022 if (globaldir == NULL && prev_dir != NULL)
9023 globaldir = vim_strsave(prev_dir);
9024 /* Remember this local directory for the window. */
9025 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9026 curwin->w_localdir = vim_strsave(NameBuff);
9027 }
9028 else
9029 {
9030 /* We are now in the global directory, no need to remember its
9031 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009032 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009033 }
9034
9035 shorten_fnames(TRUE);
9036}
9037
Bram Moolenaarea408852005-06-25 22:49:46 +00009038
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039/*
9040 * ":cd", ":lcd", ":chdir" and ":lchdir".
9041 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009042 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009043ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 char_u *new_dir;
9046 char_u *tofree;
9047
9048 new_dir = eap->arg;
9049#if !defined(UNIX) && !defined(VMS)
9050 /* for non-UNIX ":cd" means: print current directory */
9051 if (*new_dir == NUL)
9052 ex_pwd(NULL);
9053 else
9054#endif
9055 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009056 if (allbuf_locked())
9057 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009058 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9059 && !eap->forceit)
9060 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009061 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009062 return;
9063 }
9064
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 /* ":cd -": Change to previous directory */
9066 if (STRCMP(new_dir, "-") == 0)
9067 {
9068 if (prev_dir == NULL)
9069 {
9070 EMSG(_("E186: No previous directory"));
9071 return;
9072 }
9073 new_dir = prev_dir;
9074 }
9075
9076 /* Save current directory for next ":cd -" */
9077 tofree = prev_dir;
9078 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9079 prev_dir = vim_strsave(NameBuff);
9080 else
9081 prev_dir = NULL;
9082
9083#if defined(UNIX) || defined(VMS)
9084 /* for UNIX ":cd" means: go to home directory */
9085 if (*new_dir == NUL)
9086 {
9087 /* use NameBuff for home directory name */
9088# ifdef VMS
9089 char_u *p;
9090
9091 p = mch_getenv((char_u *)"SYS$LOGIN");
9092 if (p == NULL || *p == NUL) /* empty is the same as not set */
9093 NameBuff[0] = NUL;
9094 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009095 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096# else
9097 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9098# endif
9099 new_dir = NameBuff;
9100 }
9101#endif
9102 if (new_dir == NULL || vim_chdir(new_dir))
9103 EMSG(_(e_failed));
9104 else
9105 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009106 int is_local_chdir = eap->cmdidx == CMD_lcd
9107 || eap->cmdidx == CMD_lchdir;
9108
9109 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110
9111 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009112 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009114 apply_autocmds(EVENT_DIRCHANGED,
9115 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9116 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 }
9118 vim_free(tofree);
9119 }
9120}
9121
9122/*
9123 * ":pwd".
9124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009126ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127{
9128 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9129 {
9130#ifdef BACKSLASH_IN_FILENAME
9131 slash_adjust(NameBuff);
9132#endif
9133 msg(NameBuff);
9134 }
9135 else
9136 EMSG(_("E187: Unknown"));
9137}
9138
9139/*
9140 * ":=".
9141 */
9142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009143ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144{
9145 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009146 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147}
9148
9149 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009150ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009152 int n;
9153 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154
9155 if (cursor_valid())
9156 {
9157 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9158 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009159 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009161
9162 len = eap->line2;
9163 switch (*eap->arg)
9164 {
9165 case 'm': break;
9166 case NUL: len *= 1000L; break;
9167 default: EMSG2(_(e_invarg2), eap->arg); return;
9168 }
9169 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170}
9171
9172/*
9173 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9174 */
9175 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009176do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177{
9178 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009179 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180
9181 cursor_on();
9182 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009183 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009185 wait_now = msec - done > 1000L ? 1000L : msec - done;
9186#ifdef FEAT_TIMERS
9187 {
9188 long due_time = check_due_timer();
9189
9190 if (due_time > 0 && due_time < wait_now)
9191 wait_now = due_time;
9192 }
9193#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009194#ifdef FEAT_JOB_CHANNEL
9195 if (has_any_channel() && wait_now > 100L)
9196 wait_now = 100L;
9197#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009198 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009199#ifdef FEAT_JOB_CHANNEL
9200 if (has_any_channel())
9201 ui_breakcheck_force(TRUE);
9202 else
9203#endif
9204 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009205#ifdef MESSAGE_QUEUE
9206 /* Process the netbeans and clientserver messages that may have been
9207 * received in the call to ui_breakcheck() when the GUI is in use. This
9208 * may occur when running a test case. */
9209 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009212
9213 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9214 // input buffer, otherwise a following call to input() fails.
9215 if (got_int)
9216 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217}
9218
9219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009220do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221{
9222 int mode;
9223 char_u *cmdp;
9224
9225 cmdp = eap->cmd;
9226 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9227
9228 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9229 eap->arg, mode, isabbrev))
9230 {
9231 case 1: EMSG(_(e_invarg));
9232 break;
9233 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9234 break;
9235 }
9236}
9237
9238/*
9239 * ":winsize" command (obsolete).
9240 */
9241 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009242ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243{
9244 int w, h;
9245 char_u *arg = eap->arg;
9246 char_u *p;
9247
9248 w = getdigits(&arg);
9249 arg = skipwhite(arg);
9250 p = arg;
9251 h = getdigits(&arg);
9252 if (*p != NUL && *arg == NUL)
9253 set_shellsize(w, h, TRUE);
9254 else
9255 EMSG(_("E465: :winsize requires two number arguments"));
9256}
9257
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009259ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260{
9261 int xchar = NUL;
9262 char_u *p;
9263
9264 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9265 {
9266 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9267 if (eap->arg[1] == NUL)
9268 {
9269 EMSG(_(e_invarg));
9270 return;
9271 }
9272 xchar = eap->arg[1];
9273 p = eap->arg + 2;
9274 }
9275 else
9276 p = eap->arg + 1;
9277
9278 eap->nextcmd = check_nextcmd(p);
9279 p = skipwhite(p);
9280 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9281 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009282 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 {
9284 /* Pass flags on for ":vertical wincmd ]". */
9285 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009286 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9288 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009289 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 }
9291}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292
Bram Moolenaar843ee412004-06-30 16:16:41 +00009293#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294/*
9295 * ":winpos".
9296 */
9297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009298ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299{
9300 int x, y;
9301 char_u *arg = eap->arg;
9302 char_u *p;
9303
9304 if (*arg == NUL)
9305 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009306# if defined(FEAT_GUI) || defined(MSWIN)
9307# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009309# else
9310 if (mch_get_winpos(&x, &y) != FAIL)
9311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 {
9313 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9314 msg(IObuff);
9315 }
9316 else
9317# endif
9318 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9319 }
9320 else
9321 {
9322 x = getdigits(&arg);
9323 arg = skipwhite(arg);
9324 p = arg;
9325 y = getdigits(&arg);
9326 if (*p == NUL || *arg != NUL)
9327 {
9328 EMSG(_("E466: :winpos requires two number arguments"));
9329 return;
9330 }
9331# ifdef FEAT_GUI
9332 if (gui.in_use)
9333 gui_mch_set_winpos(x, y);
9334 else if (gui.starting)
9335 {
9336 /* Remember the coordinates for when the window is opened. */
9337 gui_win_x = x;
9338 gui_win_y = y;
9339 }
9340# ifdef HAVE_TGETENT
9341 else
9342# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009343# else
9344# ifdef MSWIN
9345 mch_set_winpos(x, y);
9346# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347# endif
9348# ifdef HAVE_TGETENT
9349 if (*T_CWP)
9350 term_set_winpos(x, y);
9351# endif
9352 }
9353}
9354#endif
9355
9356/*
9357 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9358 */
9359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009360ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361{
9362 oparg_T oa;
9363
9364 clear_oparg(&oa);
9365 oa.regname = eap->regname;
9366 oa.start.lnum = eap->line1;
9367 oa.end.lnum = eap->line2;
9368 oa.line_count = eap->line2 - eap->line1 + 1;
9369 oa.motion_type = MLINE;
9370#ifdef FEAT_VIRTUALEDIT
9371 virtual_op = FALSE;
9372#endif
9373 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9374 {
9375 setpcmark();
9376 curwin->w_cursor.lnum = eap->line1;
9377 beginline(BL_SOL | BL_FIX);
9378 }
9379
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009380 if (VIsual_active)
9381 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009382
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 switch (eap->cmdidx)
9384 {
9385 case CMD_delete:
9386 oa.op_type = OP_DELETE;
9387 op_delete(&oa);
9388 break;
9389
9390 case CMD_yank:
9391 oa.op_type = OP_YANK;
9392 (void)op_yank(&oa, FALSE, TRUE);
9393 break;
9394
9395 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009396 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009398 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9399#else
9400 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009402 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 oa.op_type = OP_RSHIFT;
9404 else
9405 oa.op_type = OP_LSHIFT;
9406 op_shift(&oa, FALSE, eap->amount);
9407 break;
9408 }
9409#ifdef FEAT_VIRTUALEDIT
9410 virtual_op = MAYBE;
9411#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009412 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413}
9414
9415/*
9416 * ":put".
9417 */
9418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009419ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420{
9421 /* ":0put" works like ":1put!". */
9422 if (eap->line2 == 0)
9423 {
9424 eap->line2 = 1;
9425 eap->forceit = TRUE;
9426 }
9427 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009428 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9429 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430}
9431
9432/*
9433 * Handle ":copy" and ":move".
9434 */
9435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009436ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437{
9438 long n;
9439
Bram Moolenaarded27822017-01-02 14:27:34 +01009440 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 if (eap->arg == NULL) /* error detected */
9442 {
9443 eap->nextcmd = NULL;
9444 return;
9445 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009446 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447
9448 /*
9449 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9450 */
9451 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9452 {
9453 EMSG(_(e_invaddr));
9454 return;
9455 }
9456
9457 if (eap->cmdidx == CMD_move)
9458 {
9459 if (do_move(eap->line1, eap->line2, n) == FAIL)
9460 return;
9461 }
9462 else
9463 ex_copy(eap->line1, eap->line2, n);
9464 u_clearline();
9465 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009466 ex_may_print(eap);
9467}
9468
9469/*
9470 * Print the current line if flags were given to the Ex command.
9471 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009472 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009473ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009474{
9475 if (eap->flags != 0)
9476 {
9477 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9478 (eap->flags & EXFLAG_LIST));
9479 ex_no_reprint = TRUE;
9480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481}
9482
9483/*
9484 * ":smagic" and ":snomagic".
9485 */
9486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009487ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488{
9489 int magic_save = p_magic;
9490
9491 p_magic = (eap->cmdidx == CMD_smagic);
9492 do_sub(eap);
9493 p_magic = magic_save;
9494}
9495
9496/*
9497 * ":join".
9498 */
9499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009500ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502 curwin->w_cursor.lnum = eap->line1;
9503 if (eap->line1 == eap->line2)
9504 {
9505 if (eap->addr_count >= 2) /* :2,2join does nothing */
9506 return;
9507 if (eap->line2 == curbuf->b_ml.ml_line_count)
9508 {
9509 beep_flush();
9510 return;
9511 }
9512 ++eap->line2;
9513 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009514 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009516 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517}
9518
9519/*
9520 * ":[addr]@r" or ":[addr]*r": execute register
9521 */
9522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009523ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524{
9525 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009526 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527
9528 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009529 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530
9531#ifdef USE_ON_FLY_SCROLL
9532 dont_scroll = TRUE; /* disallow scrolling here */
9533#endif
9534
9535 /* get the register name. No name means to use the previous one */
9536 c = *eap->arg;
9537 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9538 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009539 /* Put the register in the typeahead buffer with the "silent" flag. */
9540 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9541 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009542 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 else
9546 {
9547 int save_efr = exec_from_reg;
9548
9549 exec_from_reg = TRUE;
9550
9551 /*
9552 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009553 * Continue until the stuff buffer is empty and all added characters
9554 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009556 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9558
9559 exec_from_reg = save_efr;
9560 }
9561}
9562
9563/*
9564 * ":!".
9565 */
9566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009567ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568{
9569 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9570}
9571
9572/*
9573 * ":undo".
9574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009576ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009578 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009579 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009580 else
9581 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582}
9583
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009584#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009586ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009587{
9588 char_u hash[UNDO_HASH_SIZE];
9589
9590 u_compute_hash(hash);
9591 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9592}
9593
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009595ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009596{
9597 char_u hash[UNDO_HASH_SIZE];
9598
9599 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009600 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009601}
9602#endif
9603
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604/*
9605 * ":redo".
9606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009608ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609{
9610 u_redo(1);
9611}
9612
9613/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009614 * ":earlier" and ":later".
9615 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009617ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009618{
9619 long count = 0;
9620 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009621 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009622 char_u *p = eap->arg;
9623
9624 if (*p == NUL)
9625 count = 1;
9626 else if (isdigit(*p))
9627 {
9628 count = getdigits(&p);
9629 switch (*p)
9630 {
9631 case 's': ++p; sec = TRUE; break;
9632 case 'm': ++p; sec = TRUE; count *= 60; break;
9633 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009634 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9635 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009636 }
9637 }
9638
9639 if (*p != NUL)
9640 EMSG2(_(e_invarg2), eap->arg);
9641 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009642 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9643 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009644}
9645
9646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 * ":redir": start/stop redirection.
9648 */
9649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009650ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651{
9652 char *mode;
9653 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009654 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655
Bram Moolenaarba768492016-07-08 15:32:54 +02009656#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009657 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009658 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009659 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009660 return;
9661 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009662#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009663
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 if (STRICMP(eap->arg, "END") == 0)
9665 close_redir();
9666 else
9667 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009668 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009670 ++arg;
9671 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009673 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 mode = "a";
9675 }
9676 else
9677 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009678 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679
9680 close_redir();
9681
9682 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009683 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 if (fname == NULL)
9685 return;
9686#ifdef FEAT_BROWSE
9687 if (cmdmod.browse)
9688 {
9689 char_u *browseFile;
9690
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009691 browseFile = do_browse(BROWSE_SAVE,
9692 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009693 fname, NULL, NULL,
9694 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 if (browseFile == NULL)
9696 return; /* operation cancelled */
9697 vim_free(fname);
9698 fname = browseFile;
9699 eap->forceit = TRUE; /* since dialog already asked */
9700 }
9701#endif
9702
9703 redir_fd = open_exfile(fname, eap->forceit, mode);
9704 vim_free(fname);
9705 }
9706#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009707 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 {
9709 /* redirect to a register a-z (resp. A-Z for appending) */
9710 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009711 ++arg;
9712 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009714 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009715 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009717 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009719 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009720 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009721 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009722 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009724 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009725 if (*arg == '>')
9726 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009727 /* Make register empty when not using @A-@Z and the
9728 * command is valid. */
9729 if (*arg == NUL && !isupper(redir_reg))
9730 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009732 }
9733 if (*arg != NUL)
9734 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009735 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009736 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009737 }
9738 }
9739 else if (*arg == '=' && arg[1] == '>')
9740 {
9741 int append;
9742
9743 /* redirect to a variable */
9744 close_redir();
9745 arg += 2;
9746
9747 if (*arg == '>')
9748 {
9749 ++arg;
9750 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 }
9752 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009753 append = FALSE;
9754
9755 if (var_redir_start(skipwhite(arg), append) == OK)
9756 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 }
9758#endif
9759
9760 /* TODO: redirect to a buffer */
9761
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009763 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009765
9766 /* Make sure redirection is not off. Can happen for cmdline completion
9767 * that indirectly invokes a command to catch its output. */
9768 if (redir_fd != NULL
9769#ifdef FEAT_EVAL
9770 || redir_reg || redir_vname
9771#endif
9772 )
9773 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774}
9775
9776/*
9777 * ":redraw": force redraw
9778 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009779 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009780ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781{
9782 int r = RedrawingDisabled;
9783 int p = p_lz;
9784
9785 RedrawingDisabled = 0;
9786 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009787 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009789 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790#ifdef FEAT_TITLE
9791 if (need_maketitle)
9792 maketitle();
9793#endif
9794 RedrawingDisabled = r;
9795 p_lz = p;
9796
9797 /* Reset msg_didout, so that a message that's there is overwritten. */
9798 msg_didout = FALSE;
9799 msg_col = 0;
9800
Bram Moolenaar56667a52013-07-17 11:54:28 +02009801 /* No need to wait after an intentional redraw. */
9802 need_wait_return = FALSE;
9803
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 out_flush();
9805}
9806
9807/*
9808 * ":redrawstatus": force redraw of status line(s)
9809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009811ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 int r = RedrawingDisabled;
9814 int p = p_lz;
9815
9816 RedrawingDisabled = 0;
9817 p_lz = FALSE;
9818 if (eap->forceit)
9819 status_redraw_all();
9820 else
9821 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009822 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 RedrawingDisabled = r;
9824 p_lz = p;
9825 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826}
9827
9828 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009829close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830{
9831 if (redir_fd != NULL)
9832 {
9833 fclose(redir_fd);
9834 redir_fd = NULL;
9835 }
9836#ifdef FEAT_EVAL
9837 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009838 if (redir_vname)
9839 {
9840 var_redir_stop();
9841 redir_vname = 0;
9842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843#endif
9844}
9845
9846#if defined(FEAT_SESSION) && defined(USE_CRNL)
9847# define MKSESSION_NL
9848static int mksession_nl = FALSE; /* use NL only in put_eol() */
9849#endif
9850
9851/*
9852 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9853 */
9854 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009855ex_mkrc(
9856 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857{
9858 FILE *fd;
9859 int failed = FALSE;
9860 char_u *fname;
9861#ifdef FEAT_BROWSE
9862 char_u *browseFile = NULL;
9863#endif
9864#ifdef FEAT_SESSION
9865 int view_session = FALSE;
9866 int using_vdir = FALSE; /* using 'viewdir'? */
9867 char_u *viewFile = NULL;
9868 unsigned *flagp;
9869#endif
9870
9871 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9872 {
9873#ifdef FEAT_SESSION
9874 view_session = TRUE;
9875#else
9876 ex_ni(eap);
9877 return;
9878#endif
9879 }
9880
9881#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009882 /* Use the short file name until ":lcd" is used. We also don't use the
9883 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009884 did_lcd = FALSE;
9885
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9887 if (eap->cmdidx == CMD_mkview
9888 && (*eap->arg == NUL
9889 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9890 {
9891 eap->forceit = TRUE;
9892 fname = get_view_file(*eap->arg);
9893 if (fname == NULL)
9894 return;
9895 viewFile = fname;
9896 using_vdir = TRUE;
9897 }
9898 else
9899#endif
9900 if (*eap->arg != NUL)
9901 fname = eap->arg;
9902 else if (eap->cmdidx == CMD_mkvimrc)
9903 fname = (char_u *)VIMRC_FILE;
9904#ifdef FEAT_SESSION
9905 else if (eap->cmdidx == CMD_mksession)
9906 fname = (char_u *)SESSION_FILE;
9907#endif
9908 else
9909 fname = (char_u *)EXRC_FILE;
9910
9911#ifdef FEAT_BROWSE
9912 if (cmdmod.browse)
9913 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009914 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915# ifdef FEAT_SESSION
9916 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9917 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9918# endif
9919 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009920 fname, (char_u *)"vim", NULL,
9921 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 if (browseFile == NULL)
9923 goto theend;
9924 fname = browseFile;
9925 eap->forceit = TRUE; /* since dialog already asked */
9926 }
9927#endif
9928
9929#if defined(FEAT_SESSION) && defined(vim_mkdir)
9930 /* When using 'viewdir' may have to create the directory. */
9931 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009932 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933#endif
9934
9935 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9936 if (fd != NULL)
9937 {
9938#ifdef FEAT_SESSION
9939 if (eap->cmdidx == CMD_mkview)
9940 flagp = &vop_flags;
9941 else
9942 flagp = &ssop_flags;
9943#endif
9944
9945#ifdef MKSESSION_NL
9946 /* "unix" in 'sessionoptions': use NL line separator */
9947 if (view_session && (*flagp & SSOP_UNIX))
9948 mksession_nl = TRUE;
9949#endif
9950
9951 /* Write the version command for :mkvimrc */
9952 if (eap->cmdidx == CMD_mkvimrc)
9953 (void)put_line(fd, "version 6.0");
9954
9955#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009956 if (eap->cmdidx == CMD_mksession)
9957 {
9958 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9959 failed = TRUE;
9960 }
9961
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 if (eap->cmdidx != CMD_mkview)
9963#endif
9964 {
9965 /* Write setting 'compatible' first, because it has side effects.
9966 * For that same reason only do it when needed. */
9967 if (p_cp)
9968 (void)put_line(fd, "if !&cp | set cp | endif");
9969 else
9970 (void)put_line(fd, "if &cp | set nocp | endif");
9971 }
9972
9973#ifdef FEAT_SESSION
9974 if (!view_session
9975 || (eap->cmdidx == CMD_mksession
9976 && (*flagp & SSOP_OPTIONS)))
9977#endif
9978 failed |= (makemap(fd, NULL) == FAIL
9979 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9980
9981#ifdef FEAT_SESSION
9982 if (!failed && view_session)
9983 {
9984 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9985 failed = TRUE;
9986 if (eap->cmdidx == CMD_mksession)
9987 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009988 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989
Bram Moolenaard9462e32011-04-11 21:35:11 +02009990 dirnow = alloc(MAXPATHL);
9991 if (dirnow == NULL)
9992 failed = TRUE;
9993 else
9994 {
9995 /*
9996 * Change to session file's dir.
9997 */
9998 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010000 *dirnow = NUL;
10001 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10002 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010003 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010004 shorten_fnames(TRUE);
10005 }
10006 else if (*dirnow != NUL
10007 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10008 {
10009 if (mch_chdir((char *)globaldir) == 0)
10010 shorten_fnames(TRUE);
10011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012
Bram Moolenaard9462e32011-04-11 21:35:11 +020010013 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014
Bram Moolenaard9462e32011-04-11 21:35:11 +020010015 /* restore original dir */
10016 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010018 {
10019 if (mch_chdir((char *)dirnow) != 0)
10020 EMSG(_(e_prev_dir));
10021 shorten_fnames(TRUE);
10022 }
10023 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 }
10025 }
10026 else
10027 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010028 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10029 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 }
10031 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10032 == FAIL)
10033 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010034 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10035 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010036 if (eap->cmdidx == CMD_mksession)
10037 {
10038 if (put_line(fd, "unlet SessionLoad") == FAIL)
10039 failed = TRUE;
10040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 }
10042#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010043 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10044 failed = TRUE;
10045
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 failed |= fclose(fd);
10047
10048 if (failed)
10049 EMSG(_(e_write));
10050#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10051 else if (eap->cmdidx == CMD_mksession)
10052 {
10053 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010054 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055
Bram Moolenaard9462e32011-04-11 21:35:11 +020010056 tbuf = alloc(MAXPATHL);
10057 if (tbuf != NULL)
10058 {
10059 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10060 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10061 vim_free(tbuf);
10062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 }
10064#endif
10065#ifdef MKSESSION_NL
10066 mksession_nl = FALSE;
10067#endif
10068 }
10069
10070#ifdef FEAT_BROWSE
10071theend:
10072 vim_free(browseFile);
10073#endif
10074#ifdef FEAT_SESSION
10075 vim_free(viewFile);
10076#endif
10077}
10078
Bram Moolenaardf177f62005-02-22 08:39:57 +000010079#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10080 || defined(PROTO)
10081 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010082vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010083{
10084 if (vim_mkdir(name, prot) != 0)
10085 {
10086 EMSG2(_("E739: Cannot create directory: %s"), name);
10087 return FAIL;
10088 }
10089 return OK;
10090}
10091#endif
10092
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093/*
10094 * Open a file for writing for an Ex command, with some checks.
10095 * Return file descriptor, or NULL on failure.
10096 */
10097 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010098open_exfile(
10099 char_u *fname,
10100 int forceit,
10101 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102{
10103 FILE *fd;
10104
10105#ifdef UNIX
10106 /* with Unix it is possible to open a directory */
10107 if (mch_isdir(fname))
10108 {
10109 EMSG2(_(e_isadir2), fname);
10110 return NULL;
10111 }
10112#endif
10113 if (!forceit && *mode != 'a' && vim_fexists(fname))
10114 {
10115 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10116 return NULL;
10117 }
10118
10119 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10120 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10121
10122 return fd;
10123}
10124
10125/*
10126 * ":mark" and ":k".
10127 */
10128 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010129ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130{
10131 pos_T pos;
10132
10133 if (*eap->arg == NUL) /* No argument? */
10134 EMSG(_(e_argreq));
10135 else if (eap->arg[1] != NUL) /* more than one character? */
10136 EMSG(_(e_trailing));
10137 else
10138 {
10139 pos = curwin->w_cursor; /* save curwin->w_cursor */
10140 curwin->w_cursor.lnum = eap->line2;
10141 beginline(BL_WHITE | BL_FIX);
10142 if (setmark(*eap->arg) == FAIL) /* set mark */
10143 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10144 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10145 }
10146}
10147
10148/*
10149 * Update w_topline, w_leftcol and the cursor position.
10150 */
10151 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010152update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153{
10154 check_cursor(); /* put cursor on valid line */
10155 update_topline();
10156 if (!curwin->w_p_wrap)
10157 validate_cursor();
10158 update_curswant();
10159}
10160
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010162 * Save the current State and go to Normal mode.
10163 * Return TRUE if the typeahead could be saved.
10164 */
10165 int
10166save_current_state(save_state_T *sst)
10167{
10168 sst->save_msg_scroll = msg_scroll;
10169 sst->save_restart_edit = restart_edit;
10170 sst->save_msg_didout = msg_didout;
10171 sst->save_State = State;
10172 sst->save_insertmode = p_im;
10173 sst->save_finish_op = finish_op;
10174 sst->save_opcount = opcount;
10175
10176 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10177 restart_edit = 0; /* don't go to Insert mode */
10178 p_im = FALSE; /* don't use 'insertmode' */
10179
10180 /*
10181 * Save the current typeahead. This is required to allow using ":normal"
10182 * from an event handler and makes sure we don't hang when the argument
10183 * ends with half a command.
10184 */
10185 save_typeahead(&sst->tabuf);
10186 return sst->tabuf.typebuf_valid;
10187}
10188
10189 void
10190restore_current_state(save_state_T *sst)
10191{
10192 /* Restore the previous typeahead. */
10193 restore_typeahead(&sst->tabuf);
10194
10195 msg_scroll = sst->save_msg_scroll;
10196 restart_edit = sst->save_restart_edit;
10197 p_im = sst->save_insertmode;
10198 finish_op = sst->save_finish_op;
10199 opcount = sst->save_opcount;
10200 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10201
10202 /* Restore the state (needed when called from a function executed for
10203 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10204 State = sst->save_State;
10205#ifdef CURSOR_SHAPE
10206 ui_cursor_shape(); /* may show different cursor shape */
10207#endif
10208}
10209
10210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 * ":normal[!] {commands}": Execute normal mode commands.
10212 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010213 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010214ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010216 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217#ifdef FEAT_MBYTE
10218 char_u *arg = NULL;
10219 int l;
10220 char_u *p;
10221#endif
10222
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010223 if (ex_normal_lock > 0)
10224 {
10225 EMSG(_(e_secure));
10226 return;
10227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 if (ex_normal_busy >= p_mmd)
10229 {
10230 EMSG(_("E192: Recursive use of :normal too deep"));
10231 return;
10232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233
10234#ifdef FEAT_MBYTE
10235 /*
10236 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10237 * this for the K_SPECIAL leading byte, otherwise special keys will not
10238 * work.
10239 */
10240 if (has_mbyte)
10241 {
10242 int len = 0;
10243
10244 /* Count the number of characters to be escaped. */
10245 for (p = eap->arg; *p != NUL; ++p)
10246 {
10247# ifdef FEAT_GUI
10248 if (*p == CSI) /* leadbyte CSI */
10249 len += 2;
10250# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010251 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10253# ifdef FEAT_GUI
10254 || *p == CSI
10255# endif
10256 )
10257 len += 2;
10258 }
10259 if (len > 0)
10260 {
10261 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10262 if (arg != NULL)
10263 {
10264 len = 0;
10265 for (p = eap->arg; *p != NUL; ++p)
10266 {
10267 arg[len++] = *p;
10268# ifdef FEAT_GUI
10269 if (*p == CSI)
10270 {
10271 arg[len++] = KS_EXTRA;
10272 arg[len++] = (int)KE_CSI;
10273 }
10274# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010275 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 {
10277 arg[len++] = *++p;
10278 if (*p == K_SPECIAL)
10279 {
10280 arg[len++] = KS_SPECIAL;
10281 arg[len++] = KE_FILLER;
10282 }
10283# ifdef FEAT_GUI
10284 else if (*p == CSI)
10285 {
10286 arg[len++] = KS_EXTRA;
10287 arg[len++] = (int)KE_CSI;
10288 }
10289# endif
10290 }
10291 arg[len] = NUL;
10292 }
10293 }
10294 }
10295 }
10296#endif
10297
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010298 ++ex_normal_busy;
10299 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 {
10301 /*
10302 * Repeat the :normal command for each line in the range. When no
10303 * range given, execute it just once, without positioning the cursor
10304 * first.
10305 */
10306 do
10307 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 if (eap->addr_count != 0)
10309 {
10310 curwin->w_cursor.lnum = eap->line1++;
10311 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010312 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 }
10314
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010315 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316#ifdef FEAT_MBYTE
10317 arg != NULL ? arg :
10318#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010319 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 }
10321 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10322 }
10323
10324 /* Might not return to the main loop when in an event handler. */
10325 update_topline_cursor();
10326
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010327 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010329#ifdef FEAT_MOUSE
10330 setmouse();
10331#endif
10332#ifdef CURSOR_SHAPE
10333 ui_cursor_shape(); /* may show different cursor shape */
10334#endif
10335
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336#ifdef FEAT_MBYTE
10337 vim_free(arg);
10338#endif
10339}
10340
10341/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010342 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 */
10344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010345ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010347 if (eap->forceit)
10348 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010349 /* cursor line can be zero on startup */
10350 if (!curwin->w_cursor.lnum)
10351 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010352 coladvance((colnr_T)MAXCOL);
10353 curwin->w_curswant = MAXCOL;
10354 curwin->w_set_curswant = FALSE;
10355 }
10356
Bram Moolenaara40c5002005-01-09 21:16:21 +000010357 /* Ignore the command when already in Insert mode. Inserting an
10358 * expression register that invokes a function can do this. */
10359 if (State & INSERT)
10360 return;
10361
Bram Moolenaar64969662005-12-14 21:59:55 +000010362 if (eap->cmdidx == CMD_startinsert)
10363 restart_edit = 'a';
10364 else if (eap->cmdidx == CMD_startreplace)
10365 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010367 restart_edit = 'V';
10368
10369 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010371 if (eap->cmdidx == CMD_startinsert)
10372 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 curwin->w_curswant = 0; /* avoid MAXCOL */
10374 }
10375}
10376
10377/*
10378 * ":stopinsert"
10379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010381ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382{
10383 restart_edit = 0;
10384 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010385 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010388/*
10389 * Execute normal mode command "cmd".
10390 * "remap" can be REMAP_NONE or REMAP_YES.
10391 */
10392 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010393exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010394{
Bram Moolenaar25281632016-01-21 23:32:32 +010010395 /* Stuff the argument into the typeahead buffer. */
10396 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010397 exec_normal(FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010398}
Bram Moolenaar25281632016-01-21 23:32:32 +010010399
Bram Moolenaar25281632016-01-21 23:32:32 +010010400/*
10401 * Execute normal_cmd() until there is no typeahead left.
10402 */
10403 void
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010404exec_normal(int was_typed, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010405{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010406 oparg_T oa;
10407
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010408 clear_oparg(&oa);
10409 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010410 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10411 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010412 {
10413 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010414#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010415 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010416 && oa.op_type == OP_NOP && oa.regname == NUL
10417 && !VIsual_active)
10418 {
10419 /* If terminal_loop() returns OK we got a key that is handled
10420 * in Normal model. With FAIL we first need to position the
10421 * cursor and the screen needs to be redrawn. */
10422 if (terminal_loop(TRUE) == OK)
10423 normal_cmd(&oa, TRUE);
10424 }
10425 else
10426#endif
10427 /* execute a Normal mode cmd */
10428 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010429 }
10430}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010431
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432#ifdef FEAT_FIND_ID
10433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010434ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435{
10436 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10437 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10438 (linenr_T)1, (linenr_T)MAXLNUM);
10439}
10440
Bram Moolenaar4033c552017-09-16 20:54:51 +020010441#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442/*
10443 * ":psearch"
10444 */
10445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010446ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447{
10448 g_do_tagpreview = p_pvh;
10449 ex_findpat(eap);
10450 g_do_tagpreview = 0;
10451}
10452#endif
10453
10454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010455ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456{
10457 int whole = TRUE;
10458 long n;
10459 char_u *p;
10460 int action;
10461
10462 switch (cmdnames[eap->cmdidx].cmd_name[2])
10463 {
10464 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10465 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10466 action = ACTION_GOTO;
10467 else
10468 action = ACTION_SHOW;
10469 break;
10470 case 'i': /* ":ilist" and ":dlist" */
10471 action = ACTION_SHOW_ALL;
10472 break;
10473 case 'u': /* ":ijump" and ":djump" */
10474 action = ACTION_GOTO;
10475 break;
10476 default: /* ":isplit" and ":dsplit" */
10477 action = ACTION_SPLIT;
10478 break;
10479 }
10480
10481 n = 1;
10482 if (vim_isdigit(*eap->arg)) /* get count */
10483 {
10484 n = getdigits(&eap->arg);
10485 eap->arg = skipwhite(eap->arg);
10486 }
10487 if (*eap->arg == '/') /* Match regexp, not just whole words */
10488 {
10489 whole = FALSE;
10490 ++eap->arg;
10491 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10492 if (*p)
10493 {
10494 *p++ = NUL;
10495 p = skipwhite(p);
10496
10497 /* Check for trailing illegal characters */
10498 if (!ends_excmd(*p))
10499 eap->errmsg = e_trailing;
10500 else
10501 eap->nextcmd = check_nextcmd(p);
10502 }
10503 }
10504 if (!eap->skip)
10505 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10506 whole, !eap->forceit,
10507 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10508 n, action, eap->line1, eap->line2);
10509}
10510#endif
10511
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512
Bram Moolenaar4033c552017-09-16 20:54:51 +020010513#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514/*
10515 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10516 */
10517 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010518ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010520 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10522}
10523
10524/*
10525 * ":pedit"
10526 */
10527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010528ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529{
10530 win_T *curwin_save = curwin;
10531
10532 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010533 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010534 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 do_exedit(eap, NULL);
10536 keep_help_flag = FALSE;
10537 if (curwin != curwin_save && win_valid(curwin_save))
10538 {
10539 /* Return cursor to where we were */
10540 validate_cursor();
10541 redraw_later(VALID);
10542 win_enter(curwin_save, TRUE);
10543 }
10544 g_do_tagpreview = 0;
10545}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547
10548/*
10549 * ":stag", ":stselect" and ":stjump".
10550 */
10551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010552ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553{
10554 postponed_split = -1;
10555 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010556 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10558 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010559 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561
10562/*
10563 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10564 */
10565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010566ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567{
10568 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10569}
10570
10571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010572ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573{
10574 int cmd;
10575
10576 switch (name[1])
10577 {
10578 case 'j': cmd = DT_JUMP; /* ":tjump" */
10579 break;
10580 case 's': cmd = DT_SELECT; /* ":tselect" */
10581 break;
10582 case 'p': cmd = DT_PREV; /* ":tprevious" */
10583 break;
10584 case 'N': cmd = DT_PREV; /* ":tNext" */
10585 break;
10586 case 'n': cmd = DT_NEXT; /* ":tnext" */
10587 break;
10588 case 'o': cmd = DT_POP; /* ":pop" */
10589 break;
10590 case 'f': /* ":tfirst" */
10591 case 'r': cmd = DT_FIRST; /* ":trewind" */
10592 break;
10593 case 'l': cmd = DT_LAST; /* ":tlast" */
10594 break;
10595 default: /* ":tag" */
10596#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010597 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010599 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 return;
10601 }
10602#endif
10603 cmd = DT_TAG;
10604 break;
10605 }
10606
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010607 if (name[0] == 'l')
10608 {
10609#ifndef FEAT_QUICKFIX
10610 ex_ni(eap);
10611 return;
10612#else
10613 cmd = DT_LTAG;
10614#endif
10615 }
10616
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10618 eap->forceit, TRUE);
10619}
10620
10621/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010622 * Check "str" for starting with a special cmdline variable.
10623 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10624 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10625 */
10626 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010627find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010628{
10629 int len;
10630 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010631 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010632 "%",
10633#define SPEC_PERC 0
10634 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010635#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010636 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010637#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010638 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010639#define SPEC_CCWORD (SPEC_CWORD + 1)
10640 "<cexpr>", /* expr under cursor */
10641#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010642 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010643#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010644 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010645#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010646 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010647#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010648 "<afile>", /* autocommand file name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010649#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010650 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010651#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010652 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010653#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010654#ifdef FEAT_CLIENTSERVER
10655 "<client>"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010656# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010657#endif
10658 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010659
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010660 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010661 {
10662 len = (int)STRLEN(spec_str[i]);
10663 if (STRNCMP(src, spec_str[i], len) == 0)
10664 {
10665 *usedlen = len;
10666 return i;
10667 }
10668 }
10669 return -1;
10670}
10671
10672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 * Evaluate cmdline variables.
10674 *
10675 * change '%' to curbuf->b_ffname
10676 * '#' to curwin->w_altfile
10677 * '<cword>' to word under the cursor
10678 * '<cWORD>' to WORD under the cursor
10679 * '<cfile>' to path name under the cursor
10680 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010681 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 * '<afile>' to file name for autocommand
10683 * '<abuf>' to buffer number for autocommand
10684 * '<amatch>' to matching name for autocommand
10685 *
10686 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10687 * "" for error without a message) and NULL is returned.
10688 * Returns an allocated string if a valid match was found.
10689 * Returns NULL if no match was found. "usedlen" then still contains the
10690 * number of characters to skip.
10691 */
10692 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010693eval_vars(
10694 char_u *src, /* pointer into commandline */
10695 char_u *srcstart, /* beginning of valid memory for src */
10696 int *usedlen, /* characters after src that are used */
10697 linenr_T *lnump, /* line number for :e command, or NULL */
10698 char_u **errormsg, /* pointer to error message */
10699 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010700 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701{
10702 int i;
10703 char_u *s;
10704 char_u *result;
10705 char_u *resultbuf = NULL;
10706 int resultlen;
10707 buf_T *buf;
10708 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10709 int spec_idx;
10710#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010711 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712 int skip_mod = FALSE;
10713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715
10716 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010717 if (escaped != NULL)
10718 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719
10720 /*
10721 * Check if there is something to do.
10722 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010723 spec_idx = find_cmdline_var(src, usedlen);
10724 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 {
10726 *usedlen = 1;
10727 return NULL;
10728 }
10729
10730 /*
10731 * Skip when preceded with a backslash "\%" and "\#".
10732 * Note: In "\\%" the % is also not recognized!
10733 */
10734 if (src > srcstart && src[-1] == '\\')
10735 {
10736 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010737 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 return NULL;
10739 }
10740
10741 /*
10742 * word or WORD under cursor
10743 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010744 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10745 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010747 resultlen = find_ident_under_cursor(&result,
10748 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10749 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10750 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 if (resultlen == 0)
10752 {
10753 *errormsg = (char_u *)"";
10754 return NULL;
10755 }
10756 }
10757
10758 /*
10759 * '#': Alternate file name
10760 * '%': Current file name
10761 * File name under the cursor
10762 * File name for autocommand
10763 * and following modifiers
10764 */
10765 else
10766 {
10767 switch (spec_idx)
10768 {
10769 case SPEC_PERC: /* '%': current file */
10770 if (curbuf->b_fname == NULL)
10771 {
10772 result = (char_u *)"";
10773 valid = 0; /* Must have ":p:h" to be valid */
10774 }
10775 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010776 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010778#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010779 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010780#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 break;
10783
10784 case SPEC_HASH: /* '#' or "#99": alternate file */
10785 if (src[1] == '#') /* "##": the argument list */
10786 {
10787 result = arg_all();
10788 resultbuf = result;
10789 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010790 if (escaped != NULL)
10791 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792#ifdef FEAT_MODIFY_FNAME
10793 skip_mod = TRUE;
10794#endif
10795 break;
10796 }
10797 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010798 if (*s == '<') /* "#<99" uses v:oldfiles */
10799 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010801 if (s == src + 2 && src[1] == '-')
10802 /* just a minus sign, don't skip over it */
10803 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 *usedlen = (int)(s - src); /* length of what we expand */
10805
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010806 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010808 if (*usedlen < 2)
10809 {
10810 /* Should we give an error message for #<text? */
10811 *usedlen = 1;
10812 return NULL;
10813 }
10814#ifdef FEAT_EVAL
10815 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10816 (long)i);
10817 if (result == NULL)
10818 {
10819 *errormsg = (char_u *)"";
10820 return NULL;
10821 }
10822#else
10823 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 }
10827 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010828 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010829 if (i == 0 && src[1] == '<' && *usedlen > 1)
10830 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010831 buf = buflist_findnr(i);
10832 if (buf == NULL)
10833 {
10834 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10835 return NULL;
10836 }
10837 if (lnump != NULL)
10838 *lnump = ECMD_LAST;
10839 if (buf->b_fname == NULL)
10840 {
10841 result = (char_u *)"";
10842 valid = 0; /* Must have ":p:h" to be valid */
10843 }
10844 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010845 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010846 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010847#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010848 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010849#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010850 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 break;
10853
10854#ifdef FEAT_SEARCHPATH
10855 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010856 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 if (result == NULL)
10858 {
10859 *errormsg = (char_u *)"";
10860 return NULL;
10861 }
10862 resultbuf = result; /* remember allocated string */
10863 break;
10864#endif
10865
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 case SPEC_AFILE: /* file name for autocommand */
10867 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010868 if (result != NULL && !autocmd_fname_full)
10869 {
10870 /* Still need to turn the fname into a full path. It is
10871 * postponed to avoid a delay when <afile> is not used. */
10872 autocmd_fname_full = TRUE;
10873 result = FullName_save(autocmd_fname, FALSE);
10874 vim_free(autocmd_fname);
10875 autocmd_fname = result;
10876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 if (result == NULL)
10878 {
10879 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10880 return NULL;
10881 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010882 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 break;
10884
10885 case SPEC_ABUF: /* buffer number for autocommand */
10886 if (autocmd_bufnr <= 0)
10887 {
10888 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10889 return NULL;
10890 }
10891 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10892 result = strbuf;
10893 break;
10894
10895 case SPEC_AMATCH: /* match name for autocommand */
10896 result = autocmd_match;
10897 if (result == NULL)
10898 {
10899 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10900 return NULL;
10901 }
10902 break;
10903
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 case SPEC_SFILE: /* file name for ":so" command */
10905 result = sourcing_name;
10906 if (result == NULL)
10907 {
10908 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10909 return NULL;
10910 }
10911 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010912 case SPEC_SLNUM: /* line in file for ":so" command */
10913 if (sourcing_name == NULL || sourcing_lnum == 0)
10914 {
10915 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10916 return NULL;
10917 }
10918 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10919 result = strbuf;
10920 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921#if defined(FEAT_CLIENTSERVER)
10922 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010923 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10924 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 result = strbuf;
10926 break;
10927#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010928 default:
10929 result = (char_u *)""; /* avoid gcc warning */
10930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 }
10932
10933 resultlen = (int)STRLEN(result); /* length of new string */
10934 if (src[*usedlen] == '<') /* remove the file name extension */
10935 {
10936 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 resultlen = (int)(s - result);
10939 }
10940#ifdef FEAT_MODIFY_FNAME
10941 else if (!skip_mod)
10942 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010943 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 &resultlen);
10945 if (result == NULL)
10946 {
10947 *errormsg = (char_u *)"";
10948 return NULL;
10949 }
10950 }
10951#endif
10952 }
10953
10954 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10955 {
10956 if (valid != VALID_HEAD + VALID_PATH)
10957 /* xgettext:no-c-format */
10958 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10959 else
10960 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10961 result = NULL;
10962 }
10963 else
10964 result = vim_strnsave(result, resultlen);
10965 vim_free(resultbuf);
10966 return result;
10967}
10968
10969/*
10970 * Concatenate all files in the argument list, separated by spaces, and return
10971 * it in one allocated string.
10972 * Spaces and backslashes in the file names are escaped with a backslash.
10973 * Returns NULL when out of memory.
10974 */
10975 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010976arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977{
10978 int len;
10979 int idx;
10980 char_u *retval = NULL;
10981 char_u *p;
10982
10983 /*
10984 * Do this loop two times:
10985 * first time: compute the total length
10986 * second time: concatenate the names
10987 */
10988 for (;;)
10989 {
10990 len = 0;
10991 for (idx = 0; idx < ARGCOUNT; ++idx)
10992 {
10993 p = alist_name(&ARGLIST[idx]);
10994 if (p != NULL)
10995 {
10996 if (len > 0)
10997 {
10998 /* insert a space in between names */
10999 if (retval != NULL)
11000 retval[len] = ' ';
11001 ++len;
11002 }
11003 for ( ; *p != NUL; ++p)
11004 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011005 if (*p == ' '
11006#ifndef BACKSLASH_IN_FILENAME
11007 || *p == '\\'
11008#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011009 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 {
11011 /* insert a backslash */
11012 if (retval != NULL)
11013 retval[len] = '\\';
11014 ++len;
11015 }
11016 if (retval != NULL)
11017 retval[len] = *p;
11018 ++len;
11019 }
11020 }
11021 }
11022
11023 /* second time: break here */
11024 if (retval != NULL)
11025 {
11026 retval[len] = NUL;
11027 break;
11028 }
11029
11030 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011031 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 if (retval == NULL)
11033 break;
11034 }
11035
11036 return retval;
11037}
11038
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039/*
11040 * Expand the <sfile> string in "arg".
11041 *
11042 * Returns an allocated string, or NULL for any error.
11043 */
11044 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011045expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046{
11047 char_u *errormsg;
11048 int len;
11049 char_u *result;
11050 char_u *newres;
11051 char_u *repl;
11052 int srclen;
11053 char_u *p;
11054
11055 result = vim_strsave(arg);
11056 if (result == NULL)
11057 return NULL;
11058
11059 for (p = result; *p; )
11060 {
11061 if (STRNCMP(p, "<sfile>", 7) != 0)
11062 ++p;
11063 else
11064 {
11065 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011066 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 if (errormsg != NULL)
11068 {
11069 if (*errormsg)
11070 emsg(errormsg);
11071 vim_free(result);
11072 return NULL;
11073 }
11074 if (repl == NULL) /* no match (cannot happen) */
11075 {
11076 p += srclen;
11077 continue;
11078 }
11079 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11080 newres = alloc(len);
11081 if (newres == NULL)
11082 {
11083 vim_free(repl);
11084 vim_free(result);
11085 return NULL;
11086 }
11087 mch_memmove(newres, result, (size_t)(p - result));
11088 STRCPY(newres + (p - result), repl);
11089 len = (int)STRLEN(newres);
11090 STRCAT(newres, p + srclen);
11091 vim_free(repl);
11092 vim_free(result);
11093 result = newres;
11094 p = newres + len; /* continue after the match */
11095 }
11096 }
11097
11098 return result;
11099}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100
11101#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011102static int ses_winsizes(FILE *fd, int restore_size,
11103 win_T *tab_firstwin);
11104static int ses_win_rec(FILE *fd, frame_T *fr);
11105static frame_T *ses_skipframe(frame_T *fr);
11106static int ses_do_frame(frame_T *fr);
11107static int ses_do_win(win_T *wp);
11108static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11109static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011110static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111
11112/*
11113 * Write openfile commands for the current buffers to an .exrc file.
11114 * Return FAIL on error, OK otherwise.
11115 */
11116 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011117makeopens(
11118 FILE *fd,
11119 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120{
11121 buf_T *buf;
11122 int only_save_windows = TRUE;
11123 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 int restore_size = TRUE;
11125 win_T *wp;
11126 char_u *sname;
11127 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011128 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011129 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011130 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011131 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011132 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011133 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134
11135 if (ssop_flags & SSOP_BUFFERS)
11136 only_save_windows = FALSE; /* Save ALL buffers */
11137
11138 /*
11139 * Begin by setting the this_session variable, and then other
11140 * sessionable variables.
11141 */
11142#ifdef FEAT_EVAL
11143 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11144 return FAIL;
11145 if (ssop_flags & SSOP_GLOBALS)
11146 if (store_session_globals(fd) == FAIL)
11147 return FAIL;
11148#endif
11149
11150 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011151 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 */
11153 if (put_line(fd, "silent only") == FAIL)
11154 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011155 if ((ssop_flags & SSOP_TABPAGES)
11156 && put_line(fd, "silent tabonly") == FAIL)
11157 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158
11159 /*
11160 * Now a :cd command to the session directory or the current directory
11161 */
11162 if (ssop_flags & SSOP_SESDIR)
11163 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011164 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11165 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 return FAIL;
11167 }
11168 else if (ssop_flags & SSOP_CURDIR)
11169 {
11170 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11171 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011172 || fputs("cd ", fd) < 0
11173 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11174 || put_eol(fd) == FAIL)
11175 {
11176 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 vim_free(sname);
11180 }
11181
11182 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011183 * If there is an empty, unnamed buffer we will wipe it out later.
11184 * Remember the buffer number.
11185 */
11186 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11187 return FAIL;
11188 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11189 return FAIL;
11190 if (put_line(fd, "endif") == FAIL)
11191 return FAIL;
11192
11193 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194 * Now save the current files, current buffer first.
11195 */
11196 if (put_line(fd, "set shortmess=aoO") == FAIL)
11197 return FAIL;
11198
11199 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011200 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201 {
11202 if (!(only_save_windows && buf->b_nwindows == 0)
11203 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011204#ifdef FEAT_TERMINAL
11205 /* skip terminal buffers: finished ones are not useful, others
11206 * will be resurrected and result in a new buffer */
11207 && !bt_terminal(buf)
11208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 && buf->b_fname != NULL
11210 && buf->b_p_bl)
11211 {
11212 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11213 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011214 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 return FAIL;
11216 }
11217 }
11218
11219 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011220 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11222 return FAIL;
11223
11224 if (ssop_flags & SSOP_RESIZE)
11225 {
11226 /* Note: after the restore we still check it worked!*/
11227 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11228 || put_eol(fd) == FAIL)
11229 return FAIL;
11230 }
11231
11232#ifdef FEAT_GUI
11233 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11234 {
11235 int x, y;
11236
11237 if (gui_mch_get_winpos(&x, &y) == OK)
11238 {
11239 /* Note: after the restore we still check it worked!*/
11240 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11241 return FAIL;
11242 }
11243 }
11244#endif
11245
11246 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011247 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11248 * will be displayed when creating the next tab. That resizes the windows
11249 * in the first tab, which may cause problems. Set 'showtabline' to 2
11250 * temporarily to avoid that.
11251 */
11252 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11253 {
11254 if (put_line(fd, "set stal=2") == FAIL)
11255 return FAIL;
11256 restore_stal = TRUE;
11257 }
11258
11259 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011260 * May repeat putting Windows for each tab, when "tabpages" is in
11261 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011262 * Don't use goto_tabpage(), it may change directory and trigger
11263 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011265 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011266 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011267 if ((ssop_flags & SSOP_TABPAGES))
11268 {
11269 int num_tabs;
11270
11271 /*
11272 * Similar to ses_win_rec() below, populate the tab pages first so
11273 * later local options won't be copied to the new tabs.
11274 */
11275 for (tabnr = 1; ; ++tabnr)
11276 {
11277 tabpage_T *tp = find_tabpage(tabnr);
11278
11279 if (tp == NULL) /* done all tab pages */
11280 break;
11281
11282 if (tabnr > 1 && put_line(fd, "tabnew") == FAIL)
11283 return FAIL;
11284 }
11285
11286 num_tabs = tabnr - 1;
11287 if (num_tabs > 1 && (fprintf(fd, "tabnext -%d", num_tabs - 1) < 0
11288 || put_eol(fd) == FAIL))
11289 return FAIL;
11290 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011291 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011293 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011294 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011295
Bram Moolenaar18144c82006-04-12 21:52:12 +000011296 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011298 tabpage_T *tp = find_tabpage(tabnr);
11299
11300 if (tp == NULL)
11301 break; /* done all tab pages */
11302 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011303 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011304 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011305 tab_topframe = topframe;
11306 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011307 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011308 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011309 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011310 tab_topframe = tp->tp_topframe;
11311 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011312 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011313 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315
Bram Moolenaar18144c82006-04-12 21:52:12 +000011316 /*
11317 * Before creating the window layout, try loading one file. If this
11318 * is aborted we don't end up with a number of useless windows.
11319 * This may have side effects! (e.g., compressed or network file).
11320 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011321 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011322 {
11323 if (ses_do_win(wp)
11324 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011325 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011326#ifdef FEAT_QUICKFIX
11327 && !bt_nofile(wp->w_buffer)
11328#endif
11329 )
11330 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011331 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11332 return FAIL;
11333 need_tabnext = FALSE;
11334
11335 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011336 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11337 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011338 return FAIL;
11339 if (!wp->w_arg_idx_invalid)
11340 edited_win = wp;
11341 break;
11342 }
11343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011345 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011346 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011347 return FAIL;
11348
Bram Moolenaar18144c82006-04-12 21:52:12 +000011349 /*
11350 * Save current window layout.
11351 */
11352 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011354 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011356 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11357 return FAIL;
11358 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360
Bram Moolenaar18144c82006-04-12 21:52:12 +000011361 /*
11362 * Check if window sizes can be restored (no windows omitted).
11363 * Remember the window number of the current window after restoring.
11364 */
11365 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011366 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011367 {
11368 if (ses_do_win(wp))
11369 ++nr;
11370 else
11371 restore_size = FALSE;
11372 if (curwin == wp)
11373 cnr = nr;
11374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375
Bram Moolenaar18144c82006-04-12 21:52:12 +000011376 /* Go to the first window. */
11377 if (put_line(fd, "wincmd t") == FAIL)
11378 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011379
Bram Moolenaar18144c82006-04-12 21:52:12 +000011380 /*
11381 * If more than one window, see if sizes can be restored.
11382 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11383 * resized when moving between windows.
11384 * Do this before restoring the view, so that the topline and the
11385 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011386 * winminheight and winminwidth need to be set to avoid an error if the
11387 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011388 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011389 if (put_line(fd, "set winminheight=0") == FAIL
11390 || put_line(fd, "set winheight=1") == FAIL
11391 || put_line(fd, "set winminwidth=0") == FAIL
11392 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011393 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011394 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011395 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396
Bram Moolenaar18144c82006-04-12 21:52:12 +000011397 /*
11398 * Restore the view of the window (options, file, cursor, etc.).
11399 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011400 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011401 {
11402 if (!ses_do_win(wp))
11403 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011404 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11405 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011406 return FAIL;
11407 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11408 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011409 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011410 }
11411
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011412 /* The argument index in the first tab page is zero, need to set it in
11413 * each window. For further tab pages it's the window where we do
11414 * "tabedit". */
11415 cur_arg_idx = next_arg_idx;
11416
Bram Moolenaar18144c82006-04-12 21:52:12 +000011417 /*
11418 * Restore cursor to the current window if it's not the first one.
11419 */
11420 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11421 || put_eol(fd) == FAIL))
11422 return FAIL;
11423
11424 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011425 * Restore window sizes again after jumping around in windows, because
11426 * the current window has a minimum size while others may not.
11427 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011428 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011429 return FAIL;
11430
Bram Moolenaar18144c82006-04-12 21:52:12 +000011431 /* Don't continue in another tab page when doing only the current one
11432 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011433 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011434 break;
11435 }
11436
11437 if (ssop_flags & SSOP_TABPAGES)
11438 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011439 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11440 || put_eol(fd) == FAIL)
11441 return FAIL;
11442 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011443 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11444 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011445
Bram Moolenaar9c102382006-05-03 21:26:49 +000011446 /*
11447 * Wipe out an empty unnamed buffer we started in.
11448 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011449 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011450 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011451 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011452 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011453 return FAIL;
11454 if (put_line(fd, "endif") == FAIL)
11455 return FAIL;
11456 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11457 return FAIL;
11458
11459 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11460 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11461 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11462 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011463 /* Re-apply 'winminheight' and 'winminwidth'. */
11464 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11465 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11466 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467
11468 /*
11469 * Lastly, execute the x.vim file if it exists.
11470 */
11471 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11472 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011473 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 || put_line(fd, "endif") == FAIL)
11475 return FAIL;
11476
11477 return OK;
11478}
11479
11480 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011481ses_winsizes(
11482 FILE *fd,
11483 int restore_size,
11484 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485{
11486 int n = 0;
11487 win_T *wp;
11488
11489 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11490 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011491 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 {
11493 if (!ses_do_win(wp))
11494 continue;
11495 ++n;
11496
11497 /* restore height when not full height */
11498 if (wp->w_height + wp->w_status_height < topframe->fr_height
11499 && (fprintf(fd,
11500 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11501 n, (long)wp->w_height, Rows / 2, Rows) < 0
11502 || put_eol(fd) == FAIL))
11503 return FAIL;
11504
11505 /* restore width when not full width */
11506 if (wp->w_width < Columns && (fprintf(fd,
11507 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11508 n, (long)wp->w_width, Columns / 2, Columns) < 0
11509 || put_eol(fd) == FAIL))
11510 return FAIL;
11511 }
11512 }
11513 else
11514 {
11515 /* Just equalise window sizes */
11516 if (put_line(fd, "wincmd =") == FAIL)
11517 return FAIL;
11518 }
11519 return OK;
11520}
11521
11522/*
11523 * Write commands to "fd" to recursively create windows for frame "fr",
11524 * horizontally and vertically split.
11525 * After the commands the last window in the frame is the current window.
11526 * Returns FAIL when writing the commands to "fd" fails.
11527 */
11528 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011529ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530{
11531 frame_T *frc;
11532 int count = 0;
11533
11534 if (fr->fr_layout != FR_LEAF)
11535 {
11536 /* Find first frame that's not skipped and then create a window for
11537 * each following one (first frame is already there). */
11538 frc = ses_skipframe(fr->fr_child);
11539 if (frc != NULL)
11540 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11541 {
11542 /* Make window as big as possible so that we have lots of room
11543 * to split. */
11544 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11545 || put_line(fd, fr->fr_layout == FR_COL
11546 ? "split" : "vsplit") == FAIL)
11547 return FAIL;
11548 ++count;
11549 }
11550
11551 /* Go back to the first window. */
11552 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11553 ? "%dwincmd k" : "%dwincmd h", count) < 0
11554 || put_eol(fd) == FAIL))
11555 return FAIL;
11556
11557 /* Recursively create frames/windows in each window of this column or
11558 * row. */
11559 frc = ses_skipframe(fr->fr_child);
11560 while (frc != NULL)
11561 {
11562 ses_win_rec(fd, frc);
11563 frc = ses_skipframe(frc->fr_next);
11564 /* Go to next window. */
11565 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11566 return FAIL;
11567 }
11568 }
11569 return OK;
11570}
11571
11572/*
11573 * Skip frames that don't contain windows we want to save in the Session.
11574 * Returns NULL when there none.
11575 */
11576 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011577ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578{
11579 frame_T *frc;
11580
11581 for (frc = fr; frc != NULL; frc = frc->fr_next)
11582 if (ses_do_frame(frc))
11583 break;
11584 return frc;
11585}
11586
11587/*
11588 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11589 * the Session.
11590 */
11591 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011592ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593{
11594 frame_T *frc;
11595
11596 if (fr->fr_layout == FR_LEAF)
11597 return ses_do_win(fr->fr_win);
11598 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11599 if (ses_do_frame(frc))
11600 return TRUE;
11601 return FALSE;
11602}
11603
11604/*
11605 * Return non-zero if window "wp" is to be stored in the Session.
11606 */
11607 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011608ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011610#ifdef FEAT_TERMINAL
11611 if (bt_terminal(wp->w_buffer))
11612 return !term_is_finished(wp->w_buffer)
11613 && (ssop_flags & SSOP_TERMINAL)
11614 && term_should_restore(wp->w_buffer);
11615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 if (wp->w_buffer->b_fname == NULL
11617#ifdef FEAT_QUICKFIX
11618 /* When 'buftype' is "nofile" can't restore the window contents. */
11619 || bt_nofile(wp->w_buffer)
11620#endif
11621 )
11622 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011623 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624 return (ssop_flags & SSOP_HELP);
11625 return TRUE;
11626}
11627
11628/*
11629 * Write commands to "fd" to restore the view of a window.
11630 * Caller must make sure 'scrolloff' is zero.
11631 */
11632 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011633put_view(
11634 FILE *fd,
11635 win_T *wp,
11636 int add_edit, /* add ":edit" command to view */
11637 unsigned *flagp, /* vop_flags or ssop_flags */
11638 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011639 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640{
11641 win_T *save_curwin;
11642 int f;
11643 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011644 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645
11646 /* Always restore cursor position for ":mksession". For ":mkview" only
11647 * when 'viewoptions' contains "cursor". */
11648 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11649
11650 /*
11651 * Local argument list.
11652 */
11653 if (wp->w_alist == &global_alist)
11654 {
11655 if (put_line(fd, "argglobal") == FAIL)
11656 return FAIL;
11657 }
11658 else
11659 {
11660 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11661 flagp == &vop_flags
11662 || !(*flagp & SSOP_CURDIR)
11663 || wp->w_localdir != NULL, flagp) == FAIL)
11664 return FAIL;
11665 }
11666
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011667 /* Only when part of a session: restore the argument index. Some
11668 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011669 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011670 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011672 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673 || put_eol(fd) == FAIL)
11674 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011675 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676 }
11677
11678 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011679 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011681# ifdef FEAT_TERMINAL
11682 if (bt_terminal(wp->w_buffer))
11683 {
11684 if (term_write_session(fd, wp) == FAIL)
11685 return FAIL;
11686 }
11687 else
11688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 /*
11690 * Load the file.
11691 */
11692 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011693# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011695# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 )
11697 {
11698 /*
11699 * Editing a file in this buffer: use ":edit file".
11700 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011701 *
11702 * Note, if a buffer for that file already exists, use :badd to
11703 * edit that buffer, to not lose folding information (:edit resets
11704 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011706 if (fputs("if bufexists('", fd) < 0
11707 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11708 || fputs("') | buffer ", fd) < 0
11709 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11710 || fputs(" | else | edit ", fd) < 0
11711 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11712 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011713 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714 return FAIL;
11715 }
11716 else
11717 {
11718 /* No file in this buffer, just make it empty. */
11719 if (put_line(fd, "enew") == FAIL)
11720 return FAIL;
11721#ifdef FEAT_QUICKFIX
11722 if (wp->w_buffer->b_ffname != NULL)
11723 {
11724 /* The buffer does have a name, but it's not a file name. */
11725 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011726 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 return FAIL;
11728 }
11729#endif
11730 do_cursor = FALSE;
11731 }
11732 }
11733
11734 /*
11735 * Local mappings and abbreviations.
11736 */
11737 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11738 && makemap(fd, wp->w_buffer) == FAIL)
11739 return FAIL;
11740
11741 /*
11742 * Local options. Need to go to the window temporarily.
11743 * Store only local values when using ":mkview" and when ":mksession" is
11744 * used and 'sessionoptions' doesn't include "options".
11745 * Some folding options are always stored when "folds" is included,
11746 * otherwise the folds would not be restored correctly.
11747 */
11748 save_curwin = curwin;
11749 curwin = wp;
11750 curbuf = curwin->w_buffer;
11751 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11752 f = makeset(fd, OPT_LOCAL,
11753 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11754#ifdef FEAT_FOLDING
11755 else if (*flagp & SSOP_FOLDS)
11756 f = makefoldset(fd);
11757#endif
11758 else
11759 f = OK;
11760 curwin = save_curwin;
11761 curbuf = curwin->w_buffer;
11762 if (f == FAIL)
11763 return FAIL;
11764
11765#ifdef FEAT_FOLDING
11766 /*
11767 * Save Folds when 'buftype' is empty and for help files.
11768 */
11769 if ((*flagp & SSOP_FOLDS)
11770 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011771 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 {
11773 if (put_folds(fd, wp) == FAIL)
11774 return FAIL;
11775 }
11776#endif
11777
11778 /*
11779 * Set the cursor after creating folds, since that moves the cursor.
11780 */
11781 if (do_cursor)
11782 {
11783
11784 /* Restore the cursor line in the file and relatively in the
11785 * window. Don't use "G", it changes the jumplist. */
11786 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11787 (long)wp->w_cursor.lnum,
11788 (long)(wp->w_cursor.lnum - wp->w_topline),
11789 (long)wp->w_height / 2, (long)wp->w_height) < 0
11790 || put_eol(fd) == FAIL
11791 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11792 || put_line(fd, "exe s:l") == FAIL
11793 || put_line(fd, "normal! zt") == FAIL
11794 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11795 || put_eol(fd) == FAIL)
11796 return FAIL;
11797 /* Restore the cursor column and left offset when not wrapping. */
11798 if (wp->w_cursor.col == 0)
11799 {
11800 if (put_line(fd, "normal! 0") == FAIL)
11801 return FAIL;
11802 }
11803 else
11804 {
11805 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11806 {
11807 if (fprintf(fd,
11808 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011809 (long)wp->w_virtcol + 1,
11810 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811 (long)wp->w_width / 2, (long)wp->w_width) < 0
11812 || put_eol(fd) == FAIL
11813 || put_line(fd, "if s:c > 0") == FAIL
11814 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011815 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11816 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 || put_eol(fd) == FAIL
11818 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011819 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820 || put_eol(fd) == FAIL
11821 || put_line(fd, "endif") == FAIL)
11822 return FAIL;
11823 }
11824 else
11825 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011826 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827 || put_eol(fd) == FAIL)
11828 return FAIL;
11829 }
11830 }
11831 }
11832
11833 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011834 * Local directory, if the current flag is not view options or the "curdir"
11835 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011837 if (wp->w_localdir != NULL
11838 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839 {
11840 if (fputs("lcd ", fd) < 0
11841 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11842 || put_eol(fd) == FAIL)
11843 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011844 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 }
11846
11847 return OK;
11848}
11849
11850/*
11851 * Write an argument list to the session file.
11852 * Returns FAIL if writing fails.
11853 */
11854 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011855ses_arglist(
11856 FILE *fd,
11857 char *cmd,
11858 garray_T *gap,
11859 int fullname, /* TRUE: use full path name */
11860 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861{
11862 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011863 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 char_u *s;
11865
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011866 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11867 return FAIL;
11868 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869 return FAIL;
11870 for (i = 0; i < gap->ga_len; ++i)
11871 {
11872 /* NULL file names are skipped (only happens when out of memory). */
11873 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11874 if (s != NULL)
11875 {
11876 if (fullname)
11877 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011878 buf = alloc(MAXPATHL);
11879 if (buf != NULL)
11880 {
11881 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11882 s = buf;
11883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011885 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011886 || ses_put_fname(fd, s, flagp) == FAIL
11887 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011888 {
11889 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011891 }
11892 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893 }
11894 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011895 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896}
11897
11898/*
11899 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011900 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901 * Returns FAIL if writing fails.
11902 */
11903 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011904ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905{
11906 char_u *name;
11907
11908 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011909 * the session file will be sourced.
11910 * Don't do this for ":mkview", we don't know the current directory.
11911 * Don't do this after ":lcd", we don't keep track of what the current
11912 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 if (buf->b_sfname != NULL
11914 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011915 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011916#ifdef FEAT_AUTOCHDIR
11917 && !p_acd
11918#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011919 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920 name = buf->b_sfname;
11921 else
11922 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011923 if (ses_put_fname(fd, name, flagp) == FAIL
11924 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925 return FAIL;
11926 return OK;
11927}
11928
11929/*
11930 * Write a file name to the session file.
11931 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11932 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011933 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934 */
11935 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011936ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937{
11938 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011939 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941
11942 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011943 if (sname == NULL)
11944 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011946 if (*flagp & SSOP_SLASH)
11947 {
11948 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011949 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011950 if (*p == '\\')
11951 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011953
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011954 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011955 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011957 if (p == NULL)
11958 return FAIL;
11959
11960 /* write the result */
11961 if (fputs((char *)p, fd) < 0)
11962 retval = FAIL;
11963
11964 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965 return retval;
11966}
11967
11968/*
11969 * ":loadview [nr]"
11970 */
11971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011972ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973{
11974 char_u *fname;
11975
11976 fname = get_view_file(*eap->arg);
11977 if (fname != NULL)
11978 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011979 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980 vim_free(fname);
11981 }
11982}
11983
11984/*
11985 * Get the name of the view file for the current buffer.
11986 */
11987 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011988get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989{
11990 int len = 0;
11991 char_u *p, *s;
11992 char_u *retval;
11993 char_u *sname;
11994
11995 if (curbuf->b_ffname == NULL)
11996 {
11997 EMSG(_(e_noname));
11998 return NULL;
11999 }
12000 sname = home_replace_save(NULL, curbuf->b_ffname);
12001 if (sname == NULL)
12002 return NULL;
12003
12004 /*
12005 * We want a file name without separators, because we're not going to make
12006 * a directory.
12007 * "normal" path separator -> "=+"
12008 * "=" -> "=="
12009 * ":" path separator -> "=-"
12010 */
12011 for (p = sname; *p; ++p)
12012 if (*p == '=' || vim_ispathsep(*p))
12013 ++len;
12014 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12015 if (retval != NULL)
12016 {
12017 STRCPY(retval, p_vdir);
12018 add_pathsep(retval);
12019 s = retval + STRLEN(retval);
12020 for (p = sname; *p; ++p)
12021 {
12022 if (*p == '=')
12023 {
12024 *s++ = '=';
12025 *s++ = '=';
12026 }
12027 else if (vim_ispathsep(*p))
12028 {
12029 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012030#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031 if (*p == ':')
12032 *s++ = '-';
12033 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012035 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036 }
12037 else
12038 *s++ = *p;
12039 }
12040 *s++ = '=';
12041 *s++ = c;
12042 STRCPY(s, ".vim");
12043 }
12044
12045 vim_free(sname);
12046 return retval;
12047}
12048
12049#endif /* FEAT_SESSION */
12050
12051/*
12052 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12053 * Return FAIL for a write error.
12054 */
12055 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012056put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057{
12058 if (
12059#ifdef USE_CRNL
12060 (
12061# ifdef MKSESSION_NL
12062 !mksession_nl &&
12063# endif
12064 (putc('\r', fd) < 0)) ||
12065#endif
12066 (putc('\n', fd) < 0))
12067 return FAIL;
12068 return OK;
12069}
12070
12071/*
12072 * Write a line to "fd".
12073 * Return FAIL for a write error.
12074 */
12075 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012076put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077{
12078 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12079 return FAIL;
12080 return OK;
12081}
12082
12083#ifdef FEAT_VIMINFO
12084/*
12085 * ":rviminfo" and ":wviminfo".
12086 */
12087 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012088ex_viminfo(
12089 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090{
12091 char_u *save_viminfo;
12092
12093 save_viminfo = p_viminfo;
12094 if (*p_viminfo == NUL)
12095 p_viminfo = (char_u *)"'100";
12096 if (eap->cmdidx == CMD_rviminfo)
12097 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012098 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12099 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 EMSG(_("E195: Cannot open viminfo file for reading"));
12101 }
12102 else
12103 write_viminfo(eap->arg, eap->forceit);
12104 p_viminfo = save_viminfo;
12105}
12106#endif
12107
12108#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012109/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012110 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012111 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012114dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 if (fname == NULL)
12117 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012118 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119}
12120#endif
12121
12122/*
12123 * ":behave {mswin,xterm}"
12124 */
12125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012126ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127{
12128 if (STRCMP(eap->arg, "mswin") == 0)
12129 {
12130 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12131 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12132 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12133 set_option_value((char_u *)"keymodel", 0L,
12134 (char_u *)"startsel,stopsel", 0);
12135 }
12136 else if (STRCMP(eap->arg, "xterm") == 0)
12137 {
12138 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12139 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12140 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12141 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12142 }
12143 else
12144 EMSG2(_(e_invarg2), eap->arg);
12145}
12146
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012147#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12148/*
12149 * Function given to ExpandGeneric() to obtain the possible arguments of the
12150 * ":behave {mswin,xterm}" command.
12151 */
12152 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012153get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012154{
12155 if (idx == 0)
12156 return (char_u *)"mswin";
12157 if (idx == 1)
12158 return (char_u *)"xterm";
12159 return NULL;
12160}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012161
12162/*
12163 * Function given to ExpandGeneric() to obtain the possible arguments of the
12164 * ":messages {clear}" command.
12165 */
12166 char_u *
12167get_messages_arg(expand_T *xp UNUSED, int idx)
12168{
12169 if (idx == 0)
12170 return (char_u *)"clear";
12171 return NULL;
12172}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012173#endif
12174
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012175 char_u *
12176get_mapclear_arg(expand_T *xp UNUSED, int idx)
12177{
12178 if (idx == 0)
12179 return (char_u *)"<buffer>";
12180 return NULL;
12181}
12182
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183static int filetype_detect = FALSE;
12184static int filetype_plugin = FALSE;
12185static int filetype_indent = FALSE;
12186
12187/*
12188 * ":filetype [plugin] [indent] {on,off,detect}"
12189 * on: Load the filetype.vim file to install autocommands for file types.
12190 * off: Load the ftoff.vim file to remove all autocommands for file types.
12191 * plugin on: load filetype.vim and ftplugin.vim
12192 * plugin off: load ftplugof.vim
12193 * indent on: load filetype.vim and indent.vim
12194 * indent off: load indoff.vim
12195 */
12196 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012197ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198{
12199 char_u *arg = eap->arg;
12200 int plugin = FALSE;
12201 int indent = FALSE;
12202
12203 if (*eap->arg == NUL)
12204 {
12205 /* Print current status. */
12206 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12207 filetype_detect ? "ON" : "OFF",
12208 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12209 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12210 return;
12211 }
12212
12213 /* Accept "plugin" and "indent" in any order. */
12214 for (;;)
12215 {
12216 if (STRNCMP(arg, "plugin", 6) == 0)
12217 {
12218 plugin = TRUE;
12219 arg = skipwhite(arg + 6);
12220 continue;
12221 }
12222 if (STRNCMP(arg, "indent", 6) == 0)
12223 {
12224 indent = TRUE;
12225 arg = skipwhite(arg + 6);
12226 continue;
12227 }
12228 break;
12229 }
12230 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12231 {
12232 if (*arg == 'o' || !filetype_detect)
12233 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012234 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235 filetype_detect = TRUE;
12236 if (plugin)
12237 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012238 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239 filetype_plugin = TRUE;
12240 }
12241 if (indent)
12242 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012243 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244 filetype_indent = TRUE;
12245 }
12246 }
12247 if (*arg == 'd')
12248 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012249 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012250 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251 }
12252 }
12253 else if (STRCMP(arg, "off") == 0)
12254 {
12255 if (plugin || indent)
12256 {
12257 if (plugin)
12258 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012259 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 filetype_plugin = FALSE;
12261 }
12262 if (indent)
12263 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012264 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265 filetype_indent = FALSE;
12266 }
12267 }
12268 else
12269 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012270 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271 filetype_detect = FALSE;
12272 }
12273 }
12274 else
12275 EMSG2(_(e_invarg2), arg);
12276}
12277
12278/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012279 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 */
12281 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012282ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283{
12284 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012285 {
12286 char_u *arg = eap->arg;
12287
12288 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12289 arg += 9;
12290
12291 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12292 if (arg != eap->arg)
12293 did_filetype = FALSE;
12294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012298ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299{
12300#ifdef FEAT_DIGRAPHS
12301 if (*eap->arg != NUL)
12302 putdigraph(eap->arg);
12303 else
12304 listdigraphs();
12305#else
12306 EMSG(_("E196: No digraphs in this version"));
12307#endif
12308}
12309
12310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012311ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312{
12313 int flags = 0;
12314
12315 if (eap->cmdidx == CMD_setlocal)
12316 flags = OPT_LOCAL;
12317 else if (eap->cmdidx == CMD_setglobal)
12318 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012319#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320 if (cmdmod.browse && flags == 0)
12321 ex_options(eap);
12322 else
12323#endif
12324 (void)do_set(eap->arg, flags);
12325}
12326
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012327#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12328 void
12329set_no_hlsearch(int flag)
12330{
12331 no_hlsearch = flag;
12332# ifdef FEAT_EVAL
12333 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12334# endif
12335}
12336
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337/*
12338 * ":nohlsearch"
12339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012341ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012343 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012344 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345}
12346
12347/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012348 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 * Sets nextcmd to the start of the next command, if any. Also called when
12350 * skipping commands to find the next command.
12351 */
12352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012353ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354{
12355 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012356 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357 char_u *end;
12358 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012359 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012360
12361 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012362 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012363 else
12364 {
12365 EMSG(e_invcmd);
12366 return;
12367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368
12369 /* First clear any old pattern. */
12370 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012371 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372
12373 if (ends_excmd(*eap->arg))
12374 end = eap->arg;
12375 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012376 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 end = eap->arg + 4;
12378 else
12379 {
12380 p = skiptowhite(eap->arg);
12381 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012382 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383 p = skipwhite(p);
12384 if (*p == NUL)
12385 {
12386 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012387 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388 EMSG2(_(e_invarg2), eap->arg);
12389 return;
12390 }
12391 end = skip_regexp(p + 1, *p, TRUE, NULL);
12392 if (!eap->skip)
12393 {
12394 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12395 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012396 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 eap->errmsg = e_trailing;
12398 return;
12399 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012400 if (*end != *p)
12401 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012402 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012403 EMSG2(_(e_invarg2), p);
12404 return;
12405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406
12407 c = *end;
12408 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012409 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012410 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012411 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412 }
12413 }
12414 eap->nextcmd = find_nextcmd(end);
12415}
12416#endif
12417
12418#ifdef FEAT_CRYPT
12419/*
12420 * ":X": Get crypt key
12421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012423ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012425 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012426 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427}
12428#endif
12429
12430#ifdef FEAT_FOLDING
12431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012432ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433{
12434 if (foldManualAllowed(TRUE))
12435 foldCreate(eap->line1, eap->line2);
12436}
12437
12438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012439ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440{
12441 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12442 eap->forceit, FALSE);
12443}
12444
12445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012446ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447{
12448 linenr_T lnum;
12449
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012450#ifdef FEAT_CLIPBOARD
12451 start_global_changes();
12452#endif
12453
Bram Moolenaar071d4272004-06-13 20:20:40 +000012454 /* First set the marks for all lines closed/open. */
12455 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12456 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12457 ml_setmarked(lnum);
12458
12459 /* Execute the command on the marked lines. */
12460 global_exe(eap->arg);
12461 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012462#ifdef FEAT_CLIPBOARD
12463 end_global_changes();
12464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465}
12466#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012467
12468# if defined(FEAT_TIMERS) || defined(PROTO)
12469 int
12470get_pressedreturn(void)
12471{
12472 return ex_pressedreturn;
12473}
12474
12475 void
12476set_pressedreturn(int val)
12477{
12478 ex_pressedreturn = val;
12479}
12480#endif