blob: 9491e235d91abae181fb923c84066ded3430584c [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 */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001712 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 cmdmod_T save_cmdmod;
1714 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001715 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
1717 vim_memset(&ea, 0, sizeof(ea));
1718 ea.line1 = 1;
1719 ea.line2 = 1;
1720#ifdef FEAT_EVAL
1721 ++ex_nesting_level;
1722#endif
1723
Bram Moolenaar21691f82012-12-05 19:13:18 +01001724 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (quitmore
1726#ifdef FEAT_EVAL
1727 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001728 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001729#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001730 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001731 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 --quitmore;
1733
1734 /*
1735 * Reset browse, confirm, etc.. They are restored when returning, for
1736 * recursive calls.
1737 */
1738 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001740 /* "#!anything" is handled like a comment. */
1741 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1742 goto doend;
1743
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 /*
1745 * Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02001746 * The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 */
1748 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001749 ea.cmdlinep = cmdlinep;
1750 ea.getline = fgetline;
1751 ea.cookie = cookie;
1752#ifdef FEAT_EVAL
1753 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754#endif
Bram Moolenaareffed932018-08-14 13:38:17 +02001755 if (parse_command_modifiers(&ea, &errormsg) == FAIL)
1756 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001758 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
1760#ifdef FEAT_EVAL
1761 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1762 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1763#else
1764 ea.skip = (if_level > 0);
1765#endif
1766
1767#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001768# ifdef FEAT_PROFILE
1769 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001770 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001771 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001772 if (getline_equal(fgetline, cookie, get_func_line))
1773 func_line_exec(getline_cookie(fgetline, cookie));
1774 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001775 script_line_exec();
1776 }
1777#endif
1778
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 /* May go to debug mode. If this happens and the ">quit" debug command is
1780 * used, throw an interrupt exception and skip the next command. */
1781 dbg_check_breakpoint(&ea);
1782 if (!ea.skip && got_int)
1783 {
1784 ea.skip = TRUE;
1785 (void)do_intthrow(cstack);
1786 }
1787#endif
1788
1789/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001790 * 3. Skip over the range to find the command. Let "p" point to after it.
1791 *
1792 * We need the command to know what kind of range it uses.
1793 */
1794 cmd = ea.cmd;
1795 ea.cmd = skip_range(ea.cmd, NULL);
1796 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1797 ea.cmd = skipwhite(ea.cmd + 1);
1798 p = find_command(&ea, NULL);
1799
1800/*
1801 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 *
1803 * where 'addr' is:
1804 *
1805 * % (entire file)
1806 * $ [+-NUM]
1807 * 'x [+-NUM] (where x denotes a currently defined mark)
1808 * . [+-NUM]
1809 * [+-NUM]..
1810 * NUM
1811 *
1812 * The ea.cmd pointer is updated to point to the first character following the
1813 * range spec. If an initial address is found, but no second, the upper bound
1814 * is equal to the lower.
1815 */
1816
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01001817 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001818 if (!IS_USER_CMDIDX(ea.cmdidx))
1819 {
1820 if (ea.cmdidx != CMD_SIZE)
1821 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1822 else
1823 ea.addr_type = ADDR_LINES;
1824
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001825 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01001826 if (ea.cmdidx == CMD_wincmd && p != NULL)
1827 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001828 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001829
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001830 ea.cmd = cmd;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001831 if (parse_cmd_address(&ea, &errormsg) == FAIL)
1832 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001835 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
1837
1838 /*
1839 * Skip ':' and any white space
1840 */
1841 ea.cmd = skipwhite(ea.cmd);
1842 while (*ea.cmd == ':')
1843 ea.cmd = skipwhite(ea.cmd + 1);
1844
1845 /*
1846 * If we got a line, but no command, then go to the line.
1847 * If we find a '|' or '\n' we set ea.nextcmd.
1848 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001849 if (*ea.cmd == NUL || *ea.cmd == '"'
1850 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
1852 /*
1853 * strange vi behaviour:
1854 * ":3" jumps to line 3
1855 * ":3|..." prints line 3
1856 * ":|" prints current line
1857 */
1858 if (ea.skip) /* skip this if inside :if */
1859 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001860 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 {
1862 ea.cmdidx = CMD_print;
1863 ea.argt = RANGE+COUNT+TRLBAR;
1864 if ((errormsg = invalid_range(&ea)) == NULL)
1865 {
1866 correct_range(&ea);
1867 ex_print(&ea);
1868 }
1869 }
1870 else if (ea.addr_count != 0)
1871 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001872 if (ea.line2 > curbuf->b_ml.ml_line_count)
1873 {
1874 /* With '-' in 'cpoptions' a line number past the file is an
1875 * error, otherwise put it at the end of the file. */
1876 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1877 ea.line2 = -1;
1878 else
1879 ea.line2 = curbuf->b_ml.ml_line_count;
1880 }
1881
1882 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001883 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 else
1885 {
1886 if (ea.line2 == 0)
1887 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 else
1889 curwin->w_cursor.lnum = ea.line2;
1890 beginline(BL_SOL | BL_FIX);
1891 }
1892 }
1893 goto doend;
1894 }
1895
Bram Moolenaard5005162014-08-22 23:05:54 +02001896 /* If this looks like an undefined user command and there are CmdUndefined
1897 * autocommands defined, trigger the matching autocommands. */
1898 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1899 && ASCII_ISUPPER(*ea.cmd)
1900 && has_cmdundefined())
1901 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001902 int ret;
1903
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001904 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001905 while (ASCII_ISALNUM(*p))
1906 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001907 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001908 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1909 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001910 /* If the autocommands did something and didn't cause an error, try
1911 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001912 p = (ret
1913#ifdef FEAT_EVAL
1914 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001915#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001916 ) ? find_command(&ea, NULL) : ea.cmd;
1917 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001918
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919#ifdef FEAT_USR_CMDS
1920 if (p == NULL)
1921 {
1922 if (!ea.skip)
1923 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
1924 goto doend;
1925 }
1926 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001927 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1928 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 {
1930 errormsg = uc_fun_cmd();
1931 goto doend;
1932 }
1933#endif
1934 if (ea.cmdidx == CMD_SIZE)
1935 {
1936 if (!ea.skip)
1937 {
1938 STRCPY(IObuff, _("E492: Not an editor command"));
1939 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001940 {
1941 /* If the modifier was parsed OK the error must be in the
1942 * following command */
1943 if (after_modifier != NULL)
1944 append_command(after_modifier);
1945 else
1946 append_command(*cmdlinep);
1947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001949 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 }
1951 goto doend;
1952 }
1953
Bram Moolenaar958636c2014-10-21 20:01:58 +02001954 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1955 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001956#ifdef HAVE_EX_SCRIPT_NI
1957 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1958#endif
1959 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960
1961#ifndef FEAT_EVAL
1962 /*
1963 * When the expression evaluation is disabled, recognize the ":if" and
1964 * ":endif" commands and ignore everything in between it.
1965 */
1966 if (ea.cmdidx == CMD_if)
1967 ++if_level;
1968 if (if_level)
1969 {
1970 if (ea.cmdidx == CMD_endif)
1971 --if_level;
1972 goto doend;
1973 }
1974
1975#endif
1976
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001977 /* forced commands */
1978 if (*p == '!' && ea.cmdidx != CMD_substitute
1979 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
1981 ++p;
1982 ea.forceit = TRUE;
1983 }
1984 else
1985 ea.forceit = FALSE;
1986
1987/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001988 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001990 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001991 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992
1993 if (!ea.skip)
1994 {
1995#ifdef HAVE_SANDBOX
1996 if (sandbox != 0 && !(ea.argt & SBOXOK))
1997 {
1998 /* Command not allowed in sandbox. */
1999 errormsg = (char_u *)_(e_sandbox);
2000 goto doend;
2001 }
2002#endif
2003 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2004 {
2005 /* Command not allowed in non-'modifiable' buffer */
2006 errormsg = (char_u *)_(e_modifiable);
2007 goto doend;
2008 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002009
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002010 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002011 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002013 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002014 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 goto doend;
2016 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002017 /* Disallow editing another buffer when "curbuf_lock" is set.
2018 * Do allow ":edit" (check for argument later).
2019 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002020 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002021 && ea.cmdidx != CMD_edit
2022 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002023 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002024 && curbuf_locked())
2025 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2028 {
2029 /* no range allowed */
2030 errormsg = (char_u *)_(e_norange);
2031 goto doend;
2032 }
2033 }
2034
2035 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2036 {
2037 errormsg = (char_u *)_(e_nobang);
2038 goto doend;
2039 }
2040
2041 /*
2042 * Don't complain about the range if it is not used
2043 * (could happen if line_count is accidentally set to 0).
2044 */
2045 if (!ea.skip && !ni)
2046 {
2047 /*
2048 * If the range is backwards, ask for confirmation and, if given, swap
2049 * ea.line1 & ea.line2 so it's forwards again.
2050 * When global command is busy, don't ask, will fail below.
2051 */
2052 if (!global_busy && ea.line1 > ea.line2)
2053 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002054 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002056 if (sourcing || exmode_active)
2057 {
2058 errormsg = (char_u *)_("E493: Backwards range given");
2059 goto doend;
2060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 if (ask_yesno((char_u *)
2062 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002063 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065 lnum = ea.line1;
2066 ea.line1 = ea.line2;
2067 ea.line2 = lnum;
2068 }
2069 if ((errormsg = invalid_range(&ea)) != NULL)
2070 goto doend;
2071 }
2072
2073 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2074 ea.line2 = 1;
2075
2076 correct_range(&ea);
2077
2078#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002079 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2080 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
2082 /* Put the first line at the start of a closed fold, put the last line
2083 * at the end of a closed fold. */
2084 (void)hasFolding(ea.line1, &ea.line1, NULL);
2085 (void)hasFolding(ea.line2, NULL, &ea.line2);
2086 }
2087#endif
2088
2089#ifdef FEAT_QUICKFIX
2090 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002091 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 * option here, so things like % get expanded.
2093 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002094 p = replace_makeprg(&ea, p, cmdlinep);
2095 if (p == NULL)
2096 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097#endif
2098
2099 /*
2100 * Skip to start of argument.
2101 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2102 */
2103 if (ea.cmdidx == CMD_bang)
2104 ea.arg = p;
2105 else
2106 ea.arg = skipwhite(p);
2107
2108 /*
2109 * Check for "++opt=val" argument.
2110 * Must be first, allow ":w ++enc=utf8 !cmd"
2111 */
2112 if (ea.argt & ARGOPT)
2113 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2114 if (getargopt(&ea) == FAIL && !ni)
2115 {
2116 errormsg = (char_u *)_(e_invarg);
2117 goto doend;
2118 }
2119
2120 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2121 {
2122 if (*ea.arg == '>') /* append */
2123 {
2124 if (*++ea.arg != '>') /* typed wrong */
2125 {
2126 errormsg = (char_u *)_("E494: Use w or w>>");
2127 goto doend;
2128 }
2129 ea.arg = skipwhite(ea.arg + 1);
2130 ea.append = TRUE;
2131 }
2132 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2133 {
2134 ++ea.arg;
2135 ea.usefilter = TRUE;
2136 }
2137 }
2138
2139 if (ea.cmdidx == CMD_read)
2140 {
2141 if (ea.forceit)
2142 {
2143 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2144 ea.forceit = FALSE;
2145 }
2146 else if (*ea.arg == '!') /* :r !filter */
2147 {
2148 ++ea.arg;
2149 ea.usefilter = TRUE;
2150 }
2151 }
2152
2153 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2154 {
2155 ea.amount = 1;
2156 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2157 {
2158 ++ea.arg;
2159 ++ea.amount;
2160 }
2161 ea.arg = skipwhite(ea.arg);
2162 }
2163
2164 /*
2165 * Check for "+command" argument, before checking for next command.
2166 * Don't do this for ":read !cmd" and ":write !cmd".
2167 */
2168 if ((ea.argt & EDITCMD) && !ea.usefilter)
2169 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2170
2171 /*
2172 * Check for '|' to separate commands and '"' to start comments.
2173 * Don't do this for ":read !cmd" and ":write !cmd".
2174 */
2175 if ((ea.argt & TRLBAR) && !ea.usefilter)
2176 separate_nextcmd(&ea);
2177
2178 /*
2179 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002180 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2181 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002183 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002184 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002185 || ea.cmdidx == CMD_global
2186 || ea.cmdidx == CMD_vglobal
2187 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
2189 for (p = ea.arg; *p; ++p)
2190 {
2191 /* Remove one backslash before a newline, so that it's possible to
2192 * pass a newline to the shell and also a newline that is preceded
2193 * with a backslash. This makes it impossible to end a shell
2194 * command in a backslash, but that doesn't appear useful.
2195 * Halving the number of backslashes is incompatible with previous
2196 * versions. */
2197 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002198 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 else if (*p == '\n')
2200 {
2201 ea.nextcmd = p + 1;
2202 *p = NUL;
2203 break;
2204 }
2205 }
2206 }
2207
2208 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2209 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002210 buf_T *buf;
2211
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002213 switch (ea.addr_type)
2214 {
2215 case ADDR_LINES:
2216 ea.line2 = curbuf->b_ml.ml_line_count;
2217 break;
2218 case ADDR_LOADED_BUFFERS:
2219 buf = firstbuf;
2220 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2221 buf = buf->b_next;
2222 ea.line1 = buf->b_fnum;
2223 buf = lastbuf;
2224 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2225 buf = buf->b_prev;
2226 ea.line2 = buf->b_fnum;
2227 break;
2228 case ADDR_BUFFERS:
2229 ea.line1 = firstbuf->b_fnum;
2230 ea.line2 = lastbuf->b_fnum;
2231 break;
2232 case ADDR_WINDOWS:
2233 ea.line2 = LAST_WIN_NR;
2234 break;
2235 case ADDR_TABS:
2236 ea.line2 = LAST_TAB_NR;
2237 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002238 case ADDR_TABS_RELATIVE:
2239 ea.line2 = 1;
2240 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002241 case ADDR_ARGUMENTS:
2242 if (ARGCOUNT == 0)
2243 ea.line1 = ea.line2 = 0;
2244 else
2245 ea.line2 = ARGCOUNT;
2246 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002247#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002248 case ADDR_QUICKFIX:
2249 ea.line2 = qf_get_size(&ea);
2250 if (ea.line2 == 0)
2251 ea.line2 = 1;
2252 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002253#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 }
2256
2257 /* accept numbered register only when no count allowed (:put) */
2258 if ( (ea.argt & REGSTR)
2259 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002260 /* Do not allow register = for user commands */
2261 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2263 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002264#ifndef FEAT_CLIPBOARD
2265 /* check these explicitly for a more specific error message */
2266 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002268 errormsg = (char_u *)_(e_invalidreg);
2269 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002272 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2273 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002274 {
2275 ea.regname = *ea.arg++;
2276#ifdef FEAT_EVAL
2277 /* for '=' register: accept the rest of the line as an expression */
2278 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2279 {
2280 set_expr_line(vim_strsave(ea.arg));
2281 ea.arg += STRLEN(ea.arg);
2282 }
2283#endif
2284 ea.arg = skipwhite(ea.arg);
2285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287
2288 /*
2289 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2290 * count, it's a buffer name.
2291 */
2292 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2293 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002294 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 n = getdigits(&ea.arg);
2297 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002298 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
2300 errormsg = (char_u *)_(e_zerocount);
2301 goto doend;
2302 }
2303 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2304 {
2305 ea.line2 = n;
2306 if (ea.addr_count == 0)
2307 ea.addr_count = 1;
2308 }
2309 else
2310 {
2311 ea.line1 = ea.line2;
2312 ea.line2 += n - 1;
2313 ++ea.addr_count;
2314 /*
2315 * Be vi compatible: no error message for out of range.
2316 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002317 if (ea.addr_type == ADDR_LINES
2318 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 ea.line2 = curbuf->b_ml.ml_line_count;
2320 }
2321 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002322
2323 /*
2324 * Check for flags: 'l', 'p' and '#'.
2325 */
2326 if (ea.argt & EXFLAGS)
2327 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002329 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002330 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
2332 errormsg = (char_u *)_(e_trailing);
2333 goto doend;
2334 }
2335
2336 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2337 {
2338 errormsg = (char_u *)_(e_argreq);
2339 goto doend;
2340 }
2341
2342#ifdef FEAT_EVAL
2343 /*
2344 * Skip the command when it's not going to be executed.
2345 * The commands like :if, :endif, etc. always need to be executed.
2346 * Also make an exception for commands that handle a trailing command
2347 * themselves.
2348 */
2349 if (ea.skip)
2350 {
2351 switch (ea.cmdidx)
2352 {
2353 /* commands that need evaluation */
2354 case CMD_while:
2355 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002356 case CMD_for:
2357 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 case CMD_if:
2359 case CMD_elseif:
2360 case CMD_else:
2361 case CMD_endif:
2362 case CMD_try:
2363 case CMD_catch:
2364 case CMD_finally:
2365 case CMD_endtry:
2366 case CMD_function:
2367 break;
2368
2369 /* Commands that handle '|' themselves. Check: A command should
2370 * either have the TRLBAR flag, appear in this list or appear in
2371 * the list at ":help :bar". */
2372 case CMD_aboveleft:
2373 case CMD_and:
2374 case CMD_belowright:
2375 case CMD_botright:
2376 case CMD_browse:
2377 case CMD_call:
2378 case CMD_confirm:
2379 case CMD_delfunction:
2380 case CMD_djump:
2381 case CMD_dlist:
2382 case CMD_dsearch:
2383 case CMD_dsplit:
2384 case CMD_echo:
2385 case CMD_echoerr:
2386 case CMD_echomsg:
2387 case CMD_echon:
2388 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002389 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 case CMD_help:
2391 case CMD_hide:
2392 case CMD_ijump:
2393 case CMD_ilist:
2394 case CMD_isearch:
2395 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002396 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 case CMD_keepjumps:
2398 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002399 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 case CMD_leftabove:
2401 case CMD_let:
2402 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002403 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002405 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002406 case CMD_noautocmd:
2407 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 case CMD_perl:
2409 case CMD_psearch:
2410 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002411 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002412 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 case CMD_return:
2414 case CMD_rightbelow:
2415 case CMD_ruby:
2416 case CMD_silent:
2417 case CMD_smagic:
2418 case CMD_snomagic:
2419 case CMD_substitute:
2420 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002421 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 case CMD_tcl:
2423 case CMD_throw:
2424 case CMD_tilde:
2425 case CMD_topleft:
2426 case CMD_unlet:
2427 case CMD_verbose:
2428 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002429 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 break;
2431
2432 default: goto doend;
2433 }
2434 }
2435#endif
2436
2437 if (ea.argt & XFILE)
2438 {
2439 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2440 goto doend;
2441 }
2442
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 /*
2444 * Accept buffer name. Cannot be used at the same time with a buffer
2445 * number. Don't do this for a user command.
2446 */
2447 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002448 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
2450 /*
2451 * :bdelete, :bwipeout and :bunload take several arguments, separated
2452 * by spaces: find next space (skipping over escaped characters).
2453 * The others take one argument: ignore trailing spaces.
2454 */
2455 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2456 || ea.cmdidx == CMD_bunload)
2457 p = skiptowhite_esc(ea.arg);
2458 else
2459 {
2460 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002461 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 --p;
2463 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002464 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2465 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 if (ea.line2 < 0) /* failed */
2467 goto doend;
2468 ea.addr_count = 1;
2469 ea.arg = skipwhite(p);
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
Bram Moolenaar2be57332018-02-13 18:05:18 +01002472 /* The :try command saves the emsg_silent flag, reset it here when
2473 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002474 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002475 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002476 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002477 if (emsg_silent < 0)
2478 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002479 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002480 }
2481
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002483 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
2486#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002487 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
2489 /*
2490 * Execute a user-defined command.
2491 */
2492 do_ucmd(&ea);
2493 }
2494 else
2495#endif
2496 {
2497 /*
2498 * Call the function to execute the command.
2499 */
2500 ea.errmsg = NULL;
2501 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2502 if (ea.errmsg != NULL)
2503 errormsg = (char_u *)_(ea.errmsg);
2504 }
2505
2506#ifdef FEAT_EVAL
2507 /*
2508 * If the command just executed called do_cmdline(), any throw or ":return"
2509 * or ":finish" encountered there must also check the cstack of the still
2510 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2511 * exception, or reanimate a returned function or finished script file and
2512 * return or finish it again.
2513 */
2514 if (need_rethrow)
2515 do_throw(cstack);
2516 else if (check_cstack)
2517 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002518 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002520 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 && current_func_returned())
2522 do_return(&ea, TRUE, FALSE, NULL);
2523 }
2524 need_rethrow = check_cstack = FALSE;
2525#endif
2526
2527doend:
2528 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002529 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002531 curwin->w_cursor.col = 0;
2532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
2534 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2535 {
2536 if (sourcing)
2537 {
2538 if (errormsg != IObuff)
2539 {
2540 STRCPY(IObuff, errormsg);
2541 errormsg = IObuff;
2542 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002543 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 }
2545 emsg(errormsg);
2546 }
2547#ifdef FEAT_EVAL
2548 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002549 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2550 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551#endif
2552
Bram Moolenaareffed932018-08-14 13:38:17 +02002553 if (ea.verbose_save >= 0)
2554 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002555
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002556 if (cmdmod.save_ei != NULL)
2557 {
2558 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002559 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2560 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002561 free_string_option(cmdmod.save_ei);
2562 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002563
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002564 if (cmdmod.filter_regmatch.regprog != NULL)
2565 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566
2567 cmdmod = save_cmdmod;
2568
Bram Moolenaareffed932018-08-14 13:38:17 +02002569 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 {
2571 /* messages could be enabled for a serious error, need to check if the
2572 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002573 if (!did_emsg || msg_silent > ea.save_msg_silent)
2574 msg_silent = ea.save_msg_silent;
2575 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 if (emsg_silent < 0)
2577 emsg_silent = 0;
2578 /* Restore msg_scroll, it's set by file I/O commands, even when no
2579 * message is actually displayed. */
2580 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002581
2582 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2583 * somewhere in the line. Put it back in the first column. */
2584 if (redirecting())
2585 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 }
2587
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002588#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002589 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002590 --sandbox;
2591#endif
2592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2594 ea.nextcmd = NULL;
2595
2596#ifdef FEAT_EVAL
2597 --ex_nesting_level;
2598#endif
2599
2600 return ea.nextcmd;
2601}
2602#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002603 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604#endif
2605
2606/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002607 * Parse and skip over command modifiers:
2608 * - update eap->cmd
2609 * - store flags in "cmdmod".
2610 * - Set ex_pressedreturn for an empty command line.
2611 * - set msg_silent for ":silent"
2612 * - set p_verbose for ":verbose"
2613 * - Increment "sandbox" for ":sandbox"
2614 * Return FAIL when the command is not to be executed.
2615 * May set "errormsg" to an error message.
2616 */
2617 int
2618parse_command_modifiers(exarg_T *eap, char_u **errormsg)
2619{
2620 char_u *p;
2621
2622 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2623 eap->verbose_save = -1;
2624 eap->save_msg_silent = -1;
2625
2626 for (;;)
2627 {
2628/*
2629 * 1. Skip comment lines and leading white space and colons.
2630 */
2631 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2632 ++eap->cmd;
2633
2634 /* in ex mode, an empty line works like :+ */
2635 if (*eap->cmd == NUL && exmode_active
2636 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2637 || getline_equal(eap->getline, eap->cookie, getexline))
2638 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2639 {
2640 eap->cmd = (char_u *)"+";
2641 ex_pressedreturn = TRUE;
2642 }
2643
2644 /* ignore comment and empty lines */
2645 if (*eap->cmd == '"')
2646 return FAIL;
2647 if (*eap->cmd == NUL)
2648 {
2649 ex_pressedreturn = TRUE;
2650 return FAIL;
2651 }
2652
2653/*
2654 * 2. Handle command modifiers.
2655 */
2656 p = skip_range(eap->cmd, NULL);
2657 switch (*p)
2658 {
2659 /* When adding an entry, also modify cmd_exists(). */
2660 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2661 break;
2662 cmdmod.split |= WSP_ABOVE;
2663 continue;
2664
2665 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2666 {
2667 cmdmod.split |= WSP_BELOW;
2668 continue;
2669 }
2670 if (checkforcmd(&eap->cmd, "browse", 3))
2671 {
2672#ifdef FEAT_BROWSE_CMD
2673 cmdmod.browse = TRUE;
2674#endif
2675 continue;
2676 }
2677 if (!checkforcmd(&eap->cmd, "botright", 2))
2678 break;
2679 cmdmod.split |= WSP_BOT;
2680 continue;
2681
2682 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2683 break;
2684#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2685 cmdmod.confirm = TRUE;
2686#endif
2687 continue;
2688
2689 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2690 {
2691 cmdmod.keepmarks = TRUE;
2692 continue;
2693 }
2694 if (checkforcmd(&eap->cmd, "keepalt", 5))
2695 {
2696 cmdmod.keepalt = TRUE;
2697 continue;
2698 }
2699 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2700 {
2701 cmdmod.keeppatterns = TRUE;
2702 continue;
2703 }
2704 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2705 break;
2706 cmdmod.keepjumps = TRUE;
2707 continue;
2708
2709 case 'f': /* only accept ":filter {pat} cmd" */
2710 {
2711 char_u *reg_pat;
2712
2713 if (!checkforcmd(&p, "filter", 4)
2714 || *p == NUL || ends_excmd(*p))
2715 break;
2716 if (*p == '!')
2717 {
2718 cmdmod.filter_force = TRUE;
2719 p = skipwhite(p + 1);
2720 if (*p == NUL || ends_excmd(*p))
2721 break;
2722 }
2723 p = skip_vimgrep_pat(p, &reg_pat, NULL);
2724 if (p == NULL || *p == NUL)
2725 break;
2726 cmdmod.filter_regmatch.regprog =
2727 vim_regcomp(reg_pat, RE_MAGIC);
2728 if (cmdmod.filter_regmatch.regprog == NULL)
2729 break;
2730 eap->cmd = p;
2731 continue;
2732 }
2733
2734 /* ":hide" and ":hide | cmd" are not modifiers */
2735 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2736 || *p == NUL || ends_excmd(*p))
2737 break;
2738 eap->cmd = p;
2739 cmdmod.hide = TRUE;
2740 continue;
2741
2742 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2743 {
2744 cmdmod.lockmarks = TRUE;
2745 continue;
2746 }
2747
2748 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2749 break;
2750 cmdmod.split |= WSP_ABOVE;
2751 continue;
2752
2753 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2754 {
2755 if (cmdmod.save_ei == NULL)
2756 {
2757 /* Set 'eventignore' to "all". Restore the
2758 * existing option value later. */
2759 cmdmod.save_ei = vim_strsave(p_ei);
2760 set_string_option_direct((char_u *)"ei", -1,
2761 (char_u *)"all", OPT_FREE, SID_NONE);
2762 }
2763 continue;
2764 }
2765 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2766 break;
2767 cmdmod.noswapfile = TRUE;
2768 continue;
2769
2770 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2771 break;
2772 cmdmod.split |= WSP_BELOW;
2773 continue;
2774
2775 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2776 {
2777#ifdef HAVE_SANDBOX
2778 if (!eap->did_sandbox)
2779 ++sandbox;
2780 eap->did_sandbox = TRUE;
2781#endif
2782 continue;
2783 }
2784 if (!checkforcmd(&eap->cmd, "silent", 3))
2785 break;
2786 if (eap->save_msg_silent == -1)
2787 eap->save_msg_silent = msg_silent;
2788 ++msg_silent;
2789 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2790 {
2791 /* ":silent!", but not "silent !cmd" */
2792 eap->cmd = skipwhite(eap->cmd + 1);
2793 ++emsg_silent;
2794 ++eap->did_esilent;
2795 }
2796 continue;
2797
2798 case 't': if (checkforcmd(&p, "tab", 3))
2799 {
2800 long tabnr = get_address(eap, &eap->cmd, ADDR_TABS,
2801 eap->skip, FALSE, 1);
2802 if (tabnr == MAXLNUM)
2803 cmdmod.tab = tabpage_index(curtab) + 1;
2804 else
2805 {
2806 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2807 {
2808 *errormsg = (char_u *)_(e_invrange);
2809 return FAIL;
2810 }
2811 cmdmod.tab = tabnr + 1;
2812 }
2813 eap->cmd = p;
2814 continue;
2815 }
2816 if (!checkforcmd(&eap->cmd, "topleft", 2))
2817 break;
2818 cmdmod.split |= WSP_TOP;
2819 continue;
2820
2821 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2822 break;
2823 if (eap->save_msg_silent == -1)
2824 eap->save_msg_silent = msg_silent;
2825 msg_silent = 0;
2826 continue;
2827
2828 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2829 {
2830 cmdmod.split |= WSP_VERT;
2831 continue;
2832 }
2833 if (!checkforcmd(&p, "verbose", 4))
2834 break;
2835 if (eap->verbose_save < 0)
2836 eap->verbose_save = p_verbose;
2837 if (vim_isdigit(*eap->cmd))
2838 p_verbose = atoi((char *)eap->cmd);
2839 else
2840 p_verbose = 1;
2841 eap->cmd = p;
2842 continue;
2843 }
2844 break;
2845 }
2846
2847 return OK;
2848}
2849
2850/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002851 * Parse the address range, if any, in "eap".
2852 * Return FAIL and set "errormsg" or return OK.
2853 */
2854 int
2855parse_cmd_address(exarg_T *eap, char_u **errormsg)
2856{
2857 int address_count = 1;
2858 linenr_T lnum;
2859
2860 // Repeat for all ',' or ';' separated addresses.
2861 for (;;)
2862 {
2863 eap->line1 = eap->line2;
2864 switch (eap->addr_type)
2865 {
2866 case ADDR_LINES:
2867 // default is current line number
2868 eap->line2 = curwin->w_cursor.lnum;
2869 break;
2870 case ADDR_WINDOWS:
2871 eap->line2 = CURRENT_WIN_NR;
2872 break;
2873 case ADDR_ARGUMENTS:
2874 eap->line2 = curwin->w_arg_idx + 1;
2875 if (eap->line2 > ARGCOUNT)
2876 eap->line2 = ARGCOUNT;
2877 break;
2878 case ADDR_LOADED_BUFFERS:
2879 case ADDR_BUFFERS:
2880 eap->line2 = curbuf->b_fnum;
2881 break;
2882 case ADDR_TABS:
2883 eap->line2 = CURRENT_TAB_NR;
2884 break;
2885 case ADDR_TABS_RELATIVE:
2886 eap->line2 = 1;
2887 break;
2888#ifdef FEAT_QUICKFIX
2889 case ADDR_QUICKFIX:
2890 eap->line2 = qf_get_cur_valid_idx(eap);
2891 break;
2892#endif
2893 }
2894 eap->cmd = skipwhite(eap->cmd);
2895 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip,
2896 eap->addr_count == 0, address_count++);
2897 if (eap->cmd == NULL) // error detected
2898 return FAIL;
2899 if (lnum == MAXLNUM)
2900 {
2901 if (*eap->cmd == '%') // '%' - all lines
2902 {
2903 ++eap->cmd;
2904 switch (eap->addr_type)
2905 {
2906 case ADDR_LINES:
2907 eap->line1 = 1;
2908 eap->line2 = curbuf->b_ml.ml_line_count;
2909 break;
2910 case ADDR_LOADED_BUFFERS:
2911 {
2912 buf_T *buf = firstbuf;
2913
2914 while (buf->b_next != NULL
2915 && buf->b_ml.ml_mfp == NULL)
2916 buf = buf->b_next;
2917 eap->line1 = buf->b_fnum;
2918 buf = lastbuf;
2919 while (buf->b_prev != NULL
2920 && buf->b_ml.ml_mfp == NULL)
2921 buf = buf->b_prev;
2922 eap->line2 = buf->b_fnum;
2923 break;
2924 }
2925 case ADDR_BUFFERS:
2926 eap->line1 = firstbuf->b_fnum;
2927 eap->line2 = lastbuf->b_fnum;
2928 break;
2929 case ADDR_WINDOWS:
2930 case ADDR_TABS:
2931 if (IS_USER_CMDIDX(eap->cmdidx))
2932 {
2933 eap->line1 = 1;
2934 eap->line2 = eap->addr_type == ADDR_WINDOWS
2935 ? LAST_WIN_NR : LAST_TAB_NR;
2936 }
2937 else
2938 {
2939 // there is no Vim command which uses '%' and
2940 // ADDR_WINDOWS or ADDR_TABS
2941 *errormsg = (char_u *)_(e_invrange);
2942 return FAIL;
2943 }
2944 break;
2945 case ADDR_TABS_RELATIVE:
2946 *errormsg = (char_u *)_(e_invrange);
2947 return FAIL;
2948 case ADDR_ARGUMENTS:
2949 if (ARGCOUNT == 0)
2950 eap->line1 = eap->line2 = 0;
2951 else
2952 {
2953 eap->line1 = 1;
2954 eap->line2 = ARGCOUNT;
2955 }
2956 break;
2957#ifdef FEAT_QUICKFIX
2958 case ADDR_QUICKFIX:
2959 eap->line1 = 1;
2960 eap->line2 = qf_get_size(eap);
2961 if (eap->line2 == 0)
2962 eap->line2 = 1;
2963 break;
2964#endif
2965 }
2966 ++eap->addr_count;
2967 }
2968 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2969 {
2970 pos_T *fp;
2971
2972 // '*' - visual area
2973 if (eap->addr_type != ADDR_LINES)
2974 {
2975 *errormsg = (char_u *)_(e_invrange);
2976 return FAIL;
2977 }
2978
2979 ++eap->cmd;
2980 if (!eap->skip)
2981 {
2982 fp = getmark('<', FALSE);
2983 if (check_mark(fp) == FAIL)
2984 return FAIL;
2985 eap->line1 = fp->lnum;
2986 fp = getmark('>', FALSE);
2987 if (check_mark(fp) == FAIL)
2988 return FAIL;
2989 eap->line2 = fp->lnum;
2990 ++eap->addr_count;
2991 }
2992 }
2993 }
2994 else
2995 eap->line2 = lnum;
2996 eap->addr_count++;
2997
2998 if (*eap->cmd == ';')
2999 {
3000 if (!eap->skip)
3001 {
3002 curwin->w_cursor.lnum = eap->line2;
3003 // don't leave the cursor on an illegal line or column
3004 check_cursor();
3005 }
3006 }
3007 else if (*eap->cmd != ',')
3008 break;
3009 ++eap->cmd;
3010 }
3011
3012 // One address given: set start and end lines.
3013 if (eap->addr_count == 1)
3014 {
3015 eap->line1 = eap->line2;
3016 // ... but only implicit: really no address given
3017 if (lnum == MAXLNUM)
3018 eap->addr_count = 0;
3019 }
3020 return OK;
3021}
3022
3023/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003024 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 * If there is a match advance "pp" to the argument and return TRUE.
3026 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003027 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003028checkforcmd(
3029 char_u **pp, /* start of command */
3030 char *cmd, /* name of command */
3031 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032{
3033 int i;
3034
3035 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003036 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 break;
3038 if (i >= len && !isalpha((*pp)[i]))
3039 {
3040 *pp = skipwhite(*pp + i);
3041 return TRUE;
3042 }
3043 return FALSE;
3044}
3045
3046/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003047 * Append "cmd" to the error message in IObuff.
3048 * Takes care of limiting the length and handling 0xa0, which would be
3049 * invisible otherwise.
3050 */
3051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003052append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003053{
3054 char_u *s = cmd;
3055 char_u *d;
3056
3057 STRCAT(IObuff, ": ");
3058 d = IObuff + STRLEN(IObuff);
3059 while (*s != NUL && d - IObuff < IOSIZE - 7)
3060 {
3061 if (
3062#ifdef FEAT_MBYTE
3063 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3064#endif
3065 *s == 0xa0)
3066 {
3067 s +=
3068#ifdef FEAT_MBYTE
3069 enc_utf8 ? 2 :
3070#endif
3071 1;
3072 STRCPY(d, "<a0>");
3073 d += 4;
3074 }
3075 else
3076 MB_COPY_CHAR(s, d);
3077 }
3078 *d = NUL;
3079}
3080
3081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003083 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003085 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 * Returns NULL for an ambiguous user command.
3087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003089find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
3091 int len;
3092 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003093 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094
3095 /*
3096 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003097 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 * - the 'k' command can directly be followed by any character.
3099 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003100 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003102 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 */
3104 p = eap->cmd;
3105 if (*p == 'k')
3106 {
3107 eap->cmdidx = CMD_k;
3108 ++p;
3109 }
3110 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003111 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3112 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 || p[1] == 'g'
3114 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3115 || p[1] == 'I'
3116 || (p[1] == 'r' && p[2] != 'e')))
3117 {
3118 eap->cmdidx = CMD_substitute;
3119 ++p;
3120 }
3121 else
3122 {
3123 while (ASCII_ISALPHA(*p))
3124 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003125 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003126 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003127 while (ASCII_ISALNUM(*p))
3128 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003129
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 /* check for non-alpha command */
3131 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3132 ++p;
3133 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003134 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3135 {
3136 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3137 * :delete with the 'l' flag. Same for 'p'. */
3138 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003139 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003140 break;
3141 if (i == len - 1)
3142 {
3143 --len;
3144 if (p[-1] == 'l')
3145 eap->flags |= EXFLAG_LIST;
3146 else
3147 eap->flags |= EXFLAG_PRINT;
3148 }
3149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003151 if (ASCII_ISLOWER(eap->cmd[0]))
3152 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003153 int c1 = eap->cmd[0];
3154 int c2 = eap->cmd[1];
3155
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003156 if (command_count != (int)CMD_SIZE)
3157 {
3158 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3159 getout(1);
3160 }
3161
3162 /* Use a precomputed index for fast look-up in cmdnames[]
3163 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003164 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3165 if (ASCII_ISLOWER(c2))
3166 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003169 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3172 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3173 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3174 (size_t)len) == 0)
3175 {
3176#ifdef FEAT_EVAL
3177 if (full != NULL
3178 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3179 *full = TRUE;
3180#endif
3181 break;
3182 }
3183
3184#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003185 /* Look for a user defined command as a last resort. Let ":Print" be
3186 * overruled by a user defined command. */
3187 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3188 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003190 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 while (ASCII_ISALNUM(*p))
3192 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003193 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 }
3195#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003196 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 eap->cmdidx = CMD_SIZE;
3198 }
3199
3200 return p;
3201}
3202
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003203#ifdef FEAT_USR_CMDS
3204/*
3205 * Search for a user command that matches "eap->cmd".
3206 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3207 * Return a pointer to just after the command.
3208 * Return NULL if there is no matching command.
3209 */
3210 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003211find_ucmd(
3212 exarg_T *eap,
3213 char_u *p, /* end of the command (possibly including count) */
3214 int *full, /* set to TRUE for a full match */
3215 expand_T *xp, /* used for completion, NULL otherwise */
3216 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003217{
3218 int len = (int)(p - eap->cmd);
3219 int j, k, matchlen = 0;
3220 ucmd_T *uc;
3221 int found = FALSE;
3222 int possible = FALSE;
3223 char_u *cp, *np; /* Point into typed cmd and test name */
3224 garray_T *gap;
3225 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3226 only full match global is accepted. */
3227
3228 /*
3229 * Look for buffer-local user commands first, then global ones.
3230 */
3231 gap = &curbuf->b_ucmds;
3232 for (;;)
3233 {
3234 for (j = 0; j < gap->ga_len; ++j)
3235 {
3236 uc = USER_CMD_GA(gap, j);
3237 cp = eap->cmd;
3238 np = uc->uc_name;
3239 k = 0;
3240 while (k < len && *np != NUL && *cp++ == *np++)
3241 k++;
3242 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3243 {
3244 /* If finding a second match, the command is ambiguous. But
3245 * not if a buffer-local command wasn't a full match and a
3246 * global command is a full match. */
3247 if (k == len && found && *np != NUL)
3248 {
3249 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003250 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003251 amb_local = TRUE;
3252 }
3253
3254 if (!found || (k == len && *np == NUL))
3255 {
3256 /* If we matched up to a digit, then there could
3257 * be another command including the digit that we
3258 * should use instead.
3259 */
3260 if (k == len)
3261 found = TRUE;
3262 else
3263 possible = TRUE;
3264
3265 if (gap == &ucmds)
3266 eap->cmdidx = CMD_USER;
3267 else
3268 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003269 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003270 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003271 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003272
3273# ifdef FEAT_CMDL_COMPL
3274 if (compl != NULL)
3275 *compl = uc->uc_compl;
3276# ifdef FEAT_EVAL
3277 if (xp != NULL)
3278 {
3279 xp->xp_arg = uc->uc_compl_arg;
3280 xp->xp_scriptID = uc->uc_scriptID;
3281 }
3282# endif
3283# endif
3284 /* Do not search for further abbreviations
3285 * if this is an exact match. */
3286 matchlen = k;
3287 if (k == len && *np == NUL)
3288 {
3289 if (full != NULL)
3290 *full = TRUE;
3291 amb_local = FALSE;
3292 break;
3293 }
3294 }
3295 }
3296 }
3297
3298 /* Stop if we found a full match or searched all. */
3299 if (j < gap->ga_len || gap == &ucmds)
3300 break;
3301 gap = &ucmds;
3302 }
3303
3304 /* Only found ambiguous matches. */
3305 if (amb_local)
3306 {
3307 if (xp != NULL)
3308 xp->xp_context = EXPAND_UNSUCCESSFUL;
3309 return NULL;
3310 }
3311
3312 /* The match we found may be followed immediately by a number. Move "p"
3313 * back to point to it. */
3314 if (found || possible)
3315 return p + (matchlen - len);
3316 return p;
3317}
3318#endif
3319
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003321static struct cmdmod
3322{
3323 char *name;
3324 int minlen;
3325 int has_count; /* :123verbose :3tab */
3326} cmdmods[] = {
3327 {"aboveleft", 3, FALSE},
3328 {"belowright", 3, FALSE},
3329 {"botright", 2, FALSE},
3330 {"browse", 3, FALSE},
3331 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003332 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003333 {"hide", 3, FALSE},
3334 {"keepalt", 5, FALSE},
3335 {"keepjumps", 5, FALSE},
3336 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003337 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003338 {"leftabove", 5, FALSE},
3339 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003340 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003341 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003342 {"rightbelow", 6, FALSE},
3343 {"sandbox", 3, FALSE},
3344 {"silent", 3, FALSE},
3345 {"tab", 3, TRUE},
3346 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003347 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003348 {"verbose", 4, TRUE},
3349 {"vertical", 4, FALSE},
3350};
3351
3352/*
3353 * Return length of a command modifier (including optional count).
3354 * Return zero when it's not a modifier.
3355 */
3356 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003357modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003358{
3359 int i, j;
3360 char_u *p = cmd;
3361
3362 if (VIM_ISDIGIT(*cmd))
3363 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003364 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003365 {
3366 for (j = 0; p[j] != NUL; ++j)
3367 if (p[j] != cmdmods[i].name[j])
3368 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003369 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003370 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003371 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003372 }
3373 return 0;
3374}
3375
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376/*
3377 * Return > 0 if an Ex command "name" exists.
3378 * Return 2 if there is an exact match.
3379 * Return 3 if there is an ambiguous match.
3380 */
3381 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003382cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383{
3384 exarg_T ea;
3385 int full = FALSE;
3386 int i;
3387 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003388 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
3390 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003391 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
3393 for (j = 0; name[j] != NUL; ++j)
3394 if (name[j] != cmdmods[i].name[j])
3395 break;
3396 if (name[j] == NUL && j >= cmdmods[i].minlen)
3397 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3398 }
3399
Bram Moolenaara9587612006-05-04 21:47:50 +00003400 /* Check built-in commands and user defined commands.
3401 * For ":2match" and ":3match" we need to skip the number. */
3402 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003404 p = find_command(&ea, &full);
3405 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003407 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3408 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003409 if (*skipwhite(p) != NUL)
3410 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3412}
3413#endif
3414
3415/*
3416 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3417 * we don't need/want deleted. Maybe this could be done better if we didn't
3418 * repeat all this stuff. The only problem is that they may not stay
3419 * perfectly compatible with each other, but then the command line syntax
3420 * probably won't change that much -- webb.
3421 */
3422 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003423set_one_cmd_context(
3424 expand_T *xp,
3425 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
3427 char_u *p;
3428 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003429 int len = 0;
3430 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3432 int compl = EXPAND_NOTHING;
3433#endif
3434#ifdef FEAT_CMDL_COMPL
3435 int delim;
3436#endif
3437 int forceit = FALSE;
3438 int usefilter = FALSE; /* filter instead of file name */
3439
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003440 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 xp->xp_pattern = buff;
3442 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003443 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
3445/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003446 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 */
3448 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3449 ;
3450 xp->xp_pattern = cmd;
3451
3452 if (*cmd == NUL)
3453 return NULL;
3454 if (*cmd == '"') /* ignore comment lines */
3455 {
3456 xp->xp_context = EXPAND_NOTHING;
3457 return NULL;
3458 }
3459
3460/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003461 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 */
3463 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 xp->xp_pattern = cmd;
3465 if (*cmd == NUL)
3466 return NULL;
3467 if (*cmd == '"')
3468 {
3469 xp->xp_context = EXPAND_NOTHING;
3470 return NULL;
3471 }
3472
3473 if (*cmd == '|' || *cmd == '\n')
3474 return cmd + 1; /* There's another command */
3475
3476 /*
3477 * Isolate the command and search for it in the command table.
3478 * Exceptions:
3479 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003480 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3482 */
3483 if (*cmd == 'k' && cmd[1] != 'e')
3484 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003485 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 p = cmd + 1;
3487 }
3488 else
3489 {
3490 p = cmd;
3491 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3492 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003493 /* a user command may contain digits */
3494 if (ASCII_ISUPPER(cmd[0]))
3495 while (ASCII_ISALNUM(*p) || *p == '*')
3496 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003497 /* for python 3.x: ":py3*" commands completion */
3498 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003499 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003500 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003501 while (ASCII_ISALPHA(*p) || *p == '*')
3502 ++p;
3503 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003504 /* check for non-alpha command */
3505 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3506 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003507 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003509 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 {
3511 xp->xp_context = EXPAND_UNSUCCESSFUL;
3512 return NULL;
3513 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003514 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003515 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3516 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3517 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 break;
3519
3520#ifdef FEAT_USR_CMDS
3521 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3523 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524#endif
3525 }
3526
3527 /*
3528 * If the cursor is touching the command, and it ends in an alpha-numeric
3529 * character, complete the command name.
3530 */
3531 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3532 return NULL;
3533
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003534 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
3536 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3537 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003538 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 p = cmd + 1;
3540 }
3541#ifdef FEAT_USR_CMDS
3542 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3543 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003544 ea.cmd = cmd;
3545 p = find_ucmd(&ea, p, NULL, xp,
3546# if defined(FEAT_CMDL_COMPL)
3547 &compl
3548# else
3549 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003551 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003552 if (p == NULL)
3553 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555#endif
3556 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003557 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
3559 /* Not still touching the command and it was an illegal one */
3560 xp->xp_context = EXPAND_UNSUCCESSFUL;
3561 return NULL;
3562 }
3563
3564 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3565
3566 if (*p == '!') /* forced commands */
3567 {
3568 forceit = TRUE;
3569 ++p;
3570 }
3571
3572/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003573 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003575 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003576 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577
3578 arg = skipwhite(p);
3579
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003580 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
3582 if (*arg == '>') /* append */
3583 {
3584 if (*++arg == '>')
3585 ++arg;
3586 arg = skipwhite(arg);
3587 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003588 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
3590 ++arg;
3591 usefilter = TRUE;
3592 }
3593 }
3594
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003595 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
3597 usefilter = forceit; /* :r! filter if forced */
3598 if (*arg == '!') /* :r !filter */
3599 {
3600 ++arg;
3601 usefilter = TRUE;
3602 }
3603 }
3604
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003605 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
3607 while (*arg == *cmd) /* allow any number of '>' or '<' */
3608 ++arg;
3609 arg = skipwhite(arg);
3610 }
3611
3612 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003613 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
3615 /* Check if we're in the +command */
3616 p = arg + 1;
3617 arg = skip_cmd_arg(arg, FALSE);
3618
3619 /* Still touching the command after '+'? */
3620 if (*arg == NUL)
3621 return p;
3622
3623 /* Skip space(s) after +command to get to the real argument */
3624 arg = skipwhite(arg);
3625 }
3626
3627 /*
3628 * Check for '|' to separate commands and '"' to start comments.
3629 * Don't do this for ":read !cmd" and ":write !cmd".
3630 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003631 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 {
3633 p = arg;
3634 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003635 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 p += 2;
3637 while (*p)
3638 {
3639 if (*p == Ctrl_V)
3640 {
3641 if (p[1] != NUL)
3642 ++p;
3643 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003644 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 || *p == '|' || *p == '\n')
3646 {
3647 if (*(p - 1) != '\\')
3648 {
3649 if (*p == '|' || *p == '\n')
3650 return p + 1;
3651 return NULL; /* It's a comment */
3652 }
3653 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003654 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 }
3656 }
3657
3658 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003659 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 vim_strchr((char_u *)"|\"", *arg) == NULL)
3661 return NULL;
3662
3663 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003664 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003666 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003667 while (*p && p < buff + len)
3668 {
3669 if (*p == ' ' || *p == TAB)
3670 {
3671 /* argument starts after a space */
3672 xp->xp_pattern = ++p;
3673 }
3674 else
3675 {
3676 if (*p == '\\' && *(p + 1) != NUL)
3677 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003678 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003679 }
3680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003682 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003684 int c;
3685 int in_quote = FALSE;
3686 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687
3688 /*
3689 * Allow spaces within back-quotes to count as part of the argument
3690 * being expanded.
3691 */
3692 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003693 p = xp->xp_pattern;
3694 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003696#ifdef FEAT_MBYTE
3697 if (has_mbyte)
3698 c = mb_ptr2char(p);
3699 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003701 c = *p;
3702 if (c == '\\' && p[1] != NUL)
3703 ++p;
3704 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
3706 if (!in_quote)
3707 {
3708 xp->xp_pattern = p;
3709 bow = p + 1;
3710 }
3711 in_quote = !in_quote;
3712 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003713 /* An argument can contain just about everything, except
3714 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003715 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003716#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003717 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003718#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003719 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003720 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003721 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003722 while (*p != NUL)
3723 {
3724#ifdef FEAT_MBYTE
3725 if (has_mbyte)
3726 c = mb_ptr2char(p);
3727 else
3728#endif
3729 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003730 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003731 break;
3732#ifdef FEAT_MBYTE
3733 if (has_mbyte)
3734 len = (*mb_ptr2len)(p);
3735 else
3736#endif
3737 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003738 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003739 }
3740 if (in_quote)
3741 bow = p;
3742 else
3743 xp->xp_pattern = p;
3744 p -= len;
3745 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003746 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
3748
3749 /*
3750 * If we are still inside the quotes, and we passed a space, just
3751 * expand from there.
3752 */
3753 if (bow != NULL && in_quote)
3754 xp->xp_pattern = bow;
3755 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003756
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003757 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003758 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003759 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003760#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003761 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003762#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003763 /* When still after the command name expand executables. */
3764 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003765 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767
3768 /* Check for environment variable */
3769 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003770#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 || *xp->xp_pattern == '%'
3772#endif
3773 )
3774 {
3775 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3776 if (!vim_isIDc(*p))
3777 break;
3778 if (*p == NUL)
3779 {
3780 xp->xp_context = EXPAND_ENV_VARS;
3781 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003782#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3783 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003784 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003785 compl = EXPAND_ENV_VARS;
3786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 }
3788 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003789#if defined(FEAT_CMDL_COMPL)
3790 /* Check for user names */
3791 if (*xp->xp_pattern == '~')
3792 {
3793 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3794 ;
3795 /* Complete ~user only if it partially matches a user name.
3796 * A full match ~user<Tab> will be replaced by user's home
3797 * directory i.e. something like ~user<Tab> -> /home/user/ */
3798 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003799 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003800 {
3801 xp->xp_context = EXPAND_USER;
3802 ++xp->xp_pattern;
3803 }
3804 }
3805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 }
3807
3808/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003809 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003811 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003813 case CMD_find:
3814 case CMD_sfind:
3815 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003816 if (xp->xp_context == EXPAND_FILES)
3817 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003818 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 case CMD_cd:
3820 case CMD_chdir:
3821 case CMD_lcd:
3822 case CMD_lchdir:
3823 if (xp->xp_context == EXPAND_FILES)
3824 xp->xp_context = EXPAND_DIRECTORIES;
3825 break;
3826 case CMD_help:
3827 xp->xp_context = EXPAND_HELP;
3828 xp->xp_pattern = arg;
3829 break;
3830
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003831 /* Command modifiers: return the argument.
3832 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003834 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 case CMD_belowright:
3836 case CMD_botright:
3837 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003838 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003839 case CMD_cdo:
3840 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003842 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 case CMD_folddoclosed:
3844 case CMD_folddoopen:
3845 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003846 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 case CMD_keepjumps:
3848 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003849 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003850 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003852 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003854 case CMD_noautocmd:
3855 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003857 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003859 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003860 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 case CMD_topleft:
3862 case CMD_verbose:
3863 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003864 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 return arg;
3866
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003867 case CMD_filter:
3868 if (*arg != NUL)
3869 arg = skip_vimgrep_pat(arg, NULL, NULL);
3870 if (arg == NULL || *arg == NUL)
3871 {
3872 xp->xp_context = EXPAND_NOTHING;
3873 return NULL;
3874 }
3875 return skipwhite(arg);
3876
Bram Moolenaar4f688582007-07-24 12:34:30 +00003877#ifdef FEAT_CMDL_COMPL
3878# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 case CMD_match:
3880 if (*arg == NUL || !ends_excmd(*arg))
3881 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003882 /* also complete "None" */
3883 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 arg = skipwhite(skiptowhite(arg));
3885 if (*arg != NUL)
3886 {
3887 xp->xp_context = EXPAND_NOTHING;
3888 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3889 }
3890 }
3891 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003892# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894/*
3895 * All completion for the +cmdline_compl feature goes here.
3896 */
3897
3898# ifdef FEAT_USR_CMDS
3899 case CMD_command:
3900 /* Check for attributes */
3901 while (*arg == '-')
3902 {
3903 arg++; /* Skip "-" */
3904 p = skiptowhite(arg);
3905 if (*p == NUL)
3906 {
3907 /* Cursor is still in the attribute */
3908 p = vim_strchr(arg, '=');
3909 if (p == NULL)
3910 {
3911 /* No "=", so complete attribute names */
3912 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3913 xp->xp_pattern = arg;
3914 return NULL;
3915 }
3916
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003917 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 * their arguments as well.
3919 */
3920 if (STRNICMP(arg, "complete", p - arg) == 0)
3921 {
3922 xp->xp_context = EXPAND_USER_COMPLETE;
3923 xp->xp_pattern = p + 1;
3924 return NULL;
3925 }
3926 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3927 {
3928 xp->xp_context = EXPAND_USER_NARGS;
3929 xp->xp_pattern = p + 1;
3930 return NULL;
3931 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003932 else if (STRNICMP(arg, "addr", p - arg) == 0)
3933 {
3934 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3935 xp->xp_pattern = p + 1;
3936 return NULL;
3937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 return NULL;
3939 }
3940 arg = skipwhite(p);
3941 }
3942
3943 /* After the attributes comes the new command name */
3944 p = skiptowhite(arg);
3945 if (*p == NUL)
3946 {
3947 xp->xp_context = EXPAND_USER_COMMANDS;
3948 xp->xp_pattern = arg;
3949 break;
3950 }
3951
3952 /* And finally comes a normal command */
3953 return skipwhite(p);
3954
3955 case CMD_delcommand:
3956 xp->xp_context = EXPAND_USER_COMMANDS;
3957 xp->xp_pattern = arg;
3958 break;
3959# endif
3960
3961 case CMD_global:
3962 case CMD_vglobal:
3963 delim = *arg; /* get the delimiter */
3964 if (delim)
3965 ++arg; /* skip delimiter if there is one */
3966
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)
3974 return arg + 1;
3975 break;
3976 case CMD_and:
3977 case CMD_substitute:
3978 delim = *arg;
3979 if (delim)
3980 {
3981 /* skip "from" part */
3982 ++arg;
3983 arg = skip_regexp(arg, delim, p_magic, NULL);
3984 }
3985 /* skip "to" part */
3986 while (arg[0] != NUL && arg[0] != delim)
3987 {
3988 if (arg[0] == '\\' && arg[1] != NUL)
3989 ++arg;
3990 ++arg;
3991 }
3992 if (arg[0] != NUL) /* skip delimiter */
3993 ++arg;
3994 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3995 ++arg;
3996 if (arg[0] != NUL)
3997 return arg;
3998 break;
3999 case CMD_isearch:
4000 case CMD_dsearch:
4001 case CMD_ilist:
4002 case CMD_dlist:
4003 case CMD_ijump:
4004 case CMD_psearch:
4005 case CMD_djump:
4006 case CMD_isplit:
4007 case CMD_dsplit:
4008 arg = skipwhite(skipdigits(arg)); /* skip count */
4009 if (*arg == '/') /* Match regexp, not just whole words */
4010 {
4011 for (++arg; *arg && *arg != '/'; arg++)
4012 if (*arg == '\\' && arg[1] != NUL)
4013 arg++;
4014 if (*arg)
4015 {
4016 arg = skipwhite(arg + 1);
4017
4018 /* Check for trailing illegal characters */
4019 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4020 xp->xp_context = EXPAND_NOTHING;
4021 else
4022 return arg;
4023 }
4024 }
4025 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004026
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 case CMD_autocmd:
4028 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004030 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 case CMD_set:
4033 set_context_in_set_cmd(xp, arg, 0);
4034 break;
4035 case CMD_setglobal:
4036 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4037 break;
4038 case CMD_setlocal:
4039 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4040 break;
4041 case CMD_tag:
4042 case CMD_stag:
4043 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004044 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 case CMD_tselect:
4046 case CMD_stselect:
4047 case CMD_ptselect:
4048 case CMD_tjump:
4049 case CMD_stjump:
4050 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004051 if (*p_wop != NUL)
4052 xp->xp_context = EXPAND_TAGS_LISTFILES;
4053 else
4054 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 xp->xp_pattern = arg;
4056 break;
4057 case CMD_augroup:
4058 xp->xp_context = EXPAND_AUGROUP;
4059 xp->xp_pattern = arg;
4060 break;
4061#ifdef FEAT_SYN_HL
4062 case CMD_syntax:
4063 set_context_in_syntax_cmd(xp, arg);
4064 break;
4065#endif
4066#ifdef FEAT_EVAL
4067 case CMD_let:
4068 case CMD_if:
4069 case CMD_elseif:
4070 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004071 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 case CMD_echo:
4073 case CMD_echon:
4074 case CMD_execute:
4075 case CMD_echomsg:
4076 case CMD_echoerr:
4077 case CMD_call:
4078 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004079 case CMD_cexpr:
4080 case CMD_caddexpr:
4081 case CMD_cgetexpr:
4082 case CMD_lexpr:
4083 case CMD_laddexpr:
4084 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004085 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 break;
4087
4088 case CMD_unlet:
4089 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4090 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02004091
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 xp->xp_context = EXPAND_USER_VARS;
4093 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02004094
4095 if (*xp->xp_pattern == '$')
4096 {
4097 xp->xp_context = EXPAND_ENV_VARS;
4098 ++xp->xp_pattern;
4099 }
4100
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 break;
4102
4103 case CMD_function:
4104 case CMD_delfunction:
4105 xp->xp_context = EXPAND_USER_FUNC;
4106 xp->xp_pattern = arg;
4107 break;
4108
4109 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004110 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 break;
4112#endif
4113 case CMD_highlight:
4114 set_context_in_highlight_cmd(xp, arg);
4115 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004116#ifdef FEAT_CSCOPE
4117 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004118 case CMD_lcscope:
4119 case CMD_scscope:
4120 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004121 break;
4122#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004123#ifdef FEAT_SIGNS
4124 case CMD_sign:
4125 set_context_in_sign_cmd(xp, arg);
4126 break;
4127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 case CMD_bdelete:
4129 case CMD_bwipeout:
4130 case CMD_bunload:
4131 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4132 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004133 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 case CMD_buffer:
4135 case CMD_sbuffer:
4136 case CMD_checktime:
4137 xp->xp_context = EXPAND_BUFFERS;
4138 xp->xp_pattern = arg;
4139 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140#ifdef FEAT_USR_CMDS
4141 case CMD_USER:
4142 case CMD_USER_BUF:
4143 if (compl != EXPAND_NOTHING)
4144 {
4145 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004146 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 {
4148# ifdef FEAT_MENU
4149 if (compl == EXPAND_MENUS)
4150 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4151# endif
4152 if (compl == EXPAND_COMMANDS)
4153 return arg;
4154 if (compl == EXPAND_MAPPINGS)
4155 return set_context_in_map_cmd(xp, (char_u *)"map",
4156 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004157 /* Find start of last argument. */
4158 p = arg;
4159 while (*p)
4160 {
4161 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004162 /* argument starts after a space */
4163 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004164 else if (*p == '\\' && *(p + 1) != NUL)
4165 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004166 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 xp->xp_pattern = arg;
4169 }
4170 xp->xp_context = compl;
4171 }
4172 break;
4173#endif
4174 case CMD_map: case CMD_noremap:
4175 case CMD_nmap: case CMD_nnoremap:
4176 case CMD_vmap: case CMD_vnoremap:
4177 case CMD_omap: case CMD_onoremap:
4178 case CMD_imap: case CMD_inoremap:
4179 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004180 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004181 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004182 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004183 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004185 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 case CMD_unmap:
4187 case CMD_nunmap:
4188 case CMD_vunmap:
4189 case CMD_ounmap:
4190 case CMD_iunmap:
4191 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004192 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004193 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004194 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004195 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004197 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004198 case CMD_mapclear:
4199 case CMD_nmapclear:
4200 case CMD_vmapclear:
4201 case CMD_omapclear:
4202 case CMD_imapclear:
4203 case CMD_cmapclear:
4204 case CMD_lmapclear:
4205 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004206 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004207 case CMD_xmapclear:
4208 xp->xp_context = EXPAND_MAPCLEAR;
4209 xp->xp_pattern = arg;
4210 break;
4211
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 case CMD_abbreviate: case CMD_noreabbrev:
4213 case CMD_cabbrev: case CMD_cnoreabbrev:
4214 case CMD_iabbrev: case CMD_inoreabbrev:
4215 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004216 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 case CMD_unabbreviate:
4218 case CMD_cunabbrev:
4219 case CMD_iunabbrev:
4220 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004221 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222#ifdef FEAT_MENU
4223 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4224 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4225 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4226 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4227 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4228 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4229 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4230 case CMD_tmenu: case CMD_tunmenu:
4231 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4232 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4233#endif
4234
4235 case CMD_colorscheme:
4236 xp->xp_context = EXPAND_COLORS;
4237 xp->xp_pattern = arg;
4238 break;
4239
4240 case CMD_compiler:
4241 xp->xp_context = EXPAND_COMPILER;
4242 xp->xp_pattern = arg;
4243 break;
4244
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004245 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004246 xp->xp_context = EXPAND_OWNSYNTAX;
4247 xp->xp_pattern = arg;
4248 break;
4249
4250 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004251 xp->xp_context = EXPAND_FILETYPE;
4252 xp->xp_pattern = arg;
4253 break;
4254
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004255 case CMD_packadd:
4256 xp->xp_context = EXPAND_PACKADD;
4257 xp->xp_pattern = arg;
4258 break;
4259
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4261 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4262 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004263 p = skiptowhite(arg);
4264 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 {
4266 xp->xp_context = EXPAND_LANGUAGE;
4267 xp->xp_pattern = arg;
4268 }
4269 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004270 {
4271 if ( STRNCMP(arg, "messages", p - arg) == 0
4272 || STRNCMP(arg, "ctype", p - arg) == 0
4273 || STRNCMP(arg, "time", p - arg) == 0)
4274 {
4275 xp->xp_context = EXPAND_LOCALES;
4276 xp->xp_pattern = skipwhite(p);
4277 }
4278 else
4279 xp->xp_context = EXPAND_NOTHING;
4280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 break;
4282#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004283#if defined(FEAT_PROFILE)
4284 case CMD_profile:
4285 set_context_in_profile_cmd(xp, arg);
4286 break;
4287#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004288 case CMD_behave:
4289 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004290 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004291 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004293 case CMD_messages:
4294 xp->xp_context = EXPAND_MESSAGES;
4295 xp->xp_pattern = arg;
4296 break;
4297
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004298#if defined(FEAT_CMDHIST)
4299 case CMD_history:
4300 xp->xp_context = EXPAND_HISTORY;
4301 xp->xp_pattern = arg;
4302 break;
4303#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004304#if defined(FEAT_PROFILE)
4305 case CMD_syntime:
4306 xp->xp_context = EXPAND_SYNTIME;
4307 xp->xp_pattern = arg;
4308 break;
4309#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004310
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004311 case CMD_argdelete:
4312 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4313 arg = xp->xp_pattern + 1;
4314 xp->xp_context = EXPAND_ARGLIST;
4315 xp->xp_pattern = arg;
4316 break;
4317
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318#endif /* FEAT_CMDL_COMPL */
4319
4320 default:
4321 break;
4322 }
4323 return NULL;
4324}
4325
4326/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004327 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 *
4329 * Backslashed delimiters after / or ? will be skipped, and commands will
4330 * not be expanded between /'s and ?'s or after "'".
4331 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004332 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 * Returns the "cmd" pointer advanced to beyond the range.
4334 */
4335 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004336skip_range(
4337 char_u *cmd,
4338 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004340 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004342 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004344 if (*cmd == '\\')
4345 {
4346 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4347 ++cmd;
4348 else
4349 break;
4350 }
4351 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
4353 if (*++cmd == NUL && ctx != NULL)
4354 *ctx = EXPAND_NOTHING;
4355 }
4356 else if (*cmd == '/' || *cmd == '?')
4357 {
4358 delim = *cmd++;
4359 while (*cmd != NUL && *cmd != delim)
4360 if (*cmd++ == '\\' && *cmd != NUL)
4361 ++cmd;
4362 if (*cmd == NUL && ctx != NULL)
4363 *ctx = EXPAND_NOTHING;
4364 }
4365 if (*cmd != NUL)
4366 ++cmd;
4367 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004368
4369 /* Skip ":" and white space. */
4370 while (*cmd == ':')
4371 cmd = skipwhite(cmd + 1);
4372
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 return cmd;
4374}
4375
4376/*
4377 * get a single EX address
4378 *
4379 * Set ptr to the next character after the part that was interpreted.
4380 * Set ptr to NULL when an error is encountered.
4381 *
4382 * Return MAXLNUM when no Ex address was found.
4383 */
4384 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004385get_address(
4386 exarg_T *eap UNUSED,
4387 char_u **ptr,
4388 int addr_type, /* flag: one of ADDR_LINES, ... */
4389 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004390 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004391 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392{
4393 int c;
4394 int i;
4395 long n;
4396 char_u *cmd;
4397 pos_T pos;
4398 pos_T *fp;
4399 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004400 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
4402 cmd = skipwhite(*ptr);
4403 lnum = MAXLNUM;
4404 do
4405 {
4406 switch (*cmd)
4407 {
4408 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004409 ++cmd;
4410 switch (addr_type)
4411 {
4412 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 lnum = curwin->w_cursor.lnum;
4414 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004416 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004417 break;
4418 case ADDR_ARGUMENTS:
4419 lnum = curwin->w_arg_idx + 1;
4420 break;
4421 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004422 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004423 lnum = curbuf->b_fnum;
4424 break;
4425 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004426 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004427 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004428 case ADDR_TABS_RELATIVE:
4429 EMSG(_(e_invrange));
4430 cmd = NULL;
4431 goto error;
4432 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004433#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004434 case ADDR_QUICKFIX:
4435 lnum = qf_get_cur_valid_idx(eap);
4436 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004437#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004438 }
4439 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
4441 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004442 ++cmd;
4443 switch (addr_type)
4444 {
4445 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 lnum = curbuf->b_ml.ml_line_count;
4447 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004448 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004449 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 break;
4451 case ADDR_ARGUMENTS:
4452 lnum = ARGCOUNT;
4453 break;
4454 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004455 buf = lastbuf;
4456 while (buf->b_ml.ml_mfp == NULL)
4457 {
4458 if (buf->b_prev == NULL)
4459 break;
4460 buf = buf->b_prev;
4461 }
4462 lnum = buf->b_fnum;
4463 break;
4464 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004465 lnum = lastbuf->b_fnum;
4466 break;
4467 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004468 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004469 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004470 case ADDR_TABS_RELATIVE:
4471 EMSG(_(e_invrange));
4472 cmd = NULL;
4473 goto error;
4474 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004475#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004476 case ADDR_QUICKFIX:
4477 lnum = qf_get_size(eap);
4478 if (lnum == 0)
4479 lnum = 1;
4480 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004481#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004482 }
4483 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004486 if (*++cmd == NUL)
4487 {
4488 cmd = NULL;
4489 goto error;
4490 }
4491 if (addr_type != ADDR_LINES)
4492 {
4493 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004494 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 goto error;
4496 }
4497 if (skip)
4498 ++cmd;
4499 else
4500 {
4501 /* Only accept a mark in another file when it is
4502 * used by itself: ":'M". */
4503 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4504 ++cmd;
4505 if (fp == (pos_T *)-1)
4506 /* Jumped to another file. */
4507 lnum = curwin->w_cursor.lnum;
4508 else
4509 {
4510 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 {
4512 cmd = NULL;
4513 goto error;
4514 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004515 lnum = fp->lnum;
4516 }
4517 }
4518 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519
4520 case '/':
4521 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004522 c = *cmd++;
4523 if (addr_type != ADDR_LINES)
4524 {
4525 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004526 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004527 goto error;
4528 }
4529 if (skip) /* skip "/pat/" */
4530 {
4531 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4532 if (*cmd == c)
4533 ++cmd;
4534 }
4535 else
4536 {
4537 pos = curwin->w_cursor; /* save curwin->w_cursor */
4538 /*
4539 * When '/' or '?' follows another address, start
4540 * from there.
4541 */
4542 if (lnum != MAXLNUM)
4543 curwin->w_cursor.lnum = lnum;
4544 /*
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004545 * Start a forward search at the end of the line (unless
4546 * before the first line).
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004547 * Start a backward search at the start of the line.
4548 * This makes sure we never match in the current
4549 * line, and can match anywhere in the
4550 * next/previous line.
4551 */
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004552 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004553 curwin->w_cursor.col = MAXCOL;
4554 else
4555 curwin->w_cursor.col = 0;
4556 searchcmdlen = 0;
4557 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004558 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004559 {
4560 curwin->w_cursor = pos;
4561 cmd = NULL;
4562 goto error;
4563 }
4564 lnum = curwin->w_cursor.lnum;
4565 curwin->w_cursor = pos;
4566 /* adjust command string pointer */
4567 cmd += searchcmdlen;
4568 }
4569 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570
4571 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004572 ++cmd;
4573 if (addr_type != ADDR_LINES)
4574 {
4575 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004576 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004577 goto error;
4578 }
4579 if (*cmd == '&')
4580 i = RE_SUBST;
4581 else if (*cmd == '?' || *cmd == '/')
4582 i = RE_SEARCH;
4583 else
4584 {
4585 EMSG(_(e_backslash));
4586 cmd = NULL;
4587 goto error;
4588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004590 if (!skip)
4591 {
4592 /*
4593 * When search follows another address, start from
4594 * there.
4595 */
4596 if (lnum != MAXLNUM)
4597 pos.lnum = lnum;
4598 else
4599 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004601 /*
4602 * Start the search just like for the above
4603 * do_search().
4604 */
4605 if (*cmd != '?')
4606 pos.col = MAXCOL;
4607 else
4608 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004609#ifdef FEAT_VIRTUALEDIT
4610 pos.coladd = 0;
4611#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004612 if (searchit(curwin, curbuf, &pos,
4613 *cmd == '?' ? BACKWARD : FORWARD,
4614 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004615 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004616 lnum = pos.lnum;
4617 else
4618 {
4619 cmd = NULL;
4620 goto error;
4621 }
4622 }
4623 ++cmd;
4624 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625
4626 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004627 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4628 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 }
4630
4631 for (;;)
4632 {
4633 cmd = skipwhite(cmd);
4634 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4635 break;
4636
4637 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004638 {
4639 switch (addr_type)
4640 {
4641 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004642 /* "+1" is same as ".+1" */
4643 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004644 break;
4645 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004646 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004647 break;
4648 case ADDR_ARGUMENTS:
4649 lnum = curwin->w_arg_idx + 1;
4650 break;
4651 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004652 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004653 lnum = curbuf->b_fnum;
4654 break;
4655 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004656 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004657 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004658 case ADDR_TABS_RELATIVE:
4659 lnum = 1;
4660 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004661#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004662 case ADDR_QUICKFIX:
4663 lnum = qf_get_cur_valid_idx(eap);
4664 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004665#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004666 }
4667 }
4668
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (VIM_ISDIGIT(*cmd))
4670 i = '+'; /* "number" is same as "+number" */
4671 else
4672 i = *cmd++;
4673 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4674 n = 1;
4675 else
4676 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004677
4678 if (addr_type == ADDR_TABS_RELATIVE)
4679 {
4680 EMSG(_(e_invrange));
4681 cmd = NULL;
4682 goto error;
4683 }
4684 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004685 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004686 lnum = compute_buffer_local_count(
4687 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004689 {
4690#ifdef FEAT_FOLDING
4691 /* Relative line addressing, need to adjust for folded lines
4692 * now, but only do it after the first address. */
4693 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4694 && address_count >= 2)
4695 (void)hasFolding(lnum, NULL, &lnum);
4696#endif
4697 if (i == '-')
4698 lnum -= n;
4699 else
4700 lnum += n;
4701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 }
4703 } while (*cmd == '/' || *cmd == '?');
4704
4705error:
4706 *ptr = cmd;
4707 return lnum;
4708}
4709
4710/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004711 * Get flags from an Ex command argument.
4712 */
4713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004714get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004715{
4716 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4717 {
4718 if (*eap->arg == 'l')
4719 eap->flags |= EXFLAG_LIST;
4720 else if (*eap->arg == 'p')
4721 eap->flags |= EXFLAG_PRINT;
4722 else
4723 eap->flags |= EXFLAG_NR;
4724 eap->arg = skipwhite(eap->arg + 1);
4725 }
4726}
4727
4728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 * Function called for command which is Not Implemented. NI!
4730 */
4731 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004732ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
4734 if (!eap->skip)
4735 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4736}
4737
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004738#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739/*
4740 * Function called for script command which is Not Implemented. NI!
4741 * Skips over ":perl <<EOF" constructs.
4742 */
4743 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004744ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
4746 if (!eap->skip)
4747 ex_ni(eap);
4748 else
4749 vim_free(script_get(eap, eap->arg));
4750}
4751#endif
4752
4753/*
4754 * Check range in Ex command for validity.
4755 * Return NULL when valid, error message when invalid.
4756 */
4757 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004758invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004760 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if ( eap->line1 < 0
4762 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004763 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004765
4766 if (eap->argt & RANGE)
4767 {
4768 switch(eap->addr_type)
4769 {
4770 case ADDR_LINES:
4771 if (!(eap->argt & NOTADR)
4772 && eap->line2 > curbuf->b_ml.ml_line_count
4773#ifdef FEAT_DIFF
4774 + (eap->cmdidx == CMD_diffget)
4775#endif
4776 )
4777 return (char_u *)_(e_invrange);
4778 break;
4779 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004780 /* add 1 if ARGCOUNT is 0 */
4781 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004782 return (char_u *)_(e_invrange);
4783 break;
4784 case ADDR_BUFFERS:
4785 if (eap->line1 < firstbuf->b_fnum
4786 || eap->line2 > lastbuf->b_fnum)
4787 return (char_u *)_(e_invrange);
4788 break;
4789 case ADDR_LOADED_BUFFERS:
4790 buf = firstbuf;
4791 while (buf->b_ml.ml_mfp == NULL)
4792 {
4793 if (buf->b_next == NULL)
4794 return (char_u *)_(e_invrange);
4795 buf = buf->b_next;
4796 }
4797 if (eap->line1 < buf->b_fnum)
4798 return (char_u *)_(e_invrange);
4799 buf = lastbuf;
4800 while (buf->b_ml.ml_mfp == NULL)
4801 {
4802 if (buf->b_prev == NULL)
4803 return (char_u *)_(e_invrange);
4804 buf = buf->b_prev;
4805 }
4806 if (eap->line2 > buf->b_fnum)
4807 return (char_u *)_(e_invrange);
4808 break;
4809 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004810 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004811 return (char_u *)_(e_invrange);
4812 break;
4813 case ADDR_TABS:
4814 if (eap->line2 > LAST_TAB_NR)
4815 return (char_u *)_(e_invrange);
4816 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004817 case ADDR_TABS_RELATIVE:
4818 /* Do nothing */
4819 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004820#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004821 case ADDR_QUICKFIX:
4822 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4823 return (char_u *)_(e_invrange);
4824 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004825#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004826 }
4827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 return NULL;
4829}
4830
4831/*
4832 * Correct the range for zero line number, if required.
4833 */
4834 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004835correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836{
4837 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4838 {
4839 if (eap->line1 == 0)
4840 eap->line1 = 1;
4841 if (eap->line2 == 0)
4842 eap->line2 = 1;
4843 }
4844}
4845
Bram Moolenaar748bf032005-02-02 23:04:36 +00004846#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004847static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004848
4849/*
4850 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4851 * pattern. Otherwise return eap->arg.
4852 */
4853 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004854skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004855{
4856 char_u *p = eap->arg;
4857
Bram Moolenaara37420f2006-02-04 22:37:47 +00004858 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4859 || eap->cmdidx == CMD_vimgrepadd
4860 || eap->cmdidx == CMD_lvimgrepadd
4861 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004862 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004863 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004864 if (p == NULL)
4865 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004866 }
4867 return p;
4868}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004869
4870/*
4871 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4872 * in the command line, so that things like % get expanded.
4873 */
4874 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004875replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004876{
4877 char_u *new_cmdline;
4878 char_u *program;
4879 char_u *pos;
4880 char_u *ptr;
4881 int len;
4882 int i;
4883
4884 /*
4885 * Don't do it when ":vimgrep" is used for ":grep".
4886 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004887 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4888 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4889 || eap->cmdidx == CMD_grepadd
4890 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004891 && !grep_internal(eap->cmdidx))
4892 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004893 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4894 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004895 {
4896 if (*curbuf->b_p_gp == NUL)
4897 program = p_gp;
4898 else
4899 program = curbuf->b_p_gp;
4900 }
4901 else
4902 {
4903 if (*curbuf->b_p_mp == NUL)
4904 program = p_mp;
4905 else
4906 program = curbuf->b_p_mp;
4907 }
4908
4909 p = skipwhite(p);
4910
4911 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4912 {
4913 /* replace $* by given arguments */
4914 i = 1;
4915 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4916 ++i;
4917 len = (int)STRLEN(p);
4918 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4919 if (new_cmdline == NULL)
4920 return NULL; /* out of memory */
4921 ptr = new_cmdline;
4922 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4923 {
4924 i = (int)(pos - program);
4925 STRNCPY(ptr, program, i);
4926 STRCPY(ptr += i, p);
4927 ptr += len;
4928 program = pos + 2;
4929 }
4930 STRCPY(ptr, program);
4931 }
4932 else
4933 {
4934 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4935 if (new_cmdline == NULL)
4936 return NULL; /* out of memory */
4937 STRCPY(new_cmdline, program);
4938 STRCAT(new_cmdline, " ");
4939 STRCAT(new_cmdline, p);
4940 }
4941 msg_make(p);
4942
4943 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4944 vim_free(*cmdlinep);
4945 *cmdlinep = new_cmdline;
4946 p = new_cmdline;
4947 }
4948 return p;
4949}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004950#endif
4951
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952/*
4953 * Expand file name in Ex command argument.
4954 * Return FAIL for failure, OK otherwise.
4955 */
4956 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004957expand_filename(
4958 exarg_T *eap,
4959 char_u **cmdlinep,
4960 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
4962 int has_wildcards; /* need to expand wildcards */
4963 char_u *repl;
4964 int srclen;
4965 char_u *p;
4966 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004967 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
Bram Moolenaar748bf032005-02-02 23:04:36 +00004969#ifdef FEAT_QUICKFIX
4970 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4971 p = skip_grep_pat(eap);
4972#else
4973 p = eap->arg;
4974#endif
4975
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 /*
4977 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4978 * the file name contains a wildcard it should not cause expanding.
4979 * (it will be expanded anyway if there is a wildcard before replacing).
4980 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004981 has_wildcards = mch_has_wildcard(p);
4982 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004984#ifdef FEAT_EVAL
4985 /* Skip over `=expr`, wildcards in it are not expanded. */
4986 if (p[0] == '`' && p[1] == '=')
4987 {
4988 p += 2;
4989 (void)skip_expr(&p);
4990 if (*p == '`')
4991 ++p;
4992 continue;
4993 }
4994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 /*
4996 * Quick check if this cannot be the start of a special string.
4997 * Also removes backslash before '%', '#' and '<'.
4998 */
4999 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5000 {
5001 ++p;
5002 continue;
5003 }
5004
5005 /*
5006 * Try to find a match at this position.
5007 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005008 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5009 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 if (*errormsgp != NULL) /* error detected */
5011 return FAIL;
5012 if (repl == NULL) /* no match found */
5013 {
5014 p += srclen;
5015 continue;
5016 }
5017
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005018 /* Wildcards won't be expanded below, the replacement is taken
5019 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5020 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5021 {
5022 char_u *l = repl;
5023
5024 repl = expand_env_save(repl);
5025 vim_free(l);
5026 }
5027
Bram Moolenaar63b92542007-03-27 14:57:09 +00005028 /* Need to escape white space et al. with a backslash.
5029 * Don't do this for:
5030 * - replacement that already has been escaped: "##"
5031 * - shell commands (may have to use quotes instead).
5032 * - non-unix systems when there is a single argument (spaces don't
5033 * separate arguments then).
5034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005036 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 && eap->cmdidx != CMD_grep
5039 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005040 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005041 && eap->cmdidx != CMD_lgrep
5042 && eap->cmdidx != CMD_lgrepadd
5043 && eap->cmdidx != CMD_lmake
5044 && eap->cmdidx != CMD_make
5045 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046#ifndef UNIX
5047 && !(eap->argt & NOSPC)
5048#endif
5049 )
5050 {
5051 char_u *l;
5052#ifdef BACKSLASH_IN_FILENAME
5053 /* Don't escape a backslash here, because rem_backslash() doesn't
5054 * remove it later. */
5055 static char_u *nobslash = (char_u *)" \t\"|";
5056# define ESCAPE_CHARS nobslash
5057#else
5058# define ESCAPE_CHARS escape_chars
5059#endif
5060
5061 for (l = repl; *l; ++l)
5062 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5063 {
5064 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5065 if (l != NULL)
5066 {
5067 vim_free(repl);
5068 repl = l;
5069 }
5070 break;
5071 }
5072 }
5073
5074 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005075 if ((eap->usefilter || eap->cmdidx == CMD_bang
5076 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005077 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
5079 char_u *l;
5080
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005081 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 if (l != NULL)
5083 {
5084 vim_free(repl);
5085 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
5087 }
5088
5089 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5090 vim_free(repl);
5091 if (p == NULL)
5092 return FAIL;
5093 }
5094
5095 /*
5096 * One file argument: Expand wildcards.
5097 * Don't do this with ":r !command" or ":w !command".
5098 */
5099 if ((eap->argt & NOSPC) && !eap->usefilter)
5100 {
5101 /*
5102 * May do this twice:
5103 * 1. Replace environment variables.
5104 * 2. Replace any other wildcards, remove backslashes.
5105 */
5106 for (n = 1; n <= 2; ++n)
5107 {
5108 if (n == 2)
5109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 /*
5111 * Halve the number of backslashes (this is Vi compatible).
5112 * For Unix and OS/2, when wildcards are expanded, this is
5113 * done by ExpandOne() below.
5114 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005115#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 if (!has_wildcards)
5117#endif
5118 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120
5121 if (has_wildcards)
5122 {
5123 if (n == 1)
5124 {
5125 /*
5126 * First loop: May expand environment variables. This
5127 * can be done much faster with expand_env() than with
5128 * something else (e.g., calling a shell).
5129 * After expanding environment variables, check again
5130 * if there are still wildcards present.
5131 */
5132 if (vim_strchr(eap->arg, '$') != NULL
5133 || vim_strchr(eap->arg, '~') != NULL)
5134 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005135 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005136 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 has_wildcards = mch_has_wildcard(NameBuff);
5138 p = NameBuff;
5139 }
5140 else
5141 p = NULL;
5142 }
5143 else /* n == 2 */
5144 {
5145 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005146 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
5148 ExpandInit(&xpc);
5149 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005150 if (p_wic)
5151 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005153 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 if (p == NULL)
5155 return FAIL;
5156 }
5157 if (p != NULL)
5158 {
5159 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5160 p, cmdlinep);
5161 if (n == 2) /* p came from ExpandOne() */
5162 vim_free(p);
5163 }
5164 }
5165 }
5166 }
5167 return OK;
5168}
5169
5170/*
5171 * Replace part of the command line, keeping eap->cmd, eap->arg and
5172 * eap->nextcmd correct.
5173 * "src" points to the part that is to be replaced, of length "srclen".
5174 * "repl" is the replacement string.
5175 * Returns a pointer to the character after the replaced string.
5176 * Returns NULL for failure.
5177 */
5178 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005179repl_cmdline(
5180 exarg_T *eap,
5181 char_u *src,
5182 int srclen,
5183 char_u *repl,
5184 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185{
5186 int len;
5187 int i;
5188 char_u *new_cmdline;
5189
5190 /*
5191 * The new command line is build in new_cmdline[].
5192 * First allocate it.
5193 * Careful: a "+cmd" argument may have been NUL terminated.
5194 */
5195 len = (int)STRLEN(repl);
5196 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005197 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5199 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5200 return NULL; /* out of memory! */
5201
5202 /*
5203 * Copy the stuff before the expanded part.
5204 * Copy the expanded stuff.
5205 * Copy what came after the expanded part.
5206 * Copy the next commands, if there are any.
5207 */
5208 i = (int)(src - *cmdlinep); /* length of part before match */
5209 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005210
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 mch_memmove(new_cmdline + i, repl, (size_t)len);
5212 i += len; /* remember the end of the string */
5213 STRCPY(new_cmdline + i, src + srclen);
5214 src = new_cmdline + i; /* remember where to continue */
5215
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005216 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
5218 i = (int)STRLEN(new_cmdline) + 1;
5219 STRCPY(new_cmdline + i, eap->nextcmd);
5220 eap->nextcmd = new_cmdline + i;
5221 }
5222 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5223 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5224 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5225 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5226 vim_free(*cmdlinep);
5227 *cmdlinep = new_cmdline;
5228
5229 return src;
5230}
5231
5232/*
5233 * Check for '|' to separate commands and '"' to start comments.
5234 */
5235 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005236separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237{
5238 char_u *p;
5239
Bram Moolenaar86b68352004-12-27 21:59:20 +00005240#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005241 p = skip_grep_pat(eap);
5242#else
5243 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005244#endif
5245
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005246 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 {
5248 if (*p == Ctrl_V)
5249 {
5250 if (eap->argt & (USECTRLV | XFILE))
5251 ++p; /* skip CTRL-V and next char */
5252 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005253 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005254 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 if (*p == NUL) /* stop at NUL after CTRL-V */
5256 break;
5257 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005258
5259#ifdef FEAT_EVAL
5260 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005261 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005262 {
5263 p += 2;
5264 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005265 }
5266#endif
5267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 /* Check for '"': start of comment or '|': next command */
5269 /* :@" and :*" do not start a comment!
5270 * :redir @" doesn't either. */
5271 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5272 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5273 || p != eap->arg)
5274 && (eap->cmdidx != CMD_redir
5275 || p != eap->arg + 1 || p[-1] != '@'))
5276 || *p == '|' || *p == '\n')
5277 {
5278 /*
5279 * We remove the '\' before the '|', unless USECTRLV is used
5280 * AND 'b' is present in 'cpoptions'.
5281 */
5282 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5283 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5284 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005285 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 --p;
5287 }
5288 else
5289 {
5290 eap->nextcmd = check_nextcmd(p);
5291 *p = NUL;
5292 break;
5293 }
5294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5298 del_trailing_spaces(eap->arg);
5299}
5300
5301/*
5302 * get + command from ex argument
5303 */
5304 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005305getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306{
5307 char_u *arg = *argp;
5308 char_u *command = NULL;
5309
5310 if (*arg == '+') /* +[command] */
5311 {
5312 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005313 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 command = dollar_command;
5315 else
5316 {
5317 command = arg;
5318 arg = skip_cmd_arg(command, TRUE);
5319 if (*arg != NUL)
5320 *arg++ = NUL; /* terminate command with NUL */
5321 }
5322
5323 arg = skipwhite(arg); /* skip over spaces */
5324 *argp = arg;
5325 }
5326 return command;
5327}
5328
5329/*
5330 * Find end of "+command" argument. Skip over "\ " and "\\".
5331 */
5332 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005333skip_cmd_arg(
5334 char_u *p,
5335 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
5337 while (*p && !vim_isspace(*p))
5338 {
5339 if (*p == '\\' && p[1] != NUL)
5340 {
5341 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005342 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 else
5344 ++p;
5345 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005346 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
5348 return p;
5349}
5350
Bram Moolenaar86676c92018-04-05 18:56:48 +02005351#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005352 int
5353get_bad_opt(char_u *p, exarg_T *eap)
5354{
5355 if (STRICMP(p, "keep") == 0)
5356 eap->bad_char = BAD_KEEP;
5357 else if (STRICMP(p, "drop") == 0)
5358 eap->bad_char = BAD_DROP;
5359 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5360 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005361 else
5362 return FAIL;
5363 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005364}
Bram Moolenaar86676c92018-04-05 18:56:48 +02005365#endif
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005366
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367/*
5368 * Get "++opt=arg" argument.
5369 * Return FAIL or OK.
5370 */
5371 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005372getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373{
5374 char_u *arg = eap->arg + 2;
5375 int *pp = NULL;
5376#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005377 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 char_u *p;
5379#endif
5380
5381 /* ":edit ++[no]bin[ary] file" */
5382 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5383 {
5384 if (*arg == 'n')
5385 {
5386 arg += 2;
5387 eap->force_bin = FORCE_NOBIN;
5388 }
5389 else
5390 eap->force_bin = FORCE_BIN;
5391 if (!checkforcmd(&arg, "binary", 3))
5392 return FAIL;
5393 eap->arg = skipwhite(arg);
5394 return OK;
5395 }
5396
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005397 /* ":read ++edit file" */
5398 if (STRNCMP(arg, "edit", 4) == 0)
5399 {
5400 eap->read_edit = TRUE;
5401 eap->arg = skipwhite(arg + 4);
5402 return OK;
5403 }
5404
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 if (STRNCMP(arg, "ff", 2) == 0)
5406 {
5407 arg += 2;
5408 pp = &eap->force_ff;
5409 }
5410 else if (STRNCMP(arg, "fileformat", 10) == 0)
5411 {
5412 arg += 10;
5413 pp = &eap->force_ff;
5414 }
5415#ifdef FEAT_MBYTE
5416 else if (STRNCMP(arg, "enc", 3) == 0)
5417 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005418 if (STRNCMP(arg, "encoding", 8) == 0)
5419 arg += 8;
5420 else
5421 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 pp = &eap->force_enc;
5423 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005424 else if (STRNCMP(arg, "bad", 3) == 0)
5425 {
5426 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005427 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429#endif
5430
5431 if (pp == NULL || *arg != '=')
5432 return FAIL;
5433
5434 ++arg;
5435 *pp = (int)(arg - eap->cmd);
5436 arg = skip_cmd_arg(arg, FALSE);
5437 eap->arg = skipwhite(arg);
5438 *arg = NUL;
5439
5440#ifdef FEAT_MBYTE
5441 if (pp == &eap->force_ff)
5442 {
5443#endif
5444 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5445 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005446 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447#ifdef FEAT_MBYTE
5448 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005449 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
5451 /* Make 'fileencoding' lower case. */
5452 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5453 *p = TOLOWER_ASC(*p);
5454 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005455 else
5456 {
5457 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5458 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005459 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005460 return FAIL;
5461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462#endif
5463
5464 return OK;
5465}
5466
5467/*
5468 * ":abbreviate" and friends.
5469 */
5470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005471ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472{
5473 do_exmap(eap, TRUE); /* almost the same as mapping */
5474}
5475
5476/*
5477 * ":map" and friends.
5478 */
5479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005480ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481{
5482 /*
5483 * If we are sourcing .exrc or .vimrc in current directory we
5484 * print the mappings for security reasons.
5485 */
5486 if (secure)
5487 {
5488 secure = 2;
5489 msg_outtrans(eap->cmd);
5490 msg_putchar('\n');
5491 }
5492 do_exmap(eap, FALSE);
5493}
5494
5495/*
5496 * ":unmap" and friends.
5497 */
5498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005499ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500{
5501 do_exmap(eap, FALSE);
5502}
5503
5504/*
5505 * ":mapclear" and friends.
5506 */
5507 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005508ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509{
5510 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5511}
5512
5513/*
5514 * ":abclear" and friends.
5515 */
5516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005517ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
5519 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5520}
5521
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005523ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524{
5525 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005526 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 * directory for security reasons.
5528 */
5529 if (secure)
5530 {
5531 secure = 2;
5532 eap->errmsg = e_curdir;
5533 }
5534 else if (eap->cmdidx == CMD_autocmd)
5535 do_autocmd(eap->arg, eap->forceit);
5536 else
5537 do_augroup(eap->arg, eap->forceit);
5538}
5539
5540/*
5541 * ":doautocmd": Apply the automatic commands to the current buffer.
5542 */
5543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005544ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005546 char_u *arg = eap->arg;
5547 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005548 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005549
Bram Moolenaar1610d052016-06-09 22:53:01 +02005550 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5551 /* Only when there is no <nomodeline>. */
5552 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005553 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556/*
5557 * :[N]bunload[!] [N] [bufname] unload buffer
5558 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5559 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5560 */
5561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005562ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 eap->errmsg = do_bufdel(
5565 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5566 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5567 : DOBUF_UNLOAD, eap->arg,
5568 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5569}
5570
5571/*
5572 * :[N]buffer [N] to buffer N
5573 * :[N]sbuffer [N] to buffer N
5574 */
5575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005576ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 if (*eap->arg)
5579 eap->errmsg = e_trailing;
5580 else
5581 {
5582 if (eap->addr_count == 0) /* default is current buffer */
5583 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5584 else
5585 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005586 if (eap->do_ecmd_cmd != NULL)
5587 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 }
5589}
5590
5591/*
5592 * :[N]bmodified [N] to next mod. buffer
5593 * :[N]sbmodified [N] to next mod. buffer
5594 */
5595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005596ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597{
5598 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005599 if (eap->do_ecmd_cmd != NULL)
5600 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601}
5602
5603/*
5604 * :[N]bnext [N] to next buffer
5605 * :[N]sbnext [N] split and to next buffer
5606 */
5607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005608ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609{
5610 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005611 if (eap->do_ecmd_cmd != NULL)
5612 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613}
5614
5615/*
5616 * :[N]bNext [N] to previous buffer
5617 * :[N]bprevious [N] to previous buffer
5618 * :[N]sbNext [N] split and to previous buffer
5619 * :[N]sbprevious [N] split and to previous buffer
5620 */
5621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005622ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
5624 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005625 if (eap->do_ecmd_cmd != NULL)
5626 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627}
5628
5629/*
5630 * :brewind to first buffer
5631 * :bfirst to first buffer
5632 * :sbrewind split and to first buffer
5633 * :sbfirst split and to first buffer
5634 */
5635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005636ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005639 if (eap->do_ecmd_cmd != NULL)
5640 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641}
5642
5643/*
5644 * :blast to last buffer
5645 * :sblast split and to last buffer
5646 */
5647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005648ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
5650 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005651 if (eap->do_ecmd_cmd != NULL)
5652 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
5655 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005656ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657{
5658 return (c == NUL || c == '|' || c == '"' || c == '\n');
5659}
5660
5661#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5662 || defined(PROTO)
5663/*
5664 * Return the next command, after the first '|' or '\n'.
5665 * Return NULL if not found.
5666 */
5667 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005668find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669{
5670 while (*p != '|' && *p != '\n')
5671 {
5672 if (*p == NUL)
5673 return NULL;
5674 ++p;
5675 }
5676 return (p + 1);
5677}
5678#endif
5679
5680/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005681 * Check if *p is a separator between Ex commands, skipping over white space.
5682 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 */
5684 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005685check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005687 char_u *s = skipwhite(p);
5688
5689 if (*s == '|' || *s == '\n')
5690 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 else
5692 return NULL;
5693}
5694
5695/*
5696 * - if there are more files to edit
5697 * - and this is the last window
5698 * - and forceit not used
5699 * - and not repeated twice on a row
5700 * return FAIL and give error message if 'message' TRUE
5701 * return OK otherwise
5702 */
5703 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005704check_more(
5705 int message, /* when FALSE check only, no messages */
5706 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5709
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005710 if (!forceit && only_one_window()
5711 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
5713 if (message)
5714 {
5715#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5716 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5717 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005718 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
5720 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005721 vim_strncpy(buff,
5722 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005723 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005725 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 _("%d more files to edit. Quit anyway?"), n);
5727 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5728 return OK;
5729 return FAIL;
5730 }
5731#endif
5732 if (n == 1)
5733 EMSG(_("E173: 1 more file to edit"));
5734 else
5735 EMSGN(_("E173: %ld more files to edit"), n);
5736 quitmore = 2; /* next try to quit is allowed */
5737 }
5738 return FAIL;
5739 }
5740 return OK;
5741}
5742
5743#ifdef FEAT_CMDL_COMPL
5744/*
5745 * Function given to ExpandGeneric() to obtain the list of command names.
5746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005748get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
5750 if (idx >= (int)CMD_SIZE)
5751# ifdef FEAT_USR_CMDS
5752 return get_user_command_name(idx);
5753# else
5754 return NULL;
5755# endif
5756 return cmdnames[idx].cmd_name;
5757}
5758#endif
5759
5760#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005761static 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);
5762static void uc_list(char_u *name, size_t name_len);
5763static 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);
5764static char_u *uc_split_args(char_u *arg, size_t *lenp);
5765static 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 +00005766
5767 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005768uc_add_command(
5769 char_u *name,
5770 size_t name_len,
5771 char_u *rep,
5772 long argt,
5773 long def,
5774 int flags,
5775 int compl,
5776 char_u *compl_arg,
5777 int addr_type,
5778 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779{
5780 ucmd_T *cmd = NULL;
5781 char_u *p;
5782 int i;
5783 int cmp = 1;
5784 char_u *rep_buf = NULL;
5785 garray_T *gap;
5786
Bram Moolenaar9c102382006-05-03 21:26:49 +00005787 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 if (rep_buf == NULL)
5789 {
5790 /* Can't replace termcodes - try using the string as is */
5791 rep_buf = vim_strsave(rep);
5792
5793 /* Give up if out of memory */
5794 if (rep_buf == NULL)
5795 return FAIL;
5796 }
5797
5798 /* get address of growarray: global or in curbuf */
5799 if (flags & UC_BUFFER)
5800 {
5801 gap = &curbuf->b_ucmds;
5802 if (gap->ga_itemsize == 0)
5803 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5804 }
5805 else
5806 gap = &ucmds;
5807
5808 /* Search for the command in the already defined commands. */
5809 for (i = 0; i < gap->ga_len; ++i)
5810 {
5811 size_t len;
5812
5813 cmd = USER_CMD_GA(gap, i);
5814 len = STRLEN(cmd->uc_name);
5815 cmp = STRNCMP(name, cmd->uc_name, name_len);
5816 if (cmp == 0)
5817 {
5818 if (name_len < len)
5819 cmp = -1;
5820 else if (name_len > len)
5821 cmp = 1;
5822 }
5823
5824 if (cmp == 0)
5825 {
5826 if (!force)
5827 {
5828 EMSG(_("E174: Command already exists: add ! to replace it"));
5829 goto fail;
5830 }
5831
Bram Moolenaard23a8232018-02-10 18:45:26 +01005832 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005833#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005834 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 break;
5837 }
5838
5839 /* Stop as soon as we pass the name to add */
5840 if (cmp < 0)
5841 break;
5842 }
5843
5844 /* Extend the array unless we're replacing an existing command */
5845 if (cmp != 0)
5846 {
5847 if (ga_grow(gap, 1) != OK)
5848 goto fail;
5849 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5850 goto fail;
5851
5852 cmd = USER_CMD_GA(gap, i);
5853 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5854
5855 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856
5857 cmd->uc_name = p;
5858 }
5859
5860 cmd->uc_rep = rep_buf;
5861 cmd->uc_argt = argt;
5862 cmd->uc_def = def;
5863 cmd->uc_compl = compl;
5864#ifdef FEAT_EVAL
5865 cmd->uc_scriptID = current_SID;
5866# ifdef FEAT_CMDL_COMPL
5867 cmd->uc_compl_arg = compl_arg;
5868# endif
5869#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005870 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871
5872 return OK;
5873
5874fail:
5875 vim_free(rep_buf);
5876#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5877 vim_free(compl_arg);
5878#endif
5879 return FAIL;
5880}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005883#if defined(FEAT_USR_CMDS)
5884static struct
5885{
5886 int expand;
5887 char *name;
5888} addr_type_complete[] =
5889{
5890 {ADDR_ARGUMENTS, "arguments"},
5891 {ADDR_LINES, "lines"},
5892 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5893 {ADDR_TABS, "tabs"},
5894 {ADDR_BUFFERS, "buffers"},
5895 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005896 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005897 {-1, NULL}
5898};
5899#endif
5900
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005901#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902/*
5903 * List of names for completion for ":command" with the EXPAND_ flag.
5904 * Must be alphabetical for completion.
5905 */
5906static struct
5907{
5908 int expand;
5909 char *name;
5910} command_complete[] =
5911{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005912 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005914 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005916 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005918 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005919#if defined(FEAT_CSCOPE)
5920 {EXPAND_CSCOPE, "cscope"},
5921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5923 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005924 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925#endif
5926 {EXPAND_DIRECTORIES, "dir"},
5927 {EXPAND_ENV_VARS, "environment"},
5928 {EXPAND_EVENTS, "event"},
5929 {EXPAND_EXPRESSION, "expression"},
5930 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005931 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005932 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 {EXPAND_FUNCTIONS, "function"},
5934 {EXPAND_HELP, "help"},
5935 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005936#if defined(FEAT_CMDHIST)
5937 {EXPAND_HISTORY, "history"},
5938#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005939#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005940 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005941 {EXPAND_LOCALES, "locale"},
5942#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005943 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 {EXPAND_MAPPINGS, "mapping"},
5945 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005946 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005947 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005948#if defined(FEAT_PROFILE)
5949 {EXPAND_SYNTIME, "syntime"},
5950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005952 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005953 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005954#if defined(FEAT_SIGNS)
5955 {EXPAND_SIGN, "sign"},
5956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 {EXPAND_TAGS, "tag"},
5958 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005959 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 {EXPAND_USER_VARS, "var"},
5961 {0, NULL}
5962};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005965#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005967uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968{
5969 int i, j;
5970 int found = FALSE;
5971 ucmd_T *cmd;
5972 int len;
5973 long a;
5974 garray_T *gap;
5975
5976 gap = &curbuf->b_ucmds;
5977 for (;;)
5978 {
5979 for (i = 0; i < gap->ga_len; ++i)
5980 {
5981 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005982 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983
Bram Moolenaard29459b2016-08-26 22:29:11 +02005984 /* Skip commands which don't match the requested prefix and
5985 * commands filtered out. */
5986 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5987 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 continue;
5989
5990 /* Put out the title first time */
5991 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005992 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 found = TRUE;
5994 msg_putchar('\n');
5995 if (got_int)
5996 break;
5997
5998 /* Special cases */
5999 msg_putchar(a & BANG ? '!' : ' ');
6000 msg_putchar(a & REGSTR ? '"' : ' ');
6001 msg_putchar(gap != &ucmds ? 'b' : ' ');
6002 msg_putchar(' ');
6003
Bram Moolenaar8820b482017-03-16 17:23:31 +01006004 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 len = (int)STRLEN(cmd->uc_name) + 4;
6006
6007 do {
6008 msg_putchar(' ');
6009 ++len;
6010 } while (len < 16);
6011
6012 len = 0;
6013
6014 /* Arguments */
6015 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6016 {
6017 case 0: IObuff[len++] = '0'; break;
6018 case (EXTRA): IObuff[len++] = '*'; break;
6019 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6020 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6021 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6022 }
6023
6024 do {
6025 IObuff[len++] = ' ';
6026 } while (len < 5);
6027
6028 /* Range */
6029 if (a & (RANGE|COUNT))
6030 {
6031 if (a & COUNT)
6032 {
6033 /* -count=N */
6034 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6035 len += (int)STRLEN(IObuff + len);
6036 }
6037 else if (a & DFLALL)
6038 IObuff[len++] = '%';
6039 else if (cmd->uc_def >= 0)
6040 {
6041 /* -range=N */
6042 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6043 len += (int)STRLEN(IObuff + len);
6044 }
6045 else
6046 IObuff[len++] = '.';
6047 }
6048
6049 do {
6050 IObuff[len++] = ' ';
6051 } while (len < 11);
6052
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006053 /* Address Type */
6054 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6055 if (addr_type_complete[j].expand != ADDR_LINES
6056 && addr_type_complete[j].expand == cmd->uc_addr_type)
6057 {
6058 STRCPY(IObuff + len, addr_type_complete[j].name);
6059 len += (int)STRLEN(IObuff + len);
6060 break;
6061 }
6062
6063 do {
6064 IObuff[len++] = ' ';
6065 } while (len < 21);
6066
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 /* Completion */
6068 for (j = 0; command_complete[j].expand != 0; ++j)
6069 if (command_complete[j].expand == cmd->uc_compl)
6070 {
6071 STRCPY(IObuff + len, command_complete[j].name);
6072 len += (int)STRLEN(IObuff + len);
6073 break;
6074 }
6075
6076 do {
6077 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006078 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079
6080 IObuff[len] = '\0';
6081 msg_outtrans(IObuff);
6082
6083 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006084#ifdef FEAT_EVAL
6085 if (p_verbose > 0)
6086 last_set_msg(cmd->uc_scriptID);
6087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 out_flush();
6089 ui_breakcheck();
6090 if (got_int)
6091 break;
6092 }
6093 if (gap == &ucmds || i < gap->ga_len)
6094 break;
6095 gap = &ucmds;
6096 }
6097
6098 if (!found)
6099 MSG(_("No user-defined commands found"));
6100}
6101
6102 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006103uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
6105 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6106 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6107 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6108 0xb9, 0x7f, 0};
6109 int i;
6110
6111 for (i = 0; fcmd[i]; ++i)
6112 IObuff[i] = fcmd[i] - 0x40;
6113 IObuff[i] = 0;
6114 return IObuff;
6115}
6116
6117 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006118uc_scan_attr(
6119 char_u *attr,
6120 size_t len,
6121 long *argt,
6122 long *def,
6123 int *flags,
6124 int *compl,
6125 char_u **compl_arg,
6126 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127{
6128 char_u *p;
6129
6130 if (len == 0)
6131 {
6132 EMSG(_("E175: No attribute specified"));
6133 return FAIL;
6134 }
6135
6136 /* First, try the simple attributes (no arguments) */
6137 if (STRNICMP(attr, "bang", len) == 0)
6138 *argt |= BANG;
6139 else if (STRNICMP(attr, "buffer", len) == 0)
6140 *flags |= UC_BUFFER;
6141 else if (STRNICMP(attr, "register", len) == 0)
6142 *argt |= REGSTR;
6143 else if (STRNICMP(attr, "bar", len) == 0)
6144 *argt |= TRLBAR;
6145 else
6146 {
6147 int i;
6148 char_u *val = NULL;
6149 size_t vallen = 0;
6150 size_t attrlen = len;
6151
6152 /* Look for the attribute name - which is the part before any '=' */
6153 for (i = 0; i < (int)len; ++i)
6154 {
6155 if (attr[i] == '=')
6156 {
6157 val = &attr[i + 1];
6158 vallen = len - i - 1;
6159 attrlen = i;
6160 break;
6161 }
6162 }
6163
6164 if (STRNICMP(attr, "nargs", attrlen) == 0)
6165 {
6166 if (vallen == 1)
6167 {
6168 if (*val == '0')
6169 /* Do nothing - this is the default */;
6170 else if (*val == '1')
6171 *argt |= (EXTRA | NOSPC | NEEDARG);
6172 else if (*val == '*')
6173 *argt |= EXTRA;
6174 else if (*val == '?')
6175 *argt |= (EXTRA | NOSPC);
6176 else if (*val == '+')
6177 *argt |= (EXTRA | NEEDARG);
6178 else
6179 goto wrong_nargs;
6180 }
6181 else
6182 {
6183wrong_nargs:
6184 EMSG(_("E176: Invalid number of arguments"));
6185 return FAIL;
6186 }
6187 }
6188 else if (STRNICMP(attr, "range", attrlen) == 0)
6189 {
6190 *argt |= RANGE;
6191 if (vallen == 1 && *val == '%')
6192 *argt |= DFLALL;
6193 else if (val != NULL)
6194 {
6195 p = val;
6196 if (*def >= 0)
6197 {
6198two_count:
6199 EMSG(_("E177: Count cannot be specified twice"));
6200 return FAIL;
6201 }
6202
6203 *def = getdigits(&p);
6204 *argt |= (ZEROR | NOTADR);
6205
6206 if (p != val + vallen || vallen == 0)
6207 {
6208invalid_count:
6209 EMSG(_("E178: Invalid default value for count"));
6210 return FAIL;
6211 }
6212 }
6213 }
6214 else if (STRNICMP(attr, "count", attrlen) == 0)
6215 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006216 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
6218 if (val != NULL)
6219 {
6220 p = val;
6221 if (*def >= 0)
6222 goto two_count;
6223
6224 *def = getdigits(&p);
6225
6226 if (p != val + vallen)
6227 goto invalid_count;
6228 }
6229
6230 if (*def < 0)
6231 *def = 0;
6232 }
6233 else if (STRNICMP(attr, "complete", attrlen) == 0)
6234 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 if (val == NULL)
6236 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006237 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 return FAIL;
6239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006241 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6242 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006245 else if (STRNICMP(attr, "addr", attrlen) == 0)
6246 {
6247 *argt |= RANGE;
6248 if (val == NULL)
6249 {
6250 EMSG(_("E179: argument required for -addr"));
6251 return FAIL;
6252 }
6253 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6254 == FAIL)
6255 return FAIL;
6256 if (addr_type_arg != ADDR_LINES)
6257 *argt |= (ZEROR | NOTADR) ;
6258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 else
6260 {
6261 char_u ch = attr[len];
6262 attr[len] = '\0';
6263 EMSG2(_("E181: Invalid attribute: %s"), attr);
6264 attr[len] = ch;
6265 return FAIL;
6266 }
6267 }
6268
6269 return OK;
6270}
6271
Bram Moolenaara850a712009-01-28 14:42:59 +00006272/*
6273 * ":command ..."
6274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006276ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277{
6278 char_u *name;
6279 char_u *end;
6280 char_u *p;
6281 long argt = 0;
6282 long def = -1;
6283 int flags = 0;
6284 int compl = EXPAND_NOTHING;
6285 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006286 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006288 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289
6290 p = eap->arg;
6291
6292 /* Check for attributes */
6293 while (*p == '-')
6294 {
6295 ++p;
6296 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006297 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6298 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 == FAIL)
6300 return;
6301 p = skipwhite(end);
6302 }
6303
6304 /* Get the name (if any) and skip to the following argument */
6305 name = p;
6306 if (ASCII_ISALPHA(*p))
6307 while (ASCII_ISALNUM(*p))
6308 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006309 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 {
6311 EMSG(_("E182: Invalid command name"));
6312 return;
6313 }
6314 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006315 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316
6317 /* If there is nothing after the name, and no attributes were specified,
6318 * we are listing commands
6319 */
6320 p = skipwhite(end);
6321 if (!has_attr && ends_excmd(*p))
6322 {
6323 uc_list(name, end - name);
6324 }
6325 else if (!ASCII_ISUPPER(*name))
6326 {
6327 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6328 return;
6329 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006330 else if ((name_len == 1 && *name == 'X')
6331 || (name_len <= 4
6332 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6333 {
6334 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6335 return;
6336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 else
6338 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006339 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340}
6341
6342/*
6343 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 * Clear all user commands, global and for current buffer.
6345 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006346 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006347ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348{
6349 uc_clear(&ucmds);
6350 uc_clear(&curbuf->b_ucmds);
6351}
6352
6353/*
6354 * Clear all user commands for "gap".
6355 */
6356 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006357uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 int i;
6360 ucmd_T *cmd;
6361
6362 for (i = 0; i < gap->ga_len; ++i)
6363 {
6364 cmd = USER_CMD_GA(gap, i);
6365 vim_free(cmd->uc_name);
6366 vim_free(cmd->uc_rep);
6367# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6368 vim_free(cmd->uc_compl_arg);
6369# endif
6370 }
6371 ga_clear(gap);
6372}
6373
6374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006375ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376{
6377 int i = 0;
6378 ucmd_T *cmd = NULL;
6379 int cmp = -1;
6380 garray_T *gap;
6381
6382 gap = &curbuf->b_ucmds;
6383 for (;;)
6384 {
6385 for (i = 0; i < gap->ga_len; ++i)
6386 {
6387 cmd = USER_CMD_GA(gap, i);
6388 cmp = STRCMP(eap->arg, cmd->uc_name);
6389 if (cmp <= 0)
6390 break;
6391 }
6392 if (gap == &ucmds || cmp == 0)
6393 break;
6394 gap = &ucmds;
6395 }
6396
6397 if (cmp != 0)
6398 {
6399 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6400 return;
6401 }
6402
6403 vim_free(cmd->uc_name);
6404 vim_free(cmd->uc_rep);
6405# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6406 vim_free(cmd->uc_compl_arg);
6407# endif
6408
6409 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410
6411 if (i < gap->ga_len)
6412 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6413}
6414
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006415/*
6416 * split and quote args for <f-args>
6417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006419uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420{
6421 char_u *buf;
6422 char_u *p;
6423 char_u *q;
6424 int len;
6425
6426 /* Precalculate length */
6427 p = arg;
6428 len = 2; /* Initial and final quotes */
6429
6430 while (*p)
6431 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006432 if (p[0] == '\\' && p[1] == '\\')
6433 {
6434 len += 2;
6435 p += 2;
6436 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006437 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 {
6439 len += 1;
6440 p += 2;
6441 }
6442 else if (*p == '\\' || *p == '"')
6443 {
6444 len += 2;
6445 p += 1;
6446 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006447 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 {
6449 p = skipwhite(p);
6450 if (*p == NUL)
6451 break;
6452 len += 3; /* "," */
6453 }
6454 else
6455 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006456#ifdef FEAT_MBYTE
6457 int charlen = (*mb_ptr2len)(p);
6458 len += charlen;
6459 p += charlen;
6460#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 ++len;
6462 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 }
6465 }
6466
6467 buf = alloc(len + 1);
6468 if (buf == NULL)
6469 {
6470 *lenp = 0;
6471 return buf;
6472 }
6473
6474 p = arg;
6475 q = buf;
6476 *q++ = '"';
6477 while (*p)
6478 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006479 if (p[0] == '\\' && p[1] == '\\')
6480 {
6481 *q++ = '\\';
6482 *q++ = '\\';
6483 p += 2;
6484 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006485 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 {
6487 *q++ = p[1];
6488 p += 2;
6489 }
6490 else if (*p == '\\' || *p == '"')
6491 {
6492 *q++ = '\\';
6493 *q++ = *p++;
6494 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006495 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 {
6497 p = skipwhite(p);
6498 if (*p == NUL)
6499 break;
6500 *q++ = '"';
6501 *q++ = ',';
6502 *q++ = '"';
6503 }
6504 else
6505 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006506 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 }
6508 }
6509 *q++ = '"';
6510 *q = 0;
6511
6512 *lenp = len;
6513 return buf;
6514}
6515
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006516 static size_t
6517add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6518{
6519 size_t result;
6520
6521 result = STRLEN(mod_str);
6522 if (*multi_mods)
6523 result += 1;
6524 if (buf != NULL)
6525 {
6526 if (*multi_mods)
6527 STRCAT(buf, " ");
6528 STRCAT(buf, mod_str);
6529 }
6530
6531 *multi_mods = 1;
6532
6533 return result;
6534}
6535
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536/*
6537 * Check for a <> code in a user command.
6538 * "code" points to the '<'. "len" the length of the <> (inclusive).
6539 * "buf" is where the result is to be added.
6540 * "split_buf" points to a buffer used for splitting, caller should free it.
6541 * "split_len" is the length of what "split_buf" contains.
6542 * Returns the length of the replacement, which has been added to "buf".
6543 * Returns -1 if there was no match, and only the "<" has been copied.
6544 */
6545 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006546uc_check_code(
6547 char_u *code,
6548 size_t len,
6549 char_u *buf,
6550 ucmd_T *cmd, /* the user command we're expanding */
6551 exarg_T *eap, /* ex arguments */
6552 char_u **split_buf,
6553 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
6555 size_t result = 0;
6556 char_u *p = code + 1;
6557 size_t l = len - 2;
6558 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006559 enum {
6560 ct_ARGS,
6561 ct_BANG,
6562 ct_COUNT,
6563 ct_LINE1,
6564 ct_LINE2,
6565 ct_RANGE,
6566 ct_MODS,
6567 ct_REGISTER,
6568 ct_LT,
6569 ct_NONE
6570 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
6572 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6573 {
6574 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6575 p += 2;
6576 l -= 2;
6577 }
6578
Bram Moolenaar371d5402006-03-20 21:47:49 +00006579 ++l;
6580 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006582 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006584 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006586 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006588 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006590 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006592 else if (STRNICMP(p, "range>", l) == 0)
6593 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006594 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006596 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006598 else if (STRNICMP(p, "mods>", l) == 0)
6599 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600
6601 switch (type)
6602 {
6603 case ct_ARGS:
6604 /* Simple case first */
6605 if (*eap->arg == NUL)
6606 {
6607 if (quote == 1)
6608 {
6609 result = 2;
6610 if (buf != NULL)
6611 STRCPY(buf, "''");
6612 }
6613 else
6614 result = 0;
6615 break;
6616 }
6617
6618 /* When specified there is a single argument don't split it.
6619 * Works for ":Cmd %" when % is "a b c". */
6620 if ((eap->argt & NOSPC) && quote == 2)
6621 quote = 1;
6622
6623 switch (quote)
6624 {
6625 case 0: /* No quoting, no splitting */
6626 result = STRLEN(eap->arg);
6627 if (buf != NULL)
6628 STRCPY(buf, eap->arg);
6629 break;
6630 case 1: /* Quote, but don't split */
6631 result = STRLEN(eap->arg) + 2;
6632 for (p = eap->arg; *p; ++p)
6633 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006634#ifdef FEAT_MBYTE
6635 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6636 /* DBCS can contain \ in a trail byte, skip the
6637 * double-byte character. */
6638 ++p;
6639 else
6640#endif
6641 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 ++result;
6643 }
6644
6645 if (buf != NULL)
6646 {
6647 *buf++ = '"';
6648 for (p = eap->arg; *p; ++p)
6649 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006650#ifdef FEAT_MBYTE
6651 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6652 /* DBCS can contain \ in a trail byte, copy the
6653 * double-byte character to avoid escaping. */
6654 *buf++ = *p++;
6655 else
6656#endif
6657 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 *buf++ = '\\';
6659 *buf++ = *p;
6660 }
6661 *buf = '"';
6662 }
6663
6664 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006665 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 /* This is hard, so only do it once, and cache the result */
6667 if (*split_buf == NULL)
6668 *split_buf = uc_split_args(eap->arg, split_len);
6669
6670 result = *split_len;
6671 if (buf != NULL && result != 0)
6672 STRCPY(buf, *split_buf);
6673
6674 break;
6675 }
6676 break;
6677
6678 case ct_BANG:
6679 result = eap->forceit ? 1 : 0;
6680 if (quote)
6681 result += 2;
6682 if (buf != NULL)
6683 {
6684 if (quote)
6685 *buf++ = '"';
6686 if (eap->forceit)
6687 *buf++ = '!';
6688 if (quote)
6689 *buf = '"';
6690 }
6691 break;
6692
6693 case ct_LINE1:
6694 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006695 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 case ct_COUNT:
6697 {
6698 char num_buf[20];
6699 long num = (type == ct_LINE1) ? eap->line1 :
6700 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006701 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6703 size_t num_len;
6704
6705 sprintf(num_buf, "%ld", num);
6706 num_len = STRLEN(num_buf);
6707 result = num_len;
6708
6709 if (quote)
6710 result += 2;
6711
6712 if (buf != NULL)
6713 {
6714 if (quote)
6715 *buf++ = '"';
6716 STRCPY(buf, num_buf);
6717 buf += num_len;
6718 if (quote)
6719 *buf = '"';
6720 }
6721
6722 break;
6723 }
6724
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006725 case ct_MODS:
6726 {
6727 int multi_mods = 0;
6728 typedef struct {
6729 int *varp;
6730 char *name;
6731 } mod_entry_T;
6732 static mod_entry_T mod_entries[] = {
6733#ifdef FEAT_BROWSE_CMD
6734 {&cmdmod.browse, "browse"},
6735#endif
6736#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6737 {&cmdmod.confirm, "confirm"},
6738#endif
6739 {&cmdmod.hide, "hide"},
6740 {&cmdmod.keepalt, "keepalt"},
6741 {&cmdmod.keepjumps, "keepjumps"},
6742 {&cmdmod.keepmarks, "keepmarks"},
6743 {&cmdmod.keeppatterns, "keeppatterns"},
6744 {&cmdmod.lockmarks, "lockmarks"},
6745 {&cmdmod.noswapfile, "noswapfile"},
6746 {NULL, NULL}
6747 };
6748 int i;
6749
6750 result = quote ? 2 : 0;
6751 if (buf != NULL)
6752 {
6753 if (quote)
6754 *buf++ = '"';
6755 *buf = '\0';
6756 }
6757
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006758 /* :aboveleft and :leftabove */
6759 if (cmdmod.split & WSP_ABOVE)
6760 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6761 /* :belowright and :rightbelow */
6762 if (cmdmod.split & WSP_BELOW)
6763 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6764 /* :botright */
6765 if (cmdmod.split & WSP_BOT)
6766 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006767
6768 /* the modifiers that are simple flags */
6769 for (i = 0; mod_entries[i].varp != NULL; ++i)
6770 if (*mod_entries[i].varp)
6771 result += add_cmd_modifier(buf, mod_entries[i].name,
6772 &multi_mods);
6773
6774 /* TODO: How to support :noautocmd? */
6775#ifdef HAVE_SANDBOX
6776 /* TODO: How to support :sandbox?*/
6777#endif
6778 /* :silent */
6779 if (msg_silent > 0)
6780 result += add_cmd_modifier(buf,
6781 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006782 /* :tab */
6783 if (cmdmod.tab > 0)
6784 result += add_cmd_modifier(buf, "tab", &multi_mods);
6785 /* :topleft */
6786 if (cmdmod.split & WSP_TOP)
6787 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006788 /* TODO: How to support :unsilent?*/
6789 /* :verbose */
6790 if (p_verbose > 0)
6791 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006792 /* :vertical */
6793 if (cmdmod.split & WSP_VERT)
6794 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006795 if (quote && buf != NULL)
6796 {
6797 buf += result - 2;
6798 *buf = '"';
6799 }
6800 break;
6801 }
6802
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 case ct_REGISTER:
6804 result = eap->regname ? 1 : 0;
6805 if (quote)
6806 result += 2;
6807 if (buf != NULL)
6808 {
6809 if (quote)
6810 *buf++ = '\'';
6811 if (eap->regname)
6812 *buf++ = eap->regname;
6813 if (quote)
6814 *buf = '\'';
6815 }
6816 break;
6817
6818 case ct_LT:
6819 result = 1;
6820 if (buf != NULL)
6821 *buf = '<';
6822 break;
6823
6824 default:
6825 /* Not recognized: just copy the '<' and return -1. */
6826 result = (size_t)-1;
6827 if (buf != NULL)
6828 *buf = '<';
6829 break;
6830 }
6831
6832 return result;
6833}
6834
6835 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006836do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837{
6838 char_u *buf;
6839 char_u *p;
6840 char_u *q;
6841
6842 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006843 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006844 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 size_t len, totlen;
6846
6847 size_t split_len = 0;
6848 char_u *split_buf = NULL;
6849 ucmd_T *cmd;
6850#ifdef FEAT_EVAL
6851 scid_T save_current_SID = current_SID;
6852#endif
6853
6854 if (eap->cmdidx == CMD_USER)
6855 cmd = USER_CMD(eap->useridx);
6856 else
6857 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6858
6859 /*
6860 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006861 * First round: "buf" is NULL, compute length, allocate "buf".
6862 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 */
6864 buf = NULL;
6865 for (;;)
6866 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006867 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006868 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006870
6871 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006873 start = vim_strchr(p, '<');
6874 if (start != NULL)
6875 end = vim_strchr(start + 1, '>');
6876 if (buf != NULL)
6877 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006878 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6879 ;
6880 if (*ksp == K_SPECIAL
6881 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006882 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6883# ifdef FEAT_GUI
6884 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6885# endif
6886 ))
6887 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006888 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006889 * KS_SPECIAL KE_FILLER, like for mappings, but
6890 * do_cmdline() doesn't handle that, so convert it back.
6891 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6892 len = ksp - p;
6893 if (len > 0)
6894 {
6895 mch_memmove(q, p, len);
6896 q += len;
6897 }
6898 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6899 p = ksp + 3;
6900 continue;
6901 }
6902 }
6903
6904 /* break if there no <item> is found */
6905 if (start == NULL || end == NULL)
6906 break;
6907
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 /* Include the '>' */
6909 ++end;
6910
6911 /* Take everything up to the '<' */
6912 len = start - p;
6913 if (buf == NULL)
6914 totlen += len;
6915 else
6916 {
6917 mch_memmove(q, p, len);
6918 q += len;
6919 }
6920
6921 len = uc_check_code(start, end - start, q, cmd, eap,
6922 &split_buf, &split_len);
6923 if (len == (size_t)-1)
6924 {
6925 /* no match, continue after '<' */
6926 p = start + 1;
6927 len = 1;
6928 }
6929 else
6930 p = end;
6931 if (buf == NULL)
6932 totlen += len;
6933 else
6934 q += len;
6935 }
6936 if (buf != NULL) /* second time here, finished */
6937 {
6938 STRCPY(q, p);
6939 break;
6940 }
6941
6942 totlen += STRLEN(p); /* Add on the trailing characters */
6943 buf = alloc((unsigned)(totlen + 1));
6944 if (buf == NULL)
6945 {
6946 vim_free(split_buf);
6947 return;
6948 }
6949 }
6950
6951#ifdef FEAT_EVAL
6952 current_SID = cmd->uc_scriptID;
6953#endif
6954 (void)do_cmdline(buf, eap->getline, eap->cookie,
6955 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6956#ifdef FEAT_EVAL
6957 current_SID = save_current_SID;
6958#endif
6959 vim_free(buf);
6960 vim_free(split_buf);
6961}
6962
6963# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6964 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006965get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966{
6967 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6968}
6969
6970/*
6971 * Function given to ExpandGeneric() to obtain the list of user command names.
6972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006974get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
6976 if (idx < curbuf->b_ucmds.ga_len)
6977 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6978 idx -= curbuf->b_ucmds.ga_len;
6979 if (idx < ucmds.ga_len)
6980 return USER_CMD(idx)->uc_name;
6981 return NULL;
6982}
6983
6984/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006985 * Function given to ExpandGeneric() to obtain the list of user address type names.
6986 */
6987 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006988get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006989{
6990 return (char_u *)addr_type_complete[idx].name;
6991}
6992
6993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 * Function given to ExpandGeneric() to obtain the list of user command
6995 * attributes.
6996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006998get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999{
7000 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007001 {"addr", "bang", "bar", "buffer", "complete",
7002 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007004 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 return NULL;
7006 return (char_u *)user_cmd_flags[idx];
7007}
7008
7009/*
7010 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007013get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
7015 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7016
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007017 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 return NULL;
7019 return (char_u *)user_cmd_nargs[idx];
7020}
7021
7022/*
7023 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007026get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027{
7028 return (char_u *)command_complete[idx].name;
7029}
7030# endif /* FEAT_CMDL_COMPL */
7031
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007032/*
7033 * Parse address type argument
7034 */
7035 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007036parse_addr_type_arg(
7037 char_u *value,
7038 int vallen,
7039 long *argt,
7040 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007041{
7042 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007043
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007044 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7045 {
7046 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7047 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7048 if (a && b)
7049 {
7050 *addr_type_arg = addr_type_complete[i].expand;
7051 break;
7052 }
7053 }
7054
7055 if (addr_type_complete[i].expand == -1)
7056 {
7057 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007058
Bram Moolenaar1c465442017-03-12 20:10:05 +01007059 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007060 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007061 err[i] = NUL;
7062 EMSG2(_("E180: Invalid address type value: %s"), err);
7063 return FAIL;
7064 }
7065
7066 if (*addr_type_arg != ADDR_LINES)
7067 *argt |= NOTADR;
7068
7069 return OK;
7070}
7071
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072#endif /* FEAT_USR_CMDS */
7073
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007074#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7075/*
7076 * Parse a completion argument "value[vallen]".
7077 * The detected completion goes in "*complp", argument type in "*argt".
7078 * When there is an argument, for function and user defined completion, it's
7079 * copied to allocated memory and stored in "*compl_arg".
7080 * Returns FAIL if something is wrong.
7081 */
7082 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007083parse_compl_arg(
7084 char_u *value,
7085 int vallen,
7086 int *complp,
7087 long *argt,
7088 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007089{
7090 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007091# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007092 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007093# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007094 int i;
7095 int valend = vallen;
7096
7097 /* Look for any argument part - which is the part after any ',' */
7098 for (i = 0; i < vallen; ++i)
7099 {
7100 if (value[i] == ',')
7101 {
7102 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007103# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007104 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007105# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007106 valend = i;
7107 break;
7108 }
7109 }
7110
7111 for (i = 0; command_complete[i].expand != 0; ++i)
7112 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007113 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007114 && STRNCMP(value, command_complete[i].name, valend) == 0)
7115 {
7116 *complp = command_complete[i].expand;
7117 if (command_complete[i].expand == EXPAND_BUFFERS)
7118 *argt |= BUFNAME;
7119 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7120 || command_complete[i].expand == EXPAND_FILES)
7121 *argt |= XFILE;
7122 break;
7123 }
7124 }
7125
7126 if (command_complete[i].expand == 0)
7127 {
7128 EMSG2(_("E180: Invalid complete value: %s"), value);
7129 return FAIL;
7130 }
7131
7132# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7133 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7134 && arg != NULL)
7135# else
7136 if (arg != NULL)
7137# endif
7138 {
7139 EMSG(_("E468: Completion argument only allowed for custom completion"));
7140 return FAIL;
7141 }
7142
7143# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7144 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7145 && arg == NULL)
7146 {
7147 EMSG(_("E467: Custom completion requires a function argument"));
7148 return FAIL;
7149 }
7150
7151 if (arg != NULL)
7152 *compl_arg = vim_strnsave(arg, (int)arglen);
7153# endif
7154 return OK;
7155}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007156
7157 int
7158cmdcomplete_str_to_type(char_u *complete_str)
7159{
7160 int i;
7161
7162 for (i = 0; command_complete[i].expand != 0; ++i)
7163 if (STRCMP(complete_str, command_complete[i].name) == 0)
7164 return command_complete[i].expand;
7165
7166 return EXPAND_NOTHING;
7167}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007168#endif
7169
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007171ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
Bram Moolenaare6850792010-05-14 15:28:44 +02007173 if (*eap->arg == NUL)
7174 {
7175#ifdef FEAT_EVAL
7176 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7177 char_u *p = NULL;
7178
7179 if (expr != NULL)
7180 {
7181 ++emsg_off;
7182 p = eval_to_string(expr, NULL, FALSE);
7183 --emsg_off;
7184 vim_free(expr);
7185 }
7186 if (p != NULL)
7187 {
7188 MSG(p);
7189 vim_free(p);
7190 }
7191 else
7192 MSG("default");
7193#else
7194 MSG(_("unknown"));
7195#endif
7196 }
7197 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007198 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199}
7200
7201 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007202ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203{
7204 if (*eap->arg == NUL && eap->cmd[2] == '!')
7205 MSG(_("Greetings, Vim user!"));
7206 do_highlight(eap->arg, eap->forceit, FALSE);
7207}
7208
7209
7210/*
7211 * Call this function if we thought we were going to exit, but we won't
7212 * (because of an error). May need to restore the terminal mode.
7213 */
7214 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007215not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217 exiting = FALSE;
7218 settmode(TMODE_RAW);
7219}
7220
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007221 static int
7222before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7223{
7224 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7225
7226 /* Bail out when autocommands closed the window.
7227 * Refuse to quit when the buffer in the last window is being closed (can
7228 * only happen in autocommands). */
7229 if (!win_valid(wp)
7230 || curbuf_locked()
7231 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7232 return TRUE;
7233
7234 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7235 {
7236 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7237 /* Refuse to quit when locked or when the buffer in the last window is
7238 * being closed (can only happen in autocommands). */
7239 if (curbuf_locked()
7240 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7241 return TRUE;
7242 }
7243
7244 return FALSE;
7245}
7246
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007248 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007249 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 */
7251 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007252ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007254 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007255
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256#ifdef FEAT_CMDWIN
7257 if (cmdwin_type != 0)
7258 {
7259 cmdwin_result = Ctrl_C;
7260 return;
7261 }
7262#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007263 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007264 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007265 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007266 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007267 return;
7268 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007269 if (eap->addr_count > 0)
7270 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007271 int wnr = eap->line2;
7272
7273 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7274 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007275 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007276 }
7277 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007278 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007279
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007280 /* Refuse to quit when locked. */
7281 if (curbuf_locked())
7282 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007283
7284 /* Trigger QuitPre and maybe ExitPre */
7285 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007286 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287
7288#ifdef FEAT_NETBEANS_INTG
7289 netbeansForcedQuit = eap->forceit;
7290#endif
7291
7292 /*
7293 * If there are more files or windows we won't exit.
7294 */
7295 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7296 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007297 if ((!buf_hide(wp->w_buffer)
7298 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007299 | (eap->forceit ? CCGD_FORCEIT : 0)
7300 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007302 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 {
7304 not_exiting();
7305 }
7306 else
7307 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007308 /* quit last window
7309 * Note: only_one_window() returns true, even so a help window is
7310 * still open. In that case only quit, if no address has been
7311 * specified. Example:
7312 * :h|wincmd w|1q - don't quit
7313 * :h|wincmd w|q - quit
7314 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007315 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007317 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007318#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007322 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 }
7324}
7325
7326/*
7327 * ":cquit".
7328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007330ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
7332 getout(1); /* this does not always pass on the exit code to the Manx
7333 compiler. why? */
7334}
7335
7336/*
7337 * ":qall": try to quit all windows
7338 */
7339 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007340ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341{
7342# ifdef FEAT_CMDWIN
7343 if (cmdwin_type != 0)
7344 {
7345 if (eap->forceit)
7346 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7347 else
7348 cmdwin_result = K_XF2;
7349 return;
7350 }
7351# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007352
7353 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007354 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007355 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007356 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007357 return;
7358 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007359
7360 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007361 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007362
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007364 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 getout(0);
7366 not_exiting();
7367}
7368
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369/*
7370 * ":close": close current window, unless it is the last one
7371 */
7372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007373ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007375 win_T *win;
7376 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007377#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007379 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007381#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007382 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007383 {
7384 if (eap->addr_count == 0)
7385 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007386 else
7387 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007388 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007389 {
7390 winnr++;
7391 if (winnr == eap->line2)
7392 break;
7393 }
7394 if (win == NULL)
7395 win = lastwin;
7396 ex_win_close(eap->forceit, win, NULL);
7397 }
7398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399}
7400
Bram Moolenaar4033c552017-09-16 20:54:51 +02007401#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007402/*
7403 * ":pclose": Close any preview window.
7404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007406ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007407{
7408 win_T *win;
7409
Bram Moolenaar29323592016-07-24 22:04:11 +02007410 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007411 if (win->w_p_pvw)
7412 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007413 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007414 break;
7415 }
7416}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007417#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007418
Bram Moolenaarf740b292006-02-16 22:11:02 +00007419/*
7420 * Close window "win" and take care of handling closing the last window for a
7421 * modified buffer.
7422 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007424ex_win_close(
7425 int forceit,
7426 win_T *win,
7427 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
7429 int need_hide;
7430 buf_T *buf = win->w_buffer;
7431
7432 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007433 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007435#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 if ((p_confirm || cmdmod.confirm) && p_write)
7437 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007438 bufref_T bufref;
7439
7440 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007442 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 return;
7444 need_hide = FALSE;
7445 }
7446 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007449 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 return;
7451 }
7452 }
7453
Bram Moolenaar4033c552017-09-16 20:54:51 +02007454#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007456#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007457
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007459 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007460 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007461 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007462 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463}
7464
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007466 * Handle the argument for a tabpage related ex command.
7467 * Returns a tabpage number.
7468 * When an error is encountered then eap->errmsg is set.
7469 */
7470 static int
7471get_tabpage_arg(exarg_T *eap)
7472{
7473 int tab_number;
7474 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7475
7476 if (eap->arg && *eap->arg != NUL)
7477 {
7478 char_u *p = eap->arg;
7479 char_u *p_save;
7480 int relative = 0; /* argument +N/-N means: go to N places to the
7481 * right/left relative to the current position. */
7482
7483 if (*p == '-')
7484 {
7485 relative = -1;
7486 p++;
7487 }
7488 else if (*p == '+')
7489 {
7490 relative = 1;
7491 p++;
7492 }
7493
7494 p_save = p;
7495 tab_number = getdigits(&p);
7496
7497 if (relative == 0)
7498 {
7499 if (STRCMP(p, "$") == 0)
7500 tab_number = LAST_TAB_NR;
7501 else if (p == p_save || *p_save == '-' || *p != NUL
7502 || tab_number > LAST_TAB_NR)
7503 {
7504 /* No numbers as argument. */
7505 eap->errmsg = e_invarg;
7506 goto theend;
7507 }
7508 }
7509 else
7510 {
7511 if (*p_save == NUL)
7512 tab_number = 1;
7513 else if (p == p_save || *p_save == '-' || *p != NUL
7514 || tab_number == 0)
7515 {
7516 /* No numbers as argument. */
7517 eap->errmsg = e_invarg;
7518 goto theend;
7519 }
7520 tab_number = tab_number * relative + tabpage_index(curtab);
7521 if (!unaccept_arg0 && relative == -1)
7522 --tab_number;
7523 }
7524 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7525 eap->errmsg = e_invarg;
7526 }
7527 else if (eap->addr_count > 0)
7528 {
7529 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007530 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007531 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007532 tab_number = 0;
7533 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007534 else
7535 {
7536 tab_number = eap->line2;
7537 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7538 {
7539 --tab_number;
7540 if (tab_number < unaccept_arg0)
7541 eap->errmsg = e_invarg;
7542 }
7543 }
7544 }
7545 else
7546 {
7547 switch (eap->cmdidx)
7548 {
7549 case CMD_tabnext:
7550 tab_number = tabpage_index(curtab) + 1;
7551 if (tab_number > LAST_TAB_NR)
7552 tab_number = 1;
7553 break;
7554 case CMD_tabmove:
7555 tab_number = LAST_TAB_NR;
7556 break;
7557 default:
7558 tab_number = tabpage_index(curtab);
7559 }
7560 }
7561
7562theend:
7563 return tab_number;
7564}
7565
7566/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007567 * ":tabclose": close current tab page, unless it is the last one.
7568 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 */
7570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007571ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007573 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007574 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007575
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007576# ifdef FEAT_CMDWIN
7577 if (cmdwin_type != 0)
7578 cmdwin_result = K_IGNORE;
7579 else
7580# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007581 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007582 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007583 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007585 tab_number = get_tabpage_arg(eap);
7586 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007587 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007588 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007589 if (tp == NULL)
7590 {
7591 beep_flush();
7592 return;
7593 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007594 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007595 {
7596 tabpage_close_other(tp, eap->forceit);
7597 return;
7598 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007599 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007600 tabpage_close(eap->forceit);
7601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007603}
7604
7605/*
7606 * ":tabonly": close all tab pages except the current one
7607 */
7608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007609ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007610{
7611 tabpage_T *tp;
7612 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007613 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007614
7615# ifdef FEAT_CMDWIN
7616 if (cmdwin_type != 0)
7617 cmdwin_result = K_IGNORE;
7618 else
7619# endif
7620 if (first_tabpage->tp_next == NULL)
7621 MSG(_("Already only one tab page"));
7622 else
7623 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007624 tab_number = get_tabpage_arg(eap);
7625 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007626 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007627 goto_tabpage(tab_number);
7628 /* Repeat this up to a 1000 times, because autocommands may
7629 * mess up the lists. */
7630 for (done = 0; done < 1000; ++done)
7631 {
7632 FOR_ALL_TABPAGES(tp)
7633 if (tp->tp_topframe != topframe)
7634 {
7635 tabpage_close_other(tp, eap->forceit);
7636 /* if we failed to close it quit */
7637 if (valid_tabpage(tp))
7638 done = 1000;
7639 /* start over, "tp" is now invalid */
7640 break;
7641 }
7642 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007643 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007644 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007645 }
7646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648
7649/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007650 * Close the current tab page.
7651 */
7652 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007653tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007654{
7655 /* First close all the windows but the current one. If that worked then
7656 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007657 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007658 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007659 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007660 ex_win_close(forceit, curwin, NULL);
7661# ifdef FEAT_GUI
7662 need_mouse_correct = TRUE;
7663# endif
7664}
7665
7666/*
7667 * Close tab page "tp", which is not the current tab page.
7668 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007669 * Also takes care of the tab pages line disappearing when closing the
7670 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007671 */
7672 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007673tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007674{
7675 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007676 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007677 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007678
7679 /* Limit to 1000 windows, autocommands may add a window while we close
7680 * one. OK, so I'm paranoid... */
7681 while (++done < 1000)
7682 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007683 wp = tp->tp_firstwin;
7684 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007685
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007686 /* Autocommands may delete the tab page under our fingers and we may
7687 * fail to close a window with a modified buffer. */
7688 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007689 break;
7690 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007691
Bram Moolenaar29323592016-07-24 22:04:11 +02007692 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007693
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007694 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007695 if (h != tabline_height())
7696 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007697}
7698
7699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 * ":only".
7701 */
7702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007703ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007705 win_T *wp;
7706 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707# ifdef FEAT_GUI
7708 need_mouse_correct = TRUE;
7709# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007710 if (eap->addr_count > 0)
7711 {
7712 wnr = eap->line2;
7713 for (wp = firstwin; --wnr > 0; )
7714 {
7715 if (wp->w_next == NULL)
7716 break;
7717 else
7718 wp = wp->w_next;
7719 }
7720 win_goto(wp);
7721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 close_others(TRUE, eap->forceit);
7723}
7724
7725/*
7726 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007727 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007729 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007730ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731{
7732 if (eap->addr_count == 0)
7733 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007734 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736
7737 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007738ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007740 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007741 if (!eap->skip)
7742 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007743#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007744 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007745#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007746 if (eap->addr_count == 0)
7747 win_close(curwin, FALSE); /* don't free buffer */
7748 else
7749 {
7750 int winnr = 0;
7751 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007752
Bram Moolenaar2256c992016-11-15 21:17:07 +01007753 FOR_ALL_WINDOWS(win)
7754 {
7755 winnr++;
7756 if (winnr == eap->line2)
7757 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007758 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007759 if (win == NULL)
7760 win = lastwin;
7761 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 }
7764}
7765
7766/*
7767 * ":stop" and ":suspend": Suspend Vim.
7768 */
7769 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007770ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771{
7772 /*
7773 * Disallow suspending for "rvim".
7774 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007775 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 {
7777 if (!eap->forceit)
7778 autowrite_all();
7779 windgoto((int)Rows - 1, 0);
7780 out_char('\n');
7781 out_flush();
7782 stoptermcap();
7783 out_flush(); /* needed for SUN to restore xterm buffer */
7784#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007785 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786#endif
7787 ui_suspend(); /* call machine specific function */
7788#ifdef FEAT_TITLE
7789 maketitle();
7790 resettitle(); /* force updating the title */
7791#endif
7792 starttermcap();
7793 scroll_start(); /* scroll screen before redrawing */
7794 redraw_later_clear();
7795 shell_resized(); /* may have resized window */
7796 }
7797}
7798
7799/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007800 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 */
7802 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007803ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804{
7805#ifdef FEAT_CMDWIN
7806 if (cmdwin_type != 0)
7807 {
7808 cmdwin_result = Ctrl_C;
7809 return;
7810 }
7811#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007812 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007813 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007814 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007815 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007816 return;
7817 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007818
7819 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007820 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821
7822 /*
7823 * if more files or windows we won't exit
7824 */
7825 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7826 exiting = TRUE;
7827 if ( ((eap->cmdidx == CMD_wq
7828 || curbufIsChanged())
7829 && do_write(eap) == FAIL)
7830 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007831 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 {
7833 not_exiting();
7834 }
7835 else
7836 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007839 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840# ifdef FEAT_GUI
7841 need_mouse_correct = TRUE;
7842# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007843 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007844 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 }
7846}
7847
7848/*
7849 * ":print", ":list", ":number".
7850 */
7851 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007852ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007854 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7855 EMSG(_(e_emptybuf));
7856 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007858 for ( ;!got_int; ui_breakcheck())
7859 {
7860 print_line(eap->line1,
7861 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7862 || (eap->flags & EXFLAG_NR)),
7863 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7864 if (++eap->line1 > eap->line2)
7865 break;
7866 out_flush(); /* show one line at a time */
7867 }
7868 setpcmark();
7869 /* put cursor at last line */
7870 curwin->w_cursor.lnum = eap->line2;
7871 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 }
7873
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875}
7876
7877#ifdef FEAT_BYTEOFF
7878 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007879ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880{
7881 goto_byte(eap->line2);
7882}
7883#endif
7884
7885/*
7886 * ":shell".
7887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007889ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890{
7891 do_shell(NULL, 0);
7892}
7893
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007894#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007896static int drop_busy = FALSE;
7897static int drop_filec;
7898static char_u **drop_filev = NULL;
7899static int drop_split;
7900static void (*drop_callback)(void *);
7901static void *drop_cookie;
7902
7903 static void
7904handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
7906 exarg_T ea;
7907 int save_msg_scroll = msg_scroll;
7908
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007909 // Setting the argument list may cause screen updates and being called
7910 // recursively. Avoid that by setting drop_busy.
7911 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912
7913 /* Check whether the current buffer is changed. If so, we will need
7914 * to split the current window or data could be lost.
7915 * We don't need to check if the 'hidden' option is set, as in this
7916 * case the buffer won't be lost.
7917 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007918 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {
7920 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007921 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 --emsg_off;
7923 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007924 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 if (win_split(0, 0) == FAIL)
7927 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007928 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929
7930 /* When splitting the window, create a new alist. Otherwise the
7931 * existing one is overwritten. */
7932 alist_unlink(curwin->w_alist);
7933 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 }
7935
7936 /*
7937 * Set up the new argument list.
7938 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007939 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940
7941 /*
7942 * Move to the first file.
7943 */
7944 /* Fake up a minimal "next" command for do_argfile() */
7945 vim_memset(&ea, 0, sizeof(ea));
7946 ea.cmd = (char_u *)"next";
7947 do_argfile(&ea, 0);
7948
7949 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7950 * mode that is not needed here. */
7951 need_start_insertmode = FALSE;
7952
7953 /* Restore msg_scroll, otherwise a following command may cause scrolling
7954 * unexpectedly. The screen will be redrawn by the caller, thus
7955 * msg_scroll being set by displaying a message is irrelevant. */
7956 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007957
7958 if (drop_callback != NULL)
7959 drop_callback(drop_cookie);
7960
7961 drop_filev = NULL;
7962 drop_busy = FALSE;
7963}
7964
7965/*
7966 * Handle a file drop. The code is here because a drop is *nearly* like an
7967 * :args command, but not quite (we have a list of exact filenames, so we
7968 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7969 * code is very similar to :args and hence needs access to a lot of the static
7970 * functions in this file.
7971 *
7972 * The "filev" list must have been allocated using alloc(), as should each item
7973 * in the list. This function takes over responsibility for freeing the "filev"
7974 * list.
7975 */
7976 void
7977handle_drop(
7978 int filec, // the number of files dropped
7979 char_u **filev, // the list of files dropped
7980 int split, // force splitting the window
7981 void (*callback)(void *), // to be called after setting the argument
7982 // list
7983 void *cookie) // argument for "callback" (allocated)
7984{
7985 // Cannot handle recursive drops, finish the pending one.
7986 if (drop_busy)
7987 {
7988 FreeWild(filec, filev);
7989 vim_free(cookie);
7990 return;
7991 }
7992
7993 // When calling handle_drop() more than once in a row we only use the last
7994 // one.
7995 if (drop_filev != NULL)
7996 {
7997 FreeWild(drop_filec, drop_filev);
7998 vim_free(drop_cookie);
7999 }
8000
8001 drop_filec = filec;
8002 drop_filev = filev;
8003 drop_split = split;
8004 drop_callback = callback;
8005 drop_cookie = cookie;
8006
8007 // Postpone this when:
8008 // - editing the command line
8009 // - not possible to change the current buffer
8010 // - updating the screen
8011 // As it may change buffers and window structures that are in use and cause
8012 // freed memory to be used.
8013 if (text_locked() || curbuf_locked() || updating_screen)
8014 return;
8015
8016 handle_drop_internal();
8017}
8018
8019/*
8020 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8021 * reset: Handle a postponed drop.
8022 */
8023 void
8024handle_any_postponed_drop(void)
8025{
8026 if (!drop_busy && drop_filev != NULL
8027 && !text_locked() && !curbuf_locked() && !updating_screen)
8028 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029}
8030#endif
8031
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032/*
8033 * Clear an argument list: free all file names and reset it to zero entries.
8034 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008035 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008036alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037{
8038 while (--al->al_ga.ga_len >= 0)
8039 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8040 ga_clear(&al->al_ga);
8041}
8042
8043/*
8044 * Init an argument list.
8045 */
8046 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008047alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048{
8049 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8050}
8051
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052/*
8053 * Remove a reference from an argument list.
8054 * Ignored when the argument list is the global one.
8055 * If the argument list is no longer used by any window, free it.
8056 */
8057 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008058alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059{
8060 if (al != &global_alist && --al->al_refcount <= 0)
8061 {
8062 alist_clear(al);
8063 vim_free(al);
8064 }
8065}
8066
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067/*
8068 * Create a new argument list and use it for the current window.
8069 */
8070 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008071alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072{
8073 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8074 if (curwin->w_alist == NULL)
8075 {
8076 curwin->w_alist = &global_alist;
8077 ++global_alist.al_refcount;
8078 }
8079 else
8080 {
8081 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008082 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 alist_init(curwin->w_alist);
8084 }
8085}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008087#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088/*
8089 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008090 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8091 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 */
8093 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008094alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095{
8096 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008097 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 char_u **new_arg_files;
8099 int new_arg_file_count;
8100 char_u *save_p_su = p_su;
8101 int i;
8102
8103 /* Don't use 'suffixes' here. This should work like the shell did the
8104 * expansion. Also, the vimrc file isn't read yet, thus the user
8105 * can't set the options. */
8106 p_su = empty_option;
8107 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8108 if (old_arg_files != NULL)
8109 {
8110 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008111 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8112 old_arg_count = GARGCOUNT;
8113 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008115 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 && new_arg_file_count > 0)
8117 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008118 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8119 TRUE, fnum_list, fnum_len);
8120 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 }
8123 p_su = save_p_su;
8124}
8125#endif
8126
8127/*
8128 * Set the argument list for the current window.
8129 * Takes over the allocated files[] and the allocated fnames in it.
8130 */
8131 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008132alist_set(
8133 alist_T *al,
8134 int count,
8135 char_u **files,
8136 int use_curbuf,
8137 int *fnum_list,
8138 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139{
8140 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008141 static int recursive = 0;
8142
8143 if (recursive)
8144 {
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008145 EMSG(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008146 return;
8147 }
8148 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149
8150 alist_clear(al);
8151 if (ga_grow(&al->al_ga, count) == OK)
8152 {
8153 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008154 {
8155 if (got_int)
8156 {
8157 /* When adding many buffers this can take a long time. Allow
8158 * interrupting here. */
8159 while (i < count)
8160 vim_free(files[i++]);
8161 break;
8162 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008163
8164 /* May set buffer name of a buffer previously used for the
8165 * argument list, so that it's re-used by alist_add. */
8166 if (fnum_list != NULL && i < fnum_len)
8167 buf_set_name(fnum_list[i], files[i]);
8168
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008170 ui_breakcheck();
8171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 vim_free(files);
8173 }
8174 else
8175 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008178
8179 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180}
8181
8182/*
8183 * Add file "fname" to argument list "al".
8184 * "fname" must have been allocated and "al" must have been checked for room.
8185 */
8186 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008187alist_add(
8188 alist_T *al,
8189 char_u *fname,
8190 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191{
8192 if (fname == NULL) /* don't add NULL file names */
8193 return;
8194#ifdef BACKSLASH_IN_FILENAME
8195 slash_adjust(fname);
8196#endif
8197 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8198 if (set_fnum > 0)
8199 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8200 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8201 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202}
8203
8204#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8205/*
8206 * Adjust slashes in file names. Called after 'shellslash' was set.
8207 */
8208 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008209alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210{
8211 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008213 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214
8215 for (i = 0; i < GARGCOUNT; ++i)
8216 if (GARGLIST[i].ae_fname != NULL)
8217 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008218 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 if (wp->w_alist != &global_alist)
8220 for (i = 0; i < WARGCOUNT(wp); ++i)
8221 if (WARGLIST(wp)[i].ae_fname != NULL)
8222 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223}
8224#endif
8225
8226/*
8227 * ":preserve".
8228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008230ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008232 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 ml_preserve(curbuf, TRUE);
8234}
8235
8236/*
8237 * ":recover".
8238 */
8239 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008240ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241{
8242 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8243 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008244 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8245 | CCGD_MULTWIN
8246 | (eap->forceit ? CCGD_FORCEIT : 0)
8247 | CCGD_EXCMD)
8248
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 && (*eap->arg == NUL
8250 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8251 ml_recover();
8252 recoverymode = FALSE;
8253}
8254
8255/*
8256 * Command modifier used in a wrong way.
8257 */
8258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008259ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260{
8261 eap->errmsg = e_invcmd;
8262}
8263
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264/*
8265 * :sview [+command] file split window with new file, read-only
8266 * :split [[+command] file] split window with current or new file
8267 * :vsplit [[+command] file] split window vertically with current or new file
8268 * :new [[+command] file] split window with no or new file
8269 * :vnew [[+command] file] split vertically window with no or new file
8270 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008271 *
8272 * :tabedit open new Tab page with empty window
8273 * :tabedit [+command] file open new Tab page and edit "file"
8274 * :tabnew [[+command] file] just like :tabedit
8275 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 */
8277 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008278ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008280 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008281#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008283#endif
8284#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008286#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008287 int use_tab = eap->cmdidx == CMD_tabedit
8288 || eap->cmdidx == CMD_tabfind
8289 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290
Bram Moolenaar4033c552017-09-16 20:54:51 +02008291#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294
Bram Moolenaar4033c552017-09-16 20:54:51 +02008295#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008297 * quickfix windows. But it's OK when doing ":tab split". */
8298 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {
8300 if (eap->cmdidx == CMD_split)
8301 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 if (eap->cmdidx == CMD_vsplit)
8303 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306
Bram Moolenaar4033c552017-09-16 20:54:51 +02008307#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008308 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {
8310 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8311 FNAME_MESS, TRUE, curbuf->b_ffname);
8312 if (fname == NULL)
8313 goto theend;
8314 eap->arg = fname;
8315 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008316# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008317 else
8318# endif
8319#endif
8320#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 && eap->cmdidx != CMD_new)
8324 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008325 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008326# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008327 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008328# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008329 au_has_group((char_u *)"FileExplorer"))
8330 {
8331 /* No browsing supported but we do have the file explorer:
8332 * Edit the directory. */
8333 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8334 eap->arg = (char_u *)".";
8335 }
8336 else
8337 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008338 fname = do_browse(0, (char_u *)(use_tab
8339 ? _("Edit File in new tab page")
8340 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008342 if (fname == NULL)
8343 goto theend;
8344 eap->arg = fname;
8345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 }
8347 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008350 /*
8351 * Either open new tab page or split the window.
8352 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008353 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008354 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008355 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8356 : eap->addr_count == 0 ? 0
8357 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008358 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008359 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008360
8361 /* set the alternate buffer for the window we came from */
8362 if (curwin != old_curwin
8363 && win_valid(old_curwin)
8364 && old_curwin->w_buffer != curbuf
8365 && !cmdmod.keepalt)
8366 old_curwin->w_alt_fnum = curbuf->b_fnum;
8367 }
8368 }
8369 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8371 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 /* Reset 'scrollbind' when editing another file, but keep it when
8373 * doing ":split" without arguments. */
8374 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008375# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008379 {
8380 RESET_BINDING(curwin);
8381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 else
8383 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 do_exedit(eap, old_curwin);
8385 }
8386
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008387# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008389# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008391# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392theend:
8393 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008396
8397/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008398 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008399 */
8400 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008401tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008402{
8403 exarg_T ea;
8404
8405 vim_memset(&ea, 0, sizeof(ea));
8406 ea.cmdidx = CMD_tabnew;
8407 ea.cmd = (char_u *)"tabn";
8408 ea.arg = (char_u *)"";
8409 ex_splitview(&ea);
8410}
8411
8412/*
8413 * :tabnext command
8414 */
8415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008416ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008417{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008418 int tab_number;
8419
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008420 switch (eap->cmdidx)
8421 {
8422 case CMD_tabfirst:
8423 case CMD_tabrewind:
8424 goto_tabpage(1);
8425 break;
8426 case CMD_tablast:
8427 goto_tabpage(9999);
8428 break;
8429 case CMD_tabprevious:
8430 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008431 if (eap->arg && *eap->arg != NUL)
8432 {
8433 char_u *p = eap->arg;
8434 char_u *p_save = p;
8435
8436 tab_number = getdigits(&p);
8437 if (p == p_save || *p_save == '-' || *p != NUL
8438 || tab_number == 0)
8439 {
8440 /* No numbers as argument. */
8441 eap->errmsg = e_invarg;
8442 return;
8443 }
8444 }
8445 else
8446 {
8447 if (eap->addr_count == 0)
8448 tab_number = 1;
8449 else
8450 {
8451 tab_number = eap->line2;
8452 if (tab_number < 1)
8453 {
8454 eap->errmsg = e_invrange;
8455 return;
8456 }
8457 }
8458 }
8459 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008460 break;
8461 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008462 tab_number = get_tabpage_arg(eap);
8463 if (eap->errmsg == NULL)
8464 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008465 break;
8466 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008467}
8468
8469/*
8470 * :tabmove command
8471 */
8472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008473ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008474{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008475 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008476
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008477 tab_number = get_tabpage_arg(eap);
8478 if (eap->errmsg == NULL)
8479 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008480}
8481
8482/*
8483 * :tabs command: List tabs and their contents.
8484 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008486ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008487{
8488 tabpage_T *tp;
8489 win_T *wp;
8490 int tabcount = 1;
8491
8492 msg_start();
8493 msg_scroll = TRUE;
8494 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8495 {
8496 msg_putchar('\n');
8497 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008498 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008499 out_flush(); /* output one line at a time */
8500 ui_breakcheck();
8501
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008502 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008503 wp = firstwin;
8504 else
8505 wp = tp->tp_firstwin;
8506 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8507 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008508 msg_putchar('\n');
8509 msg_putchar(wp == curwin ? '>' : ' ');
8510 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008511 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8512 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008513 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008514 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008515 else
8516 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8517 IObuff, IOSIZE, TRUE);
8518 msg_outtrans(IObuff);
8519 out_flush(); /* output one line at a time */
8520 ui_breakcheck();
8521 }
8522 }
8523}
8524
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525/*
8526 * ":mode": Set screen mode.
8527 * If no argument given, just get the screen size and redraw.
8528 */
8529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008530ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531{
8532 if (*eap->arg == NUL)
8533 shell_resized();
8534 else
8535 mch_screenmode(eap->arg);
8536}
8537
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538/*
8539 * ":resize".
8540 * set, increment or decrement current window height
8541 */
8542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008543ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544{
8545 int n;
8546 win_T *wp = curwin;
8547
8548 if (eap->addr_count > 0)
8549 {
8550 n = eap->line2;
8551 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8552 ;
8553 }
8554
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008555# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008557# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 if (cmdmod.split & WSP_VERT)
8560 {
8561 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008562 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8564 n = 9999;
8565 win_setwidth_win((int)n, wp);
8566 }
8567 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 {
8569 if (*eap->arg == '-' || *eap->arg == '+')
8570 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008571 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 n = 9999;
8573 win_setheight_win((int)n, wp);
8574 }
8575}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576
8577/*
8578 * ":find [+command] <file>" command.
8579 */
8580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008581ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582{
8583#ifdef FEAT_SEARCHPATH
8584 char_u *fname;
8585 int count;
8586
8587 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8588 TRUE, curbuf->b_ffname);
8589 if (eap->addr_count > 0)
8590 {
8591 /* Repeat finding the file "count" times. This matters when it
8592 * appears several times in the path. */
8593 count = eap->line2;
8594 while (fname != NULL && --count > 0)
8595 {
8596 vim_free(fname);
8597 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8598 FALSE, curbuf->b_ffname);
8599 }
8600 }
8601
8602 if (fname != NULL)
8603 {
8604 eap->arg = fname;
8605#endif
8606 do_exedit(eap, NULL);
8607#ifdef FEAT_SEARCHPATH
8608 vim_free(fname);
8609 }
8610#endif
8611}
8612
8613/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008614 * ":open" simulation: for now just work like ":visual".
8615 */
8616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008617ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008618{
8619 regmatch_T regmatch;
8620 char_u *p;
8621
8622 curwin->w_cursor.lnum = eap->line2;
8623 beginline(BL_SOL | BL_FIX);
8624 if (*eap->arg == '/')
8625 {
8626 /* ":open /pattern/": put cursor in column found with pattern */
8627 ++eap->arg;
8628 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8629 *p = NUL;
8630 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8631 if (regmatch.regprog != NULL)
8632 {
8633 regmatch.rm_ic = p_ic;
8634 p = ml_get_curline();
8635 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008636 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008637 else
8638 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008639 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008640 }
8641 /* Move to the NUL, ignore any other arguments. */
8642 eap->arg += STRLEN(eap->arg);
8643 }
8644 check_cursor();
8645
8646 eap->cmdidx = CMD_visual;
8647 do_exedit(eap, NULL);
8648}
8649
8650/*
8651 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 */
8653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008654ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655{
8656 do_exedit(eap, NULL);
8657}
8658
8659/*
8660 * ":edit <file>" command and alikes.
8661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008663do_exedit(
8664 exarg_T *eap,
8665 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666{
8667 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008669 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670
8671 /*
8672 * ":vi" command ends Ex mode.
8673 */
8674 if (exmode_active && (eap->cmdidx == CMD_visual
8675 || eap->cmdidx == CMD_view))
8676 {
8677 exmode_active = FALSE;
8678 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008679 {
8680 /* Special case: ":global/pat/visual\NLvi-commands" */
8681 if (global_busy)
8682 {
8683 int rd = RedrawingDisabled;
8684 int nwr = no_wait_return;
8685 int ms = msg_scroll;
8686#ifdef FEAT_GUI
8687 int he = hold_gui_events;
8688#endif
8689
8690 if (eap->nextcmd != NULL)
8691 {
8692 stuffReadbuff(eap->nextcmd);
8693 eap->nextcmd = NULL;
8694 }
8695
8696 if (exmode_was != EXMODE_VIM)
8697 settmode(TMODE_RAW);
8698 RedrawingDisabled = 0;
8699 no_wait_return = 0;
8700 need_wait_return = FALSE;
8701 msg_scroll = 0;
8702#ifdef FEAT_GUI
8703 hold_gui_events = 0;
8704#endif
8705 must_redraw = CLEAR;
8706
8707 main_loop(FALSE, TRUE);
8708
8709 RedrawingDisabled = rd;
8710 no_wait_return = nwr;
8711 msg_scroll = ms;
8712#ifdef FEAT_GUI
8713 hold_gui_events = he;
8714#endif
8715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 }
8719
8720 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008721 || eap->cmdidx == CMD_tabnew
8722 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008723 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008725 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 setpcmark();
8727 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008728 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8729 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008731 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 || *eap->arg != NUL
8733#ifdef FEAT_BROWSE
8734 || cmdmod.browse
8735#endif
8736 )
8737 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008738 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8739 * can bring us here, others are stopped earlier. */
8740 if (*eap->arg != NUL && curbuf_locked())
8741 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008742
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 n = readonlymode;
8744 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8745 readonlymode = TRUE;
8746 else if (eap->cmdidx == CMD_enew)
8747 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8748 empty buffer */
8749 setpcmark();
8750 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8751 NULL, eap,
8752 /* ":edit" goes to first line if Vi compatible */
8753 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8754 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8755 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008756 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008758 /* after a split we can use an existing buffer */
8759 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008761 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 {
8763 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 if (old_curwin != NULL)
8765 {
8766 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008767 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008769#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008770 cleanup_T cs;
8771
8772 /* Reset the error/interrupt/exception state here so that
8773 * aborting() returns FALSE when closing a window. */
8774 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008775#endif
8776#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008778#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008779 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008780
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008781#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008782 /* Restore the error/interrupt/exception state if not
8783 * discarded by a new aborting error, interrupt, or
8784 * uncaught exception. */
8785 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 }
8788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 }
8790 else if (readonlymode && curbuf->b_nwindows == 1)
8791 {
8792 /* When editing an already visited buffer, 'readonly' won't be set
8793 * but the previous value is kept. With ":view" and ":sview" we
8794 * want the file to be readonly, except when another window is
8795 * editing the same buffer. */
8796 curbuf->b_p_ro = TRUE;
8797 }
8798 readonlymode = n;
8799 }
8800 else
8801 {
8802 if (eap->do_ecmd_cmd != NULL)
8803 do_cmdline_cmd(eap->do_ecmd_cmd);
8804#ifdef FEAT_TITLE
8805 n = curwin->w_arg_idx_invalid;
8806#endif
8807 check_arg_idx(curwin);
8808#ifdef FEAT_TITLE
8809 if (n != curwin->w_arg_idx_invalid)
8810 maketitle();
8811#endif
8812 }
8813
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 /*
8815 * if ":split file" worked, set alternate file name in old window to new
8816 * file
8817 */
8818 if (old_curwin != NULL
8819 && *eap->arg != NUL
8820 && curwin != old_curwin
8821 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008822 && old_curwin->w_buffer != curbuf
8823 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
8826 ex_no_reprint = TRUE;
8827}
8828
8829#ifndef FEAT_GUI
8830/*
8831 * ":gui" and ":gvim" when there is no GUI.
8832 */
8833 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008834ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835{
8836 eap->errmsg = e_nogvim;
8837}
8838#endif
8839
8840#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8841 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008842ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843{
8844 gui_make_tearoff(eap->arg);
8845}
8846#endif
8847
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008848#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8849 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008851ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008853# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8854 if (gui.in_use)
8855 gui_make_popup(eap->arg, eap->forceit);
8856# ifdef FEAT_TERM_POPUP_MENU
8857 else
8858# endif
8859# endif
8860# ifdef FEAT_TERM_POPUP_MENU
8861 pum_make_popup(eap->arg, eap->forceit);
8862# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863}
8864#endif
8865
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008867ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868{
8869 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8870 MSG(_("No swap file"));
8871 else
8872 msg(curbuf->b_ml.ml_mfp->mf_fname);
8873}
8874
8875/*
8876 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8877 * offset.
8878 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008881ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008884 win_T *save_curwin = curwin;
8885 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 long topline;
8887 long y;
8888 linenr_T old_linenr = curwin->w_cursor.lnum;
8889
8890 setpcmark();
8891
8892 /*
8893 * determine max topline
8894 */
8895 if (curwin->w_p_scb)
8896 {
8897 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008898 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 {
8900 if (wp->w_p_scb && wp->w_buffer)
8901 {
8902 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8903 if (topline > y)
8904 topline = y;
8905 }
8906 }
8907 if (topline < 1)
8908 topline = 1;
8909 }
8910 else
8911 {
8912 topline = 1;
8913 }
8914
8915
8916 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008917 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008919 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 {
8921 if (curwin->w_p_scb)
8922 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008923 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 y = topline - curwin->w_topline;
8925 if (y > 0)
8926 scrollup(y, TRUE);
8927 else
8928 scrolldown(-y, TRUE);
8929 curwin->w_scbind_pos = topline;
8930 redraw_later(VALID);
8931 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 }
8934 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008935 curwin = save_curwin;
8936 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 if (curwin->w_p_scb)
8938 {
8939 did_syncbind = TRUE;
8940 checkpcmark();
8941 if (old_linenr != curwin->w_cursor.lnum)
8942 {
8943 char_u ctrl_o[2];
8944
8945 ctrl_o[0] = Ctrl_O;
8946 ctrl_o[1] = 0;
8947 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8948 }
8949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950}
8951
8952
8953 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008954ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008956 int i;
8957 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8958 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959
8960 if (eap->usefilter) /* :r!cmd */
8961 do_bang(1, eap, FALSE, FALSE, TRUE);
8962 else
8963 {
8964 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8965 return;
8966
8967#ifdef FEAT_BROWSE
8968 if (cmdmod.browse)
8969 {
8970 char_u *browseFile;
8971
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008972 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 NULL, NULL, NULL, curbuf);
8974 if (browseFile != NULL)
8975 {
8976 i = readfile(browseFile, NULL,
8977 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8978 vim_free(browseFile);
8979 }
8980 else
8981 i = OK;
8982 }
8983 else
8984#endif
8985 if (*eap->arg == NUL)
8986 {
8987 if (check_fname() == FAIL) /* check for no file name */
8988 return;
8989 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8990 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8991 }
8992 else
8993 {
8994 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8995 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8996 i = readfile(eap->arg, NULL,
8997 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8998
8999 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009000 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009002#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 if (!aborting())
9004#endif
9005 EMSG2(_(e_notopen), eap->arg);
9006 }
9007 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009008 {
9009 if (empty && exmode_active)
9010 {
9011 /* Delete the empty line that remains. Historically ex does
9012 * this but vi doesn't. */
9013 if (eap->line2 == 0)
9014 lnum = curbuf->b_ml.ml_line_count;
9015 else
9016 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009017 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009018 {
9019 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009020 if (curwin->w_cursor.lnum > 1
9021 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009022 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009023 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009024 }
9025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 }
9029}
9030
Bram Moolenaarea408852005-06-25 22:49:46 +00009031static char_u *prev_dir = NULL;
9032
9033#if defined(EXITFREE) || defined(PROTO)
9034 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009035free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009036{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009037 VIM_CLEAR(prev_dir);
9038 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009039}
9040#endif
9041
Bram Moolenaarf4258302013-06-02 18:20:17 +02009042/*
9043 * Deal with the side effects of changing the current directory.
9044 * When "local" is TRUE then this was after an ":lcd" command.
9045 */
9046 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009047post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009048{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009049 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009050 if (local)
9051 {
9052 /* If still in global directory, need to remember current
9053 * directory as global directory. */
9054 if (globaldir == NULL && prev_dir != NULL)
9055 globaldir = vim_strsave(prev_dir);
9056 /* Remember this local directory for the window. */
9057 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9058 curwin->w_localdir = vim_strsave(NameBuff);
9059 }
9060 else
9061 {
9062 /* We are now in the global directory, no need to remember its
9063 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009064 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009065 }
9066
9067 shorten_fnames(TRUE);
9068}
9069
Bram Moolenaarea408852005-06-25 22:49:46 +00009070
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071/*
9072 * ":cd", ":lcd", ":chdir" and ":lchdir".
9073 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009074 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009075ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 char_u *new_dir;
9078 char_u *tofree;
9079
9080 new_dir = eap->arg;
9081#if !defined(UNIX) && !defined(VMS)
9082 /* for non-UNIX ":cd" means: print current directory */
9083 if (*new_dir == NUL)
9084 ex_pwd(NULL);
9085 else
9086#endif
9087 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009088 if (allbuf_locked())
9089 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009090 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9091 && !eap->forceit)
9092 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009093 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009094 return;
9095 }
9096
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 /* ":cd -": Change to previous directory */
9098 if (STRCMP(new_dir, "-") == 0)
9099 {
9100 if (prev_dir == NULL)
9101 {
9102 EMSG(_("E186: No previous directory"));
9103 return;
9104 }
9105 new_dir = prev_dir;
9106 }
9107
9108 /* Save current directory for next ":cd -" */
9109 tofree = prev_dir;
9110 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9111 prev_dir = vim_strsave(NameBuff);
9112 else
9113 prev_dir = NULL;
9114
9115#if defined(UNIX) || defined(VMS)
9116 /* for UNIX ":cd" means: go to home directory */
9117 if (*new_dir == NUL)
9118 {
9119 /* use NameBuff for home directory name */
9120# ifdef VMS
9121 char_u *p;
9122
9123 p = mch_getenv((char_u *)"SYS$LOGIN");
9124 if (p == NULL || *p == NUL) /* empty is the same as not set */
9125 NameBuff[0] = NUL;
9126 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009127 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128# else
9129 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9130# endif
9131 new_dir = NameBuff;
9132 }
9133#endif
9134 if (new_dir == NULL || vim_chdir(new_dir))
9135 EMSG(_(e_failed));
9136 else
9137 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009138 int is_local_chdir = eap->cmdidx == CMD_lcd
9139 || eap->cmdidx == CMD_lchdir;
9140
9141 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142
9143 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009144 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009146 apply_autocmds(EVENT_DIRCHANGED,
9147 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9148 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 }
9150 vim_free(tofree);
9151 }
9152}
9153
9154/*
9155 * ":pwd".
9156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009158ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159{
9160 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9161 {
9162#ifdef BACKSLASH_IN_FILENAME
9163 slash_adjust(NameBuff);
9164#endif
9165 msg(NameBuff);
9166 }
9167 else
9168 EMSG(_("E187: Unknown"));
9169}
9170
9171/*
9172 * ":=".
9173 */
9174 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009175ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176{
9177 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009178 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179}
9180
9181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009182ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009184 int n;
9185 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186
9187 if (cursor_valid())
9188 {
9189 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9190 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009191 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009193
9194 len = eap->line2;
9195 switch (*eap->arg)
9196 {
9197 case 'm': break;
9198 case NUL: len *= 1000L; break;
9199 default: EMSG2(_(e_invarg2), eap->arg); return;
9200 }
9201 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202}
9203
9204/*
9205 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9206 */
9207 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009208do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209{
9210 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009211 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212
9213 cursor_on();
9214 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009215 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009217 wait_now = msec - done > 1000L ? 1000L : msec - done;
9218#ifdef FEAT_TIMERS
9219 {
9220 long due_time = check_due_timer();
9221
9222 if (due_time > 0 && due_time < wait_now)
9223 wait_now = due_time;
9224 }
9225#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009226#ifdef FEAT_JOB_CHANNEL
9227 if (has_any_channel() && wait_now > 100L)
9228 wait_now = 100L;
9229#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009230 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009231#ifdef FEAT_JOB_CHANNEL
9232 if (has_any_channel())
9233 ui_breakcheck_force(TRUE);
9234 else
9235#endif
9236 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009237#ifdef MESSAGE_QUEUE
9238 /* Process the netbeans and clientserver messages that may have been
9239 * received in the call to ui_breakcheck() when the GUI is in use. This
9240 * may occur when running a test case. */
9241 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009244
9245 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9246 // input buffer, otherwise a following call to input() fails.
9247 if (got_int)
9248 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249}
9250
9251 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009252do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253{
9254 int mode;
9255 char_u *cmdp;
9256
9257 cmdp = eap->cmd;
9258 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9259
9260 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9261 eap->arg, mode, isabbrev))
9262 {
9263 case 1: EMSG(_(e_invarg));
9264 break;
9265 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9266 break;
9267 }
9268}
9269
9270/*
9271 * ":winsize" command (obsolete).
9272 */
9273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009274ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275{
9276 int w, h;
9277 char_u *arg = eap->arg;
9278 char_u *p;
9279
9280 w = getdigits(&arg);
9281 arg = skipwhite(arg);
9282 p = arg;
9283 h = getdigits(&arg);
9284 if (*p != NUL && *arg == NUL)
9285 set_shellsize(w, h, TRUE);
9286 else
9287 EMSG(_("E465: :winsize requires two number arguments"));
9288}
9289
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009291ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292{
9293 int xchar = NUL;
9294 char_u *p;
9295
9296 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9297 {
9298 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9299 if (eap->arg[1] == NUL)
9300 {
9301 EMSG(_(e_invarg));
9302 return;
9303 }
9304 xchar = eap->arg[1];
9305 p = eap->arg + 2;
9306 }
9307 else
9308 p = eap->arg + 1;
9309
9310 eap->nextcmd = check_nextcmd(p);
9311 p = skipwhite(p);
9312 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9313 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009314 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 {
9316 /* Pass flags on for ":vertical wincmd ]". */
9317 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009318 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9320 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009321 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 }
9323}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324
Bram Moolenaar843ee412004-06-30 16:16:41 +00009325#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326/*
9327 * ":winpos".
9328 */
9329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009330ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331{
9332 int x, y;
9333 char_u *arg = eap->arg;
9334 char_u *p;
9335
9336 if (*arg == NUL)
9337 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009338# if defined(FEAT_GUI) || defined(MSWIN)
9339# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009341# else
9342 if (mch_get_winpos(&x, &y) != FAIL)
9343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 {
9345 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9346 msg(IObuff);
9347 }
9348 else
9349# endif
9350 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9351 }
9352 else
9353 {
9354 x = getdigits(&arg);
9355 arg = skipwhite(arg);
9356 p = arg;
9357 y = getdigits(&arg);
9358 if (*p == NUL || *arg != NUL)
9359 {
9360 EMSG(_("E466: :winpos requires two number arguments"));
9361 return;
9362 }
9363# ifdef FEAT_GUI
9364 if (gui.in_use)
9365 gui_mch_set_winpos(x, y);
9366 else if (gui.starting)
9367 {
9368 /* Remember the coordinates for when the window is opened. */
9369 gui_win_x = x;
9370 gui_win_y = y;
9371 }
9372# ifdef HAVE_TGETENT
9373 else
9374# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009375# else
9376# ifdef MSWIN
9377 mch_set_winpos(x, y);
9378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379# endif
9380# ifdef HAVE_TGETENT
9381 if (*T_CWP)
9382 term_set_winpos(x, y);
9383# endif
9384 }
9385}
9386#endif
9387
9388/*
9389 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9390 */
9391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009392ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393{
9394 oparg_T oa;
9395
9396 clear_oparg(&oa);
9397 oa.regname = eap->regname;
9398 oa.start.lnum = eap->line1;
9399 oa.end.lnum = eap->line2;
9400 oa.line_count = eap->line2 - eap->line1 + 1;
9401 oa.motion_type = MLINE;
9402#ifdef FEAT_VIRTUALEDIT
9403 virtual_op = FALSE;
9404#endif
9405 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9406 {
9407 setpcmark();
9408 curwin->w_cursor.lnum = eap->line1;
9409 beginline(BL_SOL | BL_FIX);
9410 }
9411
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009412 if (VIsual_active)
9413 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009414
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 switch (eap->cmdidx)
9416 {
9417 case CMD_delete:
9418 oa.op_type = OP_DELETE;
9419 op_delete(&oa);
9420 break;
9421
9422 case CMD_yank:
9423 oa.op_type = OP_YANK;
9424 (void)op_yank(&oa, FALSE, TRUE);
9425 break;
9426
9427 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009428 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009430 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9431#else
9432 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009434 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 oa.op_type = OP_RSHIFT;
9436 else
9437 oa.op_type = OP_LSHIFT;
9438 op_shift(&oa, FALSE, eap->amount);
9439 break;
9440 }
9441#ifdef FEAT_VIRTUALEDIT
9442 virtual_op = MAYBE;
9443#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009444 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445}
9446
9447/*
9448 * ":put".
9449 */
9450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009451ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452{
9453 /* ":0put" works like ":1put!". */
9454 if (eap->line2 == 0)
9455 {
9456 eap->line2 = 1;
9457 eap->forceit = TRUE;
9458 }
9459 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009460 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9461 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462}
9463
9464/*
9465 * Handle ":copy" and ":move".
9466 */
9467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009468ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
9470 long n;
9471
Bram Moolenaarded27822017-01-02 14:27:34 +01009472 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 if (eap->arg == NULL) /* error detected */
9474 {
9475 eap->nextcmd = NULL;
9476 return;
9477 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009478 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479
9480 /*
9481 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9482 */
9483 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9484 {
9485 EMSG(_(e_invaddr));
9486 return;
9487 }
9488
9489 if (eap->cmdidx == CMD_move)
9490 {
9491 if (do_move(eap->line1, eap->line2, n) == FAIL)
9492 return;
9493 }
9494 else
9495 ex_copy(eap->line1, eap->line2, n);
9496 u_clearline();
9497 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009498 ex_may_print(eap);
9499}
9500
9501/*
9502 * Print the current line if flags were given to the Ex command.
9503 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009504 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009505ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009506{
9507 if (eap->flags != 0)
9508 {
9509 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9510 (eap->flags & EXFLAG_LIST));
9511 ex_no_reprint = TRUE;
9512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513}
9514
9515/*
9516 * ":smagic" and ":snomagic".
9517 */
9518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009519ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
9521 int magic_save = p_magic;
9522
9523 p_magic = (eap->cmdidx == CMD_smagic);
9524 do_sub(eap);
9525 p_magic = magic_save;
9526}
9527
9528/*
9529 * ":join".
9530 */
9531 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009532ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533{
9534 curwin->w_cursor.lnum = eap->line1;
9535 if (eap->line1 == eap->line2)
9536 {
9537 if (eap->addr_count >= 2) /* :2,2join does nothing */
9538 return;
9539 if (eap->line2 == curbuf->b_ml.ml_line_count)
9540 {
9541 beep_flush();
9542 return;
9543 }
9544 ++eap->line2;
9545 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009546 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009548 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549}
9550
9551/*
9552 * ":[addr]@r" or ":[addr]*r": execute register
9553 */
9554 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009555ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556{
9557 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009558 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559
9560 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009561 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562
9563#ifdef USE_ON_FLY_SCROLL
9564 dont_scroll = TRUE; /* disallow scrolling here */
9565#endif
9566
9567 /* get the register name. No name means to use the previous one */
9568 c = *eap->arg;
9569 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9570 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009571 /* Put the register in the typeahead buffer with the "silent" flag. */
9572 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9573 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 else
9578 {
9579 int save_efr = exec_from_reg;
9580
9581 exec_from_reg = TRUE;
9582
9583 /*
9584 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009585 * Continue until the stuff buffer is empty and all added characters
9586 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009588 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9590
9591 exec_from_reg = save_efr;
9592 }
9593}
9594
9595/*
9596 * ":!".
9597 */
9598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009599ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600{
9601 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9602}
9603
9604/*
9605 * ":undo".
9606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009608ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009610 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009611 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009612 else
9613 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614}
9615
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009616#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009617 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009618ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009619{
9620 char_u hash[UNDO_HASH_SIZE];
9621
9622 u_compute_hash(hash);
9623 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9624}
9625
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009627ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009628{
9629 char_u hash[UNDO_HASH_SIZE];
9630
9631 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009632 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009633}
9634#endif
9635
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636/*
9637 * ":redo".
9638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009640ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641{
9642 u_redo(1);
9643}
9644
9645/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009646 * ":earlier" and ":later".
9647 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009649ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009650{
9651 long count = 0;
9652 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009653 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009654 char_u *p = eap->arg;
9655
9656 if (*p == NUL)
9657 count = 1;
9658 else if (isdigit(*p))
9659 {
9660 count = getdigits(&p);
9661 switch (*p)
9662 {
9663 case 's': ++p; sec = TRUE; break;
9664 case 'm': ++p; sec = TRUE; count *= 60; break;
9665 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009666 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9667 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009668 }
9669 }
9670
9671 if (*p != NUL)
9672 EMSG2(_(e_invarg2), eap->arg);
9673 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009674 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9675 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009676}
9677
9678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 * ":redir": start/stop redirection.
9680 */
9681 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009682ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683{
9684 char *mode;
9685 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009686 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687
Bram Moolenaarba768492016-07-08 15:32:54 +02009688#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009689 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009690 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009691 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009692 return;
9693 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009694#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009695
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 if (STRICMP(eap->arg, "END") == 0)
9697 close_redir();
9698 else
9699 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009700 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009702 ++arg;
9703 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009705 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 mode = "a";
9707 }
9708 else
9709 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009710 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711
9712 close_redir();
9713
9714 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009715 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 if (fname == NULL)
9717 return;
9718#ifdef FEAT_BROWSE
9719 if (cmdmod.browse)
9720 {
9721 char_u *browseFile;
9722
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009723 browseFile = do_browse(BROWSE_SAVE,
9724 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009725 fname, NULL, NULL,
9726 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 if (browseFile == NULL)
9728 return; /* operation cancelled */
9729 vim_free(fname);
9730 fname = browseFile;
9731 eap->forceit = TRUE; /* since dialog already asked */
9732 }
9733#endif
9734
9735 redir_fd = open_exfile(fname, eap->forceit, mode);
9736 vim_free(fname);
9737 }
9738#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009739 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 {
9741 /* redirect to a register a-z (resp. A-Z for appending) */
9742 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009743 ++arg;
9744 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009746 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009747 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009749 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009751 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009752 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009753 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009754 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009756 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009757 if (*arg == '>')
9758 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009759 /* Make register empty when not using @A-@Z and the
9760 * command is valid. */
9761 if (*arg == NUL && !isupper(redir_reg))
9762 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009764 }
9765 if (*arg != NUL)
9766 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009767 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009768 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009769 }
9770 }
9771 else if (*arg == '=' && arg[1] == '>')
9772 {
9773 int append;
9774
9775 /* redirect to a variable */
9776 close_redir();
9777 arg += 2;
9778
9779 if (*arg == '>')
9780 {
9781 ++arg;
9782 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 }
9784 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009785 append = FALSE;
9786
9787 if (var_redir_start(skipwhite(arg), append) == OK)
9788 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 }
9790#endif
9791
9792 /* TODO: redirect to a buffer */
9793
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009795 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009797
9798 /* Make sure redirection is not off. Can happen for cmdline completion
9799 * that indirectly invokes a command to catch its output. */
9800 if (redir_fd != NULL
9801#ifdef FEAT_EVAL
9802 || redir_reg || redir_vname
9803#endif
9804 )
9805 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806}
9807
9808/*
9809 * ":redraw": force redraw
9810 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009811 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009812ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813{
9814 int r = RedrawingDisabled;
9815 int p = p_lz;
9816
9817 RedrawingDisabled = 0;
9818 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009819 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009821 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822#ifdef FEAT_TITLE
9823 if (need_maketitle)
9824 maketitle();
9825#endif
9826 RedrawingDisabled = r;
9827 p_lz = p;
9828
9829 /* Reset msg_didout, so that a message that's there is overwritten. */
9830 msg_didout = FALSE;
9831 msg_col = 0;
9832
Bram Moolenaar56667a52013-07-17 11:54:28 +02009833 /* No need to wait after an intentional redraw. */
9834 need_wait_return = FALSE;
9835
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 out_flush();
9837}
9838
9839/*
9840 * ":redrawstatus": force redraw of status line(s)
9841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009843ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 int r = RedrawingDisabled;
9846 int p = p_lz;
9847
9848 RedrawingDisabled = 0;
9849 p_lz = FALSE;
9850 if (eap->forceit)
9851 status_redraw_all();
9852 else
9853 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009854 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 RedrawingDisabled = r;
9856 p_lz = p;
9857 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858}
9859
9860 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009861close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862{
9863 if (redir_fd != NULL)
9864 {
9865 fclose(redir_fd);
9866 redir_fd = NULL;
9867 }
9868#ifdef FEAT_EVAL
9869 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009870 if (redir_vname)
9871 {
9872 var_redir_stop();
9873 redir_vname = 0;
9874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875#endif
9876}
9877
9878#if defined(FEAT_SESSION) && defined(USE_CRNL)
9879# define MKSESSION_NL
9880static int mksession_nl = FALSE; /* use NL only in put_eol() */
9881#endif
9882
9883/*
9884 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9885 */
9886 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009887ex_mkrc(
9888 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889{
9890 FILE *fd;
9891 int failed = FALSE;
9892 char_u *fname;
9893#ifdef FEAT_BROWSE
9894 char_u *browseFile = NULL;
9895#endif
9896#ifdef FEAT_SESSION
9897 int view_session = FALSE;
9898 int using_vdir = FALSE; /* using 'viewdir'? */
9899 char_u *viewFile = NULL;
9900 unsigned *flagp;
9901#endif
9902
9903 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9904 {
9905#ifdef FEAT_SESSION
9906 view_session = TRUE;
9907#else
9908 ex_ni(eap);
9909 return;
9910#endif
9911 }
9912
9913#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009914 /* Use the short file name until ":lcd" is used. We also don't use the
9915 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009916 did_lcd = FALSE;
9917
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9919 if (eap->cmdidx == CMD_mkview
9920 && (*eap->arg == NUL
9921 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9922 {
9923 eap->forceit = TRUE;
9924 fname = get_view_file(*eap->arg);
9925 if (fname == NULL)
9926 return;
9927 viewFile = fname;
9928 using_vdir = TRUE;
9929 }
9930 else
9931#endif
9932 if (*eap->arg != NUL)
9933 fname = eap->arg;
9934 else if (eap->cmdidx == CMD_mkvimrc)
9935 fname = (char_u *)VIMRC_FILE;
9936#ifdef FEAT_SESSION
9937 else if (eap->cmdidx == CMD_mksession)
9938 fname = (char_u *)SESSION_FILE;
9939#endif
9940 else
9941 fname = (char_u *)EXRC_FILE;
9942
9943#ifdef FEAT_BROWSE
9944 if (cmdmod.browse)
9945 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009946 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947# ifdef FEAT_SESSION
9948 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9949 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9950# endif
9951 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009952 fname, (char_u *)"vim", NULL,
9953 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 if (browseFile == NULL)
9955 goto theend;
9956 fname = browseFile;
9957 eap->forceit = TRUE; /* since dialog already asked */
9958 }
9959#endif
9960
9961#if defined(FEAT_SESSION) && defined(vim_mkdir)
9962 /* When using 'viewdir' may have to create the directory. */
9963 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009964 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965#endif
9966
9967 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9968 if (fd != NULL)
9969 {
9970#ifdef FEAT_SESSION
9971 if (eap->cmdidx == CMD_mkview)
9972 flagp = &vop_flags;
9973 else
9974 flagp = &ssop_flags;
9975#endif
9976
9977#ifdef MKSESSION_NL
9978 /* "unix" in 'sessionoptions': use NL line separator */
9979 if (view_session && (*flagp & SSOP_UNIX))
9980 mksession_nl = TRUE;
9981#endif
9982
9983 /* Write the version command for :mkvimrc */
9984 if (eap->cmdidx == CMD_mkvimrc)
9985 (void)put_line(fd, "version 6.0");
9986
9987#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009988 if (eap->cmdidx == CMD_mksession)
9989 {
9990 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9991 failed = TRUE;
9992 }
9993
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 if (eap->cmdidx != CMD_mkview)
9995#endif
9996 {
9997 /* Write setting 'compatible' first, because it has side effects.
9998 * For that same reason only do it when needed. */
9999 if (p_cp)
10000 (void)put_line(fd, "if !&cp | set cp | endif");
10001 else
10002 (void)put_line(fd, "if &cp | set nocp | endif");
10003 }
10004
10005#ifdef FEAT_SESSION
10006 if (!view_session
10007 || (eap->cmdidx == CMD_mksession
10008 && (*flagp & SSOP_OPTIONS)))
10009#endif
10010 failed |= (makemap(fd, NULL) == FAIL
10011 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10012
10013#ifdef FEAT_SESSION
10014 if (!failed && view_session)
10015 {
10016 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10017 failed = TRUE;
10018 if (eap->cmdidx == CMD_mksession)
10019 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010020 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021
Bram Moolenaard9462e32011-04-11 21:35:11 +020010022 dirnow = alloc(MAXPATHL);
10023 if (dirnow == NULL)
10024 failed = TRUE;
10025 else
10026 {
10027 /*
10028 * Change to session file's dir.
10029 */
10030 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010032 *dirnow = NUL;
10033 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10034 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010035 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010036 shorten_fnames(TRUE);
10037 }
10038 else if (*dirnow != NUL
10039 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10040 {
10041 if (mch_chdir((char *)globaldir) == 0)
10042 shorten_fnames(TRUE);
10043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044
Bram Moolenaard9462e32011-04-11 21:35:11 +020010045 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046
Bram Moolenaard9462e32011-04-11 21:35:11 +020010047 /* restore original dir */
10048 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010050 {
10051 if (mch_chdir((char *)dirnow) != 0)
10052 EMSG(_(e_prev_dir));
10053 shorten_fnames(TRUE);
10054 }
10055 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 }
10057 }
10058 else
10059 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010060 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10061 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 }
10063 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10064 == FAIL)
10065 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010066 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10067 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010068 if (eap->cmdidx == CMD_mksession)
10069 {
10070 if (put_line(fd, "unlet SessionLoad") == FAIL)
10071 failed = TRUE;
10072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 }
10074#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010075 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10076 failed = TRUE;
10077
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 failed |= fclose(fd);
10079
10080 if (failed)
10081 EMSG(_(e_write));
10082#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10083 else if (eap->cmdidx == CMD_mksession)
10084 {
10085 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010086 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087
Bram Moolenaard9462e32011-04-11 21:35:11 +020010088 tbuf = alloc(MAXPATHL);
10089 if (tbuf != NULL)
10090 {
10091 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10092 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10093 vim_free(tbuf);
10094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 }
10096#endif
10097#ifdef MKSESSION_NL
10098 mksession_nl = FALSE;
10099#endif
10100 }
10101
10102#ifdef FEAT_BROWSE
10103theend:
10104 vim_free(browseFile);
10105#endif
10106#ifdef FEAT_SESSION
10107 vim_free(viewFile);
10108#endif
10109}
10110
Bram Moolenaardf177f62005-02-22 08:39:57 +000010111#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10112 || defined(PROTO)
10113 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010114vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010115{
10116 if (vim_mkdir(name, prot) != 0)
10117 {
10118 EMSG2(_("E739: Cannot create directory: %s"), name);
10119 return FAIL;
10120 }
10121 return OK;
10122}
10123#endif
10124
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125/*
10126 * Open a file for writing for an Ex command, with some checks.
10127 * Return file descriptor, or NULL on failure.
10128 */
10129 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010130open_exfile(
10131 char_u *fname,
10132 int forceit,
10133 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134{
10135 FILE *fd;
10136
10137#ifdef UNIX
10138 /* with Unix it is possible to open a directory */
10139 if (mch_isdir(fname))
10140 {
10141 EMSG2(_(e_isadir2), fname);
10142 return NULL;
10143 }
10144#endif
10145 if (!forceit && *mode != 'a' && vim_fexists(fname))
10146 {
10147 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10148 return NULL;
10149 }
10150
10151 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10152 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10153
10154 return fd;
10155}
10156
10157/*
10158 * ":mark" and ":k".
10159 */
10160 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010161ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162{
10163 pos_T pos;
10164
10165 if (*eap->arg == NUL) /* No argument? */
10166 EMSG(_(e_argreq));
10167 else if (eap->arg[1] != NUL) /* more than one character? */
10168 EMSG(_(e_trailing));
10169 else
10170 {
10171 pos = curwin->w_cursor; /* save curwin->w_cursor */
10172 curwin->w_cursor.lnum = eap->line2;
10173 beginline(BL_WHITE | BL_FIX);
10174 if (setmark(*eap->arg) == FAIL) /* set mark */
10175 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10176 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10177 }
10178}
10179
10180/*
10181 * Update w_topline, w_leftcol and the cursor position.
10182 */
10183 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010184update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185{
10186 check_cursor(); /* put cursor on valid line */
10187 update_topline();
10188 if (!curwin->w_p_wrap)
10189 validate_cursor();
10190 update_curswant();
10191}
10192
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010194 * Save the current State and go to Normal mode.
10195 * Return TRUE if the typeahead could be saved.
10196 */
10197 int
10198save_current_state(save_state_T *sst)
10199{
10200 sst->save_msg_scroll = msg_scroll;
10201 sst->save_restart_edit = restart_edit;
10202 sst->save_msg_didout = msg_didout;
10203 sst->save_State = State;
10204 sst->save_insertmode = p_im;
10205 sst->save_finish_op = finish_op;
10206 sst->save_opcount = opcount;
10207
10208 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10209 restart_edit = 0; /* don't go to Insert mode */
10210 p_im = FALSE; /* don't use 'insertmode' */
10211
10212 /*
10213 * Save the current typeahead. This is required to allow using ":normal"
10214 * from an event handler and makes sure we don't hang when the argument
10215 * ends with half a command.
10216 */
10217 save_typeahead(&sst->tabuf);
10218 return sst->tabuf.typebuf_valid;
10219}
10220
10221 void
10222restore_current_state(save_state_T *sst)
10223{
10224 /* Restore the previous typeahead. */
10225 restore_typeahead(&sst->tabuf);
10226
10227 msg_scroll = sst->save_msg_scroll;
10228 restart_edit = sst->save_restart_edit;
10229 p_im = sst->save_insertmode;
10230 finish_op = sst->save_finish_op;
10231 opcount = sst->save_opcount;
10232 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10233
10234 /* Restore the state (needed when called from a function executed for
10235 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10236 State = sst->save_State;
10237#ifdef CURSOR_SHAPE
10238 ui_cursor_shape(); /* may show different cursor shape */
10239#endif
10240}
10241
10242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 * ":normal[!] {commands}": Execute normal mode commands.
10244 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010245 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010246ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010248 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249#ifdef FEAT_MBYTE
10250 char_u *arg = NULL;
10251 int l;
10252 char_u *p;
10253#endif
10254
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010255 if (ex_normal_lock > 0)
10256 {
10257 EMSG(_(e_secure));
10258 return;
10259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 if (ex_normal_busy >= p_mmd)
10261 {
10262 EMSG(_("E192: Recursive use of :normal too deep"));
10263 return;
10264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265
10266#ifdef FEAT_MBYTE
10267 /*
10268 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10269 * this for the K_SPECIAL leading byte, otherwise special keys will not
10270 * work.
10271 */
10272 if (has_mbyte)
10273 {
10274 int len = 0;
10275
10276 /* Count the number of characters to be escaped. */
10277 for (p = eap->arg; *p != NUL; ++p)
10278 {
10279# ifdef FEAT_GUI
10280 if (*p == CSI) /* leadbyte CSI */
10281 len += 2;
10282# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010283 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10285# ifdef FEAT_GUI
10286 || *p == CSI
10287# endif
10288 )
10289 len += 2;
10290 }
10291 if (len > 0)
10292 {
10293 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10294 if (arg != NULL)
10295 {
10296 len = 0;
10297 for (p = eap->arg; *p != NUL; ++p)
10298 {
10299 arg[len++] = *p;
10300# ifdef FEAT_GUI
10301 if (*p == CSI)
10302 {
10303 arg[len++] = KS_EXTRA;
10304 arg[len++] = (int)KE_CSI;
10305 }
10306# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010307 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 {
10309 arg[len++] = *++p;
10310 if (*p == K_SPECIAL)
10311 {
10312 arg[len++] = KS_SPECIAL;
10313 arg[len++] = KE_FILLER;
10314 }
10315# ifdef FEAT_GUI
10316 else if (*p == CSI)
10317 {
10318 arg[len++] = KS_EXTRA;
10319 arg[len++] = (int)KE_CSI;
10320 }
10321# endif
10322 }
10323 arg[len] = NUL;
10324 }
10325 }
10326 }
10327 }
10328#endif
10329
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010330 ++ex_normal_busy;
10331 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 {
10333 /*
10334 * Repeat the :normal command for each line in the range. When no
10335 * range given, execute it just once, without positioning the cursor
10336 * first.
10337 */
10338 do
10339 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 if (eap->addr_count != 0)
10341 {
10342 curwin->w_cursor.lnum = eap->line1++;
10343 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010344 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 }
10346
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010347 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348#ifdef FEAT_MBYTE
10349 arg != NULL ? arg :
10350#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010351 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 }
10353 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10354 }
10355
10356 /* Might not return to the main loop when in an event handler. */
10357 update_topline_cursor();
10358
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010359 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010361#ifdef FEAT_MOUSE
10362 setmouse();
10363#endif
10364#ifdef CURSOR_SHAPE
10365 ui_cursor_shape(); /* may show different cursor shape */
10366#endif
10367
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368#ifdef FEAT_MBYTE
10369 vim_free(arg);
10370#endif
10371}
10372
10373/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010374 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 */
10376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010377ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010379 if (eap->forceit)
10380 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010381 /* cursor line can be zero on startup */
10382 if (!curwin->w_cursor.lnum)
10383 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010384 coladvance((colnr_T)MAXCOL);
10385 curwin->w_curswant = MAXCOL;
10386 curwin->w_set_curswant = FALSE;
10387 }
10388
Bram Moolenaara40c5002005-01-09 21:16:21 +000010389 /* Ignore the command when already in Insert mode. Inserting an
10390 * expression register that invokes a function can do this. */
10391 if (State & INSERT)
10392 return;
10393
Bram Moolenaar64969662005-12-14 21:59:55 +000010394 if (eap->cmdidx == CMD_startinsert)
10395 restart_edit = 'a';
10396 else if (eap->cmdidx == CMD_startreplace)
10397 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010399 restart_edit = 'V';
10400
10401 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010403 if (eap->cmdidx == CMD_startinsert)
10404 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 curwin->w_curswant = 0; /* avoid MAXCOL */
10406 }
10407}
10408
10409/*
10410 * ":stopinsert"
10411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010413ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414{
10415 restart_edit = 0;
10416 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010417 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010420/*
10421 * Execute normal mode command "cmd".
10422 * "remap" can be REMAP_NONE or REMAP_YES.
10423 */
10424 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010425exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010426{
Bram Moolenaar25281632016-01-21 23:32:32 +010010427 /* Stuff the argument into the typeahead buffer. */
10428 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010429 exec_normal(FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010430}
Bram Moolenaar25281632016-01-21 23:32:32 +010010431
Bram Moolenaar25281632016-01-21 23:32:32 +010010432/*
10433 * Execute normal_cmd() until there is no typeahead left.
10434 */
10435 void
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010436exec_normal(int was_typed, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010437{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010438 oparg_T oa;
10439
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010440 clear_oparg(&oa);
10441 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010442 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10443 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010444 {
10445 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010446#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010447 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010448 && oa.op_type == OP_NOP && oa.regname == NUL
10449 && !VIsual_active)
10450 {
10451 /* If terminal_loop() returns OK we got a key that is handled
10452 * in Normal model. With FAIL we first need to position the
10453 * cursor and the screen needs to be redrawn. */
10454 if (terminal_loop(TRUE) == OK)
10455 normal_cmd(&oa, TRUE);
10456 }
10457 else
10458#endif
10459 /* execute a Normal mode cmd */
10460 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010461 }
10462}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010463
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464#ifdef FEAT_FIND_ID
10465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010466ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467{
10468 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10469 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10470 (linenr_T)1, (linenr_T)MAXLNUM);
10471}
10472
Bram Moolenaar4033c552017-09-16 20:54:51 +020010473#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474/*
10475 * ":psearch"
10476 */
10477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010478ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479{
10480 g_do_tagpreview = p_pvh;
10481 ex_findpat(eap);
10482 g_do_tagpreview = 0;
10483}
10484#endif
10485
10486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010487ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488{
10489 int whole = TRUE;
10490 long n;
10491 char_u *p;
10492 int action;
10493
10494 switch (cmdnames[eap->cmdidx].cmd_name[2])
10495 {
10496 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10497 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10498 action = ACTION_GOTO;
10499 else
10500 action = ACTION_SHOW;
10501 break;
10502 case 'i': /* ":ilist" and ":dlist" */
10503 action = ACTION_SHOW_ALL;
10504 break;
10505 case 'u': /* ":ijump" and ":djump" */
10506 action = ACTION_GOTO;
10507 break;
10508 default: /* ":isplit" and ":dsplit" */
10509 action = ACTION_SPLIT;
10510 break;
10511 }
10512
10513 n = 1;
10514 if (vim_isdigit(*eap->arg)) /* get count */
10515 {
10516 n = getdigits(&eap->arg);
10517 eap->arg = skipwhite(eap->arg);
10518 }
10519 if (*eap->arg == '/') /* Match regexp, not just whole words */
10520 {
10521 whole = FALSE;
10522 ++eap->arg;
10523 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10524 if (*p)
10525 {
10526 *p++ = NUL;
10527 p = skipwhite(p);
10528
10529 /* Check for trailing illegal characters */
10530 if (!ends_excmd(*p))
10531 eap->errmsg = e_trailing;
10532 else
10533 eap->nextcmd = check_nextcmd(p);
10534 }
10535 }
10536 if (!eap->skip)
10537 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10538 whole, !eap->forceit,
10539 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10540 n, action, eap->line1, eap->line2);
10541}
10542#endif
10543
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544
Bram Moolenaar4033c552017-09-16 20:54:51 +020010545#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546/*
10547 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10548 */
10549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010550ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010552 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10554}
10555
10556/*
10557 * ":pedit"
10558 */
10559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010560ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561{
10562 win_T *curwin_save = curwin;
10563
10564 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010565 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010566 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 do_exedit(eap, NULL);
10568 keep_help_flag = FALSE;
10569 if (curwin != curwin_save && win_valid(curwin_save))
10570 {
10571 /* Return cursor to where we were */
10572 validate_cursor();
10573 redraw_later(VALID);
10574 win_enter(curwin_save, TRUE);
10575 }
10576 g_do_tagpreview = 0;
10577}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579
10580/*
10581 * ":stag", ":stselect" and ":stjump".
10582 */
10583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010584ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585{
10586 postponed_split = -1;
10587 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010588 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10590 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010591 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593
10594/*
10595 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10596 */
10597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010598ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599{
10600 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10601}
10602
10603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010604ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605{
10606 int cmd;
10607
10608 switch (name[1])
10609 {
10610 case 'j': cmd = DT_JUMP; /* ":tjump" */
10611 break;
10612 case 's': cmd = DT_SELECT; /* ":tselect" */
10613 break;
10614 case 'p': cmd = DT_PREV; /* ":tprevious" */
10615 break;
10616 case 'N': cmd = DT_PREV; /* ":tNext" */
10617 break;
10618 case 'n': cmd = DT_NEXT; /* ":tnext" */
10619 break;
10620 case 'o': cmd = DT_POP; /* ":pop" */
10621 break;
10622 case 'f': /* ":tfirst" */
10623 case 'r': cmd = DT_FIRST; /* ":trewind" */
10624 break;
10625 case 'l': cmd = DT_LAST; /* ":tlast" */
10626 break;
10627 default: /* ":tag" */
10628#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010629 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010631 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 return;
10633 }
10634#endif
10635 cmd = DT_TAG;
10636 break;
10637 }
10638
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010639 if (name[0] == 'l')
10640 {
10641#ifndef FEAT_QUICKFIX
10642 ex_ni(eap);
10643 return;
10644#else
10645 cmd = DT_LTAG;
10646#endif
10647 }
10648
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10650 eap->forceit, TRUE);
10651}
10652
10653/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010654 * Check "str" for starting with a special cmdline variable.
10655 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10656 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10657 */
10658 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010659find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010660{
10661 int len;
10662 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010663 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010664 "%",
10665#define SPEC_PERC 0
10666 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010667#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010668 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010669#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010670 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010671#define SPEC_CCWORD (SPEC_CWORD + 1)
10672 "<cexpr>", /* expr under cursor */
10673#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010674 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010675#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010676 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010677#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010678 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010679#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010680 "<afile>", /* autocommand file name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010681#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010682 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010683#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010684 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010685#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010686#ifdef FEAT_CLIENTSERVER
10687 "<client>"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010688# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010689#endif
10690 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010691
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010692 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010693 {
10694 len = (int)STRLEN(spec_str[i]);
10695 if (STRNCMP(src, spec_str[i], len) == 0)
10696 {
10697 *usedlen = len;
10698 return i;
10699 }
10700 }
10701 return -1;
10702}
10703
10704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 * Evaluate cmdline variables.
10706 *
10707 * change '%' to curbuf->b_ffname
10708 * '#' to curwin->w_altfile
10709 * '<cword>' to word under the cursor
10710 * '<cWORD>' to WORD under the cursor
10711 * '<cfile>' to path name under the cursor
10712 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010713 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 * '<afile>' to file name for autocommand
10715 * '<abuf>' to buffer number for autocommand
10716 * '<amatch>' to matching name for autocommand
10717 *
10718 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10719 * "" for error without a message) and NULL is returned.
10720 * Returns an allocated string if a valid match was found.
10721 * Returns NULL if no match was found. "usedlen" then still contains the
10722 * number of characters to skip.
10723 */
10724 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010725eval_vars(
10726 char_u *src, /* pointer into commandline */
10727 char_u *srcstart, /* beginning of valid memory for src */
10728 int *usedlen, /* characters after src that are used */
10729 linenr_T *lnump, /* line number for :e command, or NULL */
10730 char_u **errormsg, /* pointer to error message */
10731 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010732 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733{
10734 int i;
10735 char_u *s;
10736 char_u *result;
10737 char_u *resultbuf = NULL;
10738 int resultlen;
10739 buf_T *buf;
10740 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10741 int spec_idx;
10742#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010743 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 int skip_mod = FALSE;
10745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747
10748 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010749 if (escaped != NULL)
10750 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751
10752 /*
10753 * Check if there is something to do.
10754 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010755 spec_idx = find_cmdline_var(src, usedlen);
10756 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 {
10758 *usedlen = 1;
10759 return NULL;
10760 }
10761
10762 /*
10763 * Skip when preceded with a backslash "\%" and "\#".
10764 * Note: In "\\%" the % is also not recognized!
10765 */
10766 if (src > srcstart && src[-1] == '\\')
10767 {
10768 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010769 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 return NULL;
10771 }
10772
10773 /*
10774 * word or WORD under cursor
10775 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010776 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10777 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010779 resultlen = find_ident_under_cursor(&result,
10780 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10781 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10782 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 if (resultlen == 0)
10784 {
10785 *errormsg = (char_u *)"";
10786 return NULL;
10787 }
10788 }
10789
10790 /*
10791 * '#': Alternate file name
10792 * '%': Current file name
10793 * File name under the cursor
10794 * File name for autocommand
10795 * and following modifiers
10796 */
10797 else
10798 {
10799 switch (spec_idx)
10800 {
10801 case SPEC_PERC: /* '%': current file */
10802 if (curbuf->b_fname == NULL)
10803 {
10804 result = (char_u *)"";
10805 valid = 0; /* Must have ":p:h" to be valid */
10806 }
10807 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010808 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010810#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010811 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010812#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 break;
10815
10816 case SPEC_HASH: /* '#' or "#99": alternate file */
10817 if (src[1] == '#') /* "##": the argument list */
10818 {
10819 result = arg_all();
10820 resultbuf = result;
10821 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010822 if (escaped != NULL)
10823 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824#ifdef FEAT_MODIFY_FNAME
10825 skip_mod = TRUE;
10826#endif
10827 break;
10828 }
10829 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010830 if (*s == '<') /* "#<99" uses v:oldfiles */
10831 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010833 if (s == src + 2 && src[1] == '-')
10834 /* just a minus sign, don't skip over it */
10835 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 *usedlen = (int)(s - src); /* length of what we expand */
10837
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010838 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010840 if (*usedlen < 2)
10841 {
10842 /* Should we give an error message for #<text? */
10843 *usedlen = 1;
10844 return NULL;
10845 }
10846#ifdef FEAT_EVAL
10847 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10848 (long)i);
10849 if (result == NULL)
10850 {
10851 *errormsg = (char_u *)"";
10852 return NULL;
10853 }
10854#else
10855 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 }
10859 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010860 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010861 if (i == 0 && src[1] == '<' && *usedlen > 1)
10862 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010863 buf = buflist_findnr(i);
10864 if (buf == NULL)
10865 {
10866 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10867 return NULL;
10868 }
10869 if (lnump != NULL)
10870 *lnump = ECMD_LAST;
10871 if (buf->b_fname == NULL)
10872 {
10873 result = (char_u *)"";
10874 valid = 0; /* Must have ":p:h" to be valid */
10875 }
10876 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010877 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010878 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010879#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010880 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010881#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010882 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 break;
10885
10886#ifdef FEAT_SEARCHPATH
10887 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010888 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 if (result == NULL)
10890 {
10891 *errormsg = (char_u *)"";
10892 return NULL;
10893 }
10894 resultbuf = result; /* remember allocated string */
10895 break;
10896#endif
10897
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 case SPEC_AFILE: /* file name for autocommand */
10899 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010900 if (result != NULL && !autocmd_fname_full)
10901 {
10902 /* Still need to turn the fname into a full path. It is
10903 * postponed to avoid a delay when <afile> is not used. */
10904 autocmd_fname_full = TRUE;
10905 result = FullName_save(autocmd_fname, FALSE);
10906 vim_free(autocmd_fname);
10907 autocmd_fname = result;
10908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 if (result == NULL)
10910 {
10911 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10912 return NULL;
10913 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010914 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 break;
10916
10917 case SPEC_ABUF: /* buffer number for autocommand */
10918 if (autocmd_bufnr <= 0)
10919 {
10920 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10921 return NULL;
10922 }
10923 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10924 result = strbuf;
10925 break;
10926
10927 case SPEC_AMATCH: /* match name for autocommand */
10928 result = autocmd_match;
10929 if (result == NULL)
10930 {
10931 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10932 return NULL;
10933 }
10934 break;
10935
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 case SPEC_SFILE: /* file name for ":so" command */
10937 result = sourcing_name;
10938 if (result == NULL)
10939 {
10940 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10941 return NULL;
10942 }
10943 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010944 case SPEC_SLNUM: /* line in file for ":so" command */
10945 if (sourcing_name == NULL || sourcing_lnum == 0)
10946 {
10947 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10948 return NULL;
10949 }
10950 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10951 result = strbuf;
10952 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953#if defined(FEAT_CLIENTSERVER)
10954 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010955 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10956 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 result = strbuf;
10958 break;
10959#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010960 default:
10961 result = (char_u *)""; /* avoid gcc warning */
10962 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 }
10964
10965 resultlen = (int)STRLEN(result); /* length of new string */
10966 if (src[*usedlen] == '<') /* remove the file name extension */
10967 {
10968 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 resultlen = (int)(s - result);
10971 }
10972#ifdef FEAT_MODIFY_FNAME
10973 else if (!skip_mod)
10974 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010975 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 &resultlen);
10977 if (result == NULL)
10978 {
10979 *errormsg = (char_u *)"";
10980 return NULL;
10981 }
10982 }
10983#endif
10984 }
10985
10986 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10987 {
10988 if (valid != VALID_HEAD + VALID_PATH)
10989 /* xgettext:no-c-format */
10990 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10991 else
10992 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10993 result = NULL;
10994 }
10995 else
10996 result = vim_strnsave(result, resultlen);
10997 vim_free(resultbuf);
10998 return result;
10999}
11000
11001/*
11002 * Concatenate all files in the argument list, separated by spaces, and return
11003 * it in one allocated string.
11004 * Spaces and backslashes in the file names are escaped with a backslash.
11005 * Returns NULL when out of memory.
11006 */
11007 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011008arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009{
11010 int len;
11011 int idx;
11012 char_u *retval = NULL;
11013 char_u *p;
11014
11015 /*
11016 * Do this loop two times:
11017 * first time: compute the total length
11018 * second time: concatenate the names
11019 */
11020 for (;;)
11021 {
11022 len = 0;
11023 for (idx = 0; idx < ARGCOUNT; ++idx)
11024 {
11025 p = alist_name(&ARGLIST[idx]);
11026 if (p != NULL)
11027 {
11028 if (len > 0)
11029 {
11030 /* insert a space in between names */
11031 if (retval != NULL)
11032 retval[len] = ' ';
11033 ++len;
11034 }
11035 for ( ; *p != NUL; ++p)
11036 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011037 if (*p == ' '
11038#ifndef BACKSLASH_IN_FILENAME
11039 || *p == '\\'
11040#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011041 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042 {
11043 /* insert a backslash */
11044 if (retval != NULL)
11045 retval[len] = '\\';
11046 ++len;
11047 }
11048 if (retval != NULL)
11049 retval[len] = *p;
11050 ++len;
11051 }
11052 }
11053 }
11054
11055 /* second time: break here */
11056 if (retval != NULL)
11057 {
11058 retval[len] = NUL;
11059 break;
11060 }
11061
11062 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011063 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 if (retval == NULL)
11065 break;
11066 }
11067
11068 return retval;
11069}
11070
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071/*
11072 * Expand the <sfile> string in "arg".
11073 *
11074 * Returns an allocated string, or NULL for any error.
11075 */
11076 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011077expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078{
11079 char_u *errormsg;
11080 int len;
11081 char_u *result;
11082 char_u *newres;
11083 char_u *repl;
11084 int srclen;
11085 char_u *p;
11086
11087 result = vim_strsave(arg);
11088 if (result == NULL)
11089 return NULL;
11090
11091 for (p = result; *p; )
11092 {
11093 if (STRNCMP(p, "<sfile>", 7) != 0)
11094 ++p;
11095 else
11096 {
11097 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011098 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 if (errormsg != NULL)
11100 {
11101 if (*errormsg)
11102 emsg(errormsg);
11103 vim_free(result);
11104 return NULL;
11105 }
11106 if (repl == NULL) /* no match (cannot happen) */
11107 {
11108 p += srclen;
11109 continue;
11110 }
11111 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11112 newres = alloc(len);
11113 if (newres == NULL)
11114 {
11115 vim_free(repl);
11116 vim_free(result);
11117 return NULL;
11118 }
11119 mch_memmove(newres, result, (size_t)(p - result));
11120 STRCPY(newres + (p - result), repl);
11121 len = (int)STRLEN(newres);
11122 STRCAT(newres, p + srclen);
11123 vim_free(repl);
11124 vim_free(result);
11125 result = newres;
11126 p = newres + len; /* continue after the match */
11127 }
11128 }
11129
11130 return result;
11131}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132
11133#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011134static int ses_winsizes(FILE *fd, int restore_size,
11135 win_T *tab_firstwin);
11136static int ses_win_rec(FILE *fd, frame_T *fr);
11137static frame_T *ses_skipframe(frame_T *fr);
11138static int ses_do_frame(frame_T *fr);
11139static int ses_do_win(win_T *wp);
11140static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11141static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011142static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143
11144/*
11145 * Write openfile commands for the current buffers to an .exrc file.
11146 * Return FAIL on error, OK otherwise.
11147 */
11148 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011149makeopens(
11150 FILE *fd,
11151 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152{
11153 buf_T *buf;
11154 int only_save_windows = TRUE;
11155 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 int restore_size = TRUE;
11157 win_T *wp;
11158 char_u *sname;
11159 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011160 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011161 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011162 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011163 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011164 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011165 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166
11167 if (ssop_flags & SSOP_BUFFERS)
11168 only_save_windows = FALSE; /* Save ALL buffers */
11169
11170 /*
11171 * Begin by setting the this_session variable, and then other
11172 * sessionable variables.
11173 */
11174#ifdef FEAT_EVAL
11175 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11176 return FAIL;
11177 if (ssop_flags & SSOP_GLOBALS)
11178 if (store_session_globals(fd) == FAIL)
11179 return FAIL;
11180#endif
11181
11182 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011183 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 */
11185 if (put_line(fd, "silent only") == FAIL)
11186 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011187 if ((ssop_flags & SSOP_TABPAGES)
11188 && put_line(fd, "silent tabonly") == FAIL)
11189 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190
11191 /*
11192 * Now a :cd command to the session directory or the current directory
11193 */
11194 if (ssop_flags & SSOP_SESDIR)
11195 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011196 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11197 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 return FAIL;
11199 }
11200 else if (ssop_flags & SSOP_CURDIR)
11201 {
11202 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11203 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011204 || fputs("cd ", fd) < 0
11205 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11206 || put_eol(fd) == FAIL)
11207 {
11208 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 vim_free(sname);
11212 }
11213
11214 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011215 * If there is an empty, unnamed buffer we will wipe it out later.
11216 * Remember the buffer number.
11217 */
11218 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11219 return FAIL;
11220 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11221 return FAIL;
11222 if (put_line(fd, "endif") == FAIL)
11223 return FAIL;
11224
11225 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226 * Now save the current files, current buffer first.
11227 */
11228 if (put_line(fd, "set shortmess=aoO") == FAIL)
11229 return FAIL;
11230
11231 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011232 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 {
11234 if (!(only_save_windows && buf->b_nwindows == 0)
11235 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011236#ifdef FEAT_TERMINAL
11237 /* skip terminal buffers: finished ones are not useful, others
11238 * will be resurrected and result in a new buffer */
11239 && !bt_terminal(buf)
11240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 && buf->b_fname != NULL
11242 && buf->b_p_bl)
11243 {
11244 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11245 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011246 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 return FAIL;
11248 }
11249 }
11250
11251 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011252 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11254 return FAIL;
11255
11256 if (ssop_flags & SSOP_RESIZE)
11257 {
11258 /* Note: after the restore we still check it worked!*/
11259 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11260 || put_eol(fd) == FAIL)
11261 return FAIL;
11262 }
11263
11264#ifdef FEAT_GUI
11265 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11266 {
11267 int x, y;
11268
11269 if (gui_mch_get_winpos(&x, &y) == OK)
11270 {
11271 /* Note: after the restore we still check it worked!*/
11272 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11273 return FAIL;
11274 }
11275 }
11276#endif
11277
11278 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011279 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11280 * will be displayed when creating the next tab. That resizes the windows
11281 * in the first tab, which may cause problems. Set 'showtabline' to 2
11282 * temporarily to avoid that.
11283 */
11284 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11285 {
11286 if (put_line(fd, "set stal=2") == FAIL)
11287 return FAIL;
11288 restore_stal = TRUE;
11289 }
11290
11291 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011292 * May repeat putting Windows for each tab, when "tabpages" is in
11293 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011294 * Don't use goto_tabpage(), it may change directory and trigger
11295 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011297 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011298 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011299 if ((ssop_flags & SSOP_TABPAGES))
11300 {
11301 int num_tabs;
11302
11303 /*
11304 * Similar to ses_win_rec() below, populate the tab pages first so
11305 * later local options won't be copied to the new tabs.
11306 */
11307 for (tabnr = 1; ; ++tabnr)
11308 {
11309 tabpage_T *tp = find_tabpage(tabnr);
11310
11311 if (tp == NULL) /* done all tab pages */
11312 break;
11313
11314 if (tabnr > 1 && put_line(fd, "tabnew") == FAIL)
11315 return FAIL;
11316 }
11317
11318 num_tabs = tabnr - 1;
11319 if (num_tabs > 1 && (fprintf(fd, "tabnext -%d", num_tabs - 1) < 0
11320 || put_eol(fd) == FAIL))
11321 return FAIL;
11322 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011323 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011325 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011326 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011327
Bram Moolenaar18144c82006-04-12 21:52:12 +000011328 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011330 tabpage_T *tp = find_tabpage(tabnr);
11331
11332 if (tp == NULL)
11333 break; /* done all tab pages */
11334 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011335 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011336 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011337 tab_topframe = topframe;
11338 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011339 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011340 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011341 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011342 tab_topframe = tp->tp_topframe;
11343 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011344 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011345 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347
Bram Moolenaar18144c82006-04-12 21:52:12 +000011348 /*
11349 * Before creating the window layout, try loading one file. If this
11350 * is aborted we don't end up with a number of useless windows.
11351 * This may have side effects! (e.g., compressed or network file).
11352 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011353 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011354 {
11355 if (ses_do_win(wp)
11356 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011357 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011358#ifdef FEAT_QUICKFIX
11359 && !bt_nofile(wp->w_buffer)
11360#endif
11361 )
11362 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011363 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11364 return FAIL;
11365 need_tabnext = FALSE;
11366
11367 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011368 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11369 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011370 return FAIL;
11371 if (!wp->w_arg_idx_invalid)
11372 edited_win = wp;
11373 break;
11374 }
11375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011377 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011378 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011379 return FAIL;
11380
Bram Moolenaar18144c82006-04-12 21:52:12 +000011381 /*
11382 * Save current window layout.
11383 */
11384 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011386 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011388 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11389 return FAIL;
11390 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11391 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392
Bram Moolenaar18144c82006-04-12 21:52:12 +000011393 /*
11394 * Check if window sizes can be restored (no windows omitted).
11395 * Remember the window number of the current window after restoring.
11396 */
11397 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011398 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011399 {
11400 if (ses_do_win(wp))
11401 ++nr;
11402 else
11403 restore_size = FALSE;
11404 if (curwin == wp)
11405 cnr = nr;
11406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407
Bram Moolenaar18144c82006-04-12 21:52:12 +000011408 /* Go to the first window. */
11409 if (put_line(fd, "wincmd t") == FAIL)
11410 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011411
Bram Moolenaar18144c82006-04-12 21:52:12 +000011412 /*
11413 * If more than one window, see if sizes can be restored.
11414 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11415 * resized when moving between windows.
11416 * Do this before restoring the view, so that the topline and the
11417 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011418 * winminheight and winminwidth need to be set to avoid an error if the
11419 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011420 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011421 if (put_line(fd, "set winminheight=0") == FAIL
11422 || put_line(fd, "set winheight=1") == FAIL
11423 || put_line(fd, "set winminwidth=0") == FAIL
11424 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011425 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011426 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011427 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428
Bram Moolenaar18144c82006-04-12 21:52:12 +000011429 /*
11430 * Restore the view of the window (options, file, cursor, etc.).
11431 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011432 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011433 {
11434 if (!ses_do_win(wp))
11435 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011436 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11437 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011438 return FAIL;
11439 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11440 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011441 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011442 }
11443
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011444 /* The argument index in the first tab page is zero, need to set it in
11445 * each window. For further tab pages it's the window where we do
11446 * "tabedit". */
11447 cur_arg_idx = next_arg_idx;
11448
Bram Moolenaar18144c82006-04-12 21:52:12 +000011449 /*
11450 * Restore cursor to the current window if it's not the first one.
11451 */
11452 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11453 || put_eol(fd) == FAIL))
11454 return FAIL;
11455
11456 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011457 * Restore window sizes again after jumping around in windows, because
11458 * the current window has a minimum size while others may not.
11459 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011460 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011461 return FAIL;
11462
Bram Moolenaar18144c82006-04-12 21:52:12 +000011463 /* Don't continue in another tab page when doing only the current one
11464 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011465 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011466 break;
11467 }
11468
11469 if (ssop_flags & SSOP_TABPAGES)
11470 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011471 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11472 || put_eol(fd) == FAIL)
11473 return FAIL;
11474 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011475 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11476 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011477
Bram Moolenaar9c102382006-05-03 21:26:49 +000011478 /*
11479 * Wipe out an empty unnamed buffer we started in.
11480 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011481 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011482 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011483 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011484 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011485 return FAIL;
11486 if (put_line(fd, "endif") == FAIL)
11487 return FAIL;
11488 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11489 return FAIL;
11490
11491 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11492 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11493 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11494 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011495 /* Re-apply 'winminheight' and 'winminwidth'. */
11496 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11497 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11498 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499
11500 /*
11501 * Lastly, execute the x.vim file if it exists.
11502 */
11503 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11504 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011505 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506 || put_line(fd, "endif") == FAIL)
11507 return FAIL;
11508
11509 return OK;
11510}
11511
11512 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011513ses_winsizes(
11514 FILE *fd,
11515 int restore_size,
11516 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517{
11518 int n = 0;
11519 win_T *wp;
11520
11521 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11522 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011523 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 {
11525 if (!ses_do_win(wp))
11526 continue;
11527 ++n;
11528
11529 /* restore height when not full height */
11530 if (wp->w_height + wp->w_status_height < topframe->fr_height
11531 && (fprintf(fd,
11532 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11533 n, (long)wp->w_height, Rows / 2, Rows) < 0
11534 || put_eol(fd) == FAIL))
11535 return FAIL;
11536
11537 /* restore width when not full width */
11538 if (wp->w_width < Columns && (fprintf(fd,
11539 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11540 n, (long)wp->w_width, Columns / 2, Columns) < 0
11541 || put_eol(fd) == FAIL))
11542 return FAIL;
11543 }
11544 }
11545 else
11546 {
11547 /* Just equalise window sizes */
11548 if (put_line(fd, "wincmd =") == FAIL)
11549 return FAIL;
11550 }
11551 return OK;
11552}
11553
11554/*
11555 * Write commands to "fd" to recursively create windows for frame "fr",
11556 * horizontally and vertically split.
11557 * After the commands the last window in the frame is the current window.
11558 * Returns FAIL when writing the commands to "fd" fails.
11559 */
11560 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011561ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562{
11563 frame_T *frc;
11564 int count = 0;
11565
11566 if (fr->fr_layout != FR_LEAF)
11567 {
11568 /* Find first frame that's not skipped and then create a window for
11569 * each following one (first frame is already there). */
11570 frc = ses_skipframe(fr->fr_child);
11571 if (frc != NULL)
11572 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11573 {
11574 /* Make window as big as possible so that we have lots of room
11575 * to split. */
11576 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11577 || put_line(fd, fr->fr_layout == FR_COL
11578 ? "split" : "vsplit") == FAIL)
11579 return FAIL;
11580 ++count;
11581 }
11582
11583 /* Go back to the first window. */
11584 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11585 ? "%dwincmd k" : "%dwincmd h", count) < 0
11586 || put_eol(fd) == FAIL))
11587 return FAIL;
11588
11589 /* Recursively create frames/windows in each window of this column or
11590 * row. */
11591 frc = ses_skipframe(fr->fr_child);
11592 while (frc != NULL)
11593 {
11594 ses_win_rec(fd, frc);
11595 frc = ses_skipframe(frc->fr_next);
11596 /* Go to next window. */
11597 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11598 return FAIL;
11599 }
11600 }
11601 return OK;
11602}
11603
11604/*
11605 * Skip frames that don't contain windows we want to save in the Session.
11606 * Returns NULL when there none.
11607 */
11608 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011609ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610{
11611 frame_T *frc;
11612
11613 for (frc = fr; frc != NULL; frc = frc->fr_next)
11614 if (ses_do_frame(frc))
11615 break;
11616 return frc;
11617}
11618
11619/*
11620 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11621 * the Session.
11622 */
11623 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011624ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625{
11626 frame_T *frc;
11627
11628 if (fr->fr_layout == FR_LEAF)
11629 return ses_do_win(fr->fr_win);
11630 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11631 if (ses_do_frame(frc))
11632 return TRUE;
11633 return FALSE;
11634}
11635
11636/*
11637 * Return non-zero if window "wp" is to be stored in the Session.
11638 */
11639 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011640ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011642#ifdef FEAT_TERMINAL
11643 if (bt_terminal(wp->w_buffer))
11644 return !term_is_finished(wp->w_buffer)
11645 && (ssop_flags & SSOP_TERMINAL)
11646 && term_should_restore(wp->w_buffer);
11647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648 if (wp->w_buffer->b_fname == NULL
11649#ifdef FEAT_QUICKFIX
11650 /* When 'buftype' is "nofile" can't restore the window contents. */
11651 || bt_nofile(wp->w_buffer)
11652#endif
11653 )
11654 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011655 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 return (ssop_flags & SSOP_HELP);
11657 return TRUE;
11658}
11659
11660/*
11661 * Write commands to "fd" to restore the view of a window.
11662 * Caller must make sure 'scrolloff' is zero.
11663 */
11664 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011665put_view(
11666 FILE *fd,
11667 win_T *wp,
11668 int add_edit, /* add ":edit" command to view */
11669 unsigned *flagp, /* vop_flags or ssop_flags */
11670 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011671 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672{
11673 win_T *save_curwin;
11674 int f;
11675 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011676 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677
11678 /* Always restore cursor position for ":mksession". For ":mkview" only
11679 * when 'viewoptions' contains "cursor". */
11680 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11681
11682 /*
11683 * Local argument list.
11684 */
11685 if (wp->w_alist == &global_alist)
11686 {
11687 if (put_line(fd, "argglobal") == FAIL)
11688 return FAIL;
11689 }
11690 else
11691 {
11692 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11693 flagp == &vop_flags
11694 || !(*flagp & SSOP_CURDIR)
11695 || wp->w_localdir != NULL, flagp) == FAIL)
11696 return FAIL;
11697 }
11698
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011699 /* Only when part of a session: restore the argument index. Some
11700 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011701 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011702 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011704 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 || put_eol(fd) == FAIL)
11706 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011707 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708 }
11709
11710 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011711 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011713# ifdef FEAT_TERMINAL
11714 if (bt_terminal(wp->w_buffer))
11715 {
11716 if (term_write_session(fd, wp) == FAIL)
11717 return FAIL;
11718 }
11719 else
11720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721 /*
11722 * Load the file.
11723 */
11724 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011725# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011727# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 )
11729 {
11730 /*
11731 * Editing a file in this buffer: use ":edit file".
11732 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011733 *
11734 * Note, if a buffer for that file already exists, use :badd to
11735 * edit that buffer, to not lose folding information (:edit resets
11736 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011738 if (fputs("if bufexists('", fd) < 0
11739 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11740 || fputs("') | buffer ", fd) < 0
11741 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11742 || fputs(" | else | edit ", fd) < 0
11743 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11744 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011745 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746 return FAIL;
11747 }
11748 else
11749 {
11750 /* No file in this buffer, just make it empty. */
11751 if (put_line(fd, "enew") == FAIL)
11752 return FAIL;
11753#ifdef FEAT_QUICKFIX
11754 if (wp->w_buffer->b_ffname != NULL)
11755 {
11756 /* The buffer does have a name, but it's not a file name. */
11757 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011758 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 return FAIL;
11760 }
11761#endif
11762 do_cursor = FALSE;
11763 }
11764 }
11765
11766 /*
11767 * Local mappings and abbreviations.
11768 */
11769 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11770 && makemap(fd, wp->w_buffer) == FAIL)
11771 return FAIL;
11772
11773 /*
11774 * Local options. Need to go to the window temporarily.
11775 * Store only local values when using ":mkview" and when ":mksession" is
11776 * used and 'sessionoptions' doesn't include "options".
11777 * Some folding options are always stored when "folds" is included,
11778 * otherwise the folds would not be restored correctly.
11779 */
11780 save_curwin = curwin;
11781 curwin = wp;
11782 curbuf = curwin->w_buffer;
11783 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11784 f = makeset(fd, OPT_LOCAL,
11785 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11786#ifdef FEAT_FOLDING
11787 else if (*flagp & SSOP_FOLDS)
11788 f = makefoldset(fd);
11789#endif
11790 else
11791 f = OK;
11792 curwin = save_curwin;
11793 curbuf = curwin->w_buffer;
11794 if (f == FAIL)
11795 return FAIL;
11796
11797#ifdef FEAT_FOLDING
11798 /*
11799 * Save Folds when 'buftype' is empty and for help files.
11800 */
11801 if ((*flagp & SSOP_FOLDS)
11802 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011803 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804 {
11805 if (put_folds(fd, wp) == FAIL)
11806 return FAIL;
11807 }
11808#endif
11809
11810 /*
11811 * Set the cursor after creating folds, since that moves the cursor.
11812 */
11813 if (do_cursor)
11814 {
11815
11816 /* Restore the cursor line in the file and relatively in the
11817 * window. Don't use "G", it changes the jumplist. */
11818 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11819 (long)wp->w_cursor.lnum,
11820 (long)(wp->w_cursor.lnum - wp->w_topline),
11821 (long)wp->w_height / 2, (long)wp->w_height) < 0
11822 || put_eol(fd) == FAIL
11823 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11824 || put_line(fd, "exe s:l") == FAIL
11825 || put_line(fd, "normal! zt") == FAIL
11826 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11827 || put_eol(fd) == FAIL)
11828 return FAIL;
11829 /* Restore the cursor column and left offset when not wrapping. */
11830 if (wp->w_cursor.col == 0)
11831 {
11832 if (put_line(fd, "normal! 0") == FAIL)
11833 return FAIL;
11834 }
11835 else
11836 {
11837 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11838 {
11839 if (fprintf(fd,
11840 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011841 (long)wp->w_virtcol + 1,
11842 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 (long)wp->w_width / 2, (long)wp->w_width) < 0
11844 || put_eol(fd) == FAIL
11845 || put_line(fd, "if s:c > 0") == FAIL
11846 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011847 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11848 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 || put_eol(fd) == FAIL
11850 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011851 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 || put_eol(fd) == FAIL
11853 || put_line(fd, "endif") == FAIL)
11854 return FAIL;
11855 }
11856 else
11857 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011858 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859 || put_eol(fd) == FAIL)
11860 return FAIL;
11861 }
11862 }
11863 }
11864
11865 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011866 * Local directory, if the current flag is not view options or the "curdir"
11867 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011869 if (wp->w_localdir != NULL
11870 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871 {
11872 if (fputs("lcd ", fd) < 0
11873 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11874 || put_eol(fd) == FAIL)
11875 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011876 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 }
11878
11879 return OK;
11880}
11881
11882/*
11883 * Write an argument list to the session file.
11884 * Returns FAIL if writing fails.
11885 */
11886 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011887ses_arglist(
11888 FILE *fd,
11889 char *cmd,
11890 garray_T *gap,
11891 int fullname, /* TRUE: use full path name */
11892 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893{
11894 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011895 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 char_u *s;
11897
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011898 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11899 return FAIL;
11900 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901 return FAIL;
11902 for (i = 0; i < gap->ga_len; ++i)
11903 {
11904 /* NULL file names are skipped (only happens when out of memory). */
11905 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11906 if (s != NULL)
11907 {
11908 if (fullname)
11909 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011910 buf = alloc(MAXPATHL);
11911 if (buf != NULL)
11912 {
11913 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11914 s = buf;
11915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011917 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011918 || ses_put_fname(fd, s, flagp) == FAIL
11919 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011920 {
11921 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011923 }
11924 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925 }
11926 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011927 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928}
11929
11930/*
11931 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011932 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933 * Returns FAIL if writing fails.
11934 */
11935 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011936ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937{
11938 char_u *name;
11939
11940 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011941 * the session file will be sourced.
11942 * Don't do this for ":mkview", we don't know the current directory.
11943 * Don't do this after ":lcd", we don't keep track of what the current
11944 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945 if (buf->b_sfname != NULL
11946 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011947 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011948#ifdef FEAT_AUTOCHDIR
11949 && !p_acd
11950#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011951 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952 name = buf->b_sfname;
11953 else
11954 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011955 if (ses_put_fname(fd, name, flagp) == FAIL
11956 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957 return FAIL;
11958 return OK;
11959}
11960
11961/*
11962 * Write a file name to the session file.
11963 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11964 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011965 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 */
11967 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011968ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969{
11970 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011971 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973
11974 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011975 if (sname == NULL)
11976 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011978 if (*flagp & SSOP_SLASH)
11979 {
11980 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011981 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011982 if (*p == '\\')
11983 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011985
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011986 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011987 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011989 if (p == NULL)
11990 return FAIL;
11991
11992 /* write the result */
11993 if (fputs((char *)p, fd) < 0)
11994 retval = FAIL;
11995
11996 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 return retval;
11998}
11999
12000/*
12001 * ":loadview [nr]"
12002 */
12003 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012004ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005{
12006 char_u *fname;
12007
12008 fname = get_view_file(*eap->arg);
12009 if (fname != NULL)
12010 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012011 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012 vim_free(fname);
12013 }
12014}
12015
12016/*
12017 * Get the name of the view file for the current buffer.
12018 */
12019 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012020get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021{
12022 int len = 0;
12023 char_u *p, *s;
12024 char_u *retval;
12025 char_u *sname;
12026
12027 if (curbuf->b_ffname == NULL)
12028 {
12029 EMSG(_(e_noname));
12030 return NULL;
12031 }
12032 sname = home_replace_save(NULL, curbuf->b_ffname);
12033 if (sname == NULL)
12034 return NULL;
12035
12036 /*
12037 * We want a file name without separators, because we're not going to make
12038 * a directory.
12039 * "normal" path separator -> "=+"
12040 * "=" -> "=="
12041 * ":" path separator -> "=-"
12042 */
12043 for (p = sname; *p; ++p)
12044 if (*p == '=' || vim_ispathsep(*p))
12045 ++len;
12046 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12047 if (retval != NULL)
12048 {
12049 STRCPY(retval, p_vdir);
12050 add_pathsep(retval);
12051 s = retval + STRLEN(retval);
12052 for (p = sname; *p; ++p)
12053 {
12054 if (*p == '=')
12055 {
12056 *s++ = '=';
12057 *s++ = '=';
12058 }
12059 else if (vim_ispathsep(*p))
12060 {
12061 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012062#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063 if (*p == ':')
12064 *s++ = '-';
12065 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012067 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068 }
12069 else
12070 *s++ = *p;
12071 }
12072 *s++ = '=';
12073 *s++ = c;
12074 STRCPY(s, ".vim");
12075 }
12076
12077 vim_free(sname);
12078 return retval;
12079}
12080
12081#endif /* FEAT_SESSION */
12082
12083/*
12084 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12085 * Return FAIL for a write error.
12086 */
12087 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012088put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089{
12090 if (
12091#ifdef USE_CRNL
12092 (
12093# ifdef MKSESSION_NL
12094 !mksession_nl &&
12095# endif
12096 (putc('\r', fd) < 0)) ||
12097#endif
12098 (putc('\n', fd) < 0))
12099 return FAIL;
12100 return OK;
12101}
12102
12103/*
12104 * Write a line to "fd".
12105 * Return FAIL for a write error.
12106 */
12107 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012108put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109{
12110 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12111 return FAIL;
12112 return OK;
12113}
12114
12115#ifdef FEAT_VIMINFO
12116/*
12117 * ":rviminfo" and ":wviminfo".
12118 */
12119 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012120ex_viminfo(
12121 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122{
12123 char_u *save_viminfo;
12124
12125 save_viminfo = p_viminfo;
12126 if (*p_viminfo == NUL)
12127 p_viminfo = (char_u *)"'100";
12128 if (eap->cmdidx == CMD_rviminfo)
12129 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012130 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12131 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 EMSG(_("E195: Cannot open viminfo file for reading"));
12133 }
12134 else
12135 write_viminfo(eap->arg, eap->forceit);
12136 p_viminfo = save_viminfo;
12137}
12138#endif
12139
12140#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012141/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012142 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012143 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012146dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148 if (fname == NULL)
12149 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012150 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151}
12152#endif
12153
12154/*
12155 * ":behave {mswin,xterm}"
12156 */
12157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012158ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159{
12160 if (STRCMP(eap->arg, "mswin") == 0)
12161 {
12162 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12163 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12164 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12165 set_option_value((char_u *)"keymodel", 0L,
12166 (char_u *)"startsel,stopsel", 0);
12167 }
12168 else if (STRCMP(eap->arg, "xterm") == 0)
12169 {
12170 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12171 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12172 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12173 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12174 }
12175 else
12176 EMSG2(_(e_invarg2), eap->arg);
12177}
12178
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012179#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12180/*
12181 * Function given to ExpandGeneric() to obtain the possible arguments of the
12182 * ":behave {mswin,xterm}" command.
12183 */
12184 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012185get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012186{
12187 if (idx == 0)
12188 return (char_u *)"mswin";
12189 if (idx == 1)
12190 return (char_u *)"xterm";
12191 return NULL;
12192}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012193
12194/*
12195 * Function given to ExpandGeneric() to obtain the possible arguments of the
12196 * ":messages {clear}" command.
12197 */
12198 char_u *
12199get_messages_arg(expand_T *xp UNUSED, int idx)
12200{
12201 if (idx == 0)
12202 return (char_u *)"clear";
12203 return NULL;
12204}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012205#endif
12206
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012207 char_u *
12208get_mapclear_arg(expand_T *xp UNUSED, int idx)
12209{
12210 if (idx == 0)
12211 return (char_u *)"<buffer>";
12212 return NULL;
12213}
12214
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215static int filetype_detect = FALSE;
12216static int filetype_plugin = FALSE;
12217static int filetype_indent = FALSE;
12218
12219/*
12220 * ":filetype [plugin] [indent] {on,off,detect}"
12221 * on: Load the filetype.vim file to install autocommands for file types.
12222 * off: Load the ftoff.vim file to remove all autocommands for file types.
12223 * plugin on: load filetype.vim and ftplugin.vim
12224 * plugin off: load ftplugof.vim
12225 * indent on: load filetype.vim and indent.vim
12226 * indent off: load indoff.vim
12227 */
12228 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012229ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230{
12231 char_u *arg = eap->arg;
12232 int plugin = FALSE;
12233 int indent = FALSE;
12234
12235 if (*eap->arg == NUL)
12236 {
12237 /* Print current status. */
12238 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12239 filetype_detect ? "ON" : "OFF",
12240 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12241 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12242 return;
12243 }
12244
12245 /* Accept "plugin" and "indent" in any order. */
12246 for (;;)
12247 {
12248 if (STRNCMP(arg, "plugin", 6) == 0)
12249 {
12250 plugin = TRUE;
12251 arg = skipwhite(arg + 6);
12252 continue;
12253 }
12254 if (STRNCMP(arg, "indent", 6) == 0)
12255 {
12256 indent = TRUE;
12257 arg = skipwhite(arg + 6);
12258 continue;
12259 }
12260 break;
12261 }
12262 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12263 {
12264 if (*arg == 'o' || !filetype_detect)
12265 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012266 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267 filetype_detect = TRUE;
12268 if (plugin)
12269 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012270 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271 filetype_plugin = TRUE;
12272 }
12273 if (indent)
12274 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012275 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276 filetype_indent = TRUE;
12277 }
12278 }
12279 if (*arg == 'd')
12280 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012281 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012282 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 }
12284 }
12285 else if (STRCMP(arg, "off") == 0)
12286 {
12287 if (plugin || indent)
12288 {
12289 if (plugin)
12290 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012291 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 filetype_plugin = FALSE;
12293 }
12294 if (indent)
12295 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012296 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297 filetype_indent = FALSE;
12298 }
12299 }
12300 else
12301 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012302 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 filetype_detect = FALSE;
12304 }
12305 }
12306 else
12307 EMSG2(_(e_invarg2), arg);
12308}
12309
12310/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012311 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 */
12313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012314ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315{
12316 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012317 {
12318 char_u *arg = eap->arg;
12319
12320 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12321 arg += 9;
12322
12323 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12324 if (arg != eap->arg)
12325 did_filetype = FALSE;
12326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012330ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331{
12332#ifdef FEAT_DIGRAPHS
12333 if (*eap->arg != NUL)
12334 putdigraph(eap->arg);
12335 else
12336 listdigraphs();
12337#else
12338 EMSG(_("E196: No digraphs in this version"));
12339#endif
12340}
12341
12342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012343ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344{
12345 int flags = 0;
12346
12347 if (eap->cmdidx == CMD_setlocal)
12348 flags = OPT_LOCAL;
12349 else if (eap->cmdidx == CMD_setglobal)
12350 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012351#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352 if (cmdmod.browse && flags == 0)
12353 ex_options(eap);
12354 else
12355#endif
12356 (void)do_set(eap->arg, flags);
12357}
12358
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012359#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12360 void
12361set_no_hlsearch(int flag)
12362{
12363 no_hlsearch = flag;
12364# ifdef FEAT_EVAL
12365 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12366# endif
12367}
12368
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369/*
12370 * ":nohlsearch"
12371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012373ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012375 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012376 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377}
12378
12379/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012380 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 * Sets nextcmd to the start of the next command, if any. Also called when
12382 * skipping commands to find the next command.
12383 */
12384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012385ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386{
12387 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012388 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 char_u *end;
12390 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012391 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012392
12393 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012394 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012395 else
12396 {
12397 EMSG(e_invcmd);
12398 return;
12399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400
12401 /* First clear any old pattern. */
12402 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012403 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404
12405 if (ends_excmd(*eap->arg))
12406 end = eap->arg;
12407 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012408 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409 end = eap->arg + 4;
12410 else
12411 {
12412 p = skiptowhite(eap->arg);
12413 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012414 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415 p = skipwhite(p);
12416 if (*p == NUL)
12417 {
12418 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012419 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420 EMSG2(_(e_invarg2), eap->arg);
12421 return;
12422 }
12423 end = skip_regexp(p + 1, *p, TRUE, NULL);
12424 if (!eap->skip)
12425 {
12426 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12427 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012428 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429 eap->errmsg = e_trailing;
12430 return;
12431 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012432 if (*end != *p)
12433 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012434 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012435 EMSG2(_(e_invarg2), p);
12436 return;
12437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438
12439 c = *end;
12440 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012441 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012442 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012443 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444 }
12445 }
12446 eap->nextcmd = find_nextcmd(end);
12447}
12448#endif
12449
12450#ifdef FEAT_CRYPT
12451/*
12452 * ":X": Get crypt key
12453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012455ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012457 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012458 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459}
12460#endif
12461
12462#ifdef FEAT_FOLDING
12463 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012464ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465{
12466 if (foldManualAllowed(TRUE))
12467 foldCreate(eap->line1, eap->line2);
12468}
12469
12470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012471ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472{
12473 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12474 eap->forceit, FALSE);
12475}
12476
12477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012478ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479{
12480 linenr_T lnum;
12481
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012482#ifdef FEAT_CLIPBOARD
12483 start_global_changes();
12484#endif
12485
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 /* First set the marks for all lines closed/open. */
12487 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12488 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12489 ml_setmarked(lnum);
12490
12491 /* Execute the command on the marked lines. */
12492 global_exe(eap->arg);
12493 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012494#ifdef FEAT_CLIPBOARD
12495 end_global_changes();
12496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497}
12498#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012499
12500# if defined(FEAT_TIMERS) || defined(PROTO)
12501 int
12502get_pressedreturn(void)
12503{
12504 return ex_pressedreturn;
12505}
12506
12507 void
12508set_pressedreturn(int val)
12509{
12510 ex_pressedreturn = val;
12511}
12512#endif