blob: 51aa068885ebbd873a53c3f89a56dafcf4adc047 [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 Moolenaar071d4272004-06-13 20:20:40 +00001722
1723 vim_memset(&ea, 0, sizeof(ea));
1724 ea.line1 = 1;
1725 ea.line2 = 1;
1726#ifdef FEAT_EVAL
1727 ++ex_nesting_level;
1728#endif
1729
Bram Moolenaar21691f82012-12-05 19:13:18 +01001730 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 if (quitmore
1732#ifdef FEAT_EVAL
1733 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001734 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001735#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001736 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001737 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 --quitmore;
1739
1740 /*
1741 * Reset browse, confirm, etc.. They are restored when returning, for
1742 * recursive calls.
1743 */
1744 save_cmdmod = cmdmod;
1745 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1746
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001747 /* "#!anything" is handled like a comment. */
1748 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1749 goto doend;
1750
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 /*
1752 * Repeat until no more command modifiers are found.
1753 */
1754 ea.cmd = *cmdlinep;
1755 for (;;)
1756 {
1757/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001758 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 */
1760 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1761 ++ea.cmd;
1762
1763 /* in ex mode, an empty line works like :+ */
1764 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001765 && (getline_equal(fgetline, cookie, getexmodeline)
1766 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001767 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 {
1769 ea.cmd = (char_u *)"+";
1770 ex_pressedreturn = TRUE;
1771 }
1772
1773 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001774 if (*ea.cmd == '"')
1775 goto doend;
1776 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001777 {
1778 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001783 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001785 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 switch (*p)
1787 {
1788 /* When adding an entry, also modify cmd_exists(). */
1789 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1790 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 continue;
1793
1794 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 continue;
1798 }
1799 if (checkforcmd(&ea.cmd, "browse", 3))
1800 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001801#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 cmdmod.browse = TRUE;
1803#endif
1804 continue;
1805 }
1806 if (!checkforcmd(&ea.cmd, "botright", 2))
1807 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 cmdmod.split |= WSP_BOT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 continue;
1810
1811 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1812 break;
1813#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1814 cmdmod.confirm = TRUE;
1815#endif
1816 continue;
1817
1818 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1819 {
1820 cmdmod.keepmarks = TRUE;
1821 continue;
1822 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001823 if (checkforcmd(&ea.cmd, "keepalt", 5))
1824 {
1825 cmdmod.keepalt = TRUE;
1826 continue;
1827 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001828 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1829 {
1830 cmdmod.keeppatterns = TRUE;
1831 continue;
1832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1834 break;
1835 cmdmod.keepjumps = TRUE;
1836 continue;
1837
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001838 case 'f': /* only accept ":filter {pat} cmd" */
1839 {
1840 char_u *reg_pat;
1841
1842 if (!checkforcmd(&p, "filter", 4)
1843 || *p == NUL || ends_excmd(*p))
1844 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001845 if (*p == '!')
1846 {
1847 cmdmod.filter_force = TRUE;
1848 p = skipwhite(p + 1);
1849 if (*p == NUL || ends_excmd(*p))
1850 break;
1851 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001852 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1853 if (p == NULL || *p == NUL)
1854 break;
1855 cmdmod.filter_regmatch.regprog =
1856 vim_regcomp(reg_pat, RE_MAGIC);
1857 if (cmdmod.filter_regmatch.regprog == NULL)
1858 break;
1859 ea.cmd = p;
1860 continue;
1861 }
1862
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 /* ":hide" and ":hide | cmd" are not modifiers */
1864 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1865 || *p == NUL || ends_excmd(*p))
1866 break;
1867 ea.cmd = p;
1868 cmdmod.hide = TRUE;
1869 continue;
1870
1871 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1872 {
1873 cmdmod.lockmarks = TRUE;
1874 continue;
1875 }
1876
1877 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1878 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 continue;
1881
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001882 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001883 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001884 if (cmdmod.save_ei == NULL)
1885 {
1886 /* Set 'eventignore' to "all". Restore the
1887 * existing option value later. */
1888 cmdmod.save_ei = vim_strsave(p_ei);
1889 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001890 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001891 }
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001892 continue;
1893 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001894 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001895 break;
1896 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001897 continue;
1898
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1900 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 continue;
1903
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001904 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1905 {
1906#ifdef HAVE_SANDBOX
1907 if (!did_sandbox)
1908 ++sandbox;
1909 did_sandbox = TRUE;
1910#endif
1911 continue;
1912 }
1913 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001915 if (save_msg_silent == -1)
1916 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 ++msg_silent;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001918 if (*ea.cmd == '!' && !VIM_ISWHITE(ea.cmd[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 {
1920 /* ":silent!", but not "silent !cmd" */
1921 ea.cmd = skipwhite(ea.cmd + 1);
1922 ++emsg_silent;
1923 ++did_esilent;
1924 }
1925 continue;
1926
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001927 case 't': if (checkforcmd(&p, "tab", 3))
1928 {
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001929 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01001930 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001931 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001932 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001933 else
1934 {
1935 if (tabnr < 0 || tabnr > LAST_TAB_NR)
1936 {
1937 errormsg = (char_u *)_(e_invrange);
1938 goto doend;
1939 }
1940 cmdmod.tab = tabnr + 1;
1941 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001942 ea.cmd = p;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001943 continue;
1944 }
1945 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 cmdmod.split |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 continue;
1949
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001950 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1951 break;
1952 if (save_msg_silent == -1)
1953 save_msg_silent = msg_silent;
1954 msg_silent = 0;
1955 continue;
1956
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 cmdmod.split |= WSP_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 continue;
1961 }
1962 if (!checkforcmd(&p, "verbose", 4))
1963 break;
1964 if (verbose_save < 0)
1965 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001966 if (vim_isdigit(*ea.cmd))
1967 p_verbose = atoi((char *)ea.cmd);
1968 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 p_verbose = 1;
1970 ea.cmd = p;
1971 continue;
1972 }
1973 break;
1974 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001975 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976
1977#ifdef FEAT_EVAL
1978 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1979 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1980#else
1981 ea.skip = (if_level > 0);
1982#endif
1983
1984#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001985# ifdef FEAT_PROFILE
1986 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001987 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001988 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001989 if (getline_equal(fgetline, cookie, get_func_line))
1990 func_line_exec(getline_cookie(fgetline, cookie));
1991 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001992 script_line_exec();
1993 }
1994#endif
1995
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 /* May go to debug mode. If this happens and the ">quit" debug command is
1997 * used, throw an interrupt exception and skip the next command. */
1998 dbg_check_breakpoint(&ea);
1999 if (!ea.skip && got_int)
2000 {
2001 ea.skip = TRUE;
2002 (void)do_intthrow(cstack);
2003 }
2004#endif
2005
2006/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002007 * 3. Skip over the range to find the command. Let "p" point to after it.
2008 *
2009 * We need the command to know what kind of range it uses.
2010 */
2011 cmd = ea.cmd;
2012 ea.cmd = skip_range(ea.cmd, NULL);
2013 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2014 ea.cmd = skipwhite(ea.cmd + 1);
2015 p = find_command(&ea, NULL);
2016
2017/*
2018 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 *
2020 * where 'addr' is:
2021 *
2022 * % (entire file)
2023 * $ [+-NUM]
2024 * 'x [+-NUM] (where x denotes a currently defined mark)
2025 * . [+-NUM]
2026 * [+-NUM]..
2027 * NUM
2028 *
2029 * The ea.cmd pointer is updated to point to the first character following the
2030 * range spec. If an initial address is found, but no second, the upper bound
2031 * is equal to the lower.
2032 */
2033
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002034 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002035 if (!IS_USER_CMDIDX(ea.cmdidx))
2036 {
2037 if (ea.cmdidx != CMD_SIZE)
2038 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2039 else
2040 ea.addr_type = ADDR_LINES;
2041
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002042 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002043 if (ea.cmdidx == CMD_wincmd && p != NULL)
2044 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002045 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002046
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002047 ea.cmd = cmd;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002048 if (parse_cmd_address(&ea, &errormsg) == FAIL)
2049 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002052 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 */
2054
2055 /*
2056 * Skip ':' and any white space
2057 */
2058 ea.cmd = skipwhite(ea.cmd);
2059 while (*ea.cmd == ':')
2060 ea.cmd = skipwhite(ea.cmd + 1);
2061
2062 /*
2063 * If we got a line, but no command, then go to the line.
2064 * If we find a '|' or '\n' we set ea.nextcmd.
2065 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002066 if (*ea.cmd == NUL || *ea.cmd == '"'
2067 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 {
2069 /*
2070 * strange vi behaviour:
2071 * ":3" jumps to line 3
2072 * ":3|..." prints line 3
2073 * ":|" prints current line
2074 */
2075 if (ea.skip) /* skip this if inside :if */
2076 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002077 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
2079 ea.cmdidx = CMD_print;
2080 ea.argt = RANGE+COUNT+TRLBAR;
2081 if ((errormsg = invalid_range(&ea)) == NULL)
2082 {
2083 correct_range(&ea);
2084 ex_print(&ea);
2085 }
2086 }
2087 else if (ea.addr_count != 0)
2088 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002089 if (ea.line2 > curbuf->b_ml.ml_line_count)
2090 {
2091 /* With '-' in 'cpoptions' a line number past the file is an
2092 * error, otherwise put it at the end of the file. */
2093 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2094 ea.line2 = -1;
2095 else
2096 ea.line2 = curbuf->b_ml.ml_line_count;
2097 }
2098
2099 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002100 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 else
2102 {
2103 if (ea.line2 == 0)
2104 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 else
2106 curwin->w_cursor.lnum = ea.line2;
2107 beginline(BL_SOL | BL_FIX);
2108 }
2109 }
2110 goto doend;
2111 }
2112
Bram Moolenaard5005162014-08-22 23:05:54 +02002113 /* If this looks like an undefined user command and there are CmdUndefined
2114 * autocommands defined, trigger the matching autocommands. */
2115 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2116 && ASCII_ISUPPER(*ea.cmd)
2117 && has_cmdundefined())
2118 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002119 int ret;
2120
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002121 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002122 while (ASCII_ISALNUM(*p))
2123 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002124 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002125 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2126 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002127 /* If the autocommands did something and didn't cause an error, try
2128 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002129 p = (ret
2130#ifdef FEAT_EVAL
2131 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002132#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002133 ) ? find_command(&ea, NULL) : ea.cmd;
2134 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002135
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136#ifdef FEAT_USR_CMDS
2137 if (p == NULL)
2138 {
2139 if (!ea.skip)
2140 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2141 goto doend;
2142 }
2143 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002144 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2145 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
2147 errormsg = uc_fun_cmd();
2148 goto doend;
2149 }
2150#endif
2151 if (ea.cmdidx == CMD_SIZE)
2152 {
2153 if (!ea.skip)
2154 {
2155 STRCPY(IObuff, _("E492: Not an editor command"));
2156 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002157 {
2158 /* If the modifier was parsed OK the error must be in the
2159 * following command */
2160 if (after_modifier != NULL)
2161 append_command(after_modifier);
2162 else
2163 append_command(*cmdlinep);
2164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002166 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 }
2168 goto doend;
2169 }
2170
Bram Moolenaar958636c2014-10-21 20:01:58 +02002171 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2172 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002173#ifdef HAVE_EX_SCRIPT_NI
2174 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2175#endif
2176 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178#ifndef FEAT_EVAL
2179 /*
2180 * When the expression evaluation is disabled, recognize the ":if" and
2181 * ":endif" commands and ignore everything in between it.
2182 */
2183 if (ea.cmdidx == CMD_if)
2184 ++if_level;
2185 if (if_level)
2186 {
2187 if (ea.cmdidx == CMD_endif)
2188 --if_level;
2189 goto doend;
2190 }
2191
2192#endif
2193
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002194 /* forced commands */
2195 if (*p == '!' && ea.cmdidx != CMD_substitute
2196 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
2198 ++p;
2199 ea.forceit = TRUE;
2200 }
2201 else
2202 ea.forceit = FALSE;
2203
2204/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002205 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002207 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002208 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
2210 if (!ea.skip)
2211 {
2212#ifdef HAVE_SANDBOX
2213 if (sandbox != 0 && !(ea.argt & SBOXOK))
2214 {
2215 /* Command not allowed in sandbox. */
2216 errormsg = (char_u *)_(e_sandbox);
2217 goto doend;
2218 }
2219#endif
2220 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2221 {
2222 /* Command not allowed in non-'modifiable' buffer */
2223 errormsg = (char_u *)_(e_modifiable);
2224 goto doend;
2225 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002226
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002227 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002228 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002230 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002231 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 goto doend;
2233 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002234 /* Disallow editing another buffer when "curbuf_lock" is set.
2235 * Do allow ":edit" (check for argument later).
2236 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002237 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002238 && ea.cmdidx != CMD_edit
2239 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002240 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002241 && curbuf_locked())
2242 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243
2244 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2245 {
2246 /* no range allowed */
2247 errormsg = (char_u *)_(e_norange);
2248 goto doend;
2249 }
2250 }
2251
2252 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2253 {
2254 errormsg = (char_u *)_(e_nobang);
2255 goto doend;
2256 }
2257
2258 /*
2259 * Don't complain about the range if it is not used
2260 * (could happen if line_count is accidentally set to 0).
2261 */
2262 if (!ea.skip && !ni)
2263 {
2264 /*
2265 * If the range is backwards, ask for confirmation and, if given, swap
2266 * ea.line1 & ea.line2 so it's forwards again.
2267 * When global command is busy, don't ask, will fail below.
2268 */
2269 if (!global_busy && ea.line1 > ea.line2)
2270 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002271 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002273 if (sourcing || exmode_active)
2274 {
2275 errormsg = (char_u *)_("E493: Backwards range given");
2276 goto doend;
2277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 if (ask_yesno((char_u *)
2279 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002280 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
2282 lnum = ea.line1;
2283 ea.line1 = ea.line2;
2284 ea.line2 = lnum;
2285 }
2286 if ((errormsg = invalid_range(&ea)) != NULL)
2287 goto doend;
2288 }
2289
2290 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2291 ea.line2 = 1;
2292
2293 correct_range(&ea);
2294
2295#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002296 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2297 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
2299 /* Put the first line at the start of a closed fold, put the last line
2300 * at the end of a closed fold. */
2301 (void)hasFolding(ea.line1, &ea.line1, NULL);
2302 (void)hasFolding(ea.line2, NULL, &ea.line2);
2303 }
2304#endif
2305
2306#ifdef FEAT_QUICKFIX
2307 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002308 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 * option here, so things like % get expanded.
2310 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002311 p = replace_makeprg(&ea, p, cmdlinep);
2312 if (p == NULL)
2313 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314#endif
2315
2316 /*
2317 * Skip to start of argument.
2318 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2319 */
2320 if (ea.cmdidx == CMD_bang)
2321 ea.arg = p;
2322 else
2323 ea.arg = skipwhite(p);
2324
2325 /*
2326 * Check for "++opt=val" argument.
2327 * Must be first, allow ":w ++enc=utf8 !cmd"
2328 */
2329 if (ea.argt & ARGOPT)
2330 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2331 if (getargopt(&ea) == FAIL && !ni)
2332 {
2333 errormsg = (char_u *)_(e_invarg);
2334 goto doend;
2335 }
2336
2337 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2338 {
2339 if (*ea.arg == '>') /* append */
2340 {
2341 if (*++ea.arg != '>') /* typed wrong */
2342 {
2343 errormsg = (char_u *)_("E494: Use w or w>>");
2344 goto doend;
2345 }
2346 ea.arg = skipwhite(ea.arg + 1);
2347 ea.append = TRUE;
2348 }
2349 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2350 {
2351 ++ea.arg;
2352 ea.usefilter = TRUE;
2353 }
2354 }
2355
2356 if (ea.cmdidx == CMD_read)
2357 {
2358 if (ea.forceit)
2359 {
2360 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2361 ea.forceit = FALSE;
2362 }
2363 else if (*ea.arg == '!') /* :r !filter */
2364 {
2365 ++ea.arg;
2366 ea.usefilter = TRUE;
2367 }
2368 }
2369
2370 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2371 {
2372 ea.amount = 1;
2373 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2374 {
2375 ++ea.arg;
2376 ++ea.amount;
2377 }
2378 ea.arg = skipwhite(ea.arg);
2379 }
2380
2381 /*
2382 * Check for "+command" argument, before checking for next command.
2383 * Don't do this for ":read !cmd" and ":write !cmd".
2384 */
2385 if ((ea.argt & EDITCMD) && !ea.usefilter)
2386 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2387
2388 /*
2389 * Check for '|' to separate commands and '"' to start comments.
2390 * Don't do this for ":read !cmd" and ":write !cmd".
2391 */
2392 if ((ea.argt & TRLBAR) && !ea.usefilter)
2393 separate_nextcmd(&ea);
2394
2395 /*
2396 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002397 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2398 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002400 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002401 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002402 || ea.cmdidx == CMD_global
2403 || ea.cmdidx == CMD_vglobal
2404 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {
2406 for (p = ea.arg; *p; ++p)
2407 {
2408 /* Remove one backslash before a newline, so that it's possible to
2409 * pass a newline to the shell and also a newline that is preceded
2410 * with a backslash. This makes it impossible to end a shell
2411 * command in a backslash, but that doesn't appear useful.
2412 * Halving the number of backslashes is incompatible with previous
2413 * versions. */
2414 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002415 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 else if (*p == '\n')
2417 {
2418 ea.nextcmd = p + 1;
2419 *p = NUL;
2420 break;
2421 }
2422 }
2423 }
2424
2425 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2426 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002427 buf_T *buf;
2428
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002430 switch (ea.addr_type)
2431 {
2432 case ADDR_LINES:
2433 ea.line2 = curbuf->b_ml.ml_line_count;
2434 break;
2435 case ADDR_LOADED_BUFFERS:
2436 buf = firstbuf;
2437 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2438 buf = buf->b_next;
2439 ea.line1 = buf->b_fnum;
2440 buf = lastbuf;
2441 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2442 buf = buf->b_prev;
2443 ea.line2 = buf->b_fnum;
2444 break;
2445 case ADDR_BUFFERS:
2446 ea.line1 = firstbuf->b_fnum;
2447 ea.line2 = lastbuf->b_fnum;
2448 break;
2449 case ADDR_WINDOWS:
2450 ea.line2 = LAST_WIN_NR;
2451 break;
2452 case ADDR_TABS:
2453 ea.line2 = LAST_TAB_NR;
2454 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002455 case ADDR_TABS_RELATIVE:
2456 ea.line2 = 1;
2457 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002458 case ADDR_ARGUMENTS:
2459 if (ARGCOUNT == 0)
2460 ea.line1 = ea.line2 = 0;
2461 else
2462 ea.line2 = ARGCOUNT;
2463 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002464#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002465 case ADDR_QUICKFIX:
2466 ea.line2 = qf_get_size(&ea);
2467 if (ea.line2 == 0)
2468 ea.line2 = 1;
2469 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002470#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 }
2473
2474 /* accept numbered register only when no count allowed (:put) */
2475 if ( (ea.argt & REGSTR)
2476 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002477 /* Do not allow register = for user commands */
2478 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2480 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002481#ifndef FEAT_CLIPBOARD
2482 /* check these explicitly for a more specific error message */
2483 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002485 errormsg = (char_u *)_(e_invalidreg);
2486 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 }
2488#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002489 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2490 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002491 {
2492 ea.regname = *ea.arg++;
2493#ifdef FEAT_EVAL
2494 /* for '=' register: accept the rest of the line as an expression */
2495 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2496 {
2497 set_expr_line(vim_strsave(ea.arg));
2498 ea.arg += STRLEN(ea.arg);
2499 }
2500#endif
2501 ea.arg = skipwhite(ea.arg);
2502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 }
2504
2505 /*
2506 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2507 * count, it's a buffer name.
2508 */
2509 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2510 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002511 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 {
2513 n = getdigits(&ea.arg);
2514 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002515 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
2517 errormsg = (char_u *)_(e_zerocount);
2518 goto doend;
2519 }
2520 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2521 {
2522 ea.line2 = n;
2523 if (ea.addr_count == 0)
2524 ea.addr_count = 1;
2525 }
2526 else
2527 {
2528 ea.line1 = ea.line2;
2529 ea.line2 += n - 1;
2530 ++ea.addr_count;
2531 /*
2532 * Be vi compatible: no error message for out of range.
2533 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002534 if (ea.addr_type == ADDR_LINES
2535 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 ea.line2 = curbuf->b_ml.ml_line_count;
2537 }
2538 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002539
2540 /*
2541 * Check for flags: 'l', 'p' and '#'.
2542 */
2543 if (ea.argt & EXFLAGS)
2544 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002546 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002547 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 {
2549 errormsg = (char_u *)_(e_trailing);
2550 goto doend;
2551 }
2552
2553 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2554 {
2555 errormsg = (char_u *)_(e_argreq);
2556 goto doend;
2557 }
2558
2559#ifdef FEAT_EVAL
2560 /*
2561 * Skip the command when it's not going to be executed.
2562 * The commands like :if, :endif, etc. always need to be executed.
2563 * Also make an exception for commands that handle a trailing command
2564 * themselves.
2565 */
2566 if (ea.skip)
2567 {
2568 switch (ea.cmdidx)
2569 {
2570 /* commands that need evaluation */
2571 case CMD_while:
2572 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002573 case CMD_for:
2574 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 case CMD_if:
2576 case CMD_elseif:
2577 case CMD_else:
2578 case CMD_endif:
2579 case CMD_try:
2580 case CMD_catch:
2581 case CMD_finally:
2582 case CMD_endtry:
2583 case CMD_function:
2584 break;
2585
2586 /* Commands that handle '|' themselves. Check: A command should
2587 * either have the TRLBAR flag, appear in this list or appear in
2588 * the list at ":help :bar". */
2589 case CMD_aboveleft:
2590 case CMD_and:
2591 case CMD_belowright:
2592 case CMD_botright:
2593 case CMD_browse:
2594 case CMD_call:
2595 case CMD_confirm:
2596 case CMD_delfunction:
2597 case CMD_djump:
2598 case CMD_dlist:
2599 case CMD_dsearch:
2600 case CMD_dsplit:
2601 case CMD_echo:
2602 case CMD_echoerr:
2603 case CMD_echomsg:
2604 case CMD_echon:
2605 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002606 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 case CMD_help:
2608 case CMD_hide:
2609 case CMD_ijump:
2610 case CMD_ilist:
2611 case CMD_isearch:
2612 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002613 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 case CMD_keepjumps:
2615 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002616 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 case CMD_leftabove:
2618 case CMD_let:
2619 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002620 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002622 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002623 case CMD_noautocmd:
2624 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 case CMD_perl:
2626 case CMD_psearch:
2627 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002628 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002629 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 case CMD_return:
2631 case CMD_rightbelow:
2632 case CMD_ruby:
2633 case CMD_silent:
2634 case CMD_smagic:
2635 case CMD_snomagic:
2636 case CMD_substitute:
2637 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002638 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 case CMD_tcl:
2640 case CMD_throw:
2641 case CMD_tilde:
2642 case CMD_topleft:
2643 case CMD_unlet:
2644 case CMD_verbose:
2645 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002646 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 break;
2648
2649 default: goto doend;
2650 }
2651 }
2652#endif
2653
2654 if (ea.argt & XFILE)
2655 {
2656 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2657 goto doend;
2658 }
2659
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 /*
2661 * Accept buffer name. Cannot be used at the same time with a buffer
2662 * number. Don't do this for a user command.
2663 */
2664 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002665 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 {
2667 /*
2668 * :bdelete, :bwipeout and :bunload take several arguments, separated
2669 * by spaces: find next space (skipping over escaped characters).
2670 * The others take one argument: ignore trailing spaces.
2671 */
2672 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2673 || ea.cmdidx == CMD_bunload)
2674 p = skiptowhite_esc(ea.arg);
2675 else
2676 {
2677 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002678 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 --p;
2680 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002681 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2682 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 if (ea.line2 < 0) /* failed */
2684 goto doend;
2685 ea.addr_count = 1;
2686 ea.arg = skipwhite(p);
2687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688
Bram Moolenaar2be57332018-02-13 18:05:18 +01002689 /* The :try command saves the emsg_silent flag, reset it here when
2690 * ":silent! try" was used, it should only apply to :try itself. */
2691 if (ea.cmdidx == CMD_try && did_esilent > 0)
2692 {
2693 emsg_silent -= did_esilent;
2694 if (emsg_silent < 0)
2695 emsg_silent = 0;
2696 did_esilent = 0;
2697 }
2698
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002700 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 *
2702 * The "ea" structure holds the arguments that can be used.
2703 */
2704 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002705 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 ea.cookie = cookie;
2707#ifdef FEAT_EVAL
2708 ea.cstack = cstack;
2709#endif
2710
2711#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002712 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
2714 /*
2715 * Execute a user-defined command.
2716 */
2717 do_ucmd(&ea);
2718 }
2719 else
2720#endif
2721 {
2722 /*
2723 * Call the function to execute the command.
2724 */
2725 ea.errmsg = NULL;
2726 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2727 if (ea.errmsg != NULL)
2728 errormsg = (char_u *)_(ea.errmsg);
2729 }
2730
2731#ifdef FEAT_EVAL
2732 /*
2733 * If the command just executed called do_cmdline(), any throw or ":return"
2734 * or ":finish" encountered there must also check the cstack of the still
2735 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2736 * exception, or reanimate a returned function or finished script file and
2737 * return or finish it again.
2738 */
2739 if (need_rethrow)
2740 do_throw(cstack);
2741 else if (check_cstack)
2742 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002743 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002745 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 && current_func_returned())
2747 do_return(&ea, TRUE, FALSE, NULL);
2748 }
2749 need_rethrow = check_cstack = FALSE;
2750#endif
2751
2752doend:
2753 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002754 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002756 curwin->w_cursor.col = 0;
2757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
2759 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2760 {
2761 if (sourcing)
2762 {
2763 if (errormsg != IObuff)
2764 {
2765 STRCPY(IObuff, errormsg);
2766 errormsg = IObuff;
2767 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002768 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
2770 emsg(errormsg);
2771 }
2772#ifdef FEAT_EVAL
2773 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002774 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2775 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776#endif
2777
2778 if (verbose_save >= 0)
2779 p_verbose = verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002780
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002781 if (cmdmod.save_ei != NULL)
2782 {
2783 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002784 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2785 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002786 free_string_option(cmdmod.save_ei);
2787 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002788
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002789 if (cmdmod.filter_regmatch.regprog != NULL)
2790 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791
2792 cmdmod = save_cmdmod;
2793
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002794 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 {
2796 /* messages could be enabled for a serious error, need to check if the
2797 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002798 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002799 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 emsg_silent -= did_esilent;
2801 if (emsg_silent < 0)
2802 emsg_silent = 0;
2803 /* Restore msg_scroll, it's set by file I/O commands, even when no
2804 * message is actually displayed. */
2805 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002806
2807 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2808 * somewhere in the line. Put it back in the first column. */
2809 if (redirecting())
2810 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002813#ifdef HAVE_SANDBOX
2814 if (did_sandbox)
2815 --sandbox;
2816#endif
2817
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2819 ea.nextcmd = NULL;
2820
2821#ifdef FEAT_EVAL
2822 --ex_nesting_level;
2823#endif
2824
2825 return ea.nextcmd;
2826}
2827#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002828 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829#endif
2830
2831/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002832 * Parse the address range, if any, in "eap".
2833 * Return FAIL and set "errormsg" or return OK.
2834 */
2835 int
2836parse_cmd_address(exarg_T *eap, char_u **errormsg)
2837{
2838 int address_count = 1;
2839 linenr_T lnum;
2840
2841 // Repeat for all ',' or ';' separated addresses.
2842 for (;;)
2843 {
2844 eap->line1 = eap->line2;
2845 switch (eap->addr_type)
2846 {
2847 case ADDR_LINES:
2848 // default is current line number
2849 eap->line2 = curwin->w_cursor.lnum;
2850 break;
2851 case ADDR_WINDOWS:
2852 eap->line2 = CURRENT_WIN_NR;
2853 break;
2854 case ADDR_ARGUMENTS:
2855 eap->line2 = curwin->w_arg_idx + 1;
2856 if (eap->line2 > ARGCOUNT)
2857 eap->line2 = ARGCOUNT;
2858 break;
2859 case ADDR_LOADED_BUFFERS:
2860 case ADDR_BUFFERS:
2861 eap->line2 = curbuf->b_fnum;
2862 break;
2863 case ADDR_TABS:
2864 eap->line2 = CURRENT_TAB_NR;
2865 break;
2866 case ADDR_TABS_RELATIVE:
2867 eap->line2 = 1;
2868 break;
2869#ifdef FEAT_QUICKFIX
2870 case ADDR_QUICKFIX:
2871 eap->line2 = qf_get_cur_valid_idx(eap);
2872 break;
2873#endif
2874 }
2875 eap->cmd = skipwhite(eap->cmd);
2876 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip,
2877 eap->addr_count == 0, address_count++);
2878 if (eap->cmd == NULL) // error detected
2879 return FAIL;
2880 if (lnum == MAXLNUM)
2881 {
2882 if (*eap->cmd == '%') // '%' - all lines
2883 {
2884 ++eap->cmd;
2885 switch (eap->addr_type)
2886 {
2887 case ADDR_LINES:
2888 eap->line1 = 1;
2889 eap->line2 = curbuf->b_ml.ml_line_count;
2890 break;
2891 case ADDR_LOADED_BUFFERS:
2892 {
2893 buf_T *buf = firstbuf;
2894
2895 while (buf->b_next != NULL
2896 && buf->b_ml.ml_mfp == NULL)
2897 buf = buf->b_next;
2898 eap->line1 = buf->b_fnum;
2899 buf = lastbuf;
2900 while (buf->b_prev != NULL
2901 && buf->b_ml.ml_mfp == NULL)
2902 buf = buf->b_prev;
2903 eap->line2 = buf->b_fnum;
2904 break;
2905 }
2906 case ADDR_BUFFERS:
2907 eap->line1 = firstbuf->b_fnum;
2908 eap->line2 = lastbuf->b_fnum;
2909 break;
2910 case ADDR_WINDOWS:
2911 case ADDR_TABS:
2912 if (IS_USER_CMDIDX(eap->cmdidx))
2913 {
2914 eap->line1 = 1;
2915 eap->line2 = eap->addr_type == ADDR_WINDOWS
2916 ? LAST_WIN_NR : LAST_TAB_NR;
2917 }
2918 else
2919 {
2920 // there is no Vim command which uses '%' and
2921 // ADDR_WINDOWS or ADDR_TABS
2922 *errormsg = (char_u *)_(e_invrange);
2923 return FAIL;
2924 }
2925 break;
2926 case ADDR_TABS_RELATIVE:
2927 *errormsg = (char_u *)_(e_invrange);
2928 return FAIL;
2929 case ADDR_ARGUMENTS:
2930 if (ARGCOUNT == 0)
2931 eap->line1 = eap->line2 = 0;
2932 else
2933 {
2934 eap->line1 = 1;
2935 eap->line2 = ARGCOUNT;
2936 }
2937 break;
2938#ifdef FEAT_QUICKFIX
2939 case ADDR_QUICKFIX:
2940 eap->line1 = 1;
2941 eap->line2 = qf_get_size(eap);
2942 if (eap->line2 == 0)
2943 eap->line2 = 1;
2944 break;
2945#endif
2946 }
2947 ++eap->addr_count;
2948 }
2949 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2950 {
2951 pos_T *fp;
2952
2953 // '*' - visual area
2954 if (eap->addr_type != ADDR_LINES)
2955 {
2956 *errormsg = (char_u *)_(e_invrange);
2957 return FAIL;
2958 }
2959
2960 ++eap->cmd;
2961 if (!eap->skip)
2962 {
2963 fp = getmark('<', FALSE);
2964 if (check_mark(fp) == FAIL)
2965 return FAIL;
2966 eap->line1 = fp->lnum;
2967 fp = getmark('>', FALSE);
2968 if (check_mark(fp) == FAIL)
2969 return FAIL;
2970 eap->line2 = fp->lnum;
2971 ++eap->addr_count;
2972 }
2973 }
2974 }
2975 else
2976 eap->line2 = lnum;
2977 eap->addr_count++;
2978
2979 if (*eap->cmd == ';')
2980 {
2981 if (!eap->skip)
2982 {
2983 curwin->w_cursor.lnum = eap->line2;
2984 // don't leave the cursor on an illegal line or column
2985 check_cursor();
2986 }
2987 }
2988 else if (*eap->cmd != ',')
2989 break;
2990 ++eap->cmd;
2991 }
2992
2993 // One address given: set start and end lines.
2994 if (eap->addr_count == 1)
2995 {
2996 eap->line1 = eap->line2;
2997 // ... but only implicit: really no address given
2998 if (lnum == MAXLNUM)
2999 eap->addr_count = 0;
3000 }
3001 return OK;
3002}
3003
3004/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003005 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 * If there is a match advance "pp" to the argument and return TRUE.
3007 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003008 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003009checkforcmd(
3010 char_u **pp, /* start of command */
3011 char *cmd, /* name of command */
3012 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
3014 int i;
3015
3016 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003017 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 break;
3019 if (i >= len && !isalpha((*pp)[i]))
3020 {
3021 *pp = skipwhite(*pp + i);
3022 return TRUE;
3023 }
3024 return FALSE;
3025}
3026
3027/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003028 * Append "cmd" to the error message in IObuff.
3029 * Takes care of limiting the length and handling 0xa0, which would be
3030 * invisible otherwise.
3031 */
3032 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003033append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003034{
3035 char_u *s = cmd;
3036 char_u *d;
3037
3038 STRCAT(IObuff, ": ");
3039 d = IObuff + STRLEN(IObuff);
3040 while (*s != NUL && d - IObuff < IOSIZE - 7)
3041 {
3042 if (
3043#ifdef FEAT_MBYTE
3044 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3045#endif
3046 *s == 0xa0)
3047 {
3048 s +=
3049#ifdef FEAT_MBYTE
3050 enc_utf8 ? 2 :
3051#endif
3052 1;
3053 STRCPY(d, "<a0>");
3054 d += 4;
3055 }
3056 else
3057 MB_COPY_CHAR(s, d);
3058 }
3059 *d = NUL;
3060}
3061
3062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003064 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003066 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 * Returns NULL for an ambiguous user command.
3068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003070find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 int len;
3073 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003074 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075
3076 /*
3077 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003078 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 * - the 'k' command can directly be followed by any character.
3080 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003081 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003083 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 */
3085 p = eap->cmd;
3086 if (*p == 'k')
3087 {
3088 eap->cmdidx = CMD_k;
3089 ++p;
3090 }
3091 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003092 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3093 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 || p[1] == 'g'
3095 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3096 || p[1] == 'I'
3097 || (p[1] == 'r' && p[2] != 'e')))
3098 {
3099 eap->cmdidx = CMD_substitute;
3100 ++p;
3101 }
3102 else
3103 {
3104 while (ASCII_ISALPHA(*p))
3105 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003106 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003107 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003108 while (ASCII_ISALNUM(*p))
3109 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003110
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 /* check for non-alpha command */
3112 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3113 ++p;
3114 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003115 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3116 {
3117 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3118 * :delete with the 'l' flag. Same for 'p'. */
3119 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003120 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003121 break;
3122 if (i == len - 1)
3123 {
3124 --len;
3125 if (p[-1] == 'l')
3126 eap->flags |= EXFLAG_LIST;
3127 else
3128 eap->flags |= EXFLAG_PRINT;
3129 }
3130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003132 if (ASCII_ISLOWER(eap->cmd[0]))
3133 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003134 int c1 = eap->cmd[0];
3135 int c2 = eap->cmd[1];
3136
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003137 if (command_count != (int)CMD_SIZE)
3138 {
3139 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3140 getout(1);
3141 }
3142
3143 /* Use a precomputed index for fast look-up in cmdnames[]
3144 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003145 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3146 if (ASCII_ISLOWER(c2))
3147 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003150 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3153 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3154 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3155 (size_t)len) == 0)
3156 {
3157#ifdef FEAT_EVAL
3158 if (full != NULL
3159 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3160 *full = TRUE;
3161#endif
3162 break;
3163 }
3164
3165#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003166 /* Look for a user defined command as a last resort. Let ":Print" be
3167 * overruled by a user defined command. */
3168 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3169 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003171 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 while (ASCII_ISALNUM(*p))
3173 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003174 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 }
3176#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003177 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 eap->cmdidx = CMD_SIZE;
3179 }
3180
3181 return p;
3182}
3183
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003184#ifdef FEAT_USR_CMDS
3185/*
3186 * Search for a user command that matches "eap->cmd".
3187 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3188 * Return a pointer to just after the command.
3189 * Return NULL if there is no matching command.
3190 */
3191 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003192find_ucmd(
3193 exarg_T *eap,
3194 char_u *p, /* end of the command (possibly including count) */
3195 int *full, /* set to TRUE for a full match */
3196 expand_T *xp, /* used for completion, NULL otherwise */
3197 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003198{
3199 int len = (int)(p - eap->cmd);
3200 int j, k, matchlen = 0;
3201 ucmd_T *uc;
3202 int found = FALSE;
3203 int possible = FALSE;
3204 char_u *cp, *np; /* Point into typed cmd and test name */
3205 garray_T *gap;
3206 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3207 only full match global is accepted. */
3208
3209 /*
3210 * Look for buffer-local user commands first, then global ones.
3211 */
3212 gap = &curbuf->b_ucmds;
3213 for (;;)
3214 {
3215 for (j = 0; j < gap->ga_len; ++j)
3216 {
3217 uc = USER_CMD_GA(gap, j);
3218 cp = eap->cmd;
3219 np = uc->uc_name;
3220 k = 0;
3221 while (k < len && *np != NUL && *cp++ == *np++)
3222 k++;
3223 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3224 {
3225 /* If finding a second match, the command is ambiguous. But
3226 * not if a buffer-local command wasn't a full match and a
3227 * global command is a full match. */
3228 if (k == len && found && *np != NUL)
3229 {
3230 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003231 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003232 amb_local = TRUE;
3233 }
3234
3235 if (!found || (k == len && *np == NUL))
3236 {
3237 /* If we matched up to a digit, then there could
3238 * be another command including the digit that we
3239 * should use instead.
3240 */
3241 if (k == len)
3242 found = TRUE;
3243 else
3244 possible = TRUE;
3245
3246 if (gap == &ucmds)
3247 eap->cmdidx = CMD_USER;
3248 else
3249 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003250 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003251 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003252 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003253
3254# ifdef FEAT_CMDL_COMPL
3255 if (compl != NULL)
3256 *compl = uc->uc_compl;
3257# ifdef FEAT_EVAL
3258 if (xp != NULL)
3259 {
3260 xp->xp_arg = uc->uc_compl_arg;
3261 xp->xp_scriptID = uc->uc_scriptID;
3262 }
3263# endif
3264# endif
3265 /* Do not search for further abbreviations
3266 * if this is an exact match. */
3267 matchlen = k;
3268 if (k == len && *np == NUL)
3269 {
3270 if (full != NULL)
3271 *full = TRUE;
3272 amb_local = FALSE;
3273 break;
3274 }
3275 }
3276 }
3277 }
3278
3279 /* Stop if we found a full match or searched all. */
3280 if (j < gap->ga_len || gap == &ucmds)
3281 break;
3282 gap = &ucmds;
3283 }
3284
3285 /* Only found ambiguous matches. */
3286 if (amb_local)
3287 {
3288 if (xp != NULL)
3289 xp->xp_context = EXPAND_UNSUCCESSFUL;
3290 return NULL;
3291 }
3292
3293 /* The match we found may be followed immediately by a number. Move "p"
3294 * back to point to it. */
3295 if (found || possible)
3296 return p + (matchlen - len);
3297 return p;
3298}
3299#endif
3300
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003302static struct cmdmod
3303{
3304 char *name;
3305 int minlen;
3306 int has_count; /* :123verbose :3tab */
3307} cmdmods[] = {
3308 {"aboveleft", 3, FALSE},
3309 {"belowright", 3, FALSE},
3310 {"botright", 2, FALSE},
3311 {"browse", 3, FALSE},
3312 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003313 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003314 {"hide", 3, FALSE},
3315 {"keepalt", 5, FALSE},
3316 {"keepjumps", 5, FALSE},
3317 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003318 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003319 {"leftabove", 5, FALSE},
3320 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003321 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003322 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003323 {"rightbelow", 6, FALSE},
3324 {"sandbox", 3, FALSE},
3325 {"silent", 3, FALSE},
3326 {"tab", 3, TRUE},
3327 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003328 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003329 {"verbose", 4, TRUE},
3330 {"vertical", 4, FALSE},
3331};
3332
3333/*
3334 * Return length of a command modifier (including optional count).
3335 * Return zero when it's not a modifier.
3336 */
3337 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003338modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003339{
3340 int i, j;
3341 char_u *p = cmd;
3342
3343 if (VIM_ISDIGIT(*cmd))
3344 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003345 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003346 {
3347 for (j = 0; p[j] != NUL; ++j)
3348 if (p[j] != cmdmods[i].name[j])
3349 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003350 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003351 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003352 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003353 }
3354 return 0;
3355}
3356
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357/*
3358 * Return > 0 if an Ex command "name" exists.
3359 * Return 2 if there is an exact match.
3360 * Return 3 if there is an ambiguous match.
3361 */
3362 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003363cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365 exarg_T ea;
3366 int full = FALSE;
3367 int i;
3368 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003369 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370
3371 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003372 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 {
3374 for (j = 0; name[j] != NUL; ++j)
3375 if (name[j] != cmdmods[i].name[j])
3376 break;
3377 if (name[j] == NUL && j >= cmdmods[i].minlen)
3378 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3379 }
3380
Bram Moolenaara9587612006-05-04 21:47:50 +00003381 /* Check built-in commands and user defined commands.
3382 * For ":2match" and ":3match" we need to skip the number. */
3383 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003385 p = find_command(&ea, &full);
3386 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003388 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3389 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003390 if (*skipwhite(p) != NUL)
3391 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3393}
3394#endif
3395
3396/*
3397 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3398 * we don't need/want deleted. Maybe this could be done better if we didn't
3399 * repeat all this stuff. The only problem is that they may not stay
3400 * perfectly compatible with each other, but then the command line syntax
3401 * probably won't change that much -- webb.
3402 */
3403 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003404set_one_cmd_context(
3405 expand_T *xp,
3406 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
3408 char_u *p;
3409 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003410 int len = 0;
3411 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3413 int compl = EXPAND_NOTHING;
3414#endif
3415#ifdef FEAT_CMDL_COMPL
3416 int delim;
3417#endif
3418 int forceit = FALSE;
3419 int usefilter = FALSE; /* filter instead of file name */
3420
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003421 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 xp->xp_pattern = buff;
3423 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003424 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
3426/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003427 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 */
3429 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3430 ;
3431 xp->xp_pattern = cmd;
3432
3433 if (*cmd == NUL)
3434 return NULL;
3435 if (*cmd == '"') /* ignore comment lines */
3436 {
3437 xp->xp_context = EXPAND_NOTHING;
3438 return NULL;
3439 }
3440
3441/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003442 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 */
3444 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 xp->xp_pattern = cmd;
3446 if (*cmd == NUL)
3447 return NULL;
3448 if (*cmd == '"')
3449 {
3450 xp->xp_context = EXPAND_NOTHING;
3451 return NULL;
3452 }
3453
3454 if (*cmd == '|' || *cmd == '\n')
3455 return cmd + 1; /* There's another command */
3456
3457 /*
3458 * Isolate the command and search for it in the command table.
3459 * Exceptions:
3460 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003461 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3463 */
3464 if (*cmd == 'k' && cmd[1] != 'e')
3465 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003466 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 p = cmd + 1;
3468 }
3469 else
3470 {
3471 p = cmd;
3472 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3473 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003474 /* a user command may contain digits */
3475 if (ASCII_ISUPPER(cmd[0]))
3476 while (ASCII_ISALNUM(*p) || *p == '*')
3477 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003478 /* for python 3.x: ":py3*" commands completion */
3479 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003480 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003481 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003482 while (ASCII_ISALPHA(*p) || *p == '*')
3483 ++p;
3484 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003485 /* check for non-alpha command */
3486 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3487 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003488 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003490 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
3492 xp->xp_context = EXPAND_UNSUCCESSFUL;
3493 return NULL;
3494 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003495 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003496 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3497 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3498 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 break;
3500
3501#ifdef FEAT_USR_CMDS
3502 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3504 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505#endif
3506 }
3507
3508 /*
3509 * If the cursor is touching the command, and it ends in an alpha-numeric
3510 * character, complete the command name.
3511 */
3512 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3513 return NULL;
3514
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003515 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
3517 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3518 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003519 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 p = cmd + 1;
3521 }
3522#ifdef FEAT_USR_CMDS
3523 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3524 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003525 ea.cmd = cmd;
3526 p = find_ucmd(&ea, p, NULL, xp,
3527# if defined(FEAT_CMDL_COMPL)
3528 &compl
3529# else
3530 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003532 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003533 if (p == NULL)
3534 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
3536#endif
3537 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003538 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 {
3540 /* Not still touching the command and it was an illegal one */
3541 xp->xp_context = EXPAND_UNSUCCESSFUL;
3542 return NULL;
3543 }
3544
3545 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3546
3547 if (*p == '!') /* forced commands */
3548 {
3549 forceit = TRUE;
3550 ++p;
3551 }
3552
3553/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003554 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003556 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003557 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
3559 arg = skipwhite(p);
3560
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003561 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
3563 if (*arg == '>') /* append */
3564 {
3565 if (*++arg == '>')
3566 ++arg;
3567 arg = skipwhite(arg);
3568 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003569 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
3571 ++arg;
3572 usefilter = TRUE;
3573 }
3574 }
3575
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003576 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 {
3578 usefilter = forceit; /* :r! filter if forced */
3579 if (*arg == '!') /* :r !filter */
3580 {
3581 ++arg;
3582 usefilter = TRUE;
3583 }
3584 }
3585
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003586 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
3588 while (*arg == *cmd) /* allow any number of '>' or '<' */
3589 ++arg;
3590 arg = skipwhite(arg);
3591 }
3592
3593 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003594 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 {
3596 /* Check if we're in the +command */
3597 p = arg + 1;
3598 arg = skip_cmd_arg(arg, FALSE);
3599
3600 /* Still touching the command after '+'? */
3601 if (*arg == NUL)
3602 return p;
3603
3604 /* Skip space(s) after +command to get to the real argument */
3605 arg = skipwhite(arg);
3606 }
3607
3608 /*
3609 * Check for '|' to separate commands and '"' to start comments.
3610 * Don't do this for ":read !cmd" and ":write !cmd".
3611 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003612 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
3614 p = arg;
3615 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003616 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 p += 2;
3618 while (*p)
3619 {
3620 if (*p == Ctrl_V)
3621 {
3622 if (p[1] != NUL)
3623 ++p;
3624 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003625 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 || *p == '|' || *p == '\n')
3627 {
3628 if (*(p - 1) != '\\')
3629 {
3630 if (*p == '|' || *p == '\n')
3631 return p + 1;
3632 return NULL; /* It's a comment */
3633 }
3634 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003635 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637 }
3638
3639 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003640 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 vim_strchr((char_u *)"|\"", *arg) == NULL)
3642 return NULL;
3643
3644 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003645 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003647 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003648 while (*p && p < buff + len)
3649 {
3650 if (*p == ' ' || *p == TAB)
3651 {
3652 /* argument starts after a space */
3653 xp->xp_pattern = ++p;
3654 }
3655 else
3656 {
3657 if (*p == '\\' && *(p + 1) != NUL)
3658 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003659 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003660 }
3661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003663 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003665 int c;
3666 int in_quote = FALSE;
3667 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
3669 /*
3670 * Allow spaces within back-quotes to count as part of the argument
3671 * being expanded.
3672 */
3673 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003674 p = xp->xp_pattern;
3675 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003677#ifdef FEAT_MBYTE
3678 if (has_mbyte)
3679 c = mb_ptr2char(p);
3680 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003682 c = *p;
3683 if (c == '\\' && p[1] != NUL)
3684 ++p;
3685 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
3687 if (!in_quote)
3688 {
3689 xp->xp_pattern = p;
3690 bow = p + 1;
3691 }
3692 in_quote = !in_quote;
3693 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003694 /* An argument can contain just about everything, except
3695 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003696 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003697#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003698 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003699#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003700 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003701 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003702 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003703 while (*p != NUL)
3704 {
3705#ifdef FEAT_MBYTE
3706 if (has_mbyte)
3707 c = mb_ptr2char(p);
3708 else
3709#endif
3710 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003711 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003712 break;
3713#ifdef FEAT_MBYTE
3714 if (has_mbyte)
3715 len = (*mb_ptr2len)(p);
3716 else
3717#endif
3718 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003719 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003720 }
3721 if (in_quote)
3722 bow = p;
3723 else
3724 xp->xp_pattern = p;
3725 p -= len;
3726 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003727 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729
3730 /*
3731 * If we are still inside the quotes, and we passed a space, just
3732 * expand from there.
3733 */
3734 if (bow != NULL && in_quote)
3735 xp->xp_pattern = bow;
3736 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003737
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003738 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003739 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003740 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003741#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003742 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003743#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003744 /* When still after the command name expand executables. */
3745 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003746 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
3749 /* Check for environment variable */
3750 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003751#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 || *xp->xp_pattern == '%'
3753#endif
3754 )
3755 {
3756 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3757 if (!vim_isIDc(*p))
3758 break;
3759 if (*p == NUL)
3760 {
3761 xp->xp_context = EXPAND_ENV_VARS;
3762 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003763#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3764 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003765 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003766 compl = EXPAND_ENV_VARS;
3767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
3769 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003770#if defined(FEAT_CMDL_COMPL)
3771 /* Check for user names */
3772 if (*xp->xp_pattern == '~')
3773 {
3774 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3775 ;
3776 /* Complete ~user only if it partially matches a user name.
3777 * A full match ~user<Tab> will be replaced by user's home
3778 * directory i.e. something like ~user<Tab> -> /home/user/ */
3779 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003780 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003781 {
3782 xp->xp_context = EXPAND_USER;
3783 ++xp->xp_pattern;
3784 }
3785 }
3786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 }
3788
3789/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003790 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003792 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003794 case CMD_find:
3795 case CMD_sfind:
3796 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003797 if (xp->xp_context == EXPAND_FILES)
3798 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003799 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 case CMD_cd:
3801 case CMD_chdir:
3802 case CMD_lcd:
3803 case CMD_lchdir:
3804 if (xp->xp_context == EXPAND_FILES)
3805 xp->xp_context = EXPAND_DIRECTORIES;
3806 break;
3807 case CMD_help:
3808 xp->xp_context = EXPAND_HELP;
3809 xp->xp_pattern = arg;
3810 break;
3811
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003812 /* Command modifiers: return the argument.
3813 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003815 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 case CMD_belowright:
3817 case CMD_botright:
3818 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003819 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003820 case CMD_cdo:
3821 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003823 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 case CMD_folddoclosed:
3825 case CMD_folddoopen:
3826 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003827 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 case CMD_keepjumps:
3829 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003830 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003831 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003833 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003835 case CMD_noautocmd:
3836 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003838 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003840 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003841 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 case CMD_topleft:
3843 case CMD_verbose:
3844 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003845 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 return arg;
3847
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003848 case CMD_filter:
3849 if (*arg != NUL)
3850 arg = skip_vimgrep_pat(arg, NULL, NULL);
3851 if (arg == NULL || *arg == NUL)
3852 {
3853 xp->xp_context = EXPAND_NOTHING;
3854 return NULL;
3855 }
3856 return skipwhite(arg);
3857
Bram Moolenaar4f688582007-07-24 12:34:30 +00003858#ifdef FEAT_CMDL_COMPL
3859# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 case CMD_match:
3861 if (*arg == NUL || !ends_excmd(*arg))
3862 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003863 /* also complete "None" */
3864 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 arg = skipwhite(skiptowhite(arg));
3866 if (*arg != NUL)
3867 {
3868 xp->xp_context = EXPAND_NOTHING;
3869 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3870 }
3871 }
3872 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875/*
3876 * All completion for the +cmdline_compl feature goes here.
3877 */
3878
3879# ifdef FEAT_USR_CMDS
3880 case CMD_command:
3881 /* Check for attributes */
3882 while (*arg == '-')
3883 {
3884 arg++; /* Skip "-" */
3885 p = skiptowhite(arg);
3886 if (*p == NUL)
3887 {
3888 /* Cursor is still in the attribute */
3889 p = vim_strchr(arg, '=');
3890 if (p == NULL)
3891 {
3892 /* No "=", so complete attribute names */
3893 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3894 xp->xp_pattern = arg;
3895 return NULL;
3896 }
3897
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003898 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 * their arguments as well.
3900 */
3901 if (STRNICMP(arg, "complete", p - arg) == 0)
3902 {
3903 xp->xp_context = EXPAND_USER_COMPLETE;
3904 xp->xp_pattern = p + 1;
3905 return NULL;
3906 }
3907 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3908 {
3909 xp->xp_context = EXPAND_USER_NARGS;
3910 xp->xp_pattern = p + 1;
3911 return NULL;
3912 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003913 else if (STRNICMP(arg, "addr", p - arg) == 0)
3914 {
3915 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3916 xp->xp_pattern = p + 1;
3917 return NULL;
3918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 return NULL;
3920 }
3921 arg = skipwhite(p);
3922 }
3923
3924 /* After the attributes comes the new command name */
3925 p = skiptowhite(arg);
3926 if (*p == NUL)
3927 {
3928 xp->xp_context = EXPAND_USER_COMMANDS;
3929 xp->xp_pattern = arg;
3930 break;
3931 }
3932
3933 /* And finally comes a normal command */
3934 return skipwhite(p);
3935
3936 case CMD_delcommand:
3937 xp->xp_context = EXPAND_USER_COMMANDS;
3938 xp->xp_pattern = arg;
3939 break;
3940# endif
3941
3942 case CMD_global:
3943 case CMD_vglobal:
3944 delim = *arg; /* get the delimiter */
3945 if (delim)
3946 ++arg; /* skip delimiter if there is one */
3947
3948 while (arg[0] != NUL && arg[0] != delim)
3949 {
3950 if (arg[0] == '\\' && arg[1] != NUL)
3951 ++arg;
3952 ++arg;
3953 }
3954 if (arg[0] != NUL)
3955 return arg + 1;
3956 break;
3957 case CMD_and:
3958 case CMD_substitute:
3959 delim = *arg;
3960 if (delim)
3961 {
3962 /* skip "from" part */
3963 ++arg;
3964 arg = skip_regexp(arg, delim, p_magic, NULL);
3965 }
3966 /* skip "to" part */
3967 while (arg[0] != NUL && arg[0] != delim)
3968 {
3969 if (arg[0] == '\\' && arg[1] != NUL)
3970 ++arg;
3971 ++arg;
3972 }
3973 if (arg[0] != NUL) /* skip delimiter */
3974 ++arg;
3975 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3976 ++arg;
3977 if (arg[0] != NUL)
3978 return arg;
3979 break;
3980 case CMD_isearch:
3981 case CMD_dsearch:
3982 case CMD_ilist:
3983 case CMD_dlist:
3984 case CMD_ijump:
3985 case CMD_psearch:
3986 case CMD_djump:
3987 case CMD_isplit:
3988 case CMD_dsplit:
3989 arg = skipwhite(skipdigits(arg)); /* skip count */
3990 if (*arg == '/') /* Match regexp, not just whole words */
3991 {
3992 for (++arg; *arg && *arg != '/'; arg++)
3993 if (*arg == '\\' && arg[1] != NUL)
3994 arg++;
3995 if (*arg)
3996 {
3997 arg = skipwhite(arg + 1);
3998
3999 /* Check for trailing illegal characters */
4000 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4001 xp->xp_context = EXPAND_NOTHING;
4002 else
4003 return arg;
4004 }
4005 }
4006 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004007
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 case CMD_autocmd:
4009 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004011 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 case CMD_set:
4014 set_context_in_set_cmd(xp, arg, 0);
4015 break;
4016 case CMD_setglobal:
4017 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4018 break;
4019 case CMD_setlocal:
4020 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4021 break;
4022 case CMD_tag:
4023 case CMD_stag:
4024 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004025 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 case CMD_tselect:
4027 case CMD_stselect:
4028 case CMD_ptselect:
4029 case CMD_tjump:
4030 case CMD_stjump:
4031 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004032 if (*p_wop != NUL)
4033 xp->xp_context = EXPAND_TAGS_LISTFILES;
4034 else
4035 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 xp->xp_pattern = arg;
4037 break;
4038 case CMD_augroup:
4039 xp->xp_context = EXPAND_AUGROUP;
4040 xp->xp_pattern = arg;
4041 break;
4042#ifdef FEAT_SYN_HL
4043 case CMD_syntax:
4044 set_context_in_syntax_cmd(xp, arg);
4045 break;
4046#endif
4047#ifdef FEAT_EVAL
4048 case CMD_let:
4049 case CMD_if:
4050 case CMD_elseif:
4051 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004052 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 case CMD_echo:
4054 case CMD_echon:
4055 case CMD_execute:
4056 case CMD_echomsg:
4057 case CMD_echoerr:
4058 case CMD_call:
4059 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004060 case CMD_cexpr:
4061 case CMD_caddexpr:
4062 case CMD_cgetexpr:
4063 case CMD_lexpr:
4064 case CMD_laddexpr:
4065 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004066 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 break;
4068
4069 case CMD_unlet:
4070 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4071 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02004072
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 xp->xp_context = EXPAND_USER_VARS;
4074 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02004075
4076 if (*xp->xp_pattern == '$')
4077 {
4078 xp->xp_context = EXPAND_ENV_VARS;
4079 ++xp->xp_pattern;
4080 }
4081
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 break;
4083
4084 case CMD_function:
4085 case CMD_delfunction:
4086 xp->xp_context = EXPAND_USER_FUNC;
4087 xp->xp_pattern = arg;
4088 break;
4089
4090 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004091 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 break;
4093#endif
4094 case CMD_highlight:
4095 set_context_in_highlight_cmd(xp, arg);
4096 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004097#ifdef FEAT_CSCOPE
4098 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004099 case CMD_lcscope:
4100 case CMD_scscope:
4101 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004102 break;
4103#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004104#ifdef FEAT_SIGNS
4105 case CMD_sign:
4106 set_context_in_sign_cmd(xp, arg);
4107 break;
4108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 case CMD_bdelete:
4110 case CMD_bwipeout:
4111 case CMD_bunload:
4112 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4113 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004114 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 case CMD_buffer:
4116 case CMD_sbuffer:
4117 case CMD_checktime:
4118 xp->xp_context = EXPAND_BUFFERS;
4119 xp->xp_pattern = arg;
4120 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121#ifdef FEAT_USR_CMDS
4122 case CMD_USER:
4123 case CMD_USER_BUF:
4124 if (compl != EXPAND_NOTHING)
4125 {
4126 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004127 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
4129# ifdef FEAT_MENU
4130 if (compl == EXPAND_MENUS)
4131 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4132# endif
4133 if (compl == EXPAND_COMMANDS)
4134 return arg;
4135 if (compl == EXPAND_MAPPINGS)
4136 return set_context_in_map_cmd(xp, (char_u *)"map",
4137 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004138 /* Find start of last argument. */
4139 p = arg;
4140 while (*p)
4141 {
4142 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004143 /* argument starts after a space */
4144 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004145 else if (*p == '\\' && *(p + 1) != NUL)
4146 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004147 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 xp->xp_pattern = arg;
4150 }
4151 xp->xp_context = compl;
4152 }
4153 break;
4154#endif
4155 case CMD_map: case CMD_noremap:
4156 case CMD_nmap: case CMD_nnoremap:
4157 case CMD_vmap: case CMD_vnoremap:
4158 case CMD_omap: case CMD_onoremap:
4159 case CMD_imap: case CMD_inoremap:
4160 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004161 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004162 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004163 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004164 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004166 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 case CMD_unmap:
4168 case CMD_nunmap:
4169 case CMD_vunmap:
4170 case CMD_ounmap:
4171 case CMD_iunmap:
4172 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004173 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004174 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004175 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004176 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004178 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004179 case CMD_mapclear:
4180 case CMD_nmapclear:
4181 case CMD_vmapclear:
4182 case CMD_omapclear:
4183 case CMD_imapclear:
4184 case CMD_cmapclear:
4185 case CMD_lmapclear:
4186 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004187 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004188 case CMD_xmapclear:
4189 xp->xp_context = EXPAND_MAPCLEAR;
4190 xp->xp_pattern = arg;
4191 break;
4192
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 case CMD_abbreviate: case CMD_noreabbrev:
4194 case CMD_cabbrev: case CMD_cnoreabbrev:
4195 case CMD_iabbrev: case CMD_inoreabbrev:
4196 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004197 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 case CMD_unabbreviate:
4199 case CMD_cunabbrev:
4200 case CMD_iunabbrev:
4201 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004202 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203#ifdef FEAT_MENU
4204 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4205 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4206 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4207 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4208 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4209 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4210 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4211 case CMD_tmenu: case CMD_tunmenu:
4212 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4213 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4214#endif
4215
4216 case CMD_colorscheme:
4217 xp->xp_context = EXPAND_COLORS;
4218 xp->xp_pattern = arg;
4219 break;
4220
4221 case CMD_compiler:
4222 xp->xp_context = EXPAND_COMPILER;
4223 xp->xp_pattern = arg;
4224 break;
4225
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004226 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004227 xp->xp_context = EXPAND_OWNSYNTAX;
4228 xp->xp_pattern = arg;
4229 break;
4230
4231 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004232 xp->xp_context = EXPAND_FILETYPE;
4233 xp->xp_pattern = arg;
4234 break;
4235
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004236 case CMD_packadd:
4237 xp->xp_context = EXPAND_PACKADD;
4238 xp->xp_pattern = arg;
4239 break;
4240
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4242 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4243 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004244 p = skiptowhite(arg);
4245 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 {
4247 xp->xp_context = EXPAND_LANGUAGE;
4248 xp->xp_pattern = arg;
4249 }
4250 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004251 {
4252 if ( STRNCMP(arg, "messages", p - arg) == 0
4253 || STRNCMP(arg, "ctype", p - arg) == 0
4254 || STRNCMP(arg, "time", p - arg) == 0)
4255 {
4256 xp->xp_context = EXPAND_LOCALES;
4257 xp->xp_pattern = skipwhite(p);
4258 }
4259 else
4260 xp->xp_context = EXPAND_NOTHING;
4261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 break;
4263#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004264#if defined(FEAT_PROFILE)
4265 case CMD_profile:
4266 set_context_in_profile_cmd(xp, arg);
4267 break;
4268#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004269 case CMD_behave:
4270 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004271 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004274 case CMD_messages:
4275 xp->xp_context = EXPAND_MESSAGES;
4276 xp->xp_pattern = arg;
4277 break;
4278
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004279#if defined(FEAT_CMDHIST)
4280 case CMD_history:
4281 xp->xp_context = EXPAND_HISTORY;
4282 xp->xp_pattern = arg;
4283 break;
4284#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004285#if defined(FEAT_PROFILE)
4286 case CMD_syntime:
4287 xp->xp_context = EXPAND_SYNTIME;
4288 xp->xp_pattern = arg;
4289 break;
4290#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004291
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004292 case CMD_argdelete:
4293 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4294 arg = xp->xp_pattern + 1;
4295 xp->xp_context = EXPAND_ARGLIST;
4296 xp->xp_pattern = arg;
4297 break;
4298
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299#endif /* FEAT_CMDL_COMPL */
4300
4301 default:
4302 break;
4303 }
4304 return NULL;
4305}
4306
4307/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004308 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 *
4310 * Backslashed delimiters after / or ? will be skipped, and commands will
4311 * not be expanded between /'s and ?'s or after "'".
4312 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004313 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 * Returns the "cmd" pointer advanced to beyond the range.
4315 */
4316 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004317skip_range(
4318 char_u *cmd,
4319 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004321 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004323 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004325 if (*cmd == '\\')
4326 {
4327 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4328 ++cmd;
4329 else
4330 break;
4331 }
4332 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 {
4334 if (*++cmd == NUL && ctx != NULL)
4335 *ctx = EXPAND_NOTHING;
4336 }
4337 else if (*cmd == '/' || *cmd == '?')
4338 {
4339 delim = *cmd++;
4340 while (*cmd != NUL && *cmd != delim)
4341 if (*cmd++ == '\\' && *cmd != NUL)
4342 ++cmd;
4343 if (*cmd == NUL && ctx != NULL)
4344 *ctx = EXPAND_NOTHING;
4345 }
4346 if (*cmd != NUL)
4347 ++cmd;
4348 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004349
4350 /* Skip ":" and white space. */
4351 while (*cmd == ':')
4352 cmd = skipwhite(cmd + 1);
4353
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 return cmd;
4355}
4356
4357/*
4358 * get a single EX address
4359 *
4360 * Set ptr to the next character after the part that was interpreted.
4361 * Set ptr to NULL when an error is encountered.
4362 *
4363 * Return MAXLNUM when no Ex address was found.
4364 */
4365 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004366get_address(
4367 exarg_T *eap UNUSED,
4368 char_u **ptr,
4369 int addr_type, /* flag: one of ADDR_LINES, ... */
4370 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004371 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004372 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373{
4374 int c;
4375 int i;
4376 long n;
4377 char_u *cmd;
4378 pos_T pos;
4379 pos_T *fp;
4380 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004381 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382
4383 cmd = skipwhite(*ptr);
4384 lnum = MAXLNUM;
4385 do
4386 {
4387 switch (*cmd)
4388 {
4389 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004390 ++cmd;
4391 switch (addr_type)
4392 {
4393 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 lnum = curwin->w_cursor.lnum;
4395 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004396 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004397 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004398 break;
4399 case ADDR_ARGUMENTS:
4400 lnum = curwin->w_arg_idx + 1;
4401 break;
4402 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004403 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004404 lnum = curbuf->b_fnum;
4405 break;
4406 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004407 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004408 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004409 case ADDR_TABS_RELATIVE:
4410 EMSG(_(e_invrange));
4411 cmd = NULL;
4412 goto error;
4413 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004414#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004415 case ADDR_QUICKFIX:
4416 lnum = qf_get_cur_valid_idx(eap);
4417 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004418#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004419 }
4420 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
4422 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004423 ++cmd;
4424 switch (addr_type)
4425 {
4426 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 lnum = curbuf->b_ml.ml_line_count;
4428 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004430 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004431 break;
4432 case ADDR_ARGUMENTS:
4433 lnum = ARGCOUNT;
4434 break;
4435 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004436 buf = lastbuf;
4437 while (buf->b_ml.ml_mfp == NULL)
4438 {
4439 if (buf->b_prev == NULL)
4440 break;
4441 buf = buf->b_prev;
4442 }
4443 lnum = buf->b_fnum;
4444 break;
4445 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004446 lnum = lastbuf->b_fnum;
4447 break;
4448 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004449 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004451 case ADDR_TABS_RELATIVE:
4452 EMSG(_(e_invrange));
4453 cmd = NULL;
4454 goto error;
4455 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004456#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004457 case ADDR_QUICKFIX:
4458 lnum = qf_get_size(eap);
4459 if (lnum == 0)
4460 lnum = 1;
4461 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004462#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004463 }
4464 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
4466 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004467 if (*++cmd == NUL)
4468 {
4469 cmd = NULL;
4470 goto error;
4471 }
4472 if (addr_type != ADDR_LINES)
4473 {
4474 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004475 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004476 goto error;
4477 }
4478 if (skip)
4479 ++cmd;
4480 else
4481 {
4482 /* Only accept a mark in another file when it is
4483 * used by itself: ":'M". */
4484 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4485 ++cmd;
4486 if (fp == (pos_T *)-1)
4487 /* Jumped to another file. */
4488 lnum = curwin->w_cursor.lnum;
4489 else
4490 {
4491 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
4493 cmd = NULL;
4494 goto error;
4495 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004496 lnum = fp->lnum;
4497 }
4498 }
4499 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
4501 case '/':
4502 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004503 c = *cmd++;
4504 if (addr_type != ADDR_LINES)
4505 {
4506 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004507 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004508 goto error;
4509 }
4510 if (skip) /* skip "/pat/" */
4511 {
4512 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4513 if (*cmd == c)
4514 ++cmd;
4515 }
4516 else
4517 {
4518 pos = curwin->w_cursor; /* save curwin->w_cursor */
4519 /*
4520 * When '/' or '?' follows another address, start
4521 * from there.
4522 */
4523 if (lnum != MAXLNUM)
4524 curwin->w_cursor.lnum = lnum;
4525 /*
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004526 * Start a forward search at the end of the line (unless
4527 * before the first line).
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004528 * Start a backward search at the start of the line.
4529 * This makes sure we never match in the current
4530 * line, and can match anywhere in the
4531 * next/previous line.
4532 */
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004533 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004534 curwin->w_cursor.col = MAXCOL;
4535 else
4536 curwin->w_cursor.col = 0;
4537 searchcmdlen = 0;
4538 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004539 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004540 {
4541 curwin->w_cursor = pos;
4542 cmd = NULL;
4543 goto error;
4544 }
4545 lnum = curwin->w_cursor.lnum;
4546 curwin->w_cursor = pos;
4547 /* adjust command string pointer */
4548 cmd += searchcmdlen;
4549 }
4550 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
4552 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004553 ++cmd;
4554 if (addr_type != ADDR_LINES)
4555 {
4556 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004557 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004558 goto error;
4559 }
4560 if (*cmd == '&')
4561 i = RE_SUBST;
4562 else if (*cmd == '?' || *cmd == '/')
4563 i = RE_SEARCH;
4564 else
4565 {
4566 EMSG(_(e_backslash));
4567 cmd = NULL;
4568 goto error;
4569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004571 if (!skip)
4572 {
4573 /*
4574 * When search follows another address, start from
4575 * there.
4576 */
4577 if (lnum != MAXLNUM)
4578 pos.lnum = lnum;
4579 else
4580 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004582 /*
4583 * Start the search just like for the above
4584 * do_search().
4585 */
4586 if (*cmd != '?')
4587 pos.col = MAXCOL;
4588 else
4589 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004590#ifdef FEAT_VIRTUALEDIT
4591 pos.coladd = 0;
4592#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004593 if (searchit(curwin, curbuf, &pos,
4594 *cmd == '?' ? BACKWARD : FORWARD,
4595 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004596 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004597 lnum = pos.lnum;
4598 else
4599 {
4600 cmd = NULL;
4601 goto error;
4602 }
4603 }
4604 ++cmd;
4605 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606
4607 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004608 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4609 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
4611
4612 for (;;)
4613 {
4614 cmd = skipwhite(cmd);
4615 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4616 break;
4617
4618 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004619 {
4620 switch (addr_type)
4621 {
4622 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004623 /* "+1" is same as ".+1" */
4624 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004625 break;
4626 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004627 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004628 break;
4629 case ADDR_ARGUMENTS:
4630 lnum = curwin->w_arg_idx + 1;
4631 break;
4632 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004633 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004634 lnum = curbuf->b_fnum;
4635 break;
4636 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004637 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004638 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004639 case ADDR_TABS_RELATIVE:
4640 lnum = 1;
4641 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004642#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004643 case ADDR_QUICKFIX:
4644 lnum = qf_get_cur_valid_idx(eap);
4645 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004646#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004647 }
4648 }
4649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 if (VIM_ISDIGIT(*cmd))
4651 i = '+'; /* "number" is same as "+number" */
4652 else
4653 i = *cmd++;
4654 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4655 n = 1;
4656 else
4657 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004658
4659 if (addr_type == ADDR_TABS_RELATIVE)
4660 {
4661 EMSG(_(e_invrange));
4662 cmd = NULL;
4663 goto error;
4664 }
4665 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004666 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004667 lnum = compute_buffer_local_count(
4668 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004670 {
4671#ifdef FEAT_FOLDING
4672 /* Relative line addressing, need to adjust for folded lines
4673 * now, but only do it after the first address. */
4674 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4675 && address_count >= 2)
4676 (void)hasFolding(lnum, NULL, &lnum);
4677#endif
4678 if (i == '-')
4679 lnum -= n;
4680 else
4681 lnum += n;
4682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
4684 } while (*cmd == '/' || *cmd == '?');
4685
4686error:
4687 *ptr = cmd;
4688 return lnum;
4689}
4690
4691/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004692 * Get flags from an Ex command argument.
4693 */
4694 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004695get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004696{
4697 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4698 {
4699 if (*eap->arg == 'l')
4700 eap->flags |= EXFLAG_LIST;
4701 else if (*eap->arg == 'p')
4702 eap->flags |= EXFLAG_PRINT;
4703 else
4704 eap->flags |= EXFLAG_NR;
4705 eap->arg = skipwhite(eap->arg + 1);
4706 }
4707}
4708
4709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 * Function called for command which is Not Implemented. NI!
4711 */
4712 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004713ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715 if (!eap->skip)
4716 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4717}
4718
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004719#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720/*
4721 * Function called for script command which is Not Implemented. NI!
4722 * Skips over ":perl <<EOF" constructs.
4723 */
4724 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004725ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
4727 if (!eap->skip)
4728 ex_ni(eap);
4729 else
4730 vim_free(script_get(eap, eap->arg));
4731}
4732#endif
4733
4734/*
4735 * Check range in Ex command for validity.
4736 * Return NULL when valid, error message when invalid.
4737 */
4738 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004739invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004741 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 if ( eap->line1 < 0
4743 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004744 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004746
4747 if (eap->argt & RANGE)
4748 {
4749 switch(eap->addr_type)
4750 {
4751 case ADDR_LINES:
4752 if (!(eap->argt & NOTADR)
4753 && eap->line2 > curbuf->b_ml.ml_line_count
4754#ifdef FEAT_DIFF
4755 + (eap->cmdidx == CMD_diffget)
4756#endif
4757 )
4758 return (char_u *)_(e_invrange);
4759 break;
4760 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004761 /* add 1 if ARGCOUNT is 0 */
4762 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004763 return (char_u *)_(e_invrange);
4764 break;
4765 case ADDR_BUFFERS:
4766 if (eap->line1 < firstbuf->b_fnum
4767 || eap->line2 > lastbuf->b_fnum)
4768 return (char_u *)_(e_invrange);
4769 break;
4770 case ADDR_LOADED_BUFFERS:
4771 buf = firstbuf;
4772 while (buf->b_ml.ml_mfp == NULL)
4773 {
4774 if (buf->b_next == NULL)
4775 return (char_u *)_(e_invrange);
4776 buf = buf->b_next;
4777 }
4778 if (eap->line1 < buf->b_fnum)
4779 return (char_u *)_(e_invrange);
4780 buf = lastbuf;
4781 while (buf->b_ml.ml_mfp == NULL)
4782 {
4783 if (buf->b_prev == NULL)
4784 return (char_u *)_(e_invrange);
4785 buf = buf->b_prev;
4786 }
4787 if (eap->line2 > buf->b_fnum)
4788 return (char_u *)_(e_invrange);
4789 break;
4790 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004791 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004792 return (char_u *)_(e_invrange);
4793 break;
4794 case ADDR_TABS:
4795 if (eap->line2 > LAST_TAB_NR)
4796 return (char_u *)_(e_invrange);
4797 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004798 case ADDR_TABS_RELATIVE:
4799 /* Do nothing */
4800 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004801#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004802 case ADDR_QUICKFIX:
4803 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4804 return (char_u *)_(e_invrange);
4805 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004806#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004807 }
4808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 return NULL;
4810}
4811
4812/*
4813 * Correct the range for zero line number, if required.
4814 */
4815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004816correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817{
4818 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4819 {
4820 if (eap->line1 == 0)
4821 eap->line1 = 1;
4822 if (eap->line2 == 0)
4823 eap->line2 = 1;
4824 }
4825}
4826
Bram Moolenaar748bf032005-02-02 23:04:36 +00004827#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004828static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004829
4830/*
4831 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4832 * pattern. Otherwise return eap->arg.
4833 */
4834 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004835skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004836{
4837 char_u *p = eap->arg;
4838
Bram Moolenaara37420f2006-02-04 22:37:47 +00004839 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4840 || eap->cmdidx == CMD_vimgrepadd
4841 || eap->cmdidx == CMD_lvimgrepadd
4842 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004843 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004844 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004845 if (p == NULL)
4846 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004847 }
4848 return p;
4849}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004850
4851/*
4852 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4853 * in the command line, so that things like % get expanded.
4854 */
4855 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004856replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004857{
4858 char_u *new_cmdline;
4859 char_u *program;
4860 char_u *pos;
4861 char_u *ptr;
4862 int len;
4863 int i;
4864
4865 /*
4866 * Don't do it when ":vimgrep" is used for ":grep".
4867 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004868 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4869 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4870 || eap->cmdidx == CMD_grepadd
4871 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004872 && !grep_internal(eap->cmdidx))
4873 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004874 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4875 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004876 {
4877 if (*curbuf->b_p_gp == NUL)
4878 program = p_gp;
4879 else
4880 program = curbuf->b_p_gp;
4881 }
4882 else
4883 {
4884 if (*curbuf->b_p_mp == NUL)
4885 program = p_mp;
4886 else
4887 program = curbuf->b_p_mp;
4888 }
4889
4890 p = skipwhite(p);
4891
4892 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4893 {
4894 /* replace $* by given arguments */
4895 i = 1;
4896 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4897 ++i;
4898 len = (int)STRLEN(p);
4899 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4900 if (new_cmdline == NULL)
4901 return NULL; /* out of memory */
4902 ptr = new_cmdline;
4903 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4904 {
4905 i = (int)(pos - program);
4906 STRNCPY(ptr, program, i);
4907 STRCPY(ptr += i, p);
4908 ptr += len;
4909 program = pos + 2;
4910 }
4911 STRCPY(ptr, program);
4912 }
4913 else
4914 {
4915 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4916 if (new_cmdline == NULL)
4917 return NULL; /* out of memory */
4918 STRCPY(new_cmdline, program);
4919 STRCAT(new_cmdline, " ");
4920 STRCAT(new_cmdline, p);
4921 }
4922 msg_make(p);
4923
4924 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4925 vim_free(*cmdlinep);
4926 *cmdlinep = new_cmdline;
4927 p = new_cmdline;
4928 }
4929 return p;
4930}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004931#endif
4932
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933/*
4934 * Expand file name in Ex command argument.
4935 * Return FAIL for failure, OK otherwise.
4936 */
4937 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004938expand_filename(
4939 exarg_T *eap,
4940 char_u **cmdlinep,
4941 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942{
4943 int has_wildcards; /* need to expand wildcards */
4944 char_u *repl;
4945 int srclen;
4946 char_u *p;
4947 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004948 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
Bram Moolenaar748bf032005-02-02 23:04:36 +00004950#ifdef FEAT_QUICKFIX
4951 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4952 p = skip_grep_pat(eap);
4953#else
4954 p = eap->arg;
4955#endif
4956
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 /*
4958 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4959 * the file name contains a wildcard it should not cause expanding.
4960 * (it will be expanded anyway if there is a wildcard before replacing).
4961 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004962 has_wildcards = mch_has_wildcard(p);
4963 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004965#ifdef FEAT_EVAL
4966 /* Skip over `=expr`, wildcards in it are not expanded. */
4967 if (p[0] == '`' && p[1] == '=')
4968 {
4969 p += 2;
4970 (void)skip_expr(&p);
4971 if (*p == '`')
4972 ++p;
4973 continue;
4974 }
4975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 /*
4977 * Quick check if this cannot be the start of a special string.
4978 * Also removes backslash before '%', '#' and '<'.
4979 */
4980 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4981 {
4982 ++p;
4983 continue;
4984 }
4985
4986 /*
4987 * Try to find a match at this position.
4988 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004989 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4990 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 if (*errormsgp != NULL) /* error detected */
4992 return FAIL;
4993 if (repl == NULL) /* no match found */
4994 {
4995 p += srclen;
4996 continue;
4997 }
4998
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004999 /* Wildcards won't be expanded below, the replacement is taken
5000 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5001 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5002 {
5003 char_u *l = repl;
5004
5005 repl = expand_env_save(repl);
5006 vim_free(l);
5007 }
5008
Bram Moolenaar63b92542007-03-27 14:57:09 +00005009 /* Need to escape white space et al. with a backslash.
5010 * Don't do this for:
5011 * - replacement that already has been escaped: "##"
5012 * - shell commands (may have to use quotes instead).
5013 * - non-unix systems when there is a single argument (spaces don't
5014 * separate arguments then).
5015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005017 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 && eap->cmdidx != CMD_grep
5020 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005021 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005022 && eap->cmdidx != CMD_lgrep
5023 && eap->cmdidx != CMD_lgrepadd
5024 && eap->cmdidx != CMD_lmake
5025 && eap->cmdidx != CMD_make
5026 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027#ifndef UNIX
5028 && !(eap->argt & NOSPC)
5029#endif
5030 )
5031 {
5032 char_u *l;
5033#ifdef BACKSLASH_IN_FILENAME
5034 /* Don't escape a backslash here, because rem_backslash() doesn't
5035 * remove it later. */
5036 static char_u *nobslash = (char_u *)" \t\"|";
5037# define ESCAPE_CHARS nobslash
5038#else
5039# define ESCAPE_CHARS escape_chars
5040#endif
5041
5042 for (l = repl; *l; ++l)
5043 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5044 {
5045 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5046 if (l != NULL)
5047 {
5048 vim_free(repl);
5049 repl = l;
5050 }
5051 break;
5052 }
5053 }
5054
5055 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005056 if ((eap->usefilter || eap->cmdidx == CMD_bang
5057 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005058 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
5060 char_u *l;
5061
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005062 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 if (l != NULL)
5064 {
5065 vim_free(repl);
5066 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 }
5068 }
5069
5070 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5071 vim_free(repl);
5072 if (p == NULL)
5073 return FAIL;
5074 }
5075
5076 /*
5077 * One file argument: Expand wildcards.
5078 * Don't do this with ":r !command" or ":w !command".
5079 */
5080 if ((eap->argt & NOSPC) && !eap->usefilter)
5081 {
5082 /*
5083 * May do this twice:
5084 * 1. Replace environment variables.
5085 * 2. Replace any other wildcards, remove backslashes.
5086 */
5087 for (n = 1; n <= 2; ++n)
5088 {
5089 if (n == 2)
5090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 /*
5092 * Halve the number of backslashes (this is Vi compatible).
5093 * For Unix and OS/2, when wildcards are expanded, this is
5094 * done by ExpandOne() below.
5095 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005096#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 if (!has_wildcards)
5098#endif
5099 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
5101
5102 if (has_wildcards)
5103 {
5104 if (n == 1)
5105 {
5106 /*
5107 * First loop: May expand environment variables. This
5108 * can be done much faster with expand_env() than with
5109 * something else (e.g., calling a shell).
5110 * After expanding environment variables, check again
5111 * if there are still wildcards present.
5112 */
5113 if (vim_strchr(eap->arg, '$') != NULL
5114 || vim_strchr(eap->arg, '~') != NULL)
5115 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005116 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005117 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 has_wildcards = mch_has_wildcard(NameBuff);
5119 p = NameBuff;
5120 }
5121 else
5122 p = NULL;
5123 }
5124 else /* n == 2 */
5125 {
5126 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005127 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
5129 ExpandInit(&xpc);
5130 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005131 if (p_wic)
5132 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005134 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 if (p == NULL)
5136 return FAIL;
5137 }
5138 if (p != NULL)
5139 {
5140 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5141 p, cmdlinep);
5142 if (n == 2) /* p came from ExpandOne() */
5143 vim_free(p);
5144 }
5145 }
5146 }
5147 }
5148 return OK;
5149}
5150
5151/*
5152 * Replace part of the command line, keeping eap->cmd, eap->arg and
5153 * eap->nextcmd correct.
5154 * "src" points to the part that is to be replaced, of length "srclen".
5155 * "repl" is the replacement string.
5156 * Returns a pointer to the character after the replaced string.
5157 * Returns NULL for failure.
5158 */
5159 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005160repl_cmdline(
5161 exarg_T *eap,
5162 char_u *src,
5163 int srclen,
5164 char_u *repl,
5165 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166{
5167 int len;
5168 int i;
5169 char_u *new_cmdline;
5170
5171 /*
5172 * The new command line is build in new_cmdline[].
5173 * First allocate it.
5174 * Careful: a "+cmd" argument may have been NUL terminated.
5175 */
5176 len = (int)STRLEN(repl);
5177 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005178 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5180 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5181 return NULL; /* out of memory! */
5182
5183 /*
5184 * Copy the stuff before the expanded part.
5185 * Copy the expanded stuff.
5186 * Copy what came after the expanded part.
5187 * Copy the next commands, if there are any.
5188 */
5189 i = (int)(src - *cmdlinep); /* length of part before match */
5190 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005191
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 mch_memmove(new_cmdline + i, repl, (size_t)len);
5193 i += len; /* remember the end of the string */
5194 STRCPY(new_cmdline + i, src + srclen);
5195 src = new_cmdline + i; /* remember where to continue */
5196
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005197 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 {
5199 i = (int)STRLEN(new_cmdline) + 1;
5200 STRCPY(new_cmdline + i, eap->nextcmd);
5201 eap->nextcmd = new_cmdline + i;
5202 }
5203 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5204 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5205 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5206 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5207 vim_free(*cmdlinep);
5208 *cmdlinep = new_cmdline;
5209
5210 return src;
5211}
5212
5213/*
5214 * Check for '|' to separate commands and '"' to start comments.
5215 */
5216 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005217separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218{
5219 char_u *p;
5220
Bram Moolenaar86b68352004-12-27 21:59:20 +00005221#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005222 p = skip_grep_pat(eap);
5223#else
5224 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005225#endif
5226
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005227 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 {
5229 if (*p == Ctrl_V)
5230 {
5231 if (eap->argt & (USECTRLV | XFILE))
5232 ++p; /* skip CTRL-V and next char */
5233 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005234 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005235 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 if (*p == NUL) /* stop at NUL after CTRL-V */
5237 break;
5238 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005239
5240#ifdef FEAT_EVAL
5241 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005242 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005243 {
5244 p += 2;
5245 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005246 }
5247#endif
5248
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 /* Check for '"': start of comment or '|': next command */
5250 /* :@" and :*" do not start a comment!
5251 * :redir @" doesn't either. */
5252 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5253 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5254 || p != eap->arg)
5255 && (eap->cmdidx != CMD_redir
5256 || p != eap->arg + 1 || p[-1] != '@'))
5257 || *p == '|' || *p == '\n')
5258 {
5259 /*
5260 * We remove the '\' before the '|', unless USECTRLV is used
5261 * AND 'b' is present in 'cpoptions'.
5262 */
5263 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5264 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5265 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005266 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 --p;
5268 }
5269 else
5270 {
5271 eap->nextcmd = check_nextcmd(p);
5272 *p = NUL;
5273 break;
5274 }
5275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005277
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5279 del_trailing_spaces(eap->arg);
5280}
5281
5282/*
5283 * get + command from ex argument
5284 */
5285 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005286getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287{
5288 char_u *arg = *argp;
5289 char_u *command = NULL;
5290
5291 if (*arg == '+') /* +[command] */
5292 {
5293 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005294 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 command = dollar_command;
5296 else
5297 {
5298 command = arg;
5299 arg = skip_cmd_arg(command, TRUE);
5300 if (*arg != NUL)
5301 *arg++ = NUL; /* terminate command with NUL */
5302 }
5303
5304 arg = skipwhite(arg); /* skip over spaces */
5305 *argp = arg;
5306 }
5307 return command;
5308}
5309
5310/*
5311 * Find end of "+command" argument. Skip over "\ " and "\\".
5312 */
5313 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005314skip_cmd_arg(
5315 char_u *p,
5316 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 while (*p && !vim_isspace(*p))
5319 {
5320 if (*p == '\\' && p[1] != NUL)
5321 {
5322 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005323 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 else
5325 ++p;
5326 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005327 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
5329 return p;
5330}
5331
Bram Moolenaar86676c92018-04-05 18:56:48 +02005332#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005333 int
5334get_bad_opt(char_u *p, exarg_T *eap)
5335{
5336 if (STRICMP(p, "keep") == 0)
5337 eap->bad_char = BAD_KEEP;
5338 else if (STRICMP(p, "drop") == 0)
5339 eap->bad_char = BAD_DROP;
5340 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5341 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005342 else
5343 return FAIL;
5344 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005345}
Bram Moolenaar86676c92018-04-05 18:56:48 +02005346#endif
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005347
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348/*
5349 * Get "++opt=arg" argument.
5350 * Return FAIL or OK.
5351 */
5352 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005353getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354{
5355 char_u *arg = eap->arg + 2;
5356 int *pp = NULL;
5357#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005358 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 char_u *p;
5360#endif
5361
5362 /* ":edit ++[no]bin[ary] file" */
5363 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5364 {
5365 if (*arg == 'n')
5366 {
5367 arg += 2;
5368 eap->force_bin = FORCE_NOBIN;
5369 }
5370 else
5371 eap->force_bin = FORCE_BIN;
5372 if (!checkforcmd(&arg, "binary", 3))
5373 return FAIL;
5374 eap->arg = skipwhite(arg);
5375 return OK;
5376 }
5377
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005378 /* ":read ++edit file" */
5379 if (STRNCMP(arg, "edit", 4) == 0)
5380 {
5381 eap->read_edit = TRUE;
5382 eap->arg = skipwhite(arg + 4);
5383 return OK;
5384 }
5385
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 if (STRNCMP(arg, "ff", 2) == 0)
5387 {
5388 arg += 2;
5389 pp = &eap->force_ff;
5390 }
5391 else if (STRNCMP(arg, "fileformat", 10) == 0)
5392 {
5393 arg += 10;
5394 pp = &eap->force_ff;
5395 }
5396#ifdef FEAT_MBYTE
5397 else if (STRNCMP(arg, "enc", 3) == 0)
5398 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005399 if (STRNCMP(arg, "encoding", 8) == 0)
5400 arg += 8;
5401 else
5402 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 pp = &eap->force_enc;
5404 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005405 else if (STRNCMP(arg, "bad", 3) == 0)
5406 {
5407 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005408 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410#endif
5411
5412 if (pp == NULL || *arg != '=')
5413 return FAIL;
5414
5415 ++arg;
5416 *pp = (int)(arg - eap->cmd);
5417 arg = skip_cmd_arg(arg, FALSE);
5418 eap->arg = skipwhite(arg);
5419 *arg = NUL;
5420
5421#ifdef FEAT_MBYTE
5422 if (pp == &eap->force_ff)
5423 {
5424#endif
5425 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5426 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005427 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428#ifdef FEAT_MBYTE
5429 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005430 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {
5432 /* Make 'fileencoding' lower case. */
5433 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5434 *p = TOLOWER_ASC(*p);
5435 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005436 else
5437 {
5438 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5439 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005440 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005441 return FAIL;
5442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443#endif
5444
5445 return OK;
5446}
5447
5448/*
5449 * ":abbreviate" and friends.
5450 */
5451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005452ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453{
5454 do_exmap(eap, TRUE); /* almost the same as mapping */
5455}
5456
5457/*
5458 * ":map" and friends.
5459 */
5460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005461ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462{
5463 /*
5464 * If we are sourcing .exrc or .vimrc in current directory we
5465 * print the mappings for security reasons.
5466 */
5467 if (secure)
5468 {
5469 secure = 2;
5470 msg_outtrans(eap->cmd);
5471 msg_putchar('\n');
5472 }
5473 do_exmap(eap, FALSE);
5474}
5475
5476/*
5477 * ":unmap" and friends.
5478 */
5479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005480ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481{
5482 do_exmap(eap, FALSE);
5483}
5484
5485/*
5486 * ":mapclear" and friends.
5487 */
5488 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005489ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490{
5491 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5492}
5493
5494/*
5495 * ":abclear" and friends.
5496 */
5497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005498ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
5500 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5501}
5502
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005504ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
5506 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005507 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 * directory for security reasons.
5509 */
5510 if (secure)
5511 {
5512 secure = 2;
5513 eap->errmsg = e_curdir;
5514 }
5515 else if (eap->cmdidx == CMD_autocmd)
5516 do_autocmd(eap->arg, eap->forceit);
5517 else
5518 do_augroup(eap->arg, eap->forceit);
5519}
5520
5521/*
5522 * ":doautocmd": Apply the automatic commands to the current buffer.
5523 */
5524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005525ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005527 char_u *arg = eap->arg;
5528 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005529 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005530
Bram Moolenaar1610d052016-06-09 22:53:01 +02005531 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5532 /* Only when there is no <nomodeline>. */
5533 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005534 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537/*
5538 * :[N]bunload[!] [N] [bufname] unload buffer
5539 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5540 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5541 */
5542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005543ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 eap->errmsg = do_bufdel(
5546 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5547 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5548 : DOBUF_UNLOAD, eap->arg,
5549 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5550}
5551
5552/*
5553 * :[N]buffer [N] to buffer N
5554 * :[N]sbuffer [N] to buffer N
5555 */
5556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005557ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
5559 if (*eap->arg)
5560 eap->errmsg = e_trailing;
5561 else
5562 {
5563 if (eap->addr_count == 0) /* default is current buffer */
5564 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5565 else
5566 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005567 if (eap->do_ecmd_cmd != NULL)
5568 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
5570}
5571
5572/*
5573 * :[N]bmodified [N] to next mod. buffer
5574 * :[N]sbmodified [N] to next mod. buffer
5575 */
5576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005580 if (eap->do_ecmd_cmd != NULL)
5581 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582}
5583
5584/*
5585 * :[N]bnext [N] to next buffer
5586 * :[N]sbnext [N] split and to next buffer
5587 */
5588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005589ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
5591 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005592 if (eap->do_ecmd_cmd != NULL)
5593 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594}
5595
5596/*
5597 * :[N]bNext [N] to previous buffer
5598 * :[N]bprevious [N] to previous buffer
5599 * :[N]sbNext [N] split and to previous buffer
5600 * :[N]sbprevious [N] split and to previous buffer
5601 */
5602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005603ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604{
5605 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005606 if (eap->do_ecmd_cmd != NULL)
5607 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608}
5609
5610/*
5611 * :brewind to first buffer
5612 * :bfirst to first buffer
5613 * :sbrewind split and to first buffer
5614 * :sbfirst split and to first buffer
5615 */
5616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005617ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005620 if (eap->do_ecmd_cmd != NULL)
5621 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622}
5623
5624/*
5625 * :blast to last buffer
5626 * :sblast split and to last buffer
5627 */
5628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005629ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
5631 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005632 if (eap->do_ecmd_cmd != NULL)
5633 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
5636 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005637ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638{
5639 return (c == NUL || c == '|' || c == '"' || c == '\n');
5640}
5641
5642#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5643 || defined(PROTO)
5644/*
5645 * Return the next command, after the first '|' or '\n'.
5646 * Return NULL if not found.
5647 */
5648 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005649find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650{
5651 while (*p != '|' && *p != '\n')
5652 {
5653 if (*p == NUL)
5654 return NULL;
5655 ++p;
5656 }
5657 return (p + 1);
5658}
5659#endif
5660
5661/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005662 * Check if *p is a separator between Ex commands, skipping over white space.
5663 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 */
5665 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005666check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005668 char_u *s = skipwhite(p);
5669
5670 if (*s == '|' || *s == '\n')
5671 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 else
5673 return NULL;
5674}
5675
5676/*
5677 * - if there are more files to edit
5678 * - and this is the last window
5679 * - and forceit not used
5680 * - and not repeated twice on a row
5681 * return FAIL and give error message if 'message' TRUE
5682 * return OK otherwise
5683 */
5684 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005685check_more(
5686 int message, /* when FALSE check only, no messages */
5687 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
5689 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5690
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005691 if (!forceit && only_one_window()
5692 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 {
5694 if (message)
5695 {
5696#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5697 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5698 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005699 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700
5701 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005702 vim_strncpy(buff,
5703 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005704 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005706 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 _("%d more files to edit. Quit anyway?"), n);
5708 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5709 return OK;
5710 return FAIL;
5711 }
5712#endif
5713 if (n == 1)
5714 EMSG(_("E173: 1 more file to edit"));
5715 else
5716 EMSGN(_("E173: %ld more files to edit"), n);
5717 quitmore = 2; /* next try to quit is allowed */
5718 }
5719 return FAIL;
5720 }
5721 return OK;
5722}
5723
5724#ifdef FEAT_CMDL_COMPL
5725/*
5726 * Function given to ExpandGeneric() to obtain the list of command names.
5727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005729get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
5731 if (idx >= (int)CMD_SIZE)
5732# ifdef FEAT_USR_CMDS
5733 return get_user_command_name(idx);
5734# else
5735 return NULL;
5736# endif
5737 return cmdnames[idx].cmd_name;
5738}
5739#endif
5740
5741#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005742static 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);
5743static void uc_list(char_u *name, size_t name_len);
5744static 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);
5745static char_u *uc_split_args(char_u *arg, size_t *lenp);
5746static 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 +00005747
5748 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005749uc_add_command(
5750 char_u *name,
5751 size_t name_len,
5752 char_u *rep,
5753 long argt,
5754 long def,
5755 int flags,
5756 int compl,
5757 char_u *compl_arg,
5758 int addr_type,
5759 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760{
5761 ucmd_T *cmd = NULL;
5762 char_u *p;
5763 int i;
5764 int cmp = 1;
5765 char_u *rep_buf = NULL;
5766 garray_T *gap;
5767
Bram Moolenaar9c102382006-05-03 21:26:49 +00005768 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 if (rep_buf == NULL)
5770 {
5771 /* Can't replace termcodes - try using the string as is */
5772 rep_buf = vim_strsave(rep);
5773
5774 /* Give up if out of memory */
5775 if (rep_buf == NULL)
5776 return FAIL;
5777 }
5778
5779 /* get address of growarray: global or in curbuf */
5780 if (flags & UC_BUFFER)
5781 {
5782 gap = &curbuf->b_ucmds;
5783 if (gap->ga_itemsize == 0)
5784 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5785 }
5786 else
5787 gap = &ucmds;
5788
5789 /* Search for the command in the already defined commands. */
5790 for (i = 0; i < gap->ga_len; ++i)
5791 {
5792 size_t len;
5793
5794 cmd = USER_CMD_GA(gap, i);
5795 len = STRLEN(cmd->uc_name);
5796 cmp = STRNCMP(name, cmd->uc_name, name_len);
5797 if (cmp == 0)
5798 {
5799 if (name_len < len)
5800 cmp = -1;
5801 else if (name_len > len)
5802 cmp = 1;
5803 }
5804
5805 if (cmp == 0)
5806 {
5807 if (!force)
5808 {
5809 EMSG(_("E174: Command already exists: add ! to replace it"));
5810 goto fail;
5811 }
5812
Bram Moolenaard23a8232018-02-10 18:45:26 +01005813 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005814#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005815 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 break;
5818 }
5819
5820 /* Stop as soon as we pass the name to add */
5821 if (cmp < 0)
5822 break;
5823 }
5824
5825 /* Extend the array unless we're replacing an existing command */
5826 if (cmp != 0)
5827 {
5828 if (ga_grow(gap, 1) != OK)
5829 goto fail;
5830 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5831 goto fail;
5832
5833 cmd = USER_CMD_GA(gap, i);
5834 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5835
5836 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837
5838 cmd->uc_name = p;
5839 }
5840
5841 cmd->uc_rep = rep_buf;
5842 cmd->uc_argt = argt;
5843 cmd->uc_def = def;
5844 cmd->uc_compl = compl;
5845#ifdef FEAT_EVAL
5846 cmd->uc_scriptID = current_SID;
5847# ifdef FEAT_CMDL_COMPL
5848 cmd->uc_compl_arg = compl_arg;
5849# endif
5850#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005851 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
5853 return OK;
5854
5855fail:
5856 vim_free(rep_buf);
5857#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5858 vim_free(compl_arg);
5859#endif
5860 return FAIL;
5861}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005864#if defined(FEAT_USR_CMDS)
5865static struct
5866{
5867 int expand;
5868 char *name;
5869} addr_type_complete[] =
5870{
5871 {ADDR_ARGUMENTS, "arguments"},
5872 {ADDR_LINES, "lines"},
5873 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5874 {ADDR_TABS, "tabs"},
5875 {ADDR_BUFFERS, "buffers"},
5876 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005877 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005878 {-1, NULL}
5879};
5880#endif
5881
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005882#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883/*
5884 * List of names for completion for ":command" with the EXPAND_ flag.
5885 * Must be alphabetical for completion.
5886 */
5887static struct
5888{
5889 int expand;
5890 char *name;
5891} command_complete[] =
5892{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005893 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005895 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005897 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005899 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005900#if defined(FEAT_CSCOPE)
5901 {EXPAND_CSCOPE, "cscope"},
5902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5904 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005905 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906#endif
5907 {EXPAND_DIRECTORIES, "dir"},
5908 {EXPAND_ENV_VARS, "environment"},
5909 {EXPAND_EVENTS, "event"},
5910 {EXPAND_EXPRESSION, "expression"},
5911 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005912 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005913 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 {EXPAND_FUNCTIONS, "function"},
5915 {EXPAND_HELP, "help"},
5916 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005917#if defined(FEAT_CMDHIST)
5918 {EXPAND_HISTORY, "history"},
5919#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005920#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005921 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005922 {EXPAND_LOCALES, "locale"},
5923#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005924 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {EXPAND_MAPPINGS, "mapping"},
5926 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005927 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005928 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005929#if defined(FEAT_PROFILE)
5930 {EXPAND_SYNTIME, "syntime"},
5931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005933 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005934 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005935#if defined(FEAT_SIGNS)
5936 {EXPAND_SIGN, "sign"},
5937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 {EXPAND_TAGS, "tag"},
5939 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005940 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 {EXPAND_USER_VARS, "var"},
5942 {0, NULL}
5943};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005946#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005948uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949{
5950 int i, j;
5951 int found = FALSE;
5952 ucmd_T *cmd;
5953 int len;
5954 long a;
5955 garray_T *gap;
5956
5957 gap = &curbuf->b_ucmds;
5958 for (;;)
5959 {
5960 for (i = 0; i < gap->ga_len; ++i)
5961 {
5962 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005963 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964
Bram Moolenaard29459b2016-08-26 22:29:11 +02005965 /* Skip commands which don't match the requested prefix and
5966 * commands filtered out. */
5967 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5968 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 continue;
5970
5971 /* Put out the title first time */
5972 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005973 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 found = TRUE;
5975 msg_putchar('\n');
5976 if (got_int)
5977 break;
5978
5979 /* Special cases */
5980 msg_putchar(a & BANG ? '!' : ' ');
5981 msg_putchar(a & REGSTR ? '"' : ' ');
5982 msg_putchar(gap != &ucmds ? 'b' : ' ');
5983 msg_putchar(' ');
5984
Bram Moolenaar8820b482017-03-16 17:23:31 +01005985 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 len = (int)STRLEN(cmd->uc_name) + 4;
5987
5988 do {
5989 msg_putchar(' ');
5990 ++len;
5991 } while (len < 16);
5992
5993 len = 0;
5994
5995 /* Arguments */
5996 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5997 {
5998 case 0: IObuff[len++] = '0'; break;
5999 case (EXTRA): IObuff[len++] = '*'; break;
6000 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6001 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6002 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6003 }
6004
6005 do {
6006 IObuff[len++] = ' ';
6007 } while (len < 5);
6008
6009 /* Range */
6010 if (a & (RANGE|COUNT))
6011 {
6012 if (a & COUNT)
6013 {
6014 /* -count=N */
6015 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6016 len += (int)STRLEN(IObuff + len);
6017 }
6018 else if (a & DFLALL)
6019 IObuff[len++] = '%';
6020 else if (cmd->uc_def >= 0)
6021 {
6022 /* -range=N */
6023 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6024 len += (int)STRLEN(IObuff + len);
6025 }
6026 else
6027 IObuff[len++] = '.';
6028 }
6029
6030 do {
6031 IObuff[len++] = ' ';
6032 } while (len < 11);
6033
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006034 /* Address Type */
6035 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6036 if (addr_type_complete[j].expand != ADDR_LINES
6037 && addr_type_complete[j].expand == cmd->uc_addr_type)
6038 {
6039 STRCPY(IObuff + len, addr_type_complete[j].name);
6040 len += (int)STRLEN(IObuff + len);
6041 break;
6042 }
6043
6044 do {
6045 IObuff[len++] = ' ';
6046 } while (len < 21);
6047
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 /* Completion */
6049 for (j = 0; command_complete[j].expand != 0; ++j)
6050 if (command_complete[j].expand == cmd->uc_compl)
6051 {
6052 STRCPY(IObuff + len, command_complete[j].name);
6053 len += (int)STRLEN(IObuff + len);
6054 break;
6055 }
6056
6057 do {
6058 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006059 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060
6061 IObuff[len] = '\0';
6062 msg_outtrans(IObuff);
6063
6064 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006065#ifdef FEAT_EVAL
6066 if (p_verbose > 0)
6067 last_set_msg(cmd->uc_scriptID);
6068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 out_flush();
6070 ui_breakcheck();
6071 if (got_int)
6072 break;
6073 }
6074 if (gap == &ucmds || i < gap->ga_len)
6075 break;
6076 gap = &ucmds;
6077 }
6078
6079 if (!found)
6080 MSG(_("No user-defined commands found"));
6081}
6082
6083 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006084uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085{
6086 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6087 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6088 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6089 0xb9, 0x7f, 0};
6090 int i;
6091
6092 for (i = 0; fcmd[i]; ++i)
6093 IObuff[i] = fcmd[i] - 0x40;
6094 IObuff[i] = 0;
6095 return IObuff;
6096}
6097
6098 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006099uc_scan_attr(
6100 char_u *attr,
6101 size_t len,
6102 long *argt,
6103 long *def,
6104 int *flags,
6105 int *compl,
6106 char_u **compl_arg,
6107 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108{
6109 char_u *p;
6110
6111 if (len == 0)
6112 {
6113 EMSG(_("E175: No attribute specified"));
6114 return FAIL;
6115 }
6116
6117 /* First, try the simple attributes (no arguments) */
6118 if (STRNICMP(attr, "bang", len) == 0)
6119 *argt |= BANG;
6120 else if (STRNICMP(attr, "buffer", len) == 0)
6121 *flags |= UC_BUFFER;
6122 else if (STRNICMP(attr, "register", len) == 0)
6123 *argt |= REGSTR;
6124 else if (STRNICMP(attr, "bar", len) == 0)
6125 *argt |= TRLBAR;
6126 else
6127 {
6128 int i;
6129 char_u *val = NULL;
6130 size_t vallen = 0;
6131 size_t attrlen = len;
6132
6133 /* Look for the attribute name - which is the part before any '=' */
6134 for (i = 0; i < (int)len; ++i)
6135 {
6136 if (attr[i] == '=')
6137 {
6138 val = &attr[i + 1];
6139 vallen = len - i - 1;
6140 attrlen = i;
6141 break;
6142 }
6143 }
6144
6145 if (STRNICMP(attr, "nargs", attrlen) == 0)
6146 {
6147 if (vallen == 1)
6148 {
6149 if (*val == '0')
6150 /* Do nothing - this is the default */;
6151 else if (*val == '1')
6152 *argt |= (EXTRA | NOSPC | NEEDARG);
6153 else if (*val == '*')
6154 *argt |= EXTRA;
6155 else if (*val == '?')
6156 *argt |= (EXTRA | NOSPC);
6157 else if (*val == '+')
6158 *argt |= (EXTRA | NEEDARG);
6159 else
6160 goto wrong_nargs;
6161 }
6162 else
6163 {
6164wrong_nargs:
6165 EMSG(_("E176: Invalid number of arguments"));
6166 return FAIL;
6167 }
6168 }
6169 else if (STRNICMP(attr, "range", attrlen) == 0)
6170 {
6171 *argt |= RANGE;
6172 if (vallen == 1 && *val == '%')
6173 *argt |= DFLALL;
6174 else if (val != NULL)
6175 {
6176 p = val;
6177 if (*def >= 0)
6178 {
6179two_count:
6180 EMSG(_("E177: Count cannot be specified twice"));
6181 return FAIL;
6182 }
6183
6184 *def = getdigits(&p);
6185 *argt |= (ZEROR | NOTADR);
6186
6187 if (p != val + vallen || vallen == 0)
6188 {
6189invalid_count:
6190 EMSG(_("E178: Invalid default value for count"));
6191 return FAIL;
6192 }
6193 }
6194 }
6195 else if (STRNICMP(attr, "count", attrlen) == 0)
6196 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006197 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198
6199 if (val != NULL)
6200 {
6201 p = val;
6202 if (*def >= 0)
6203 goto two_count;
6204
6205 *def = getdigits(&p);
6206
6207 if (p != val + vallen)
6208 goto invalid_count;
6209 }
6210
6211 if (*def < 0)
6212 *def = 0;
6213 }
6214 else if (STRNICMP(attr, "complete", attrlen) == 0)
6215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 if (val == NULL)
6217 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006218 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 return FAIL;
6220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006222 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6223 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006226 else if (STRNICMP(attr, "addr", attrlen) == 0)
6227 {
6228 *argt |= RANGE;
6229 if (val == NULL)
6230 {
6231 EMSG(_("E179: argument required for -addr"));
6232 return FAIL;
6233 }
6234 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6235 == FAIL)
6236 return FAIL;
6237 if (addr_type_arg != ADDR_LINES)
6238 *argt |= (ZEROR | NOTADR) ;
6239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 else
6241 {
6242 char_u ch = attr[len];
6243 attr[len] = '\0';
6244 EMSG2(_("E181: Invalid attribute: %s"), attr);
6245 attr[len] = ch;
6246 return FAIL;
6247 }
6248 }
6249
6250 return OK;
6251}
6252
Bram Moolenaara850a712009-01-28 14:42:59 +00006253/*
6254 * ":command ..."
6255 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006257ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258{
6259 char_u *name;
6260 char_u *end;
6261 char_u *p;
6262 long argt = 0;
6263 long def = -1;
6264 int flags = 0;
6265 int compl = EXPAND_NOTHING;
6266 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006267 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006269 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270
6271 p = eap->arg;
6272
6273 /* Check for attributes */
6274 while (*p == '-')
6275 {
6276 ++p;
6277 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006278 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6279 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 == FAIL)
6281 return;
6282 p = skipwhite(end);
6283 }
6284
6285 /* Get the name (if any) and skip to the following argument */
6286 name = p;
6287 if (ASCII_ISALPHA(*p))
6288 while (ASCII_ISALNUM(*p))
6289 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006290 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 {
6292 EMSG(_("E182: Invalid command name"));
6293 return;
6294 }
6295 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006296 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297
6298 /* If there is nothing after the name, and no attributes were specified,
6299 * we are listing commands
6300 */
6301 p = skipwhite(end);
6302 if (!has_attr && ends_excmd(*p))
6303 {
6304 uc_list(name, end - name);
6305 }
6306 else if (!ASCII_ISUPPER(*name))
6307 {
6308 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6309 return;
6310 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006311 else if ((name_len == 1 && *name == 'X')
6312 || (name_len <= 4
6313 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6314 {
6315 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6316 return;
6317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 else
6319 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006320 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321}
6322
6323/*
6324 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 * Clear all user commands, global and for current buffer.
6326 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006327 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006328ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329{
6330 uc_clear(&ucmds);
6331 uc_clear(&curbuf->b_ucmds);
6332}
6333
6334/*
6335 * Clear all user commands for "gap".
6336 */
6337 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006338uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
6340 int i;
6341 ucmd_T *cmd;
6342
6343 for (i = 0; i < gap->ga_len; ++i)
6344 {
6345 cmd = USER_CMD_GA(gap, i);
6346 vim_free(cmd->uc_name);
6347 vim_free(cmd->uc_rep);
6348# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6349 vim_free(cmd->uc_compl_arg);
6350# endif
6351 }
6352 ga_clear(gap);
6353}
6354
6355 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006356ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357{
6358 int i = 0;
6359 ucmd_T *cmd = NULL;
6360 int cmp = -1;
6361 garray_T *gap;
6362
6363 gap = &curbuf->b_ucmds;
6364 for (;;)
6365 {
6366 for (i = 0; i < gap->ga_len; ++i)
6367 {
6368 cmd = USER_CMD_GA(gap, i);
6369 cmp = STRCMP(eap->arg, cmd->uc_name);
6370 if (cmp <= 0)
6371 break;
6372 }
6373 if (gap == &ucmds || cmp == 0)
6374 break;
6375 gap = &ucmds;
6376 }
6377
6378 if (cmp != 0)
6379 {
6380 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6381 return;
6382 }
6383
6384 vim_free(cmd->uc_name);
6385 vim_free(cmd->uc_rep);
6386# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6387 vim_free(cmd->uc_compl_arg);
6388# endif
6389
6390 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391
6392 if (i < gap->ga_len)
6393 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6394}
6395
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006396/*
6397 * split and quote args for <f-args>
6398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006400uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401{
6402 char_u *buf;
6403 char_u *p;
6404 char_u *q;
6405 int len;
6406
6407 /* Precalculate length */
6408 p = arg;
6409 len = 2; /* Initial and final quotes */
6410
6411 while (*p)
6412 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006413 if (p[0] == '\\' && p[1] == '\\')
6414 {
6415 len += 2;
6416 p += 2;
6417 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006418 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 {
6420 len += 1;
6421 p += 2;
6422 }
6423 else if (*p == '\\' || *p == '"')
6424 {
6425 len += 2;
6426 p += 1;
6427 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006428 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 {
6430 p = skipwhite(p);
6431 if (*p == NUL)
6432 break;
6433 len += 3; /* "," */
6434 }
6435 else
6436 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006437#ifdef FEAT_MBYTE
6438 int charlen = (*mb_ptr2len)(p);
6439 len += charlen;
6440 p += charlen;
6441#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 ++len;
6443 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 }
6446 }
6447
6448 buf = alloc(len + 1);
6449 if (buf == NULL)
6450 {
6451 *lenp = 0;
6452 return buf;
6453 }
6454
6455 p = arg;
6456 q = buf;
6457 *q++ = '"';
6458 while (*p)
6459 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006460 if (p[0] == '\\' && p[1] == '\\')
6461 {
6462 *q++ = '\\';
6463 *q++ = '\\';
6464 p += 2;
6465 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006466 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 {
6468 *q++ = p[1];
6469 p += 2;
6470 }
6471 else if (*p == '\\' || *p == '"')
6472 {
6473 *q++ = '\\';
6474 *q++ = *p++;
6475 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006476 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 {
6478 p = skipwhite(p);
6479 if (*p == NUL)
6480 break;
6481 *q++ = '"';
6482 *q++ = ',';
6483 *q++ = '"';
6484 }
6485 else
6486 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006487 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 }
6489 }
6490 *q++ = '"';
6491 *q = 0;
6492
6493 *lenp = len;
6494 return buf;
6495}
6496
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006497 static size_t
6498add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6499{
6500 size_t result;
6501
6502 result = STRLEN(mod_str);
6503 if (*multi_mods)
6504 result += 1;
6505 if (buf != NULL)
6506 {
6507 if (*multi_mods)
6508 STRCAT(buf, " ");
6509 STRCAT(buf, mod_str);
6510 }
6511
6512 *multi_mods = 1;
6513
6514 return result;
6515}
6516
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517/*
6518 * Check for a <> code in a user command.
6519 * "code" points to the '<'. "len" the length of the <> (inclusive).
6520 * "buf" is where the result is to be added.
6521 * "split_buf" points to a buffer used for splitting, caller should free it.
6522 * "split_len" is the length of what "split_buf" contains.
6523 * Returns the length of the replacement, which has been added to "buf".
6524 * Returns -1 if there was no match, and only the "<" has been copied.
6525 */
6526 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006527uc_check_code(
6528 char_u *code,
6529 size_t len,
6530 char_u *buf,
6531 ucmd_T *cmd, /* the user command we're expanding */
6532 exarg_T *eap, /* ex arguments */
6533 char_u **split_buf,
6534 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535{
6536 size_t result = 0;
6537 char_u *p = code + 1;
6538 size_t l = len - 2;
6539 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006540 enum {
6541 ct_ARGS,
6542 ct_BANG,
6543 ct_COUNT,
6544 ct_LINE1,
6545 ct_LINE2,
6546 ct_RANGE,
6547 ct_MODS,
6548 ct_REGISTER,
6549 ct_LT,
6550 ct_NONE
6551 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552
6553 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6554 {
6555 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6556 p += 2;
6557 l -= 2;
6558 }
6559
Bram Moolenaar371d5402006-03-20 21:47:49 +00006560 ++l;
6561 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006563 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006565 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006567 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006569 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006571 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006573 else if (STRNICMP(p, "range>", l) == 0)
6574 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006575 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006577 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006579 else if (STRNICMP(p, "mods>", l) == 0)
6580 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581
6582 switch (type)
6583 {
6584 case ct_ARGS:
6585 /* Simple case first */
6586 if (*eap->arg == NUL)
6587 {
6588 if (quote == 1)
6589 {
6590 result = 2;
6591 if (buf != NULL)
6592 STRCPY(buf, "''");
6593 }
6594 else
6595 result = 0;
6596 break;
6597 }
6598
6599 /* When specified there is a single argument don't split it.
6600 * Works for ":Cmd %" when % is "a b c". */
6601 if ((eap->argt & NOSPC) && quote == 2)
6602 quote = 1;
6603
6604 switch (quote)
6605 {
6606 case 0: /* No quoting, no splitting */
6607 result = STRLEN(eap->arg);
6608 if (buf != NULL)
6609 STRCPY(buf, eap->arg);
6610 break;
6611 case 1: /* Quote, but don't split */
6612 result = STRLEN(eap->arg) + 2;
6613 for (p = eap->arg; *p; ++p)
6614 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006615#ifdef FEAT_MBYTE
6616 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6617 /* DBCS can contain \ in a trail byte, skip the
6618 * double-byte character. */
6619 ++p;
6620 else
6621#endif
6622 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 ++result;
6624 }
6625
6626 if (buf != NULL)
6627 {
6628 *buf++ = '"';
6629 for (p = eap->arg; *p; ++p)
6630 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006631#ifdef FEAT_MBYTE
6632 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6633 /* DBCS can contain \ in a trail byte, copy the
6634 * double-byte character to avoid escaping. */
6635 *buf++ = *p++;
6636 else
6637#endif
6638 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 *buf++ = '\\';
6640 *buf++ = *p;
6641 }
6642 *buf = '"';
6643 }
6644
6645 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006646 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 /* This is hard, so only do it once, and cache the result */
6648 if (*split_buf == NULL)
6649 *split_buf = uc_split_args(eap->arg, split_len);
6650
6651 result = *split_len;
6652 if (buf != NULL && result != 0)
6653 STRCPY(buf, *split_buf);
6654
6655 break;
6656 }
6657 break;
6658
6659 case ct_BANG:
6660 result = eap->forceit ? 1 : 0;
6661 if (quote)
6662 result += 2;
6663 if (buf != NULL)
6664 {
6665 if (quote)
6666 *buf++ = '"';
6667 if (eap->forceit)
6668 *buf++ = '!';
6669 if (quote)
6670 *buf = '"';
6671 }
6672 break;
6673
6674 case ct_LINE1:
6675 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006676 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 case ct_COUNT:
6678 {
6679 char num_buf[20];
6680 long num = (type == ct_LINE1) ? eap->line1 :
6681 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006682 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6684 size_t num_len;
6685
6686 sprintf(num_buf, "%ld", num);
6687 num_len = STRLEN(num_buf);
6688 result = num_len;
6689
6690 if (quote)
6691 result += 2;
6692
6693 if (buf != NULL)
6694 {
6695 if (quote)
6696 *buf++ = '"';
6697 STRCPY(buf, num_buf);
6698 buf += num_len;
6699 if (quote)
6700 *buf = '"';
6701 }
6702
6703 break;
6704 }
6705
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006706 case ct_MODS:
6707 {
6708 int multi_mods = 0;
6709 typedef struct {
6710 int *varp;
6711 char *name;
6712 } mod_entry_T;
6713 static mod_entry_T mod_entries[] = {
6714#ifdef FEAT_BROWSE_CMD
6715 {&cmdmod.browse, "browse"},
6716#endif
6717#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6718 {&cmdmod.confirm, "confirm"},
6719#endif
6720 {&cmdmod.hide, "hide"},
6721 {&cmdmod.keepalt, "keepalt"},
6722 {&cmdmod.keepjumps, "keepjumps"},
6723 {&cmdmod.keepmarks, "keepmarks"},
6724 {&cmdmod.keeppatterns, "keeppatterns"},
6725 {&cmdmod.lockmarks, "lockmarks"},
6726 {&cmdmod.noswapfile, "noswapfile"},
6727 {NULL, NULL}
6728 };
6729 int i;
6730
6731 result = quote ? 2 : 0;
6732 if (buf != NULL)
6733 {
6734 if (quote)
6735 *buf++ = '"';
6736 *buf = '\0';
6737 }
6738
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006739 /* :aboveleft and :leftabove */
6740 if (cmdmod.split & WSP_ABOVE)
6741 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6742 /* :belowright and :rightbelow */
6743 if (cmdmod.split & WSP_BELOW)
6744 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6745 /* :botright */
6746 if (cmdmod.split & WSP_BOT)
6747 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006748
6749 /* the modifiers that are simple flags */
6750 for (i = 0; mod_entries[i].varp != NULL; ++i)
6751 if (*mod_entries[i].varp)
6752 result += add_cmd_modifier(buf, mod_entries[i].name,
6753 &multi_mods);
6754
6755 /* TODO: How to support :noautocmd? */
6756#ifdef HAVE_SANDBOX
6757 /* TODO: How to support :sandbox?*/
6758#endif
6759 /* :silent */
6760 if (msg_silent > 0)
6761 result += add_cmd_modifier(buf,
6762 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006763 /* :tab */
6764 if (cmdmod.tab > 0)
6765 result += add_cmd_modifier(buf, "tab", &multi_mods);
6766 /* :topleft */
6767 if (cmdmod.split & WSP_TOP)
6768 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006769 /* TODO: How to support :unsilent?*/
6770 /* :verbose */
6771 if (p_verbose > 0)
6772 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006773 /* :vertical */
6774 if (cmdmod.split & WSP_VERT)
6775 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006776 if (quote && buf != NULL)
6777 {
6778 buf += result - 2;
6779 *buf = '"';
6780 }
6781 break;
6782 }
6783
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 case ct_REGISTER:
6785 result = eap->regname ? 1 : 0;
6786 if (quote)
6787 result += 2;
6788 if (buf != NULL)
6789 {
6790 if (quote)
6791 *buf++ = '\'';
6792 if (eap->regname)
6793 *buf++ = eap->regname;
6794 if (quote)
6795 *buf = '\'';
6796 }
6797 break;
6798
6799 case ct_LT:
6800 result = 1;
6801 if (buf != NULL)
6802 *buf = '<';
6803 break;
6804
6805 default:
6806 /* Not recognized: just copy the '<' and return -1. */
6807 result = (size_t)-1;
6808 if (buf != NULL)
6809 *buf = '<';
6810 break;
6811 }
6812
6813 return result;
6814}
6815
6816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006817do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818{
6819 char_u *buf;
6820 char_u *p;
6821 char_u *q;
6822
6823 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006824 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006825 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 size_t len, totlen;
6827
6828 size_t split_len = 0;
6829 char_u *split_buf = NULL;
6830 ucmd_T *cmd;
6831#ifdef FEAT_EVAL
6832 scid_T save_current_SID = current_SID;
6833#endif
6834
6835 if (eap->cmdidx == CMD_USER)
6836 cmd = USER_CMD(eap->useridx);
6837 else
6838 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6839
6840 /*
6841 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006842 * First round: "buf" is NULL, compute length, allocate "buf".
6843 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 */
6845 buf = NULL;
6846 for (;;)
6847 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006848 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006849 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006851
6852 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006854 start = vim_strchr(p, '<');
6855 if (start != NULL)
6856 end = vim_strchr(start + 1, '>');
6857 if (buf != NULL)
6858 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006859 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6860 ;
6861 if (*ksp == K_SPECIAL
6862 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006863 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6864# ifdef FEAT_GUI
6865 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6866# endif
6867 ))
6868 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006869 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006870 * KS_SPECIAL KE_FILLER, like for mappings, but
6871 * do_cmdline() doesn't handle that, so convert it back.
6872 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6873 len = ksp - p;
6874 if (len > 0)
6875 {
6876 mch_memmove(q, p, len);
6877 q += len;
6878 }
6879 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6880 p = ksp + 3;
6881 continue;
6882 }
6883 }
6884
6885 /* break if there no <item> is found */
6886 if (start == NULL || end == NULL)
6887 break;
6888
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 /* Include the '>' */
6890 ++end;
6891
6892 /* Take everything up to the '<' */
6893 len = start - p;
6894 if (buf == NULL)
6895 totlen += len;
6896 else
6897 {
6898 mch_memmove(q, p, len);
6899 q += len;
6900 }
6901
6902 len = uc_check_code(start, end - start, q, cmd, eap,
6903 &split_buf, &split_len);
6904 if (len == (size_t)-1)
6905 {
6906 /* no match, continue after '<' */
6907 p = start + 1;
6908 len = 1;
6909 }
6910 else
6911 p = end;
6912 if (buf == NULL)
6913 totlen += len;
6914 else
6915 q += len;
6916 }
6917 if (buf != NULL) /* second time here, finished */
6918 {
6919 STRCPY(q, p);
6920 break;
6921 }
6922
6923 totlen += STRLEN(p); /* Add on the trailing characters */
6924 buf = alloc((unsigned)(totlen + 1));
6925 if (buf == NULL)
6926 {
6927 vim_free(split_buf);
6928 return;
6929 }
6930 }
6931
6932#ifdef FEAT_EVAL
6933 current_SID = cmd->uc_scriptID;
6934#endif
6935 (void)do_cmdline(buf, eap->getline, eap->cookie,
6936 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6937#ifdef FEAT_EVAL
6938 current_SID = save_current_SID;
6939#endif
6940 vim_free(buf);
6941 vim_free(split_buf);
6942}
6943
6944# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6945 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006946get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947{
6948 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6949}
6950
6951/*
6952 * Function given to ExpandGeneric() to obtain the list of user command names.
6953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006955get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956{
6957 if (idx < curbuf->b_ucmds.ga_len)
6958 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6959 idx -= curbuf->b_ucmds.ga_len;
6960 if (idx < ucmds.ga_len)
6961 return USER_CMD(idx)->uc_name;
6962 return NULL;
6963}
6964
6965/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006966 * Function given to ExpandGeneric() to obtain the list of user address type names.
6967 */
6968 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006969get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006970{
6971 return (char_u *)addr_type_complete[idx].name;
6972}
6973
6974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 * Function given to ExpandGeneric() to obtain the list of user command
6976 * attributes.
6977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006979get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980{
6981 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006982 {"addr", "bang", "bar", "buffer", "complete",
6983 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006985 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 return NULL;
6987 return (char_u *)user_cmd_flags[idx];
6988}
6989
6990/*
6991 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006994get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
6996 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6997
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006998 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 return NULL;
7000 return (char_u *)user_cmd_nargs[idx];
7001}
7002
7003/*
7004 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007007get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008{
7009 return (char_u *)command_complete[idx].name;
7010}
7011# endif /* FEAT_CMDL_COMPL */
7012
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007013/*
7014 * Parse address type argument
7015 */
7016 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007017parse_addr_type_arg(
7018 char_u *value,
7019 int vallen,
7020 long *argt,
7021 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007022{
7023 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007024
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007025 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7026 {
7027 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7028 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7029 if (a && b)
7030 {
7031 *addr_type_arg = addr_type_complete[i].expand;
7032 break;
7033 }
7034 }
7035
7036 if (addr_type_complete[i].expand == -1)
7037 {
7038 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007039
Bram Moolenaar1c465442017-03-12 20:10:05 +01007040 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007041 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007042 err[i] = NUL;
7043 EMSG2(_("E180: Invalid address type value: %s"), err);
7044 return FAIL;
7045 }
7046
7047 if (*addr_type_arg != ADDR_LINES)
7048 *argt |= NOTADR;
7049
7050 return OK;
7051}
7052
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053#endif /* FEAT_USR_CMDS */
7054
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007055#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7056/*
7057 * Parse a completion argument "value[vallen]".
7058 * The detected completion goes in "*complp", argument type in "*argt".
7059 * When there is an argument, for function and user defined completion, it's
7060 * copied to allocated memory and stored in "*compl_arg".
7061 * Returns FAIL if something is wrong.
7062 */
7063 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007064parse_compl_arg(
7065 char_u *value,
7066 int vallen,
7067 int *complp,
7068 long *argt,
7069 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007070{
7071 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007072# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007073 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007074# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007075 int i;
7076 int valend = vallen;
7077
7078 /* Look for any argument part - which is the part after any ',' */
7079 for (i = 0; i < vallen; ++i)
7080 {
7081 if (value[i] == ',')
7082 {
7083 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007084# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007085 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007086# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007087 valend = i;
7088 break;
7089 }
7090 }
7091
7092 for (i = 0; command_complete[i].expand != 0; ++i)
7093 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007094 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007095 && STRNCMP(value, command_complete[i].name, valend) == 0)
7096 {
7097 *complp = command_complete[i].expand;
7098 if (command_complete[i].expand == EXPAND_BUFFERS)
7099 *argt |= BUFNAME;
7100 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7101 || command_complete[i].expand == EXPAND_FILES)
7102 *argt |= XFILE;
7103 break;
7104 }
7105 }
7106
7107 if (command_complete[i].expand == 0)
7108 {
7109 EMSG2(_("E180: Invalid complete value: %s"), value);
7110 return FAIL;
7111 }
7112
7113# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7114 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7115 && arg != NULL)
7116# else
7117 if (arg != NULL)
7118# endif
7119 {
7120 EMSG(_("E468: Completion argument only allowed for custom completion"));
7121 return FAIL;
7122 }
7123
7124# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7125 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7126 && arg == NULL)
7127 {
7128 EMSG(_("E467: Custom completion requires a function argument"));
7129 return FAIL;
7130 }
7131
7132 if (arg != NULL)
7133 *compl_arg = vim_strnsave(arg, (int)arglen);
7134# endif
7135 return OK;
7136}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007137
7138 int
7139cmdcomplete_str_to_type(char_u *complete_str)
7140{
7141 int i;
7142
7143 for (i = 0; command_complete[i].expand != 0; ++i)
7144 if (STRCMP(complete_str, command_complete[i].name) == 0)
7145 return command_complete[i].expand;
7146
7147 return EXPAND_NOTHING;
7148}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007149#endif
7150
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007152ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
Bram Moolenaare6850792010-05-14 15:28:44 +02007154 if (*eap->arg == NUL)
7155 {
7156#ifdef FEAT_EVAL
7157 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7158 char_u *p = NULL;
7159
7160 if (expr != NULL)
7161 {
7162 ++emsg_off;
7163 p = eval_to_string(expr, NULL, FALSE);
7164 --emsg_off;
7165 vim_free(expr);
7166 }
7167 if (p != NULL)
7168 {
7169 MSG(p);
7170 vim_free(p);
7171 }
7172 else
7173 MSG("default");
7174#else
7175 MSG(_("unknown"));
7176#endif
7177 }
7178 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007179 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180}
7181
7182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007183ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
7185 if (*eap->arg == NUL && eap->cmd[2] == '!')
7186 MSG(_("Greetings, Vim user!"));
7187 do_highlight(eap->arg, eap->forceit, FALSE);
7188}
7189
7190
7191/*
7192 * Call this function if we thought we were going to exit, but we won't
7193 * (because of an error). May need to restore the terminal mode.
7194 */
7195 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007196not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198 exiting = FALSE;
7199 settmode(TMODE_RAW);
7200}
7201
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007202 static int
7203before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7204{
7205 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7206
7207 /* Bail out when autocommands closed the window.
7208 * Refuse to quit when the buffer in the last window is being closed (can
7209 * only happen in autocommands). */
7210 if (!win_valid(wp)
7211 || curbuf_locked()
7212 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7213 return TRUE;
7214
7215 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7216 {
7217 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7218 /* Refuse to quit when locked or when the buffer in the last window is
7219 * being closed (can only happen in autocommands). */
7220 if (curbuf_locked()
7221 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7222 return TRUE;
7223 }
7224
7225 return FALSE;
7226}
7227
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007229 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007230 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 */
7232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007233ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007235 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007236
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237#ifdef FEAT_CMDWIN
7238 if (cmdwin_type != 0)
7239 {
7240 cmdwin_result = Ctrl_C;
7241 return;
7242 }
7243#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007244 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007245 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007246 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007247 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007248 return;
7249 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007250 if (eap->addr_count > 0)
7251 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007252 int wnr = eap->line2;
7253
7254 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7255 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007256 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007257 }
7258 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007259 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007260
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007261 /* Refuse to quit when locked. */
7262 if (curbuf_locked())
7263 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007264
7265 /* Trigger QuitPre and maybe ExitPre */
7266 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007267 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268
7269#ifdef FEAT_NETBEANS_INTG
7270 netbeansForcedQuit = eap->forceit;
7271#endif
7272
7273 /*
7274 * If there are more files or windows we won't exit.
7275 */
7276 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7277 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007278 if ((!buf_hide(wp->w_buffer)
7279 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007280 | (eap->forceit ? CCGD_FORCEIT : 0)
7281 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007283 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 {
7285 not_exiting();
7286 }
7287 else
7288 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007289 /* quit last window
7290 * Note: only_one_window() returns true, even so a help window is
7291 * still open. In that case only quit, if no address has been
7292 * specified. Example:
7293 * :h|wincmd w|1q - don't quit
7294 * :h|wincmd w|q - quit
7295 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007296 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007298 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007299#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007303 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 }
7305}
7306
7307/*
7308 * ":cquit".
7309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007311ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312{
7313 getout(1); /* this does not always pass on the exit code to the Manx
7314 compiler. why? */
7315}
7316
7317/*
7318 * ":qall": try to quit all windows
7319 */
7320 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007321ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322{
7323# ifdef FEAT_CMDWIN
7324 if (cmdwin_type != 0)
7325 {
7326 if (eap->forceit)
7327 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7328 else
7329 cmdwin_result = K_XF2;
7330 return;
7331 }
7332# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007333
7334 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007335 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007336 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007337 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007338 return;
7339 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007340
7341 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007342 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007343
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007345 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 getout(0);
7347 not_exiting();
7348}
7349
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350/*
7351 * ":close": close current window, unless it is the last one
7352 */
7353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007354ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007356 win_T *win;
7357 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007358#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007360 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007362#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007363 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007364 {
7365 if (eap->addr_count == 0)
7366 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007367 else
7368 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007369 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007370 {
7371 winnr++;
7372 if (winnr == eap->line2)
7373 break;
7374 }
7375 if (win == NULL)
7376 win = lastwin;
7377 ex_win_close(eap->forceit, win, NULL);
7378 }
7379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380}
7381
Bram Moolenaar4033c552017-09-16 20:54:51 +02007382#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007383/*
7384 * ":pclose": Close any preview window.
7385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007387ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007388{
7389 win_T *win;
7390
Bram Moolenaar29323592016-07-24 22:04:11 +02007391 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007392 if (win->w_p_pvw)
7393 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007394 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007395 break;
7396 }
7397}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007398#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007399
Bram Moolenaarf740b292006-02-16 22:11:02 +00007400/*
7401 * Close window "win" and take care of handling closing the last window for a
7402 * modified buffer.
7403 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007405ex_win_close(
7406 int forceit,
7407 win_T *win,
7408 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409{
7410 int need_hide;
7411 buf_T *buf = win->w_buffer;
7412
7413 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007414 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007416#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 if ((p_confirm || cmdmod.confirm) && p_write)
7418 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007419 bufref_T bufref;
7420
7421 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007423 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 return;
7425 need_hide = FALSE;
7426 }
7427 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007430 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 return;
7432 }
7433 }
7434
Bram Moolenaar4033c552017-09-16 20:54:51 +02007435#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007437#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007438
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007440 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007441 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007442 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007443 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444}
7445
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007447 * Handle the argument for a tabpage related ex command.
7448 * Returns a tabpage number.
7449 * When an error is encountered then eap->errmsg is set.
7450 */
7451 static int
7452get_tabpage_arg(exarg_T *eap)
7453{
7454 int tab_number;
7455 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7456
7457 if (eap->arg && *eap->arg != NUL)
7458 {
7459 char_u *p = eap->arg;
7460 char_u *p_save;
7461 int relative = 0; /* argument +N/-N means: go to N places to the
7462 * right/left relative to the current position. */
7463
7464 if (*p == '-')
7465 {
7466 relative = -1;
7467 p++;
7468 }
7469 else if (*p == '+')
7470 {
7471 relative = 1;
7472 p++;
7473 }
7474
7475 p_save = p;
7476 tab_number = getdigits(&p);
7477
7478 if (relative == 0)
7479 {
7480 if (STRCMP(p, "$") == 0)
7481 tab_number = LAST_TAB_NR;
7482 else if (p == p_save || *p_save == '-' || *p != NUL
7483 || tab_number > LAST_TAB_NR)
7484 {
7485 /* No numbers as argument. */
7486 eap->errmsg = e_invarg;
7487 goto theend;
7488 }
7489 }
7490 else
7491 {
7492 if (*p_save == NUL)
7493 tab_number = 1;
7494 else if (p == p_save || *p_save == '-' || *p != NUL
7495 || tab_number == 0)
7496 {
7497 /* No numbers as argument. */
7498 eap->errmsg = e_invarg;
7499 goto theend;
7500 }
7501 tab_number = tab_number * relative + tabpage_index(curtab);
7502 if (!unaccept_arg0 && relative == -1)
7503 --tab_number;
7504 }
7505 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7506 eap->errmsg = e_invarg;
7507 }
7508 else if (eap->addr_count > 0)
7509 {
7510 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007511 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007512 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007513 tab_number = 0;
7514 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007515 else
7516 {
7517 tab_number = eap->line2;
7518 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7519 {
7520 --tab_number;
7521 if (tab_number < unaccept_arg0)
7522 eap->errmsg = e_invarg;
7523 }
7524 }
7525 }
7526 else
7527 {
7528 switch (eap->cmdidx)
7529 {
7530 case CMD_tabnext:
7531 tab_number = tabpage_index(curtab) + 1;
7532 if (tab_number > LAST_TAB_NR)
7533 tab_number = 1;
7534 break;
7535 case CMD_tabmove:
7536 tab_number = LAST_TAB_NR;
7537 break;
7538 default:
7539 tab_number = tabpage_index(curtab);
7540 }
7541 }
7542
7543theend:
7544 return tab_number;
7545}
7546
7547/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007548 * ":tabclose": close current tab page, unless it is the last one.
7549 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 */
7551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007552ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007554 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007555 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007556
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007557# ifdef FEAT_CMDWIN
7558 if (cmdwin_type != 0)
7559 cmdwin_result = K_IGNORE;
7560 else
7561# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007562 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007563 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007564 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007566 tab_number = get_tabpage_arg(eap);
7567 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007568 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007569 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007570 if (tp == NULL)
7571 {
7572 beep_flush();
7573 return;
7574 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007575 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007576 {
7577 tabpage_close_other(tp, eap->forceit);
7578 return;
7579 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007580 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007581 tabpage_close(eap->forceit);
7582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007584}
7585
7586/*
7587 * ":tabonly": close all tab pages except the current one
7588 */
7589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007590ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007591{
7592 tabpage_T *tp;
7593 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007594 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007595
7596# ifdef FEAT_CMDWIN
7597 if (cmdwin_type != 0)
7598 cmdwin_result = K_IGNORE;
7599 else
7600# endif
7601 if (first_tabpage->tp_next == NULL)
7602 MSG(_("Already only one tab page"));
7603 else
7604 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007605 tab_number = get_tabpage_arg(eap);
7606 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007607 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007608 goto_tabpage(tab_number);
7609 /* Repeat this up to a 1000 times, because autocommands may
7610 * mess up the lists. */
7611 for (done = 0; done < 1000; ++done)
7612 {
7613 FOR_ALL_TABPAGES(tp)
7614 if (tp->tp_topframe != topframe)
7615 {
7616 tabpage_close_other(tp, eap->forceit);
7617 /* if we failed to close it quit */
7618 if (valid_tabpage(tp))
7619 done = 1000;
7620 /* start over, "tp" is now invalid */
7621 break;
7622 }
7623 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007624 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007625 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007626 }
7627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629
7630/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007631 * Close the current tab page.
7632 */
7633 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007634tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007635{
7636 /* First close all the windows but the current one. If that worked then
7637 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007638 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007639 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007640 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007641 ex_win_close(forceit, curwin, NULL);
7642# ifdef FEAT_GUI
7643 need_mouse_correct = TRUE;
7644# endif
7645}
7646
7647/*
7648 * Close tab page "tp", which is not the current tab page.
7649 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007650 * Also takes care of the tab pages line disappearing when closing the
7651 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007652 */
7653 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007654tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007655{
7656 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007657 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007658 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007659
7660 /* Limit to 1000 windows, autocommands may add a window while we close
7661 * one. OK, so I'm paranoid... */
7662 while (++done < 1000)
7663 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007664 wp = tp->tp_firstwin;
7665 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007666
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007667 /* Autocommands may delete the tab page under our fingers and we may
7668 * fail to close a window with a modified buffer. */
7669 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007670 break;
7671 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007672
Bram Moolenaar29323592016-07-24 22:04:11 +02007673 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007674
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007675 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007676 if (h != tabline_height())
7677 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007678}
7679
7680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 * ":only".
7682 */
7683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007684ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007686 win_T *wp;
7687 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688# ifdef FEAT_GUI
7689 need_mouse_correct = TRUE;
7690# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007691 if (eap->addr_count > 0)
7692 {
7693 wnr = eap->line2;
7694 for (wp = firstwin; --wnr > 0; )
7695 {
7696 if (wp->w_next == NULL)
7697 break;
7698 else
7699 wp = wp->w_next;
7700 }
7701 win_goto(wp);
7702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 close_others(TRUE, eap->forceit);
7704}
7705
7706/*
7707 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007708 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007710 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007711ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712{
7713 if (eap->addr_count == 0)
7714 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007715 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717
7718 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007719ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007721 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007722 if (!eap->skip)
7723 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007724#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007725 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007726#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007727 if (eap->addr_count == 0)
7728 win_close(curwin, FALSE); /* don't free buffer */
7729 else
7730 {
7731 int winnr = 0;
7732 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007733
Bram Moolenaar2256c992016-11-15 21:17:07 +01007734 FOR_ALL_WINDOWS(win)
7735 {
7736 winnr++;
7737 if (winnr == eap->line2)
7738 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007739 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007740 if (win == NULL)
7741 win = lastwin;
7742 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 }
7745}
7746
7747/*
7748 * ":stop" and ":suspend": Suspend Vim.
7749 */
7750 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007751ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752{
7753 /*
7754 * Disallow suspending for "rvim".
7755 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007756 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {
7758 if (!eap->forceit)
7759 autowrite_all();
7760 windgoto((int)Rows - 1, 0);
7761 out_char('\n');
7762 out_flush();
7763 stoptermcap();
7764 out_flush(); /* needed for SUN to restore xterm buffer */
7765#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007766 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767#endif
7768 ui_suspend(); /* call machine specific function */
7769#ifdef FEAT_TITLE
7770 maketitle();
7771 resettitle(); /* force updating the title */
7772#endif
7773 starttermcap();
7774 scroll_start(); /* scroll screen before redrawing */
7775 redraw_later_clear();
7776 shell_resized(); /* may have resized window */
7777 }
7778}
7779
7780/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007781 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 */
7783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007784ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
7786#ifdef FEAT_CMDWIN
7787 if (cmdwin_type != 0)
7788 {
7789 cmdwin_result = Ctrl_C;
7790 return;
7791 }
7792#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007793 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007794 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007795 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007796 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007797 return;
7798 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007799
7800 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007801 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802
7803 /*
7804 * if more files or windows we won't exit
7805 */
7806 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7807 exiting = TRUE;
7808 if ( ((eap->cmdidx == CMD_wq
7809 || curbufIsChanged())
7810 && do_write(eap) == FAIL)
7811 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007812 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 {
7814 not_exiting();
7815 }
7816 else
7817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007820 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821# ifdef FEAT_GUI
7822 need_mouse_correct = TRUE;
7823# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007824 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007825 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 }
7827}
7828
7829/*
7830 * ":print", ":list", ":number".
7831 */
7832 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007833ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007835 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7836 EMSG(_(e_emptybuf));
7837 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007839 for ( ;!got_int; ui_breakcheck())
7840 {
7841 print_line(eap->line1,
7842 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7843 || (eap->flags & EXFLAG_NR)),
7844 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7845 if (++eap->line1 > eap->line2)
7846 break;
7847 out_flush(); /* show one line at a time */
7848 }
7849 setpcmark();
7850 /* put cursor at last line */
7851 curwin->w_cursor.lnum = eap->line2;
7852 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 }
7854
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856}
7857
7858#ifdef FEAT_BYTEOFF
7859 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007860ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
7862 goto_byte(eap->line2);
7863}
7864#endif
7865
7866/*
7867 * ":shell".
7868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007870ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871{
7872 do_shell(NULL, 0);
7873}
7874
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007875#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007877static int drop_busy = FALSE;
7878static int drop_filec;
7879static char_u **drop_filev = NULL;
7880static int drop_split;
7881static void (*drop_callback)(void *);
7882static void *drop_cookie;
7883
7884 static void
7885handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
7887 exarg_T ea;
7888 int save_msg_scroll = msg_scroll;
7889
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007890 // Setting the argument list may cause screen updates and being called
7891 // recursively. Avoid that by setting drop_busy.
7892 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893
7894 /* Check whether the current buffer is changed. If so, we will need
7895 * to split the current window or data could be lost.
7896 * We don't need to check if the 'hidden' option is set, as in this
7897 * case the buffer won't be lost.
7898 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007899 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {
7901 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007902 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 --emsg_off;
7904 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007905 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 if (win_split(0, 0) == FAIL)
7908 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007909 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910
7911 /* When splitting the window, create a new alist. Otherwise the
7912 * existing one is overwritten. */
7913 alist_unlink(curwin->w_alist);
7914 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 }
7916
7917 /*
7918 * Set up the new argument list.
7919 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007920 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921
7922 /*
7923 * Move to the first file.
7924 */
7925 /* Fake up a minimal "next" command for do_argfile() */
7926 vim_memset(&ea, 0, sizeof(ea));
7927 ea.cmd = (char_u *)"next";
7928 do_argfile(&ea, 0);
7929
7930 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7931 * mode that is not needed here. */
7932 need_start_insertmode = FALSE;
7933
7934 /* Restore msg_scroll, otherwise a following command may cause scrolling
7935 * unexpectedly. The screen will be redrawn by the caller, thus
7936 * msg_scroll being set by displaying a message is irrelevant. */
7937 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007938
7939 if (drop_callback != NULL)
7940 drop_callback(drop_cookie);
7941
7942 drop_filev = NULL;
7943 drop_busy = FALSE;
7944}
7945
7946/*
7947 * Handle a file drop. The code is here because a drop is *nearly* like an
7948 * :args command, but not quite (we have a list of exact filenames, so we
7949 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7950 * code is very similar to :args and hence needs access to a lot of the static
7951 * functions in this file.
7952 *
7953 * The "filev" list must have been allocated using alloc(), as should each item
7954 * in the list. This function takes over responsibility for freeing the "filev"
7955 * list.
7956 */
7957 void
7958handle_drop(
7959 int filec, // the number of files dropped
7960 char_u **filev, // the list of files dropped
7961 int split, // force splitting the window
7962 void (*callback)(void *), // to be called after setting the argument
7963 // list
7964 void *cookie) // argument for "callback" (allocated)
7965{
7966 // Cannot handle recursive drops, finish the pending one.
7967 if (drop_busy)
7968 {
7969 FreeWild(filec, filev);
7970 vim_free(cookie);
7971 return;
7972 }
7973
7974 // When calling handle_drop() more than once in a row we only use the last
7975 // one.
7976 if (drop_filev != NULL)
7977 {
7978 FreeWild(drop_filec, drop_filev);
7979 vim_free(drop_cookie);
7980 }
7981
7982 drop_filec = filec;
7983 drop_filev = filev;
7984 drop_split = split;
7985 drop_callback = callback;
7986 drop_cookie = cookie;
7987
7988 // Postpone this when:
7989 // - editing the command line
7990 // - not possible to change the current buffer
7991 // - updating the screen
7992 // As it may change buffers and window structures that are in use and cause
7993 // freed memory to be used.
7994 if (text_locked() || curbuf_locked() || updating_screen)
7995 return;
7996
7997 handle_drop_internal();
7998}
7999
8000/*
8001 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8002 * reset: Handle a postponed drop.
8003 */
8004 void
8005handle_any_postponed_drop(void)
8006{
8007 if (!drop_busy && drop_filev != NULL
8008 && !text_locked() && !curbuf_locked() && !updating_screen)
8009 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010}
8011#endif
8012
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013/*
8014 * Clear an argument list: free all file names and reset it to zero entries.
8015 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008016 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008017alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018{
8019 while (--al->al_ga.ga_len >= 0)
8020 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8021 ga_clear(&al->al_ga);
8022}
8023
8024/*
8025 * Init an argument list.
8026 */
8027 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008028alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029{
8030 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8031}
8032
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033/*
8034 * Remove a reference from an argument list.
8035 * Ignored when the argument list is the global one.
8036 * If the argument list is no longer used by any window, free it.
8037 */
8038 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008039alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040{
8041 if (al != &global_alist && --al->al_refcount <= 0)
8042 {
8043 alist_clear(al);
8044 vim_free(al);
8045 }
8046}
8047
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048/*
8049 * Create a new argument list and use it for the current window.
8050 */
8051 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008052alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
8054 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8055 if (curwin->w_alist == NULL)
8056 {
8057 curwin->w_alist = &global_alist;
8058 ++global_alist.al_refcount;
8059 }
8060 else
8061 {
8062 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008063 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 alist_init(curwin->w_alist);
8065 }
8066}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008068#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069/*
8070 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008071 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8072 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 */
8074 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008075alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076{
8077 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008078 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 char_u **new_arg_files;
8080 int new_arg_file_count;
8081 char_u *save_p_su = p_su;
8082 int i;
8083
8084 /* Don't use 'suffixes' here. This should work like the shell did the
8085 * expansion. Also, the vimrc file isn't read yet, thus the user
8086 * can't set the options. */
8087 p_su = empty_option;
8088 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8089 if (old_arg_files != NULL)
8090 {
8091 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008092 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8093 old_arg_count = GARGCOUNT;
8094 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008096 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 && new_arg_file_count > 0)
8098 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008099 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8100 TRUE, fnum_list, fnum_len);
8101 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 }
8104 p_su = save_p_su;
8105}
8106#endif
8107
8108/*
8109 * Set the argument list for the current window.
8110 * Takes over the allocated files[] and the allocated fnames in it.
8111 */
8112 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008113alist_set(
8114 alist_T *al,
8115 int count,
8116 char_u **files,
8117 int use_curbuf,
8118 int *fnum_list,
8119 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120{
8121 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008122 static int recursive = 0;
8123
8124 if (recursive)
8125 {
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008126 EMSG(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008127 return;
8128 }
8129 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130
8131 alist_clear(al);
8132 if (ga_grow(&al->al_ga, count) == OK)
8133 {
8134 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008135 {
8136 if (got_int)
8137 {
8138 /* When adding many buffers this can take a long time. Allow
8139 * interrupting here. */
8140 while (i < count)
8141 vim_free(files[i++]);
8142 break;
8143 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008144
8145 /* May set buffer name of a buffer previously used for the
8146 * argument list, so that it's re-used by alist_add. */
8147 if (fnum_list != NULL && i < fnum_len)
8148 buf_set_name(fnum_list[i], files[i]);
8149
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008151 ui_breakcheck();
8152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 vim_free(files);
8154 }
8155 else
8156 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008159
8160 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161}
8162
8163/*
8164 * Add file "fname" to argument list "al".
8165 * "fname" must have been allocated and "al" must have been checked for room.
8166 */
8167 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008168alist_add(
8169 alist_T *al,
8170 char_u *fname,
8171 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172{
8173 if (fname == NULL) /* don't add NULL file names */
8174 return;
8175#ifdef BACKSLASH_IN_FILENAME
8176 slash_adjust(fname);
8177#endif
8178 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8179 if (set_fnum > 0)
8180 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8181 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8182 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183}
8184
8185#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8186/*
8187 * Adjust slashes in file names. Called after 'shellslash' was set.
8188 */
8189 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008190alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191{
8192 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008194 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195
8196 for (i = 0; i < GARGCOUNT; ++i)
8197 if (GARGLIST[i].ae_fname != NULL)
8198 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008199 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 if (wp->w_alist != &global_alist)
8201 for (i = 0; i < WARGCOUNT(wp); ++i)
8202 if (WARGLIST(wp)[i].ae_fname != NULL)
8203 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204}
8205#endif
8206
8207/*
8208 * ":preserve".
8209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008211ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008213 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 ml_preserve(curbuf, TRUE);
8215}
8216
8217/*
8218 * ":recover".
8219 */
8220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008221ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222{
8223 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8224 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008225 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8226 | CCGD_MULTWIN
8227 | (eap->forceit ? CCGD_FORCEIT : 0)
8228 | CCGD_EXCMD)
8229
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 && (*eap->arg == NUL
8231 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8232 ml_recover();
8233 recoverymode = FALSE;
8234}
8235
8236/*
8237 * Command modifier used in a wrong way.
8238 */
8239 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008240ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241{
8242 eap->errmsg = e_invcmd;
8243}
8244
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245/*
8246 * :sview [+command] file split window with new file, read-only
8247 * :split [[+command] file] split window with current or new file
8248 * :vsplit [[+command] file] split window vertically with current or new file
8249 * :new [[+command] file] split window with no or new file
8250 * :vnew [[+command] file] split vertically window with no or new file
8251 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008252 *
8253 * :tabedit open new Tab page with empty window
8254 * :tabedit [+command] file open new Tab page and edit "file"
8255 * :tabnew [[+command] file] just like :tabedit
8256 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 */
8258 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008259ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008261 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008262#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008264#endif
8265#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008267#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008268 int use_tab = eap->cmdidx == CMD_tabedit
8269 || eap->cmdidx == CMD_tabfind
8270 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271
Bram Moolenaar4033c552017-09-16 20:54:51 +02008272#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275
Bram Moolenaar4033c552017-09-16 20:54:51 +02008276#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008278 * quickfix windows. But it's OK when doing ":tab split". */
8279 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {
8281 if (eap->cmdidx == CMD_split)
8282 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 if (eap->cmdidx == CMD_vsplit)
8284 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287
Bram Moolenaar4033c552017-09-16 20:54:51 +02008288#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008289 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {
8291 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8292 FNAME_MESS, TRUE, curbuf->b_ffname);
8293 if (fname == NULL)
8294 goto theend;
8295 eap->arg = fname;
8296 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008297# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008298 else
8299# endif
8300#endif
8301#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 && eap->cmdidx != CMD_new)
8305 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008306 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008307# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008308 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008309# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008310 au_has_group((char_u *)"FileExplorer"))
8311 {
8312 /* No browsing supported but we do have the file explorer:
8313 * Edit the directory. */
8314 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8315 eap->arg = (char_u *)".";
8316 }
8317 else
8318 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008319 fname = do_browse(0, (char_u *)(use_tab
8320 ? _("Edit File in new tab page")
8321 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008323 if (fname == NULL)
8324 goto theend;
8325 eap->arg = fname;
8326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 }
8328 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008331 /*
8332 * Either open new tab page or split the window.
8333 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008334 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008335 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008336 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8337 : eap->addr_count == 0 ? 0
8338 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008339 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008340 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008341
8342 /* set the alternate buffer for the window we came from */
8343 if (curwin != old_curwin
8344 && win_valid(old_curwin)
8345 && old_curwin->w_buffer != curbuf
8346 && !cmdmod.keepalt)
8347 old_curwin->w_alt_fnum = curbuf->b_fnum;
8348 }
8349 }
8350 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8352 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 /* Reset 'scrollbind' when editing another file, but keep it when
8354 * doing ":split" without arguments. */
8355 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008356# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008360 {
8361 RESET_BINDING(curwin);
8362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 else
8364 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 do_exedit(eap, old_curwin);
8366 }
8367
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008368# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008370# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008372# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373theend:
8374 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008377
8378/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008379 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008380 */
8381 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008382tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008383{
8384 exarg_T ea;
8385
8386 vim_memset(&ea, 0, sizeof(ea));
8387 ea.cmdidx = CMD_tabnew;
8388 ea.cmd = (char_u *)"tabn";
8389 ea.arg = (char_u *)"";
8390 ex_splitview(&ea);
8391}
8392
8393/*
8394 * :tabnext command
8395 */
8396 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008397ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008398{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008399 int tab_number;
8400
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008401 switch (eap->cmdidx)
8402 {
8403 case CMD_tabfirst:
8404 case CMD_tabrewind:
8405 goto_tabpage(1);
8406 break;
8407 case CMD_tablast:
8408 goto_tabpage(9999);
8409 break;
8410 case CMD_tabprevious:
8411 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008412 if (eap->arg && *eap->arg != NUL)
8413 {
8414 char_u *p = eap->arg;
8415 char_u *p_save = p;
8416
8417 tab_number = getdigits(&p);
8418 if (p == p_save || *p_save == '-' || *p != NUL
8419 || tab_number == 0)
8420 {
8421 /* No numbers as argument. */
8422 eap->errmsg = e_invarg;
8423 return;
8424 }
8425 }
8426 else
8427 {
8428 if (eap->addr_count == 0)
8429 tab_number = 1;
8430 else
8431 {
8432 tab_number = eap->line2;
8433 if (tab_number < 1)
8434 {
8435 eap->errmsg = e_invrange;
8436 return;
8437 }
8438 }
8439 }
8440 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008441 break;
8442 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008443 tab_number = get_tabpage_arg(eap);
8444 if (eap->errmsg == NULL)
8445 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008446 break;
8447 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008448}
8449
8450/*
8451 * :tabmove command
8452 */
8453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008454ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008455{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008456 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008457
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008458 tab_number = get_tabpage_arg(eap);
8459 if (eap->errmsg == NULL)
8460 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008461}
8462
8463/*
8464 * :tabs command: List tabs and their contents.
8465 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008467ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008468{
8469 tabpage_T *tp;
8470 win_T *wp;
8471 int tabcount = 1;
8472
8473 msg_start();
8474 msg_scroll = TRUE;
8475 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8476 {
8477 msg_putchar('\n');
8478 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008479 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008480 out_flush(); /* output one line at a time */
8481 ui_breakcheck();
8482
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008483 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008484 wp = firstwin;
8485 else
8486 wp = tp->tp_firstwin;
8487 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8488 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008489 msg_putchar('\n');
8490 msg_putchar(wp == curwin ? '>' : ' ');
8491 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008492 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8493 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008494 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008495 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008496 else
8497 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8498 IObuff, IOSIZE, TRUE);
8499 msg_outtrans(IObuff);
8500 out_flush(); /* output one line at a time */
8501 ui_breakcheck();
8502 }
8503 }
8504}
8505
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506/*
8507 * ":mode": Set screen mode.
8508 * If no argument given, just get the screen size and redraw.
8509 */
8510 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008511ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512{
8513 if (*eap->arg == NUL)
8514 shell_resized();
8515 else
8516 mch_screenmode(eap->arg);
8517}
8518
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519/*
8520 * ":resize".
8521 * set, increment or decrement current window height
8522 */
8523 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008524ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525{
8526 int n;
8527 win_T *wp = curwin;
8528
8529 if (eap->addr_count > 0)
8530 {
8531 n = eap->line2;
8532 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8533 ;
8534 }
8535
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008536# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008538# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 if (cmdmod.split & WSP_VERT)
8541 {
8542 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008543 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8545 n = 9999;
8546 win_setwidth_win((int)n, wp);
8547 }
8548 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 {
8550 if (*eap->arg == '-' || *eap->arg == '+')
8551 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008552 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 n = 9999;
8554 win_setheight_win((int)n, wp);
8555 }
8556}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557
8558/*
8559 * ":find [+command] <file>" command.
8560 */
8561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008562ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
8564#ifdef FEAT_SEARCHPATH
8565 char_u *fname;
8566 int count;
8567
8568 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8569 TRUE, curbuf->b_ffname);
8570 if (eap->addr_count > 0)
8571 {
8572 /* Repeat finding the file "count" times. This matters when it
8573 * appears several times in the path. */
8574 count = eap->line2;
8575 while (fname != NULL && --count > 0)
8576 {
8577 vim_free(fname);
8578 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8579 FALSE, curbuf->b_ffname);
8580 }
8581 }
8582
8583 if (fname != NULL)
8584 {
8585 eap->arg = fname;
8586#endif
8587 do_exedit(eap, NULL);
8588#ifdef FEAT_SEARCHPATH
8589 vim_free(fname);
8590 }
8591#endif
8592}
8593
8594/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008595 * ":open" simulation: for now just work like ":visual".
8596 */
8597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008598ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008599{
8600 regmatch_T regmatch;
8601 char_u *p;
8602
8603 curwin->w_cursor.lnum = eap->line2;
8604 beginline(BL_SOL | BL_FIX);
8605 if (*eap->arg == '/')
8606 {
8607 /* ":open /pattern/": put cursor in column found with pattern */
8608 ++eap->arg;
8609 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8610 *p = NUL;
8611 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8612 if (regmatch.regprog != NULL)
8613 {
8614 regmatch.rm_ic = p_ic;
8615 p = ml_get_curline();
8616 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008617 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008618 else
8619 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008620 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008621 }
8622 /* Move to the NUL, ignore any other arguments. */
8623 eap->arg += STRLEN(eap->arg);
8624 }
8625 check_cursor();
8626
8627 eap->cmdidx = CMD_visual;
8628 do_exedit(eap, NULL);
8629}
8630
8631/*
8632 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 */
8634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008635ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636{
8637 do_exedit(eap, NULL);
8638}
8639
8640/*
8641 * ":edit <file>" command and alikes.
8642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008644do_exedit(
8645 exarg_T *eap,
8646 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647{
8648 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008650 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651
8652 /*
8653 * ":vi" command ends Ex mode.
8654 */
8655 if (exmode_active && (eap->cmdidx == CMD_visual
8656 || eap->cmdidx == CMD_view))
8657 {
8658 exmode_active = FALSE;
8659 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008660 {
8661 /* Special case: ":global/pat/visual\NLvi-commands" */
8662 if (global_busy)
8663 {
8664 int rd = RedrawingDisabled;
8665 int nwr = no_wait_return;
8666 int ms = msg_scroll;
8667#ifdef FEAT_GUI
8668 int he = hold_gui_events;
8669#endif
8670
8671 if (eap->nextcmd != NULL)
8672 {
8673 stuffReadbuff(eap->nextcmd);
8674 eap->nextcmd = NULL;
8675 }
8676
8677 if (exmode_was != EXMODE_VIM)
8678 settmode(TMODE_RAW);
8679 RedrawingDisabled = 0;
8680 no_wait_return = 0;
8681 need_wait_return = FALSE;
8682 msg_scroll = 0;
8683#ifdef FEAT_GUI
8684 hold_gui_events = 0;
8685#endif
8686 must_redraw = CLEAR;
8687
8688 main_loop(FALSE, TRUE);
8689
8690 RedrawingDisabled = rd;
8691 no_wait_return = nwr;
8692 msg_scroll = ms;
8693#ifdef FEAT_GUI
8694 hold_gui_events = he;
8695#endif
8696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 }
8700
8701 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008702 || eap->cmdidx == CMD_tabnew
8703 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008704 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008706 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 setpcmark();
8708 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008709 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8710 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008712 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 || *eap->arg != NUL
8714#ifdef FEAT_BROWSE
8715 || cmdmod.browse
8716#endif
8717 )
8718 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008719 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8720 * can bring us here, others are stopped earlier. */
8721 if (*eap->arg != NUL && curbuf_locked())
8722 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008723
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 n = readonlymode;
8725 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8726 readonlymode = TRUE;
8727 else if (eap->cmdidx == CMD_enew)
8728 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8729 empty buffer */
8730 setpcmark();
8731 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8732 NULL, eap,
8733 /* ":edit" goes to first line if Vi compatible */
8734 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8735 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8736 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008737 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008739 /* after a split we can use an existing buffer */
8740 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008742 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 {
8744 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 if (old_curwin != NULL)
8746 {
8747 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008748 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008750#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008751 cleanup_T cs;
8752
8753 /* Reset the error/interrupt/exception state here so that
8754 * aborting() returns FALSE when closing a window. */
8755 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008756#endif
8757#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008759#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008760 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008761
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008762#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008763 /* Restore the error/interrupt/exception state if not
8764 * discarded by a new aborting error, interrupt, or
8765 * uncaught exception. */
8766 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 }
8769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 }
8771 else if (readonlymode && curbuf->b_nwindows == 1)
8772 {
8773 /* When editing an already visited buffer, 'readonly' won't be set
8774 * but the previous value is kept. With ":view" and ":sview" we
8775 * want the file to be readonly, except when another window is
8776 * editing the same buffer. */
8777 curbuf->b_p_ro = TRUE;
8778 }
8779 readonlymode = n;
8780 }
8781 else
8782 {
8783 if (eap->do_ecmd_cmd != NULL)
8784 do_cmdline_cmd(eap->do_ecmd_cmd);
8785#ifdef FEAT_TITLE
8786 n = curwin->w_arg_idx_invalid;
8787#endif
8788 check_arg_idx(curwin);
8789#ifdef FEAT_TITLE
8790 if (n != curwin->w_arg_idx_invalid)
8791 maketitle();
8792#endif
8793 }
8794
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 /*
8796 * if ":split file" worked, set alternate file name in old window to new
8797 * file
8798 */
8799 if (old_curwin != NULL
8800 && *eap->arg != NUL
8801 && curwin != old_curwin
8802 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008803 && old_curwin->w_buffer != curbuf
8804 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806
8807 ex_no_reprint = TRUE;
8808}
8809
8810#ifndef FEAT_GUI
8811/*
8812 * ":gui" and ":gvim" when there is no GUI.
8813 */
8814 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008815ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816{
8817 eap->errmsg = e_nogvim;
8818}
8819#endif
8820
8821#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8822 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008823ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
8825 gui_make_tearoff(eap->arg);
8826}
8827#endif
8828
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008829#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8830 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008832ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008834# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8835 if (gui.in_use)
8836 gui_make_popup(eap->arg, eap->forceit);
8837# ifdef FEAT_TERM_POPUP_MENU
8838 else
8839# endif
8840# endif
8841# ifdef FEAT_TERM_POPUP_MENU
8842 pum_make_popup(eap->arg, eap->forceit);
8843# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844}
8845#endif
8846
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008848ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849{
8850 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8851 MSG(_("No swap file"));
8852 else
8853 msg(curbuf->b_ml.ml_mfp->mf_fname);
8854}
8855
8856/*
8857 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8858 * offset.
8859 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008862ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008865 win_T *save_curwin = curwin;
8866 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 long topline;
8868 long y;
8869 linenr_T old_linenr = curwin->w_cursor.lnum;
8870
8871 setpcmark();
8872
8873 /*
8874 * determine max topline
8875 */
8876 if (curwin->w_p_scb)
8877 {
8878 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008879 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 {
8881 if (wp->w_p_scb && wp->w_buffer)
8882 {
8883 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8884 if (topline > y)
8885 topline = y;
8886 }
8887 }
8888 if (topline < 1)
8889 topline = 1;
8890 }
8891 else
8892 {
8893 topline = 1;
8894 }
8895
8896
8897 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008898 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008900 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 {
8902 if (curwin->w_p_scb)
8903 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008904 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 y = topline - curwin->w_topline;
8906 if (y > 0)
8907 scrollup(y, TRUE);
8908 else
8909 scrolldown(-y, TRUE);
8910 curwin->w_scbind_pos = topline;
8911 redraw_later(VALID);
8912 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 }
8915 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008916 curwin = save_curwin;
8917 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 if (curwin->w_p_scb)
8919 {
8920 did_syncbind = TRUE;
8921 checkpcmark();
8922 if (old_linenr != curwin->w_cursor.lnum)
8923 {
8924 char_u ctrl_o[2];
8925
8926 ctrl_o[0] = Ctrl_O;
8927 ctrl_o[1] = 0;
8928 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8929 }
8930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931}
8932
8933
8934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008935ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008937 int i;
8938 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8939 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940
8941 if (eap->usefilter) /* :r!cmd */
8942 do_bang(1, eap, FALSE, FALSE, TRUE);
8943 else
8944 {
8945 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8946 return;
8947
8948#ifdef FEAT_BROWSE
8949 if (cmdmod.browse)
8950 {
8951 char_u *browseFile;
8952
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008953 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 NULL, NULL, NULL, curbuf);
8955 if (browseFile != NULL)
8956 {
8957 i = readfile(browseFile, NULL,
8958 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8959 vim_free(browseFile);
8960 }
8961 else
8962 i = OK;
8963 }
8964 else
8965#endif
8966 if (*eap->arg == NUL)
8967 {
8968 if (check_fname() == FAIL) /* check for no file name */
8969 return;
8970 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8971 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8972 }
8973 else
8974 {
8975 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8976 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8977 i = readfile(eap->arg, NULL,
8978 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8979
8980 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008981 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008983#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 if (!aborting())
8985#endif
8986 EMSG2(_(e_notopen), eap->arg);
8987 }
8988 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008989 {
8990 if (empty && exmode_active)
8991 {
8992 /* Delete the empty line that remains. Historically ex does
8993 * this but vi doesn't. */
8994 if (eap->line2 == 0)
8995 lnum = curbuf->b_ml.ml_line_count;
8996 else
8997 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008998 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008999 {
9000 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009001 if (curwin->w_cursor.lnum > 1
9002 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009003 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009004 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009005 }
9006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 }
9010}
9011
Bram Moolenaarea408852005-06-25 22:49:46 +00009012static char_u *prev_dir = NULL;
9013
9014#if defined(EXITFREE) || defined(PROTO)
9015 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009016free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009017{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009018 VIM_CLEAR(prev_dir);
9019 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009020}
9021#endif
9022
Bram Moolenaarf4258302013-06-02 18:20:17 +02009023/*
9024 * Deal with the side effects of changing the current directory.
9025 * When "local" is TRUE then this was after an ":lcd" command.
9026 */
9027 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009028post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009029{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009030 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009031 if (local)
9032 {
9033 /* If still in global directory, need to remember current
9034 * directory as global directory. */
9035 if (globaldir == NULL && prev_dir != NULL)
9036 globaldir = vim_strsave(prev_dir);
9037 /* Remember this local directory for the window. */
9038 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9039 curwin->w_localdir = vim_strsave(NameBuff);
9040 }
9041 else
9042 {
9043 /* We are now in the global directory, no need to remember its
9044 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009045 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009046 }
9047
9048 shorten_fnames(TRUE);
9049}
9050
Bram Moolenaarea408852005-06-25 22:49:46 +00009051
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052/*
9053 * ":cd", ":lcd", ":chdir" and ":lchdir".
9054 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009055 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009056ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 char_u *new_dir;
9059 char_u *tofree;
9060
9061 new_dir = eap->arg;
9062#if !defined(UNIX) && !defined(VMS)
9063 /* for non-UNIX ":cd" means: print current directory */
9064 if (*new_dir == NUL)
9065 ex_pwd(NULL);
9066 else
9067#endif
9068 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009069 if (allbuf_locked())
9070 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009071 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9072 && !eap->forceit)
9073 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009074 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009075 return;
9076 }
9077
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 /* ":cd -": Change to previous directory */
9079 if (STRCMP(new_dir, "-") == 0)
9080 {
9081 if (prev_dir == NULL)
9082 {
9083 EMSG(_("E186: No previous directory"));
9084 return;
9085 }
9086 new_dir = prev_dir;
9087 }
9088
9089 /* Save current directory for next ":cd -" */
9090 tofree = prev_dir;
9091 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9092 prev_dir = vim_strsave(NameBuff);
9093 else
9094 prev_dir = NULL;
9095
9096#if defined(UNIX) || defined(VMS)
9097 /* for UNIX ":cd" means: go to home directory */
9098 if (*new_dir == NUL)
9099 {
9100 /* use NameBuff for home directory name */
9101# ifdef VMS
9102 char_u *p;
9103
9104 p = mch_getenv((char_u *)"SYS$LOGIN");
9105 if (p == NULL || *p == NUL) /* empty is the same as not set */
9106 NameBuff[0] = NUL;
9107 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009108 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109# else
9110 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9111# endif
9112 new_dir = NameBuff;
9113 }
9114#endif
9115 if (new_dir == NULL || vim_chdir(new_dir))
9116 EMSG(_(e_failed));
9117 else
9118 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009119 int is_local_chdir = eap->cmdidx == CMD_lcd
9120 || eap->cmdidx == CMD_lchdir;
9121
9122 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123
9124 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009125 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009127 apply_autocmds(EVENT_DIRCHANGED,
9128 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9129 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 }
9131 vim_free(tofree);
9132 }
9133}
9134
9135/*
9136 * ":pwd".
9137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009139ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140{
9141 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9142 {
9143#ifdef BACKSLASH_IN_FILENAME
9144 slash_adjust(NameBuff);
9145#endif
9146 msg(NameBuff);
9147 }
9148 else
9149 EMSG(_("E187: Unknown"));
9150}
9151
9152/*
9153 * ":=".
9154 */
9155 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009156ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157{
9158 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009159 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160}
9161
9162 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009163ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009165 int n;
9166 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167
9168 if (cursor_valid())
9169 {
9170 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9171 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009172 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009174
9175 len = eap->line2;
9176 switch (*eap->arg)
9177 {
9178 case 'm': break;
9179 case NUL: len *= 1000L; break;
9180 default: EMSG2(_(e_invarg2), eap->arg); return;
9181 }
9182 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183}
9184
9185/*
9186 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9187 */
9188 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009189do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190{
9191 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009192 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193
9194 cursor_on();
9195 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009196 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009198 wait_now = msec - done > 1000L ? 1000L : msec - done;
9199#ifdef FEAT_TIMERS
9200 {
9201 long due_time = check_due_timer();
9202
9203 if (due_time > 0 && due_time < wait_now)
9204 wait_now = due_time;
9205 }
9206#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009207#ifdef FEAT_JOB_CHANNEL
9208 if (has_any_channel() && wait_now > 100L)
9209 wait_now = 100L;
9210#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009211 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009212#ifdef FEAT_JOB_CHANNEL
9213 if (has_any_channel())
9214 ui_breakcheck_force(TRUE);
9215 else
9216#endif
9217 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009218#ifdef MESSAGE_QUEUE
9219 /* Process the netbeans and clientserver messages that may have been
9220 * received in the call to ui_breakcheck() when the GUI is in use. This
9221 * may occur when running a test case. */
9222 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009225
9226 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9227 // input buffer, otherwise a following call to input() fails.
9228 if (got_int)
9229 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230}
9231
9232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009233do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234{
9235 int mode;
9236 char_u *cmdp;
9237
9238 cmdp = eap->cmd;
9239 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9240
9241 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9242 eap->arg, mode, isabbrev))
9243 {
9244 case 1: EMSG(_(e_invarg));
9245 break;
9246 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9247 break;
9248 }
9249}
9250
9251/*
9252 * ":winsize" command (obsolete).
9253 */
9254 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009255ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256{
9257 int w, h;
9258 char_u *arg = eap->arg;
9259 char_u *p;
9260
9261 w = getdigits(&arg);
9262 arg = skipwhite(arg);
9263 p = arg;
9264 h = getdigits(&arg);
9265 if (*p != NUL && *arg == NUL)
9266 set_shellsize(w, h, TRUE);
9267 else
9268 EMSG(_("E465: :winsize requires two number arguments"));
9269}
9270
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009272ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273{
9274 int xchar = NUL;
9275 char_u *p;
9276
9277 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9278 {
9279 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9280 if (eap->arg[1] == NUL)
9281 {
9282 EMSG(_(e_invarg));
9283 return;
9284 }
9285 xchar = eap->arg[1];
9286 p = eap->arg + 2;
9287 }
9288 else
9289 p = eap->arg + 1;
9290
9291 eap->nextcmd = check_nextcmd(p);
9292 p = skipwhite(p);
9293 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9294 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009295 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 {
9297 /* Pass flags on for ":vertical wincmd ]". */
9298 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009299 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9301 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009302 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 }
9304}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305
Bram Moolenaar843ee412004-06-30 16:16:41 +00009306#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307/*
9308 * ":winpos".
9309 */
9310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009311ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312{
9313 int x, y;
9314 char_u *arg = eap->arg;
9315 char_u *p;
9316
9317 if (*arg == NUL)
9318 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009319# if defined(FEAT_GUI) || defined(MSWIN)
9320# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009322# else
9323 if (mch_get_winpos(&x, &y) != FAIL)
9324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 {
9326 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9327 msg(IObuff);
9328 }
9329 else
9330# endif
9331 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9332 }
9333 else
9334 {
9335 x = getdigits(&arg);
9336 arg = skipwhite(arg);
9337 p = arg;
9338 y = getdigits(&arg);
9339 if (*p == NUL || *arg != NUL)
9340 {
9341 EMSG(_("E466: :winpos requires two number arguments"));
9342 return;
9343 }
9344# ifdef FEAT_GUI
9345 if (gui.in_use)
9346 gui_mch_set_winpos(x, y);
9347 else if (gui.starting)
9348 {
9349 /* Remember the coordinates for when the window is opened. */
9350 gui_win_x = x;
9351 gui_win_y = y;
9352 }
9353# ifdef HAVE_TGETENT
9354 else
9355# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009356# else
9357# ifdef MSWIN
9358 mch_set_winpos(x, y);
9359# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360# endif
9361# ifdef HAVE_TGETENT
9362 if (*T_CWP)
9363 term_set_winpos(x, y);
9364# endif
9365 }
9366}
9367#endif
9368
9369/*
9370 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9371 */
9372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009373ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374{
9375 oparg_T oa;
9376
9377 clear_oparg(&oa);
9378 oa.regname = eap->regname;
9379 oa.start.lnum = eap->line1;
9380 oa.end.lnum = eap->line2;
9381 oa.line_count = eap->line2 - eap->line1 + 1;
9382 oa.motion_type = MLINE;
9383#ifdef FEAT_VIRTUALEDIT
9384 virtual_op = FALSE;
9385#endif
9386 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9387 {
9388 setpcmark();
9389 curwin->w_cursor.lnum = eap->line1;
9390 beginline(BL_SOL | BL_FIX);
9391 }
9392
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009393 if (VIsual_active)
9394 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009395
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 switch (eap->cmdidx)
9397 {
9398 case CMD_delete:
9399 oa.op_type = OP_DELETE;
9400 op_delete(&oa);
9401 break;
9402
9403 case CMD_yank:
9404 oa.op_type = OP_YANK;
9405 (void)op_yank(&oa, FALSE, TRUE);
9406 break;
9407
9408 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009409 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009411 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9412#else
9413 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009415 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 oa.op_type = OP_RSHIFT;
9417 else
9418 oa.op_type = OP_LSHIFT;
9419 op_shift(&oa, FALSE, eap->amount);
9420 break;
9421 }
9422#ifdef FEAT_VIRTUALEDIT
9423 virtual_op = MAYBE;
9424#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009425 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426}
9427
9428/*
9429 * ":put".
9430 */
9431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009432ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433{
9434 /* ":0put" works like ":1put!". */
9435 if (eap->line2 == 0)
9436 {
9437 eap->line2 = 1;
9438 eap->forceit = TRUE;
9439 }
9440 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009441 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9442 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443}
9444
9445/*
9446 * Handle ":copy" and ":move".
9447 */
9448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009449ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450{
9451 long n;
9452
Bram Moolenaarded27822017-01-02 14:27:34 +01009453 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 if (eap->arg == NULL) /* error detected */
9455 {
9456 eap->nextcmd = NULL;
9457 return;
9458 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009459 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460
9461 /*
9462 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9463 */
9464 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9465 {
9466 EMSG(_(e_invaddr));
9467 return;
9468 }
9469
9470 if (eap->cmdidx == CMD_move)
9471 {
9472 if (do_move(eap->line1, eap->line2, n) == FAIL)
9473 return;
9474 }
9475 else
9476 ex_copy(eap->line1, eap->line2, n);
9477 u_clearline();
9478 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009479 ex_may_print(eap);
9480}
9481
9482/*
9483 * Print the current line if flags were given to the Ex command.
9484 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009485 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009486ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009487{
9488 if (eap->flags != 0)
9489 {
9490 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9491 (eap->flags & EXFLAG_LIST));
9492 ex_no_reprint = TRUE;
9493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494}
9495
9496/*
9497 * ":smagic" and ":snomagic".
9498 */
9499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009500ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502 int magic_save = p_magic;
9503
9504 p_magic = (eap->cmdidx == CMD_smagic);
9505 do_sub(eap);
9506 p_magic = magic_save;
9507}
9508
9509/*
9510 * ":join".
9511 */
9512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009513ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514{
9515 curwin->w_cursor.lnum = eap->line1;
9516 if (eap->line1 == eap->line2)
9517 {
9518 if (eap->addr_count >= 2) /* :2,2join does nothing */
9519 return;
9520 if (eap->line2 == curbuf->b_ml.ml_line_count)
9521 {
9522 beep_flush();
9523 return;
9524 }
9525 ++eap->line2;
9526 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009527 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009529 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530}
9531
9532/*
9533 * ":[addr]@r" or ":[addr]*r": execute register
9534 */
9535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009536ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537{
9538 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009539 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540
9541 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009542 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543
9544#ifdef USE_ON_FLY_SCROLL
9545 dont_scroll = TRUE; /* disallow scrolling here */
9546#endif
9547
9548 /* get the register name. No name means to use the previous one */
9549 c = *eap->arg;
9550 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9551 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009552 /* Put the register in the typeahead buffer with the "silent" flag. */
9553 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9554 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009555 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 else
9559 {
9560 int save_efr = exec_from_reg;
9561
9562 exec_from_reg = TRUE;
9563
9564 /*
9565 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009566 * Continue until the stuff buffer is empty and all added characters
9567 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009569 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9571
9572 exec_from_reg = save_efr;
9573 }
9574}
9575
9576/*
9577 * ":!".
9578 */
9579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009580ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581{
9582 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9583}
9584
9585/*
9586 * ":undo".
9587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009589ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009591 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009592 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009593 else
9594 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595}
9596
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009597#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009599ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009600{
9601 char_u hash[UNDO_HASH_SIZE];
9602
9603 u_compute_hash(hash);
9604 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9605}
9606
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009608ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009609{
9610 char_u hash[UNDO_HASH_SIZE];
9611
9612 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009613 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009614}
9615#endif
9616
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617/*
9618 * ":redo".
9619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009621ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622{
9623 u_redo(1);
9624}
9625
9626/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009627 * ":earlier" and ":later".
9628 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009630ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009631{
9632 long count = 0;
9633 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009634 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009635 char_u *p = eap->arg;
9636
9637 if (*p == NUL)
9638 count = 1;
9639 else if (isdigit(*p))
9640 {
9641 count = getdigits(&p);
9642 switch (*p)
9643 {
9644 case 's': ++p; sec = TRUE; break;
9645 case 'm': ++p; sec = TRUE; count *= 60; break;
9646 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009647 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9648 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009649 }
9650 }
9651
9652 if (*p != NUL)
9653 EMSG2(_(e_invarg2), eap->arg);
9654 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009655 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9656 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009657}
9658
9659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 * ":redir": start/stop redirection.
9661 */
9662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009663ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
9665 char *mode;
9666 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009667 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668
Bram Moolenaarba768492016-07-08 15:32:54 +02009669#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009670 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009671 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009672 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009673 return;
9674 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009675#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009676
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 if (STRICMP(eap->arg, "END") == 0)
9678 close_redir();
9679 else
9680 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009681 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009683 ++arg;
9684 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009686 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 mode = "a";
9688 }
9689 else
9690 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009691 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
9693 close_redir();
9694
9695 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009696 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 if (fname == NULL)
9698 return;
9699#ifdef FEAT_BROWSE
9700 if (cmdmod.browse)
9701 {
9702 char_u *browseFile;
9703
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009704 browseFile = do_browse(BROWSE_SAVE,
9705 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009706 fname, NULL, NULL,
9707 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 if (browseFile == NULL)
9709 return; /* operation cancelled */
9710 vim_free(fname);
9711 fname = browseFile;
9712 eap->forceit = TRUE; /* since dialog already asked */
9713 }
9714#endif
9715
9716 redir_fd = open_exfile(fname, eap->forceit, mode);
9717 vim_free(fname);
9718 }
9719#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009720 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 {
9722 /* redirect to a register a-z (resp. A-Z for appending) */
9723 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009724 ++arg;
9725 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009727 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009728 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009730 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009732 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009733 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009734 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009735 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009737 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009738 if (*arg == '>')
9739 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009740 /* Make register empty when not using @A-@Z and the
9741 * command is valid. */
9742 if (*arg == NUL && !isupper(redir_reg))
9743 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009745 }
9746 if (*arg != NUL)
9747 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009748 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009749 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009750 }
9751 }
9752 else if (*arg == '=' && arg[1] == '>')
9753 {
9754 int append;
9755
9756 /* redirect to a variable */
9757 close_redir();
9758 arg += 2;
9759
9760 if (*arg == '>')
9761 {
9762 ++arg;
9763 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 }
9765 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009766 append = FALSE;
9767
9768 if (var_redir_start(skipwhite(arg), append) == OK)
9769 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 }
9771#endif
9772
9773 /* TODO: redirect to a buffer */
9774
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009776 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009778
9779 /* Make sure redirection is not off. Can happen for cmdline completion
9780 * that indirectly invokes a command to catch its output. */
9781 if (redir_fd != NULL
9782#ifdef FEAT_EVAL
9783 || redir_reg || redir_vname
9784#endif
9785 )
9786 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787}
9788
9789/*
9790 * ":redraw": force redraw
9791 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009792 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009793ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794{
9795 int r = RedrawingDisabled;
9796 int p = p_lz;
9797
9798 RedrawingDisabled = 0;
9799 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009800 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009802 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803#ifdef FEAT_TITLE
9804 if (need_maketitle)
9805 maketitle();
9806#endif
9807 RedrawingDisabled = r;
9808 p_lz = p;
9809
9810 /* Reset msg_didout, so that a message that's there is overwritten. */
9811 msg_didout = FALSE;
9812 msg_col = 0;
9813
Bram Moolenaar56667a52013-07-17 11:54:28 +02009814 /* No need to wait after an intentional redraw. */
9815 need_wait_return = FALSE;
9816
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 out_flush();
9818}
9819
9820/*
9821 * ":redrawstatus": force redraw of status line(s)
9822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009824ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 int r = RedrawingDisabled;
9827 int p = p_lz;
9828
9829 RedrawingDisabled = 0;
9830 p_lz = FALSE;
9831 if (eap->forceit)
9832 status_redraw_all();
9833 else
9834 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009835 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 RedrawingDisabled = r;
9837 p_lz = p;
9838 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839}
9840
9841 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009842close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843{
9844 if (redir_fd != NULL)
9845 {
9846 fclose(redir_fd);
9847 redir_fd = NULL;
9848 }
9849#ifdef FEAT_EVAL
9850 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009851 if (redir_vname)
9852 {
9853 var_redir_stop();
9854 redir_vname = 0;
9855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856#endif
9857}
9858
9859#if defined(FEAT_SESSION) && defined(USE_CRNL)
9860# define MKSESSION_NL
9861static int mksession_nl = FALSE; /* use NL only in put_eol() */
9862#endif
9863
9864/*
9865 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9866 */
9867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009868ex_mkrc(
9869 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870{
9871 FILE *fd;
9872 int failed = FALSE;
9873 char_u *fname;
9874#ifdef FEAT_BROWSE
9875 char_u *browseFile = NULL;
9876#endif
9877#ifdef FEAT_SESSION
9878 int view_session = FALSE;
9879 int using_vdir = FALSE; /* using 'viewdir'? */
9880 char_u *viewFile = NULL;
9881 unsigned *flagp;
9882#endif
9883
9884 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9885 {
9886#ifdef FEAT_SESSION
9887 view_session = TRUE;
9888#else
9889 ex_ni(eap);
9890 return;
9891#endif
9892 }
9893
9894#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009895 /* Use the short file name until ":lcd" is used. We also don't use the
9896 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009897 did_lcd = FALSE;
9898
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9900 if (eap->cmdidx == CMD_mkview
9901 && (*eap->arg == NUL
9902 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9903 {
9904 eap->forceit = TRUE;
9905 fname = get_view_file(*eap->arg);
9906 if (fname == NULL)
9907 return;
9908 viewFile = fname;
9909 using_vdir = TRUE;
9910 }
9911 else
9912#endif
9913 if (*eap->arg != NUL)
9914 fname = eap->arg;
9915 else if (eap->cmdidx == CMD_mkvimrc)
9916 fname = (char_u *)VIMRC_FILE;
9917#ifdef FEAT_SESSION
9918 else if (eap->cmdidx == CMD_mksession)
9919 fname = (char_u *)SESSION_FILE;
9920#endif
9921 else
9922 fname = (char_u *)EXRC_FILE;
9923
9924#ifdef FEAT_BROWSE
9925 if (cmdmod.browse)
9926 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009927 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928# ifdef FEAT_SESSION
9929 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9930 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9931# endif
9932 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009933 fname, (char_u *)"vim", NULL,
9934 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 if (browseFile == NULL)
9936 goto theend;
9937 fname = browseFile;
9938 eap->forceit = TRUE; /* since dialog already asked */
9939 }
9940#endif
9941
9942#if defined(FEAT_SESSION) && defined(vim_mkdir)
9943 /* When using 'viewdir' may have to create the directory. */
9944 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009945 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946#endif
9947
9948 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9949 if (fd != NULL)
9950 {
9951#ifdef FEAT_SESSION
9952 if (eap->cmdidx == CMD_mkview)
9953 flagp = &vop_flags;
9954 else
9955 flagp = &ssop_flags;
9956#endif
9957
9958#ifdef MKSESSION_NL
9959 /* "unix" in 'sessionoptions': use NL line separator */
9960 if (view_session && (*flagp & SSOP_UNIX))
9961 mksession_nl = TRUE;
9962#endif
9963
9964 /* Write the version command for :mkvimrc */
9965 if (eap->cmdidx == CMD_mkvimrc)
9966 (void)put_line(fd, "version 6.0");
9967
9968#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009969 if (eap->cmdidx == CMD_mksession)
9970 {
9971 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9972 failed = TRUE;
9973 }
9974
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 if (eap->cmdidx != CMD_mkview)
9976#endif
9977 {
9978 /* Write setting 'compatible' first, because it has side effects.
9979 * For that same reason only do it when needed. */
9980 if (p_cp)
9981 (void)put_line(fd, "if !&cp | set cp | endif");
9982 else
9983 (void)put_line(fd, "if &cp | set nocp | endif");
9984 }
9985
9986#ifdef FEAT_SESSION
9987 if (!view_session
9988 || (eap->cmdidx == CMD_mksession
9989 && (*flagp & SSOP_OPTIONS)))
9990#endif
9991 failed |= (makemap(fd, NULL) == FAIL
9992 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9993
9994#ifdef FEAT_SESSION
9995 if (!failed && view_session)
9996 {
9997 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9998 failed = TRUE;
9999 if (eap->cmdidx == CMD_mksession)
10000 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010001 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002
Bram Moolenaard9462e32011-04-11 21:35:11 +020010003 dirnow = alloc(MAXPATHL);
10004 if (dirnow == NULL)
10005 failed = TRUE;
10006 else
10007 {
10008 /*
10009 * Change to session file's dir.
10010 */
10011 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010013 *dirnow = NUL;
10014 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10015 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010016 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010017 shorten_fnames(TRUE);
10018 }
10019 else if (*dirnow != NUL
10020 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10021 {
10022 if (mch_chdir((char *)globaldir) == 0)
10023 shorten_fnames(TRUE);
10024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025
Bram Moolenaard9462e32011-04-11 21:35:11 +020010026 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027
Bram Moolenaard9462e32011-04-11 21:35:11 +020010028 /* restore original dir */
10029 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010031 {
10032 if (mch_chdir((char *)dirnow) != 0)
10033 EMSG(_(e_prev_dir));
10034 shorten_fnames(TRUE);
10035 }
10036 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 }
10038 }
10039 else
10040 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010041 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10042 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 }
10044 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10045 == FAIL)
10046 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010047 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10048 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010049 if (eap->cmdidx == CMD_mksession)
10050 {
10051 if (put_line(fd, "unlet SessionLoad") == FAIL)
10052 failed = TRUE;
10053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 }
10055#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010056 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10057 failed = TRUE;
10058
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 failed |= fclose(fd);
10060
10061 if (failed)
10062 EMSG(_(e_write));
10063#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10064 else if (eap->cmdidx == CMD_mksession)
10065 {
10066 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010067 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068
Bram Moolenaard9462e32011-04-11 21:35:11 +020010069 tbuf = alloc(MAXPATHL);
10070 if (tbuf != NULL)
10071 {
10072 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10073 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10074 vim_free(tbuf);
10075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 }
10077#endif
10078#ifdef MKSESSION_NL
10079 mksession_nl = FALSE;
10080#endif
10081 }
10082
10083#ifdef FEAT_BROWSE
10084theend:
10085 vim_free(browseFile);
10086#endif
10087#ifdef FEAT_SESSION
10088 vim_free(viewFile);
10089#endif
10090}
10091
Bram Moolenaardf177f62005-02-22 08:39:57 +000010092#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10093 || defined(PROTO)
10094 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010095vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010096{
10097 if (vim_mkdir(name, prot) != 0)
10098 {
10099 EMSG2(_("E739: Cannot create directory: %s"), name);
10100 return FAIL;
10101 }
10102 return OK;
10103}
10104#endif
10105
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106/*
10107 * Open a file for writing for an Ex command, with some checks.
10108 * Return file descriptor, or NULL on failure.
10109 */
10110 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010111open_exfile(
10112 char_u *fname,
10113 int forceit,
10114 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115{
10116 FILE *fd;
10117
10118#ifdef UNIX
10119 /* with Unix it is possible to open a directory */
10120 if (mch_isdir(fname))
10121 {
10122 EMSG2(_(e_isadir2), fname);
10123 return NULL;
10124 }
10125#endif
10126 if (!forceit && *mode != 'a' && vim_fexists(fname))
10127 {
10128 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10129 return NULL;
10130 }
10131
10132 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10133 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10134
10135 return fd;
10136}
10137
10138/*
10139 * ":mark" and ":k".
10140 */
10141 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010142ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143{
10144 pos_T pos;
10145
10146 if (*eap->arg == NUL) /* No argument? */
10147 EMSG(_(e_argreq));
10148 else if (eap->arg[1] != NUL) /* more than one character? */
10149 EMSG(_(e_trailing));
10150 else
10151 {
10152 pos = curwin->w_cursor; /* save curwin->w_cursor */
10153 curwin->w_cursor.lnum = eap->line2;
10154 beginline(BL_WHITE | BL_FIX);
10155 if (setmark(*eap->arg) == FAIL) /* set mark */
10156 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10157 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10158 }
10159}
10160
10161/*
10162 * Update w_topline, w_leftcol and the cursor position.
10163 */
10164 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010165update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166{
10167 check_cursor(); /* put cursor on valid line */
10168 update_topline();
10169 if (!curwin->w_p_wrap)
10170 validate_cursor();
10171 update_curswant();
10172}
10173
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010175 * Save the current State and go to Normal mode.
10176 * Return TRUE if the typeahead could be saved.
10177 */
10178 int
10179save_current_state(save_state_T *sst)
10180{
10181 sst->save_msg_scroll = msg_scroll;
10182 sst->save_restart_edit = restart_edit;
10183 sst->save_msg_didout = msg_didout;
10184 sst->save_State = State;
10185 sst->save_insertmode = p_im;
10186 sst->save_finish_op = finish_op;
10187 sst->save_opcount = opcount;
10188
10189 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10190 restart_edit = 0; /* don't go to Insert mode */
10191 p_im = FALSE; /* don't use 'insertmode' */
10192
10193 /*
10194 * Save the current typeahead. This is required to allow using ":normal"
10195 * from an event handler and makes sure we don't hang when the argument
10196 * ends with half a command.
10197 */
10198 save_typeahead(&sst->tabuf);
10199 return sst->tabuf.typebuf_valid;
10200}
10201
10202 void
10203restore_current_state(save_state_T *sst)
10204{
10205 /* Restore the previous typeahead. */
10206 restore_typeahead(&sst->tabuf);
10207
10208 msg_scroll = sst->save_msg_scroll;
10209 restart_edit = sst->save_restart_edit;
10210 p_im = sst->save_insertmode;
10211 finish_op = sst->save_finish_op;
10212 opcount = sst->save_opcount;
10213 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10214
10215 /* Restore the state (needed when called from a function executed for
10216 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10217 State = sst->save_State;
10218#ifdef CURSOR_SHAPE
10219 ui_cursor_shape(); /* may show different cursor shape */
10220#endif
10221}
10222
10223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 * ":normal[!] {commands}": Execute normal mode commands.
10225 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010226 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010227ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010229 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230#ifdef FEAT_MBYTE
10231 char_u *arg = NULL;
10232 int l;
10233 char_u *p;
10234#endif
10235
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010236 if (ex_normal_lock > 0)
10237 {
10238 EMSG(_(e_secure));
10239 return;
10240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 if (ex_normal_busy >= p_mmd)
10242 {
10243 EMSG(_("E192: Recursive use of :normal too deep"));
10244 return;
10245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246
10247#ifdef FEAT_MBYTE
10248 /*
10249 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10250 * this for the K_SPECIAL leading byte, otherwise special keys will not
10251 * work.
10252 */
10253 if (has_mbyte)
10254 {
10255 int len = 0;
10256
10257 /* Count the number of characters to be escaped. */
10258 for (p = eap->arg; *p != NUL; ++p)
10259 {
10260# ifdef FEAT_GUI
10261 if (*p == CSI) /* leadbyte CSI */
10262 len += 2;
10263# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010264 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10266# ifdef FEAT_GUI
10267 || *p == CSI
10268# endif
10269 )
10270 len += 2;
10271 }
10272 if (len > 0)
10273 {
10274 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10275 if (arg != NULL)
10276 {
10277 len = 0;
10278 for (p = eap->arg; *p != NUL; ++p)
10279 {
10280 arg[len++] = *p;
10281# ifdef FEAT_GUI
10282 if (*p == CSI)
10283 {
10284 arg[len++] = KS_EXTRA;
10285 arg[len++] = (int)KE_CSI;
10286 }
10287# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010288 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 {
10290 arg[len++] = *++p;
10291 if (*p == K_SPECIAL)
10292 {
10293 arg[len++] = KS_SPECIAL;
10294 arg[len++] = KE_FILLER;
10295 }
10296# ifdef FEAT_GUI
10297 else if (*p == CSI)
10298 {
10299 arg[len++] = KS_EXTRA;
10300 arg[len++] = (int)KE_CSI;
10301 }
10302# endif
10303 }
10304 arg[len] = NUL;
10305 }
10306 }
10307 }
10308 }
10309#endif
10310
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010311 ++ex_normal_busy;
10312 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 {
10314 /*
10315 * Repeat the :normal command for each line in the range. When no
10316 * range given, execute it just once, without positioning the cursor
10317 * first.
10318 */
10319 do
10320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 if (eap->addr_count != 0)
10322 {
10323 curwin->w_cursor.lnum = eap->line1++;
10324 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010325 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 }
10327
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010328 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329#ifdef FEAT_MBYTE
10330 arg != NULL ? arg :
10331#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010332 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 }
10334 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10335 }
10336
10337 /* Might not return to the main loop when in an event handler. */
10338 update_topline_cursor();
10339
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010340 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010342#ifdef FEAT_MOUSE
10343 setmouse();
10344#endif
10345#ifdef CURSOR_SHAPE
10346 ui_cursor_shape(); /* may show different cursor shape */
10347#endif
10348
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349#ifdef FEAT_MBYTE
10350 vim_free(arg);
10351#endif
10352}
10353
10354/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010355 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 */
10357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010358ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010360 if (eap->forceit)
10361 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010362 /* cursor line can be zero on startup */
10363 if (!curwin->w_cursor.lnum)
10364 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010365 coladvance((colnr_T)MAXCOL);
10366 curwin->w_curswant = MAXCOL;
10367 curwin->w_set_curswant = FALSE;
10368 }
10369
Bram Moolenaara40c5002005-01-09 21:16:21 +000010370 /* Ignore the command when already in Insert mode. Inserting an
10371 * expression register that invokes a function can do this. */
10372 if (State & INSERT)
10373 return;
10374
Bram Moolenaar64969662005-12-14 21:59:55 +000010375 if (eap->cmdidx == CMD_startinsert)
10376 restart_edit = 'a';
10377 else if (eap->cmdidx == CMD_startreplace)
10378 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010380 restart_edit = 'V';
10381
10382 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010384 if (eap->cmdidx == CMD_startinsert)
10385 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 curwin->w_curswant = 0; /* avoid MAXCOL */
10387 }
10388}
10389
10390/*
10391 * ":stopinsert"
10392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010394ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395{
10396 restart_edit = 0;
10397 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010398 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010401/*
10402 * Execute normal mode command "cmd".
10403 * "remap" can be REMAP_NONE or REMAP_YES.
10404 */
10405 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010406exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010407{
Bram Moolenaar25281632016-01-21 23:32:32 +010010408 /* Stuff the argument into the typeahead buffer. */
10409 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010410 exec_normal(FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010411}
Bram Moolenaar25281632016-01-21 23:32:32 +010010412
Bram Moolenaar25281632016-01-21 23:32:32 +010010413/*
10414 * Execute normal_cmd() until there is no typeahead left.
10415 */
10416 void
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010417exec_normal(int was_typed, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010418{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010419 oparg_T oa;
10420
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010421 clear_oparg(&oa);
10422 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010423 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10424 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010425 {
10426 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010427#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010428 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010429 && oa.op_type == OP_NOP && oa.regname == NUL
10430 && !VIsual_active)
10431 {
10432 /* If terminal_loop() returns OK we got a key that is handled
10433 * in Normal model. With FAIL we first need to position the
10434 * cursor and the screen needs to be redrawn. */
10435 if (terminal_loop(TRUE) == OK)
10436 normal_cmd(&oa, TRUE);
10437 }
10438 else
10439#endif
10440 /* execute a Normal mode cmd */
10441 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010442 }
10443}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010444
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445#ifdef FEAT_FIND_ID
10446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010447ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448{
10449 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10450 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10451 (linenr_T)1, (linenr_T)MAXLNUM);
10452}
10453
Bram Moolenaar4033c552017-09-16 20:54:51 +020010454#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455/*
10456 * ":psearch"
10457 */
10458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010459ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460{
10461 g_do_tagpreview = p_pvh;
10462 ex_findpat(eap);
10463 g_do_tagpreview = 0;
10464}
10465#endif
10466
10467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010468ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469{
10470 int whole = TRUE;
10471 long n;
10472 char_u *p;
10473 int action;
10474
10475 switch (cmdnames[eap->cmdidx].cmd_name[2])
10476 {
10477 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10478 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10479 action = ACTION_GOTO;
10480 else
10481 action = ACTION_SHOW;
10482 break;
10483 case 'i': /* ":ilist" and ":dlist" */
10484 action = ACTION_SHOW_ALL;
10485 break;
10486 case 'u': /* ":ijump" and ":djump" */
10487 action = ACTION_GOTO;
10488 break;
10489 default: /* ":isplit" and ":dsplit" */
10490 action = ACTION_SPLIT;
10491 break;
10492 }
10493
10494 n = 1;
10495 if (vim_isdigit(*eap->arg)) /* get count */
10496 {
10497 n = getdigits(&eap->arg);
10498 eap->arg = skipwhite(eap->arg);
10499 }
10500 if (*eap->arg == '/') /* Match regexp, not just whole words */
10501 {
10502 whole = FALSE;
10503 ++eap->arg;
10504 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10505 if (*p)
10506 {
10507 *p++ = NUL;
10508 p = skipwhite(p);
10509
10510 /* Check for trailing illegal characters */
10511 if (!ends_excmd(*p))
10512 eap->errmsg = e_trailing;
10513 else
10514 eap->nextcmd = check_nextcmd(p);
10515 }
10516 }
10517 if (!eap->skip)
10518 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10519 whole, !eap->forceit,
10520 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10521 n, action, eap->line1, eap->line2);
10522}
10523#endif
10524
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525
Bram Moolenaar4033c552017-09-16 20:54:51 +020010526#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527/*
10528 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10529 */
10530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010531ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010533 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10535}
10536
10537/*
10538 * ":pedit"
10539 */
10540 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010541ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542{
10543 win_T *curwin_save = curwin;
10544
10545 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010546 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010547 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 do_exedit(eap, NULL);
10549 keep_help_flag = FALSE;
10550 if (curwin != curwin_save && win_valid(curwin_save))
10551 {
10552 /* Return cursor to where we were */
10553 validate_cursor();
10554 redraw_later(VALID);
10555 win_enter(curwin_save, TRUE);
10556 }
10557 g_do_tagpreview = 0;
10558}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560
10561/*
10562 * ":stag", ":stselect" and ":stjump".
10563 */
10564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010565ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566{
10567 postponed_split = -1;
10568 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010569 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10571 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010572 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574
10575/*
10576 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10577 */
10578 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010579ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580{
10581 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10582}
10583
10584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010585ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586{
10587 int cmd;
10588
10589 switch (name[1])
10590 {
10591 case 'j': cmd = DT_JUMP; /* ":tjump" */
10592 break;
10593 case 's': cmd = DT_SELECT; /* ":tselect" */
10594 break;
10595 case 'p': cmd = DT_PREV; /* ":tprevious" */
10596 break;
10597 case 'N': cmd = DT_PREV; /* ":tNext" */
10598 break;
10599 case 'n': cmd = DT_NEXT; /* ":tnext" */
10600 break;
10601 case 'o': cmd = DT_POP; /* ":pop" */
10602 break;
10603 case 'f': /* ":tfirst" */
10604 case 'r': cmd = DT_FIRST; /* ":trewind" */
10605 break;
10606 case 'l': cmd = DT_LAST; /* ":tlast" */
10607 break;
10608 default: /* ":tag" */
10609#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010610 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010612 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 return;
10614 }
10615#endif
10616 cmd = DT_TAG;
10617 break;
10618 }
10619
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010620 if (name[0] == 'l')
10621 {
10622#ifndef FEAT_QUICKFIX
10623 ex_ni(eap);
10624 return;
10625#else
10626 cmd = DT_LTAG;
10627#endif
10628 }
10629
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10631 eap->forceit, TRUE);
10632}
10633
10634/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010635 * Check "str" for starting with a special cmdline variable.
10636 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10637 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10638 */
10639 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010640find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010641{
10642 int len;
10643 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010644 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010645 "%",
10646#define SPEC_PERC 0
10647 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010648#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010649 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010650#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010651 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010652#define SPEC_CCWORD (SPEC_CWORD + 1)
10653 "<cexpr>", /* expr under cursor */
10654#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010655 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010656#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010657 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010658#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010659 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010660#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010661 "<afile>", /* autocommand file name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010662#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010663 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010664#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010665 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010666#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010667#ifdef FEAT_CLIENTSERVER
10668 "<client>"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010669# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010670#endif
10671 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010672
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010673 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010674 {
10675 len = (int)STRLEN(spec_str[i]);
10676 if (STRNCMP(src, spec_str[i], len) == 0)
10677 {
10678 *usedlen = len;
10679 return i;
10680 }
10681 }
10682 return -1;
10683}
10684
10685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 * Evaluate cmdline variables.
10687 *
10688 * change '%' to curbuf->b_ffname
10689 * '#' to curwin->w_altfile
10690 * '<cword>' to word under the cursor
10691 * '<cWORD>' to WORD under the cursor
10692 * '<cfile>' to path name under the cursor
10693 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010694 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 * '<afile>' to file name for autocommand
10696 * '<abuf>' to buffer number for autocommand
10697 * '<amatch>' to matching name for autocommand
10698 *
10699 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10700 * "" for error without a message) and NULL is returned.
10701 * Returns an allocated string if a valid match was found.
10702 * Returns NULL if no match was found. "usedlen" then still contains the
10703 * number of characters to skip.
10704 */
10705 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010706eval_vars(
10707 char_u *src, /* pointer into commandline */
10708 char_u *srcstart, /* beginning of valid memory for src */
10709 int *usedlen, /* characters after src that are used */
10710 linenr_T *lnump, /* line number for :e command, or NULL */
10711 char_u **errormsg, /* pointer to error message */
10712 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010713 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714{
10715 int i;
10716 char_u *s;
10717 char_u *result;
10718 char_u *resultbuf = NULL;
10719 int resultlen;
10720 buf_T *buf;
10721 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10722 int spec_idx;
10723#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010724 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 int skip_mod = FALSE;
10726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728
10729 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010730 if (escaped != NULL)
10731 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732
10733 /*
10734 * Check if there is something to do.
10735 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010736 spec_idx = find_cmdline_var(src, usedlen);
10737 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 {
10739 *usedlen = 1;
10740 return NULL;
10741 }
10742
10743 /*
10744 * Skip when preceded with a backslash "\%" and "\#".
10745 * Note: In "\\%" the % is also not recognized!
10746 */
10747 if (src > srcstart && src[-1] == '\\')
10748 {
10749 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010750 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 return NULL;
10752 }
10753
10754 /*
10755 * word or WORD under cursor
10756 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010757 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10758 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010760 resultlen = find_ident_under_cursor(&result,
10761 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10762 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10763 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 if (resultlen == 0)
10765 {
10766 *errormsg = (char_u *)"";
10767 return NULL;
10768 }
10769 }
10770
10771 /*
10772 * '#': Alternate file name
10773 * '%': Current file name
10774 * File name under the cursor
10775 * File name for autocommand
10776 * and following modifiers
10777 */
10778 else
10779 {
10780 switch (spec_idx)
10781 {
10782 case SPEC_PERC: /* '%': current file */
10783 if (curbuf->b_fname == NULL)
10784 {
10785 result = (char_u *)"";
10786 valid = 0; /* Must have ":p:h" to be valid */
10787 }
10788 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010789 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010791#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010792 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010793#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 break;
10796
10797 case SPEC_HASH: /* '#' or "#99": alternate file */
10798 if (src[1] == '#') /* "##": the argument list */
10799 {
10800 result = arg_all();
10801 resultbuf = result;
10802 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010803 if (escaped != NULL)
10804 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805#ifdef FEAT_MODIFY_FNAME
10806 skip_mod = TRUE;
10807#endif
10808 break;
10809 }
10810 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010811 if (*s == '<') /* "#<99" uses v:oldfiles */
10812 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010814 if (s == src + 2 && src[1] == '-')
10815 /* just a minus sign, don't skip over it */
10816 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 *usedlen = (int)(s - src); /* length of what we expand */
10818
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010819 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010821 if (*usedlen < 2)
10822 {
10823 /* Should we give an error message for #<text? */
10824 *usedlen = 1;
10825 return NULL;
10826 }
10827#ifdef FEAT_EVAL
10828 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10829 (long)i);
10830 if (result == NULL)
10831 {
10832 *errormsg = (char_u *)"";
10833 return NULL;
10834 }
10835#else
10836 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 }
10840 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010841 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010842 if (i == 0 && src[1] == '<' && *usedlen > 1)
10843 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010844 buf = buflist_findnr(i);
10845 if (buf == NULL)
10846 {
10847 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10848 return NULL;
10849 }
10850 if (lnump != NULL)
10851 *lnump = ECMD_LAST;
10852 if (buf->b_fname == NULL)
10853 {
10854 result = (char_u *)"";
10855 valid = 0; /* Must have ":p:h" to be valid */
10856 }
10857 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010858 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010859 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010860#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010861 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010862#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010863 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 break;
10866
10867#ifdef FEAT_SEARCHPATH
10868 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010869 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 if (result == NULL)
10871 {
10872 *errormsg = (char_u *)"";
10873 return NULL;
10874 }
10875 resultbuf = result; /* remember allocated string */
10876 break;
10877#endif
10878
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 case SPEC_AFILE: /* file name for autocommand */
10880 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010881 if (result != NULL && !autocmd_fname_full)
10882 {
10883 /* Still need to turn the fname into a full path. It is
10884 * postponed to avoid a delay when <afile> is not used. */
10885 autocmd_fname_full = TRUE;
10886 result = FullName_save(autocmd_fname, FALSE);
10887 vim_free(autocmd_fname);
10888 autocmd_fname = result;
10889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 if (result == NULL)
10891 {
10892 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10893 return NULL;
10894 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010895 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 break;
10897
10898 case SPEC_ABUF: /* buffer number for autocommand */
10899 if (autocmd_bufnr <= 0)
10900 {
10901 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10902 return NULL;
10903 }
10904 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10905 result = strbuf;
10906 break;
10907
10908 case SPEC_AMATCH: /* match name for autocommand */
10909 result = autocmd_match;
10910 if (result == NULL)
10911 {
10912 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10913 return NULL;
10914 }
10915 break;
10916
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 case SPEC_SFILE: /* file name for ":so" command */
10918 result = sourcing_name;
10919 if (result == NULL)
10920 {
10921 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10922 return NULL;
10923 }
10924 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010925 case SPEC_SLNUM: /* line in file for ":so" command */
10926 if (sourcing_name == NULL || sourcing_lnum == 0)
10927 {
10928 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10929 return NULL;
10930 }
10931 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10932 result = strbuf;
10933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934#if defined(FEAT_CLIENTSERVER)
10935 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010936 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10937 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 result = strbuf;
10939 break;
10940#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010941 default:
10942 result = (char_u *)""; /* avoid gcc warning */
10943 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 }
10945
10946 resultlen = (int)STRLEN(result); /* length of new string */
10947 if (src[*usedlen] == '<') /* remove the file name extension */
10948 {
10949 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 resultlen = (int)(s - result);
10952 }
10953#ifdef FEAT_MODIFY_FNAME
10954 else if (!skip_mod)
10955 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010956 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 &resultlen);
10958 if (result == NULL)
10959 {
10960 *errormsg = (char_u *)"";
10961 return NULL;
10962 }
10963 }
10964#endif
10965 }
10966
10967 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10968 {
10969 if (valid != VALID_HEAD + VALID_PATH)
10970 /* xgettext:no-c-format */
10971 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10972 else
10973 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10974 result = NULL;
10975 }
10976 else
10977 result = vim_strnsave(result, resultlen);
10978 vim_free(resultbuf);
10979 return result;
10980}
10981
10982/*
10983 * Concatenate all files in the argument list, separated by spaces, and return
10984 * it in one allocated string.
10985 * Spaces and backslashes in the file names are escaped with a backslash.
10986 * Returns NULL when out of memory.
10987 */
10988 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010989arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990{
10991 int len;
10992 int idx;
10993 char_u *retval = NULL;
10994 char_u *p;
10995
10996 /*
10997 * Do this loop two times:
10998 * first time: compute the total length
10999 * second time: concatenate the names
11000 */
11001 for (;;)
11002 {
11003 len = 0;
11004 for (idx = 0; idx < ARGCOUNT; ++idx)
11005 {
11006 p = alist_name(&ARGLIST[idx]);
11007 if (p != NULL)
11008 {
11009 if (len > 0)
11010 {
11011 /* insert a space in between names */
11012 if (retval != NULL)
11013 retval[len] = ' ';
11014 ++len;
11015 }
11016 for ( ; *p != NUL; ++p)
11017 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011018 if (*p == ' '
11019#ifndef BACKSLASH_IN_FILENAME
11020 || *p == '\\'
11021#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011022 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 {
11024 /* insert a backslash */
11025 if (retval != NULL)
11026 retval[len] = '\\';
11027 ++len;
11028 }
11029 if (retval != NULL)
11030 retval[len] = *p;
11031 ++len;
11032 }
11033 }
11034 }
11035
11036 /* second time: break here */
11037 if (retval != NULL)
11038 {
11039 retval[len] = NUL;
11040 break;
11041 }
11042
11043 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011044 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 if (retval == NULL)
11046 break;
11047 }
11048
11049 return retval;
11050}
11051
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052/*
11053 * Expand the <sfile> string in "arg".
11054 *
11055 * Returns an allocated string, or NULL for any error.
11056 */
11057 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011058expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059{
11060 char_u *errormsg;
11061 int len;
11062 char_u *result;
11063 char_u *newres;
11064 char_u *repl;
11065 int srclen;
11066 char_u *p;
11067
11068 result = vim_strsave(arg);
11069 if (result == NULL)
11070 return NULL;
11071
11072 for (p = result; *p; )
11073 {
11074 if (STRNCMP(p, "<sfile>", 7) != 0)
11075 ++p;
11076 else
11077 {
11078 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011079 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 if (errormsg != NULL)
11081 {
11082 if (*errormsg)
11083 emsg(errormsg);
11084 vim_free(result);
11085 return NULL;
11086 }
11087 if (repl == NULL) /* no match (cannot happen) */
11088 {
11089 p += srclen;
11090 continue;
11091 }
11092 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11093 newres = alloc(len);
11094 if (newres == NULL)
11095 {
11096 vim_free(repl);
11097 vim_free(result);
11098 return NULL;
11099 }
11100 mch_memmove(newres, result, (size_t)(p - result));
11101 STRCPY(newres + (p - result), repl);
11102 len = (int)STRLEN(newres);
11103 STRCAT(newres, p + srclen);
11104 vim_free(repl);
11105 vim_free(result);
11106 result = newres;
11107 p = newres + len; /* continue after the match */
11108 }
11109 }
11110
11111 return result;
11112}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113
11114#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011115static int ses_winsizes(FILE *fd, int restore_size,
11116 win_T *tab_firstwin);
11117static int ses_win_rec(FILE *fd, frame_T *fr);
11118static frame_T *ses_skipframe(frame_T *fr);
11119static int ses_do_frame(frame_T *fr);
11120static int ses_do_win(win_T *wp);
11121static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11122static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011123static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124
11125/*
11126 * Write openfile commands for the current buffers to an .exrc file.
11127 * Return FAIL on error, OK otherwise.
11128 */
11129 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011130makeopens(
11131 FILE *fd,
11132 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133{
11134 buf_T *buf;
11135 int only_save_windows = TRUE;
11136 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 int restore_size = TRUE;
11138 win_T *wp;
11139 char_u *sname;
11140 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011141 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011142 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011143 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011144 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011145 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011146 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147
11148 if (ssop_flags & SSOP_BUFFERS)
11149 only_save_windows = FALSE; /* Save ALL buffers */
11150
11151 /*
11152 * Begin by setting the this_session variable, and then other
11153 * sessionable variables.
11154 */
11155#ifdef FEAT_EVAL
11156 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11157 return FAIL;
11158 if (ssop_flags & SSOP_GLOBALS)
11159 if (store_session_globals(fd) == FAIL)
11160 return FAIL;
11161#endif
11162
11163 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011164 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 */
11166 if (put_line(fd, "silent only") == FAIL)
11167 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011168 if ((ssop_flags & SSOP_TABPAGES)
11169 && put_line(fd, "silent tabonly") == FAIL)
11170 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171
11172 /*
11173 * Now a :cd command to the session directory or the current directory
11174 */
11175 if (ssop_flags & SSOP_SESDIR)
11176 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011177 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11178 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 return FAIL;
11180 }
11181 else if (ssop_flags & SSOP_CURDIR)
11182 {
11183 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11184 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011185 || fputs("cd ", fd) < 0
11186 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11187 || put_eol(fd) == FAIL)
11188 {
11189 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 vim_free(sname);
11193 }
11194
11195 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011196 * If there is an empty, unnamed buffer we will wipe it out later.
11197 * Remember the buffer number.
11198 */
11199 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11200 return FAIL;
11201 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11202 return FAIL;
11203 if (put_line(fd, "endif") == FAIL)
11204 return FAIL;
11205
11206 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 * Now save the current files, current buffer first.
11208 */
11209 if (put_line(fd, "set shortmess=aoO") == FAIL)
11210 return FAIL;
11211
11212 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011213 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214 {
11215 if (!(only_save_windows && buf->b_nwindows == 0)
11216 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011217#ifdef FEAT_TERMINAL
11218 /* skip terminal buffers: finished ones are not useful, others
11219 * will be resurrected and result in a new buffer */
11220 && !bt_terminal(buf)
11221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 && buf->b_fname != NULL
11223 && buf->b_p_bl)
11224 {
11225 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11226 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011227 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 return FAIL;
11229 }
11230 }
11231
11232 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011233 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11235 return FAIL;
11236
11237 if (ssop_flags & SSOP_RESIZE)
11238 {
11239 /* Note: after the restore we still check it worked!*/
11240 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11241 || put_eol(fd) == FAIL)
11242 return FAIL;
11243 }
11244
11245#ifdef FEAT_GUI
11246 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11247 {
11248 int x, y;
11249
11250 if (gui_mch_get_winpos(&x, &y) == OK)
11251 {
11252 /* Note: after the restore we still check it worked!*/
11253 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11254 return FAIL;
11255 }
11256 }
11257#endif
11258
11259 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011260 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11261 * will be displayed when creating the next tab. That resizes the windows
11262 * in the first tab, which may cause problems. Set 'showtabline' to 2
11263 * temporarily to avoid that.
11264 */
11265 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11266 {
11267 if (put_line(fd, "set stal=2") == FAIL)
11268 return FAIL;
11269 restore_stal = TRUE;
11270 }
11271
11272 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011273 * May repeat putting Windows for each tab, when "tabpages" is in
11274 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011275 * Don't use goto_tabpage(), it may change directory and trigger
11276 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011278 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011279 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011280 if ((ssop_flags & SSOP_TABPAGES))
11281 {
11282 int num_tabs;
11283
11284 /*
11285 * Similar to ses_win_rec() below, populate the tab pages first so
11286 * later local options won't be copied to the new tabs.
11287 */
11288 for (tabnr = 1; ; ++tabnr)
11289 {
11290 tabpage_T *tp = find_tabpage(tabnr);
11291
11292 if (tp == NULL) /* done all tab pages */
11293 break;
11294
11295 if (tabnr > 1 && put_line(fd, "tabnew") == FAIL)
11296 return FAIL;
11297 }
11298
11299 num_tabs = tabnr - 1;
11300 if (num_tabs > 1 && (fprintf(fd, "tabnext -%d", num_tabs - 1) < 0
11301 || put_eol(fd) == FAIL))
11302 return FAIL;
11303 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011304 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011306 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011307 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011308
Bram Moolenaar18144c82006-04-12 21:52:12 +000011309 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011311 tabpage_T *tp = find_tabpage(tabnr);
11312
11313 if (tp == NULL)
11314 break; /* done all tab pages */
11315 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011316 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011317 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011318 tab_topframe = topframe;
11319 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011320 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011321 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011322 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011323 tab_topframe = tp->tp_topframe;
11324 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011325 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011326 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328
Bram Moolenaar18144c82006-04-12 21:52:12 +000011329 /*
11330 * Before creating the window layout, try loading one file. If this
11331 * is aborted we don't end up with a number of useless windows.
11332 * This may have side effects! (e.g., compressed or network file).
11333 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011334 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011335 {
11336 if (ses_do_win(wp)
11337 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011338 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011339#ifdef FEAT_QUICKFIX
11340 && !bt_nofile(wp->w_buffer)
11341#endif
11342 )
11343 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011344 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11345 return FAIL;
11346 need_tabnext = FALSE;
11347
11348 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011349 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11350 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011351 return FAIL;
11352 if (!wp->w_arg_idx_invalid)
11353 edited_win = wp;
11354 break;
11355 }
11356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011358 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011359 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011360 return FAIL;
11361
Bram Moolenaar18144c82006-04-12 21:52:12 +000011362 /*
11363 * Save current window layout.
11364 */
11365 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011367 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011369 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11370 return FAIL;
11371 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11372 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373
Bram Moolenaar18144c82006-04-12 21:52:12 +000011374 /*
11375 * Check if window sizes can be restored (no windows omitted).
11376 * Remember the window number of the current window after restoring.
11377 */
11378 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011379 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011380 {
11381 if (ses_do_win(wp))
11382 ++nr;
11383 else
11384 restore_size = FALSE;
11385 if (curwin == wp)
11386 cnr = nr;
11387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388
Bram Moolenaar18144c82006-04-12 21:52:12 +000011389 /* Go to the first window. */
11390 if (put_line(fd, "wincmd t") == FAIL)
11391 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011392
Bram Moolenaar18144c82006-04-12 21:52:12 +000011393 /*
11394 * If more than one window, see if sizes can be restored.
11395 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11396 * resized when moving between windows.
11397 * Do this before restoring the view, so that the topline and the
11398 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011399 * winminheight and winminwidth need to be set to avoid an error if the
11400 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011401 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011402 if (put_line(fd, "set winminheight=0") == FAIL
11403 || put_line(fd, "set winheight=1") == FAIL
11404 || put_line(fd, "set winminwidth=0") == FAIL
11405 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011406 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011407 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011408 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409
Bram Moolenaar18144c82006-04-12 21:52:12 +000011410 /*
11411 * Restore the view of the window (options, file, cursor, etc.).
11412 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011413 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011414 {
11415 if (!ses_do_win(wp))
11416 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011417 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11418 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011419 return FAIL;
11420 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11421 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011422 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011423 }
11424
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011425 /* The argument index in the first tab page is zero, need to set it in
11426 * each window. For further tab pages it's the window where we do
11427 * "tabedit". */
11428 cur_arg_idx = next_arg_idx;
11429
Bram Moolenaar18144c82006-04-12 21:52:12 +000011430 /*
11431 * Restore cursor to the current window if it's not the first one.
11432 */
11433 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11434 || put_eol(fd) == FAIL))
11435 return FAIL;
11436
11437 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011438 * Restore window sizes again after jumping around in windows, because
11439 * the current window has a minimum size while others may not.
11440 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011441 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011442 return FAIL;
11443
Bram Moolenaar18144c82006-04-12 21:52:12 +000011444 /* Don't continue in another tab page when doing only the current one
11445 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011446 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011447 break;
11448 }
11449
11450 if (ssop_flags & SSOP_TABPAGES)
11451 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011452 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11453 || put_eol(fd) == FAIL)
11454 return FAIL;
11455 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011456 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11457 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011458
Bram Moolenaar9c102382006-05-03 21:26:49 +000011459 /*
11460 * Wipe out an empty unnamed buffer we started in.
11461 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011462 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011463 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011464 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011465 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011466 return FAIL;
11467 if (put_line(fd, "endif") == FAIL)
11468 return FAIL;
11469 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11470 return FAIL;
11471
11472 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11473 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11474 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11475 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011476 /* Re-apply 'winminheight' and 'winminwidth'. */
11477 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11478 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11479 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480
11481 /*
11482 * Lastly, execute the x.vim file if it exists.
11483 */
11484 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11485 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011486 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487 || put_line(fd, "endif") == FAIL)
11488 return FAIL;
11489
11490 return OK;
11491}
11492
11493 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011494ses_winsizes(
11495 FILE *fd,
11496 int restore_size,
11497 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498{
11499 int n = 0;
11500 win_T *wp;
11501
11502 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11503 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011504 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505 {
11506 if (!ses_do_win(wp))
11507 continue;
11508 ++n;
11509
11510 /* restore height when not full height */
11511 if (wp->w_height + wp->w_status_height < topframe->fr_height
11512 && (fprintf(fd,
11513 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11514 n, (long)wp->w_height, Rows / 2, Rows) < 0
11515 || put_eol(fd) == FAIL))
11516 return FAIL;
11517
11518 /* restore width when not full width */
11519 if (wp->w_width < Columns && (fprintf(fd,
11520 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11521 n, (long)wp->w_width, Columns / 2, Columns) < 0
11522 || put_eol(fd) == FAIL))
11523 return FAIL;
11524 }
11525 }
11526 else
11527 {
11528 /* Just equalise window sizes */
11529 if (put_line(fd, "wincmd =") == FAIL)
11530 return FAIL;
11531 }
11532 return OK;
11533}
11534
11535/*
11536 * Write commands to "fd" to recursively create windows for frame "fr",
11537 * horizontally and vertically split.
11538 * After the commands the last window in the frame is the current window.
11539 * Returns FAIL when writing the commands to "fd" fails.
11540 */
11541 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011542ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543{
11544 frame_T *frc;
11545 int count = 0;
11546
11547 if (fr->fr_layout != FR_LEAF)
11548 {
11549 /* Find first frame that's not skipped and then create a window for
11550 * each following one (first frame is already there). */
11551 frc = ses_skipframe(fr->fr_child);
11552 if (frc != NULL)
11553 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11554 {
11555 /* Make window as big as possible so that we have lots of room
11556 * to split. */
11557 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11558 || put_line(fd, fr->fr_layout == FR_COL
11559 ? "split" : "vsplit") == FAIL)
11560 return FAIL;
11561 ++count;
11562 }
11563
11564 /* Go back to the first window. */
11565 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11566 ? "%dwincmd k" : "%dwincmd h", count) < 0
11567 || put_eol(fd) == FAIL))
11568 return FAIL;
11569
11570 /* Recursively create frames/windows in each window of this column or
11571 * row. */
11572 frc = ses_skipframe(fr->fr_child);
11573 while (frc != NULL)
11574 {
11575 ses_win_rec(fd, frc);
11576 frc = ses_skipframe(frc->fr_next);
11577 /* Go to next window. */
11578 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11579 return FAIL;
11580 }
11581 }
11582 return OK;
11583}
11584
11585/*
11586 * Skip frames that don't contain windows we want to save in the Session.
11587 * Returns NULL when there none.
11588 */
11589 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011590ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591{
11592 frame_T *frc;
11593
11594 for (frc = fr; frc != NULL; frc = frc->fr_next)
11595 if (ses_do_frame(frc))
11596 break;
11597 return frc;
11598}
11599
11600/*
11601 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11602 * the Session.
11603 */
11604 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011605ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606{
11607 frame_T *frc;
11608
11609 if (fr->fr_layout == FR_LEAF)
11610 return ses_do_win(fr->fr_win);
11611 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11612 if (ses_do_frame(frc))
11613 return TRUE;
11614 return FALSE;
11615}
11616
11617/*
11618 * Return non-zero if window "wp" is to be stored in the Session.
11619 */
11620 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011621ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011623#ifdef FEAT_TERMINAL
11624 if (bt_terminal(wp->w_buffer))
11625 return !term_is_finished(wp->w_buffer)
11626 && (ssop_flags & SSOP_TERMINAL)
11627 && term_should_restore(wp->w_buffer);
11628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629 if (wp->w_buffer->b_fname == NULL
11630#ifdef FEAT_QUICKFIX
11631 /* When 'buftype' is "nofile" can't restore the window contents. */
11632 || bt_nofile(wp->w_buffer)
11633#endif
11634 )
11635 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011636 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637 return (ssop_flags & SSOP_HELP);
11638 return TRUE;
11639}
11640
11641/*
11642 * Write commands to "fd" to restore the view of a window.
11643 * Caller must make sure 'scrolloff' is zero.
11644 */
11645 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011646put_view(
11647 FILE *fd,
11648 win_T *wp,
11649 int add_edit, /* add ":edit" command to view */
11650 unsigned *flagp, /* vop_flags or ssop_flags */
11651 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011652 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653{
11654 win_T *save_curwin;
11655 int f;
11656 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011657 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658
11659 /* Always restore cursor position for ":mksession". For ":mkview" only
11660 * when 'viewoptions' contains "cursor". */
11661 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11662
11663 /*
11664 * Local argument list.
11665 */
11666 if (wp->w_alist == &global_alist)
11667 {
11668 if (put_line(fd, "argglobal") == FAIL)
11669 return FAIL;
11670 }
11671 else
11672 {
11673 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11674 flagp == &vop_flags
11675 || !(*flagp & SSOP_CURDIR)
11676 || wp->w_localdir != NULL, flagp) == FAIL)
11677 return FAIL;
11678 }
11679
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011680 /* Only when part of a session: restore the argument index. Some
11681 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011682 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011683 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011685 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686 || put_eol(fd) == FAIL)
11687 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011688 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 }
11690
11691 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011692 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011694# ifdef FEAT_TERMINAL
11695 if (bt_terminal(wp->w_buffer))
11696 {
11697 if (term_write_session(fd, wp) == FAIL)
11698 return FAIL;
11699 }
11700 else
11701# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 /*
11703 * Load the file.
11704 */
11705 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011706# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011708# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 )
11710 {
11711 /*
11712 * Editing a file in this buffer: use ":edit file".
11713 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011714 *
11715 * Note, if a buffer for that file already exists, use :badd to
11716 * edit that buffer, to not lose folding information (:edit resets
11717 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011719 if (fputs("if bufexists('", fd) < 0
11720 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11721 || fputs("') | buffer ", fd) < 0
11722 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11723 || fputs(" | else | edit ", fd) < 0
11724 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11725 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011726 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 return FAIL;
11728 }
11729 else
11730 {
11731 /* No file in this buffer, just make it empty. */
11732 if (put_line(fd, "enew") == FAIL)
11733 return FAIL;
11734#ifdef FEAT_QUICKFIX
11735 if (wp->w_buffer->b_ffname != NULL)
11736 {
11737 /* The buffer does have a name, but it's not a file name. */
11738 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011739 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 return FAIL;
11741 }
11742#endif
11743 do_cursor = FALSE;
11744 }
11745 }
11746
11747 /*
11748 * Local mappings and abbreviations.
11749 */
11750 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11751 && makemap(fd, wp->w_buffer) == FAIL)
11752 return FAIL;
11753
11754 /*
11755 * Local options. Need to go to the window temporarily.
11756 * Store only local values when using ":mkview" and when ":mksession" is
11757 * used and 'sessionoptions' doesn't include "options".
11758 * Some folding options are always stored when "folds" is included,
11759 * otherwise the folds would not be restored correctly.
11760 */
11761 save_curwin = curwin;
11762 curwin = wp;
11763 curbuf = curwin->w_buffer;
11764 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11765 f = makeset(fd, OPT_LOCAL,
11766 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11767#ifdef FEAT_FOLDING
11768 else if (*flagp & SSOP_FOLDS)
11769 f = makefoldset(fd);
11770#endif
11771 else
11772 f = OK;
11773 curwin = save_curwin;
11774 curbuf = curwin->w_buffer;
11775 if (f == FAIL)
11776 return FAIL;
11777
11778#ifdef FEAT_FOLDING
11779 /*
11780 * Save Folds when 'buftype' is empty and for help files.
11781 */
11782 if ((*flagp & SSOP_FOLDS)
11783 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011784 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 {
11786 if (put_folds(fd, wp) == FAIL)
11787 return FAIL;
11788 }
11789#endif
11790
11791 /*
11792 * Set the cursor after creating folds, since that moves the cursor.
11793 */
11794 if (do_cursor)
11795 {
11796
11797 /* Restore the cursor line in the file and relatively in the
11798 * window. Don't use "G", it changes the jumplist. */
11799 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11800 (long)wp->w_cursor.lnum,
11801 (long)(wp->w_cursor.lnum - wp->w_topline),
11802 (long)wp->w_height / 2, (long)wp->w_height) < 0
11803 || put_eol(fd) == FAIL
11804 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11805 || put_line(fd, "exe s:l") == FAIL
11806 || put_line(fd, "normal! zt") == FAIL
11807 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11808 || put_eol(fd) == FAIL)
11809 return FAIL;
11810 /* Restore the cursor column and left offset when not wrapping. */
11811 if (wp->w_cursor.col == 0)
11812 {
11813 if (put_line(fd, "normal! 0") == FAIL)
11814 return FAIL;
11815 }
11816 else
11817 {
11818 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11819 {
11820 if (fprintf(fd,
11821 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011822 (long)wp->w_virtcol + 1,
11823 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 (long)wp->w_width / 2, (long)wp->w_width) < 0
11825 || put_eol(fd) == FAIL
11826 || put_line(fd, "if s:c > 0") == FAIL
11827 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011828 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11829 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830 || put_eol(fd) == FAIL
11831 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011832 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833 || put_eol(fd) == FAIL
11834 || put_line(fd, "endif") == FAIL)
11835 return FAIL;
11836 }
11837 else
11838 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011839 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 || put_eol(fd) == FAIL)
11841 return FAIL;
11842 }
11843 }
11844 }
11845
11846 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011847 * Local directory, if the current flag is not view options or the "curdir"
11848 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011850 if (wp->w_localdir != NULL
11851 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 {
11853 if (fputs("lcd ", fd) < 0
11854 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11855 || put_eol(fd) == FAIL)
11856 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011857 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858 }
11859
11860 return OK;
11861}
11862
11863/*
11864 * Write an argument list to the session file.
11865 * Returns FAIL if writing fails.
11866 */
11867 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011868ses_arglist(
11869 FILE *fd,
11870 char *cmd,
11871 garray_T *gap,
11872 int fullname, /* TRUE: use full path name */
11873 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874{
11875 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011876 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 char_u *s;
11878
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011879 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11880 return FAIL;
11881 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882 return FAIL;
11883 for (i = 0; i < gap->ga_len; ++i)
11884 {
11885 /* NULL file names are skipped (only happens when out of memory). */
11886 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11887 if (s != NULL)
11888 {
11889 if (fullname)
11890 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011891 buf = alloc(MAXPATHL);
11892 if (buf != NULL)
11893 {
11894 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11895 s = buf;
11896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011898 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011899 || ses_put_fname(fd, s, flagp) == FAIL
11900 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011901 {
11902 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011904 }
11905 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906 }
11907 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011908 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909}
11910
11911/*
11912 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011913 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 * Returns FAIL if writing fails.
11915 */
11916 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011917ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918{
11919 char_u *name;
11920
11921 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011922 * the session file will be sourced.
11923 * Don't do this for ":mkview", we don't know the current directory.
11924 * Don't do this after ":lcd", we don't keep track of what the current
11925 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926 if (buf->b_sfname != NULL
11927 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011928 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011929#ifdef FEAT_AUTOCHDIR
11930 && !p_acd
11931#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011932 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933 name = buf->b_sfname;
11934 else
11935 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011936 if (ses_put_fname(fd, name, flagp) == FAIL
11937 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 return FAIL;
11939 return OK;
11940}
11941
11942/*
11943 * Write a file name to the session file.
11944 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11945 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011946 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947 */
11948 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011949ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950{
11951 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011952 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954
11955 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011956 if (sname == NULL)
11957 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011959 if (*flagp & SSOP_SLASH)
11960 {
11961 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011962 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011963 if (*p == '\\')
11964 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011966
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011967 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011968 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011970 if (p == NULL)
11971 return FAIL;
11972
11973 /* write the result */
11974 if (fputs((char *)p, fd) < 0)
11975 retval = FAIL;
11976
11977 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978 return retval;
11979}
11980
11981/*
11982 * ":loadview [nr]"
11983 */
11984 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011985ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986{
11987 char_u *fname;
11988
11989 fname = get_view_file(*eap->arg);
11990 if (fname != NULL)
11991 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011992 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 vim_free(fname);
11994 }
11995}
11996
11997/*
11998 * Get the name of the view file for the current buffer.
11999 */
12000 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012001get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002{
12003 int len = 0;
12004 char_u *p, *s;
12005 char_u *retval;
12006 char_u *sname;
12007
12008 if (curbuf->b_ffname == NULL)
12009 {
12010 EMSG(_(e_noname));
12011 return NULL;
12012 }
12013 sname = home_replace_save(NULL, curbuf->b_ffname);
12014 if (sname == NULL)
12015 return NULL;
12016
12017 /*
12018 * We want a file name without separators, because we're not going to make
12019 * a directory.
12020 * "normal" path separator -> "=+"
12021 * "=" -> "=="
12022 * ":" path separator -> "=-"
12023 */
12024 for (p = sname; *p; ++p)
12025 if (*p == '=' || vim_ispathsep(*p))
12026 ++len;
12027 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12028 if (retval != NULL)
12029 {
12030 STRCPY(retval, p_vdir);
12031 add_pathsep(retval);
12032 s = retval + STRLEN(retval);
12033 for (p = sname; *p; ++p)
12034 {
12035 if (*p == '=')
12036 {
12037 *s++ = '=';
12038 *s++ = '=';
12039 }
12040 else if (vim_ispathsep(*p))
12041 {
12042 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012043#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044 if (*p == ':')
12045 *s++ = '-';
12046 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012048 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049 }
12050 else
12051 *s++ = *p;
12052 }
12053 *s++ = '=';
12054 *s++ = c;
12055 STRCPY(s, ".vim");
12056 }
12057
12058 vim_free(sname);
12059 return retval;
12060}
12061
12062#endif /* FEAT_SESSION */
12063
12064/*
12065 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12066 * Return FAIL for a write error.
12067 */
12068 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012069put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070{
12071 if (
12072#ifdef USE_CRNL
12073 (
12074# ifdef MKSESSION_NL
12075 !mksession_nl &&
12076# endif
12077 (putc('\r', fd) < 0)) ||
12078#endif
12079 (putc('\n', fd) < 0))
12080 return FAIL;
12081 return OK;
12082}
12083
12084/*
12085 * Write a line to "fd".
12086 * Return FAIL for a write error.
12087 */
12088 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012089put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090{
12091 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12092 return FAIL;
12093 return OK;
12094}
12095
12096#ifdef FEAT_VIMINFO
12097/*
12098 * ":rviminfo" and ":wviminfo".
12099 */
12100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012101ex_viminfo(
12102 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103{
12104 char_u *save_viminfo;
12105
12106 save_viminfo = p_viminfo;
12107 if (*p_viminfo == NUL)
12108 p_viminfo = (char_u *)"'100";
12109 if (eap->cmdidx == CMD_rviminfo)
12110 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012111 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12112 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113 EMSG(_("E195: Cannot open viminfo file for reading"));
12114 }
12115 else
12116 write_viminfo(eap->arg, eap->forceit);
12117 p_viminfo = save_viminfo;
12118}
12119#endif
12120
12121#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012122/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012123 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012124 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012127dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012129 if (fname == NULL)
12130 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012131 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132}
12133#endif
12134
12135/*
12136 * ":behave {mswin,xterm}"
12137 */
12138 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012139ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140{
12141 if (STRCMP(eap->arg, "mswin") == 0)
12142 {
12143 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12144 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12145 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12146 set_option_value((char_u *)"keymodel", 0L,
12147 (char_u *)"startsel,stopsel", 0);
12148 }
12149 else if (STRCMP(eap->arg, "xterm") == 0)
12150 {
12151 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12152 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12153 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12154 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12155 }
12156 else
12157 EMSG2(_(e_invarg2), eap->arg);
12158}
12159
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012160#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12161/*
12162 * Function given to ExpandGeneric() to obtain the possible arguments of the
12163 * ":behave {mswin,xterm}" command.
12164 */
12165 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012166get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012167{
12168 if (idx == 0)
12169 return (char_u *)"mswin";
12170 if (idx == 1)
12171 return (char_u *)"xterm";
12172 return NULL;
12173}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012174
12175/*
12176 * Function given to ExpandGeneric() to obtain the possible arguments of the
12177 * ":messages {clear}" command.
12178 */
12179 char_u *
12180get_messages_arg(expand_T *xp UNUSED, int idx)
12181{
12182 if (idx == 0)
12183 return (char_u *)"clear";
12184 return NULL;
12185}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012186#endif
12187
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012188 char_u *
12189get_mapclear_arg(expand_T *xp UNUSED, int idx)
12190{
12191 if (idx == 0)
12192 return (char_u *)"<buffer>";
12193 return NULL;
12194}
12195
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196static int filetype_detect = FALSE;
12197static int filetype_plugin = FALSE;
12198static int filetype_indent = FALSE;
12199
12200/*
12201 * ":filetype [plugin] [indent] {on,off,detect}"
12202 * on: Load the filetype.vim file to install autocommands for file types.
12203 * off: Load the ftoff.vim file to remove all autocommands for file types.
12204 * plugin on: load filetype.vim and ftplugin.vim
12205 * plugin off: load ftplugof.vim
12206 * indent on: load filetype.vim and indent.vim
12207 * indent off: load indoff.vim
12208 */
12209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012210ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211{
12212 char_u *arg = eap->arg;
12213 int plugin = FALSE;
12214 int indent = FALSE;
12215
12216 if (*eap->arg == NUL)
12217 {
12218 /* Print current status. */
12219 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12220 filetype_detect ? "ON" : "OFF",
12221 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12222 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12223 return;
12224 }
12225
12226 /* Accept "plugin" and "indent" in any order. */
12227 for (;;)
12228 {
12229 if (STRNCMP(arg, "plugin", 6) == 0)
12230 {
12231 plugin = TRUE;
12232 arg = skipwhite(arg + 6);
12233 continue;
12234 }
12235 if (STRNCMP(arg, "indent", 6) == 0)
12236 {
12237 indent = TRUE;
12238 arg = skipwhite(arg + 6);
12239 continue;
12240 }
12241 break;
12242 }
12243 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12244 {
12245 if (*arg == 'o' || !filetype_detect)
12246 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012247 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248 filetype_detect = TRUE;
12249 if (plugin)
12250 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012251 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252 filetype_plugin = TRUE;
12253 }
12254 if (indent)
12255 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012256 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257 filetype_indent = TRUE;
12258 }
12259 }
12260 if (*arg == 'd')
12261 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012262 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012263 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264 }
12265 }
12266 else if (STRCMP(arg, "off") == 0)
12267 {
12268 if (plugin || indent)
12269 {
12270 if (plugin)
12271 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012272 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273 filetype_plugin = FALSE;
12274 }
12275 if (indent)
12276 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012277 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278 filetype_indent = FALSE;
12279 }
12280 }
12281 else
12282 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012283 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284 filetype_detect = FALSE;
12285 }
12286 }
12287 else
12288 EMSG2(_(e_invarg2), arg);
12289}
12290
12291/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012292 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293 */
12294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012295ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296{
12297 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012298 {
12299 char_u *arg = eap->arg;
12300
12301 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12302 arg += 9;
12303
12304 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12305 if (arg != eap->arg)
12306 did_filetype = FALSE;
12307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012311ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312{
12313#ifdef FEAT_DIGRAPHS
12314 if (*eap->arg != NUL)
12315 putdigraph(eap->arg);
12316 else
12317 listdigraphs();
12318#else
12319 EMSG(_("E196: No digraphs in this version"));
12320#endif
12321}
12322
12323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012324ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325{
12326 int flags = 0;
12327
12328 if (eap->cmdidx == CMD_setlocal)
12329 flags = OPT_LOCAL;
12330 else if (eap->cmdidx == CMD_setglobal)
12331 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012332#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 if (cmdmod.browse && flags == 0)
12334 ex_options(eap);
12335 else
12336#endif
12337 (void)do_set(eap->arg, flags);
12338}
12339
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012340#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12341 void
12342set_no_hlsearch(int flag)
12343{
12344 no_hlsearch = flag;
12345# ifdef FEAT_EVAL
12346 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12347# endif
12348}
12349
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350/*
12351 * ":nohlsearch"
12352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012354ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012356 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012357 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358}
12359
12360/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012361 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362 * Sets nextcmd to the start of the next command, if any. Also called when
12363 * skipping commands to find the next command.
12364 */
12365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012366ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367{
12368 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012369 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370 char_u *end;
12371 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012372 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012373
12374 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012375 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012376 else
12377 {
12378 EMSG(e_invcmd);
12379 return;
12380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381
12382 /* First clear any old pattern. */
12383 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012384 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385
12386 if (ends_excmd(*eap->arg))
12387 end = eap->arg;
12388 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012389 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390 end = eap->arg + 4;
12391 else
12392 {
12393 p = skiptowhite(eap->arg);
12394 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012395 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396 p = skipwhite(p);
12397 if (*p == NUL)
12398 {
12399 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012400 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401 EMSG2(_(e_invarg2), eap->arg);
12402 return;
12403 }
12404 end = skip_regexp(p + 1, *p, TRUE, NULL);
12405 if (!eap->skip)
12406 {
12407 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12408 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012409 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410 eap->errmsg = e_trailing;
12411 return;
12412 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012413 if (*end != *p)
12414 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012415 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012416 EMSG2(_(e_invarg2), p);
12417 return;
12418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419
12420 c = *end;
12421 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012422 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012423 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012424 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425 }
12426 }
12427 eap->nextcmd = find_nextcmd(end);
12428}
12429#endif
12430
12431#ifdef FEAT_CRYPT
12432/*
12433 * ":X": Get crypt key
12434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012436ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012438 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012439 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440}
12441#endif
12442
12443#ifdef FEAT_FOLDING
12444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012445ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446{
12447 if (foldManualAllowed(TRUE))
12448 foldCreate(eap->line1, eap->line2);
12449}
12450
12451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012452ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453{
12454 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12455 eap->forceit, FALSE);
12456}
12457
12458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012459ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460{
12461 linenr_T lnum;
12462
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012463#ifdef FEAT_CLIPBOARD
12464 start_global_changes();
12465#endif
12466
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467 /* First set the marks for all lines closed/open. */
12468 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12469 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12470 ml_setmarked(lnum);
12471
12472 /* Execute the command on the marked lines. */
12473 global_exe(eap->arg);
12474 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012475#ifdef FEAT_CLIPBOARD
12476 end_global_changes();
12477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478}
12479#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012480
12481# if defined(FEAT_TIMERS) || defined(PROTO)
12482 int
12483get_pressedreturn(void)
12484{
12485 return ex_pressedreturn;
12486}
12487
12488 void
12489set_pressedreturn(int val)
12490{
12491 ex_pressedreturn = val;
12492}
12493#endif