blob: 3a6c7cfb15f45115dfcb7033e93dfb246afbac1d [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
3767 && match_user(xp->xp_pattern + 1) == 1)
3768 {
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;
4059 xp->xp_context = EXPAND_USER_VARS;
4060 xp->xp_pattern = arg;
4061 break;
4062
4063 case CMD_function:
4064 case CMD_delfunction:
4065 xp->xp_context = EXPAND_USER_FUNC;
4066 xp->xp_pattern = arg;
4067 break;
4068
4069 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004070 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 break;
4072#endif
4073 case CMD_highlight:
4074 set_context_in_highlight_cmd(xp, arg);
4075 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004076#ifdef FEAT_CSCOPE
4077 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004078 case CMD_lcscope:
4079 case CMD_scscope:
4080 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004081 break;
4082#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004083#ifdef FEAT_SIGNS
4084 case CMD_sign:
4085 set_context_in_sign_cmd(xp, arg);
4086 break;
4087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 case CMD_bdelete:
4089 case CMD_bwipeout:
4090 case CMD_bunload:
4091 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4092 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004093 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 case CMD_buffer:
4095 case CMD_sbuffer:
4096 case CMD_checktime:
4097 xp->xp_context = EXPAND_BUFFERS;
4098 xp->xp_pattern = arg;
4099 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100#ifdef FEAT_USR_CMDS
4101 case CMD_USER:
4102 case CMD_USER_BUF:
4103 if (compl != EXPAND_NOTHING)
4104 {
4105 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004106 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 {
4108# ifdef FEAT_MENU
4109 if (compl == EXPAND_MENUS)
4110 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4111# endif
4112 if (compl == EXPAND_COMMANDS)
4113 return arg;
4114 if (compl == EXPAND_MAPPINGS)
4115 return set_context_in_map_cmd(xp, (char_u *)"map",
4116 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004117 /* Find start of last argument. */
4118 p = arg;
4119 while (*p)
4120 {
4121 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004122 /* argument starts after a space */
4123 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004124 else if (*p == '\\' && *(p + 1) != NUL)
4125 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004126 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 xp->xp_pattern = arg;
4129 }
4130 xp->xp_context = compl;
4131 }
4132 break;
4133#endif
4134 case CMD_map: case CMD_noremap:
4135 case CMD_nmap: case CMD_nnoremap:
4136 case CMD_vmap: case CMD_vnoremap:
4137 case CMD_omap: case CMD_onoremap:
4138 case CMD_imap: case CMD_inoremap:
4139 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004140 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004141 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004142 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004143 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004145 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 case CMD_unmap:
4147 case CMD_nunmap:
4148 case CMD_vunmap:
4149 case CMD_ounmap:
4150 case CMD_iunmap:
4151 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004152 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004153 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004154 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004155 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004157 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004158 case CMD_mapclear:
4159 case CMD_nmapclear:
4160 case CMD_vmapclear:
4161 case CMD_omapclear:
4162 case CMD_imapclear:
4163 case CMD_cmapclear:
4164 case CMD_lmapclear:
4165 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004166 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004167 case CMD_xmapclear:
4168 xp->xp_context = EXPAND_MAPCLEAR;
4169 xp->xp_pattern = arg;
4170 break;
4171
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 case CMD_abbreviate: case CMD_noreabbrev:
4173 case CMD_cabbrev: case CMD_cnoreabbrev:
4174 case CMD_iabbrev: case CMD_inoreabbrev:
4175 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004176 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 case CMD_unabbreviate:
4178 case CMD_cunabbrev:
4179 case CMD_iunabbrev:
4180 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004181 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182#ifdef FEAT_MENU
4183 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4184 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4185 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4186 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4187 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4188 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4189 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4190 case CMD_tmenu: case CMD_tunmenu:
4191 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4192 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4193#endif
4194
4195 case CMD_colorscheme:
4196 xp->xp_context = EXPAND_COLORS;
4197 xp->xp_pattern = arg;
4198 break;
4199
4200 case CMD_compiler:
4201 xp->xp_context = EXPAND_COMPILER;
4202 xp->xp_pattern = arg;
4203 break;
4204
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004205 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004206 xp->xp_context = EXPAND_OWNSYNTAX;
4207 xp->xp_pattern = arg;
4208 break;
4209
4210 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004211 xp->xp_context = EXPAND_FILETYPE;
4212 xp->xp_pattern = arg;
4213 break;
4214
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004215 case CMD_packadd:
4216 xp->xp_context = EXPAND_PACKADD;
4217 xp->xp_pattern = arg;
4218 break;
4219
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4221 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4222 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004223 p = skiptowhite(arg);
4224 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 {
4226 xp->xp_context = EXPAND_LANGUAGE;
4227 xp->xp_pattern = arg;
4228 }
4229 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004230 {
4231 if ( STRNCMP(arg, "messages", p - arg) == 0
4232 || STRNCMP(arg, "ctype", p - arg) == 0
4233 || STRNCMP(arg, "time", p - arg) == 0)
4234 {
4235 xp->xp_context = EXPAND_LOCALES;
4236 xp->xp_pattern = skipwhite(p);
4237 }
4238 else
4239 xp->xp_context = EXPAND_NOTHING;
4240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 break;
4242#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004243#if defined(FEAT_PROFILE)
4244 case CMD_profile:
4245 set_context_in_profile_cmd(xp, arg);
4246 break;
4247#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004248 case CMD_behave:
4249 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004250 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004251 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004253 case CMD_messages:
4254 xp->xp_context = EXPAND_MESSAGES;
4255 xp->xp_pattern = arg;
4256 break;
4257
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004258#if defined(FEAT_CMDHIST)
4259 case CMD_history:
4260 xp->xp_context = EXPAND_HISTORY;
4261 xp->xp_pattern = arg;
4262 break;
4263#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004264#if defined(FEAT_PROFILE)
4265 case CMD_syntime:
4266 xp->xp_context = EXPAND_SYNTIME;
4267 xp->xp_pattern = arg;
4268 break;
4269#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004270
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004271 case CMD_argdelete:
4272 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4273 arg = xp->xp_pattern + 1;
4274 xp->xp_context = EXPAND_ARGLIST;
4275 xp->xp_pattern = arg;
4276 break;
4277
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278#endif /* FEAT_CMDL_COMPL */
4279
4280 default:
4281 break;
4282 }
4283 return NULL;
4284}
4285
4286/*
4287 * skip a range specifier of the form: addr [,addr] [;addr] ..
4288 *
4289 * Backslashed delimiters after / or ? will be skipped, and commands will
4290 * not be expanded between /'s and ?'s or after "'".
4291 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004292 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 * Returns the "cmd" pointer advanced to beyond the range.
4294 */
4295 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004296skip_range(
4297 char_u *cmd,
4298 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004300 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004302 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004304 if (*cmd == '\\')
4305 {
4306 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4307 ++cmd;
4308 else
4309 break;
4310 }
4311 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
4313 if (*++cmd == NUL && ctx != NULL)
4314 *ctx = EXPAND_NOTHING;
4315 }
4316 else if (*cmd == '/' || *cmd == '?')
4317 {
4318 delim = *cmd++;
4319 while (*cmd != NUL && *cmd != delim)
4320 if (*cmd++ == '\\' && *cmd != NUL)
4321 ++cmd;
4322 if (*cmd == NUL && ctx != NULL)
4323 *ctx = EXPAND_NOTHING;
4324 }
4325 if (*cmd != NUL)
4326 ++cmd;
4327 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004328
4329 /* Skip ":" and white space. */
4330 while (*cmd == ':')
4331 cmd = skipwhite(cmd + 1);
4332
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 return cmd;
4334}
4335
4336/*
4337 * get a single EX address
4338 *
4339 * Set ptr to the next character after the part that was interpreted.
4340 * Set ptr to NULL when an error is encountered.
4341 *
4342 * Return MAXLNUM when no Ex address was found.
4343 */
4344 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004345get_address(
4346 exarg_T *eap UNUSED,
4347 char_u **ptr,
4348 int addr_type, /* flag: one of ADDR_LINES, ... */
4349 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004350 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004351 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352{
4353 int c;
4354 int i;
4355 long n;
4356 char_u *cmd;
4357 pos_T pos;
4358 pos_T *fp;
4359 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004360 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361
4362 cmd = skipwhite(*ptr);
4363 lnum = MAXLNUM;
4364 do
4365 {
4366 switch (*cmd)
4367 {
4368 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004369 ++cmd;
4370 switch (addr_type)
4371 {
4372 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 lnum = curwin->w_cursor.lnum;
4374 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004375 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004376 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004377 break;
4378 case ADDR_ARGUMENTS:
4379 lnum = curwin->w_arg_idx + 1;
4380 break;
4381 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004382 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004383 lnum = curbuf->b_fnum;
4384 break;
4385 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004386 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004387 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004388 case ADDR_TABS_RELATIVE:
4389 EMSG(_(e_invrange));
4390 cmd = NULL;
4391 goto error;
4392 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004393#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004394 case ADDR_QUICKFIX:
4395 lnum = qf_get_cur_valid_idx(eap);
4396 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004397#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004398 }
4399 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400
4401 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 ++cmd;
4403 switch (addr_type)
4404 {
4405 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 lnum = curbuf->b_ml.ml_line_count;
4407 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004408 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004409 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004410 break;
4411 case ADDR_ARGUMENTS:
4412 lnum = ARGCOUNT;
4413 break;
4414 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004415 buf = lastbuf;
4416 while (buf->b_ml.ml_mfp == NULL)
4417 {
4418 if (buf->b_prev == NULL)
4419 break;
4420 buf = buf->b_prev;
4421 }
4422 lnum = buf->b_fnum;
4423 break;
4424 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004425 lnum = lastbuf->b_fnum;
4426 break;
4427 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004428 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004430 case ADDR_TABS_RELATIVE:
4431 EMSG(_(e_invrange));
4432 cmd = NULL;
4433 goto error;
4434 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004435#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004436 case ADDR_QUICKFIX:
4437 lnum = qf_get_size(eap);
4438 if (lnum == 0)
4439 lnum = 1;
4440 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004441#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004442 }
4443 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
4445 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004446 if (*++cmd == NUL)
4447 {
4448 cmd = NULL;
4449 goto error;
4450 }
4451 if (addr_type != ADDR_LINES)
4452 {
4453 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004454 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004455 goto error;
4456 }
4457 if (skip)
4458 ++cmd;
4459 else
4460 {
4461 /* Only accept a mark in another file when it is
4462 * used by itself: ":'M". */
4463 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4464 ++cmd;
4465 if (fp == (pos_T *)-1)
4466 /* Jumped to another file. */
4467 lnum = curwin->w_cursor.lnum;
4468 else
4469 {
4470 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 {
4472 cmd = NULL;
4473 goto error;
4474 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004475 lnum = fp->lnum;
4476 }
4477 }
4478 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
4480 case '/':
4481 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004482 c = *cmd++;
4483 if (addr_type != ADDR_LINES)
4484 {
4485 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004486 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 goto error;
4488 }
4489 if (skip) /* skip "/pat/" */
4490 {
4491 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4492 if (*cmd == c)
4493 ++cmd;
4494 }
4495 else
4496 {
4497 pos = curwin->w_cursor; /* save curwin->w_cursor */
4498 /*
4499 * When '/' or '?' follows another address, start
4500 * from there.
4501 */
4502 if (lnum != MAXLNUM)
4503 curwin->w_cursor.lnum = lnum;
4504 /*
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004505 * Start a forward search at the end of the line (unless
4506 * before the first line).
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004507 * Start a backward search at the start of the line.
4508 * This makes sure we never match in the current
4509 * line, and can match anywhere in the
4510 * next/previous line.
4511 */
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004512 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004513 curwin->w_cursor.col = MAXCOL;
4514 else
4515 curwin->w_cursor.col = 0;
4516 searchcmdlen = 0;
4517 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004518 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004519 {
4520 curwin->w_cursor = pos;
4521 cmd = NULL;
4522 goto error;
4523 }
4524 lnum = curwin->w_cursor.lnum;
4525 curwin->w_cursor = pos;
4526 /* adjust command string pointer */
4527 cmd += searchcmdlen;
4528 }
4529 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
4531 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004532 ++cmd;
4533 if (addr_type != ADDR_LINES)
4534 {
4535 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004536 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004537 goto error;
4538 }
4539 if (*cmd == '&')
4540 i = RE_SUBST;
4541 else if (*cmd == '?' || *cmd == '/')
4542 i = RE_SEARCH;
4543 else
4544 {
4545 EMSG(_(e_backslash));
4546 cmd = NULL;
4547 goto error;
4548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004550 if (!skip)
4551 {
4552 /*
4553 * When search follows another address, start from
4554 * there.
4555 */
4556 if (lnum != MAXLNUM)
4557 pos.lnum = lnum;
4558 else
4559 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004561 /*
4562 * Start the search just like for the above
4563 * do_search().
4564 */
4565 if (*cmd != '?')
4566 pos.col = MAXCOL;
4567 else
4568 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004569#ifdef FEAT_VIRTUALEDIT
4570 pos.coladd = 0;
4571#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004572 if (searchit(curwin, curbuf, &pos,
4573 *cmd == '?' ? BACKWARD : FORWARD,
4574 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004575 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004576 lnum = pos.lnum;
4577 else
4578 {
4579 cmd = NULL;
4580 goto error;
4581 }
4582 }
4583 ++cmd;
4584 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
4586 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004587 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4588 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
4590
4591 for (;;)
4592 {
4593 cmd = skipwhite(cmd);
4594 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4595 break;
4596
4597 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004598 {
4599 switch (addr_type)
4600 {
4601 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004602 /* "+1" is same as ".+1" */
4603 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004604 break;
4605 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004606 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004607 break;
4608 case ADDR_ARGUMENTS:
4609 lnum = curwin->w_arg_idx + 1;
4610 break;
4611 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004612 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004613 lnum = curbuf->b_fnum;
4614 break;
4615 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004616 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004617 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004618 case ADDR_TABS_RELATIVE:
4619 lnum = 1;
4620 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004621#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004622 case ADDR_QUICKFIX:
4623 lnum = qf_get_cur_valid_idx(eap);
4624 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004625#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004626 }
4627 }
4628
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 if (VIM_ISDIGIT(*cmd))
4630 i = '+'; /* "number" is same as "+number" */
4631 else
4632 i = *cmd++;
4633 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4634 n = 1;
4635 else
4636 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004637
4638 if (addr_type == ADDR_TABS_RELATIVE)
4639 {
4640 EMSG(_(e_invrange));
4641 cmd = NULL;
4642 goto error;
4643 }
4644 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004645 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004646 lnum = compute_buffer_local_count(
4647 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004649 {
4650#ifdef FEAT_FOLDING
4651 /* Relative line addressing, need to adjust for folded lines
4652 * now, but only do it after the first address. */
4653 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4654 && address_count >= 2)
4655 (void)hasFolding(lnum, NULL, &lnum);
4656#endif
4657 if (i == '-')
4658 lnum -= n;
4659 else
4660 lnum += n;
4661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 }
4663 } while (*cmd == '/' || *cmd == '?');
4664
4665error:
4666 *ptr = cmd;
4667 return lnum;
4668}
4669
4670/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004671 * Get flags from an Ex command argument.
4672 */
4673 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004674get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004675{
4676 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4677 {
4678 if (*eap->arg == 'l')
4679 eap->flags |= EXFLAG_LIST;
4680 else if (*eap->arg == 'p')
4681 eap->flags |= EXFLAG_PRINT;
4682 else
4683 eap->flags |= EXFLAG_NR;
4684 eap->arg = skipwhite(eap->arg + 1);
4685 }
4686}
4687
4688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 * Function called for command which is Not Implemented. NI!
4690 */
4691 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004692ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
4694 if (!eap->skip)
4695 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4696}
4697
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004698#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699/*
4700 * Function called for script command which is Not Implemented. NI!
4701 * Skips over ":perl <<EOF" constructs.
4702 */
4703 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004704ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705{
4706 if (!eap->skip)
4707 ex_ni(eap);
4708 else
4709 vim_free(script_get(eap, eap->arg));
4710}
4711#endif
4712
4713/*
4714 * Check range in Ex command for validity.
4715 * Return NULL when valid, error message when invalid.
4716 */
4717 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004718invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004720 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 if ( eap->line1 < 0
4722 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004723 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004725
4726 if (eap->argt & RANGE)
4727 {
4728 switch(eap->addr_type)
4729 {
4730 case ADDR_LINES:
4731 if (!(eap->argt & NOTADR)
4732 && eap->line2 > curbuf->b_ml.ml_line_count
4733#ifdef FEAT_DIFF
4734 + (eap->cmdidx == CMD_diffget)
4735#endif
4736 )
4737 return (char_u *)_(e_invrange);
4738 break;
4739 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004740 /* add 1 if ARGCOUNT is 0 */
4741 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004742 return (char_u *)_(e_invrange);
4743 break;
4744 case ADDR_BUFFERS:
4745 if (eap->line1 < firstbuf->b_fnum
4746 || eap->line2 > lastbuf->b_fnum)
4747 return (char_u *)_(e_invrange);
4748 break;
4749 case ADDR_LOADED_BUFFERS:
4750 buf = firstbuf;
4751 while (buf->b_ml.ml_mfp == NULL)
4752 {
4753 if (buf->b_next == NULL)
4754 return (char_u *)_(e_invrange);
4755 buf = buf->b_next;
4756 }
4757 if (eap->line1 < buf->b_fnum)
4758 return (char_u *)_(e_invrange);
4759 buf = lastbuf;
4760 while (buf->b_ml.ml_mfp == NULL)
4761 {
4762 if (buf->b_prev == NULL)
4763 return (char_u *)_(e_invrange);
4764 buf = buf->b_prev;
4765 }
4766 if (eap->line2 > buf->b_fnum)
4767 return (char_u *)_(e_invrange);
4768 break;
4769 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004770 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004771 return (char_u *)_(e_invrange);
4772 break;
4773 case ADDR_TABS:
4774 if (eap->line2 > LAST_TAB_NR)
4775 return (char_u *)_(e_invrange);
4776 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004777 case ADDR_TABS_RELATIVE:
4778 /* Do nothing */
4779 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004780#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004781 case ADDR_QUICKFIX:
4782 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4783 return (char_u *)_(e_invrange);
4784 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004785#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004786 }
4787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 return NULL;
4789}
4790
4791/*
4792 * Correct the range for zero line number, if required.
4793 */
4794 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004795correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796{
4797 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4798 {
4799 if (eap->line1 == 0)
4800 eap->line1 = 1;
4801 if (eap->line2 == 0)
4802 eap->line2 = 1;
4803 }
4804}
4805
Bram Moolenaar748bf032005-02-02 23:04:36 +00004806#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004807static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004808
4809/*
4810 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4811 * pattern. Otherwise return eap->arg.
4812 */
4813 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004814skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004815{
4816 char_u *p = eap->arg;
4817
Bram Moolenaara37420f2006-02-04 22:37:47 +00004818 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4819 || eap->cmdidx == CMD_vimgrepadd
4820 || eap->cmdidx == CMD_lvimgrepadd
4821 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004822 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004823 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004824 if (p == NULL)
4825 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004826 }
4827 return p;
4828}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004829
4830/*
4831 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4832 * in the command line, so that things like % get expanded.
4833 */
4834 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004835replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004836{
4837 char_u *new_cmdline;
4838 char_u *program;
4839 char_u *pos;
4840 char_u *ptr;
4841 int len;
4842 int i;
4843
4844 /*
4845 * Don't do it when ":vimgrep" is used for ":grep".
4846 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004847 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4848 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4849 || eap->cmdidx == CMD_grepadd
4850 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004851 && !grep_internal(eap->cmdidx))
4852 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004853 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4854 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004855 {
4856 if (*curbuf->b_p_gp == NUL)
4857 program = p_gp;
4858 else
4859 program = curbuf->b_p_gp;
4860 }
4861 else
4862 {
4863 if (*curbuf->b_p_mp == NUL)
4864 program = p_mp;
4865 else
4866 program = curbuf->b_p_mp;
4867 }
4868
4869 p = skipwhite(p);
4870
4871 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4872 {
4873 /* replace $* by given arguments */
4874 i = 1;
4875 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4876 ++i;
4877 len = (int)STRLEN(p);
4878 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4879 if (new_cmdline == NULL)
4880 return NULL; /* out of memory */
4881 ptr = new_cmdline;
4882 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4883 {
4884 i = (int)(pos - program);
4885 STRNCPY(ptr, program, i);
4886 STRCPY(ptr += i, p);
4887 ptr += len;
4888 program = pos + 2;
4889 }
4890 STRCPY(ptr, program);
4891 }
4892 else
4893 {
4894 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4895 if (new_cmdline == NULL)
4896 return NULL; /* out of memory */
4897 STRCPY(new_cmdline, program);
4898 STRCAT(new_cmdline, " ");
4899 STRCAT(new_cmdline, p);
4900 }
4901 msg_make(p);
4902
4903 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4904 vim_free(*cmdlinep);
4905 *cmdlinep = new_cmdline;
4906 p = new_cmdline;
4907 }
4908 return p;
4909}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004910#endif
4911
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912/*
4913 * Expand file name in Ex command argument.
4914 * Return FAIL for failure, OK otherwise.
4915 */
4916 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004917expand_filename(
4918 exarg_T *eap,
4919 char_u **cmdlinep,
4920 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921{
4922 int has_wildcards; /* need to expand wildcards */
4923 char_u *repl;
4924 int srclen;
4925 char_u *p;
4926 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004927 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928
Bram Moolenaar748bf032005-02-02 23:04:36 +00004929#ifdef FEAT_QUICKFIX
4930 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4931 p = skip_grep_pat(eap);
4932#else
4933 p = eap->arg;
4934#endif
4935
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 /*
4937 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4938 * the file name contains a wildcard it should not cause expanding.
4939 * (it will be expanded anyway if there is a wildcard before replacing).
4940 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004941 has_wildcards = mch_has_wildcard(p);
4942 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004944#ifdef FEAT_EVAL
4945 /* Skip over `=expr`, wildcards in it are not expanded. */
4946 if (p[0] == '`' && p[1] == '=')
4947 {
4948 p += 2;
4949 (void)skip_expr(&p);
4950 if (*p == '`')
4951 ++p;
4952 continue;
4953 }
4954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 /*
4956 * Quick check if this cannot be the start of a special string.
4957 * Also removes backslash before '%', '#' and '<'.
4958 */
4959 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4960 {
4961 ++p;
4962 continue;
4963 }
4964
4965 /*
4966 * Try to find a match at this position.
4967 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004968 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4969 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 if (*errormsgp != NULL) /* error detected */
4971 return FAIL;
4972 if (repl == NULL) /* no match found */
4973 {
4974 p += srclen;
4975 continue;
4976 }
4977
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004978 /* Wildcards won't be expanded below, the replacement is taken
4979 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4980 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4981 {
4982 char_u *l = repl;
4983
4984 repl = expand_env_save(repl);
4985 vim_free(l);
4986 }
4987
Bram Moolenaar63b92542007-03-27 14:57:09 +00004988 /* Need to escape white space et al. with a backslash.
4989 * Don't do this for:
4990 * - replacement that already has been escaped: "##"
4991 * - shell commands (may have to use quotes instead).
4992 * - non-unix systems when there is a single argument (spaces don't
4993 * separate arguments then).
4994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004996 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 && eap->cmdidx != CMD_grep
4999 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005000 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005001 && eap->cmdidx != CMD_lgrep
5002 && eap->cmdidx != CMD_lgrepadd
5003 && eap->cmdidx != CMD_lmake
5004 && eap->cmdidx != CMD_make
5005 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006#ifndef UNIX
5007 && !(eap->argt & NOSPC)
5008#endif
5009 )
5010 {
5011 char_u *l;
5012#ifdef BACKSLASH_IN_FILENAME
5013 /* Don't escape a backslash here, because rem_backslash() doesn't
5014 * remove it later. */
5015 static char_u *nobslash = (char_u *)" \t\"|";
5016# define ESCAPE_CHARS nobslash
5017#else
5018# define ESCAPE_CHARS escape_chars
5019#endif
5020
5021 for (l = repl; *l; ++l)
5022 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5023 {
5024 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5025 if (l != NULL)
5026 {
5027 vim_free(repl);
5028 repl = l;
5029 }
5030 break;
5031 }
5032 }
5033
5034 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005035 if ((eap->usefilter || eap->cmdidx == CMD_bang
5036 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005037 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 {
5039 char_u *l;
5040
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005041 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 if (l != NULL)
5043 {
5044 vim_free(repl);
5045 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
5047 }
5048
5049 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5050 vim_free(repl);
5051 if (p == NULL)
5052 return FAIL;
5053 }
5054
5055 /*
5056 * One file argument: Expand wildcards.
5057 * Don't do this with ":r !command" or ":w !command".
5058 */
5059 if ((eap->argt & NOSPC) && !eap->usefilter)
5060 {
5061 /*
5062 * May do this twice:
5063 * 1. Replace environment variables.
5064 * 2. Replace any other wildcards, remove backslashes.
5065 */
5066 for (n = 1; n <= 2; ++n)
5067 {
5068 if (n == 2)
5069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 /*
5071 * Halve the number of backslashes (this is Vi compatible).
5072 * For Unix and OS/2, when wildcards are expanded, this is
5073 * done by ExpandOne() below.
5074 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005075#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 if (!has_wildcards)
5077#endif
5078 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080
5081 if (has_wildcards)
5082 {
5083 if (n == 1)
5084 {
5085 /*
5086 * First loop: May expand environment variables. This
5087 * can be done much faster with expand_env() than with
5088 * something else (e.g., calling a shell).
5089 * After expanding environment variables, check again
5090 * if there are still wildcards present.
5091 */
5092 if (vim_strchr(eap->arg, '$') != NULL
5093 || vim_strchr(eap->arg, '~') != NULL)
5094 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005095 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005096 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 has_wildcards = mch_has_wildcard(NameBuff);
5098 p = NameBuff;
5099 }
5100 else
5101 p = NULL;
5102 }
5103 else /* n == 2 */
5104 {
5105 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005106 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
5108 ExpandInit(&xpc);
5109 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005110 if (p_wic)
5111 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005113 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (p == NULL)
5115 return FAIL;
5116 }
5117 if (p != NULL)
5118 {
5119 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5120 p, cmdlinep);
5121 if (n == 2) /* p came from ExpandOne() */
5122 vim_free(p);
5123 }
5124 }
5125 }
5126 }
5127 return OK;
5128}
5129
5130/*
5131 * Replace part of the command line, keeping eap->cmd, eap->arg and
5132 * eap->nextcmd correct.
5133 * "src" points to the part that is to be replaced, of length "srclen".
5134 * "repl" is the replacement string.
5135 * Returns a pointer to the character after the replaced string.
5136 * Returns NULL for failure.
5137 */
5138 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005139repl_cmdline(
5140 exarg_T *eap,
5141 char_u *src,
5142 int srclen,
5143 char_u *repl,
5144 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
5146 int len;
5147 int i;
5148 char_u *new_cmdline;
5149
5150 /*
5151 * The new command line is build in new_cmdline[].
5152 * First allocate it.
5153 * Careful: a "+cmd" argument may have been NUL terminated.
5154 */
5155 len = (int)STRLEN(repl);
5156 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005157 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5159 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5160 return NULL; /* out of memory! */
5161
5162 /*
5163 * Copy the stuff before the expanded part.
5164 * Copy the expanded stuff.
5165 * Copy what came after the expanded part.
5166 * Copy the next commands, if there are any.
5167 */
5168 i = (int)(src - *cmdlinep); /* length of part before match */
5169 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005170
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 mch_memmove(new_cmdline + i, repl, (size_t)len);
5172 i += len; /* remember the end of the string */
5173 STRCPY(new_cmdline + i, src + srclen);
5174 src = new_cmdline + i; /* remember where to continue */
5175
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005176 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
5178 i = (int)STRLEN(new_cmdline) + 1;
5179 STRCPY(new_cmdline + i, eap->nextcmd);
5180 eap->nextcmd = new_cmdline + i;
5181 }
5182 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5183 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5184 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5185 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5186 vim_free(*cmdlinep);
5187 *cmdlinep = new_cmdline;
5188
5189 return src;
5190}
5191
5192/*
5193 * Check for '|' to separate commands and '"' to start comments.
5194 */
5195 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005196separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197{
5198 char_u *p;
5199
Bram Moolenaar86b68352004-12-27 21:59:20 +00005200#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005201 p = skip_grep_pat(eap);
5202#else
5203 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005204#endif
5205
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005206 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
5208 if (*p == Ctrl_V)
5209 {
5210 if (eap->argt & (USECTRLV | XFILE))
5211 ++p; /* skip CTRL-V and next char */
5212 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005213 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005214 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (*p == NUL) /* stop at NUL after CTRL-V */
5216 break;
5217 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005218
5219#ifdef FEAT_EVAL
5220 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005221 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005222 {
5223 p += 2;
5224 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005225 }
5226#endif
5227
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 /* Check for '"': start of comment or '|': next command */
5229 /* :@" and :*" do not start a comment!
5230 * :redir @" doesn't either. */
5231 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5232 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5233 || p != eap->arg)
5234 && (eap->cmdidx != CMD_redir
5235 || p != eap->arg + 1 || p[-1] != '@'))
5236 || *p == '|' || *p == '\n')
5237 {
5238 /*
5239 * We remove the '\' before the '|', unless USECTRLV is used
5240 * AND 'b' is present in 'cpoptions'.
5241 */
5242 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5243 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5244 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005245 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 --p;
5247 }
5248 else
5249 {
5250 eap->nextcmd = check_nextcmd(p);
5251 *p = NUL;
5252 break;
5253 }
5254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005256
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5258 del_trailing_spaces(eap->arg);
5259}
5260
5261/*
5262 * get + command from ex argument
5263 */
5264 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005265getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266{
5267 char_u *arg = *argp;
5268 char_u *command = NULL;
5269
5270 if (*arg == '+') /* +[command] */
5271 {
5272 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005273 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 command = dollar_command;
5275 else
5276 {
5277 command = arg;
5278 arg = skip_cmd_arg(command, TRUE);
5279 if (*arg != NUL)
5280 *arg++ = NUL; /* terminate command with NUL */
5281 }
5282
5283 arg = skipwhite(arg); /* skip over spaces */
5284 *argp = arg;
5285 }
5286 return command;
5287}
5288
5289/*
5290 * Find end of "+command" argument. Skip over "\ " and "\\".
5291 */
5292 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005293skip_cmd_arg(
5294 char_u *p,
5295 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296{
5297 while (*p && !vim_isspace(*p))
5298 {
5299 if (*p == '\\' && p[1] != NUL)
5300 {
5301 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005302 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 else
5304 ++p;
5305 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005306 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 }
5308 return p;
5309}
5310
5311/*
5312 * Get "++opt=arg" argument.
5313 * Return FAIL or OK.
5314 */
5315 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005316getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 char_u *arg = eap->arg + 2;
5319 int *pp = NULL;
5320#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005321 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 char_u *p;
5323#endif
5324
5325 /* ":edit ++[no]bin[ary] file" */
5326 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5327 {
5328 if (*arg == 'n')
5329 {
5330 arg += 2;
5331 eap->force_bin = FORCE_NOBIN;
5332 }
5333 else
5334 eap->force_bin = FORCE_BIN;
5335 if (!checkforcmd(&arg, "binary", 3))
5336 return FAIL;
5337 eap->arg = skipwhite(arg);
5338 return OK;
5339 }
5340
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005341 /* ":read ++edit file" */
5342 if (STRNCMP(arg, "edit", 4) == 0)
5343 {
5344 eap->read_edit = TRUE;
5345 eap->arg = skipwhite(arg + 4);
5346 return OK;
5347 }
5348
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 if (STRNCMP(arg, "ff", 2) == 0)
5350 {
5351 arg += 2;
5352 pp = &eap->force_ff;
5353 }
5354 else if (STRNCMP(arg, "fileformat", 10) == 0)
5355 {
5356 arg += 10;
5357 pp = &eap->force_ff;
5358 }
5359#ifdef FEAT_MBYTE
5360 else if (STRNCMP(arg, "enc", 3) == 0)
5361 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005362 if (STRNCMP(arg, "encoding", 8) == 0)
5363 arg += 8;
5364 else
5365 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 pp = &eap->force_enc;
5367 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005368 else if (STRNCMP(arg, "bad", 3) == 0)
5369 {
5370 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005371 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373#endif
5374
5375 if (pp == NULL || *arg != '=')
5376 return FAIL;
5377
5378 ++arg;
5379 *pp = (int)(arg - eap->cmd);
5380 arg = skip_cmd_arg(arg, FALSE);
5381 eap->arg = skipwhite(arg);
5382 *arg = NUL;
5383
5384#ifdef FEAT_MBYTE
5385 if (pp == &eap->force_ff)
5386 {
5387#endif
5388 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5389 return FAIL;
5390#ifdef FEAT_MBYTE
5391 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005392 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
5394 /* Make 'fileencoding' lower case. */
5395 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5396 *p = TOLOWER_ASC(*p);
5397 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005398 else
5399 {
5400 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5401 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005402 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005403 if (STRICMP(p, "keep") == 0)
5404 eap->bad_char = BAD_KEEP;
5405 else if (STRICMP(p, "drop") == 0)
5406 eap->bad_char = BAD_DROP;
5407 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5408 eap->bad_char = *p;
5409 else
5410 return FAIL;
5411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412#endif
5413
5414 return OK;
5415}
5416
5417/*
5418 * ":abbreviate" and friends.
5419 */
5420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005421ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422{
5423 do_exmap(eap, TRUE); /* almost the same as mapping */
5424}
5425
5426/*
5427 * ":map" and friends.
5428 */
5429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005430ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
5432 /*
5433 * If we are sourcing .exrc or .vimrc in current directory we
5434 * print the mappings for security reasons.
5435 */
5436 if (secure)
5437 {
5438 secure = 2;
5439 msg_outtrans(eap->cmd);
5440 msg_putchar('\n');
5441 }
5442 do_exmap(eap, FALSE);
5443}
5444
5445/*
5446 * ":unmap" and friends.
5447 */
5448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005449ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450{
5451 do_exmap(eap, FALSE);
5452}
5453
5454/*
5455 * ":mapclear" and friends.
5456 */
5457 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005458ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459{
5460 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5461}
5462
5463/*
5464 * ":abclear" and friends.
5465 */
5466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005467ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468{
5469 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5470}
5471
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005473ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474{
5475 /*
5476 * Disallow auto commands from .exrc and .vimrc in current
5477 * directory for security reasons.
5478 */
5479 if (secure)
5480 {
5481 secure = 2;
5482 eap->errmsg = e_curdir;
5483 }
5484 else if (eap->cmdidx == CMD_autocmd)
5485 do_autocmd(eap->arg, eap->forceit);
5486 else
5487 do_augroup(eap->arg, eap->forceit);
5488}
5489
5490/*
5491 * ":doautocmd": Apply the automatic commands to the current buffer.
5492 */
5493 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005494ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005496 char_u *arg = eap->arg;
5497 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005498 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005499
Bram Moolenaar1610d052016-06-09 22:53:01 +02005500 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5501 /* Only when there is no <nomodeline>. */
5502 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005503 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506/*
5507 * :[N]bunload[!] [N] [bufname] unload buffer
5508 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5509 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5510 */
5511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005512ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
5514 eap->errmsg = do_bufdel(
5515 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5516 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5517 : DOBUF_UNLOAD, eap->arg,
5518 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5519}
5520
5521/*
5522 * :[N]buffer [N] to buffer N
5523 * :[N]sbuffer [N] to buffer N
5524 */
5525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005526ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527{
5528 if (*eap->arg)
5529 eap->errmsg = e_trailing;
5530 else
5531 {
5532 if (eap->addr_count == 0) /* default is current buffer */
5533 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5534 else
5535 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005536 if (eap->do_ecmd_cmd != NULL)
5537 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 }
5539}
5540
5541/*
5542 * :[N]bmodified [N] to next mod. buffer
5543 * :[N]sbmodified [N] to next mod. buffer
5544 */
5545 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005546ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
5548 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005549 if (eap->do_ecmd_cmd != NULL)
5550 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551}
5552
5553/*
5554 * :[N]bnext [N] to next buffer
5555 * :[N]sbnext [N] split and to next buffer
5556 */
5557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005558ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559{
5560 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005561 if (eap->do_ecmd_cmd != NULL)
5562 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563}
5564
5565/*
5566 * :[N]bNext [N] to previous buffer
5567 * :[N]bprevious [N] to previous buffer
5568 * :[N]sbNext [N] split and to previous buffer
5569 * :[N]sbprevious [N] split and to previous buffer
5570 */
5571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005572ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573{
5574 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005575 if (eap->do_ecmd_cmd != NULL)
5576 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577}
5578
5579/*
5580 * :brewind to first buffer
5581 * :bfirst to first buffer
5582 * :sbrewind split and to first buffer
5583 * :sbfirst split and to first buffer
5584 */
5585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005586ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587{
5588 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005589 if (eap->do_ecmd_cmd != NULL)
5590 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591}
5592
5593/*
5594 * :blast to last buffer
5595 * :sblast split and to last buffer
5596 */
5597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005598ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599{
5600 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005601 if (eap->do_ecmd_cmd != NULL)
5602 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604
5605 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005606ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
5608 return (c == NUL || c == '|' || c == '"' || c == '\n');
5609}
5610
5611#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5612 || defined(PROTO)
5613/*
5614 * Return the next command, after the first '|' or '\n'.
5615 * Return NULL if not found.
5616 */
5617 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005618find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619{
5620 while (*p != '|' && *p != '\n')
5621 {
5622 if (*p == NUL)
5623 return NULL;
5624 ++p;
5625 }
5626 return (p + 1);
5627}
5628#endif
5629
5630/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005631 * Check if *p is a separator between Ex commands, skipping over white space.
5632 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 */
5634 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005635check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005637 char_u *s = skipwhite(p);
5638
5639 if (*s == '|' || *s == '\n')
5640 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 else
5642 return NULL;
5643}
5644
5645/*
5646 * - if there are more files to edit
5647 * - and this is the last window
5648 * - and forceit not used
5649 * - and not repeated twice on a row
5650 * return FAIL and give error message if 'message' TRUE
5651 * return OK otherwise
5652 */
5653 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005654check_more(
5655 int message, /* when FALSE check only, no messages */
5656 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657{
5658 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5659
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005660 if (!forceit && only_one_window()
5661 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 {
5663 if (message)
5664 {
5665#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5666 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5667 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005668 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669
5670 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005671 vim_strncpy(buff,
5672 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005673 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005675 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 _("%d more files to edit. Quit anyway?"), n);
5677 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5678 return OK;
5679 return FAIL;
5680 }
5681#endif
5682 if (n == 1)
5683 EMSG(_("E173: 1 more file to edit"));
5684 else
5685 EMSGN(_("E173: %ld more files to edit"), n);
5686 quitmore = 2; /* next try to quit is allowed */
5687 }
5688 return FAIL;
5689 }
5690 return OK;
5691}
5692
5693#ifdef FEAT_CMDL_COMPL
5694/*
5695 * Function given to ExpandGeneric() to obtain the list of command names.
5696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005698get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
5700 if (idx >= (int)CMD_SIZE)
5701# ifdef FEAT_USR_CMDS
5702 return get_user_command_name(idx);
5703# else
5704 return NULL;
5705# endif
5706 return cmdnames[idx].cmd_name;
5707}
5708#endif
5709
5710#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005711static 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);
5712static void uc_list(char_u *name, size_t name_len);
5713static 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);
5714static char_u *uc_split_args(char_u *arg, size_t *lenp);
5715static 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 +00005716
5717 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005718uc_add_command(
5719 char_u *name,
5720 size_t name_len,
5721 char_u *rep,
5722 long argt,
5723 long def,
5724 int flags,
5725 int compl,
5726 char_u *compl_arg,
5727 int addr_type,
5728 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
5730 ucmd_T *cmd = NULL;
5731 char_u *p;
5732 int i;
5733 int cmp = 1;
5734 char_u *rep_buf = NULL;
5735 garray_T *gap;
5736
Bram Moolenaar9c102382006-05-03 21:26:49 +00005737 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 if (rep_buf == NULL)
5739 {
5740 /* Can't replace termcodes - try using the string as is */
5741 rep_buf = vim_strsave(rep);
5742
5743 /* Give up if out of memory */
5744 if (rep_buf == NULL)
5745 return FAIL;
5746 }
5747
5748 /* get address of growarray: global or in curbuf */
5749 if (flags & UC_BUFFER)
5750 {
5751 gap = &curbuf->b_ucmds;
5752 if (gap->ga_itemsize == 0)
5753 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5754 }
5755 else
5756 gap = &ucmds;
5757
5758 /* Search for the command in the already defined commands. */
5759 for (i = 0; i < gap->ga_len; ++i)
5760 {
5761 size_t len;
5762
5763 cmd = USER_CMD_GA(gap, i);
5764 len = STRLEN(cmd->uc_name);
5765 cmp = STRNCMP(name, cmd->uc_name, name_len);
5766 if (cmp == 0)
5767 {
5768 if (name_len < len)
5769 cmp = -1;
5770 else if (name_len > len)
5771 cmp = 1;
5772 }
5773
5774 if (cmp == 0)
5775 {
5776 if (!force)
5777 {
5778 EMSG(_("E174: Command already exists: add ! to replace it"));
5779 goto fail;
5780 }
5781
Bram Moolenaard23a8232018-02-10 18:45:26 +01005782 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005783#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005784 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 break;
5787 }
5788
5789 /* Stop as soon as we pass the name to add */
5790 if (cmp < 0)
5791 break;
5792 }
5793
5794 /* Extend the array unless we're replacing an existing command */
5795 if (cmp != 0)
5796 {
5797 if (ga_grow(gap, 1) != OK)
5798 goto fail;
5799 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5800 goto fail;
5801
5802 cmd = USER_CMD_GA(gap, i);
5803 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5804
5805 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
5807 cmd->uc_name = p;
5808 }
5809
5810 cmd->uc_rep = rep_buf;
5811 cmd->uc_argt = argt;
5812 cmd->uc_def = def;
5813 cmd->uc_compl = compl;
5814#ifdef FEAT_EVAL
5815 cmd->uc_scriptID = current_SID;
5816# ifdef FEAT_CMDL_COMPL
5817 cmd->uc_compl_arg = compl_arg;
5818# endif
5819#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005820 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821
5822 return OK;
5823
5824fail:
5825 vim_free(rep_buf);
5826#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5827 vim_free(compl_arg);
5828#endif
5829 return FAIL;
5830}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005833#if defined(FEAT_USR_CMDS)
5834static struct
5835{
5836 int expand;
5837 char *name;
5838} addr_type_complete[] =
5839{
5840 {ADDR_ARGUMENTS, "arguments"},
5841 {ADDR_LINES, "lines"},
5842 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5843 {ADDR_TABS, "tabs"},
5844 {ADDR_BUFFERS, "buffers"},
5845 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005846 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005847 {-1, NULL}
5848};
5849#endif
5850
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005851#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852/*
5853 * List of names for completion for ":command" with the EXPAND_ flag.
5854 * Must be alphabetical for completion.
5855 */
5856static struct
5857{
5858 int expand;
5859 char *name;
5860} command_complete[] =
5861{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005862 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005864 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005866 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005868 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005869#if defined(FEAT_CSCOPE)
5870 {EXPAND_CSCOPE, "cscope"},
5871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5873 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005874 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875#endif
5876 {EXPAND_DIRECTORIES, "dir"},
5877 {EXPAND_ENV_VARS, "environment"},
5878 {EXPAND_EVENTS, "event"},
5879 {EXPAND_EXPRESSION, "expression"},
5880 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005881 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005882 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 {EXPAND_FUNCTIONS, "function"},
5884 {EXPAND_HELP, "help"},
5885 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005886#if defined(FEAT_CMDHIST)
5887 {EXPAND_HISTORY, "history"},
5888#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005889#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005890 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005891 {EXPAND_LOCALES, "locale"},
5892#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005893 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 {EXPAND_MAPPINGS, "mapping"},
5895 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005896 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005897 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005898#if defined(FEAT_PROFILE)
5899 {EXPAND_SYNTIME, "syntime"},
5900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005902 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005903 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005904#if defined(FEAT_SIGNS)
5905 {EXPAND_SIGN, "sign"},
5906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 {EXPAND_TAGS, "tag"},
5908 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005909 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {EXPAND_USER_VARS, "var"},
5911 {0, NULL}
5912};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005915#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005917uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918{
5919 int i, j;
5920 int found = FALSE;
5921 ucmd_T *cmd;
5922 int len;
5923 long a;
5924 garray_T *gap;
5925
5926 gap = &curbuf->b_ucmds;
5927 for (;;)
5928 {
5929 for (i = 0; i < gap->ga_len; ++i)
5930 {
5931 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005932 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933
Bram Moolenaard29459b2016-08-26 22:29:11 +02005934 /* Skip commands which don't match the requested prefix and
5935 * commands filtered out. */
5936 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5937 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 continue;
5939
5940 /* Put out the title first time */
5941 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005942 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 found = TRUE;
5944 msg_putchar('\n');
5945 if (got_int)
5946 break;
5947
5948 /* Special cases */
5949 msg_putchar(a & BANG ? '!' : ' ');
5950 msg_putchar(a & REGSTR ? '"' : ' ');
5951 msg_putchar(gap != &ucmds ? 'b' : ' ');
5952 msg_putchar(' ');
5953
Bram Moolenaar8820b482017-03-16 17:23:31 +01005954 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 len = (int)STRLEN(cmd->uc_name) + 4;
5956
5957 do {
5958 msg_putchar(' ');
5959 ++len;
5960 } while (len < 16);
5961
5962 len = 0;
5963
5964 /* Arguments */
5965 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5966 {
5967 case 0: IObuff[len++] = '0'; break;
5968 case (EXTRA): IObuff[len++] = '*'; break;
5969 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5970 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5971 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5972 }
5973
5974 do {
5975 IObuff[len++] = ' ';
5976 } while (len < 5);
5977
5978 /* Range */
5979 if (a & (RANGE|COUNT))
5980 {
5981 if (a & COUNT)
5982 {
5983 /* -count=N */
5984 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5985 len += (int)STRLEN(IObuff + len);
5986 }
5987 else if (a & DFLALL)
5988 IObuff[len++] = '%';
5989 else if (cmd->uc_def >= 0)
5990 {
5991 /* -range=N */
5992 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5993 len += (int)STRLEN(IObuff + len);
5994 }
5995 else
5996 IObuff[len++] = '.';
5997 }
5998
5999 do {
6000 IObuff[len++] = ' ';
6001 } while (len < 11);
6002
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006003 /* Address Type */
6004 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6005 if (addr_type_complete[j].expand != ADDR_LINES
6006 && addr_type_complete[j].expand == cmd->uc_addr_type)
6007 {
6008 STRCPY(IObuff + len, addr_type_complete[j].name);
6009 len += (int)STRLEN(IObuff + len);
6010 break;
6011 }
6012
6013 do {
6014 IObuff[len++] = ' ';
6015 } while (len < 21);
6016
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 /* Completion */
6018 for (j = 0; command_complete[j].expand != 0; ++j)
6019 if (command_complete[j].expand == cmd->uc_compl)
6020 {
6021 STRCPY(IObuff + len, command_complete[j].name);
6022 len += (int)STRLEN(IObuff + len);
6023 break;
6024 }
6025
6026 do {
6027 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006028 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029
6030 IObuff[len] = '\0';
6031 msg_outtrans(IObuff);
6032
6033 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006034#ifdef FEAT_EVAL
6035 if (p_verbose > 0)
6036 last_set_msg(cmd->uc_scriptID);
6037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 out_flush();
6039 ui_breakcheck();
6040 if (got_int)
6041 break;
6042 }
6043 if (gap == &ucmds || i < gap->ga_len)
6044 break;
6045 gap = &ucmds;
6046 }
6047
6048 if (!found)
6049 MSG(_("No user-defined commands found"));
6050}
6051
6052 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006053uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
6055 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6056 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6057 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6058 0xb9, 0x7f, 0};
6059 int i;
6060
6061 for (i = 0; fcmd[i]; ++i)
6062 IObuff[i] = fcmd[i] - 0x40;
6063 IObuff[i] = 0;
6064 return IObuff;
6065}
6066
6067 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006068uc_scan_attr(
6069 char_u *attr,
6070 size_t len,
6071 long *argt,
6072 long *def,
6073 int *flags,
6074 int *compl,
6075 char_u **compl_arg,
6076 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
6078 char_u *p;
6079
6080 if (len == 0)
6081 {
6082 EMSG(_("E175: No attribute specified"));
6083 return FAIL;
6084 }
6085
6086 /* First, try the simple attributes (no arguments) */
6087 if (STRNICMP(attr, "bang", len) == 0)
6088 *argt |= BANG;
6089 else if (STRNICMP(attr, "buffer", len) == 0)
6090 *flags |= UC_BUFFER;
6091 else if (STRNICMP(attr, "register", len) == 0)
6092 *argt |= REGSTR;
6093 else if (STRNICMP(attr, "bar", len) == 0)
6094 *argt |= TRLBAR;
6095 else
6096 {
6097 int i;
6098 char_u *val = NULL;
6099 size_t vallen = 0;
6100 size_t attrlen = len;
6101
6102 /* Look for the attribute name - which is the part before any '=' */
6103 for (i = 0; i < (int)len; ++i)
6104 {
6105 if (attr[i] == '=')
6106 {
6107 val = &attr[i + 1];
6108 vallen = len - i - 1;
6109 attrlen = i;
6110 break;
6111 }
6112 }
6113
6114 if (STRNICMP(attr, "nargs", attrlen) == 0)
6115 {
6116 if (vallen == 1)
6117 {
6118 if (*val == '0')
6119 /* Do nothing - this is the default */;
6120 else if (*val == '1')
6121 *argt |= (EXTRA | NOSPC | NEEDARG);
6122 else if (*val == '*')
6123 *argt |= EXTRA;
6124 else if (*val == '?')
6125 *argt |= (EXTRA | NOSPC);
6126 else if (*val == '+')
6127 *argt |= (EXTRA | NEEDARG);
6128 else
6129 goto wrong_nargs;
6130 }
6131 else
6132 {
6133wrong_nargs:
6134 EMSG(_("E176: Invalid number of arguments"));
6135 return FAIL;
6136 }
6137 }
6138 else if (STRNICMP(attr, "range", attrlen) == 0)
6139 {
6140 *argt |= RANGE;
6141 if (vallen == 1 && *val == '%')
6142 *argt |= DFLALL;
6143 else if (val != NULL)
6144 {
6145 p = val;
6146 if (*def >= 0)
6147 {
6148two_count:
6149 EMSG(_("E177: Count cannot be specified twice"));
6150 return FAIL;
6151 }
6152
6153 *def = getdigits(&p);
6154 *argt |= (ZEROR | NOTADR);
6155
6156 if (p != val + vallen || vallen == 0)
6157 {
6158invalid_count:
6159 EMSG(_("E178: Invalid default value for count"));
6160 return FAIL;
6161 }
6162 }
6163 }
6164 else if (STRNICMP(attr, "count", attrlen) == 0)
6165 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006166 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167
6168 if (val != NULL)
6169 {
6170 p = val;
6171 if (*def >= 0)
6172 goto two_count;
6173
6174 *def = getdigits(&p);
6175
6176 if (p != val + vallen)
6177 goto invalid_count;
6178 }
6179
6180 if (*def < 0)
6181 *def = 0;
6182 }
6183 else if (STRNICMP(attr, "complete", attrlen) == 0)
6184 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 if (val == NULL)
6186 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006187 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 return FAIL;
6189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006191 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6192 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006195 else if (STRNICMP(attr, "addr", attrlen) == 0)
6196 {
6197 *argt |= RANGE;
6198 if (val == NULL)
6199 {
6200 EMSG(_("E179: argument required for -addr"));
6201 return FAIL;
6202 }
6203 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6204 == FAIL)
6205 return FAIL;
6206 if (addr_type_arg != ADDR_LINES)
6207 *argt |= (ZEROR | NOTADR) ;
6208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 else
6210 {
6211 char_u ch = attr[len];
6212 attr[len] = '\0';
6213 EMSG2(_("E181: Invalid attribute: %s"), attr);
6214 attr[len] = ch;
6215 return FAIL;
6216 }
6217 }
6218
6219 return OK;
6220}
6221
Bram Moolenaara850a712009-01-28 14:42:59 +00006222/*
6223 * ":command ..."
6224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006226ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227{
6228 char_u *name;
6229 char_u *end;
6230 char_u *p;
6231 long argt = 0;
6232 long def = -1;
6233 int flags = 0;
6234 int compl = EXPAND_NOTHING;
6235 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006236 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006238 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239
6240 p = eap->arg;
6241
6242 /* Check for attributes */
6243 while (*p == '-')
6244 {
6245 ++p;
6246 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006247 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6248 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 == FAIL)
6250 return;
6251 p = skipwhite(end);
6252 }
6253
6254 /* Get the name (if any) and skip to the following argument */
6255 name = p;
6256 if (ASCII_ISALPHA(*p))
6257 while (ASCII_ISALNUM(*p))
6258 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006259 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {
6261 EMSG(_("E182: Invalid command name"));
6262 return;
6263 }
6264 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006265 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
6267 /* If there is nothing after the name, and no attributes were specified,
6268 * we are listing commands
6269 */
6270 p = skipwhite(end);
6271 if (!has_attr && ends_excmd(*p))
6272 {
6273 uc_list(name, end - name);
6274 }
6275 else if (!ASCII_ISUPPER(*name))
6276 {
6277 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6278 return;
6279 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006280 else if ((name_len == 1 && *name == 'X')
6281 || (name_len <= 4
6282 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6283 {
6284 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6285 return;
6286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 else
6288 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006289 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290}
6291
6292/*
6293 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 * Clear all user commands, global and for current buffer.
6295 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006296 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006297ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298{
6299 uc_clear(&ucmds);
6300 uc_clear(&curbuf->b_ucmds);
6301}
6302
6303/*
6304 * Clear all user commands for "gap".
6305 */
6306 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006307uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308{
6309 int i;
6310 ucmd_T *cmd;
6311
6312 for (i = 0; i < gap->ga_len; ++i)
6313 {
6314 cmd = USER_CMD_GA(gap, i);
6315 vim_free(cmd->uc_name);
6316 vim_free(cmd->uc_rep);
6317# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6318 vim_free(cmd->uc_compl_arg);
6319# endif
6320 }
6321 ga_clear(gap);
6322}
6323
6324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006325ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 int i = 0;
6328 ucmd_T *cmd = NULL;
6329 int cmp = -1;
6330 garray_T *gap;
6331
6332 gap = &curbuf->b_ucmds;
6333 for (;;)
6334 {
6335 for (i = 0; i < gap->ga_len; ++i)
6336 {
6337 cmd = USER_CMD_GA(gap, i);
6338 cmp = STRCMP(eap->arg, cmd->uc_name);
6339 if (cmp <= 0)
6340 break;
6341 }
6342 if (gap == &ucmds || cmp == 0)
6343 break;
6344 gap = &ucmds;
6345 }
6346
6347 if (cmp != 0)
6348 {
6349 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6350 return;
6351 }
6352
6353 vim_free(cmd->uc_name);
6354 vim_free(cmd->uc_rep);
6355# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6356 vim_free(cmd->uc_compl_arg);
6357# endif
6358
6359 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360
6361 if (i < gap->ga_len)
6362 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6363}
6364
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006365/*
6366 * split and quote args for <f-args>
6367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006369uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370{
6371 char_u *buf;
6372 char_u *p;
6373 char_u *q;
6374 int len;
6375
6376 /* Precalculate length */
6377 p = arg;
6378 len = 2; /* Initial and final quotes */
6379
6380 while (*p)
6381 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006382 if (p[0] == '\\' && p[1] == '\\')
6383 {
6384 len += 2;
6385 p += 2;
6386 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006387 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 {
6389 len += 1;
6390 p += 2;
6391 }
6392 else if (*p == '\\' || *p == '"')
6393 {
6394 len += 2;
6395 p += 1;
6396 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006397 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 {
6399 p = skipwhite(p);
6400 if (*p == NUL)
6401 break;
6402 len += 3; /* "," */
6403 }
6404 else
6405 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006406#ifdef FEAT_MBYTE
6407 int charlen = (*mb_ptr2len)(p);
6408 len += charlen;
6409 p += charlen;
6410#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 ++len;
6412 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 }
6415 }
6416
6417 buf = alloc(len + 1);
6418 if (buf == NULL)
6419 {
6420 *lenp = 0;
6421 return buf;
6422 }
6423
6424 p = arg;
6425 q = buf;
6426 *q++ = '"';
6427 while (*p)
6428 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006429 if (p[0] == '\\' && p[1] == '\\')
6430 {
6431 *q++ = '\\';
6432 *q++ = '\\';
6433 p += 2;
6434 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006435 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 {
6437 *q++ = p[1];
6438 p += 2;
6439 }
6440 else if (*p == '\\' || *p == '"')
6441 {
6442 *q++ = '\\';
6443 *q++ = *p++;
6444 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006445 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 {
6447 p = skipwhite(p);
6448 if (*p == NUL)
6449 break;
6450 *q++ = '"';
6451 *q++ = ',';
6452 *q++ = '"';
6453 }
6454 else
6455 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006456 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 }
6458 }
6459 *q++ = '"';
6460 *q = 0;
6461
6462 *lenp = len;
6463 return buf;
6464}
6465
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006466 static size_t
6467add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6468{
6469 size_t result;
6470
6471 result = STRLEN(mod_str);
6472 if (*multi_mods)
6473 result += 1;
6474 if (buf != NULL)
6475 {
6476 if (*multi_mods)
6477 STRCAT(buf, " ");
6478 STRCAT(buf, mod_str);
6479 }
6480
6481 *multi_mods = 1;
6482
6483 return result;
6484}
6485
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486/*
6487 * Check for a <> code in a user command.
6488 * "code" points to the '<'. "len" the length of the <> (inclusive).
6489 * "buf" is where the result is to be added.
6490 * "split_buf" points to a buffer used for splitting, caller should free it.
6491 * "split_len" is the length of what "split_buf" contains.
6492 * Returns the length of the replacement, which has been added to "buf".
6493 * Returns -1 if there was no match, and only the "<" has been copied.
6494 */
6495 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006496uc_check_code(
6497 char_u *code,
6498 size_t len,
6499 char_u *buf,
6500 ucmd_T *cmd, /* the user command we're expanding */
6501 exarg_T *eap, /* ex arguments */
6502 char_u **split_buf,
6503 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504{
6505 size_t result = 0;
6506 char_u *p = code + 1;
6507 size_t l = len - 2;
6508 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006509 enum {
6510 ct_ARGS,
6511 ct_BANG,
6512 ct_COUNT,
6513 ct_LINE1,
6514 ct_LINE2,
6515 ct_RANGE,
6516 ct_MODS,
6517 ct_REGISTER,
6518 ct_LT,
6519 ct_NONE
6520 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
6522 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6523 {
6524 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6525 p += 2;
6526 l -= 2;
6527 }
6528
Bram Moolenaar371d5402006-03-20 21:47:49 +00006529 ++l;
6530 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006532 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006534 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006536 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006538 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006540 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006542 else if (STRNICMP(p, "range>", l) == 0)
6543 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006544 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006546 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006548 else if (STRNICMP(p, "mods>", l) == 0)
6549 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550
6551 switch (type)
6552 {
6553 case ct_ARGS:
6554 /* Simple case first */
6555 if (*eap->arg == NUL)
6556 {
6557 if (quote == 1)
6558 {
6559 result = 2;
6560 if (buf != NULL)
6561 STRCPY(buf, "''");
6562 }
6563 else
6564 result = 0;
6565 break;
6566 }
6567
6568 /* When specified there is a single argument don't split it.
6569 * Works for ":Cmd %" when % is "a b c". */
6570 if ((eap->argt & NOSPC) && quote == 2)
6571 quote = 1;
6572
6573 switch (quote)
6574 {
6575 case 0: /* No quoting, no splitting */
6576 result = STRLEN(eap->arg);
6577 if (buf != NULL)
6578 STRCPY(buf, eap->arg);
6579 break;
6580 case 1: /* Quote, but don't split */
6581 result = STRLEN(eap->arg) + 2;
6582 for (p = eap->arg; *p; ++p)
6583 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006584#ifdef FEAT_MBYTE
6585 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6586 /* DBCS can contain \ in a trail byte, skip the
6587 * double-byte character. */
6588 ++p;
6589 else
6590#endif
6591 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 ++result;
6593 }
6594
6595 if (buf != NULL)
6596 {
6597 *buf++ = '"';
6598 for (p = eap->arg; *p; ++p)
6599 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006600#ifdef FEAT_MBYTE
6601 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6602 /* DBCS can contain \ in a trail byte, copy the
6603 * double-byte character to avoid escaping. */
6604 *buf++ = *p++;
6605 else
6606#endif
6607 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 *buf++ = '\\';
6609 *buf++ = *p;
6610 }
6611 *buf = '"';
6612 }
6613
6614 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006615 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 /* This is hard, so only do it once, and cache the result */
6617 if (*split_buf == NULL)
6618 *split_buf = uc_split_args(eap->arg, split_len);
6619
6620 result = *split_len;
6621 if (buf != NULL && result != 0)
6622 STRCPY(buf, *split_buf);
6623
6624 break;
6625 }
6626 break;
6627
6628 case ct_BANG:
6629 result = eap->forceit ? 1 : 0;
6630 if (quote)
6631 result += 2;
6632 if (buf != NULL)
6633 {
6634 if (quote)
6635 *buf++ = '"';
6636 if (eap->forceit)
6637 *buf++ = '!';
6638 if (quote)
6639 *buf = '"';
6640 }
6641 break;
6642
6643 case ct_LINE1:
6644 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006645 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 case ct_COUNT:
6647 {
6648 char num_buf[20];
6649 long num = (type == ct_LINE1) ? eap->line1 :
6650 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006651 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6653 size_t num_len;
6654
6655 sprintf(num_buf, "%ld", num);
6656 num_len = STRLEN(num_buf);
6657 result = num_len;
6658
6659 if (quote)
6660 result += 2;
6661
6662 if (buf != NULL)
6663 {
6664 if (quote)
6665 *buf++ = '"';
6666 STRCPY(buf, num_buf);
6667 buf += num_len;
6668 if (quote)
6669 *buf = '"';
6670 }
6671
6672 break;
6673 }
6674
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006675 case ct_MODS:
6676 {
6677 int multi_mods = 0;
6678 typedef struct {
6679 int *varp;
6680 char *name;
6681 } mod_entry_T;
6682 static mod_entry_T mod_entries[] = {
6683#ifdef FEAT_BROWSE_CMD
6684 {&cmdmod.browse, "browse"},
6685#endif
6686#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6687 {&cmdmod.confirm, "confirm"},
6688#endif
6689 {&cmdmod.hide, "hide"},
6690 {&cmdmod.keepalt, "keepalt"},
6691 {&cmdmod.keepjumps, "keepjumps"},
6692 {&cmdmod.keepmarks, "keepmarks"},
6693 {&cmdmod.keeppatterns, "keeppatterns"},
6694 {&cmdmod.lockmarks, "lockmarks"},
6695 {&cmdmod.noswapfile, "noswapfile"},
6696 {NULL, NULL}
6697 };
6698 int i;
6699
6700 result = quote ? 2 : 0;
6701 if (buf != NULL)
6702 {
6703 if (quote)
6704 *buf++ = '"';
6705 *buf = '\0';
6706 }
6707
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006708 /* :aboveleft and :leftabove */
6709 if (cmdmod.split & WSP_ABOVE)
6710 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6711 /* :belowright and :rightbelow */
6712 if (cmdmod.split & WSP_BELOW)
6713 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6714 /* :botright */
6715 if (cmdmod.split & WSP_BOT)
6716 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006717
6718 /* the modifiers that are simple flags */
6719 for (i = 0; mod_entries[i].varp != NULL; ++i)
6720 if (*mod_entries[i].varp)
6721 result += add_cmd_modifier(buf, mod_entries[i].name,
6722 &multi_mods);
6723
6724 /* TODO: How to support :noautocmd? */
6725#ifdef HAVE_SANDBOX
6726 /* TODO: How to support :sandbox?*/
6727#endif
6728 /* :silent */
6729 if (msg_silent > 0)
6730 result += add_cmd_modifier(buf,
6731 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006732 /* :tab */
6733 if (cmdmod.tab > 0)
6734 result += add_cmd_modifier(buf, "tab", &multi_mods);
6735 /* :topleft */
6736 if (cmdmod.split & WSP_TOP)
6737 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006738 /* TODO: How to support :unsilent?*/
6739 /* :verbose */
6740 if (p_verbose > 0)
6741 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006742 /* :vertical */
6743 if (cmdmod.split & WSP_VERT)
6744 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006745 if (quote && buf != NULL)
6746 {
6747 buf += result - 2;
6748 *buf = '"';
6749 }
6750 break;
6751 }
6752
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 case ct_REGISTER:
6754 result = eap->regname ? 1 : 0;
6755 if (quote)
6756 result += 2;
6757 if (buf != NULL)
6758 {
6759 if (quote)
6760 *buf++ = '\'';
6761 if (eap->regname)
6762 *buf++ = eap->regname;
6763 if (quote)
6764 *buf = '\'';
6765 }
6766 break;
6767
6768 case ct_LT:
6769 result = 1;
6770 if (buf != NULL)
6771 *buf = '<';
6772 break;
6773
6774 default:
6775 /* Not recognized: just copy the '<' and return -1. */
6776 result = (size_t)-1;
6777 if (buf != NULL)
6778 *buf = '<';
6779 break;
6780 }
6781
6782 return result;
6783}
6784
6785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006786do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787{
6788 char_u *buf;
6789 char_u *p;
6790 char_u *q;
6791
6792 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006793 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006794 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 size_t len, totlen;
6796
6797 size_t split_len = 0;
6798 char_u *split_buf = NULL;
6799 ucmd_T *cmd;
6800#ifdef FEAT_EVAL
6801 scid_T save_current_SID = current_SID;
6802#endif
6803
6804 if (eap->cmdidx == CMD_USER)
6805 cmd = USER_CMD(eap->useridx);
6806 else
6807 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6808
6809 /*
6810 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006811 * First round: "buf" is NULL, compute length, allocate "buf".
6812 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 */
6814 buf = NULL;
6815 for (;;)
6816 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006817 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006818 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006820
6821 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006823 start = vim_strchr(p, '<');
6824 if (start != NULL)
6825 end = vim_strchr(start + 1, '>');
6826 if (buf != NULL)
6827 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006828 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6829 ;
6830 if (*ksp == K_SPECIAL
6831 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006832 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6833# ifdef FEAT_GUI
6834 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6835# endif
6836 ))
6837 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006838 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006839 * KS_SPECIAL KE_FILLER, like for mappings, but
6840 * do_cmdline() doesn't handle that, so convert it back.
6841 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6842 len = ksp - p;
6843 if (len > 0)
6844 {
6845 mch_memmove(q, p, len);
6846 q += len;
6847 }
6848 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6849 p = ksp + 3;
6850 continue;
6851 }
6852 }
6853
6854 /* break if there no <item> is found */
6855 if (start == NULL || end == NULL)
6856 break;
6857
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 /* Include the '>' */
6859 ++end;
6860
6861 /* Take everything up to the '<' */
6862 len = start - p;
6863 if (buf == NULL)
6864 totlen += len;
6865 else
6866 {
6867 mch_memmove(q, p, len);
6868 q += len;
6869 }
6870
6871 len = uc_check_code(start, end - start, q, cmd, eap,
6872 &split_buf, &split_len);
6873 if (len == (size_t)-1)
6874 {
6875 /* no match, continue after '<' */
6876 p = start + 1;
6877 len = 1;
6878 }
6879 else
6880 p = end;
6881 if (buf == NULL)
6882 totlen += len;
6883 else
6884 q += len;
6885 }
6886 if (buf != NULL) /* second time here, finished */
6887 {
6888 STRCPY(q, p);
6889 break;
6890 }
6891
6892 totlen += STRLEN(p); /* Add on the trailing characters */
6893 buf = alloc((unsigned)(totlen + 1));
6894 if (buf == NULL)
6895 {
6896 vim_free(split_buf);
6897 return;
6898 }
6899 }
6900
6901#ifdef FEAT_EVAL
6902 current_SID = cmd->uc_scriptID;
6903#endif
6904 (void)do_cmdline(buf, eap->getline, eap->cookie,
6905 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6906#ifdef FEAT_EVAL
6907 current_SID = save_current_SID;
6908#endif
6909 vim_free(buf);
6910 vim_free(split_buf);
6911}
6912
6913# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6914 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006915get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916{
6917 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6918}
6919
6920/*
6921 * Function given to ExpandGeneric() to obtain the list of user command names.
6922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006924get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925{
6926 if (idx < curbuf->b_ucmds.ga_len)
6927 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6928 idx -= curbuf->b_ucmds.ga_len;
6929 if (idx < ucmds.ga_len)
6930 return USER_CMD(idx)->uc_name;
6931 return NULL;
6932}
6933
6934/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006935 * Function given to ExpandGeneric() to obtain the list of user address type names.
6936 */
6937 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006938get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006939{
6940 return (char_u *)addr_type_complete[idx].name;
6941}
6942
6943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 * Function given to ExpandGeneric() to obtain the list of user command
6945 * attributes.
6946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006948get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949{
6950 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006951 {"addr", "bang", "bar", "buffer", "complete",
6952 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006954 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 return NULL;
6956 return (char_u *)user_cmd_flags[idx];
6957}
6958
6959/*
6960 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006963get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964{
6965 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6966
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006967 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 return NULL;
6969 return (char_u *)user_cmd_nargs[idx];
6970}
6971
6972/*
6973 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006976get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977{
6978 return (char_u *)command_complete[idx].name;
6979}
6980# endif /* FEAT_CMDL_COMPL */
6981
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006982/*
6983 * Parse address type argument
6984 */
6985 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006986parse_addr_type_arg(
6987 char_u *value,
6988 int vallen,
6989 long *argt,
6990 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006991{
6992 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006993
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006994 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6995 {
6996 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6997 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6998 if (a && b)
6999 {
7000 *addr_type_arg = addr_type_complete[i].expand;
7001 break;
7002 }
7003 }
7004
7005 if (addr_type_complete[i].expand == -1)
7006 {
7007 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007008
Bram Moolenaar1c465442017-03-12 20:10:05 +01007009 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007010 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007011 err[i] = NUL;
7012 EMSG2(_("E180: Invalid address type value: %s"), err);
7013 return FAIL;
7014 }
7015
7016 if (*addr_type_arg != ADDR_LINES)
7017 *argt |= NOTADR;
7018
7019 return OK;
7020}
7021
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022#endif /* FEAT_USR_CMDS */
7023
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007024#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7025/*
7026 * Parse a completion argument "value[vallen]".
7027 * The detected completion goes in "*complp", argument type in "*argt".
7028 * When there is an argument, for function and user defined completion, it's
7029 * copied to allocated memory and stored in "*compl_arg".
7030 * Returns FAIL if something is wrong.
7031 */
7032 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007033parse_compl_arg(
7034 char_u *value,
7035 int vallen,
7036 int *complp,
7037 long *argt,
7038 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007039{
7040 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007041# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007042 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007043# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007044 int i;
7045 int valend = vallen;
7046
7047 /* Look for any argument part - which is the part after any ',' */
7048 for (i = 0; i < vallen; ++i)
7049 {
7050 if (value[i] == ',')
7051 {
7052 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007053# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007054 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007055# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007056 valend = i;
7057 break;
7058 }
7059 }
7060
7061 for (i = 0; command_complete[i].expand != 0; ++i)
7062 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007063 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007064 && STRNCMP(value, command_complete[i].name, valend) == 0)
7065 {
7066 *complp = command_complete[i].expand;
7067 if (command_complete[i].expand == EXPAND_BUFFERS)
7068 *argt |= BUFNAME;
7069 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7070 || command_complete[i].expand == EXPAND_FILES)
7071 *argt |= XFILE;
7072 break;
7073 }
7074 }
7075
7076 if (command_complete[i].expand == 0)
7077 {
7078 EMSG2(_("E180: Invalid complete value: %s"), value);
7079 return FAIL;
7080 }
7081
7082# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7083 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7084 && arg != NULL)
7085# else
7086 if (arg != NULL)
7087# endif
7088 {
7089 EMSG(_("E468: Completion argument only allowed for custom completion"));
7090 return FAIL;
7091 }
7092
7093# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7094 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7095 && arg == NULL)
7096 {
7097 EMSG(_("E467: Custom completion requires a function argument"));
7098 return FAIL;
7099 }
7100
7101 if (arg != NULL)
7102 *compl_arg = vim_strnsave(arg, (int)arglen);
7103# endif
7104 return OK;
7105}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007106
7107 int
7108cmdcomplete_str_to_type(char_u *complete_str)
7109{
7110 int i;
7111
7112 for (i = 0; command_complete[i].expand != 0; ++i)
7113 if (STRCMP(complete_str, command_complete[i].name) == 0)
7114 return command_complete[i].expand;
7115
7116 return EXPAND_NOTHING;
7117}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007118#endif
7119
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007121ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122{
Bram Moolenaare6850792010-05-14 15:28:44 +02007123 if (*eap->arg == NUL)
7124 {
7125#ifdef FEAT_EVAL
7126 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7127 char_u *p = NULL;
7128
7129 if (expr != NULL)
7130 {
7131 ++emsg_off;
7132 p = eval_to_string(expr, NULL, FALSE);
7133 --emsg_off;
7134 vim_free(expr);
7135 }
7136 if (p != NULL)
7137 {
7138 MSG(p);
7139 vim_free(p);
7140 }
7141 else
7142 MSG("default");
7143#else
7144 MSG(_("unknown"));
7145#endif
7146 }
7147 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007148 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149}
7150
7151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007152ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
7154 if (*eap->arg == NUL && eap->cmd[2] == '!')
7155 MSG(_("Greetings, Vim user!"));
7156 do_highlight(eap->arg, eap->forceit, FALSE);
7157}
7158
7159
7160/*
7161 * Call this function if we thought we were going to exit, but we won't
7162 * (because of an error). May need to restore the terminal mode.
7163 */
7164 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007165not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166{
7167 exiting = FALSE;
7168 settmode(TMODE_RAW);
7169}
7170
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007171 static int
7172before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7173{
7174 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7175
7176 /* Bail out when autocommands closed the window.
7177 * Refuse to quit when the buffer in the last window is being closed (can
7178 * only happen in autocommands). */
7179 if (!win_valid(wp)
7180 || curbuf_locked()
7181 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7182 return TRUE;
7183
7184 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7185 {
7186 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7187 /* Refuse to quit when locked or when the buffer in the last window is
7188 * being closed (can only happen in autocommands). */
7189 if (curbuf_locked()
7190 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7191 return TRUE;
7192 }
7193
7194 return FALSE;
7195}
7196
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007198 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007199 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 */
7201 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007202ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007204 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007205
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206#ifdef FEAT_CMDWIN
7207 if (cmdwin_type != 0)
7208 {
7209 cmdwin_result = Ctrl_C;
7210 return;
7211 }
7212#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007213 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007214 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007215 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007216 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007217 return;
7218 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007219 if (eap->addr_count > 0)
7220 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007221 int wnr = eap->line2;
7222
7223 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7224 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007225 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007226 }
7227 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007228 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007229
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007230 /* Refuse to quit when locked. */
7231 if (curbuf_locked())
7232 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007233
7234 /* Trigger QuitPre and maybe ExitPre */
7235 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007236 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
7238#ifdef FEAT_NETBEANS_INTG
7239 netbeansForcedQuit = eap->forceit;
7240#endif
7241
7242 /*
7243 * If there are more files or windows we won't exit.
7244 */
7245 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7246 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007247 if ((!buf_hide(wp->w_buffer)
7248 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007249 | (eap->forceit ? CCGD_FORCEIT : 0)
7250 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007252 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 {
7254 not_exiting();
7255 }
7256 else
7257 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007258 /* quit last window
7259 * Note: only_one_window() returns true, even so a help window is
7260 * still open. In that case only quit, if no address has been
7261 * specified. Example:
7262 * :h|wincmd w|1q - don't quit
7263 * :h|wincmd w|q - quit
7264 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007265 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007267 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007268#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007272 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 }
7274}
7275
7276/*
7277 * ":cquit".
7278 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007280ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281{
7282 getout(1); /* this does not always pass on the exit code to the Manx
7283 compiler. why? */
7284}
7285
7286/*
7287 * ":qall": try to quit all windows
7288 */
7289 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007290ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291{
7292# ifdef FEAT_CMDWIN
7293 if (cmdwin_type != 0)
7294 {
7295 if (eap->forceit)
7296 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7297 else
7298 cmdwin_result = K_XF2;
7299 return;
7300 }
7301# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007302
7303 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007304 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007305 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007306 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007307 return;
7308 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007309
7310 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007311 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007312
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007314 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 getout(0);
7316 not_exiting();
7317}
7318
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319/*
7320 * ":close": close current window, unless it is the last one
7321 */
7322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007323ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007325 win_T *win;
7326 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007327#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007329 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007331#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007332 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007333 {
7334 if (eap->addr_count == 0)
7335 ex_win_close(eap->forceit, curwin, NULL);
7336 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007337 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007338 {
7339 winnr++;
7340 if (winnr == eap->line2)
7341 break;
7342 }
7343 if (win == NULL)
7344 win = lastwin;
7345 ex_win_close(eap->forceit, win, NULL);
7346 }
7347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348}
7349
Bram Moolenaar4033c552017-09-16 20:54:51 +02007350#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007351/*
7352 * ":pclose": Close any preview window.
7353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007355ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007356{
7357 win_T *win;
7358
Bram Moolenaar29323592016-07-24 22:04:11 +02007359 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007360 if (win->w_p_pvw)
7361 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007362 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007363 break;
7364 }
7365}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007366#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007367
Bram Moolenaarf740b292006-02-16 22:11:02 +00007368/*
7369 * Close window "win" and take care of handling closing the last window for a
7370 * modified buffer.
7371 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007373ex_win_close(
7374 int forceit,
7375 win_T *win,
7376 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377{
7378 int need_hide;
7379 buf_T *buf = win->w_buffer;
7380
7381 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007382 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007384#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 if ((p_confirm || cmdmod.confirm) && p_write)
7386 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007387 bufref_T bufref;
7388
7389 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007391 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 return;
7393 need_hide = FALSE;
7394 }
7395 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007398 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 return;
7400 }
7401 }
7402
Bram Moolenaar4033c552017-09-16 20:54:51 +02007403#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007405#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007406
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007408 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007409 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007410 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007411 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412}
7413
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007415 * Handle the argument for a tabpage related ex command.
7416 * Returns a tabpage number.
7417 * When an error is encountered then eap->errmsg is set.
7418 */
7419 static int
7420get_tabpage_arg(exarg_T *eap)
7421{
7422 int tab_number;
7423 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7424
7425 if (eap->arg && *eap->arg != NUL)
7426 {
7427 char_u *p = eap->arg;
7428 char_u *p_save;
7429 int relative = 0; /* argument +N/-N means: go to N places to the
7430 * right/left relative to the current position. */
7431
7432 if (*p == '-')
7433 {
7434 relative = -1;
7435 p++;
7436 }
7437 else if (*p == '+')
7438 {
7439 relative = 1;
7440 p++;
7441 }
7442
7443 p_save = p;
7444 tab_number = getdigits(&p);
7445
7446 if (relative == 0)
7447 {
7448 if (STRCMP(p, "$") == 0)
7449 tab_number = LAST_TAB_NR;
7450 else if (p == p_save || *p_save == '-' || *p != NUL
7451 || tab_number > LAST_TAB_NR)
7452 {
7453 /* No numbers as argument. */
7454 eap->errmsg = e_invarg;
7455 goto theend;
7456 }
7457 }
7458 else
7459 {
7460 if (*p_save == NUL)
7461 tab_number = 1;
7462 else if (p == p_save || *p_save == '-' || *p != NUL
7463 || tab_number == 0)
7464 {
7465 /* No numbers as argument. */
7466 eap->errmsg = e_invarg;
7467 goto theend;
7468 }
7469 tab_number = tab_number * relative + tabpage_index(curtab);
7470 if (!unaccept_arg0 && relative == -1)
7471 --tab_number;
7472 }
7473 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7474 eap->errmsg = e_invarg;
7475 }
7476 else if (eap->addr_count > 0)
7477 {
7478 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007479 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007480 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007481 tab_number = 0;
7482 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007483 else
7484 {
7485 tab_number = eap->line2;
7486 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7487 {
7488 --tab_number;
7489 if (tab_number < unaccept_arg0)
7490 eap->errmsg = e_invarg;
7491 }
7492 }
7493 }
7494 else
7495 {
7496 switch (eap->cmdidx)
7497 {
7498 case CMD_tabnext:
7499 tab_number = tabpage_index(curtab) + 1;
7500 if (tab_number > LAST_TAB_NR)
7501 tab_number = 1;
7502 break;
7503 case CMD_tabmove:
7504 tab_number = LAST_TAB_NR;
7505 break;
7506 default:
7507 tab_number = tabpage_index(curtab);
7508 }
7509 }
7510
7511theend:
7512 return tab_number;
7513}
7514
7515/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007516 * ":tabclose": close current tab page, unless it is the last one.
7517 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 */
7519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007520ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007522 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007523 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007524
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007525# ifdef FEAT_CMDWIN
7526 if (cmdwin_type != 0)
7527 cmdwin_result = K_IGNORE;
7528 else
7529# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007530 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007531 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007532 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007534 tab_number = get_tabpage_arg(eap);
7535 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007536 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007537 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007538 if (tp == NULL)
7539 {
7540 beep_flush();
7541 return;
7542 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007543 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007544 {
7545 tabpage_close_other(tp, eap->forceit);
7546 return;
7547 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007548 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007549 tabpage_close(eap->forceit);
7550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007552}
7553
7554/*
7555 * ":tabonly": close all tab pages except the current one
7556 */
7557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007558ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007559{
7560 tabpage_T *tp;
7561 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007562 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007563
7564# ifdef FEAT_CMDWIN
7565 if (cmdwin_type != 0)
7566 cmdwin_result = K_IGNORE;
7567 else
7568# endif
7569 if (first_tabpage->tp_next == NULL)
7570 MSG(_("Already only one tab page"));
7571 else
7572 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007573 tab_number = get_tabpage_arg(eap);
7574 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007575 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007576 goto_tabpage(tab_number);
7577 /* Repeat this up to a 1000 times, because autocommands may
7578 * mess up the lists. */
7579 for (done = 0; done < 1000; ++done)
7580 {
7581 FOR_ALL_TABPAGES(tp)
7582 if (tp->tp_topframe != topframe)
7583 {
7584 tabpage_close_other(tp, eap->forceit);
7585 /* if we failed to close it quit */
7586 if (valid_tabpage(tp))
7587 done = 1000;
7588 /* start over, "tp" is now invalid */
7589 break;
7590 }
7591 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007592 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007593 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007594 }
7595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597
7598/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007599 * Close the current tab page.
7600 */
7601 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007602tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007603{
7604 /* First close all the windows but the current one. If that worked then
7605 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007606 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007607 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007608 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007609 ex_win_close(forceit, curwin, NULL);
7610# ifdef FEAT_GUI
7611 need_mouse_correct = TRUE;
7612# endif
7613}
7614
7615/*
7616 * Close tab page "tp", which is not the current tab page.
7617 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007618 * Also takes care of the tab pages line disappearing when closing the
7619 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007620 */
7621 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007622tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007623{
7624 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007625 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007626 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007627
7628 /* Limit to 1000 windows, autocommands may add a window while we close
7629 * one. OK, so I'm paranoid... */
7630 while (++done < 1000)
7631 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007632 wp = tp->tp_firstwin;
7633 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007634
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007635 /* Autocommands may delete the tab page under our fingers and we may
7636 * fail to close a window with a modified buffer. */
7637 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007638 break;
7639 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007640
Bram Moolenaar29323592016-07-24 22:04:11 +02007641 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007642
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007643 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007644 if (h != tabline_height())
7645 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007646}
7647
7648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 * ":only".
7650 */
7651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007652ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007654 win_T *wp;
7655 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656# ifdef FEAT_GUI
7657 need_mouse_correct = TRUE;
7658# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007659 if (eap->addr_count > 0)
7660 {
7661 wnr = eap->line2;
7662 for (wp = firstwin; --wnr > 0; )
7663 {
7664 if (wp->w_next == NULL)
7665 break;
7666 else
7667 wp = wp->w_next;
7668 }
7669 win_goto(wp);
7670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 close_others(TRUE, eap->forceit);
7672}
7673
7674/*
7675 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007676 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007678 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007679ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680{
7681 if (eap->addr_count == 0)
7682 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007683 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685
7686 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007687ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007689 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007690 if (!eap->skip)
7691 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007692#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007693 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007694#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007695 if (eap->addr_count == 0)
7696 win_close(curwin, FALSE); /* don't free buffer */
7697 else
7698 {
7699 int winnr = 0;
7700 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007701
Bram Moolenaar2256c992016-11-15 21:17:07 +01007702 FOR_ALL_WINDOWS(win)
7703 {
7704 winnr++;
7705 if (winnr == eap->line2)
7706 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007707 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007708 if (win == NULL)
7709 win = lastwin;
7710 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 }
7713}
7714
7715/*
7716 * ":stop" and ":suspend": Suspend Vim.
7717 */
7718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007719ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720{
7721 /*
7722 * Disallow suspending for "rvim".
7723 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007724 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 {
7726 if (!eap->forceit)
7727 autowrite_all();
7728 windgoto((int)Rows - 1, 0);
7729 out_char('\n');
7730 out_flush();
7731 stoptermcap();
7732 out_flush(); /* needed for SUN to restore xterm buffer */
7733#ifdef FEAT_TITLE
7734 mch_restore_title(3); /* restore window titles */
7735#endif
7736 ui_suspend(); /* call machine specific function */
7737#ifdef FEAT_TITLE
7738 maketitle();
7739 resettitle(); /* force updating the title */
7740#endif
7741 starttermcap();
7742 scroll_start(); /* scroll screen before redrawing */
7743 redraw_later_clear();
7744 shell_resized(); /* may have resized window */
7745 }
7746}
7747
7748/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007749 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 */
7751 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007752ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
7754#ifdef FEAT_CMDWIN
7755 if (cmdwin_type != 0)
7756 {
7757 cmdwin_result = Ctrl_C;
7758 return;
7759 }
7760#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007761 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007762 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007763 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007764 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007765 return;
7766 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007767
7768 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007769 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770
7771 /*
7772 * if more files or windows we won't exit
7773 */
7774 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7775 exiting = TRUE;
7776 if ( ((eap->cmdidx == CMD_wq
7777 || curbufIsChanged())
7778 && do_write(eap) == FAIL)
7779 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007780 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 {
7782 not_exiting();
7783 }
7784 else
7785 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007788 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789# ifdef FEAT_GUI
7790 need_mouse_correct = TRUE;
7791# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007792 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007793 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 }
7795}
7796
7797/*
7798 * ":print", ":list", ":number".
7799 */
7800 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007801ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007803 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7804 EMSG(_(e_emptybuf));
7805 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007807 for ( ;!got_int; ui_breakcheck())
7808 {
7809 print_line(eap->line1,
7810 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7811 || (eap->flags & EXFLAG_NR)),
7812 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7813 if (++eap->line1 > eap->line2)
7814 break;
7815 out_flush(); /* show one line at a time */
7816 }
7817 setpcmark();
7818 /* put cursor at last line */
7819 curwin->w_cursor.lnum = eap->line2;
7820 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 }
7822
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824}
7825
7826#ifdef FEAT_BYTEOFF
7827 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007828ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829{
7830 goto_byte(eap->line2);
7831}
7832#endif
7833
7834/*
7835 * ":shell".
7836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007838ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839{
7840 do_shell(NULL, 0);
7841}
7842
Bram Moolenaar4033c552017-09-16 20:54:51 +02007843#if defined(HAVE_DROP_FILE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007845 || defined(FEAT_GUI_MSWIN) \
7846 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 || defined(PROTO)
7848
7849/*
7850 * Handle a file drop. The code is here because a drop is *nearly* like an
7851 * :args command, but not quite (we have a list of exact filenames, so we
7852 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7853 * code is very similar to :args and hence needs access to a lot of the static
7854 * functions in this file.
7855 *
7856 * The list should be allocated using alloc(), as should each item in the
7857 * list. This function takes over responsibility for freeing the list.
7858 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007859 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007860 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 */
7862 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007863handle_drop(
7864 int filec, /* the number of files dropped */
7865 char_u **filev, /* the list of files dropped */
7866 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867{
7868 exarg_T ea;
7869 int save_msg_scroll = msg_scroll;
7870
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007871 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007872 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007874 if (curbuf_locked())
7875 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007876
Bram Moolenaarc236c162008-07-13 17:41:49 +00007877 /* When the screen is being updated we should not change buffers and
7878 * windows structures, it may cause freed memory to be used. */
7879 if (updating_screen)
7880 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881
7882 /* Check whether the current buffer is changed. If so, we will need
7883 * to split the current window or data could be lost.
7884 * We don't need to check if the 'hidden' option is set, as in this
7885 * case the buffer won't be lost.
7886 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007887 if (!buf_hide(curbuf) && !split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {
7889 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007890 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 --emsg_off;
7892 }
7893 if (split)
7894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 if (win_split(0, 0) == FAIL)
7896 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007897 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898
7899 /* When splitting the window, create a new alist. Otherwise the
7900 * existing one is overwritten. */
7901 alist_unlink(curwin->w_alist);
7902 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 }
7904
7905 /*
7906 * Set up the new argument list.
7907 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007908 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909
7910 /*
7911 * Move to the first file.
7912 */
7913 /* Fake up a minimal "next" command for do_argfile() */
7914 vim_memset(&ea, 0, sizeof(ea));
7915 ea.cmd = (char_u *)"next";
7916 do_argfile(&ea, 0);
7917
7918 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7919 * mode that is not needed here. */
7920 need_start_insertmode = FALSE;
7921
7922 /* Restore msg_scroll, otherwise a following command may cause scrolling
7923 * unexpectedly. The screen will be redrawn by the caller, thus
7924 * msg_scroll being set by displaying a message is irrelevant. */
7925 msg_scroll = save_msg_scroll;
7926}
7927#endif
7928
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929/*
7930 * Clear an argument list: free all file names and reset it to zero entries.
7931 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007932 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007933alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
7935 while (--al->al_ga.ga_len >= 0)
7936 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7937 ga_clear(&al->al_ga);
7938}
7939
7940/*
7941 * Init an argument list.
7942 */
7943 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007944alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945{
7946 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7947}
7948
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949/*
7950 * Remove a reference from an argument list.
7951 * Ignored when the argument list is the global one.
7952 * If the argument list is no longer used by any window, free it.
7953 */
7954 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007955alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956{
7957 if (al != &global_alist && --al->al_refcount <= 0)
7958 {
7959 alist_clear(al);
7960 vim_free(al);
7961 }
7962}
7963
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964/*
7965 * Create a new argument list and use it for the current window.
7966 */
7967 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007968alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
7970 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7971 if (curwin->w_alist == NULL)
7972 {
7973 curwin->w_alist = &global_alist;
7974 ++global_alist.al_refcount;
7975 }
7976 else
7977 {
7978 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007979 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 alist_init(curwin->w_alist);
7981 }
7982}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007984#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985/*
7986 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007987 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7988 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 */
7990 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007991alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992{
7993 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007994 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 char_u **new_arg_files;
7996 int new_arg_file_count;
7997 char_u *save_p_su = p_su;
7998 int i;
7999
8000 /* Don't use 'suffixes' here. This should work like the shell did the
8001 * expansion. Also, the vimrc file isn't read yet, thus the user
8002 * can't set the options. */
8003 p_su = empty_option;
8004 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8005 if (old_arg_files != NULL)
8006 {
8007 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008008 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8009 old_arg_count = GARGCOUNT;
8010 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008012 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 && new_arg_file_count > 0)
8014 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008015 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8016 TRUE, fnum_list, fnum_len);
8017 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 }
8020 p_su = save_p_su;
8021}
8022#endif
8023
8024/*
8025 * Set the argument list for the current window.
8026 * Takes over the allocated files[] and the allocated fnames in it.
8027 */
8028 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008029alist_set(
8030 alist_T *al,
8031 int count,
8032 char_u **files,
8033 int use_curbuf,
8034 int *fnum_list,
8035 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036{
8037 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008038 static int recursive = 0;
8039
8040 if (recursive)
8041 {
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008042 EMSG(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008043 return;
8044 }
8045 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046
8047 alist_clear(al);
8048 if (ga_grow(&al->al_ga, count) == OK)
8049 {
8050 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008051 {
8052 if (got_int)
8053 {
8054 /* When adding many buffers this can take a long time. Allow
8055 * interrupting here. */
8056 while (i < count)
8057 vim_free(files[i++]);
8058 break;
8059 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008060
8061 /* May set buffer name of a buffer previously used for the
8062 * argument list, so that it's re-used by alist_add. */
8063 if (fnum_list != NULL && i < fnum_len)
8064 buf_set_name(fnum_list[i], files[i]);
8065
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008067 ui_breakcheck();
8068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 vim_free(files);
8070 }
8071 else
8072 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008075
8076 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077}
8078
8079/*
8080 * Add file "fname" to argument list "al".
8081 * "fname" must have been allocated and "al" must have been checked for room.
8082 */
8083 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008084alist_add(
8085 alist_T *al,
8086 char_u *fname,
8087 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088{
8089 if (fname == NULL) /* don't add NULL file names */
8090 return;
8091#ifdef BACKSLASH_IN_FILENAME
8092 slash_adjust(fname);
8093#endif
8094 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8095 if (set_fnum > 0)
8096 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8097 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8098 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099}
8100
8101#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8102/*
8103 * Adjust slashes in file names. Called after 'shellslash' was set.
8104 */
8105 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008106alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
8108 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008110 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111
8112 for (i = 0; i < GARGCOUNT; ++i)
8113 if (GARGLIST[i].ae_fname != NULL)
8114 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008115 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 if (wp->w_alist != &global_alist)
8117 for (i = 0; i < WARGCOUNT(wp); ++i)
8118 if (WARGLIST(wp)[i].ae_fname != NULL)
8119 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120}
8121#endif
8122
8123/*
8124 * ":preserve".
8125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008127ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008129 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 ml_preserve(curbuf, TRUE);
8131}
8132
8133/*
8134 * ":recover".
8135 */
8136 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008137ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138{
8139 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8140 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008141 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8142 | CCGD_MULTWIN
8143 | (eap->forceit ? CCGD_FORCEIT : 0)
8144 | CCGD_EXCMD)
8145
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 && (*eap->arg == NUL
8147 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8148 ml_recover();
8149 recoverymode = FALSE;
8150}
8151
8152/*
8153 * Command modifier used in a wrong way.
8154 */
8155 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008156ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157{
8158 eap->errmsg = e_invcmd;
8159}
8160
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161/*
8162 * :sview [+command] file split window with new file, read-only
8163 * :split [[+command] file] split window with current or new file
8164 * :vsplit [[+command] file] split window vertically with current or new file
8165 * :new [[+command] file] split window with no or new file
8166 * :vnew [[+command] file] split vertically window with no or new file
8167 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008168 *
8169 * :tabedit open new Tab page with empty window
8170 * :tabedit [+command] file open new Tab page and edit "file"
8171 * :tabnew [[+command] file] just like :tabedit
8172 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 */
8174 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008175ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008177 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008178#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008180#endif
8181#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184
Bram Moolenaar4033c552017-09-16 20:54:51 +02008185#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188
Bram Moolenaar4033c552017-09-16 20:54:51 +02008189#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008191 * quickfix windows. But it's OK when doing ":tab split". */
8192 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {
8194 if (eap->cmdidx == CMD_split)
8195 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 if (eap->cmdidx == CMD_vsplit)
8197 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200
Bram Moolenaar4033c552017-09-16 20:54:51 +02008201#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008202 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {
8204 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8205 FNAME_MESS, TRUE, curbuf->b_ffname);
8206 if (fname == NULL)
8207 goto theend;
8208 eap->arg = fname;
8209 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008210# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008211 else
8212# endif
8213#endif
8214#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 && eap->cmdidx != CMD_new)
8218 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008219 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008220# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008221 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008222# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008223 au_has_group((char_u *)"FileExplorer"))
8224 {
8225 /* No browsing supported but we do have the file explorer:
8226 * Edit the directory. */
8227 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8228 eap->arg = (char_u *)".";
8229 }
8230 else
8231 {
8232 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008234 if (fname == NULL)
8235 goto theend;
8236 eap->arg = fname;
8237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 }
8239 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008242 /*
8243 * Either open new tab page or split the window.
8244 */
8245 if (eap->cmdidx == CMD_tabedit
8246 || eap->cmdidx == CMD_tabfind
8247 || eap->cmdidx == CMD_tabnew)
8248 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008249 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8250 : eap->addr_count == 0 ? 0
8251 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008252 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008253 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008254
8255 /* set the alternate buffer for the window we came from */
8256 if (curwin != old_curwin
8257 && win_valid(old_curwin)
8258 && old_curwin->w_buffer != curbuf
8259 && !cmdmod.keepalt)
8260 old_curwin->w_alt_fnum = curbuf->b_fnum;
8261 }
8262 }
8263 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 /* Reset 'scrollbind' when editing another file, but keep it when
8267 * doing ":split" without arguments. */
8268 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008269# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008273 {
8274 RESET_BINDING(curwin);
8275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 else
8277 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 do_exedit(eap, old_curwin);
8279 }
8280
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008281# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008283# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008285# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286theend:
8287 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008290
8291/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008292 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008293 */
8294 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008295tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008296{
8297 exarg_T ea;
8298
8299 vim_memset(&ea, 0, sizeof(ea));
8300 ea.cmdidx = CMD_tabnew;
8301 ea.cmd = (char_u *)"tabn";
8302 ea.arg = (char_u *)"";
8303 ex_splitview(&ea);
8304}
8305
8306/*
8307 * :tabnext command
8308 */
8309 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008310ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008311{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008312 int tab_number;
8313
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008314 switch (eap->cmdidx)
8315 {
8316 case CMD_tabfirst:
8317 case CMD_tabrewind:
8318 goto_tabpage(1);
8319 break;
8320 case CMD_tablast:
8321 goto_tabpage(9999);
8322 break;
8323 case CMD_tabprevious:
8324 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008325 if (eap->arg && *eap->arg != NUL)
8326 {
8327 char_u *p = eap->arg;
8328 char_u *p_save = p;
8329
8330 tab_number = getdigits(&p);
8331 if (p == p_save || *p_save == '-' || *p != NUL
8332 || tab_number == 0)
8333 {
8334 /* No numbers as argument. */
8335 eap->errmsg = e_invarg;
8336 return;
8337 }
8338 }
8339 else
8340 {
8341 if (eap->addr_count == 0)
8342 tab_number = 1;
8343 else
8344 {
8345 tab_number = eap->line2;
8346 if (tab_number < 1)
8347 {
8348 eap->errmsg = e_invrange;
8349 return;
8350 }
8351 }
8352 }
8353 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008354 break;
8355 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008356 tab_number = get_tabpage_arg(eap);
8357 if (eap->errmsg == NULL)
8358 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008359 break;
8360 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008361}
8362
8363/*
8364 * :tabmove command
8365 */
8366 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008367ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008368{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008369 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008370
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008371 tab_number = get_tabpage_arg(eap);
8372 if (eap->errmsg == NULL)
8373 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008374}
8375
8376/*
8377 * :tabs command: List tabs and their contents.
8378 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008379 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008380ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008381{
8382 tabpage_T *tp;
8383 win_T *wp;
8384 int tabcount = 1;
8385
8386 msg_start();
8387 msg_scroll = TRUE;
8388 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8389 {
8390 msg_putchar('\n');
8391 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008392 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008393 out_flush(); /* output one line at a time */
8394 ui_breakcheck();
8395
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008396 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008397 wp = firstwin;
8398 else
8399 wp = tp->tp_firstwin;
8400 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8401 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008402 msg_putchar('\n');
8403 msg_putchar(wp == curwin ? '>' : ' ');
8404 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008405 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8406 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008407 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008408 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008409 else
8410 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8411 IObuff, IOSIZE, TRUE);
8412 msg_outtrans(IObuff);
8413 out_flush(); /* output one line at a time */
8414 ui_breakcheck();
8415 }
8416 }
8417}
8418
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419/*
8420 * ":mode": Set screen mode.
8421 * If no argument given, just get the screen size and redraw.
8422 */
8423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008424ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425{
8426 if (*eap->arg == NUL)
8427 shell_resized();
8428 else
8429 mch_screenmode(eap->arg);
8430}
8431
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432/*
8433 * ":resize".
8434 * set, increment or decrement current window height
8435 */
8436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008437ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438{
8439 int n;
8440 win_T *wp = curwin;
8441
8442 if (eap->addr_count > 0)
8443 {
8444 n = eap->line2;
8445 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8446 ;
8447 }
8448
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008449# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 if (cmdmod.split & WSP_VERT)
8454 {
8455 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008456 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8458 n = 9999;
8459 win_setwidth_win((int)n, wp);
8460 }
8461 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 {
8463 if (*eap->arg == '-' || *eap->arg == '+')
8464 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008465 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 n = 9999;
8467 win_setheight_win((int)n, wp);
8468 }
8469}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470
8471/*
8472 * ":find [+command] <file>" command.
8473 */
8474 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008475ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476{
8477#ifdef FEAT_SEARCHPATH
8478 char_u *fname;
8479 int count;
8480
8481 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8482 TRUE, curbuf->b_ffname);
8483 if (eap->addr_count > 0)
8484 {
8485 /* Repeat finding the file "count" times. This matters when it
8486 * appears several times in the path. */
8487 count = eap->line2;
8488 while (fname != NULL && --count > 0)
8489 {
8490 vim_free(fname);
8491 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8492 FALSE, curbuf->b_ffname);
8493 }
8494 }
8495
8496 if (fname != NULL)
8497 {
8498 eap->arg = fname;
8499#endif
8500 do_exedit(eap, NULL);
8501#ifdef FEAT_SEARCHPATH
8502 vim_free(fname);
8503 }
8504#endif
8505}
8506
8507/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008508 * ":open" simulation: for now just work like ":visual".
8509 */
8510 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008511ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008512{
8513 regmatch_T regmatch;
8514 char_u *p;
8515
8516 curwin->w_cursor.lnum = eap->line2;
8517 beginline(BL_SOL | BL_FIX);
8518 if (*eap->arg == '/')
8519 {
8520 /* ":open /pattern/": put cursor in column found with pattern */
8521 ++eap->arg;
8522 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8523 *p = NUL;
8524 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8525 if (regmatch.regprog != NULL)
8526 {
8527 regmatch.rm_ic = p_ic;
8528 p = ml_get_curline();
8529 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008530 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008531 else
8532 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008533 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008534 }
8535 /* Move to the NUL, ignore any other arguments. */
8536 eap->arg += STRLEN(eap->arg);
8537 }
8538 check_cursor();
8539
8540 eap->cmdidx = CMD_visual;
8541 do_exedit(eap, NULL);
8542}
8543
8544/*
8545 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 */
8547 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008548ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549{
8550 do_exedit(eap, NULL);
8551}
8552
8553/*
8554 * ":edit <file>" command and alikes.
8555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008557do_exedit(
8558 exarg_T *eap,
8559 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560{
8561 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008563 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564
8565 /*
8566 * ":vi" command ends Ex mode.
8567 */
8568 if (exmode_active && (eap->cmdidx == CMD_visual
8569 || eap->cmdidx == CMD_view))
8570 {
8571 exmode_active = FALSE;
8572 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008573 {
8574 /* Special case: ":global/pat/visual\NLvi-commands" */
8575 if (global_busy)
8576 {
8577 int rd = RedrawingDisabled;
8578 int nwr = no_wait_return;
8579 int ms = msg_scroll;
8580#ifdef FEAT_GUI
8581 int he = hold_gui_events;
8582#endif
8583
8584 if (eap->nextcmd != NULL)
8585 {
8586 stuffReadbuff(eap->nextcmd);
8587 eap->nextcmd = NULL;
8588 }
8589
8590 if (exmode_was != EXMODE_VIM)
8591 settmode(TMODE_RAW);
8592 RedrawingDisabled = 0;
8593 no_wait_return = 0;
8594 need_wait_return = FALSE;
8595 msg_scroll = 0;
8596#ifdef FEAT_GUI
8597 hold_gui_events = 0;
8598#endif
8599 must_redraw = CLEAR;
8600
8601 main_loop(FALSE, TRUE);
8602
8603 RedrawingDisabled = rd;
8604 no_wait_return = nwr;
8605 msg_scroll = ms;
8606#ifdef FEAT_GUI
8607 hold_gui_events = he;
8608#endif
8609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 }
8613
8614 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008615 || eap->cmdidx == CMD_tabnew
8616 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008617 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008619 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 setpcmark();
8621 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008622 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8623 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008625 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 || *eap->arg != NUL
8627#ifdef FEAT_BROWSE
8628 || cmdmod.browse
8629#endif
8630 )
8631 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008632 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8633 * can bring us here, others are stopped earlier. */
8634 if (*eap->arg != NUL && curbuf_locked())
8635 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008636
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 n = readonlymode;
8638 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8639 readonlymode = TRUE;
8640 else if (eap->cmdidx == CMD_enew)
8641 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8642 empty buffer */
8643 setpcmark();
8644 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8645 NULL, eap,
8646 /* ":edit" goes to first line if Vi compatible */
8647 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8648 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8649 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008650 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008652 /* after a split we can use an existing buffer */
8653 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008655 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 {
8657 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 if (old_curwin != NULL)
8659 {
8660 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008661 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008663#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008664 cleanup_T cs;
8665
8666 /* Reset the error/interrupt/exception state here so that
8667 * aborting() returns FALSE when closing a window. */
8668 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008669#endif
8670#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008672#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008673 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008674
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008675#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008676 /* Restore the error/interrupt/exception state if not
8677 * discarded by a new aborting error, interrupt, or
8678 * uncaught exception. */
8679 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 }
8682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 }
8684 else if (readonlymode && curbuf->b_nwindows == 1)
8685 {
8686 /* When editing an already visited buffer, 'readonly' won't be set
8687 * but the previous value is kept. With ":view" and ":sview" we
8688 * want the file to be readonly, except when another window is
8689 * editing the same buffer. */
8690 curbuf->b_p_ro = TRUE;
8691 }
8692 readonlymode = n;
8693 }
8694 else
8695 {
8696 if (eap->do_ecmd_cmd != NULL)
8697 do_cmdline_cmd(eap->do_ecmd_cmd);
8698#ifdef FEAT_TITLE
8699 n = curwin->w_arg_idx_invalid;
8700#endif
8701 check_arg_idx(curwin);
8702#ifdef FEAT_TITLE
8703 if (n != curwin->w_arg_idx_invalid)
8704 maketitle();
8705#endif
8706 }
8707
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 /*
8709 * if ":split file" worked, set alternate file name in old window to new
8710 * file
8711 */
8712 if (old_curwin != NULL
8713 && *eap->arg != NUL
8714 && curwin != old_curwin
8715 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008716 && old_curwin->w_buffer != curbuf
8717 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719
8720 ex_no_reprint = TRUE;
8721}
8722
8723#ifndef FEAT_GUI
8724/*
8725 * ":gui" and ":gvim" when there is no GUI.
8726 */
8727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008728ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729{
8730 eap->errmsg = e_nogvim;
8731}
8732#endif
8733
8734#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008736ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737{
8738 gui_make_tearoff(eap->arg);
8739}
8740#endif
8741
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008742#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8743 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008745ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008747# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8748 if (gui.in_use)
8749 gui_make_popup(eap->arg, eap->forceit);
8750# ifdef FEAT_TERM_POPUP_MENU
8751 else
8752# endif
8753# endif
8754# ifdef FEAT_TERM_POPUP_MENU
8755 pum_make_popup(eap->arg, eap->forceit);
8756# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757}
8758#endif
8759
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008761ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762{
8763 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8764 MSG(_("No swap file"));
8765 else
8766 msg(curbuf->b_ml.ml_mfp->mf_fname);
8767}
8768
8769/*
8770 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8771 * offset.
8772 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008775ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008778 win_T *save_curwin = curwin;
8779 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 long topline;
8781 long y;
8782 linenr_T old_linenr = curwin->w_cursor.lnum;
8783
8784 setpcmark();
8785
8786 /*
8787 * determine max topline
8788 */
8789 if (curwin->w_p_scb)
8790 {
8791 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008792 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 {
8794 if (wp->w_p_scb && wp->w_buffer)
8795 {
8796 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8797 if (topline > y)
8798 topline = y;
8799 }
8800 }
8801 if (topline < 1)
8802 topline = 1;
8803 }
8804 else
8805 {
8806 topline = 1;
8807 }
8808
8809
8810 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008811 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008813 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 {
8815 if (curwin->w_p_scb)
8816 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008817 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 y = topline - curwin->w_topline;
8819 if (y > 0)
8820 scrollup(y, TRUE);
8821 else
8822 scrolldown(-y, TRUE);
8823 curwin->w_scbind_pos = topline;
8824 redraw_later(VALID);
8825 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 }
8828 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008829 curwin = save_curwin;
8830 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 if (curwin->w_p_scb)
8832 {
8833 did_syncbind = TRUE;
8834 checkpcmark();
8835 if (old_linenr != curwin->w_cursor.lnum)
8836 {
8837 char_u ctrl_o[2];
8838
8839 ctrl_o[0] = Ctrl_O;
8840 ctrl_o[1] = 0;
8841 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8842 }
8843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844}
8845
8846
8847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008848ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008850 int i;
8851 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8852 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853
8854 if (eap->usefilter) /* :r!cmd */
8855 do_bang(1, eap, FALSE, FALSE, TRUE);
8856 else
8857 {
8858 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8859 return;
8860
8861#ifdef FEAT_BROWSE
8862 if (cmdmod.browse)
8863 {
8864 char_u *browseFile;
8865
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008866 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 NULL, NULL, NULL, curbuf);
8868 if (browseFile != NULL)
8869 {
8870 i = readfile(browseFile, NULL,
8871 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8872 vim_free(browseFile);
8873 }
8874 else
8875 i = OK;
8876 }
8877 else
8878#endif
8879 if (*eap->arg == NUL)
8880 {
8881 if (check_fname() == FAIL) /* check for no file name */
8882 return;
8883 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8884 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8885 }
8886 else
8887 {
8888 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8889 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8890 i = readfile(eap->arg, NULL,
8891 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8892
8893 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008894 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008896#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 if (!aborting())
8898#endif
8899 EMSG2(_(e_notopen), eap->arg);
8900 }
8901 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008902 {
8903 if (empty && exmode_active)
8904 {
8905 /* Delete the empty line that remains. Historically ex does
8906 * this but vi doesn't. */
8907 if (eap->line2 == 0)
8908 lnum = curbuf->b_ml.ml_line_count;
8909 else
8910 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008911 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008912 {
8913 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008914 if (curwin->w_cursor.lnum > 1
8915 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008916 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008917 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008918 }
8919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 }
8923}
8924
Bram Moolenaarea408852005-06-25 22:49:46 +00008925static char_u *prev_dir = NULL;
8926
8927#if defined(EXITFREE) || defined(PROTO)
8928 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008929free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008930{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008931 VIM_CLEAR(prev_dir);
8932 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00008933}
8934#endif
8935
Bram Moolenaarf4258302013-06-02 18:20:17 +02008936/*
8937 * Deal with the side effects of changing the current directory.
8938 * When "local" is TRUE then this was after an ":lcd" command.
8939 */
8940 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008941post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008942{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008943 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008944 if (local)
8945 {
8946 /* If still in global directory, need to remember current
8947 * directory as global directory. */
8948 if (globaldir == NULL && prev_dir != NULL)
8949 globaldir = vim_strsave(prev_dir);
8950 /* Remember this local directory for the window. */
8951 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8952 curwin->w_localdir = vim_strsave(NameBuff);
8953 }
8954 else
8955 {
8956 /* We are now in the global directory, no need to remember its
8957 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01008958 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008959 }
8960
8961 shorten_fnames(TRUE);
8962}
8963
Bram Moolenaarea408852005-06-25 22:49:46 +00008964
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965/*
8966 * ":cd", ":lcd", ":chdir" and ":lchdir".
8967 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008968 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008969ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 char_u *new_dir;
8972 char_u *tofree;
8973
8974 new_dir = eap->arg;
8975#if !defined(UNIX) && !defined(VMS)
8976 /* for non-UNIX ":cd" means: print current directory */
8977 if (*new_dir == NUL)
8978 ex_pwd(NULL);
8979 else
8980#endif
8981 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008982 if (allbuf_locked())
8983 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008984 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8985 && !eap->forceit)
8986 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008987 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008988 return;
8989 }
8990
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 /* ":cd -": Change to previous directory */
8992 if (STRCMP(new_dir, "-") == 0)
8993 {
8994 if (prev_dir == NULL)
8995 {
8996 EMSG(_("E186: No previous directory"));
8997 return;
8998 }
8999 new_dir = prev_dir;
9000 }
9001
9002 /* Save current directory for next ":cd -" */
9003 tofree = prev_dir;
9004 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9005 prev_dir = vim_strsave(NameBuff);
9006 else
9007 prev_dir = NULL;
9008
9009#if defined(UNIX) || defined(VMS)
9010 /* for UNIX ":cd" means: go to home directory */
9011 if (*new_dir == NUL)
9012 {
9013 /* use NameBuff for home directory name */
9014# ifdef VMS
9015 char_u *p;
9016
9017 p = mch_getenv((char_u *)"SYS$LOGIN");
9018 if (p == NULL || *p == NUL) /* empty is the same as not set */
9019 NameBuff[0] = NUL;
9020 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009021 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022# else
9023 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9024# endif
9025 new_dir = NameBuff;
9026 }
9027#endif
9028 if (new_dir == NULL || vim_chdir(new_dir))
9029 EMSG(_(e_failed));
9030 else
9031 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009032 int is_local_chdir = eap->cmdidx == CMD_lcd
9033 || eap->cmdidx == CMD_lchdir;
9034
9035 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036
9037 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009038 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009040 apply_autocmds(EVENT_DIRCHANGED,
9041 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9042 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 }
9044 vim_free(tofree);
9045 }
9046}
9047
9048/*
9049 * ":pwd".
9050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009052ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
9054 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9055 {
9056#ifdef BACKSLASH_IN_FILENAME
9057 slash_adjust(NameBuff);
9058#endif
9059 msg(NameBuff);
9060 }
9061 else
9062 EMSG(_("E187: Unknown"));
9063}
9064
9065/*
9066 * ":=".
9067 */
9068 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009069ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070{
9071 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009072 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073}
9074
9075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009076ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009078 int n;
9079 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080
9081 if (cursor_valid())
9082 {
9083 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9084 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009085 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009087
9088 len = eap->line2;
9089 switch (*eap->arg)
9090 {
9091 case 'm': break;
9092 case NUL: len *= 1000L; break;
9093 default: EMSG2(_(e_invarg2), eap->arg); return;
9094 }
9095 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096}
9097
9098/*
9099 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9100 */
9101 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009102do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103{
9104 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009105 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106
9107 cursor_on();
9108 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009109 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009111 wait_now = msec - done > 1000L ? 1000L : msec - done;
9112#ifdef FEAT_TIMERS
9113 {
9114 long due_time = check_due_timer();
9115
9116 if (due_time > 0 && due_time < wait_now)
9117 wait_now = due_time;
9118 }
9119#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009120#ifdef FEAT_JOB_CHANNEL
9121 if (has_any_channel() && wait_now > 100L)
9122 wait_now = 100L;
9123#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009124 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009125#ifdef FEAT_JOB_CHANNEL
9126 if (has_any_channel())
9127 ui_breakcheck_force(TRUE);
9128 else
9129#endif
9130 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009131#ifdef MESSAGE_QUEUE
9132 /* Process the netbeans and clientserver messages that may have been
9133 * received in the call to ui_breakcheck() when the GUI is in use. This
9134 * may occur when running a test case. */
9135 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 }
9138}
9139
9140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009141do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142{
9143 int mode;
9144 char_u *cmdp;
9145
9146 cmdp = eap->cmd;
9147 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9148
9149 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9150 eap->arg, mode, isabbrev))
9151 {
9152 case 1: EMSG(_(e_invarg));
9153 break;
9154 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9155 break;
9156 }
9157}
9158
9159/*
9160 * ":winsize" command (obsolete).
9161 */
9162 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009163ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
9165 int w, h;
9166 char_u *arg = eap->arg;
9167 char_u *p;
9168
9169 w = getdigits(&arg);
9170 arg = skipwhite(arg);
9171 p = arg;
9172 h = getdigits(&arg);
9173 if (*p != NUL && *arg == NUL)
9174 set_shellsize(w, h, TRUE);
9175 else
9176 EMSG(_("E465: :winsize requires two number arguments"));
9177}
9178
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009180ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181{
9182 int xchar = NUL;
9183 char_u *p;
9184
9185 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9186 {
9187 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9188 if (eap->arg[1] == NUL)
9189 {
9190 EMSG(_(e_invarg));
9191 return;
9192 }
9193 xchar = eap->arg[1];
9194 p = eap->arg + 2;
9195 }
9196 else
9197 p = eap->arg + 1;
9198
9199 eap->nextcmd = check_nextcmd(p);
9200 p = skipwhite(p);
9201 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9202 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009203 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 {
9205 /* Pass flags on for ":vertical wincmd ]". */
9206 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009207 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9209 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009210 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 }
9212}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213
Bram Moolenaar843ee412004-06-30 16:16:41 +00009214#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215/*
9216 * ":winpos".
9217 */
9218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009219ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220{
9221 int x, y;
9222 char_u *arg = eap->arg;
9223 char_u *p;
9224
9225 if (*arg == NUL)
9226 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009227# if defined(FEAT_GUI) || defined(MSWIN)
9228# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009230# else
9231 if (mch_get_winpos(&x, &y) != FAIL)
9232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 {
9234 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9235 msg(IObuff);
9236 }
9237 else
9238# endif
9239 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9240 }
9241 else
9242 {
9243 x = getdigits(&arg);
9244 arg = skipwhite(arg);
9245 p = arg;
9246 y = getdigits(&arg);
9247 if (*p == NUL || *arg != NUL)
9248 {
9249 EMSG(_("E466: :winpos requires two number arguments"));
9250 return;
9251 }
9252# ifdef FEAT_GUI
9253 if (gui.in_use)
9254 gui_mch_set_winpos(x, y);
9255 else if (gui.starting)
9256 {
9257 /* Remember the coordinates for when the window is opened. */
9258 gui_win_x = x;
9259 gui_win_y = y;
9260 }
9261# ifdef HAVE_TGETENT
9262 else
9263# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009264# else
9265# ifdef MSWIN
9266 mch_set_winpos(x, y);
9267# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268# endif
9269# ifdef HAVE_TGETENT
9270 if (*T_CWP)
9271 term_set_winpos(x, y);
9272# endif
9273 }
9274}
9275#endif
9276
9277/*
9278 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9279 */
9280 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009281ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282{
9283 oparg_T oa;
9284
9285 clear_oparg(&oa);
9286 oa.regname = eap->regname;
9287 oa.start.lnum = eap->line1;
9288 oa.end.lnum = eap->line2;
9289 oa.line_count = eap->line2 - eap->line1 + 1;
9290 oa.motion_type = MLINE;
9291#ifdef FEAT_VIRTUALEDIT
9292 virtual_op = FALSE;
9293#endif
9294 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9295 {
9296 setpcmark();
9297 curwin->w_cursor.lnum = eap->line1;
9298 beginline(BL_SOL | BL_FIX);
9299 }
9300
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009301 if (VIsual_active)
9302 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009303
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 switch (eap->cmdidx)
9305 {
9306 case CMD_delete:
9307 oa.op_type = OP_DELETE;
9308 op_delete(&oa);
9309 break;
9310
9311 case CMD_yank:
9312 oa.op_type = OP_YANK;
9313 (void)op_yank(&oa, FALSE, TRUE);
9314 break;
9315
9316 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009317 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009319 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9320#else
9321 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009323 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 oa.op_type = OP_RSHIFT;
9325 else
9326 oa.op_type = OP_LSHIFT;
9327 op_shift(&oa, FALSE, eap->amount);
9328 break;
9329 }
9330#ifdef FEAT_VIRTUALEDIT
9331 virtual_op = MAYBE;
9332#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009333 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334}
9335
9336/*
9337 * ":put".
9338 */
9339 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009340ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341{
9342 /* ":0put" works like ":1put!". */
9343 if (eap->line2 == 0)
9344 {
9345 eap->line2 = 1;
9346 eap->forceit = TRUE;
9347 }
9348 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009349 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9350 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351}
9352
9353/*
9354 * Handle ":copy" and ":move".
9355 */
9356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009357ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358{
9359 long n;
9360
Bram Moolenaarded27822017-01-02 14:27:34 +01009361 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 if (eap->arg == NULL) /* error detected */
9363 {
9364 eap->nextcmd = NULL;
9365 return;
9366 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009367 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368
9369 /*
9370 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9371 */
9372 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9373 {
9374 EMSG(_(e_invaddr));
9375 return;
9376 }
9377
9378 if (eap->cmdidx == CMD_move)
9379 {
9380 if (do_move(eap->line1, eap->line2, n) == FAIL)
9381 return;
9382 }
9383 else
9384 ex_copy(eap->line1, eap->line2, n);
9385 u_clearline();
9386 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009387 ex_may_print(eap);
9388}
9389
9390/*
9391 * Print the current line if flags were given to the Ex command.
9392 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009393 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009394ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009395{
9396 if (eap->flags != 0)
9397 {
9398 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9399 (eap->flags & EXFLAG_LIST));
9400 ex_no_reprint = TRUE;
9401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402}
9403
9404/*
9405 * ":smagic" and ":snomagic".
9406 */
9407 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009408ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
9410 int magic_save = p_magic;
9411
9412 p_magic = (eap->cmdidx == CMD_smagic);
9413 do_sub(eap);
9414 p_magic = magic_save;
9415}
9416
9417/*
9418 * ":join".
9419 */
9420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009421ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422{
9423 curwin->w_cursor.lnum = eap->line1;
9424 if (eap->line1 == eap->line2)
9425 {
9426 if (eap->addr_count >= 2) /* :2,2join does nothing */
9427 return;
9428 if (eap->line2 == curbuf->b_ml.ml_line_count)
9429 {
9430 beep_flush();
9431 return;
9432 }
9433 ++eap->line2;
9434 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009435 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009437 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438}
9439
9440/*
9441 * ":[addr]@r" or ":[addr]*r": execute register
9442 */
9443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009444ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445{
9446 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009447 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448
9449 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009450 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451
9452#ifdef USE_ON_FLY_SCROLL
9453 dont_scroll = TRUE; /* disallow scrolling here */
9454#endif
9455
9456 /* get the register name. No name means to use the previous one */
9457 c = *eap->arg;
9458 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9459 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009460 /* Put the register in the typeahead buffer with the "silent" flag. */
9461 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9462 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 else
9467 {
9468 int save_efr = exec_from_reg;
9469
9470 exec_from_reg = TRUE;
9471
9472 /*
9473 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009474 * Continue until the stuff buffer is empty and all added characters
9475 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009477 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9479
9480 exec_from_reg = save_efr;
9481 }
9482}
9483
9484/*
9485 * ":!".
9486 */
9487 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009488ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489{
9490 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9491}
9492
9493/*
9494 * ":undo".
9495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009497ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009499 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009500 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009501 else
9502 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503}
9504
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009505#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009507ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009508{
9509 char_u hash[UNDO_HASH_SIZE];
9510
9511 u_compute_hash(hash);
9512 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9513}
9514
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009516ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009517{
9518 char_u hash[UNDO_HASH_SIZE];
9519
9520 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009521 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009522}
9523#endif
9524
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525/*
9526 * ":redo".
9527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009529ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530{
9531 u_redo(1);
9532}
9533
9534/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009535 * ":earlier" and ":later".
9536 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009538ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009539{
9540 long count = 0;
9541 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009542 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009543 char_u *p = eap->arg;
9544
9545 if (*p == NUL)
9546 count = 1;
9547 else if (isdigit(*p))
9548 {
9549 count = getdigits(&p);
9550 switch (*p)
9551 {
9552 case 's': ++p; sec = TRUE; break;
9553 case 'm': ++p; sec = TRUE; count *= 60; break;
9554 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009555 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9556 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009557 }
9558 }
9559
9560 if (*p != NUL)
9561 EMSG2(_(e_invarg2), eap->arg);
9562 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009563 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9564 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009565}
9566
9567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 * ":redir": start/stop redirection.
9569 */
9570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009571ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572{
9573 char *mode;
9574 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009575 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576
Bram Moolenaarba768492016-07-08 15:32:54 +02009577#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009578 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009579 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009580 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009581 return;
9582 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009583#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009584
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 if (STRICMP(eap->arg, "END") == 0)
9586 close_redir();
9587 else
9588 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009589 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009591 ++arg;
9592 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009594 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 mode = "a";
9596 }
9597 else
9598 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009599 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600
9601 close_redir();
9602
9603 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009604 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 if (fname == NULL)
9606 return;
9607#ifdef FEAT_BROWSE
9608 if (cmdmod.browse)
9609 {
9610 char_u *browseFile;
9611
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009612 browseFile = do_browse(BROWSE_SAVE,
9613 (char_u *)_("Save Redirection"),
9614 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 if (browseFile == NULL)
9616 return; /* operation cancelled */
9617 vim_free(fname);
9618 fname = browseFile;
9619 eap->forceit = TRUE; /* since dialog already asked */
9620 }
9621#endif
9622
9623 redir_fd = open_exfile(fname, eap->forceit, mode);
9624 vim_free(fname);
9625 }
9626#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009627 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 {
9629 /* redirect to a register a-z (resp. A-Z for appending) */
9630 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009631 ++arg;
9632 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009634 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009635 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009637 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009639 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009640 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009641 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009642 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009644 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009645 if (*arg == '>')
9646 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009647 /* Make register empty when not using @A-@Z and the
9648 * command is valid. */
9649 if (*arg == NUL && !isupper(redir_reg))
9650 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009652 }
9653 if (*arg != NUL)
9654 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009655 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009656 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009657 }
9658 }
9659 else if (*arg == '=' && arg[1] == '>')
9660 {
9661 int append;
9662
9663 /* redirect to a variable */
9664 close_redir();
9665 arg += 2;
9666
9667 if (*arg == '>')
9668 {
9669 ++arg;
9670 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 }
9672 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009673 append = FALSE;
9674
9675 if (var_redir_start(skipwhite(arg), append) == OK)
9676 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 }
9678#endif
9679
9680 /* TODO: redirect to a buffer */
9681
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009683 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009685
9686 /* Make sure redirection is not off. Can happen for cmdline completion
9687 * that indirectly invokes a command to catch its output. */
9688 if (redir_fd != NULL
9689#ifdef FEAT_EVAL
9690 || redir_reg || redir_vname
9691#endif
9692 )
9693 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694}
9695
9696/*
9697 * ":redraw": force redraw
9698 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009699 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009700ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701{
9702 int r = RedrawingDisabled;
9703 int p = p_lz;
9704
9705 RedrawingDisabled = 0;
9706 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009707 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009709 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710#ifdef FEAT_TITLE
9711 if (need_maketitle)
9712 maketitle();
9713#endif
9714 RedrawingDisabled = r;
9715 p_lz = p;
9716
9717 /* Reset msg_didout, so that a message that's there is overwritten. */
9718 msg_didout = FALSE;
9719 msg_col = 0;
9720
Bram Moolenaar56667a52013-07-17 11:54:28 +02009721 /* No need to wait after an intentional redraw. */
9722 need_wait_return = FALSE;
9723
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 out_flush();
9725}
9726
9727/*
9728 * ":redrawstatus": force redraw of status line(s)
9729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009731ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 int r = RedrawingDisabled;
9734 int p = p_lz;
9735
9736 RedrawingDisabled = 0;
9737 p_lz = FALSE;
9738 if (eap->forceit)
9739 status_redraw_all();
9740 else
9741 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009742 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 RedrawingDisabled = r;
9744 p_lz = p;
9745 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746}
9747
9748 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009749close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750{
9751 if (redir_fd != NULL)
9752 {
9753 fclose(redir_fd);
9754 redir_fd = NULL;
9755 }
9756#ifdef FEAT_EVAL
9757 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009758 if (redir_vname)
9759 {
9760 var_redir_stop();
9761 redir_vname = 0;
9762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763#endif
9764}
9765
9766#if defined(FEAT_SESSION) && defined(USE_CRNL)
9767# define MKSESSION_NL
9768static int mksession_nl = FALSE; /* use NL only in put_eol() */
9769#endif
9770
9771/*
9772 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9773 */
9774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009775ex_mkrc(
9776 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777{
9778 FILE *fd;
9779 int failed = FALSE;
9780 char_u *fname;
9781#ifdef FEAT_BROWSE
9782 char_u *browseFile = NULL;
9783#endif
9784#ifdef FEAT_SESSION
9785 int view_session = FALSE;
9786 int using_vdir = FALSE; /* using 'viewdir'? */
9787 char_u *viewFile = NULL;
9788 unsigned *flagp;
9789#endif
9790
9791 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9792 {
9793#ifdef FEAT_SESSION
9794 view_session = TRUE;
9795#else
9796 ex_ni(eap);
9797 return;
9798#endif
9799 }
9800
9801#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009802 /* Use the short file name until ":lcd" is used. We also don't use the
9803 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009804 did_lcd = FALSE;
9805
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9807 if (eap->cmdidx == CMD_mkview
9808 && (*eap->arg == NUL
9809 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9810 {
9811 eap->forceit = TRUE;
9812 fname = get_view_file(*eap->arg);
9813 if (fname == NULL)
9814 return;
9815 viewFile = fname;
9816 using_vdir = TRUE;
9817 }
9818 else
9819#endif
9820 if (*eap->arg != NUL)
9821 fname = eap->arg;
9822 else if (eap->cmdidx == CMD_mkvimrc)
9823 fname = (char_u *)VIMRC_FILE;
9824#ifdef FEAT_SESSION
9825 else if (eap->cmdidx == CMD_mksession)
9826 fname = (char_u *)SESSION_FILE;
9827#endif
9828 else
9829 fname = (char_u *)EXRC_FILE;
9830
9831#ifdef FEAT_BROWSE
9832 if (cmdmod.browse)
9833 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009834 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835# ifdef FEAT_SESSION
9836 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9837 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9838# endif
9839 (char_u *)_("Save Setup"),
9840 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9841 if (browseFile == NULL)
9842 goto theend;
9843 fname = browseFile;
9844 eap->forceit = TRUE; /* since dialog already asked */
9845 }
9846#endif
9847
9848#if defined(FEAT_SESSION) && defined(vim_mkdir)
9849 /* When using 'viewdir' may have to create the directory. */
9850 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009851 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852#endif
9853
9854 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9855 if (fd != NULL)
9856 {
9857#ifdef FEAT_SESSION
9858 if (eap->cmdidx == CMD_mkview)
9859 flagp = &vop_flags;
9860 else
9861 flagp = &ssop_flags;
9862#endif
9863
9864#ifdef MKSESSION_NL
9865 /* "unix" in 'sessionoptions': use NL line separator */
9866 if (view_session && (*flagp & SSOP_UNIX))
9867 mksession_nl = TRUE;
9868#endif
9869
9870 /* Write the version command for :mkvimrc */
9871 if (eap->cmdidx == CMD_mkvimrc)
9872 (void)put_line(fd, "version 6.0");
9873
9874#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009875 if (eap->cmdidx == CMD_mksession)
9876 {
9877 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9878 failed = TRUE;
9879 }
9880
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 if (eap->cmdidx != CMD_mkview)
9882#endif
9883 {
9884 /* Write setting 'compatible' first, because it has side effects.
9885 * For that same reason only do it when needed. */
9886 if (p_cp)
9887 (void)put_line(fd, "if !&cp | set cp | endif");
9888 else
9889 (void)put_line(fd, "if &cp | set nocp | endif");
9890 }
9891
9892#ifdef FEAT_SESSION
9893 if (!view_session
9894 || (eap->cmdidx == CMD_mksession
9895 && (*flagp & SSOP_OPTIONS)))
9896#endif
9897 failed |= (makemap(fd, NULL) == FAIL
9898 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9899
9900#ifdef FEAT_SESSION
9901 if (!failed && view_session)
9902 {
9903 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9904 failed = TRUE;
9905 if (eap->cmdidx == CMD_mksession)
9906 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009907 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908
Bram Moolenaard9462e32011-04-11 21:35:11 +02009909 dirnow = alloc(MAXPATHL);
9910 if (dirnow == NULL)
9911 failed = TRUE;
9912 else
9913 {
9914 /*
9915 * Change to session file's dir.
9916 */
9917 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009919 *dirnow = NUL;
9920 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9921 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009922 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009923 shorten_fnames(TRUE);
9924 }
9925 else if (*dirnow != NUL
9926 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9927 {
9928 if (mch_chdir((char *)globaldir) == 0)
9929 shorten_fnames(TRUE);
9930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931
Bram Moolenaard9462e32011-04-11 21:35:11 +02009932 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933
Bram Moolenaard9462e32011-04-11 21:35:11 +02009934 /* restore original dir */
9935 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009937 {
9938 if (mch_chdir((char *)dirnow) != 0)
9939 EMSG(_(e_prev_dir));
9940 shorten_fnames(TRUE);
9941 }
9942 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 }
9944 }
9945 else
9946 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009947 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9948 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 }
9950 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9951 == FAIL)
9952 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009953 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9954 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009955 if (eap->cmdidx == CMD_mksession)
9956 {
9957 if (put_line(fd, "unlet SessionLoad") == FAIL)
9958 failed = TRUE;
9959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 }
9961#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009962 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9963 failed = TRUE;
9964
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 failed |= fclose(fd);
9966
9967 if (failed)
9968 EMSG(_(e_write));
9969#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9970 else if (eap->cmdidx == CMD_mksession)
9971 {
9972 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009973 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974
Bram Moolenaard9462e32011-04-11 21:35:11 +02009975 tbuf = alloc(MAXPATHL);
9976 if (tbuf != NULL)
9977 {
9978 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9979 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9980 vim_free(tbuf);
9981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 }
9983#endif
9984#ifdef MKSESSION_NL
9985 mksession_nl = FALSE;
9986#endif
9987 }
9988
9989#ifdef FEAT_BROWSE
9990theend:
9991 vim_free(browseFile);
9992#endif
9993#ifdef FEAT_SESSION
9994 vim_free(viewFile);
9995#endif
9996}
9997
Bram Moolenaardf177f62005-02-22 08:39:57 +00009998#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9999 || defined(PROTO)
10000 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010001vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010002{
10003 if (vim_mkdir(name, prot) != 0)
10004 {
10005 EMSG2(_("E739: Cannot create directory: %s"), name);
10006 return FAIL;
10007 }
10008 return OK;
10009}
10010#endif
10011
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012/*
10013 * Open a file for writing for an Ex command, with some checks.
10014 * Return file descriptor, or NULL on failure.
10015 */
10016 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010017open_exfile(
10018 char_u *fname,
10019 int forceit,
10020 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021{
10022 FILE *fd;
10023
10024#ifdef UNIX
10025 /* with Unix it is possible to open a directory */
10026 if (mch_isdir(fname))
10027 {
10028 EMSG2(_(e_isadir2), fname);
10029 return NULL;
10030 }
10031#endif
10032 if (!forceit && *mode != 'a' && vim_fexists(fname))
10033 {
10034 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10035 return NULL;
10036 }
10037
10038 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10039 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10040
10041 return fd;
10042}
10043
10044/*
10045 * ":mark" and ":k".
10046 */
10047 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010048ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049{
10050 pos_T pos;
10051
10052 if (*eap->arg == NUL) /* No argument? */
10053 EMSG(_(e_argreq));
10054 else if (eap->arg[1] != NUL) /* more than one character? */
10055 EMSG(_(e_trailing));
10056 else
10057 {
10058 pos = curwin->w_cursor; /* save curwin->w_cursor */
10059 curwin->w_cursor.lnum = eap->line2;
10060 beginline(BL_WHITE | BL_FIX);
10061 if (setmark(*eap->arg) == FAIL) /* set mark */
10062 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10063 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10064 }
10065}
10066
10067/*
10068 * Update w_topline, w_leftcol and the cursor position.
10069 */
10070 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010071update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072{
10073 check_cursor(); /* put cursor on valid line */
10074 update_topline();
10075 if (!curwin->w_p_wrap)
10076 validate_cursor();
10077 update_curswant();
10078}
10079
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010081 * Save the current State and go to Normal mode.
10082 * Return TRUE if the typeahead could be saved.
10083 */
10084 int
10085save_current_state(save_state_T *sst)
10086{
10087 sst->save_msg_scroll = msg_scroll;
10088 sst->save_restart_edit = restart_edit;
10089 sst->save_msg_didout = msg_didout;
10090 sst->save_State = State;
10091 sst->save_insertmode = p_im;
10092 sst->save_finish_op = finish_op;
10093 sst->save_opcount = opcount;
10094
10095 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10096 restart_edit = 0; /* don't go to Insert mode */
10097 p_im = FALSE; /* don't use 'insertmode' */
10098
10099 /*
10100 * Save the current typeahead. This is required to allow using ":normal"
10101 * from an event handler and makes sure we don't hang when the argument
10102 * ends with half a command.
10103 */
10104 save_typeahead(&sst->tabuf);
10105 return sst->tabuf.typebuf_valid;
10106}
10107
10108 void
10109restore_current_state(save_state_T *sst)
10110{
10111 /* Restore the previous typeahead. */
10112 restore_typeahead(&sst->tabuf);
10113
10114 msg_scroll = sst->save_msg_scroll;
10115 restart_edit = sst->save_restart_edit;
10116 p_im = sst->save_insertmode;
10117 finish_op = sst->save_finish_op;
10118 opcount = sst->save_opcount;
10119 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10120
10121 /* Restore the state (needed when called from a function executed for
10122 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10123 State = sst->save_State;
10124#ifdef CURSOR_SHAPE
10125 ui_cursor_shape(); /* may show different cursor shape */
10126#endif
10127}
10128
10129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 * ":normal[!] {commands}": Execute normal mode commands.
10131 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010132 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010133ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010135 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136#ifdef FEAT_MBYTE
10137 char_u *arg = NULL;
10138 int l;
10139 char_u *p;
10140#endif
10141
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010142 if (ex_normal_lock > 0)
10143 {
10144 EMSG(_(e_secure));
10145 return;
10146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 if (ex_normal_busy >= p_mmd)
10148 {
10149 EMSG(_("E192: Recursive use of :normal too deep"));
10150 return;
10151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152
10153#ifdef FEAT_MBYTE
10154 /*
10155 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10156 * this for the K_SPECIAL leading byte, otherwise special keys will not
10157 * work.
10158 */
10159 if (has_mbyte)
10160 {
10161 int len = 0;
10162
10163 /* Count the number of characters to be escaped. */
10164 for (p = eap->arg; *p != NUL; ++p)
10165 {
10166# ifdef FEAT_GUI
10167 if (*p == CSI) /* leadbyte CSI */
10168 len += 2;
10169# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010170 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10172# ifdef FEAT_GUI
10173 || *p == CSI
10174# endif
10175 )
10176 len += 2;
10177 }
10178 if (len > 0)
10179 {
10180 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10181 if (arg != NULL)
10182 {
10183 len = 0;
10184 for (p = eap->arg; *p != NUL; ++p)
10185 {
10186 arg[len++] = *p;
10187# ifdef FEAT_GUI
10188 if (*p == CSI)
10189 {
10190 arg[len++] = KS_EXTRA;
10191 arg[len++] = (int)KE_CSI;
10192 }
10193# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010194 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 {
10196 arg[len++] = *++p;
10197 if (*p == K_SPECIAL)
10198 {
10199 arg[len++] = KS_SPECIAL;
10200 arg[len++] = KE_FILLER;
10201 }
10202# ifdef FEAT_GUI
10203 else if (*p == CSI)
10204 {
10205 arg[len++] = KS_EXTRA;
10206 arg[len++] = (int)KE_CSI;
10207 }
10208# endif
10209 }
10210 arg[len] = NUL;
10211 }
10212 }
10213 }
10214 }
10215#endif
10216
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010217 ++ex_normal_busy;
10218 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 {
10220 /*
10221 * Repeat the :normal command for each line in the range. When no
10222 * range given, execute it just once, without positioning the cursor
10223 * first.
10224 */
10225 do
10226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 if (eap->addr_count != 0)
10228 {
10229 curwin->w_cursor.lnum = eap->line1++;
10230 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010231 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 }
10233
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010234 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235#ifdef FEAT_MBYTE
10236 arg != NULL ? arg :
10237#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010238 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 }
10240 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10241 }
10242
10243 /* Might not return to the main loop when in an event handler. */
10244 update_topline_cursor();
10245
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010246 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010248#ifdef FEAT_MOUSE
10249 setmouse();
10250#endif
10251#ifdef CURSOR_SHAPE
10252 ui_cursor_shape(); /* may show different cursor shape */
10253#endif
10254
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255#ifdef FEAT_MBYTE
10256 vim_free(arg);
10257#endif
10258}
10259
10260/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010261 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 */
10263 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010264ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010266 if (eap->forceit)
10267 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010268 /* cursor line can be zero on startup */
10269 if (!curwin->w_cursor.lnum)
10270 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010271 coladvance((colnr_T)MAXCOL);
10272 curwin->w_curswant = MAXCOL;
10273 curwin->w_set_curswant = FALSE;
10274 }
10275
Bram Moolenaara40c5002005-01-09 21:16:21 +000010276 /* Ignore the command when already in Insert mode. Inserting an
10277 * expression register that invokes a function can do this. */
10278 if (State & INSERT)
10279 return;
10280
Bram Moolenaar64969662005-12-14 21:59:55 +000010281 if (eap->cmdidx == CMD_startinsert)
10282 restart_edit = 'a';
10283 else if (eap->cmdidx == CMD_startreplace)
10284 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010286 restart_edit = 'V';
10287
10288 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010290 if (eap->cmdidx == CMD_startinsert)
10291 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 curwin->w_curswant = 0; /* avoid MAXCOL */
10293 }
10294}
10295
10296/*
10297 * ":stopinsert"
10298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010300ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301{
10302 restart_edit = 0;
10303 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010304 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010307/*
10308 * Execute normal mode command "cmd".
10309 * "remap" can be REMAP_NONE or REMAP_YES.
10310 */
10311 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010312exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010313{
Bram Moolenaar25281632016-01-21 23:32:32 +010010314 /* Stuff the argument into the typeahead buffer. */
10315 ins_typebuf(cmd, remap, 0, TRUE, silent);
10316 exec_normal(FALSE);
10317}
Bram Moolenaar25281632016-01-21 23:32:32 +010010318
Bram Moolenaar25281632016-01-21 23:32:32 +010010319/*
10320 * Execute normal_cmd() until there is no typeahead left.
10321 */
10322 void
10323exec_normal(int was_typed)
10324{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010325 oparg_T oa;
10326
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010327 clear_oparg(&oa);
10328 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010329 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10330 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010331 {
10332 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010333 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010334 }
10335}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010336
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337#ifdef FEAT_FIND_ID
10338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010339ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340{
10341 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10342 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10343 (linenr_T)1, (linenr_T)MAXLNUM);
10344}
10345
Bram Moolenaar4033c552017-09-16 20:54:51 +020010346#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347/*
10348 * ":psearch"
10349 */
10350 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010351ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352{
10353 g_do_tagpreview = p_pvh;
10354 ex_findpat(eap);
10355 g_do_tagpreview = 0;
10356}
10357#endif
10358
10359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010360ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361{
10362 int whole = TRUE;
10363 long n;
10364 char_u *p;
10365 int action;
10366
10367 switch (cmdnames[eap->cmdidx].cmd_name[2])
10368 {
10369 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10370 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10371 action = ACTION_GOTO;
10372 else
10373 action = ACTION_SHOW;
10374 break;
10375 case 'i': /* ":ilist" and ":dlist" */
10376 action = ACTION_SHOW_ALL;
10377 break;
10378 case 'u': /* ":ijump" and ":djump" */
10379 action = ACTION_GOTO;
10380 break;
10381 default: /* ":isplit" and ":dsplit" */
10382 action = ACTION_SPLIT;
10383 break;
10384 }
10385
10386 n = 1;
10387 if (vim_isdigit(*eap->arg)) /* get count */
10388 {
10389 n = getdigits(&eap->arg);
10390 eap->arg = skipwhite(eap->arg);
10391 }
10392 if (*eap->arg == '/') /* Match regexp, not just whole words */
10393 {
10394 whole = FALSE;
10395 ++eap->arg;
10396 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10397 if (*p)
10398 {
10399 *p++ = NUL;
10400 p = skipwhite(p);
10401
10402 /* Check for trailing illegal characters */
10403 if (!ends_excmd(*p))
10404 eap->errmsg = e_trailing;
10405 else
10406 eap->nextcmd = check_nextcmd(p);
10407 }
10408 }
10409 if (!eap->skip)
10410 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10411 whole, !eap->forceit,
10412 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10413 n, action, eap->line1, eap->line2);
10414}
10415#endif
10416
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417
Bram Moolenaar4033c552017-09-16 20:54:51 +020010418#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419/*
10420 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10421 */
10422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010423ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010425 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10427}
10428
10429/*
10430 * ":pedit"
10431 */
10432 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010433ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434{
10435 win_T *curwin_save = curwin;
10436
10437 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010438 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010439 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 do_exedit(eap, NULL);
10441 keep_help_flag = FALSE;
10442 if (curwin != curwin_save && win_valid(curwin_save))
10443 {
10444 /* Return cursor to where we were */
10445 validate_cursor();
10446 redraw_later(VALID);
10447 win_enter(curwin_save, TRUE);
10448 }
10449 g_do_tagpreview = 0;
10450}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452
10453/*
10454 * ":stag", ":stselect" and ":stjump".
10455 */
10456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010457ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458{
10459 postponed_split = -1;
10460 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010461 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10463 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010464 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466
10467/*
10468 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10469 */
10470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010471ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472{
10473 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10474}
10475
10476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010477ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478{
10479 int cmd;
10480
10481 switch (name[1])
10482 {
10483 case 'j': cmd = DT_JUMP; /* ":tjump" */
10484 break;
10485 case 's': cmd = DT_SELECT; /* ":tselect" */
10486 break;
10487 case 'p': cmd = DT_PREV; /* ":tprevious" */
10488 break;
10489 case 'N': cmd = DT_PREV; /* ":tNext" */
10490 break;
10491 case 'n': cmd = DT_NEXT; /* ":tnext" */
10492 break;
10493 case 'o': cmd = DT_POP; /* ":pop" */
10494 break;
10495 case 'f': /* ":tfirst" */
10496 case 'r': cmd = DT_FIRST; /* ":trewind" */
10497 break;
10498 case 'l': cmd = DT_LAST; /* ":tlast" */
10499 break;
10500 default: /* ":tag" */
10501#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010502 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010504 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 return;
10506 }
10507#endif
10508 cmd = DT_TAG;
10509 break;
10510 }
10511
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010512 if (name[0] == 'l')
10513 {
10514#ifndef FEAT_QUICKFIX
10515 ex_ni(eap);
10516 return;
10517#else
10518 cmd = DT_LTAG;
10519#endif
10520 }
10521
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10523 eap->forceit, TRUE);
10524}
10525
10526/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010527 * Check "str" for starting with a special cmdline variable.
10528 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10529 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10530 */
10531 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010532find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010533{
10534 int len;
10535 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010536 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010537 "%",
10538#define SPEC_PERC 0
10539 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010540#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010541 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010542#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010543 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010544#define SPEC_CCWORD (SPEC_CWORD + 1)
10545 "<cexpr>", /* expr under cursor */
10546#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010547 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010548#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010549 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010550#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010551 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010552#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010553 "<afile>", /* autocommand file name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010554#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010555 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010556#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010557 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010558#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010559#ifdef FEAT_CLIENTSERVER
10560 "<client>"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010561# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010562#endif
10563 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010564
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010565 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010566 {
10567 len = (int)STRLEN(spec_str[i]);
10568 if (STRNCMP(src, spec_str[i], len) == 0)
10569 {
10570 *usedlen = len;
10571 return i;
10572 }
10573 }
10574 return -1;
10575}
10576
10577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 * Evaluate cmdline variables.
10579 *
10580 * change '%' to curbuf->b_ffname
10581 * '#' to curwin->w_altfile
10582 * '<cword>' to word under the cursor
10583 * '<cWORD>' to WORD under the cursor
10584 * '<cfile>' to path name under the cursor
10585 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010586 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 * '<afile>' to file name for autocommand
10588 * '<abuf>' to buffer number for autocommand
10589 * '<amatch>' to matching name for autocommand
10590 *
10591 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10592 * "" for error without a message) and NULL is returned.
10593 * Returns an allocated string if a valid match was found.
10594 * Returns NULL if no match was found. "usedlen" then still contains the
10595 * number of characters to skip.
10596 */
10597 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010598eval_vars(
10599 char_u *src, /* pointer into commandline */
10600 char_u *srcstart, /* beginning of valid memory for src */
10601 int *usedlen, /* characters after src that are used */
10602 linenr_T *lnump, /* line number for :e command, or NULL */
10603 char_u **errormsg, /* pointer to error message */
10604 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010605 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606{
10607 int i;
10608 char_u *s;
10609 char_u *result;
10610 char_u *resultbuf = NULL;
10611 int resultlen;
10612 buf_T *buf;
10613 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10614 int spec_idx;
10615#ifdef FEAT_MODIFY_FNAME
10616 int skip_mod = FALSE;
10617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619
10620 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010621 if (escaped != NULL)
10622 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623
10624 /*
10625 * Check if there is something to do.
10626 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010627 spec_idx = find_cmdline_var(src, usedlen);
10628 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 {
10630 *usedlen = 1;
10631 return NULL;
10632 }
10633
10634 /*
10635 * Skip when preceded with a backslash "\%" and "\#".
10636 * Note: In "\\%" the % is also not recognized!
10637 */
10638 if (src > srcstart && src[-1] == '\\')
10639 {
10640 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010641 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 return NULL;
10643 }
10644
10645 /*
10646 * word or WORD under cursor
10647 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010648 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10649 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010651 resultlen = find_ident_under_cursor(&result,
10652 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10653 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10654 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 if (resultlen == 0)
10656 {
10657 *errormsg = (char_u *)"";
10658 return NULL;
10659 }
10660 }
10661
10662 /*
10663 * '#': Alternate file name
10664 * '%': Current file name
10665 * File name under the cursor
10666 * File name for autocommand
10667 * and following modifiers
10668 */
10669 else
10670 {
10671 switch (spec_idx)
10672 {
10673 case SPEC_PERC: /* '%': current file */
10674 if (curbuf->b_fname == NULL)
10675 {
10676 result = (char_u *)"";
10677 valid = 0; /* Must have ":p:h" to be valid */
10678 }
10679 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 break;
10682
10683 case SPEC_HASH: /* '#' or "#99": alternate file */
10684 if (src[1] == '#') /* "##": the argument list */
10685 {
10686 result = arg_all();
10687 resultbuf = result;
10688 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010689 if (escaped != NULL)
10690 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691#ifdef FEAT_MODIFY_FNAME
10692 skip_mod = TRUE;
10693#endif
10694 break;
10695 }
10696 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010697 if (*s == '<') /* "#<99" uses v:oldfiles */
10698 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010700 if (s == src + 2 && src[1] == '-')
10701 /* just a minus sign, don't skip over it */
10702 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 *usedlen = (int)(s - src); /* length of what we expand */
10704
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010705 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010707 if (*usedlen < 2)
10708 {
10709 /* Should we give an error message for #<text? */
10710 *usedlen = 1;
10711 return NULL;
10712 }
10713#ifdef FEAT_EVAL
10714 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10715 (long)i);
10716 if (result == NULL)
10717 {
10718 *errormsg = (char_u *)"";
10719 return NULL;
10720 }
10721#else
10722 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 }
10726 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010727 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010728 if (i == 0 && src[1] == '<' && *usedlen > 1)
10729 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010730 buf = buflist_findnr(i);
10731 if (buf == NULL)
10732 {
10733 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10734 return NULL;
10735 }
10736 if (lnump != NULL)
10737 *lnump = ECMD_LAST;
10738 if (buf->b_fname == NULL)
10739 {
10740 result = (char_u *)"";
10741 valid = 0; /* Must have ":p:h" to be valid */
10742 }
10743 else
10744 result = buf->b_fname;
10745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 break;
10747
10748#ifdef FEAT_SEARCHPATH
10749 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010750 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 if (result == NULL)
10752 {
10753 *errormsg = (char_u *)"";
10754 return NULL;
10755 }
10756 resultbuf = result; /* remember allocated string */
10757 break;
10758#endif
10759
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 case SPEC_AFILE: /* file name for autocommand */
10761 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010762 if (result != NULL && !autocmd_fname_full)
10763 {
10764 /* Still need to turn the fname into a full path. It is
10765 * postponed to avoid a delay when <afile> is not used. */
10766 autocmd_fname_full = TRUE;
10767 result = FullName_save(autocmd_fname, FALSE);
10768 vim_free(autocmd_fname);
10769 autocmd_fname = result;
10770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 if (result == NULL)
10772 {
10773 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10774 return NULL;
10775 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010776 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 break;
10778
10779 case SPEC_ABUF: /* buffer number for autocommand */
10780 if (autocmd_bufnr <= 0)
10781 {
10782 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10783 return NULL;
10784 }
10785 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10786 result = strbuf;
10787 break;
10788
10789 case SPEC_AMATCH: /* match name for autocommand */
10790 result = autocmd_match;
10791 if (result == NULL)
10792 {
10793 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10794 return NULL;
10795 }
10796 break;
10797
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 case SPEC_SFILE: /* file name for ":so" command */
10799 result = sourcing_name;
10800 if (result == NULL)
10801 {
10802 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10803 return NULL;
10804 }
10805 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010806 case SPEC_SLNUM: /* line in file for ":so" command */
10807 if (sourcing_name == NULL || sourcing_lnum == 0)
10808 {
10809 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10810 return NULL;
10811 }
10812 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10813 result = strbuf;
10814 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815#if defined(FEAT_CLIENTSERVER)
10816 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010817 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10818 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 result = strbuf;
10820 break;
10821#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010822 default:
10823 result = (char_u *)""; /* avoid gcc warning */
10824 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 }
10826
10827 resultlen = (int)STRLEN(result); /* length of new string */
10828 if (src[*usedlen] == '<') /* remove the file name extension */
10829 {
10830 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 resultlen = (int)(s - result);
10833 }
10834#ifdef FEAT_MODIFY_FNAME
10835 else if (!skip_mod)
10836 {
10837 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10838 &resultlen);
10839 if (result == NULL)
10840 {
10841 *errormsg = (char_u *)"";
10842 return NULL;
10843 }
10844 }
10845#endif
10846 }
10847
10848 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10849 {
10850 if (valid != VALID_HEAD + VALID_PATH)
10851 /* xgettext:no-c-format */
10852 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10853 else
10854 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10855 result = NULL;
10856 }
10857 else
10858 result = vim_strnsave(result, resultlen);
10859 vim_free(resultbuf);
10860 return result;
10861}
10862
10863/*
10864 * Concatenate all files in the argument list, separated by spaces, and return
10865 * it in one allocated string.
10866 * Spaces and backslashes in the file names are escaped with a backslash.
10867 * Returns NULL when out of memory.
10868 */
10869 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010870arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871{
10872 int len;
10873 int idx;
10874 char_u *retval = NULL;
10875 char_u *p;
10876
10877 /*
10878 * Do this loop two times:
10879 * first time: compute the total length
10880 * second time: concatenate the names
10881 */
10882 for (;;)
10883 {
10884 len = 0;
10885 for (idx = 0; idx < ARGCOUNT; ++idx)
10886 {
10887 p = alist_name(&ARGLIST[idx]);
10888 if (p != NULL)
10889 {
10890 if (len > 0)
10891 {
10892 /* insert a space in between names */
10893 if (retval != NULL)
10894 retval[len] = ' ';
10895 ++len;
10896 }
10897 for ( ; *p != NUL; ++p)
10898 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010899 if (*p == ' '
10900#ifndef BACKSLASH_IN_FILENAME
10901 || *p == '\\'
10902#endif
10903 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 {
10905 /* insert a backslash */
10906 if (retval != NULL)
10907 retval[len] = '\\';
10908 ++len;
10909 }
10910 if (retval != NULL)
10911 retval[len] = *p;
10912 ++len;
10913 }
10914 }
10915 }
10916
10917 /* second time: break here */
10918 if (retval != NULL)
10919 {
10920 retval[len] = NUL;
10921 break;
10922 }
10923
10924 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010925 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 if (retval == NULL)
10927 break;
10928 }
10929
10930 return retval;
10931}
10932
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933/*
10934 * Expand the <sfile> string in "arg".
10935 *
10936 * Returns an allocated string, or NULL for any error.
10937 */
10938 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010939expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940{
10941 char_u *errormsg;
10942 int len;
10943 char_u *result;
10944 char_u *newres;
10945 char_u *repl;
10946 int srclen;
10947 char_u *p;
10948
10949 result = vim_strsave(arg);
10950 if (result == NULL)
10951 return NULL;
10952
10953 for (p = result; *p; )
10954 {
10955 if (STRNCMP(p, "<sfile>", 7) != 0)
10956 ++p;
10957 else
10958 {
10959 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010960 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 if (errormsg != NULL)
10962 {
10963 if (*errormsg)
10964 emsg(errormsg);
10965 vim_free(result);
10966 return NULL;
10967 }
10968 if (repl == NULL) /* no match (cannot happen) */
10969 {
10970 p += srclen;
10971 continue;
10972 }
10973 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10974 newres = alloc(len);
10975 if (newres == NULL)
10976 {
10977 vim_free(repl);
10978 vim_free(result);
10979 return NULL;
10980 }
10981 mch_memmove(newres, result, (size_t)(p - result));
10982 STRCPY(newres + (p - result), repl);
10983 len = (int)STRLEN(newres);
10984 STRCAT(newres, p + srclen);
10985 vim_free(repl);
10986 vim_free(result);
10987 result = newres;
10988 p = newres + len; /* continue after the match */
10989 }
10990 }
10991
10992 return result;
10993}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994
10995#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010996static int ses_winsizes(FILE *fd, int restore_size,
10997 win_T *tab_firstwin);
10998static int ses_win_rec(FILE *fd, frame_T *fr);
10999static frame_T *ses_skipframe(frame_T *fr);
11000static int ses_do_frame(frame_T *fr);
11001static int ses_do_win(win_T *wp);
11002static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11003static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011004static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005
11006/*
11007 * Write openfile commands for the current buffers to an .exrc file.
11008 * Return FAIL on error, OK otherwise.
11009 */
11010 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011011makeopens(
11012 FILE *fd,
11013 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014{
11015 buf_T *buf;
11016 int only_save_windows = TRUE;
11017 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 int restore_size = TRUE;
11019 win_T *wp;
11020 char_u *sname;
11021 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011022 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011023 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011024 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011025 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011026 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011027 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028
11029 if (ssop_flags & SSOP_BUFFERS)
11030 only_save_windows = FALSE; /* Save ALL buffers */
11031
11032 /*
11033 * Begin by setting the this_session variable, and then other
11034 * sessionable variables.
11035 */
11036#ifdef FEAT_EVAL
11037 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11038 return FAIL;
11039 if (ssop_flags & SSOP_GLOBALS)
11040 if (store_session_globals(fd) == FAIL)
11041 return FAIL;
11042#endif
11043
11044 /*
11045 * Close all windows but one.
11046 */
11047 if (put_line(fd, "silent only") == FAIL)
11048 return FAIL;
11049
11050 /*
11051 * Now a :cd command to the session directory or the current directory
11052 */
11053 if (ssop_flags & SSOP_SESDIR)
11054 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011055 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11056 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 return FAIL;
11058 }
11059 else if (ssop_flags & SSOP_CURDIR)
11060 {
11061 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11062 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011063 || fputs("cd ", fd) < 0
11064 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11065 || put_eol(fd) == FAIL)
11066 {
11067 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 vim_free(sname);
11071 }
11072
11073 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011074 * If there is an empty, unnamed buffer we will wipe it out later.
11075 * Remember the buffer number.
11076 */
11077 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11078 return FAIL;
11079 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11080 return FAIL;
11081 if (put_line(fd, "endif") == FAIL)
11082 return FAIL;
11083
11084 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 * Now save the current files, current buffer first.
11086 */
11087 if (put_line(fd, "set shortmess=aoO") == FAIL)
11088 return FAIL;
11089
11090 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011091 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 {
11093 if (!(only_save_windows && buf->b_nwindows == 0)
11094 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011095#ifdef FEAT_TERMINAL
11096 /* skip terminal buffers: finished ones are not useful, others
11097 * will be resurrected and result in a new buffer */
11098 && !bt_terminal(buf)
11099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 && buf->b_fname != NULL
11101 && buf->b_p_bl)
11102 {
11103 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11104 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011105 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 return FAIL;
11107 }
11108 }
11109
11110 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011111 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11113 return FAIL;
11114
11115 if (ssop_flags & SSOP_RESIZE)
11116 {
11117 /* Note: after the restore we still check it worked!*/
11118 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11119 || put_eol(fd) == FAIL)
11120 return FAIL;
11121 }
11122
11123#ifdef FEAT_GUI
11124 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11125 {
11126 int x, y;
11127
11128 if (gui_mch_get_winpos(&x, &y) == OK)
11129 {
11130 /* Note: after the restore we still check it worked!*/
11131 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11132 return FAIL;
11133 }
11134 }
11135#endif
11136
11137 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011138 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11139 * will be displayed when creating the next tab. That resizes the windows
11140 * in the first tab, which may cause problems. Set 'showtabline' to 2
11141 * temporarily to avoid that.
11142 */
11143 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11144 {
11145 if (put_line(fd, "set stal=2") == FAIL)
11146 return FAIL;
11147 restore_stal = TRUE;
11148 }
11149
11150 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011151 * May repeat putting Windows for each tab, when "tabpages" is in
11152 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011153 * Don't use goto_tabpage(), it may change directory and trigger
11154 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011156 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011157 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011158 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011160 int need_tabnew = FALSE;
11161 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011162
Bram Moolenaar18144c82006-04-12 21:52:12 +000011163 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011165 tabpage_T *tp = find_tabpage(tabnr);
11166
11167 if (tp == NULL)
11168 break; /* done all tab pages */
11169 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011170 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011171 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011172 tab_topframe = topframe;
11173 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011174 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011175 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011176 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011177 tab_topframe = tp->tp_topframe;
11178 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011179 if (tabnr > 1)
11180 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182
Bram Moolenaar18144c82006-04-12 21:52:12 +000011183 /*
11184 * Before creating the window layout, try loading one file. If this
11185 * is aborted we don't end up with a number of useless windows.
11186 * This may have side effects! (e.g., compressed or network file).
11187 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011188 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011189 {
11190 if (ses_do_win(wp)
11191 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011192 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011193#ifdef FEAT_QUICKFIX
11194 && !bt_nofile(wp->w_buffer)
11195#endif
11196 )
11197 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011198 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011199 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11200 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011201 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011202 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011203 if (!wp->w_arg_idx_invalid)
11204 edited_win = wp;
11205 break;
11206 }
11207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011209 /* If no file got edited create an empty tab page. */
11210 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11211 return FAIL;
11212
Bram Moolenaar18144c82006-04-12 21:52:12 +000011213 /*
11214 * Save current window layout.
11215 */
11216 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011218 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011220 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11221 return FAIL;
11222 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11223 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224
Bram Moolenaar18144c82006-04-12 21:52:12 +000011225 /*
11226 * Check if window sizes can be restored (no windows omitted).
11227 * Remember the window number of the current window after restoring.
11228 */
11229 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011230 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011231 {
11232 if (ses_do_win(wp))
11233 ++nr;
11234 else
11235 restore_size = FALSE;
11236 if (curwin == wp)
11237 cnr = nr;
11238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239
Bram Moolenaar18144c82006-04-12 21:52:12 +000011240 /* Go to the first window. */
11241 if (put_line(fd, "wincmd t") == FAIL)
11242 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011243
Bram Moolenaar18144c82006-04-12 21:52:12 +000011244 /*
11245 * If more than one window, see if sizes can be restored.
11246 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11247 * resized when moving between windows.
11248 * Do this before restoring the view, so that the topline and the
11249 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011250 * winminheight and winminwidth need to be set to avoid an error if the
11251 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011252 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011253 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011254 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011255 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011256 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257
Bram Moolenaar18144c82006-04-12 21:52:12 +000011258 /*
11259 * Restore the view of the window (options, file, cursor, etc.).
11260 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011261 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011262 {
11263 if (!ses_do_win(wp))
11264 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011265 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11266 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011267 return FAIL;
11268 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11269 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011270 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011271 }
11272
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011273 /* The argument index in the first tab page is zero, need to set it in
11274 * each window. For further tab pages it's the window where we do
11275 * "tabedit". */
11276 cur_arg_idx = next_arg_idx;
11277
Bram Moolenaar18144c82006-04-12 21:52:12 +000011278 /*
11279 * Restore cursor to the current window if it's not the first one.
11280 */
11281 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11282 || put_eol(fd) == FAIL))
11283 return FAIL;
11284
11285 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011286 * Restore window sizes again after jumping around in windows, because
11287 * the current window has a minimum size while others may not.
11288 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011289 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011290 return FAIL;
11291
Bram Moolenaar18144c82006-04-12 21:52:12 +000011292 /* Don't continue in another tab page when doing only the current one
11293 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011294 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011295 break;
11296 }
11297
11298 if (ssop_flags & SSOP_TABPAGES)
11299 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011300 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11301 || put_eol(fd) == FAIL)
11302 return FAIL;
11303 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011304 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11305 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011306
Bram Moolenaar9c102382006-05-03 21:26:49 +000011307 /*
11308 * Wipe out an empty unnamed buffer we started in.
11309 */
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011310 if (put_line(fd, "if exists('s:wipebuf') && s:wipebuf != bufnr('%')")
11311 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011312 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011313 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011314 return FAIL;
11315 if (put_line(fd, "endif") == FAIL)
11316 return FAIL;
11317 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11318 return FAIL;
11319
11320 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11321 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11322 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11323 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011324 /* Re-apply 'winminheight' and 'winminwidth'. */
11325 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11326 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11327 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328
11329 /*
11330 * Lastly, execute the x.vim file if it exists.
11331 */
11332 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11333 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011334 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335 || put_line(fd, "endif") == FAIL)
11336 return FAIL;
11337
11338 return OK;
11339}
11340
11341 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011342ses_winsizes(
11343 FILE *fd,
11344 int restore_size,
11345 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346{
11347 int n = 0;
11348 win_T *wp;
11349
11350 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11351 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011352 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 {
11354 if (!ses_do_win(wp))
11355 continue;
11356 ++n;
11357
11358 /* restore height when not full height */
11359 if (wp->w_height + wp->w_status_height < topframe->fr_height
11360 && (fprintf(fd,
11361 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11362 n, (long)wp->w_height, Rows / 2, Rows) < 0
11363 || put_eol(fd) == FAIL))
11364 return FAIL;
11365
11366 /* restore width when not full width */
11367 if (wp->w_width < Columns && (fprintf(fd,
11368 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11369 n, (long)wp->w_width, Columns / 2, Columns) < 0
11370 || put_eol(fd) == FAIL))
11371 return FAIL;
11372 }
11373 }
11374 else
11375 {
11376 /* Just equalise window sizes */
11377 if (put_line(fd, "wincmd =") == FAIL)
11378 return FAIL;
11379 }
11380 return OK;
11381}
11382
11383/*
11384 * Write commands to "fd" to recursively create windows for frame "fr",
11385 * horizontally and vertically split.
11386 * After the commands the last window in the frame is the current window.
11387 * Returns FAIL when writing the commands to "fd" fails.
11388 */
11389 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011390ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391{
11392 frame_T *frc;
11393 int count = 0;
11394
11395 if (fr->fr_layout != FR_LEAF)
11396 {
11397 /* Find first frame that's not skipped and then create a window for
11398 * each following one (first frame is already there). */
11399 frc = ses_skipframe(fr->fr_child);
11400 if (frc != NULL)
11401 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11402 {
11403 /* Make window as big as possible so that we have lots of room
11404 * to split. */
11405 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11406 || put_line(fd, fr->fr_layout == FR_COL
11407 ? "split" : "vsplit") == FAIL)
11408 return FAIL;
11409 ++count;
11410 }
11411
11412 /* Go back to the first window. */
11413 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11414 ? "%dwincmd k" : "%dwincmd h", count) < 0
11415 || put_eol(fd) == FAIL))
11416 return FAIL;
11417
11418 /* Recursively create frames/windows in each window of this column or
11419 * row. */
11420 frc = ses_skipframe(fr->fr_child);
11421 while (frc != NULL)
11422 {
11423 ses_win_rec(fd, frc);
11424 frc = ses_skipframe(frc->fr_next);
11425 /* Go to next window. */
11426 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11427 return FAIL;
11428 }
11429 }
11430 return OK;
11431}
11432
11433/*
11434 * Skip frames that don't contain windows we want to save in the Session.
11435 * Returns NULL when there none.
11436 */
11437 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011438ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439{
11440 frame_T *frc;
11441
11442 for (frc = fr; frc != NULL; frc = frc->fr_next)
11443 if (ses_do_frame(frc))
11444 break;
11445 return frc;
11446}
11447
11448/*
11449 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11450 * the Session.
11451 */
11452 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011453ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454{
11455 frame_T *frc;
11456
11457 if (fr->fr_layout == FR_LEAF)
11458 return ses_do_win(fr->fr_win);
11459 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11460 if (ses_do_frame(frc))
11461 return TRUE;
11462 return FALSE;
11463}
11464
11465/*
11466 * Return non-zero if window "wp" is to be stored in the Session.
11467 */
11468 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011469ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011471#ifdef FEAT_TERMINAL
11472 if (bt_terminal(wp->w_buffer))
11473 return !term_is_finished(wp->w_buffer)
11474 && (ssop_flags & SSOP_TERMINAL)
11475 && term_should_restore(wp->w_buffer);
11476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 if (wp->w_buffer->b_fname == NULL
11478#ifdef FEAT_QUICKFIX
11479 /* When 'buftype' is "nofile" can't restore the window contents. */
11480 || bt_nofile(wp->w_buffer)
11481#endif
11482 )
11483 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011484 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 return (ssop_flags & SSOP_HELP);
11486 return TRUE;
11487}
11488
11489/*
11490 * Write commands to "fd" to restore the view of a window.
11491 * Caller must make sure 'scrolloff' is zero.
11492 */
11493 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011494put_view(
11495 FILE *fd,
11496 win_T *wp,
11497 int add_edit, /* add ":edit" command to view */
11498 unsigned *flagp, /* vop_flags or ssop_flags */
11499 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011500 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501{
11502 win_T *save_curwin;
11503 int f;
11504 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011505 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506
11507 /* Always restore cursor position for ":mksession". For ":mkview" only
11508 * when 'viewoptions' contains "cursor". */
11509 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11510
11511 /*
11512 * Local argument list.
11513 */
11514 if (wp->w_alist == &global_alist)
11515 {
11516 if (put_line(fd, "argglobal") == FAIL)
11517 return FAIL;
11518 }
11519 else
11520 {
11521 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11522 flagp == &vop_flags
11523 || !(*flagp & SSOP_CURDIR)
11524 || wp->w_localdir != NULL, flagp) == FAIL)
11525 return FAIL;
11526 }
11527
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011528 /* Only when part of a session: restore the argument index. Some
11529 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011530 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011531 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011533 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534 || put_eol(fd) == FAIL)
11535 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011536 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 }
11538
11539 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011540 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011542# ifdef FEAT_TERMINAL
11543 if (bt_terminal(wp->w_buffer))
11544 {
11545 if (term_write_session(fd, wp) == FAIL)
11546 return FAIL;
11547 }
11548 else
11549# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 /*
11551 * Load the file.
11552 */
11553 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011554# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011556# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 )
11558 {
11559 /*
11560 * Editing a file in this buffer: use ":edit file".
11561 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011562 *
11563 * Note, if a buffer for that file already exists, use :badd to
11564 * edit that buffer, to not lose folding information (:edit resets
11565 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011567 if (fputs("if bufexists('", fd) < 0
11568 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11569 || fputs("') | buffer ", fd) < 0
11570 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11571 || fputs(" | else | edit ", fd) < 0
11572 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11573 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011574 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575 return FAIL;
11576 }
11577 else
11578 {
11579 /* No file in this buffer, just make it empty. */
11580 if (put_line(fd, "enew") == FAIL)
11581 return FAIL;
11582#ifdef FEAT_QUICKFIX
11583 if (wp->w_buffer->b_ffname != NULL)
11584 {
11585 /* The buffer does have a name, but it's not a file name. */
11586 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011587 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 return FAIL;
11589 }
11590#endif
11591 do_cursor = FALSE;
11592 }
11593 }
11594
11595 /*
11596 * Local mappings and abbreviations.
11597 */
11598 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11599 && makemap(fd, wp->w_buffer) == FAIL)
11600 return FAIL;
11601
11602 /*
11603 * Local options. Need to go to the window temporarily.
11604 * Store only local values when using ":mkview" and when ":mksession" is
11605 * used and 'sessionoptions' doesn't include "options".
11606 * Some folding options are always stored when "folds" is included,
11607 * otherwise the folds would not be restored correctly.
11608 */
11609 save_curwin = curwin;
11610 curwin = wp;
11611 curbuf = curwin->w_buffer;
11612 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11613 f = makeset(fd, OPT_LOCAL,
11614 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11615#ifdef FEAT_FOLDING
11616 else if (*flagp & SSOP_FOLDS)
11617 f = makefoldset(fd);
11618#endif
11619 else
11620 f = OK;
11621 curwin = save_curwin;
11622 curbuf = curwin->w_buffer;
11623 if (f == FAIL)
11624 return FAIL;
11625
11626#ifdef FEAT_FOLDING
11627 /*
11628 * Save Folds when 'buftype' is empty and for help files.
11629 */
11630 if ((*flagp & SSOP_FOLDS)
11631 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011632 && (*wp->w_buffer->b_p_bt == NUL || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633 {
11634 if (put_folds(fd, wp) == FAIL)
11635 return FAIL;
11636 }
11637#endif
11638
11639 /*
11640 * Set the cursor after creating folds, since that moves the cursor.
11641 */
11642 if (do_cursor)
11643 {
11644
11645 /* Restore the cursor line in the file and relatively in the
11646 * window. Don't use "G", it changes the jumplist. */
11647 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11648 (long)wp->w_cursor.lnum,
11649 (long)(wp->w_cursor.lnum - wp->w_topline),
11650 (long)wp->w_height / 2, (long)wp->w_height) < 0
11651 || put_eol(fd) == FAIL
11652 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11653 || put_line(fd, "exe s:l") == FAIL
11654 || put_line(fd, "normal! zt") == FAIL
11655 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11656 || put_eol(fd) == FAIL)
11657 return FAIL;
11658 /* Restore the cursor column and left offset when not wrapping. */
11659 if (wp->w_cursor.col == 0)
11660 {
11661 if (put_line(fd, "normal! 0") == FAIL)
11662 return FAIL;
11663 }
11664 else
11665 {
11666 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11667 {
11668 if (fprintf(fd,
11669 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011670 (long)wp->w_virtcol + 1,
11671 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672 (long)wp->w_width / 2, (long)wp->w_width) < 0
11673 || put_eol(fd) == FAIL
11674 || put_line(fd, "if s:c > 0") == FAIL
11675 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011676 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11677 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678 || put_eol(fd) == FAIL
11679 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011680 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 || put_eol(fd) == FAIL
11682 || put_line(fd, "endif") == FAIL)
11683 return FAIL;
11684 }
11685 else
11686 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011687 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 || put_eol(fd) == FAIL)
11689 return FAIL;
11690 }
11691 }
11692 }
11693
11694 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011695 * Local directory, if the current flag is not view options or the "curdir"
11696 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011698 if (wp->w_localdir != NULL
11699 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 {
11701 if (fputs("lcd ", fd) < 0
11702 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11703 || put_eol(fd) == FAIL)
11704 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011705 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706 }
11707
11708 return OK;
11709}
11710
11711/*
11712 * Write an argument list to the session file.
11713 * Returns FAIL if writing fails.
11714 */
11715 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011716ses_arglist(
11717 FILE *fd,
11718 char *cmd,
11719 garray_T *gap,
11720 int fullname, /* TRUE: use full path name */
11721 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722{
11723 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011724 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725 char_u *s;
11726
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011727 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11728 return FAIL;
11729 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730 return FAIL;
11731 for (i = 0; i < gap->ga_len; ++i)
11732 {
11733 /* NULL file names are skipped (only happens when out of memory). */
11734 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11735 if (s != NULL)
11736 {
11737 if (fullname)
11738 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011739 buf = alloc(MAXPATHL);
11740 if (buf != NULL)
11741 {
11742 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11743 s = buf;
11744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011746 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011747 || ses_put_fname(fd, s, flagp) == FAIL
11748 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011749 {
11750 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011752 }
11753 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 }
11755 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011756 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757}
11758
11759/*
11760 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011761 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 * Returns FAIL if writing fails.
11763 */
11764 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011765ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766{
11767 char_u *name;
11768
11769 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011770 * the session file will be sourced.
11771 * Don't do this for ":mkview", we don't know the current directory.
11772 * Don't do this after ":lcd", we don't keep track of what the current
11773 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 if (buf->b_sfname != NULL
11775 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011776 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011777#ifdef FEAT_AUTOCHDIR
11778 && !p_acd
11779#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011780 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781 name = buf->b_sfname;
11782 else
11783 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011784 if (ses_put_fname(fd, name, flagp) == FAIL
11785 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786 return FAIL;
11787 return OK;
11788}
11789
11790/*
11791 * Write a file name to the session file.
11792 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11793 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011794 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 */
11796 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011797ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798{
11799 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011800 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802
11803 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011804 if (sname == NULL)
11805 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011807 if (*flagp & SSOP_SLASH)
11808 {
11809 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011810 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011811 if (*p == '\\')
11812 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011814
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011815 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011816 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011818 if (p == NULL)
11819 return FAIL;
11820
11821 /* write the result */
11822 if (fputs((char *)p, fd) < 0)
11823 retval = FAIL;
11824
11825 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826 return retval;
11827}
11828
11829/*
11830 * ":loadview [nr]"
11831 */
11832 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011833ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834{
11835 char_u *fname;
11836
11837 fname = get_view_file(*eap->arg);
11838 if (fname != NULL)
11839 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011840 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 vim_free(fname);
11842 }
11843}
11844
11845/*
11846 * Get the name of the view file for the current buffer.
11847 */
11848 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011849get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850{
11851 int len = 0;
11852 char_u *p, *s;
11853 char_u *retval;
11854 char_u *sname;
11855
11856 if (curbuf->b_ffname == NULL)
11857 {
11858 EMSG(_(e_noname));
11859 return NULL;
11860 }
11861 sname = home_replace_save(NULL, curbuf->b_ffname);
11862 if (sname == NULL)
11863 return NULL;
11864
11865 /*
11866 * We want a file name without separators, because we're not going to make
11867 * a directory.
11868 * "normal" path separator -> "=+"
11869 * "=" -> "=="
11870 * ":" path separator -> "=-"
11871 */
11872 for (p = sname; *p; ++p)
11873 if (*p == '=' || vim_ispathsep(*p))
11874 ++len;
11875 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11876 if (retval != NULL)
11877 {
11878 STRCPY(retval, p_vdir);
11879 add_pathsep(retval);
11880 s = retval + STRLEN(retval);
11881 for (p = sname; *p; ++p)
11882 {
11883 if (*p == '=')
11884 {
11885 *s++ = '=';
11886 *s++ = '=';
11887 }
11888 else if (vim_ispathsep(*p))
11889 {
11890 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011891#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 if (*p == ':')
11893 *s++ = '-';
11894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011896 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 }
11898 else
11899 *s++ = *p;
11900 }
11901 *s++ = '=';
11902 *s++ = c;
11903 STRCPY(s, ".vim");
11904 }
11905
11906 vim_free(sname);
11907 return retval;
11908}
11909
11910#endif /* FEAT_SESSION */
11911
11912/*
11913 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11914 * Return FAIL for a write error.
11915 */
11916 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011917put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918{
11919 if (
11920#ifdef USE_CRNL
11921 (
11922# ifdef MKSESSION_NL
11923 !mksession_nl &&
11924# endif
11925 (putc('\r', fd) < 0)) ||
11926#endif
11927 (putc('\n', fd) < 0))
11928 return FAIL;
11929 return OK;
11930}
11931
11932/*
11933 * Write a line to "fd".
11934 * Return FAIL for a write error.
11935 */
11936 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011937put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938{
11939 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11940 return FAIL;
11941 return OK;
11942}
11943
11944#ifdef FEAT_VIMINFO
11945/*
11946 * ":rviminfo" and ":wviminfo".
11947 */
11948 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011949ex_viminfo(
11950 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951{
11952 char_u *save_viminfo;
11953
11954 save_viminfo = p_viminfo;
11955 if (*p_viminfo == NUL)
11956 p_viminfo = (char_u *)"'100";
11957 if (eap->cmdidx == CMD_rviminfo)
11958 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011959 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11960 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961 EMSG(_("E195: Cannot open viminfo file for reading"));
11962 }
11963 else
11964 write_viminfo(eap->arg, eap->forceit);
11965 p_viminfo = save_viminfo;
11966}
11967#endif
11968
11969#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011970/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011971 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011972 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011975dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977 if (fname == NULL)
11978 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011979 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980}
11981#endif
11982
11983/*
11984 * ":behave {mswin,xterm}"
11985 */
11986 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011987ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988{
11989 if (STRCMP(eap->arg, "mswin") == 0)
11990 {
11991 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11992 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11993 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11994 set_option_value((char_u *)"keymodel", 0L,
11995 (char_u *)"startsel,stopsel", 0);
11996 }
11997 else if (STRCMP(eap->arg, "xterm") == 0)
11998 {
11999 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12000 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12001 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12002 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12003 }
12004 else
12005 EMSG2(_(e_invarg2), eap->arg);
12006}
12007
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012008#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12009/*
12010 * Function given to ExpandGeneric() to obtain the possible arguments of the
12011 * ":behave {mswin,xterm}" command.
12012 */
12013 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012014get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012015{
12016 if (idx == 0)
12017 return (char_u *)"mswin";
12018 if (idx == 1)
12019 return (char_u *)"xterm";
12020 return NULL;
12021}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012022
12023/*
12024 * Function given to ExpandGeneric() to obtain the possible arguments of the
12025 * ":messages {clear}" command.
12026 */
12027 char_u *
12028get_messages_arg(expand_T *xp UNUSED, int idx)
12029{
12030 if (idx == 0)
12031 return (char_u *)"clear";
12032 return NULL;
12033}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012034#endif
12035
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012036 char_u *
12037get_mapclear_arg(expand_T *xp UNUSED, int idx)
12038{
12039 if (idx == 0)
12040 return (char_u *)"<buffer>";
12041 return NULL;
12042}
12043
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044static int filetype_detect = FALSE;
12045static int filetype_plugin = FALSE;
12046static int filetype_indent = FALSE;
12047
12048/*
12049 * ":filetype [plugin] [indent] {on,off,detect}"
12050 * on: Load the filetype.vim file to install autocommands for file types.
12051 * off: Load the ftoff.vim file to remove all autocommands for file types.
12052 * plugin on: load filetype.vim and ftplugin.vim
12053 * plugin off: load ftplugof.vim
12054 * indent on: load filetype.vim and indent.vim
12055 * indent off: load indoff.vim
12056 */
12057 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012058ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059{
12060 char_u *arg = eap->arg;
12061 int plugin = FALSE;
12062 int indent = FALSE;
12063
12064 if (*eap->arg == NUL)
12065 {
12066 /* Print current status. */
12067 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12068 filetype_detect ? "ON" : "OFF",
12069 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12070 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12071 return;
12072 }
12073
12074 /* Accept "plugin" and "indent" in any order. */
12075 for (;;)
12076 {
12077 if (STRNCMP(arg, "plugin", 6) == 0)
12078 {
12079 plugin = TRUE;
12080 arg = skipwhite(arg + 6);
12081 continue;
12082 }
12083 if (STRNCMP(arg, "indent", 6) == 0)
12084 {
12085 indent = TRUE;
12086 arg = skipwhite(arg + 6);
12087 continue;
12088 }
12089 break;
12090 }
12091 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12092 {
12093 if (*arg == 'o' || !filetype_detect)
12094 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012095 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096 filetype_detect = TRUE;
12097 if (plugin)
12098 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012099 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 filetype_plugin = TRUE;
12101 }
12102 if (indent)
12103 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012104 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105 filetype_indent = TRUE;
12106 }
12107 }
12108 if (*arg == 'd')
12109 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012110 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012111 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112 }
12113 }
12114 else if (STRCMP(arg, "off") == 0)
12115 {
12116 if (plugin || indent)
12117 {
12118 if (plugin)
12119 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012120 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121 filetype_plugin = FALSE;
12122 }
12123 if (indent)
12124 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012125 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126 filetype_indent = FALSE;
12127 }
12128 }
12129 else
12130 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012131 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 filetype_detect = FALSE;
12133 }
12134 }
12135 else
12136 EMSG2(_(e_invarg2), arg);
12137}
12138
12139/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012140 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141 */
12142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012143ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144{
12145 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012146 {
12147 char_u *arg = eap->arg;
12148
12149 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12150 arg += 9;
12151
12152 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12153 if (arg != eap->arg)
12154 did_filetype = FALSE;
12155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012159ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160{
12161#ifdef FEAT_DIGRAPHS
12162 if (*eap->arg != NUL)
12163 putdigraph(eap->arg);
12164 else
12165 listdigraphs();
12166#else
12167 EMSG(_("E196: No digraphs in this version"));
12168#endif
12169}
12170
12171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012172ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173{
12174 int flags = 0;
12175
12176 if (eap->cmdidx == CMD_setlocal)
12177 flags = OPT_LOCAL;
12178 else if (eap->cmdidx == CMD_setglobal)
12179 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012180#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181 if (cmdmod.browse && flags == 0)
12182 ex_options(eap);
12183 else
12184#endif
12185 (void)do_set(eap->arg, flags);
12186}
12187
12188#ifdef FEAT_SEARCH_EXTRA
12189/*
12190 * ":nohlsearch"
12191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012193ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012195 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012196 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197}
12198
12199/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012200 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201 * Sets nextcmd to the start of the next command, if any. Also called when
12202 * skipping commands to find the next command.
12203 */
12204 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012205ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206{
12207 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012208 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209 char_u *end;
12210 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012211 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012212
12213 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012214 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012215 else
12216 {
12217 EMSG(e_invcmd);
12218 return;
12219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220
12221 /* First clear any old pattern. */
12222 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012223 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224
12225 if (ends_excmd(*eap->arg))
12226 end = eap->arg;
12227 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012228 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 end = eap->arg + 4;
12230 else
12231 {
12232 p = skiptowhite(eap->arg);
12233 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012234 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235 p = skipwhite(p);
12236 if (*p == NUL)
12237 {
12238 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012239 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 EMSG2(_(e_invarg2), eap->arg);
12241 return;
12242 }
12243 end = skip_regexp(p + 1, *p, TRUE, NULL);
12244 if (!eap->skip)
12245 {
12246 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12247 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012248 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249 eap->errmsg = e_trailing;
12250 return;
12251 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012252 if (*end != *p)
12253 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012254 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012255 EMSG2(_(e_invarg2), p);
12256 return;
12257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258
12259 c = *end;
12260 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012261 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012262 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012263 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264 }
12265 }
12266 eap->nextcmd = find_nextcmd(end);
12267}
12268#endif
12269
12270#ifdef FEAT_CRYPT
12271/*
12272 * ":X": Get crypt key
12273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012275ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012277 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012278 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279}
12280#endif
12281
12282#ifdef FEAT_FOLDING
12283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012284ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285{
12286 if (foldManualAllowed(TRUE))
12287 foldCreate(eap->line1, eap->line2);
12288}
12289
12290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012291ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292{
12293 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12294 eap->forceit, FALSE);
12295}
12296
12297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012298ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299{
12300 linenr_T lnum;
12301
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012302#ifdef FEAT_CLIPBOARD
12303 start_global_changes();
12304#endif
12305
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306 /* First set the marks for all lines closed/open. */
12307 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12308 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12309 ml_setmarked(lnum);
12310
12311 /* Execute the command on the marked lines. */
12312 global_exe(eap->arg);
12313 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012314#ifdef FEAT_CLIPBOARD
12315 end_global_changes();
12316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317}
12318#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012319
12320# if defined(FEAT_TIMERS) || defined(PROTO)
12321 int
12322get_pressedreturn(void)
12323{
12324 return ex_pressedreturn;
12325}
12326
12327 void
12328set_pressedreturn(int val)
12329{
12330 ex_pressedreturn = val;
12331}
12332#endif