blob: 38acbfeba9ebde5faa3d9de7b764191a4cc54f4d [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 */
29 scid_T uc_scriptID; /* SID where the command was defined */
30 int uc_compl; /* completion type */
31# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
32 char_u *uc_compl_arg; /* completion argument if any */
33# endif
34} ucmd_T;
35
36#define UC_BUFFER 1 /* -buffer: local to current buffer */
37
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000038static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
41#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
42
43static void do_ucmd __ARGS((exarg_T *eap));
44static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000045static void ex_delcommand __ARGS((exarg_T *eap));
46# ifdef FEAT_CMDL_COMPL
47static char_u *get_user_command_name __ARGS((int idx));
48# endif
49
50#else
51# define ex_command ex_ni
52# define ex_comclear ex_ni
53# define ex_delcommand ex_ni
54#endif
55
56#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000057static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000059static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060static int if_level = 0; /* depth in :if */
61#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static char_u *find_command __ARGS((exarg_T *eap, int *full));
63
64static void ex_abbreviate __ARGS((exarg_T *eap));
65static void ex_map __ARGS((exarg_T *eap));
66static void ex_unmap __ARGS((exarg_T *eap));
67static void ex_mapclear __ARGS((exarg_T *eap));
68static void ex_abclear __ARGS((exarg_T *eap));
69#ifndef FEAT_MENU
70# define ex_emenu ex_ni
71# define ex_menu ex_ni
72# define ex_menutranslate ex_ni
73#endif
74#ifdef FEAT_AUTOCMD
75static void ex_autocmd __ARGS((exarg_T *eap));
76static void ex_doautocmd __ARGS((exarg_T *eap));
77#else
78# define ex_autocmd ex_ni
79# define ex_doautocmd ex_ni
80# define ex_doautoall ex_ni
81#endif
82#ifdef FEAT_LISTCMDS
83static void ex_bunload __ARGS((exarg_T *eap));
84static void ex_buffer __ARGS((exarg_T *eap));
85static void ex_bmodified __ARGS((exarg_T *eap));
86static void ex_bnext __ARGS((exarg_T *eap));
87static void ex_bprevious __ARGS((exarg_T *eap));
88static void ex_brewind __ARGS((exarg_T *eap));
89static void ex_blast __ARGS((exarg_T *eap));
90#else
91# define ex_bunload ex_ni
92# define ex_buffer ex_ni
93# define ex_bmodified ex_ni
94# define ex_bnext ex_ni
95# define ex_bprevious ex_ni
96# define ex_brewind ex_ni
97# define ex_blast ex_ni
98# define buflist_list ex_ni
99# define ex_checktime ex_ni
100#endif
101#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
102# define ex_buffer_all ex_ni
103#endif
104static char_u *getargcmd __ARGS((char_u **));
105static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
106static int getargopt __ARGS((exarg_T *eap));
107#ifndef FEAT_QUICKFIX
108# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000109# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110# define ex_cc ex_ni
111# define ex_cnext ex_ni
112# define ex_cfile ex_ni
113# define qf_list ex_ni
114# define qf_age ex_ni
115# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000116# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#endif
118#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
119# define ex_cclose ex_ni
120# define ex_copen ex_ni
121# define ex_cwindow ex_ni
122#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000123#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
124# define ex_cexpr ex_ni
125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
127static int check_more __ARGS((int, int));
128static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000129static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000131 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000132# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133static void ex_script_ni __ARGS((exarg_T *eap));
134#endif
135static char_u *invalid_range __ARGS((exarg_T *eap));
136static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000137#ifdef FEAT_QUICKFIX
138static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
141static void ex_highlight __ARGS((exarg_T *eap));
142static void ex_colorscheme __ARGS((exarg_T *eap));
143static void ex_quit __ARGS((exarg_T *eap));
144static void ex_cquit __ARGS((exarg_T *eap));
145static void ex_quit_all __ARGS((exarg_T *eap));
146#ifdef FEAT_WINDOWS
147static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000148static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150static void ex_resize __ARGS((exarg_T *eap));
151static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000152static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000153static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000154static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000155static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000156static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#else
158# define ex_close ex_ni
159# define ex_only ex_ni
160# define ex_all ex_ni
161# define ex_resize ex_ni
162# define ex_splitview ex_ni
163# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000164# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000165# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000166# define ex_tabs ex_ni
167# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000168# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif
170#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
171static void ex_pclose __ARGS((exarg_T *eap));
172static void ex_ptag __ARGS((exarg_T *eap));
173static void ex_pedit __ARGS((exarg_T *eap));
174#else
175# define ex_pclose ex_ni
176# define ex_ptag ex_ni
177# define ex_pedit ex_ni
178#endif
179static void ex_hide __ARGS((exarg_T *eap));
180static void ex_stop __ARGS((exarg_T *eap));
181static void ex_exit __ARGS((exarg_T *eap));
182static void ex_print __ARGS((exarg_T *eap));
183#ifdef FEAT_BYTEOFF
184static void ex_goto __ARGS((exarg_T *eap));
185#else
186# define ex_goto ex_ni
187#endif
188static void ex_shell __ARGS((exarg_T *eap));
189static void ex_preserve __ARGS((exarg_T *eap));
190static void ex_recover __ARGS((exarg_T *eap));
191#ifndef FEAT_LISTCMDS
192# define ex_argedit ex_ni
193# define ex_argadd ex_ni
194# define ex_argdelete ex_ni
195# define ex_listdo ex_ni
196#endif
197static void ex_mode __ARGS((exarg_T *eap));
198static void ex_wrongmodifier __ARGS((exarg_T *eap));
199static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000200static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201static void ex_edit __ARGS((exarg_T *eap));
202#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
203# define ex_drop ex_ni
204#endif
205#ifndef FEAT_GUI
206# define ex_gui ex_nogui
207static void ex_nogui __ARGS((exarg_T *eap));
208#endif
209#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
210static void ex_tearoff __ARGS((exarg_T *eap));
211#else
212# define ex_tearoff ex_ni
213#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000214#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215static void ex_popup __ARGS((exarg_T *eap));
216#else
217# define ex_popup ex_ni
218#endif
219#ifndef FEAT_GUI_MSWIN
220# define ex_simalt ex_ni
221#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000222#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223# define gui_mch_find_dialog ex_ni
224# define gui_mch_replace_dialog ex_ni
225#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000226#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227# define ex_helpfind ex_ni
228#endif
229#ifndef FEAT_CSCOPE
230# define do_cscope ex_ni
231# define do_scscope ex_ni
232# define do_cstag ex_ni
233#endif
234#ifndef FEAT_SYN_HL
235# define ex_syntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000236#endif
237#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000238# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000239# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000240# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000241# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000242# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000243#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000244#ifndef FEAT_MZSCHEME
245# define ex_mzscheme ex_script_ni
246# define ex_mzfile ex_ni
247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248#ifndef FEAT_PERL
249# define ex_perl ex_script_ni
250# define ex_perldo ex_ni
251#endif
252#ifndef FEAT_PYTHON
253# define ex_python ex_script_ni
254# define ex_pyfile ex_ni
255#endif
256#ifndef FEAT_TCL
257# define ex_tcl ex_script_ni
258# define ex_tcldo ex_ni
259# define ex_tclfile ex_ni
260#endif
261#ifndef FEAT_RUBY
262# define ex_ruby ex_script_ni
263# define ex_rubydo ex_ni
264# define ex_rubyfile ex_ni
265#endif
266#ifndef FEAT_SNIFF
267# define ex_sniff ex_ni
268#endif
269#ifndef FEAT_KEYMAP
270# define ex_loadkeymap ex_ni
271#endif
272static void ex_swapname __ARGS((exarg_T *eap));
273static void ex_syncbind __ARGS((exarg_T *eap));
274static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275static void ex_pwd __ARGS((exarg_T *eap));
276static void ex_equal __ARGS((exarg_T *eap));
277static void ex_sleep __ARGS((exarg_T *eap));
278static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
279static void ex_winsize __ARGS((exarg_T *eap));
280#ifdef FEAT_WINDOWS
281static void ex_wincmd __ARGS((exarg_T *eap));
282#else
283# define ex_wincmd ex_ni
284#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000285#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286static void ex_winpos __ARGS((exarg_T *eap));
287#else
288# define ex_winpos ex_ni
289#endif
290static void ex_operators __ARGS((exarg_T *eap));
291static void ex_put __ARGS((exarg_T *eap));
292static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000293static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294static void ex_submagic __ARGS((exarg_T *eap));
295static void ex_join __ARGS((exarg_T *eap));
296static void ex_at __ARGS((exarg_T *eap));
297static void ex_bang __ARGS((exarg_T *eap));
298static void ex_undo __ARGS((exarg_T *eap));
299static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000300static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301static void ex_redir __ARGS((exarg_T *eap));
302static void ex_redraw __ARGS((exarg_T *eap));
303static void ex_redrawstatus __ARGS((exarg_T *eap));
304static void close_redir __ARGS((void));
305static void ex_mkrc __ARGS((exarg_T *eap));
306static void ex_mark __ARGS((exarg_T *eap));
307#ifdef FEAT_USR_CMDS
308static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000309static char_u *find_ucmd __ARGS((exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#endif
311#ifdef FEAT_EX_EXTRA
312static void ex_normal __ARGS((exarg_T *eap));
313static void ex_startinsert __ARGS((exarg_T *eap));
314static void ex_stopinsert __ARGS((exarg_T *eap));
315#else
316# define ex_normal ex_ni
317# define ex_align ex_ni
318# define ex_retab ex_ni
319# define ex_startinsert ex_ni
320# define ex_stopinsert ex_ni
321# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000322# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#endif
324#ifdef FEAT_FIND_ID
325static void ex_checkpath __ARGS((exarg_T *eap));
326static void ex_findpat __ARGS((exarg_T *eap));
327#else
328# define ex_findpat ex_ni
329# define ex_checkpath ex_ni
330#endif
331#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
332static void ex_psearch __ARGS((exarg_T *eap));
333#else
334# define ex_psearch ex_ni
335#endif
336static void ex_tag __ARGS((exarg_T *eap));
337static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
338#ifndef FEAT_EVAL
339# define ex_scriptnames ex_ni
340# define ex_finish ex_ni
341# define ex_echo ex_ni
342# define ex_echohl ex_ni
343# define ex_execute ex_ni
344# define ex_call ex_ni
345# define ex_if ex_ni
346# define ex_endif ex_ni
347# define ex_else ex_ni
348# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000349# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350# define ex_continue ex_ni
351# define ex_break ex_ni
352# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000353# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354# define ex_throw ex_ni
355# define ex_try ex_ni
356# define ex_catch ex_ni
357# define ex_finally ex_ni
358# define ex_endtry ex_ni
359# define ex_endfunction ex_ni
360# define ex_let ex_ni
361# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000362# define ex_lockvar ex_ni
363# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364# define ex_function ex_ni
365# define ex_delfunction ex_ni
366# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000367# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368#endif
369static char_u *arg_all __ARGS((void));
370#ifdef FEAT_SESSION
371static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000372static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373static void ex_loadview __ARGS((exarg_T *eap));
374static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000375static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#else
377# define ex_loadview ex_ni
378#endif
379#ifndef FEAT_EVAL
380# define ex_compiler ex_ni
381#endif
382#ifdef FEAT_VIMINFO
383static void ex_viminfo __ARGS((exarg_T *eap));
384#else
385# define ex_viminfo ex_ni
386#endif
387static void ex_behave __ARGS((exarg_T *eap));
388#ifdef FEAT_AUTOCMD
389static void ex_filetype __ARGS((exarg_T *eap));
390static void ex_setfiletype __ARGS((exarg_T *eap));
391#else
392# define ex_filetype ex_ni
393# define ex_setfiletype ex_ni
394#endif
395#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000396# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397# define ex_diffpatch ex_ni
398# define ex_diffgetput ex_ni
399# define ex_diffsplit ex_ni
400# define ex_diffthis ex_ni
401# define ex_diffupdate ex_ni
402#endif
403static void ex_digraphs __ARGS((exarg_T *eap));
404static void ex_set __ARGS((exarg_T *eap));
405#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
406# define ex_options ex_ni
407#endif
408#ifdef FEAT_SEARCH_EXTRA
409static void ex_nohlsearch __ARGS((exarg_T *eap));
410static void ex_match __ARGS((exarg_T *eap));
411#else
412# define ex_nohlsearch ex_ni
413# define ex_match ex_ni
414#endif
415#ifdef FEAT_CRYPT
416static void ex_X __ARGS((exarg_T *eap));
417#else
418# define ex_X ex_ni
419#endif
420#ifdef FEAT_FOLDING
421static void ex_fold __ARGS((exarg_T *eap));
422static void ex_foldopen __ARGS((exarg_T *eap));
423static void ex_folddo __ARGS((exarg_T *eap));
424#else
425# define ex_fold ex_ni
426# define ex_foldopen ex_ni
427# define ex_folddo ex_ni
428#endif
429#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
430 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
431# define ex_language ex_ni
432#endif
433#ifndef FEAT_SIGNS
434# define ex_sign ex_ni
435#endif
436#ifndef FEAT_SUN_WORKSHOP
437# define ex_wsverb ex_ni
438#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000439#ifndef FEAT_NETBEANS_INTG
440# define ex_nbkey ex_ni
441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442
443#ifndef FEAT_EVAL
444# define ex_debug ex_ni
445# define ex_breakadd ex_ni
446# define ex_debuggreedy ex_ni
447# define ex_breakdel ex_ni
448# define ex_breaklist ex_ni
449#endif
450
451#ifndef FEAT_CMDHIST
452# define ex_history ex_ni
453#endif
454#ifndef FEAT_JUMPLIST
455# define ex_jumps ex_ni
456# define ex_changes ex_ni
457#endif
458
Bram Moolenaar05159a02005-02-26 23:04:13 +0000459#ifndef FEAT_PROFILE
460# define ex_profile ex_ni
461#endif
462
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463/*
464 * Declare cmdnames[].
465 */
466#define DO_DECLARE_EXCMD
467#include "ex_cmds.h"
468
469/*
470 * Table used to quickly search for a command, based on its first character.
471 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000472static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 CMD_append,
475 CMD_buffer,
476 CMD_change,
477 CMD_delete,
478 CMD_edit,
479 CMD_file,
480 CMD_global,
481 CMD_help,
482 CMD_insert,
483 CMD_join,
484 CMD_k,
485 CMD_list,
486 CMD_move,
487 CMD_next,
488 CMD_open,
489 CMD_print,
490 CMD_quit,
491 CMD_read,
492 CMD_substitute,
493 CMD_t,
494 CMD_undo,
495 CMD_vglobal,
496 CMD_write,
497 CMD_xit,
498 CMD_yank,
499 CMD_z,
500 CMD_bang
501};
502
503static char_u dollar_command[2] = {'$', 0};
504
505
506#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000507/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508typedef struct
509{
510 char_u *line; /* command line */
511 linenr_T lnum; /* sourcing_lnum of the line */
512} wcmd_T;
513
514/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000515 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000517 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000519struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520{
521 garray_T *lines_gap; /* growarray with line info */
522 int current_line; /* last read line from growarray */
523 int repeating; /* TRUE when looping a second time */
524 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
525 char_u *(*getline) __ARGS((int, void *, int));
526 void *cookie;
527};
528
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000529static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
530static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000532
533/* Struct to save a few things while debugging. Used in do_cmdline() only. */
534struct dbg_stuff
535{
536 int trylevel;
537 int force_abort;
538 except_T *caught_stack;
539 char_u *vv_exception;
540 char_u *vv_throwpoint;
541 int did_emsg;
542 int got_int;
543 int did_throw;
544 int need_rethrow;
545 int check_cstack;
546 except_T *current_exception;
547};
548
549static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
550static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
551
552 static void
553save_dbg_stuff(dsp)
554 struct dbg_stuff *dsp;
555{
556 dsp->trylevel = trylevel; trylevel = 0;
557 dsp->force_abort = force_abort; force_abort = FALSE;
558 dsp->caught_stack = caught_stack; caught_stack = NULL;
559 dsp->vv_exception = v_exception(NULL);
560 dsp->vv_throwpoint = v_throwpoint(NULL);
561
562 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
563 dsp->did_emsg = did_emsg; did_emsg = FALSE;
564 dsp->got_int = got_int; got_int = FALSE;
565 dsp->did_throw = did_throw; did_throw = FALSE;
566 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
567 dsp->check_cstack = check_cstack; check_cstack = FALSE;
568 dsp->current_exception = current_exception; current_exception = NULL;
569}
570
571 static void
572restore_dbg_stuff(dsp)
573 struct dbg_stuff *dsp;
574{
575 suppress_errthrow = FALSE;
576 trylevel = dsp->trylevel;
577 force_abort = dsp->force_abort;
578 caught_stack = dsp->caught_stack;
579 (void)v_exception(dsp->vv_exception);
580 (void)v_throwpoint(dsp->vv_throwpoint);
581 did_emsg = dsp->did_emsg;
582 got_int = dsp->got_int;
583 did_throw = dsp->did_throw;
584 need_rethrow = dsp->need_rethrow;
585 check_cstack = dsp->check_cstack;
586 current_exception = dsp->current_exception;
587}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588#endif
589
590
591/*
592 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
593 * command is given.
594 */
595 void
596do_exmode(improved)
597 int improved; /* TRUE for "improved Ex" mode */
598{
599 int save_msg_scroll;
600 int prev_msg_row;
601 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000602 int changedtick;
603
604 if (improved)
605 exmode_active = EXMODE_VIM;
606 else
607 exmode_active = EXMODE_NORMAL;
608 State = NORMAL;
609
610 /* When using ":global /pat/ visual" and then "Q" we return to continue
611 * the :global command. */
612 if (global_busy)
613 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614
615 save_msg_scroll = msg_scroll;
616 ++RedrawingDisabled; /* don't redisplay the window */
617 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#ifdef FEAT_GUI
619 /* Ignore scrollbar and mouse events in Ex mode */
620 ++hold_gui_events;
621#endif
622#ifdef FEAT_SNIFF
623 want_sniff_request = 0; /* No K_SNIFF wanted */
624#endif
625
626 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
627 while (exmode_active)
628 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000629#ifdef FEAT_EX_EXTRA
630 /* Check for a ":normal" command and no more characters left. */
631 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
632 {
633 exmode_active = FALSE;
634 break;
635 }
636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 msg_scroll = TRUE;
638 need_wait_return = FALSE;
639 ex_pressedreturn = FALSE;
640 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000641 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 prev_msg_row = msg_row;
643 prev_line = curwin->w_cursor.lnum;
644#ifdef FEAT_SNIFF
645 ProcessSniffRequests();
646#endif
647 if (improved)
648 {
649 cmdline_row = msg_row;
650 do_cmdline(NULL, getexline, NULL, 0);
651 }
652 else
653 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
654 lines_left = Rows - 1;
655
Bram Moolenaardf177f62005-02-22 08:39:57 +0000656 if ((prev_line != curwin->w_cursor.lnum
657 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000659 if (curbuf->b_ml.ml_flags & ML_EMPTY)
660 EMSG(_(e_emptybuf));
661 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000663 if (ex_pressedreturn)
664 {
665 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000666 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000667 msg_row = prev_msg_row;
668 if (prev_msg_row == Rows - 1)
669 msg_row--;
670 }
671 msg_col = 0;
672 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
673 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000676 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
677 {
678 if (curbuf->b_ml.ml_flags & ML_EMPTY)
679 EMSG(_(e_emptybuf));
680 else
681 EMSG(_("E501: At end-of-file"));
682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 }
684
685#ifdef FEAT_GUI
686 --hold_gui_events;
687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 --RedrawingDisabled;
689 --no_wait_return;
690 update_screen(CLEAR);
691 need_wait_return = FALSE;
692 msg_scroll = save_msg_scroll;
693}
694
695/*
696 * Execute a simple command line. Used for translated commands like "*".
697 */
698 int
699do_cmdline_cmd(cmd)
700 char_u *cmd;
701{
702 return do_cmdline(cmd, NULL, NULL,
703 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
704}
705
706/*
707 * do_cmdline(): execute one Ex command line
708 *
709 * 1. Execute "cmdline" when it is not NULL.
710 * If "cmdline" is NULL, or more lines are needed, getline() is used.
711 * 2. Split up in parts separated with '|'.
712 *
713 * This function can be called recursively!
714 *
715 * flags:
716 * DOCMD_VERBOSE - The command will be included in the error message.
717 * DOCMD_NOWAIT - Don't call wait_return() and friends.
718 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
719 * DOCMD_KEYTYPED - Don't reset KeyTyped.
720 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
721 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
722 *
723 * return FAIL if cmdline could not be executed, OK otherwise
724 */
725 int
726do_cmdline(cmdline, getline, cookie, flags)
727 char_u *cmdline;
728 char_u *(*getline) __ARGS((int, void *, int));
729 void *cookie; /* argument for getline() */
730 int flags;
731{
732 char_u *next_cmdline; /* next cmd to execute */
733 char_u *cmdline_copy = NULL; /* copy of cmd line */
734 int used_getline = FALSE; /* used "getline" to obtain command */
735 static int recursive = 0; /* recursive depth */
736 int msg_didout_before_start = 0;
737 int count = 0; /* line number count */
738 int did_inc = FALSE; /* incremented RedrawingDisabled */
739 int retval = OK;
740#ifdef FEAT_EVAL
741 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000742 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 int current_line = 0; /* active line in lines_ga */
744 char_u *fname = NULL; /* function or script name */
745 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
746 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000747 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 int initial_trylevel;
749 struct msglist **saved_msg_list = NULL;
750 struct msglist *private_msg_list;
751
752 /* "getline" and "cookie" passed to do_one_cmd() */
753 char_u *(*cmd_getline) __ARGS((int, void *, int));
754 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000755 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000757 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758#else
759# define cmd_getline getline
760# define cmd_cookie cookie
761#endif
762 static int call_depth = 0; /* recursiveness */
763
764#ifdef FEAT_EVAL
765 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
766 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000767 * This ensures that the do_errthrow() call in do_one_cmd() does not
768 * combine the messages stored by an earlier invocation of do_one_cmd()
769 * with the command name of the later one. This would happen when
770 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 saved_msg_list = msg_list;
772 msg_list = &private_msg_list;
773 private_msg_list = NULL;
774#endif
775
776 /* It's possible to create an endless loop with ":execute", catch that
777 * here. The value of 200 allows nested function calls, ":source", etc. */
778 if (call_depth == 200)
779 {
780 EMSG(_("E169: Command too recursive"));
781#ifdef FEAT_EVAL
782 /* When converting to an exception, we do not include the command name
783 * since this is not an error of the specific command. */
784 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
785 msg_list = saved_msg_list;
786#endif
787 return FAIL;
788 }
789 ++call_depth;
790
791#ifdef FEAT_EVAL
792 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000793 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 cstack.cs_trylevel = 0;
795 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000796 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
798
799 real_cookie = getline_cookie(getline, cookie);
800
801 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000802 getline_is_func = getline_equal(getline, cookie, get_func_line);
803 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 ++ex_nesting_level;
805
806 /* Get the function or script name and the address where the next breakpoint
807 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 {
810 fname = func_name(real_cookie);
811 breakpoint = func_breakpoint(real_cookie);
812 dbg_tick = func_dbg_tick(real_cookie);
813 }
814 else if (getline_equal(getline, cookie, getsourceline))
815 {
816 fname = sourcing_name;
817 breakpoint = source_breakpoint(real_cookie);
818 dbg_tick = source_dbg_tick(real_cookie);
819 }
820
821 /*
822 * Initialize "force_abort" and "suppress_errthrow" at the top level.
823 */
824 if (!recursive)
825 {
826 force_abort = FALSE;
827 suppress_errthrow = FALSE;
828 }
829
830 /*
831 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000832 * exception handling (used when debugging). Otherwise clear it to avoid
833 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000835 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000836 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000837 else
838 memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839
840 initial_trylevel = trylevel;
841
842 /*
843 * "did_throw" will be set to TRUE when an exception is being thrown.
844 */
845 did_throw = FALSE;
846#endif
847 /*
848 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000849 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 * If force_abort is set, we cancel everything.
851 */
852 did_emsg = FALSE;
853
854 /*
855 * KeyTyped is only set when calling vgetc(). Reset it here when not
856 * calling vgetc() (sourced command lines).
857 */
858 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
859 KeyTyped = FALSE;
860
861 /*
862 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000863 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 * - for multiple commands on one line, separated with '|'
865 * - when repeating until there are no more lines (for ":source")
866 */
867 next_cmdline = cmdline;
868 do
869 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000870#ifdef FEAT_EVAL
871 getline_is_func = getline_equal(getline, cookie, get_func_line);
872#endif
873
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000874 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (next_cmdline == NULL
876#ifdef FEAT_EVAL
877 && !force_abort
878 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000879 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880#endif
881 )
882 did_emsg = FALSE;
883
884 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000885 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 * 2. If no line given: Get an allocated line with getline().
887 * 3. If a line is given: Make a copy, so we can mess with it.
888 */
889
890#ifdef FEAT_EVAL
891 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000892 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 {
894 /* Each '|' separated command is stored separately in lines_ga, to
895 * be able to jump to it. Don't use next_cmdline now. */
896 vim_free(cmdline_copy);
897 cmdline_copy = NULL;
898
899 /* Check if a function has returned or, unless it has an unclosed
900 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000901 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000904 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000905 func_line_end(real_cookie);
906# endif
907 if (func_has_ended(real_cookie))
908 {
909 retval = FAIL;
910 break;
911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000913#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000914 else if (do_profiling == PROF_YES
Bram Moolenaar05159a02005-02-26 23:04:13 +0000915 && getline_equal(getline, cookie, getsourceline))
916 script_line_end();
917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
919 /* Check if a sourced file hit a ":finish" command. */
920 if (source_finished(getline, cookie))
921 {
922 retval = FAIL;
923 break;
924 }
925
926 /* If breakpoints have been added/deleted need to check for it. */
927 if (breakpoint != NULL && dbg_tick != NULL
928 && *dbg_tick != debug_tick)
929 {
930 *breakpoint = dbg_find_breakpoint(
931 getline_equal(getline, cookie, getsourceline),
932 fname, sourcing_lnum);
933 *dbg_tick = debug_tick;
934 }
935
936 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
937 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
938
939 /* Did we encounter a breakpoint? */
940 if (breakpoint != NULL && *breakpoint != 0
941 && *breakpoint <= sourcing_lnum)
942 {
943 dbg_breakpoint(fname, sourcing_lnum);
944 /* Find next breakpoint. */
945 *breakpoint = dbg_find_breakpoint(
946 getline_equal(getline, cookie, getsourceline),
947 fname, sourcing_lnum);
948 *dbg_tick = debug_tick;
949 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000950# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000951 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000952 {
953 if (getline_is_func)
954 func_line_start(real_cookie);
955 else if (getline_equal(getline, cookie, getsourceline))
956 script_line_start();
957 }
958# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 }
960
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000961 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000963 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 * again. Pass a different "getline" function to do_one_cmd()
965 * below, so that it stores lines in or reads them from
966 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000967 * while/for loop. */
968 cmd_getline = get_loop_line;
969 cmd_cookie = (void *)&cmd_loop_cookie;
970 cmd_loop_cookie.lines_gap = &lines_ga;
971 cmd_loop_cookie.current_line = current_line;
972 cmd_loop_cookie.getline = getline;
973 cmd_loop_cookie.cookie = cookie;
974 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 }
976 else
977 {
978 cmd_getline = getline;
979 cmd_cookie = cookie;
980 }
981#endif
982
983 /* 2. If no line given, get an allocated line with getline(). */
984 if (next_cmdline == NULL)
985 {
986 /*
987 * Need to set msg_didout for the first line after an ":if",
988 * otherwise the ":if" will be overwritten.
989 */
990 if (count == 1 && getline_equal(getline, cookie, getexline))
991 msg_didout = TRUE;
992 if (getline == NULL || (next_cmdline = getline(':', cookie,
993#ifdef FEAT_EVAL
994 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
995#else
996 0
997#endif
998 )) == NULL)
999 {
1000 /* Don't call wait_return for aborted command line. The NULL
1001 * returned for the end of a sourced file or executed function
1002 * doesn't do this. */
1003 if (KeyTyped && !(flags & DOCMD_REPEAT))
1004 need_wait_return = FALSE;
1005 retval = FAIL;
1006 break;
1007 }
1008 used_getline = TRUE;
1009
1010 /*
1011 * Keep the first typed line. Clear it when more lines are typed.
1012 */
1013 if (flags & DOCMD_KEEPLINE)
1014 {
1015 vim_free(repeat_cmdline);
1016 if (count == 0)
1017 repeat_cmdline = vim_strsave(next_cmdline);
1018 else
1019 repeat_cmdline = NULL;
1020 }
1021 }
1022
1023 /* 3. Make a copy of the command so we can mess with it. */
1024 else if (cmdline_copy == NULL)
1025 {
1026 next_cmdline = vim_strsave(next_cmdline);
1027 if (next_cmdline == NULL)
1028 {
1029 EMSG(_(e_outofmem));
1030 retval = FAIL;
1031 break;
1032 }
1033 }
1034 cmdline_copy = next_cmdline;
1035
1036#ifdef FEAT_EVAL
1037 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001038 * Save the current line when inside a ":while" or ":for", and when
1039 * the command looks like a ":while" or ":for", because we may need it
1040 * later. When there is a '|' and another command, it is stored
1041 * separately, because we need to be able to jump back to it from an
1042 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 if (current_line == lines_ga.ga_len
1045 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 {
1049 retval = FAIL;
1050 break;
1051 }
1052 }
1053 did_endif = FALSE;
1054#endif
1055
1056 if (count++ == 0)
1057 {
1058 /*
1059 * All output from the commands is put below each other, without
1060 * waiting for a return. Don't do this when executing commands
1061 * from a script or when being called recursive (e.g. for ":e
1062 * +command file").
1063 */
1064 if (!(flags & DOCMD_NOWAIT) && !recursive)
1065 {
1066 msg_didout_before_start = msg_didout;
1067 msg_didany = FALSE; /* no output yet */
1068 msg_start();
1069 msg_scroll = TRUE; /* put messages below each other */
1070 ++no_wait_return; /* dont wait for return until finished */
1071 ++RedrawingDisabled;
1072 did_inc = TRUE;
1073 }
1074 }
1075
1076 if (p_verbose >= 15 && sourcing_name != NULL)
1077 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001079 verbose_enter_scroll();
1080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 smsg((char_u *)_("line %ld: %s"),
1082 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001083 if (msg_silent == 0)
1084 msg_puts((char_u *)"\n"); /* don't overwrite this */
1085
1086 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 --no_wait_return;
1088 }
1089
1090 /*
1091 * 2. Execute one '|' separated command.
1092 * do_one_cmd() will return NULL if there is no trailing '|'.
1093 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1094 */
1095 ++recursive;
1096 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1097#ifdef FEAT_EVAL
1098 &cstack,
1099#endif
1100 cmd_getline, cmd_cookie);
1101 --recursive;
1102
1103#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 if (cmd_cookie == (void *)&cmd_loop_cookie)
1105 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001107 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108#endif
1109
1110 if (next_cmdline == NULL)
1111 {
1112 vim_free(cmdline_copy);
1113 cmdline_copy = NULL;
1114#ifdef FEAT_CMDHIST
1115 /*
1116 * If the command was typed, remember it for the ':' register.
1117 * Do this AFTER executing the command to make :@: work.
1118 */
1119 if (getline_equal(getline, cookie, getexline)
1120 && new_last_cmdline != NULL)
1121 {
1122 vim_free(last_cmdline);
1123 last_cmdline = new_last_cmdline;
1124 new_last_cmdline = NULL;
1125 }
1126#endif
1127 }
1128 else
1129 {
1130 /* need to copy the command after the '|' to cmdline_copy, for the
1131 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001132 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 next_cmdline = cmdline_copy;
1134 }
1135
1136
1137#ifdef FEAT_EVAL
1138 /* reset did_emsg for a function that is not aborted by an error */
1139 if (did_emsg && !force_abort
1140 && getline_equal(getline, cookie, get_func_line)
1141 && !func_has_abort(real_cookie))
1142 did_emsg = FALSE;
1143
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001144 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
1146 ++current_line;
1147
1148 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001149 * An ":endwhile", ":endfor" and ":continue" is handled here.
1150 * If we were executing commands, jump back to the ":while" or
1151 * ":for".
1152 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001154 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001156 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001158 /* Jump back to the matching ":while" or ":for". Be careful
1159 * not to use a cs_line[] from an entry that isn't a ":while"
1160 * or ":for": It would make "current_line" invalid and can
1161 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 if (!did_emsg && !got_int && !did_throw
1163 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001164 && (cstack.cs_flags[cstack.cs_idx]
1165 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 && cstack.cs_line[cstack.cs_idx] >= 0
1167 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1168 {
1169 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 /* remember we jumped there */
1171 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 line_breakcheck(); /* check if CTRL-C typed */
1173
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001174 /* Check for the next breakpoint at or after the ":while"
1175 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 if (breakpoint != NULL)
1177 {
1178 *breakpoint = dbg_find_breakpoint(
1179 getline_equal(getline, cookie, getsourceline),
1180 fname,
1181 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1182 *dbg_tick = debug_tick;
1183 }
1184 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001187 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001189 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1190 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192 }
1193
1194 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001195 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001199 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1201 }
1202 }
1203
1204 /*
1205 * When not inside any ":while" loop, clear remembered lines.
1206 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001207 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 {
1209 if (lines_ga.ga_len > 0)
1210 {
1211 sourcing_lnum =
1212 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1213 free_cmdlines(&lines_ga);
1214 }
1215 current_line = 0;
1216 }
1217
1218 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001219 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1220 * being restored at the ":endtry". Reset them here and set the
1221 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1222 * This includes the case where a missing ":endif", ":endwhile" or
1223 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001227 cstack.cs_lflags &= ~CSL_HAD_FINA;
1228 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1229 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 did_throw ? (void *)current_exception : NULL);
1231 did_emsg = got_int = did_throw = FALSE;
1232 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1233 }
1234
1235 /* Update global "trylevel" for recursive calls to do_cmdline() from
1236 * within this loop. */
1237 trylevel = initial_trylevel + cstack.cs_trylevel;
1238
1239 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001240 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 * files) is aborted because of an error, an interrupt, or an uncaught
1242 * exception, cancel everything. If it is left normally, reset
1243 * force_abort to get the non-EH compatible abortion behavior for
1244 * the rest of the script.
1245 */
1246 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1247 force_abort = FALSE;
1248
1249 /* Convert an interrupt to an exception if appropriate. */
1250 (void)do_intthrow(&cstack);
1251#endif /* FEAT_EVAL */
1252
1253 }
1254 /*
1255 * Continue executing command lines when:
1256 * - no CTRL-C typed, no aborting error, no exception thrown or try
1257 * conditionals need to be checked for executing finally clauses or
1258 * catching an interrupt exception
1259 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001260 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 * looping for ":source" command or function call.
1262 */
1263 while (!((got_int
1264#ifdef FEAT_EVAL
1265 || (did_emsg && force_abort) || did_throw
1266#endif
1267 )
1268#ifdef FEAT_EVAL
1269 && cstack.cs_trylevel == 0
1270#endif
1271 )
1272 && !(did_emsg && used_getline
1273 && (getline_equal(getline, cookie, getexmodeline)
1274 || getline_equal(getline, cookie, getexline)))
1275 && (next_cmdline != NULL
1276#ifdef FEAT_EVAL
1277 || cstack.cs_idx >= 0
1278#endif
1279 || (flags & DOCMD_REPEAT)));
1280
1281 vim_free(cmdline_copy);
1282#ifdef FEAT_EVAL
1283 free_cmdlines(&lines_ga);
1284 ga_clear(&lines_ga);
1285
1286 if (cstack.cs_idx >= 0)
1287 {
1288 /*
1289 * If a sourced file or executed function ran to its end, report the
1290 * unclosed conditional.
1291 */
1292 if (!got_int && !did_throw
1293 && ((getline_equal(getline, cookie, getsourceline)
1294 && !source_finished(getline, cookie))
1295 || (getline_equal(getline, cookie, get_func_line)
1296 && !func_has_ended(real_cookie))))
1297 {
1298 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1299 EMSG(_(e_endtry));
1300 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1301 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001302 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1303 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 else
1305 EMSG(_(e_endif));
1306 }
1307
1308 /*
1309 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1310 * ":endtry" in a sourced file or executed function. If the try
1311 * conditional is in its finally clause, ignore anything pending.
1312 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001313 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 */
1315 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001316 {
1317 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1318
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001319 if (idx >= 0)
1320 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001321 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1322 &cstack.cs_looplevel);
1323 }
1324 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 trylevel = initial_trylevel;
1326 }
1327
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001328 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1329 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 * exception, do this now after rewinding the cstack. */
1331 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1332 ? (char_u *)"endfunction" : (char_u *)NULL);
1333
1334 if (trylevel == 0)
1335 {
1336 /*
1337 * When an exception is being thrown out of the outermost try
1338 * conditional, discard the uncaught exception, disable the conversion
1339 * of interrupts or errors to exceptions, and ensure that no more
1340 * commands are executed.
1341 */
1342 if (did_throw)
1343 {
1344 void *p = NULL;
1345 char_u *saved_sourcing_name;
1346 int saved_sourcing_lnum;
1347 struct msglist *messages = NULL, *next;
1348
1349 /*
1350 * If the uncaught exception is a user exception, report it as an
1351 * error. If it is an error exception, display the saved error
1352 * message now. For an interrupt exception, do nothing; the
1353 * interrupt message is given elsewhere.
1354 */
1355 switch (current_exception->type)
1356 {
1357 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001358 vim_snprintf((char *)IObuff, IOSIZE,
1359 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 current_exception->value);
1361 p = vim_strsave(IObuff);
1362 break;
1363 case ET_ERROR:
1364 messages = current_exception->messages;
1365 current_exception->messages = NULL;
1366 break;
1367 case ET_INTERRUPT:
1368 break;
1369 default:
1370 p = vim_strsave((char_u *)_(e_internal));
1371 }
1372
1373 saved_sourcing_name = sourcing_name;
1374 saved_sourcing_lnum = sourcing_lnum;
1375 sourcing_name = current_exception->throw_name;
1376 sourcing_lnum = current_exception->throw_lnum;
1377 current_exception->throw_name = NULL;
1378
1379 discard_current_exception(); /* uses IObuff if 'verbose' */
1380 suppress_errthrow = TRUE;
1381 force_abort = TRUE;
1382
1383 if (messages != NULL)
1384 {
1385 do
1386 {
1387 next = messages->next;
1388 emsg(messages->msg);
1389 vim_free(messages->msg);
1390 vim_free(messages);
1391 messages = next;
1392 }
1393 while (messages != NULL);
1394 }
1395 else if (p != NULL)
1396 {
1397 emsg(p);
1398 vim_free(p);
1399 }
1400 vim_free(sourcing_name);
1401 sourcing_name = saved_sourcing_name;
1402 sourcing_lnum = saved_sourcing_lnum;
1403 }
1404
1405 /*
1406 * On an interrupt or an aborting error not converted to an exception,
1407 * disable the conversion of errors to exceptions. (Interrupts are not
1408 * converted any more, here.) This enables also the interrupt message
1409 * when force_abort is set and did_emsg unset in case of an interrupt
1410 * from a finally clause after an error.
1411 */
1412 else if (got_int || (did_emsg && force_abort))
1413 suppress_errthrow = TRUE;
1414 }
1415
1416 /*
1417 * The current cstack will be freed when do_cmdline() returns. An uncaught
1418 * exception will have to be rethrown in the previous cstack. If a function
1419 * has just returned or a script file was just finished and the previous
1420 * cstack belongs to the same function or, respectively, script file, it
1421 * will have to be checked for finally clauses to be executed due to the
1422 * ":return" or ":finish". This is done in do_one_cmd().
1423 */
1424 if (did_throw)
1425 need_rethrow = TRUE;
1426 if ((getline_equal(getline, cookie, getsourceline)
1427 && ex_nesting_level > source_level(real_cookie))
1428 || (getline_equal(getline, cookie, get_func_line)
1429 && ex_nesting_level > func_level(real_cookie) + 1))
1430 {
1431 if (!did_throw)
1432 check_cstack = TRUE;
1433 }
1434 else
1435 {
1436 /* When leaving a function, reduce nesting level. */
1437 if (getline_equal(getline, cookie, get_func_line))
1438 --ex_nesting_level;
1439 /*
1440 * Go to debug mode when returning from a function in which we are
1441 * single-stepping.
1442 */
1443 if ((getline_equal(getline, cookie, getsourceline)
1444 || getline_equal(getline, cookie, get_func_line))
1445 && ex_nesting_level + 1 <= debug_break_level)
1446 do_debug(getline_equal(getline, cookie, getsourceline)
1447 ? (char_u *)_("End of sourced file")
1448 : (char_u *)_("End of function"));
1449 }
1450
1451 /*
1452 * Restore the exception environment (done after returning from the
1453 * debugger).
1454 */
1455 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001456 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457
1458 msg_list = saved_msg_list;
1459#endif /* FEAT_EVAL */
1460
1461 /*
1462 * If there was too much output to fit on the command line, ask the user to
1463 * hit return before redrawing the screen. With the ":global" command we do
1464 * this only once after the command is finished.
1465 */
1466 if (did_inc)
1467 {
1468 --RedrawingDisabled;
1469 --no_wait_return;
1470 msg_scroll = FALSE;
1471
1472 /*
1473 * When just finished an ":if"-":else" which was typed, no need to
1474 * wait for hit-return. Also for an error situation.
1475 */
1476 if (retval == FAIL
1477#ifdef FEAT_EVAL
1478 || (did_endif && KeyTyped && !did_emsg)
1479#endif
1480 )
1481 {
1482 need_wait_return = FALSE;
1483 msg_didany = FALSE; /* don't wait when restarting edit */
1484 }
1485 else if (need_wait_return)
1486 {
1487 /*
1488 * The msg_start() above clears msg_didout. The wait_return we do
1489 * here should not overwrite the command that may be shown before
1490 * doing that.
1491 */
1492 msg_didout |= msg_didout_before_start;
1493 wait_return(FALSE);
1494 }
1495 }
1496
1497#ifndef FEAT_EVAL
1498 /*
1499 * Reset if_level, in case a sourced script file contains more ":if" than
1500 * ":endif" (could be ":if x | foo | endif").
1501 */
1502 if_level = 0;
1503#endif
1504
1505 --call_depth;
1506 return retval;
1507}
1508
1509#ifdef FEAT_EVAL
1510/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001511 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 */
1513 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001514get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 int c;
1516 void *cookie;
1517 int indent;
1518{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001519 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 wcmd_T *wp;
1521 char_u *line;
1522
1523 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1524 {
1525 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001528 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 if (cp->getline == NULL)
1530 line = getcmdline(c, 0L, indent);
1531 else
1532 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001533 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 ++cp->current_line;
1535
1536 return line;
1537 }
1538
1539 KeyTyped = FALSE;
1540 ++cp->current_line;
1541 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1542 sourcing_lnum = wp->lnum;
1543 return vim_strsave(wp->line);
1544}
1545
1546/*
1547 * Store a line in "gap" so that a ":while" loop can execute it again.
1548 */
1549 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001550store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 garray_T *gap;
1552 char_u *line;
1553{
1554 if (ga_grow(gap, 1) == FAIL)
1555 return FAIL;
1556 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1557 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1558 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 return OK;
1560}
1561
1562/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001563 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 */
1565 static void
1566free_cmdlines(gap)
1567 garray_T *gap;
1568{
1569 while (gap->ga_len > 0)
1570 {
1571 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1572 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 }
1574}
1575#endif
1576
1577/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001578 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1579 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 */
1581/*ARGSUSED*/
1582 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001583getline_equal(fgetline, cookie, func)
1584 char_u *(*fgetline) __ARGS((int, void *, int));
1585 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 char_u *(*func) __ARGS((int, void *, int));
1587{
1588#ifdef FEAT_EVAL
1589 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001590 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591
Bram Moolenaar89d40322006-08-29 15:30:07 +00001592 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001593 * function that's originally used to obtain the lines. This may be
1594 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001595 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001596 cp = (struct loop_cookie *)cookie;
1597 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {
1599 gp = cp->getline;
1600 cp = cp->cookie;
1601 }
1602 return gp == func;
1603#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001604 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605#endif
1606}
1607
1608#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1609/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001610 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 * getline function. Otherwise return "cookie".
1612 */
1613/*ARGSUSED*/
1614 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001615getline_cookie(fgetline, cookie)
1616 char_u *(*fgetline) __ARGS((int, void *, int));
1617 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
1619# ifdef FEAT_EVAL
1620 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001621 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
Bram Moolenaar89d40322006-08-29 15:30:07 +00001623 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001624 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001626 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001627 cp = (struct loop_cookie *)cookie;
1628 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 {
1630 gp = cp->getline;
1631 cp = cp->cookie;
1632 }
1633 return cp;
1634# else
1635 return cookie;
1636# endif
1637}
1638#endif
1639
1640/*
1641 * Execute one Ex command.
1642 *
1643 * If 'sourcing' is TRUE, the command will be included in the error message.
1644 *
1645 * 1. skip comment lines and leading space
1646 * 2. handle command modifiers
1647 * 3. parse range
1648 * 4. parse command
1649 * 5. parse arguments
1650 * 6. switch on command name
1651 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001652 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 *
1654 * This function may be called recursively!
1655 */
1656#if (_MSC_VER == 1200)
1657/*
Bram Moolenaared203462004-06-16 11:19:22 +00001658 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001660 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661#endif
1662 static char_u *
1663do_one_cmd(cmdlinep, sourcing,
1664#ifdef FEAT_EVAL
1665 cstack,
1666#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001667 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 char_u **cmdlinep;
1669 int sourcing;
1670#ifdef FEAT_EVAL
1671 struct condstack *cstack;
1672#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001673 char_u *(*fgetline) __ARGS((int, void *, int));
1674 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
1676 char_u *p;
1677 linenr_T lnum;
1678 long n;
1679 char_u *errormsg = NULL; /* error message */
1680 exarg_T ea; /* Ex command arguments */
1681 long verbose_save = -1;
1682 int save_msg_scroll = 0;
1683 int did_silent = 0;
1684 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001685#ifdef HAVE_SANDBOX
1686 int did_sandbox = FALSE;
1687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 cmdmod_T save_cmdmod;
1689 int ni; /* set when Not Implemented */
1690
1691 vim_memset(&ea, 0, sizeof(ea));
1692 ea.line1 = 1;
1693 ea.line2 = 1;
1694#ifdef FEAT_EVAL
1695 ++ex_nesting_level;
1696#endif
1697
1698 /* when not editing the last file :q has to be typed twice */
1699 if (quitmore
1700#ifdef FEAT_EVAL
1701 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001702 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
1704 )
1705 --quitmore;
1706
1707 /*
1708 * Reset browse, confirm, etc.. They are restored when returning, for
1709 * recursive calls.
1710 */
1711 save_cmdmod = cmdmod;
1712 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1713
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001714 /* "#!anything" is handled like a comment. */
1715 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1716 goto doend;
1717
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 /*
1719 * Repeat until no more command modifiers are found.
1720 */
1721 ea.cmd = *cmdlinep;
1722 for (;;)
1723 {
1724/*
1725 * 1. skip comment lines and leading white space and colons
1726 */
1727 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1728 ++ea.cmd;
1729
1730 /* in ex mode, an empty line works like :+ */
1731 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001732 && (getline_equal(fgetline, cookie, getexmodeline)
1733 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001734 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 {
1736 ea.cmd = (char_u *)"+";
1737 ex_pressedreturn = TRUE;
1738 }
1739
1740 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001741 if (*ea.cmd == '"')
1742 goto doend;
1743 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001744 {
1745 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
1749/*
1750 * 2. handle command modifiers.
1751 */
1752 p = ea.cmd;
1753 if (VIM_ISDIGIT(*ea.cmd))
1754 p = skipwhite(skipdigits(ea.cmd));
1755 switch (*p)
1756 {
1757 /* When adding an entry, also modify cmd_exists(). */
1758 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1759 break;
1760#ifdef FEAT_WINDOWS
1761 cmdmod.split |= WSP_ABOVE;
1762#endif
1763 continue;
1764
1765 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1766 {
1767#ifdef FEAT_WINDOWS
1768 cmdmod.split |= WSP_BELOW;
1769#endif
1770 continue;
1771 }
1772 if (checkforcmd(&ea.cmd, "browse", 3))
1773 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001774#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 cmdmod.browse = TRUE;
1776#endif
1777 continue;
1778 }
1779 if (!checkforcmd(&ea.cmd, "botright", 2))
1780 break;
1781#ifdef FEAT_WINDOWS
1782 cmdmod.split |= WSP_BOT;
1783#endif
1784 continue;
1785
1786 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1787 break;
1788#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1789 cmdmod.confirm = TRUE;
1790#endif
1791 continue;
1792
1793 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1794 {
1795 cmdmod.keepmarks = TRUE;
1796 continue;
1797 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001798 if (checkforcmd(&ea.cmd, "keepalt", 5))
1799 {
1800 cmdmod.keepalt = TRUE;
1801 continue;
1802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1804 break;
1805 cmdmod.keepjumps = TRUE;
1806 continue;
1807
1808 /* ":hide" and ":hide | cmd" are not modifiers */
1809 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1810 || *p == NUL || ends_excmd(*p))
1811 break;
1812 ea.cmd = p;
1813 cmdmod.hide = TRUE;
1814 continue;
1815
1816 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1817 {
1818 cmdmod.lockmarks = TRUE;
1819 continue;
1820 }
1821
1822 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1823 break;
1824#ifdef FEAT_WINDOWS
1825 cmdmod.split |= WSP_ABOVE;
1826#endif
1827 continue;
1828
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001829 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1830 break;
1831#ifdef FEAT_AUTOCMD
1832 if (cmdmod.save_ei == NULL)
1833 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001834 /* Set 'eventignore' to "all". Restore the
1835 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001836 cmdmod.save_ei = vim_strsave(p_ei);
1837 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001838 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001839 }
1840#endif
1841 continue;
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1844 break;
1845#ifdef FEAT_WINDOWS
1846 cmdmod.split |= WSP_BELOW;
1847#endif
1848 continue;
1849
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001850 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1851 {
1852#ifdef HAVE_SANDBOX
1853 if (!did_sandbox)
1854 ++sandbox;
1855 did_sandbox = TRUE;
1856#endif
1857 continue;
1858 }
1859 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 break;
1861 ++did_silent;
1862 ++msg_silent;
1863 save_msg_scroll = msg_scroll;
1864 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1865 {
1866 /* ":silent!", but not "silent !cmd" */
1867 ea.cmd = skipwhite(ea.cmd + 1);
1868 ++emsg_silent;
1869 ++did_esilent;
1870 }
1871 continue;
1872
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001873 case 't': if (checkforcmd(&p, "tab", 3))
1874 {
1875#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001876 if (vim_isdigit(*ea.cmd))
1877 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1878 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001879 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001880 ea.cmd = p;
1881#endif
1882 continue;
1883 }
1884 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 break;
1886#ifdef FEAT_WINDOWS
1887 cmdmod.split |= WSP_TOP;
1888#endif
1889 continue;
1890
1891 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1892 {
1893#ifdef FEAT_VERTSPLIT
1894 cmdmod.split |= WSP_VERT;
1895#endif
1896 continue;
1897 }
1898 if (!checkforcmd(&p, "verbose", 4))
1899 break;
1900 if (verbose_save < 0)
1901 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001902 if (vim_isdigit(*ea.cmd))
1903 p_verbose = atoi((char *)ea.cmd);
1904 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 p_verbose = 1;
1906 ea.cmd = p;
1907 continue;
1908 }
1909 break;
1910 }
1911
1912#ifdef FEAT_EVAL
1913 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1914 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1915#else
1916 ea.skip = (if_level > 0);
1917#endif
1918
1919#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001920# ifdef FEAT_PROFILE
1921 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001922 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001923 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001924 if (getline_equal(fgetline, cookie, get_func_line))
1925 func_line_exec(getline_cookie(fgetline, cookie));
1926 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001927 script_line_exec();
1928 }
1929#endif
1930
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 /* May go to debug mode. If this happens and the ">quit" debug command is
1932 * used, throw an interrupt exception and skip the next command. */
1933 dbg_check_breakpoint(&ea);
1934 if (!ea.skip && got_int)
1935 {
1936 ea.skip = TRUE;
1937 (void)do_intthrow(cstack);
1938 }
1939#endif
1940
1941/*
1942 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1943 *
1944 * where 'addr' is:
1945 *
1946 * % (entire file)
1947 * $ [+-NUM]
1948 * 'x [+-NUM] (where x denotes a currently defined mark)
1949 * . [+-NUM]
1950 * [+-NUM]..
1951 * NUM
1952 *
1953 * The ea.cmd pointer is updated to point to the first character following the
1954 * range spec. If an initial address is found, but no second, the upper bound
1955 * is equal to the lower.
1956 */
1957
1958 /* repeat for all ',' or ';' separated addresses */
1959 for (;;)
1960 {
1961 ea.line1 = ea.line2;
1962 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1963 ea.cmd = skipwhite(ea.cmd);
1964 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1965 if (ea.cmd == NULL) /* error detected */
1966 goto doend;
1967 if (lnum == MAXLNUM)
1968 {
1969 if (*ea.cmd == '%') /* '%' - all lines */
1970 {
1971 ++ea.cmd;
1972 ea.line1 = 1;
1973 ea.line2 = curbuf->b_ml.ml_line_count;
1974 ++ea.addr_count;
1975 }
1976 /* '*' - visual area */
1977 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1978 {
1979 pos_T *fp;
1980
1981 ++ea.cmd;
1982 if (!ea.skip)
1983 {
1984 fp = getmark('<', FALSE);
1985 if (check_mark(fp) == FAIL)
1986 goto doend;
1987 ea.line1 = fp->lnum;
1988 fp = getmark('>', FALSE);
1989 if (check_mark(fp) == FAIL)
1990 goto doend;
1991 ea.line2 = fp->lnum;
1992 ++ea.addr_count;
1993 }
1994 }
1995 }
1996 else
1997 ea.line2 = lnum;
1998 ea.addr_count++;
1999
2000 if (*ea.cmd == ';')
2001 {
2002 if (!ea.skip)
2003 curwin->w_cursor.lnum = ea.line2;
2004 }
2005 else if (*ea.cmd != ',')
2006 break;
2007 ++ea.cmd;
2008 }
2009
2010 /* One address given: set start and end lines */
2011 if (ea.addr_count == 1)
2012 {
2013 ea.line1 = ea.line2;
2014 /* ... but only implicit: really no address given */
2015 if (lnum == MAXLNUM)
2016 ea.addr_count = 0;
2017 }
2018
2019 /* Don't leave the cursor on an illegal line (caused by ';') */
2020 check_cursor_lnum();
2021
2022/*
2023 * 4. parse command
2024 */
2025
2026 /*
2027 * Skip ':' and any white space
2028 */
2029 ea.cmd = skipwhite(ea.cmd);
2030 while (*ea.cmd == ':')
2031 ea.cmd = skipwhite(ea.cmd + 1);
2032
2033 /*
2034 * If we got a line, but no command, then go to the line.
2035 * If we find a '|' or '\n' we set ea.nextcmd.
2036 */
2037 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2038 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2039 {
2040 /*
2041 * strange vi behaviour:
2042 * ":3" jumps to line 3
2043 * ":3|..." prints line 3
2044 * ":|" prints current line
2045 */
2046 if (ea.skip) /* skip this if inside :if */
2047 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002048 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 {
2050 ea.cmdidx = CMD_print;
2051 ea.argt = RANGE+COUNT+TRLBAR;
2052 if ((errormsg = invalid_range(&ea)) == NULL)
2053 {
2054 correct_range(&ea);
2055 ex_print(&ea);
2056 }
2057 }
2058 else if (ea.addr_count != 0)
2059 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002060 if (ea.line2 > curbuf->b_ml.ml_line_count)
2061 {
2062 /* With '-' in 'cpoptions' a line number past the file is an
2063 * error, otherwise put it at the end of the file. */
2064 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2065 ea.line2 = -1;
2066 else
2067 ea.line2 = curbuf->b_ml.ml_line_count;
2068 }
2069
2070 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002071 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 else
2073 {
2074 if (ea.line2 == 0)
2075 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 else
2077 curwin->w_cursor.lnum = ea.line2;
2078 beginline(BL_SOL | BL_FIX);
2079 }
2080 }
2081 goto doend;
2082 }
2083
2084 /* Find the command and let "p" point to after it. */
2085 p = find_command(&ea, NULL);
2086
2087#ifdef FEAT_USR_CMDS
2088 if (p == NULL)
2089 {
2090 if (!ea.skip)
2091 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2092 goto doend;
2093 }
2094 /* Check for wrong commands. */
2095 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2096 {
2097 errormsg = uc_fun_cmd();
2098 goto doend;
2099 }
2100#endif
2101 if (ea.cmdidx == CMD_SIZE)
2102 {
2103 if (!ea.skip)
2104 {
2105 STRCPY(IObuff, _("E492: Not an editor command"));
2106 if (!sourcing)
2107 {
2108 STRCAT(IObuff, ": ");
2109 STRNCAT(IObuff, *cmdlinep, 40);
2110 }
2111 errormsg = IObuff;
2112 }
2113 goto doend;
2114 }
2115
2116 ni = (
2117#ifdef FEAT_USR_CMDS
2118 !USER_CMDIDX(ea.cmdidx) &&
2119#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002120 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002121#ifdef HAVE_EX_SCRIPT_NI
2122 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2123#endif
2124 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125
2126#ifndef FEAT_EVAL
2127 /*
2128 * When the expression evaluation is disabled, recognize the ":if" and
2129 * ":endif" commands and ignore everything in between it.
2130 */
2131 if (ea.cmdidx == CMD_if)
2132 ++if_level;
2133 if (if_level)
2134 {
2135 if (ea.cmdidx == CMD_endif)
2136 --if_level;
2137 goto doend;
2138 }
2139
2140#endif
2141
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002142 /* forced commands */
2143 if (*p == '!' && ea.cmdidx != CMD_substitute
2144 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
2146 ++p;
2147 ea.forceit = TRUE;
2148 }
2149 else
2150 ea.forceit = FALSE;
2151
2152/*
2153 * 5. parse arguments
2154 */
2155#ifdef FEAT_USR_CMDS
2156 if (!USER_CMDIDX(ea.cmdidx))
2157#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002158 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159
2160 if (!ea.skip)
2161 {
2162#ifdef HAVE_SANDBOX
2163 if (sandbox != 0 && !(ea.argt & SBOXOK))
2164 {
2165 /* Command not allowed in sandbox. */
2166 errormsg = (char_u *)_(e_sandbox);
2167 goto doend;
2168 }
2169#endif
2170 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2171 {
2172 /* Command not allowed in non-'modifiable' buffer */
2173 errormsg = (char_u *)_(e_modifiable);
2174 goto doend;
2175 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002176
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002177 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178# ifdef FEAT_USR_CMDS
2179 && !USER_CMDIDX(ea.cmdidx)
2180# endif
2181 )
2182 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002183 /* Command not allowed when editing the command line. */
2184#ifdef FEAT_CMDWIN
2185 if (cmdwin_type != 0)
2186 errormsg = (char_u *)_(e_cmdwin);
2187 else
2188#endif
2189 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 goto doend;
2191 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002192#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002193 /* Disallow editing another buffer when "curbuf_lock" is set.
2194 * Do allow ":edit" (check for argument later).
2195 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002196 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002197 && ea.cmdidx != CMD_edit
2198 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002199# ifdef FEAT_USR_CMDS
2200 && !USER_CMDIDX(ea.cmdidx)
2201# endif
2202 && curbuf_locked())
2203 goto doend;
2204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205
2206 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2207 {
2208 /* no range allowed */
2209 errormsg = (char_u *)_(e_norange);
2210 goto doend;
2211 }
2212 }
2213
2214 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2215 {
2216 errormsg = (char_u *)_(e_nobang);
2217 goto doend;
2218 }
2219
2220 /*
2221 * Don't complain about the range if it is not used
2222 * (could happen if line_count is accidentally set to 0).
2223 */
2224 if (!ea.skip && !ni)
2225 {
2226 /*
2227 * If the range is backwards, ask for confirmation and, if given, swap
2228 * ea.line1 & ea.line2 so it's forwards again.
2229 * When global command is busy, don't ask, will fail below.
2230 */
2231 if (!global_busy && ea.line1 > ea.line2)
2232 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002233 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002235 if (sourcing || exmode_active)
2236 {
2237 errormsg = (char_u *)_("E493: Backwards range given");
2238 goto doend;
2239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 if (ask_yesno((char_u *)
2241 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002242 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 }
2244 lnum = ea.line1;
2245 ea.line1 = ea.line2;
2246 ea.line2 = lnum;
2247 }
2248 if ((errormsg = invalid_range(&ea)) != NULL)
2249 goto doend;
2250 }
2251
2252 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2253 ea.line2 = 1;
2254
2255 correct_range(&ea);
2256
2257#ifdef FEAT_FOLDING
2258 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2259 {
2260 /* Put the first line at the start of a closed fold, put the last line
2261 * at the end of a closed fold. */
2262 (void)hasFolding(ea.line1, &ea.line1, NULL);
2263 (void)hasFolding(ea.line2, NULL, &ea.line2);
2264 }
2265#endif
2266
2267#ifdef FEAT_QUICKFIX
2268 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002269 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 * option here, so things like % get expanded.
2271 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002272 p = replace_makeprg(&ea, p, cmdlinep);
2273 if (p == NULL)
2274 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275#endif
2276
2277 /*
2278 * Skip to start of argument.
2279 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2280 */
2281 if (ea.cmdidx == CMD_bang)
2282 ea.arg = p;
2283 else
2284 ea.arg = skipwhite(p);
2285
2286 /*
2287 * Check for "++opt=val" argument.
2288 * Must be first, allow ":w ++enc=utf8 !cmd"
2289 */
2290 if (ea.argt & ARGOPT)
2291 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2292 if (getargopt(&ea) == FAIL && !ni)
2293 {
2294 errormsg = (char_u *)_(e_invarg);
2295 goto doend;
2296 }
2297
2298 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2299 {
2300 if (*ea.arg == '>') /* append */
2301 {
2302 if (*++ea.arg != '>') /* typed wrong */
2303 {
2304 errormsg = (char_u *)_("E494: Use w or w>>");
2305 goto doend;
2306 }
2307 ea.arg = skipwhite(ea.arg + 1);
2308 ea.append = TRUE;
2309 }
2310 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2311 {
2312 ++ea.arg;
2313 ea.usefilter = TRUE;
2314 }
2315 }
2316
2317 if (ea.cmdidx == CMD_read)
2318 {
2319 if (ea.forceit)
2320 {
2321 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2322 ea.forceit = FALSE;
2323 }
2324 else if (*ea.arg == '!') /* :r !filter */
2325 {
2326 ++ea.arg;
2327 ea.usefilter = TRUE;
2328 }
2329 }
2330
2331 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2332 {
2333 ea.amount = 1;
2334 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2335 {
2336 ++ea.arg;
2337 ++ea.amount;
2338 }
2339 ea.arg = skipwhite(ea.arg);
2340 }
2341
2342 /*
2343 * Check for "+command" argument, before checking for next command.
2344 * Don't do this for ":read !cmd" and ":write !cmd".
2345 */
2346 if ((ea.argt & EDITCMD) && !ea.usefilter)
2347 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2348
2349 /*
2350 * Check for '|' to separate commands and '"' to start comments.
2351 * Don't do this for ":read !cmd" and ":write !cmd".
2352 */
2353 if ((ea.argt & TRLBAR) && !ea.usefilter)
2354 separate_nextcmd(&ea);
2355
2356 /*
2357 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002358 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2359 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002361 else if (ea.cmdidx == CMD_bang
2362 || ea.cmdidx == CMD_global
2363 || ea.cmdidx == CMD_vglobal
2364 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
2366 for (p = ea.arg; *p; ++p)
2367 {
2368 /* Remove one backslash before a newline, so that it's possible to
2369 * pass a newline to the shell and also a newline that is preceded
2370 * with a backslash. This makes it impossible to end a shell
2371 * command in a backslash, but that doesn't appear useful.
2372 * Halving the number of backslashes is incompatible with previous
2373 * versions. */
2374 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002375 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 else if (*p == '\n')
2377 {
2378 ea.nextcmd = p + 1;
2379 *p = NUL;
2380 break;
2381 }
2382 }
2383 }
2384
2385 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2386 {
2387 ea.line1 = 1;
2388 ea.line2 = curbuf->b_ml.ml_line_count;
2389 }
2390
2391 /* accept numbered register only when no count allowed (:put) */
2392 if ( (ea.argt & REGSTR)
2393 && *ea.arg != NUL
2394#ifdef FEAT_USR_CMDS
2395 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2396 && USER_CMDIDX(ea.cmdidx)))
2397 /* Do not allow register = for user commands */
2398 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2399#else
2400 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2401#endif
2402 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2403 {
2404 ea.regname = *ea.arg++;
2405#ifdef FEAT_EVAL
2406 /* for '=' register: accept the rest of the line as an expression */
2407 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2408 {
2409 set_expr_line(vim_strsave(ea.arg));
2410 ea.arg += STRLEN(ea.arg);
2411 }
2412#endif
2413 ea.arg = skipwhite(ea.arg);
2414 }
2415
2416 /*
2417 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2418 * count, it's a buffer name.
2419 */
2420 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2421 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2422 || vim_iswhite(*p)))
2423 {
2424 n = getdigits(&ea.arg);
2425 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002426 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 {
2428 errormsg = (char_u *)_(e_zerocount);
2429 goto doend;
2430 }
2431 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2432 {
2433 ea.line2 = n;
2434 if (ea.addr_count == 0)
2435 ea.addr_count = 1;
2436 }
2437 else
2438 {
2439 ea.line1 = ea.line2;
2440 ea.line2 += n - 1;
2441 ++ea.addr_count;
2442 /*
2443 * Be vi compatible: no error message for out of range.
2444 */
2445 if (ea.line2 > curbuf->b_ml.ml_line_count)
2446 ea.line2 = curbuf->b_ml.ml_line_count;
2447 }
2448 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002449
2450 /*
2451 * Check for flags: 'l', 'p' and '#'.
2452 */
2453 if (ea.argt & EXFLAGS)
2454 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002456 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002457 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 {
2459 errormsg = (char_u *)_(e_trailing);
2460 goto doend;
2461 }
2462
2463 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2464 {
2465 errormsg = (char_u *)_(e_argreq);
2466 goto doend;
2467 }
2468
2469#ifdef FEAT_EVAL
2470 /*
2471 * Skip the command when it's not going to be executed.
2472 * The commands like :if, :endif, etc. always need to be executed.
2473 * Also make an exception for commands that handle a trailing command
2474 * themselves.
2475 */
2476 if (ea.skip)
2477 {
2478 switch (ea.cmdidx)
2479 {
2480 /* commands that need evaluation */
2481 case CMD_while:
2482 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002483 case CMD_for:
2484 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 case CMD_if:
2486 case CMD_elseif:
2487 case CMD_else:
2488 case CMD_endif:
2489 case CMD_try:
2490 case CMD_catch:
2491 case CMD_finally:
2492 case CMD_endtry:
2493 case CMD_function:
2494 break;
2495
2496 /* Commands that handle '|' themselves. Check: A command should
2497 * either have the TRLBAR flag, appear in this list or appear in
2498 * the list at ":help :bar". */
2499 case CMD_aboveleft:
2500 case CMD_and:
2501 case CMD_belowright:
2502 case CMD_botright:
2503 case CMD_browse:
2504 case CMD_call:
2505 case CMD_confirm:
2506 case CMD_delfunction:
2507 case CMD_djump:
2508 case CMD_dlist:
2509 case CMD_dsearch:
2510 case CMD_dsplit:
2511 case CMD_echo:
2512 case CMD_echoerr:
2513 case CMD_echomsg:
2514 case CMD_echon:
2515 case CMD_execute:
2516 case CMD_help:
2517 case CMD_hide:
2518 case CMD_ijump:
2519 case CMD_ilist:
2520 case CMD_isearch:
2521 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002522 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 case CMD_keepjumps:
2524 case CMD_keepmarks:
2525 case CMD_leftabove:
2526 case CMD_let:
2527 case CMD_lockmarks:
2528 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002529 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 case CMD_perl:
2531 case CMD_psearch:
2532 case CMD_python:
2533 case CMD_return:
2534 case CMD_rightbelow:
2535 case CMD_ruby:
2536 case CMD_silent:
2537 case CMD_smagic:
2538 case CMD_snomagic:
2539 case CMD_substitute:
2540 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002541 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 case CMD_tcl:
2543 case CMD_throw:
2544 case CMD_tilde:
2545 case CMD_topleft:
2546 case CMD_unlet:
2547 case CMD_verbose:
2548 case CMD_vertical:
2549 break;
2550
2551 default: goto doend;
2552 }
2553 }
2554#endif
2555
2556 if (ea.argt & XFILE)
2557 {
2558 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2559 goto doend;
2560 }
2561
2562#ifdef FEAT_LISTCMDS
2563 /*
2564 * Accept buffer name. Cannot be used at the same time with a buffer
2565 * number. Don't do this for a user command.
2566 */
2567 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2568# ifdef FEAT_USR_CMDS
2569 && !USER_CMDIDX(ea.cmdidx)
2570# endif
2571 )
2572 {
2573 /*
2574 * :bdelete, :bwipeout and :bunload take several arguments, separated
2575 * by spaces: find next space (skipping over escaped characters).
2576 * The others take one argument: ignore trailing spaces.
2577 */
2578 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2579 || ea.cmdidx == CMD_bunload)
2580 p = skiptowhite_esc(ea.arg);
2581 else
2582 {
2583 p = ea.arg + STRLEN(ea.arg);
2584 while (p > ea.arg && vim_iswhite(p[-1]))
2585 --p;
2586 }
2587 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2588 if (ea.line2 < 0) /* failed */
2589 goto doend;
2590 ea.addr_count = 1;
2591 ea.arg = skipwhite(p);
2592 }
2593#endif
2594
2595/*
2596 * 6. switch on command name
2597 *
2598 * The "ea" structure holds the arguments that can be used.
2599 */
2600 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002601 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 ea.cookie = cookie;
2603#ifdef FEAT_EVAL
2604 ea.cstack = cstack;
2605#endif
2606
2607#ifdef FEAT_USR_CMDS
2608 if (USER_CMDIDX(ea.cmdidx))
2609 {
2610 /*
2611 * Execute a user-defined command.
2612 */
2613 do_ucmd(&ea);
2614 }
2615 else
2616#endif
2617 {
2618 /*
2619 * Call the function to execute the command.
2620 */
2621 ea.errmsg = NULL;
2622 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2623 if (ea.errmsg != NULL)
2624 errormsg = (char_u *)_(ea.errmsg);
2625 }
2626
2627#ifdef FEAT_EVAL
2628 /*
2629 * If the command just executed called do_cmdline(), any throw or ":return"
2630 * or ":finish" encountered there must also check the cstack of the still
2631 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2632 * exception, or reanimate a returned function or finished script file and
2633 * return or finish it again.
2634 */
2635 if (need_rethrow)
2636 do_throw(cstack);
2637 else if (check_cstack)
2638 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002639 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002641 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 && current_func_returned())
2643 do_return(&ea, TRUE, FALSE, NULL);
2644 }
2645 need_rethrow = check_cstack = FALSE;
2646#endif
2647
2648doend:
2649 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2650 curwin->w_cursor.lnum = 1;
2651
2652 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2653 {
2654 if (sourcing)
2655 {
2656 if (errormsg != IObuff)
2657 {
2658 STRCPY(IObuff, errormsg);
2659 errormsg = IObuff;
2660 }
2661 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002662 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 }
2664 emsg(errormsg);
2665 }
2666#ifdef FEAT_EVAL
2667 do_errthrow(cstack,
2668 (ea.cmdidx != CMD_SIZE
2669# ifdef FEAT_USR_CMDS
2670 && !USER_CMDIDX(ea.cmdidx)
2671# endif
2672 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2673#endif
2674
2675 if (verbose_save >= 0)
2676 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002677#ifdef FEAT_AUTOCMD
2678 if (cmdmod.save_ei != NULL)
2679 {
2680 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002681 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2682 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002683 free_string_option(cmdmod.save_ei);
2684 }
2685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
2687 cmdmod = save_cmdmod;
2688
2689 if (did_silent > 0)
2690 {
2691 /* messages could be enabled for a serious error, need to check if the
2692 * counters don't become negative */
2693 msg_silent -= did_silent;
2694 if (msg_silent < 0)
2695 msg_silent = 0;
2696 emsg_silent -= did_esilent;
2697 if (emsg_silent < 0)
2698 emsg_silent = 0;
2699 /* Restore msg_scroll, it's set by file I/O commands, even when no
2700 * message is actually displayed. */
2701 msg_scroll = save_msg_scroll;
2702 }
2703
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002704#ifdef HAVE_SANDBOX
2705 if (did_sandbox)
2706 --sandbox;
2707#endif
2708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2710 ea.nextcmd = NULL;
2711
2712#ifdef FEAT_EVAL
2713 --ex_nesting_level;
2714#endif
2715
2716 return ea.nextcmd;
2717}
2718#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002719 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720#endif
2721
2722/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002723 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 * If there is a match advance "pp" to the argument and return TRUE.
2725 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002726 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002728 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 char *cmd; /* name of command */
2730 int len; /* required length */
2731{
2732 int i;
2733
2734 for (i = 0; cmd[i] != NUL; ++i)
2735 if (cmd[i] != (*pp)[i])
2736 break;
2737 if (i >= len && !isalpha((*pp)[i]))
2738 {
2739 *pp = skipwhite(*pp + i);
2740 return TRUE;
2741 }
2742 return FALSE;
2743}
2744
2745/*
2746 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002747 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002749 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 * Returns NULL for an ambiguous user command.
2751 */
2752/*ARGSUSED*/
2753 static char_u *
2754find_command(eap, full)
2755 exarg_T *eap;
2756 int *full;
2757{
2758 int len;
2759 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002760 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
2762 /*
2763 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002764 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 * - the 'k' command can directly be followed by any character.
2766 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2767 * but :sre[wind] is another command, as are :scrip[tnames],
2768 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002769 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 */
2771 p = eap->cmd;
2772 if (*p == 'k')
2773 {
2774 eap->cmdidx = CMD_k;
2775 ++p;
2776 }
2777 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002778 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2779 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 || p[1] == 'g'
2781 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2782 || p[1] == 'I'
2783 || (p[1] == 'r' && p[2] != 'e')))
2784 {
2785 eap->cmdidx = CMD_substitute;
2786 ++p;
2787 }
2788 else
2789 {
2790 while (ASCII_ISALPHA(*p))
2791 ++p;
2792 /* check for non-alpha command */
2793 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2794 ++p;
2795 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002796 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2797 {
2798 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2799 * :delete with the 'l' flag. Same for 'p'. */
2800 for (i = 0; i < len; ++i)
2801 if (eap->cmd[i] != "delete"[i])
2802 break;
2803 if (i == len - 1)
2804 {
2805 --len;
2806 if (p[-1] == 'l')
2807 eap->flags |= EXFLAG_LIST;
2808 else
2809 eap->flags |= EXFLAG_PRINT;
2810 }
2811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
2813 if (ASCII_ISLOWER(*eap->cmd))
2814 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2815 else
2816 eap->cmdidx = cmdidxs[26];
2817
2818 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2819 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2820 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2821 (size_t)len) == 0)
2822 {
2823#ifdef FEAT_EVAL
2824 if (full != NULL
2825 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2826 *full = TRUE;
2827#endif
2828 break;
2829 }
2830
2831#ifdef FEAT_USR_CMDS
2832 /* Look for a user defined command as a last resort */
2833 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2834 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002835 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 while (ASCII_ISALNUM(*p))
2837 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002838 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002841 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 eap->cmdidx = CMD_SIZE;
2843 }
2844
2845 return p;
2846}
2847
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002848#ifdef FEAT_USR_CMDS
2849/*
2850 * Search for a user command that matches "eap->cmd".
2851 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2852 * Return a pointer to just after the command.
2853 * Return NULL if there is no matching command.
2854 */
2855 static char_u *
2856find_ucmd(eap, p, full, xp, compl)
2857 exarg_T *eap;
2858 char_u *p; /* end of the command (possibly including count) */
2859 int *full; /* set to TRUE for a full match */
2860 expand_T *xp; /* used for completion, NULL otherwise */
2861 int *compl; /* completion flags or NULL */
2862{
2863 int len = (int)(p - eap->cmd);
2864 int j, k, matchlen = 0;
2865 ucmd_T *uc;
2866 int found = FALSE;
2867 int possible = FALSE;
2868 char_u *cp, *np; /* Point into typed cmd and test name */
2869 garray_T *gap;
2870 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2871 only full match global is accepted. */
2872
2873 /*
2874 * Look for buffer-local user commands first, then global ones.
2875 */
2876 gap = &curbuf->b_ucmds;
2877 for (;;)
2878 {
2879 for (j = 0; j < gap->ga_len; ++j)
2880 {
2881 uc = USER_CMD_GA(gap, j);
2882 cp = eap->cmd;
2883 np = uc->uc_name;
2884 k = 0;
2885 while (k < len && *np != NUL && *cp++ == *np++)
2886 k++;
2887 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2888 {
2889 /* If finding a second match, the command is ambiguous. But
2890 * not if a buffer-local command wasn't a full match and a
2891 * global command is a full match. */
2892 if (k == len && found && *np != NUL)
2893 {
2894 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002895 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002896 amb_local = TRUE;
2897 }
2898
2899 if (!found || (k == len && *np == NUL))
2900 {
2901 /* If we matched up to a digit, then there could
2902 * be another command including the digit that we
2903 * should use instead.
2904 */
2905 if (k == len)
2906 found = TRUE;
2907 else
2908 possible = TRUE;
2909
2910 if (gap == &ucmds)
2911 eap->cmdidx = CMD_USER;
2912 else
2913 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002914 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002915 eap->useridx = j;
2916
2917# ifdef FEAT_CMDL_COMPL
2918 if (compl != NULL)
2919 *compl = uc->uc_compl;
2920# ifdef FEAT_EVAL
2921 if (xp != NULL)
2922 {
2923 xp->xp_arg = uc->uc_compl_arg;
2924 xp->xp_scriptID = uc->uc_scriptID;
2925 }
2926# endif
2927# endif
2928 /* Do not search for further abbreviations
2929 * if this is an exact match. */
2930 matchlen = k;
2931 if (k == len && *np == NUL)
2932 {
2933 if (full != NULL)
2934 *full = TRUE;
2935 amb_local = FALSE;
2936 break;
2937 }
2938 }
2939 }
2940 }
2941
2942 /* Stop if we found a full match or searched all. */
2943 if (j < gap->ga_len || gap == &ucmds)
2944 break;
2945 gap = &ucmds;
2946 }
2947
2948 /* Only found ambiguous matches. */
2949 if (amb_local)
2950 {
2951 if (xp != NULL)
2952 xp->xp_context = EXPAND_UNSUCCESSFUL;
2953 return NULL;
2954 }
2955
2956 /* The match we found may be followed immediately by a number. Move "p"
2957 * back to point to it. */
2958 if (found || possible)
2959 return p + (matchlen - len);
2960 return p;
2961}
2962#endif
2963
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00002965static struct cmdmod
2966{
2967 char *name;
2968 int minlen;
2969 int has_count; /* :123verbose :3tab */
2970} cmdmods[] = {
2971 {"aboveleft", 3, FALSE},
2972 {"belowright", 3, FALSE},
2973 {"botright", 2, FALSE},
2974 {"browse", 3, FALSE},
2975 {"confirm", 4, FALSE},
2976 {"hide", 3, FALSE},
2977 {"keepalt", 5, FALSE},
2978 {"keepjumps", 5, FALSE},
2979 {"keepmarks", 3, FALSE},
2980 {"leftabove", 5, FALSE},
2981 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00002982 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00002983 {"rightbelow", 6, FALSE},
2984 {"sandbox", 3, FALSE},
2985 {"silent", 3, FALSE},
2986 {"tab", 3, TRUE},
2987 {"topleft", 2, FALSE},
2988 {"verbose", 4, TRUE},
2989 {"vertical", 4, FALSE},
2990};
2991
2992/*
2993 * Return length of a command modifier (including optional count).
2994 * Return zero when it's not a modifier.
2995 */
2996 int
2997modifier_len(cmd)
2998 char_u *cmd;
2999{
3000 int i, j;
3001 char_u *p = cmd;
3002
3003 if (VIM_ISDIGIT(*cmd))
3004 p = skipwhite(skipdigits(cmd));
3005 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
3006 {
3007 for (j = 0; p[j] != NUL; ++j)
3008 if (p[j] != cmdmods[i].name[j])
3009 break;
3010 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3011 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003012 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003013 }
3014 return 0;
3015}
3016
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017/*
3018 * Return > 0 if an Ex command "name" exists.
3019 * Return 2 if there is an exact match.
3020 * Return 3 if there is an ambiguous match.
3021 */
3022 int
3023cmd_exists(name)
3024 char_u *name;
3025{
3026 exarg_T ea;
3027 int full = FALSE;
3028 int i;
3029 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003030 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
3032 /* Check command modifiers. */
3033 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
3034 {
3035 for (j = 0; name[j] != NUL; ++j)
3036 if (name[j] != cmdmods[i].name[j])
3037 break;
3038 if (name[j] == NUL && j >= cmdmods[i].minlen)
3039 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3040 }
3041
Bram Moolenaara9587612006-05-04 21:47:50 +00003042 /* Check built-in commands and user defined commands.
3043 * For ":2match" and ":3match" we need to skip the number. */
3044 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003046 p = find_command(&ea, &full);
3047 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003049 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3050 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003051 if (*skipwhite(p) != NUL)
3052 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3054}
3055#endif
3056
3057/*
3058 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3059 * we don't need/want deleted. Maybe this could be done better if we didn't
3060 * repeat all this stuff. The only problem is that they may not stay
3061 * perfectly compatible with each other, but then the command line syntax
3062 * probably won't change that much -- webb.
3063 */
3064 char_u *
3065set_one_cmd_context(xp, buff)
3066 expand_T *xp;
3067 char_u *buff; /* buffer for command string */
3068{
3069 char_u *p;
3070 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003071 int len = 0;
3072 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3074 int compl = EXPAND_NOTHING;
3075#endif
3076#ifdef FEAT_CMDL_COMPL
3077 int delim;
3078#endif
3079 int forceit = FALSE;
3080 int usefilter = FALSE; /* filter instead of file name */
3081
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003082 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 xp->xp_pattern = buff;
3084 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003085 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086
3087/*
3088 * 2. skip comment lines and leading space, colons or bars
3089 */
3090 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3091 ;
3092 xp->xp_pattern = cmd;
3093
3094 if (*cmd == NUL)
3095 return NULL;
3096 if (*cmd == '"') /* ignore comment lines */
3097 {
3098 xp->xp_context = EXPAND_NOTHING;
3099 return NULL;
3100 }
3101
3102/*
3103 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3104 */
3105 cmd = skip_range(cmd, &xp->xp_context);
3106
3107/*
3108 * 4. parse command
3109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 xp->xp_pattern = cmd;
3111 if (*cmd == NUL)
3112 return NULL;
3113 if (*cmd == '"')
3114 {
3115 xp->xp_context = EXPAND_NOTHING;
3116 return NULL;
3117 }
3118
3119 if (*cmd == '|' || *cmd == '\n')
3120 return cmd + 1; /* There's another command */
3121
3122 /*
3123 * Isolate the command and search for it in the command table.
3124 * Exceptions:
3125 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003126 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3128 */
3129 if (*cmd == 'k' && cmd[1] != 'e')
3130 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003131 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 p = cmd + 1;
3133 }
3134 else
3135 {
3136 p = cmd;
3137 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3138 ++p;
3139 /* check for non-alpha command */
3140 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3141 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003142 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003144 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 {
3146 xp->xp_context = EXPAND_UNSUCCESSFUL;
3147 return NULL;
3148 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003149 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3150 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3151 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 break;
3153
3154#ifdef FEAT_USR_CMDS
3155 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3156 {
3157 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3158 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003159 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 }
3161#endif
3162 }
3163
3164 /*
3165 * If the cursor is touching the command, and it ends in an alpha-numeric
3166 * character, complete the command name.
3167 */
3168 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3169 return NULL;
3170
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003171 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 {
3173 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3174 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003175 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 p = cmd + 1;
3177 }
3178#ifdef FEAT_USR_CMDS
3179 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3180 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003181 ea.cmd = cmd;
3182 p = find_ucmd(&ea, p, NULL, xp,
3183# if defined(FEAT_CMDL_COMPL)
3184 &compl
3185# else
3186 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003188 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003189 if (p == NULL)
3190 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 }
3192#endif
3193 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003194 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 {
3196 /* Not still touching the command and it was an illegal one */
3197 xp->xp_context = EXPAND_UNSUCCESSFUL;
3198 return NULL;
3199 }
3200
3201 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3202
3203 if (*p == '!') /* forced commands */
3204 {
3205 forceit = TRUE;
3206 ++p;
3207 }
3208
3209/*
3210 * 5. parse arguments
3211 */
3212#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003213 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003215 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216
3217 arg = skipwhite(p);
3218
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003219 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 {
3221 if (*arg == '>') /* append */
3222 {
3223 if (*++arg == '>')
3224 ++arg;
3225 arg = skipwhite(arg);
3226 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003227 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 {
3229 ++arg;
3230 usefilter = TRUE;
3231 }
3232 }
3233
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003234 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 {
3236 usefilter = forceit; /* :r! filter if forced */
3237 if (*arg == '!') /* :r !filter */
3238 {
3239 ++arg;
3240 usefilter = TRUE;
3241 }
3242 }
3243
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003244 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 {
3246 while (*arg == *cmd) /* allow any number of '>' or '<' */
3247 ++arg;
3248 arg = skipwhite(arg);
3249 }
3250
3251 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003252 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 {
3254 /* Check if we're in the +command */
3255 p = arg + 1;
3256 arg = skip_cmd_arg(arg, FALSE);
3257
3258 /* Still touching the command after '+'? */
3259 if (*arg == NUL)
3260 return p;
3261
3262 /* Skip space(s) after +command to get to the real argument */
3263 arg = skipwhite(arg);
3264 }
3265
3266 /*
3267 * Check for '|' to separate commands and '"' to start comments.
3268 * Don't do this for ":read !cmd" and ":write !cmd".
3269 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003270 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
3272 p = arg;
3273 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003274 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 p += 2;
3276 while (*p)
3277 {
3278 if (*p == Ctrl_V)
3279 {
3280 if (p[1] != NUL)
3281 ++p;
3282 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003283 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 || *p == '|' || *p == '\n')
3285 {
3286 if (*(p - 1) != '\\')
3287 {
3288 if (*p == '|' || *p == '\n')
3289 return p + 1;
3290 return NULL; /* It's a comment */
3291 }
3292 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003293 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 }
3295 }
3296
3297 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003298 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 vim_strchr((char_u *)"|\"", *arg) == NULL)
3300 return NULL;
3301
3302 /* Find start of last argument (argument just before cursor): */
3303 p = buff + STRLEN(buff);
3304 while (p != arg && *p != ' ' && *p != TAB)
3305 p--;
3306 if (*p == ' ' || *p == TAB)
3307 p++;
3308 xp->xp_pattern = p;
3309
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003310 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003312 int c;
3313 int in_quote = FALSE;
3314 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
3316 /*
3317 * Allow spaces within back-quotes to count as part of the argument
3318 * being expanded.
3319 */
3320 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003321 p = xp->xp_pattern;
3322 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003324#ifdef FEAT_MBYTE
3325 if (has_mbyte)
3326 c = mb_ptr2char(p);
3327 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003329 c = *p;
3330 if (c == '\\' && p[1] != NUL)
3331 ++p;
3332 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
3334 if (!in_quote)
3335 {
3336 xp->xp_pattern = p;
3337 bow = p + 1;
3338 }
3339 in_quote = !in_quote;
3340 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003341 /* An argument can contain just about everything, except
3342 * characters that end the command and white space. */
3343 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003344#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003345 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003346#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003347 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003348 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003349 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003350 while (*p != NUL)
3351 {
3352#ifdef FEAT_MBYTE
3353 if (has_mbyte)
3354 c = mb_ptr2char(p);
3355 else
3356#endif
3357 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003358 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003359 break;
3360#ifdef FEAT_MBYTE
3361 if (has_mbyte)
3362 len = (*mb_ptr2len)(p);
3363 else
3364#endif
3365 len = 1;
3366 mb_ptr_adv(p);
3367 }
3368 if (in_quote)
3369 bow = p;
3370 else
3371 xp->xp_pattern = p;
3372 p -= len;
3373 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003374 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
3376
3377 /*
3378 * If we are still inside the quotes, and we passed a space, just
3379 * expand from there.
3380 */
3381 if (bow != NULL && in_quote)
3382 xp->xp_pattern = bow;
3383 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003384
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003385#ifndef BACKSLASH_IN_FILENAME
3386 /* For a shell command more chars need to be escaped. */
3387 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003388 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003389 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003390
3391 /* When still after the command name expand executables. */
3392 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003393 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003394 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396
3397 /* Check for environment variable */
3398 if (*xp->xp_pattern == '$'
3399#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3400 || *xp->xp_pattern == '%'
3401#endif
3402 )
3403 {
3404 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3405 if (!vim_isIDc(*p))
3406 break;
3407 if (*p == NUL)
3408 {
3409 xp->xp_context = EXPAND_ENV_VARS;
3410 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003411#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3412 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003413 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003414 compl = EXPAND_ENV_VARS;
3415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
3417 }
3418 }
3419
3420/*
3421 * 6. switch on command name
3422 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003423 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {
3425 case CMD_cd:
3426 case CMD_chdir:
3427 case CMD_lcd:
3428 case CMD_lchdir:
3429 if (xp->xp_context == EXPAND_FILES)
3430 xp->xp_context = EXPAND_DIRECTORIES;
3431 break;
3432 case CMD_help:
3433 xp->xp_context = EXPAND_HELP;
3434 xp->xp_pattern = arg;
3435 break;
3436
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003437 /* Command modifiers: return the argument.
3438 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003440 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 case CMD_belowright:
3442 case CMD_botright:
3443 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003444 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003446 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 case CMD_folddoclosed:
3448 case CMD_folddoopen:
3449 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003450 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 case CMD_keepjumps:
3452 case CMD_keepmarks:
3453 case CMD_leftabove:
3454 case CMD_lockmarks:
3455 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003456 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003458 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 case CMD_topleft:
3460 case CMD_verbose:
3461 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003462 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 return arg;
3464
Bram Moolenaar4f688582007-07-24 12:34:30 +00003465#ifdef FEAT_CMDL_COMPL
3466# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 case CMD_match:
3468 if (*arg == NUL || !ends_excmd(*arg))
3469 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003470 /* also complete "None" */
3471 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 arg = skipwhite(skiptowhite(arg));
3473 if (*arg != NUL)
3474 {
3475 xp->xp_context = EXPAND_NOTHING;
3476 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3477 }
3478 }
3479 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482/*
3483 * All completion for the +cmdline_compl feature goes here.
3484 */
3485
3486# ifdef FEAT_USR_CMDS
3487 case CMD_command:
3488 /* Check for attributes */
3489 while (*arg == '-')
3490 {
3491 arg++; /* Skip "-" */
3492 p = skiptowhite(arg);
3493 if (*p == NUL)
3494 {
3495 /* Cursor is still in the attribute */
3496 p = vim_strchr(arg, '=');
3497 if (p == NULL)
3498 {
3499 /* No "=", so complete attribute names */
3500 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3501 xp->xp_pattern = arg;
3502 return NULL;
3503 }
3504
3505 /* For the -complete and -nargs attributes, we complete
3506 * their arguments as well.
3507 */
3508 if (STRNICMP(arg, "complete", p - arg) == 0)
3509 {
3510 xp->xp_context = EXPAND_USER_COMPLETE;
3511 xp->xp_pattern = p + 1;
3512 return NULL;
3513 }
3514 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3515 {
3516 xp->xp_context = EXPAND_USER_NARGS;
3517 xp->xp_pattern = p + 1;
3518 return NULL;
3519 }
3520 return NULL;
3521 }
3522 arg = skipwhite(p);
3523 }
3524
3525 /* After the attributes comes the new command name */
3526 p = skiptowhite(arg);
3527 if (*p == NUL)
3528 {
3529 xp->xp_context = EXPAND_USER_COMMANDS;
3530 xp->xp_pattern = arg;
3531 break;
3532 }
3533
3534 /* And finally comes a normal command */
3535 return skipwhite(p);
3536
3537 case CMD_delcommand:
3538 xp->xp_context = EXPAND_USER_COMMANDS;
3539 xp->xp_pattern = arg;
3540 break;
3541# endif
3542
3543 case CMD_global:
3544 case CMD_vglobal:
3545 delim = *arg; /* get the delimiter */
3546 if (delim)
3547 ++arg; /* skip delimiter if there is one */
3548
3549 while (arg[0] != NUL && arg[0] != delim)
3550 {
3551 if (arg[0] == '\\' && arg[1] != NUL)
3552 ++arg;
3553 ++arg;
3554 }
3555 if (arg[0] != NUL)
3556 return arg + 1;
3557 break;
3558 case CMD_and:
3559 case CMD_substitute:
3560 delim = *arg;
3561 if (delim)
3562 {
3563 /* skip "from" part */
3564 ++arg;
3565 arg = skip_regexp(arg, delim, p_magic, NULL);
3566 }
3567 /* skip "to" part */
3568 while (arg[0] != NUL && arg[0] != delim)
3569 {
3570 if (arg[0] == '\\' && arg[1] != NUL)
3571 ++arg;
3572 ++arg;
3573 }
3574 if (arg[0] != NUL) /* skip delimiter */
3575 ++arg;
3576 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3577 ++arg;
3578 if (arg[0] != NUL)
3579 return arg;
3580 break;
3581 case CMD_isearch:
3582 case CMD_dsearch:
3583 case CMD_ilist:
3584 case CMD_dlist:
3585 case CMD_ijump:
3586 case CMD_psearch:
3587 case CMD_djump:
3588 case CMD_isplit:
3589 case CMD_dsplit:
3590 arg = skipwhite(skipdigits(arg)); /* skip count */
3591 if (*arg == '/') /* Match regexp, not just whole words */
3592 {
3593 for (++arg; *arg && *arg != '/'; arg++)
3594 if (*arg == '\\' && arg[1] != NUL)
3595 arg++;
3596 if (*arg)
3597 {
3598 arg = skipwhite(arg + 1);
3599
3600 /* Check for trailing illegal characters */
3601 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3602 xp->xp_context = EXPAND_NOTHING;
3603 else
3604 return arg;
3605 }
3606 }
3607 break;
3608#ifdef FEAT_AUTOCMD
3609 case CMD_autocmd:
3610 return set_context_in_autocmd(xp, arg, FALSE);
3611
3612 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003613 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 return set_context_in_autocmd(xp, arg, TRUE);
3615#endif
3616 case CMD_set:
3617 set_context_in_set_cmd(xp, arg, 0);
3618 break;
3619 case CMD_setglobal:
3620 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3621 break;
3622 case CMD_setlocal:
3623 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3624 break;
3625 case CMD_tag:
3626 case CMD_stag:
3627 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003628 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 case CMD_tselect:
3630 case CMD_stselect:
3631 case CMD_ptselect:
3632 case CMD_tjump:
3633 case CMD_stjump:
3634 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003635 if (*p_wop != NUL)
3636 xp->xp_context = EXPAND_TAGS_LISTFILES;
3637 else
3638 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 xp->xp_pattern = arg;
3640 break;
3641 case CMD_augroup:
3642 xp->xp_context = EXPAND_AUGROUP;
3643 xp->xp_pattern = arg;
3644 break;
3645#ifdef FEAT_SYN_HL
3646 case CMD_syntax:
3647 set_context_in_syntax_cmd(xp, arg);
3648 break;
3649#endif
3650#ifdef FEAT_EVAL
3651 case CMD_let:
3652 case CMD_if:
3653 case CMD_elseif:
3654 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003655 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 case CMD_echo:
3657 case CMD_echon:
3658 case CMD_execute:
3659 case CMD_echomsg:
3660 case CMD_echoerr:
3661 case CMD_call:
3662 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003663 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 break;
3665
3666 case CMD_unlet:
3667 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3668 arg = xp->xp_pattern + 1;
3669 xp->xp_context = EXPAND_USER_VARS;
3670 xp->xp_pattern = arg;
3671 break;
3672
3673 case CMD_function:
3674 case CMD_delfunction:
3675 xp->xp_context = EXPAND_USER_FUNC;
3676 xp->xp_pattern = arg;
3677 break;
3678
3679 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003680 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 break;
3682#endif
3683 case CMD_highlight:
3684 set_context_in_highlight_cmd(xp, arg);
3685 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003686#ifdef FEAT_CSCOPE
3687 case CMD_cscope:
3688 set_context_in_cscope_cmd(xp, arg);
3689 break;
3690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691#ifdef FEAT_LISTCMDS
3692 case CMD_bdelete:
3693 case CMD_bwipeout:
3694 case CMD_bunload:
3695 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3696 arg = xp->xp_pattern + 1;
3697 /*FALLTHROUGH*/
3698 case CMD_buffer:
3699 case CMD_sbuffer:
3700 case CMD_checktime:
3701 xp->xp_context = EXPAND_BUFFERS;
3702 xp->xp_pattern = arg;
3703 break;
3704#endif
3705#ifdef FEAT_USR_CMDS
3706 case CMD_USER:
3707 case CMD_USER_BUF:
3708 if (compl != EXPAND_NOTHING)
3709 {
3710 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003711 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 {
3713# ifdef FEAT_MENU
3714 if (compl == EXPAND_MENUS)
3715 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3716# endif
3717 if (compl == EXPAND_COMMANDS)
3718 return arg;
3719 if (compl == EXPAND_MAPPINGS)
3720 return set_context_in_map_cmd(xp, (char_u *)"map",
3721 arg, forceit, FALSE, FALSE, CMD_map);
3722 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3723 arg = xp->xp_pattern + 1;
3724 xp->xp_pattern = arg;
3725 }
3726 xp->xp_context = compl;
3727 }
3728 break;
3729#endif
3730 case CMD_map: case CMD_noremap:
3731 case CMD_nmap: case CMD_nnoremap:
3732 case CMD_vmap: case CMD_vnoremap:
3733 case CMD_omap: case CMD_onoremap:
3734 case CMD_imap: case CMD_inoremap:
3735 case CMD_cmap: case CMD_cnoremap:
3736 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003737 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 case CMD_unmap:
3739 case CMD_nunmap:
3740 case CMD_vunmap:
3741 case CMD_ounmap:
3742 case CMD_iunmap:
3743 case CMD_cunmap:
3744 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003745 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 case CMD_abbreviate: case CMD_noreabbrev:
3747 case CMD_cabbrev: case CMD_cnoreabbrev:
3748 case CMD_iabbrev: case CMD_inoreabbrev:
3749 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003750 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 case CMD_unabbreviate:
3752 case CMD_cunabbrev:
3753 case CMD_iunabbrev:
3754 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003755 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756#ifdef FEAT_MENU
3757 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3758 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3759 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3760 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3761 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3762 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3763 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3764 case CMD_tmenu: case CMD_tunmenu:
3765 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3766 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3767#endif
3768
3769 case CMD_colorscheme:
3770 xp->xp_context = EXPAND_COLORS;
3771 xp->xp_pattern = arg;
3772 break;
3773
3774 case CMD_compiler:
3775 xp->xp_context = EXPAND_COMPILER;
3776 xp->xp_pattern = arg;
3777 break;
3778
3779#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3780 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3781 case CMD_language:
3782 if (*skiptowhite(arg) == NUL)
3783 {
3784 xp->xp_context = EXPAND_LANGUAGE;
3785 xp->xp_pattern = arg;
3786 }
3787 else
3788 xp->xp_context = EXPAND_NOTHING;
3789 break;
3790#endif
3791
3792#endif /* FEAT_CMDL_COMPL */
3793
3794 default:
3795 break;
3796 }
3797 return NULL;
3798}
3799
3800/*
3801 * skip a range specifier of the form: addr [,addr] [;addr] ..
3802 *
3803 * Backslashed delimiters after / or ? will be skipped, and commands will
3804 * not be expanded between /'s and ?'s or after "'".
3805 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003806 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 * Returns the "cmd" pointer advanced to beyond the range.
3808 */
3809 char_u *
3810skip_range(cmd, ctx)
3811 char_u *cmd;
3812 int *ctx; /* pointer to xp_context or NULL */
3813{
3814 int delim;
3815
Bram Moolenaardf177f62005-02-22 08:39:57 +00003816 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 {
3818 if (*cmd == '\'')
3819 {
3820 if (*++cmd == NUL && ctx != NULL)
3821 *ctx = EXPAND_NOTHING;
3822 }
3823 else if (*cmd == '/' || *cmd == '?')
3824 {
3825 delim = *cmd++;
3826 while (*cmd != NUL && *cmd != delim)
3827 if (*cmd++ == '\\' && *cmd != NUL)
3828 ++cmd;
3829 if (*cmd == NUL && ctx != NULL)
3830 *ctx = EXPAND_NOTHING;
3831 }
3832 if (*cmd != NUL)
3833 ++cmd;
3834 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003835
3836 /* Skip ":" and white space. */
3837 while (*cmd == ':')
3838 cmd = skipwhite(cmd + 1);
3839
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 return cmd;
3841}
3842
3843/*
3844 * get a single EX address
3845 *
3846 * Set ptr to the next character after the part that was interpreted.
3847 * Set ptr to NULL when an error is encountered.
3848 *
3849 * Return MAXLNUM when no Ex address was found.
3850 */
3851 static linenr_T
3852get_address(ptr, skip, to_other_file)
3853 char_u **ptr;
3854 int skip; /* only skip the address, don't use it */
3855 int to_other_file; /* flag: may jump to other file */
3856{
3857 int c;
3858 int i;
3859 long n;
3860 char_u *cmd;
3861 pos_T pos;
3862 pos_T *fp;
3863 linenr_T lnum;
3864
3865 cmd = skipwhite(*ptr);
3866 lnum = MAXLNUM;
3867 do
3868 {
3869 switch (*cmd)
3870 {
3871 case '.': /* '.' - Cursor position */
3872 ++cmd;
3873 lnum = curwin->w_cursor.lnum;
3874 break;
3875
3876 case '$': /* '$' - last line */
3877 ++cmd;
3878 lnum = curbuf->b_ml.ml_line_count;
3879 break;
3880
3881 case '\'': /* ''' - mark */
3882 if (*++cmd == NUL)
3883 {
3884 cmd = NULL;
3885 goto error;
3886 }
3887 if (skip)
3888 ++cmd;
3889 else
3890 {
3891 /* Only accept a mark in another file when it is
3892 * used by itself: ":'M". */
3893 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3894 ++cmd;
3895 if (fp == (pos_T *)-1)
3896 /* Jumped to another file. */
3897 lnum = curwin->w_cursor.lnum;
3898 else
3899 {
3900 if (check_mark(fp) == FAIL)
3901 {
3902 cmd = NULL;
3903 goto error;
3904 }
3905 lnum = fp->lnum;
3906 }
3907 }
3908 break;
3909
3910 case '/':
3911 case '?': /* '/' or '?' - search */
3912 c = *cmd++;
3913 if (skip) /* skip "/pat/" */
3914 {
3915 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3916 if (*cmd == c)
3917 ++cmd;
3918 }
3919 else
3920 {
3921 pos = curwin->w_cursor; /* save curwin->w_cursor */
3922 /*
3923 * When '/' or '?' follows another address, start
3924 * from there.
3925 */
3926 if (lnum != MAXLNUM)
3927 curwin->w_cursor.lnum = lnum;
3928 /*
3929 * Start a forward search at the end of the line.
3930 * Start a backward search at the start of the line.
3931 * This makes sure we never match in the current
3932 * line, and can match anywhere in the
3933 * next/previous line.
3934 */
3935 if (c == '/')
3936 curwin->w_cursor.col = MAXCOL;
3937 else
3938 curwin->w_cursor.col = 0;
3939 searchcmdlen = 0;
3940 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00003941 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
3943 curwin->w_cursor = pos;
3944 cmd = NULL;
3945 goto error;
3946 }
3947 lnum = curwin->w_cursor.lnum;
3948 curwin->w_cursor = pos;
3949 /* adjust command string pointer */
3950 cmd += searchcmdlen;
3951 }
3952 break;
3953
3954 case '\\': /* "\?", "\/" or "\&", repeat search */
3955 ++cmd;
3956 if (*cmd == '&')
3957 i = RE_SUBST;
3958 else if (*cmd == '?' || *cmd == '/')
3959 i = RE_SEARCH;
3960 else
3961 {
3962 EMSG(_(e_backslash));
3963 cmd = NULL;
3964 goto error;
3965 }
3966
3967 if (!skip)
3968 {
3969 /*
3970 * When search follows another address, start from
3971 * there.
3972 */
3973 if (lnum != MAXLNUM)
3974 pos.lnum = lnum;
3975 else
3976 pos.lnum = curwin->w_cursor.lnum;
3977
3978 /*
3979 * Start the search just like for the above
3980 * do_search().
3981 */
3982 if (*cmd != '?')
3983 pos.col = MAXCOL;
3984 else
3985 pos.col = 0;
3986 if (searchit(curwin, curbuf, &pos,
3987 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00003988 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00003989 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 lnum = pos.lnum;
3991 else
3992 {
3993 cmd = NULL;
3994 goto error;
3995 }
3996 }
3997 ++cmd;
3998 break;
3999
4000 default:
4001 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4002 lnum = getdigits(&cmd);
4003 }
4004
4005 for (;;)
4006 {
4007 cmd = skipwhite(cmd);
4008 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4009 break;
4010
4011 if (lnum == MAXLNUM)
4012 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4013 if (VIM_ISDIGIT(*cmd))
4014 i = '+'; /* "number" is same as "+number" */
4015 else
4016 i = *cmd++;
4017 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4018 n = 1;
4019 else
4020 n = getdigits(&cmd);
4021 if (i == '-')
4022 lnum -= n;
4023 else
4024 lnum += n;
4025 }
4026 } while (*cmd == '/' || *cmd == '?');
4027
4028error:
4029 *ptr = cmd;
4030 return lnum;
4031}
4032
4033/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004034 * Get flags from an Ex command argument.
4035 */
4036 static void
4037get_flags(eap)
4038 exarg_T *eap;
4039{
4040 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4041 {
4042 if (*eap->arg == 'l')
4043 eap->flags |= EXFLAG_LIST;
4044 else if (*eap->arg == 'p')
4045 eap->flags |= EXFLAG_PRINT;
4046 else
4047 eap->flags |= EXFLAG_NR;
4048 eap->arg = skipwhite(eap->arg + 1);
4049 }
4050}
4051
4052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 * Function called for command which is Not Implemented. NI!
4054 */
4055 void
4056ex_ni(eap)
4057 exarg_T *eap;
4058{
4059 if (!eap->skip)
4060 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4061}
4062
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004063#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064/*
4065 * Function called for script command which is Not Implemented. NI!
4066 * Skips over ":perl <<EOF" constructs.
4067 */
4068 static void
4069ex_script_ni(eap)
4070 exarg_T *eap;
4071{
4072 if (!eap->skip)
4073 ex_ni(eap);
4074 else
4075 vim_free(script_get(eap, eap->arg));
4076}
4077#endif
4078
4079/*
4080 * Check range in Ex command for validity.
4081 * Return NULL when valid, error message when invalid.
4082 */
4083 static char_u *
4084invalid_range(eap)
4085 exarg_T *eap;
4086{
4087 if ( eap->line1 < 0
4088 || eap->line2 < 0
4089 || eap->line1 > eap->line2
4090 || ((eap->argt & RANGE)
4091 && !(eap->argt & NOTADR)
4092 && eap->line2 > curbuf->b_ml.ml_line_count
4093#ifdef FEAT_DIFF
4094 + (eap->cmdidx == CMD_diffget)
4095#endif
4096 ))
4097 return (char_u *)_(e_invrange);
4098 return NULL;
4099}
4100
4101/*
4102 * Correct the range for zero line number, if required.
4103 */
4104 static void
4105correct_range(eap)
4106 exarg_T *eap;
4107{
4108 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4109 {
4110 if (eap->line1 == 0)
4111 eap->line1 = 1;
4112 if (eap->line2 == 0)
4113 eap->line2 = 1;
4114 }
4115}
4116
Bram Moolenaar748bf032005-02-02 23:04:36 +00004117#ifdef FEAT_QUICKFIX
4118static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4119
4120/*
4121 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4122 * pattern. Otherwise return eap->arg.
4123 */
4124 static char_u *
4125skip_grep_pat(eap)
4126 exarg_T *eap;
4127{
4128 char_u *p = eap->arg;
4129
Bram Moolenaara37420f2006-02-04 22:37:47 +00004130 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4131 || eap->cmdidx == CMD_vimgrepadd
4132 || eap->cmdidx == CMD_lvimgrepadd
4133 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004134 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004135 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004136 if (p == NULL)
4137 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004138 }
4139 return p;
4140}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004141
4142/*
4143 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4144 * in the command line, so that things like % get expanded.
4145 */
4146 static char_u *
4147replace_makeprg(eap, p, cmdlinep)
4148 exarg_T *eap;
4149 char_u *p;
4150 char_u **cmdlinep;
4151{
4152 char_u *new_cmdline;
4153 char_u *program;
4154 char_u *pos;
4155 char_u *ptr;
4156 int len;
4157 int i;
4158
4159 /*
4160 * Don't do it when ":vimgrep" is used for ":grep".
4161 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004162 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4163 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4164 || eap->cmdidx == CMD_grepadd
4165 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004166 && !grep_internal(eap->cmdidx))
4167 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004168 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4169 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004170 {
4171 if (*curbuf->b_p_gp == NUL)
4172 program = p_gp;
4173 else
4174 program = curbuf->b_p_gp;
4175 }
4176 else
4177 {
4178 if (*curbuf->b_p_mp == NUL)
4179 program = p_mp;
4180 else
4181 program = curbuf->b_p_mp;
4182 }
4183
4184 p = skipwhite(p);
4185
4186 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4187 {
4188 /* replace $* by given arguments */
4189 i = 1;
4190 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4191 ++i;
4192 len = (int)STRLEN(p);
4193 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4194 if (new_cmdline == NULL)
4195 return NULL; /* out of memory */
4196 ptr = new_cmdline;
4197 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4198 {
4199 i = (int)(pos - program);
4200 STRNCPY(ptr, program, i);
4201 STRCPY(ptr += i, p);
4202 ptr += len;
4203 program = pos + 2;
4204 }
4205 STRCPY(ptr, program);
4206 }
4207 else
4208 {
4209 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4210 if (new_cmdline == NULL)
4211 return NULL; /* out of memory */
4212 STRCPY(new_cmdline, program);
4213 STRCAT(new_cmdline, " ");
4214 STRCAT(new_cmdline, p);
4215 }
4216 msg_make(p);
4217
4218 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4219 vim_free(*cmdlinep);
4220 *cmdlinep = new_cmdline;
4221 p = new_cmdline;
4222 }
4223 return p;
4224}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004225#endif
4226
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227/*
4228 * Expand file name in Ex command argument.
4229 * Return FAIL for failure, OK otherwise.
4230 */
4231 int
4232expand_filename(eap, cmdlinep, errormsgp)
4233 exarg_T *eap;
4234 char_u **cmdlinep;
4235 char_u **errormsgp;
4236{
4237 int has_wildcards; /* need to expand wildcards */
4238 char_u *repl;
4239 int srclen;
4240 char_u *p;
4241 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004242 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
Bram Moolenaar748bf032005-02-02 23:04:36 +00004244#ifdef FEAT_QUICKFIX
4245 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4246 p = skip_grep_pat(eap);
4247#else
4248 p = eap->arg;
4249#endif
4250
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 /*
4252 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4253 * the file name contains a wildcard it should not cause expanding.
4254 * (it will be expanded anyway if there is a wildcard before replacing).
4255 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004256 has_wildcards = mch_has_wildcard(p);
4257 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004259#ifdef FEAT_EVAL
4260 /* Skip over `=expr`, wildcards in it are not expanded. */
4261 if (p[0] == '`' && p[1] == '=')
4262 {
4263 p += 2;
4264 (void)skip_expr(&p);
4265 if (*p == '`')
4266 ++p;
4267 continue;
4268 }
4269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 /*
4271 * Quick check if this cannot be the start of a special string.
4272 * Also removes backslash before '%', '#' and '<'.
4273 */
4274 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4275 {
4276 ++p;
4277 continue;
4278 }
4279
4280 /*
4281 * Try to find a match at this position.
4282 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004283 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4284 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 if (*errormsgp != NULL) /* error detected */
4286 return FAIL;
4287 if (repl == NULL) /* no match found */
4288 {
4289 p += srclen;
4290 continue;
4291 }
4292
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004293 /* Wildcards won't be expanded below, the replacement is taken
4294 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4295 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4296 {
4297 char_u *l = repl;
4298
4299 repl = expand_env_save(repl);
4300 vim_free(l);
4301 }
4302
Bram Moolenaar63b92542007-03-27 14:57:09 +00004303 /* Need to escape white space et al. with a backslash.
4304 * Don't do this for:
4305 * - replacement that already has been escaped: "##"
4306 * - shell commands (may have to use quotes instead).
4307 * - non-unix systems when there is a single argument (spaces don't
4308 * separate arguments then).
4309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004311 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 && eap->cmdidx != CMD_bang
4313 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004314 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004316 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004318 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319#ifndef UNIX
4320 && !(eap->argt & NOSPC)
4321#endif
4322 )
4323 {
4324 char_u *l;
4325#ifdef BACKSLASH_IN_FILENAME
4326 /* Don't escape a backslash here, because rem_backslash() doesn't
4327 * remove it later. */
4328 static char_u *nobslash = (char_u *)" \t\"|";
4329# define ESCAPE_CHARS nobslash
4330#else
4331# define ESCAPE_CHARS escape_chars
4332#endif
4333
4334 for (l = repl; *l; ++l)
4335 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4336 {
4337 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4338 if (l != NULL)
4339 {
4340 vim_free(repl);
4341 repl = l;
4342 }
4343 break;
4344 }
4345 }
4346
4347 /* For a shell command a '!' must be escaped. */
4348 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004349 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 {
4351 char_u *l;
4352
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004353 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 if (l != NULL)
4355 {
4356 vim_free(repl);
4357 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004358 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 if (strstr((char *)p_sh, "sh") != NULL)
4360 {
4361 l = vim_strsave_escaped(repl, (char_u *)"!");
4362 if (l != NULL)
4363 {
4364 vim_free(repl);
4365 repl = l;
4366 }
4367 }
4368 }
4369 }
4370
4371 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4372 vim_free(repl);
4373 if (p == NULL)
4374 return FAIL;
4375 }
4376
4377 /*
4378 * One file argument: Expand wildcards.
4379 * Don't do this with ":r !command" or ":w !command".
4380 */
4381 if ((eap->argt & NOSPC) && !eap->usefilter)
4382 {
4383 /*
4384 * May do this twice:
4385 * 1. Replace environment variables.
4386 * 2. Replace any other wildcards, remove backslashes.
4387 */
4388 for (n = 1; n <= 2; ++n)
4389 {
4390 if (n == 2)
4391 {
4392#ifdef UNIX
4393 /*
4394 * Only for Unix we check for more than one file name.
4395 * For other systems spaces are considered to be part
4396 * of the file name.
4397 * Only check here if there is no wildcard, otherwise
4398 * ExpandOne() will check for errors. This allows
4399 * ":e `ls ve*.c`" on Unix.
4400 */
4401 if (!has_wildcards)
4402 for (p = eap->arg; *p; ++p)
4403 {
4404 /* skip escaped characters */
4405 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4406 ++p;
4407 else if (vim_iswhite(*p))
4408 {
4409 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4410 return FAIL;
4411 }
4412 }
4413#endif
4414
4415 /*
4416 * Halve the number of backslashes (this is Vi compatible).
4417 * For Unix and OS/2, when wildcards are expanded, this is
4418 * done by ExpandOne() below.
4419 */
4420#if defined(UNIX) || defined(OS2)
4421 if (!has_wildcards)
4422#endif
4423 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425
4426 if (has_wildcards)
4427 {
4428 if (n == 1)
4429 {
4430 /*
4431 * First loop: May expand environment variables. This
4432 * can be done much faster with expand_env() than with
4433 * something else (e.g., calling a shell).
4434 * After expanding environment variables, check again
4435 * if there are still wildcards present.
4436 */
4437 if (vim_strchr(eap->arg, '$') != NULL
4438 || vim_strchr(eap->arg, '~') != NULL)
4439 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004440 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004441 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 has_wildcards = mch_has_wildcard(NameBuff);
4443 p = NameBuff;
4444 }
4445 else
4446 p = NULL;
4447 }
4448 else /* n == 2 */
4449 {
4450 expand_T xpc;
4451
4452 ExpandInit(&xpc);
4453 xpc.xp_context = EXPAND_FILES;
4454 p = ExpandOne(&xpc, eap->arg, NULL,
4455 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4456 WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 if (p == NULL)
4458 return FAIL;
4459 }
4460 if (p != NULL)
4461 {
4462 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4463 p, cmdlinep);
4464 if (n == 2) /* p came from ExpandOne() */
4465 vim_free(p);
4466 }
4467 }
4468 }
4469 }
4470 return OK;
4471}
4472
4473/*
4474 * Replace part of the command line, keeping eap->cmd, eap->arg and
4475 * eap->nextcmd correct.
4476 * "src" points to the part that is to be replaced, of length "srclen".
4477 * "repl" is the replacement string.
4478 * Returns a pointer to the character after the replaced string.
4479 * Returns NULL for failure.
4480 */
4481 static char_u *
4482repl_cmdline(eap, src, srclen, repl, cmdlinep)
4483 exarg_T *eap;
4484 char_u *src;
4485 int srclen;
4486 char_u *repl;
4487 char_u **cmdlinep;
4488{
4489 int len;
4490 int i;
4491 char_u *new_cmdline;
4492
4493 /*
4494 * The new command line is build in new_cmdline[].
4495 * First allocate it.
4496 * Careful: a "+cmd" argument may have been NUL terminated.
4497 */
4498 len = (int)STRLEN(repl);
4499 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004500 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4502 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4503 return NULL; /* out of memory! */
4504
4505 /*
4506 * Copy the stuff before the expanded part.
4507 * Copy the expanded stuff.
4508 * Copy what came after the expanded part.
4509 * Copy the next commands, if there are any.
4510 */
4511 i = (int)(src - *cmdlinep); /* length of part before match */
4512 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004513
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 mch_memmove(new_cmdline + i, repl, (size_t)len);
4515 i += len; /* remember the end of the string */
4516 STRCPY(new_cmdline + i, src + srclen);
4517 src = new_cmdline + i; /* remember where to continue */
4518
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004519 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 {
4521 i = (int)STRLEN(new_cmdline) + 1;
4522 STRCPY(new_cmdline + i, eap->nextcmd);
4523 eap->nextcmd = new_cmdline + i;
4524 }
4525 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4526 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4527 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4528 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4529 vim_free(*cmdlinep);
4530 *cmdlinep = new_cmdline;
4531
4532 return src;
4533}
4534
4535/*
4536 * Check for '|' to separate commands and '"' to start comments.
4537 */
4538 void
4539separate_nextcmd(eap)
4540 exarg_T *eap;
4541{
4542 char_u *p;
4543
Bram Moolenaar86b68352004-12-27 21:59:20 +00004544#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004545 p = skip_grep_pat(eap);
4546#else
4547 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004548#endif
4549
4550 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
4552 if (*p == Ctrl_V)
4553 {
4554 if (eap->argt & (USECTRLV | XFILE))
4555 ++p; /* skip CTRL-V and next char */
4556 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004557 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004558 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 if (*p == NUL) /* stop at NUL after CTRL-V */
4560 break;
4561 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004562
4563#ifdef FEAT_EVAL
4564 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004565 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004566 {
4567 p += 2;
4568 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004569 }
4570#endif
4571
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 /* Check for '"': start of comment or '|': next command */
4573 /* :@" and :*" do not start a comment!
4574 * :redir @" doesn't either. */
4575 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4576 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4577 || p != eap->arg)
4578 && (eap->cmdidx != CMD_redir
4579 || p != eap->arg + 1 || p[-1] != '@'))
4580 || *p == '|' || *p == '\n')
4581 {
4582 /*
4583 * We remove the '\' before the '|', unless USECTRLV is used
4584 * AND 'b' is present in 'cpoptions'.
4585 */
4586 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4587 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4588 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004589 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 --p;
4591 }
4592 else
4593 {
4594 eap->nextcmd = check_nextcmd(p);
4595 *p = NUL;
4596 break;
4597 }
4598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004600
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4602 del_trailing_spaces(eap->arg);
4603}
4604
4605/*
4606 * get + command from ex argument
4607 */
4608 static char_u *
4609getargcmd(argp)
4610 char_u **argp;
4611{
4612 char_u *arg = *argp;
4613 char_u *command = NULL;
4614
4615 if (*arg == '+') /* +[command] */
4616 {
4617 ++arg;
4618 if (vim_isspace(*arg))
4619 command = dollar_command;
4620 else
4621 {
4622 command = arg;
4623 arg = skip_cmd_arg(command, TRUE);
4624 if (*arg != NUL)
4625 *arg++ = NUL; /* terminate command with NUL */
4626 }
4627
4628 arg = skipwhite(arg); /* skip over spaces */
4629 *argp = arg;
4630 }
4631 return command;
4632}
4633
4634/*
4635 * Find end of "+command" argument. Skip over "\ " and "\\".
4636 */
4637 static char_u *
4638skip_cmd_arg(p, rembs)
4639 char_u *p;
4640 int rembs; /* TRUE to halve the number of backslashes */
4641{
4642 while (*p && !vim_isspace(*p))
4643 {
4644 if (*p == '\\' && p[1] != NUL)
4645 {
4646 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004647 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 else
4649 ++p;
4650 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004651 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
4653 return p;
4654}
4655
4656/*
4657 * Get "++opt=arg" argument.
4658 * Return FAIL or OK.
4659 */
4660 static int
4661getargopt(eap)
4662 exarg_T *eap;
4663{
4664 char_u *arg = eap->arg + 2;
4665 int *pp = NULL;
4666#ifdef FEAT_MBYTE
4667 char_u *p;
4668#endif
4669
4670 /* ":edit ++[no]bin[ary] file" */
4671 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4672 {
4673 if (*arg == 'n')
4674 {
4675 arg += 2;
4676 eap->force_bin = FORCE_NOBIN;
4677 }
4678 else
4679 eap->force_bin = FORCE_BIN;
4680 if (!checkforcmd(&arg, "binary", 3))
4681 return FAIL;
4682 eap->arg = skipwhite(arg);
4683 return OK;
4684 }
4685
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004686 /* ":read ++edit file" */
4687 if (STRNCMP(arg, "edit", 4) == 0)
4688 {
4689 eap->read_edit = TRUE;
4690 eap->arg = skipwhite(arg + 4);
4691 return OK;
4692 }
4693
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 if (STRNCMP(arg, "ff", 2) == 0)
4695 {
4696 arg += 2;
4697 pp = &eap->force_ff;
4698 }
4699 else if (STRNCMP(arg, "fileformat", 10) == 0)
4700 {
4701 arg += 10;
4702 pp = &eap->force_ff;
4703 }
4704#ifdef FEAT_MBYTE
4705 else if (STRNCMP(arg, "enc", 3) == 0)
4706 {
4707 arg += 3;
4708 pp = &eap->force_enc;
4709 }
4710 else if (STRNCMP(arg, "encoding", 8) == 0)
4711 {
4712 arg += 8;
4713 pp = &eap->force_enc;
4714 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004715 else if (STRNCMP(arg, "bad", 3) == 0)
4716 {
4717 arg += 3;
4718 pp = &eap->bad_char;
4719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720#endif
4721
4722 if (pp == NULL || *arg != '=')
4723 return FAIL;
4724
4725 ++arg;
4726 *pp = (int)(arg - eap->cmd);
4727 arg = skip_cmd_arg(arg, FALSE);
4728 eap->arg = skipwhite(arg);
4729 *arg = NUL;
4730
4731#ifdef FEAT_MBYTE
4732 if (pp == &eap->force_ff)
4733 {
4734#endif
4735 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4736 return FAIL;
4737#ifdef FEAT_MBYTE
4738 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004739 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
4741 /* Make 'fileencoding' lower case. */
4742 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4743 *p = TOLOWER_ASC(*p);
4744 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004745 else
4746 {
4747 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4748 * "drop". */
4749 p = eap->cmd + eap->bad_char;
4750 if (STRICMP(p, "keep") == 0)
4751 eap->bad_char = BAD_KEEP;
4752 else if (STRICMP(p, "drop") == 0)
4753 eap->bad_char = BAD_DROP;
4754 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4755 eap->bad_char = *p;
4756 else
4757 return FAIL;
4758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759#endif
4760
4761 return OK;
4762}
4763
4764/*
4765 * ":abbreviate" and friends.
4766 */
4767 static void
4768ex_abbreviate(eap)
4769 exarg_T *eap;
4770{
4771 do_exmap(eap, TRUE); /* almost the same as mapping */
4772}
4773
4774/*
4775 * ":map" and friends.
4776 */
4777 static void
4778ex_map(eap)
4779 exarg_T *eap;
4780{
4781 /*
4782 * If we are sourcing .exrc or .vimrc in current directory we
4783 * print the mappings for security reasons.
4784 */
4785 if (secure)
4786 {
4787 secure = 2;
4788 msg_outtrans(eap->cmd);
4789 msg_putchar('\n');
4790 }
4791 do_exmap(eap, FALSE);
4792}
4793
4794/*
4795 * ":unmap" and friends.
4796 */
4797 static void
4798ex_unmap(eap)
4799 exarg_T *eap;
4800{
4801 do_exmap(eap, FALSE);
4802}
4803
4804/*
4805 * ":mapclear" and friends.
4806 */
4807 static void
4808ex_mapclear(eap)
4809 exarg_T *eap;
4810{
4811 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4812}
4813
4814/*
4815 * ":abclear" and friends.
4816 */
4817 static void
4818ex_abclear(eap)
4819 exarg_T *eap;
4820{
4821 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4822}
4823
4824#ifdef FEAT_AUTOCMD
4825 static void
4826ex_autocmd(eap)
4827 exarg_T *eap;
4828{
4829 /*
4830 * Disallow auto commands from .exrc and .vimrc in current
4831 * directory for security reasons.
4832 */
4833 if (secure)
4834 {
4835 secure = 2;
4836 eap->errmsg = e_curdir;
4837 }
4838 else if (eap->cmdidx == CMD_autocmd)
4839 do_autocmd(eap->arg, eap->forceit);
4840 else
4841 do_augroup(eap->arg, eap->forceit);
4842}
4843
4844/*
4845 * ":doautocmd": Apply the automatic commands to the current buffer.
4846 */
4847 static void
4848ex_doautocmd(eap)
4849 exarg_T *eap;
4850{
4851 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004852 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853}
4854#endif
4855
4856#ifdef FEAT_LISTCMDS
4857/*
4858 * :[N]bunload[!] [N] [bufname] unload buffer
4859 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4860 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4861 */
4862 static void
4863ex_bunload(eap)
4864 exarg_T *eap;
4865{
4866 eap->errmsg = do_bufdel(
4867 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4868 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4869 : DOBUF_UNLOAD, eap->arg,
4870 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4871}
4872
4873/*
4874 * :[N]buffer [N] to buffer N
4875 * :[N]sbuffer [N] to buffer N
4876 */
4877 static void
4878ex_buffer(eap)
4879 exarg_T *eap;
4880{
4881 if (*eap->arg)
4882 eap->errmsg = e_trailing;
4883 else
4884 {
4885 if (eap->addr_count == 0) /* default is current buffer */
4886 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4887 else
4888 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4889 }
4890}
4891
4892/*
4893 * :[N]bmodified [N] to next mod. buffer
4894 * :[N]sbmodified [N] to next mod. buffer
4895 */
4896 static void
4897ex_bmodified(eap)
4898 exarg_T *eap;
4899{
4900 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4901}
4902
4903/*
4904 * :[N]bnext [N] to next buffer
4905 * :[N]sbnext [N] split and to next buffer
4906 */
4907 static void
4908ex_bnext(eap)
4909 exarg_T *eap;
4910{
4911 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4912}
4913
4914/*
4915 * :[N]bNext [N] to previous buffer
4916 * :[N]bprevious [N] to previous buffer
4917 * :[N]sbNext [N] split and to previous buffer
4918 * :[N]sbprevious [N] split and to previous buffer
4919 */
4920 static void
4921ex_bprevious(eap)
4922 exarg_T *eap;
4923{
4924 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4925}
4926
4927/*
4928 * :brewind to first buffer
4929 * :bfirst to first buffer
4930 * :sbrewind split and to first buffer
4931 * :sbfirst split and to first buffer
4932 */
4933 static void
4934ex_brewind(eap)
4935 exarg_T *eap;
4936{
4937 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4938}
4939
4940/*
4941 * :blast to last buffer
4942 * :sblast split and to last buffer
4943 */
4944 static void
4945ex_blast(eap)
4946 exarg_T *eap;
4947{
4948 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4949}
4950#endif
4951
4952 int
4953ends_excmd(c)
4954 int c;
4955{
4956 return (c == NUL || c == '|' || c == '"' || c == '\n');
4957}
4958
4959#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4960 || defined(PROTO)
4961/*
4962 * Return the next command, after the first '|' or '\n'.
4963 * Return NULL if not found.
4964 */
4965 char_u *
4966find_nextcmd(p)
4967 char_u *p;
4968{
4969 while (*p != '|' && *p != '\n')
4970 {
4971 if (*p == NUL)
4972 return NULL;
4973 ++p;
4974 }
4975 return (p + 1);
4976}
4977#endif
4978
4979/*
4980 * Check if *p is a separator between Ex commands.
4981 * Return NULL if it isn't, (p + 1) if it is.
4982 */
4983 char_u *
4984check_nextcmd(p)
4985 char_u *p;
4986{
4987 p = skipwhite(p);
4988 if (*p == '|' || *p == '\n')
4989 return (p + 1);
4990 else
4991 return NULL;
4992}
4993
4994/*
4995 * - if there are more files to edit
4996 * - and this is the last window
4997 * - and forceit not used
4998 * - and not repeated twice on a row
4999 * return FAIL and give error message if 'message' TRUE
5000 * return OK otherwise
5001 */
5002 static int
5003check_more(message, forceit)
5004 int message; /* when FALSE check only, no messages */
5005 int forceit;
5006{
5007 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5008
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005009 if (!forceit && only_one_window()
5010 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 {
5012 if (message)
5013 {
5014#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5015 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5016 {
5017 char_u buff[IOSIZE];
5018
5019 if (n == 1)
5020 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
5021 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005022 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 _("%d more files to edit. Quit anyway?"), n);
5024 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5025 return OK;
5026 return FAIL;
5027 }
5028#endif
5029 if (n == 1)
5030 EMSG(_("E173: 1 more file to edit"));
5031 else
5032 EMSGN(_("E173: %ld more files to edit"), n);
5033 quitmore = 2; /* next try to quit is allowed */
5034 }
5035 return FAIL;
5036 }
5037 return OK;
5038}
5039
5040#ifdef FEAT_CMDL_COMPL
5041/*
5042 * Function given to ExpandGeneric() to obtain the list of command names.
5043 */
5044/*ARGSUSED*/
5045 char_u *
5046get_command_name(xp, idx)
5047 expand_T *xp;
5048 int idx;
5049{
5050 if (idx >= (int)CMD_SIZE)
5051# ifdef FEAT_USR_CMDS
5052 return get_user_command_name(idx);
5053# else
5054 return NULL;
5055# endif
5056 return cmdnames[idx].cmd_name;
5057}
5058#endif
5059
5060#if defined(FEAT_USR_CMDS) || defined(PROTO)
5061static int uc_add_command __ARGS((char_u *name, size_t name_len, char_u *rep, long argt, long def, int flags, int compl, char_u *compl_arg, int force));
5062static void uc_list __ARGS((char_u *name, size_t name_len));
5063static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5064static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5065static size_t uc_check_code __ARGS((char_u *code, size_t len, char_u *buf, ucmd_T *cmd, exarg_T *eap, char_u **split_buf, size_t *split_len));
5066
5067 static int
5068uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5069 char_u *name;
5070 size_t name_len;
5071 char_u *rep;
5072 long argt;
5073 long def;
5074 int flags;
5075 int compl;
5076 char_u *compl_arg;
5077 int force;
5078{
5079 ucmd_T *cmd = NULL;
5080 char_u *p;
5081 int i;
5082 int cmp = 1;
5083 char_u *rep_buf = NULL;
5084 garray_T *gap;
5085
Bram Moolenaar9c102382006-05-03 21:26:49 +00005086 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 if (rep_buf == NULL)
5088 {
5089 /* Can't replace termcodes - try using the string as is */
5090 rep_buf = vim_strsave(rep);
5091
5092 /* Give up if out of memory */
5093 if (rep_buf == NULL)
5094 return FAIL;
5095 }
5096
5097 /* get address of growarray: global or in curbuf */
5098 if (flags & UC_BUFFER)
5099 {
5100 gap = &curbuf->b_ucmds;
5101 if (gap->ga_itemsize == 0)
5102 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5103 }
5104 else
5105 gap = &ucmds;
5106
5107 /* Search for the command in the already defined commands. */
5108 for (i = 0; i < gap->ga_len; ++i)
5109 {
5110 size_t len;
5111
5112 cmd = USER_CMD_GA(gap, i);
5113 len = STRLEN(cmd->uc_name);
5114 cmp = STRNCMP(name, cmd->uc_name, name_len);
5115 if (cmp == 0)
5116 {
5117 if (name_len < len)
5118 cmp = -1;
5119 else if (name_len > len)
5120 cmp = 1;
5121 }
5122
5123 if (cmp == 0)
5124 {
5125 if (!force)
5126 {
5127 EMSG(_("E174: Command already exists: add ! to replace it"));
5128 goto fail;
5129 }
5130
5131 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005132 cmd->uc_rep = NULL;
5133#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5134 vim_free(cmd->uc_compl_arg);
5135 cmd->uc_compl_arg = NULL;
5136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 break;
5138 }
5139
5140 /* Stop as soon as we pass the name to add */
5141 if (cmp < 0)
5142 break;
5143 }
5144
5145 /* Extend the array unless we're replacing an existing command */
5146 if (cmp != 0)
5147 {
5148 if (ga_grow(gap, 1) != OK)
5149 goto fail;
5150 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5151 goto fail;
5152
5153 cmd = USER_CMD_GA(gap, i);
5154 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5155
5156 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
5158 cmd->uc_name = p;
5159 }
5160
5161 cmd->uc_rep = rep_buf;
5162 cmd->uc_argt = argt;
5163 cmd->uc_def = def;
5164 cmd->uc_compl = compl;
5165#ifdef FEAT_EVAL
5166 cmd->uc_scriptID = current_SID;
5167# ifdef FEAT_CMDL_COMPL
5168 cmd->uc_compl_arg = compl_arg;
5169# endif
5170#endif
5171
5172 return OK;
5173
5174fail:
5175 vim_free(rep_buf);
5176#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5177 vim_free(compl_arg);
5178#endif
5179 return FAIL;
5180}
5181
5182/*
5183 * List of names for completion for ":command" with the EXPAND_ flag.
5184 * Must be alphabetical for completion.
5185 */
5186static struct
5187{
5188 int expand;
5189 char *name;
5190} command_complete[] =
5191{
5192 {EXPAND_AUGROUP, "augroup"},
5193 {EXPAND_BUFFERS, "buffer"},
5194 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005195#if defined(FEAT_CSCOPE)
5196 {EXPAND_CSCOPE, "cscope"},
5197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5199 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005200 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201#endif
5202 {EXPAND_DIRECTORIES, "dir"},
5203 {EXPAND_ENV_VARS, "environment"},
5204 {EXPAND_EVENTS, "event"},
5205 {EXPAND_EXPRESSION, "expression"},
5206 {EXPAND_FILES, "file"},
5207 {EXPAND_FUNCTIONS, "function"},
5208 {EXPAND_HELP, "help"},
5209 {EXPAND_HIGHLIGHT, "highlight"},
5210 {EXPAND_MAPPINGS, "mapping"},
5211 {EXPAND_MENUS, "menu"},
5212 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005213 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 {EXPAND_TAGS, "tag"},
5215 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5216 {EXPAND_USER_VARS, "var"},
5217 {0, NULL}
5218};
5219
5220 static void
5221uc_list(name, name_len)
5222 char_u *name;
5223 size_t name_len;
5224{
5225 int i, j;
5226 int found = FALSE;
5227 ucmd_T *cmd;
5228 int len;
5229 long a;
5230 garray_T *gap;
5231
5232 gap = &curbuf->b_ucmds;
5233 for (;;)
5234 {
5235 for (i = 0; i < gap->ga_len; ++i)
5236 {
5237 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005238 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
5240 /* Skip commands which don't match the requested prefix */
5241 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5242 continue;
5243
5244 /* Put out the title first time */
5245 if (!found)
5246 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5247 found = TRUE;
5248 msg_putchar('\n');
5249 if (got_int)
5250 break;
5251
5252 /* Special cases */
5253 msg_putchar(a & BANG ? '!' : ' ');
5254 msg_putchar(a & REGSTR ? '"' : ' ');
5255 msg_putchar(gap != &ucmds ? 'b' : ' ');
5256 msg_putchar(' ');
5257
5258 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5259 len = (int)STRLEN(cmd->uc_name) + 4;
5260
5261 do {
5262 msg_putchar(' ');
5263 ++len;
5264 } while (len < 16);
5265
5266 len = 0;
5267
5268 /* Arguments */
5269 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5270 {
5271 case 0: IObuff[len++] = '0'; break;
5272 case (EXTRA): IObuff[len++] = '*'; break;
5273 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5274 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5275 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5276 }
5277
5278 do {
5279 IObuff[len++] = ' ';
5280 } while (len < 5);
5281
5282 /* Range */
5283 if (a & (RANGE|COUNT))
5284 {
5285 if (a & COUNT)
5286 {
5287 /* -count=N */
5288 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5289 len += (int)STRLEN(IObuff + len);
5290 }
5291 else if (a & DFLALL)
5292 IObuff[len++] = '%';
5293 else if (cmd->uc_def >= 0)
5294 {
5295 /* -range=N */
5296 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5297 len += (int)STRLEN(IObuff + len);
5298 }
5299 else
5300 IObuff[len++] = '.';
5301 }
5302
5303 do {
5304 IObuff[len++] = ' ';
5305 } while (len < 11);
5306
5307 /* Completion */
5308 for (j = 0; command_complete[j].expand != 0; ++j)
5309 if (command_complete[j].expand == cmd->uc_compl)
5310 {
5311 STRCPY(IObuff + len, command_complete[j].name);
5312 len += (int)STRLEN(IObuff + len);
5313 break;
5314 }
5315
5316 do {
5317 IObuff[len++] = ' ';
5318 } while (len < 21);
5319
5320 IObuff[len] = '\0';
5321 msg_outtrans(IObuff);
5322
5323 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005324#ifdef FEAT_EVAL
5325 if (p_verbose > 0)
5326 last_set_msg(cmd->uc_scriptID);
5327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 out_flush();
5329 ui_breakcheck();
5330 if (got_int)
5331 break;
5332 }
5333 if (gap == &ucmds || i < gap->ga_len)
5334 break;
5335 gap = &ucmds;
5336 }
5337
5338 if (!found)
5339 MSG(_("No user-defined commands found"));
5340}
5341
5342 static char_u *
5343uc_fun_cmd()
5344{
5345 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5346 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5347 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5348 0xb9, 0x7f, 0};
5349 int i;
5350
5351 for (i = 0; fcmd[i]; ++i)
5352 IObuff[i] = fcmd[i] - 0x40;
5353 IObuff[i] = 0;
5354 return IObuff;
5355}
5356
5357 static int
5358uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5359 char_u *attr;
5360 size_t len;
5361 long *argt;
5362 long *def;
5363 int *flags;
5364 int *compl;
5365 char_u **compl_arg;
5366{
5367 char_u *p;
5368
5369 if (len == 0)
5370 {
5371 EMSG(_("E175: No attribute specified"));
5372 return FAIL;
5373 }
5374
5375 /* First, try the simple attributes (no arguments) */
5376 if (STRNICMP(attr, "bang", len) == 0)
5377 *argt |= BANG;
5378 else if (STRNICMP(attr, "buffer", len) == 0)
5379 *flags |= UC_BUFFER;
5380 else if (STRNICMP(attr, "register", len) == 0)
5381 *argt |= REGSTR;
5382 else if (STRNICMP(attr, "bar", len) == 0)
5383 *argt |= TRLBAR;
5384 else
5385 {
5386 int i;
5387 char_u *val = NULL;
5388 size_t vallen = 0;
5389 size_t attrlen = len;
5390
5391 /* Look for the attribute name - which is the part before any '=' */
5392 for (i = 0; i < (int)len; ++i)
5393 {
5394 if (attr[i] == '=')
5395 {
5396 val = &attr[i + 1];
5397 vallen = len - i - 1;
5398 attrlen = i;
5399 break;
5400 }
5401 }
5402
5403 if (STRNICMP(attr, "nargs", attrlen) == 0)
5404 {
5405 if (vallen == 1)
5406 {
5407 if (*val == '0')
5408 /* Do nothing - this is the default */;
5409 else if (*val == '1')
5410 *argt |= (EXTRA | NOSPC | NEEDARG);
5411 else if (*val == '*')
5412 *argt |= EXTRA;
5413 else if (*val == '?')
5414 *argt |= (EXTRA | NOSPC);
5415 else if (*val == '+')
5416 *argt |= (EXTRA | NEEDARG);
5417 else
5418 goto wrong_nargs;
5419 }
5420 else
5421 {
5422wrong_nargs:
5423 EMSG(_("E176: Invalid number of arguments"));
5424 return FAIL;
5425 }
5426 }
5427 else if (STRNICMP(attr, "range", attrlen) == 0)
5428 {
5429 *argt |= RANGE;
5430 if (vallen == 1 && *val == '%')
5431 *argt |= DFLALL;
5432 else if (val != NULL)
5433 {
5434 p = val;
5435 if (*def >= 0)
5436 {
5437two_count:
5438 EMSG(_("E177: Count cannot be specified twice"));
5439 return FAIL;
5440 }
5441
5442 *def = getdigits(&p);
5443 *argt |= (ZEROR | NOTADR);
5444
5445 if (p != val + vallen || vallen == 0)
5446 {
5447invalid_count:
5448 EMSG(_("E178: Invalid default value for count"));
5449 return FAIL;
5450 }
5451 }
5452 }
5453 else if (STRNICMP(attr, "count", attrlen) == 0)
5454 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005455 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456
5457 if (val != NULL)
5458 {
5459 p = val;
5460 if (*def >= 0)
5461 goto two_count;
5462
5463 *def = getdigits(&p);
5464
5465 if (p != val + vallen)
5466 goto invalid_count;
5467 }
5468
5469 if (*def < 0)
5470 *def = 0;
5471 }
5472 else if (STRNICMP(attr, "complete", attrlen) == 0)
5473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 if (val == NULL)
5475 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005476 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 return FAIL;
5478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005480 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5481 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 }
5484 else
5485 {
5486 char_u ch = attr[len];
5487 attr[len] = '\0';
5488 EMSG2(_("E181: Invalid attribute: %s"), attr);
5489 attr[len] = ch;
5490 return FAIL;
5491 }
5492 }
5493
5494 return OK;
5495}
5496
Bram Moolenaara850a712009-01-28 14:42:59 +00005497/*
5498 * ":command ..."
5499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 static void
5501ex_command(eap)
5502 exarg_T *eap;
5503{
5504 char_u *name;
5505 char_u *end;
5506 char_u *p;
5507 long argt = 0;
5508 long def = -1;
5509 int flags = 0;
5510 int compl = EXPAND_NOTHING;
5511 char_u *compl_arg = NULL;
5512 int has_attr = (eap->arg[0] == '-');
5513
5514 p = eap->arg;
5515
5516 /* Check for attributes */
5517 while (*p == '-')
5518 {
5519 ++p;
5520 end = skiptowhite(p);
5521 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5522 == FAIL)
5523 return;
5524 p = skipwhite(end);
5525 }
5526
5527 /* Get the name (if any) and skip to the following argument */
5528 name = p;
5529 if (ASCII_ISALPHA(*p))
5530 while (ASCII_ISALNUM(*p))
5531 ++p;
5532 if (!ends_excmd(*p) && !vim_iswhite(*p))
5533 {
5534 EMSG(_("E182: Invalid command name"));
5535 return;
5536 }
5537 end = p;
5538
5539 /* If there is nothing after the name, and no attributes were specified,
5540 * we are listing commands
5541 */
5542 p = skipwhite(end);
5543 if (!has_attr && ends_excmd(*p))
5544 {
5545 uc_list(name, end - name);
5546 }
5547 else if (!ASCII_ISUPPER(*name))
5548 {
5549 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5550 return;
5551 }
5552 else
5553 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5554 eap->forceit);
5555}
5556
5557/*
5558 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 * Clear all user commands, global and for current buffer.
5560 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005561/*ARGSUSED*/
5562 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563ex_comclear(eap)
5564 exarg_T *eap;
5565{
5566 uc_clear(&ucmds);
5567 uc_clear(&curbuf->b_ucmds);
5568}
5569
5570/*
5571 * Clear all user commands for "gap".
5572 */
5573 void
5574uc_clear(gap)
5575 garray_T *gap;
5576{
5577 int i;
5578 ucmd_T *cmd;
5579
5580 for (i = 0; i < gap->ga_len; ++i)
5581 {
5582 cmd = USER_CMD_GA(gap, i);
5583 vim_free(cmd->uc_name);
5584 vim_free(cmd->uc_rep);
5585# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5586 vim_free(cmd->uc_compl_arg);
5587# endif
5588 }
5589 ga_clear(gap);
5590}
5591
5592 static void
5593ex_delcommand(eap)
5594 exarg_T *eap;
5595{
5596 int i = 0;
5597 ucmd_T *cmd = NULL;
5598 int cmp = -1;
5599 garray_T *gap;
5600
5601 gap = &curbuf->b_ucmds;
5602 for (;;)
5603 {
5604 for (i = 0; i < gap->ga_len; ++i)
5605 {
5606 cmd = USER_CMD_GA(gap, i);
5607 cmp = STRCMP(eap->arg, cmd->uc_name);
5608 if (cmp <= 0)
5609 break;
5610 }
5611 if (gap == &ucmds || cmp == 0)
5612 break;
5613 gap = &ucmds;
5614 }
5615
5616 if (cmp != 0)
5617 {
5618 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5619 return;
5620 }
5621
5622 vim_free(cmd->uc_name);
5623 vim_free(cmd->uc_rep);
5624# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5625 vim_free(cmd->uc_compl_arg);
5626# endif
5627
5628 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629
5630 if (i < gap->ga_len)
5631 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5632}
5633
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005634/*
5635 * split and quote args for <f-args>
5636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 static char_u *
5638uc_split_args(arg, lenp)
5639 char_u *arg;
5640 size_t *lenp;
5641{
5642 char_u *buf;
5643 char_u *p;
5644 char_u *q;
5645 int len;
5646
5647 /* Precalculate length */
5648 p = arg;
5649 len = 2; /* Initial and final quotes */
5650
5651 while (*p)
5652 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005653 if (p[0] == '\\' && p[1] == '\\')
5654 {
5655 len += 2;
5656 p += 2;
5657 }
5658 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 {
5660 len += 1;
5661 p += 2;
5662 }
5663 else if (*p == '\\' || *p == '"')
5664 {
5665 len += 2;
5666 p += 1;
5667 }
5668 else if (vim_iswhite(*p))
5669 {
5670 p = skipwhite(p);
5671 if (*p == NUL)
5672 break;
5673 len += 3; /* "," */
5674 }
5675 else
5676 {
5677 ++len;
5678 ++p;
5679 }
5680 }
5681
5682 buf = alloc(len + 1);
5683 if (buf == NULL)
5684 {
5685 *lenp = 0;
5686 return buf;
5687 }
5688
5689 p = arg;
5690 q = buf;
5691 *q++ = '"';
5692 while (*p)
5693 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005694 if (p[0] == '\\' && p[1] == '\\')
5695 {
5696 *q++ = '\\';
5697 *q++ = '\\';
5698 p += 2;
5699 }
5700 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
5702 *q++ = p[1];
5703 p += 2;
5704 }
5705 else if (*p == '\\' || *p == '"')
5706 {
5707 *q++ = '\\';
5708 *q++ = *p++;
5709 }
5710 else if (vim_iswhite(*p))
5711 {
5712 p = skipwhite(p);
5713 if (*p == NUL)
5714 break;
5715 *q++ = '"';
5716 *q++ = ',';
5717 *q++ = '"';
5718 }
5719 else
5720 {
5721 *q++ = *p++;
5722 }
5723 }
5724 *q++ = '"';
5725 *q = 0;
5726
5727 *lenp = len;
5728 return buf;
5729}
5730
5731/*
5732 * Check for a <> code in a user command.
5733 * "code" points to the '<'. "len" the length of the <> (inclusive).
5734 * "buf" is where the result is to be added.
5735 * "split_buf" points to a buffer used for splitting, caller should free it.
5736 * "split_len" is the length of what "split_buf" contains.
5737 * Returns the length of the replacement, which has been added to "buf".
5738 * Returns -1 if there was no match, and only the "<" has been copied.
5739 */
5740 static size_t
5741uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5742 char_u *code;
5743 size_t len;
5744 char_u *buf;
5745 ucmd_T *cmd; /* the user command we're expanding */
5746 exarg_T *eap; /* ex arguments */
5747 char_u **split_buf;
5748 size_t *split_len;
5749{
5750 size_t result = 0;
5751 char_u *p = code + 1;
5752 size_t l = len - 2;
5753 int quote = 0;
5754 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5755 ct_LT, ct_NONE } type = ct_NONE;
5756
5757 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5758 {
5759 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5760 p += 2;
5761 l -= 2;
5762 }
5763
Bram Moolenaar371d5402006-03-20 21:47:49 +00005764 ++l;
5765 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005767 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005769 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005771 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005773 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005775 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005777 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005779 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 type = ct_REGISTER;
5781
5782 switch (type)
5783 {
5784 case ct_ARGS:
5785 /* Simple case first */
5786 if (*eap->arg == NUL)
5787 {
5788 if (quote == 1)
5789 {
5790 result = 2;
5791 if (buf != NULL)
5792 STRCPY(buf, "''");
5793 }
5794 else
5795 result = 0;
5796 break;
5797 }
5798
5799 /* When specified there is a single argument don't split it.
5800 * Works for ":Cmd %" when % is "a b c". */
5801 if ((eap->argt & NOSPC) && quote == 2)
5802 quote = 1;
5803
5804 switch (quote)
5805 {
5806 case 0: /* No quoting, no splitting */
5807 result = STRLEN(eap->arg);
5808 if (buf != NULL)
5809 STRCPY(buf, eap->arg);
5810 break;
5811 case 1: /* Quote, but don't split */
5812 result = STRLEN(eap->arg) + 2;
5813 for (p = eap->arg; *p; ++p)
5814 {
5815 if (*p == '\\' || *p == '"')
5816 ++result;
5817 }
5818
5819 if (buf != NULL)
5820 {
5821 *buf++ = '"';
5822 for (p = eap->arg; *p; ++p)
5823 {
5824 if (*p == '\\' || *p == '"')
5825 *buf++ = '\\';
5826 *buf++ = *p;
5827 }
5828 *buf = '"';
5829 }
5830
5831 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005832 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 /* This is hard, so only do it once, and cache the result */
5834 if (*split_buf == NULL)
5835 *split_buf = uc_split_args(eap->arg, split_len);
5836
5837 result = *split_len;
5838 if (buf != NULL && result != 0)
5839 STRCPY(buf, *split_buf);
5840
5841 break;
5842 }
5843 break;
5844
5845 case ct_BANG:
5846 result = eap->forceit ? 1 : 0;
5847 if (quote)
5848 result += 2;
5849 if (buf != NULL)
5850 {
5851 if (quote)
5852 *buf++ = '"';
5853 if (eap->forceit)
5854 *buf++ = '!';
5855 if (quote)
5856 *buf = '"';
5857 }
5858 break;
5859
5860 case ct_LINE1:
5861 case ct_LINE2:
5862 case ct_COUNT:
5863 {
5864 char num_buf[20];
5865 long num = (type == ct_LINE1) ? eap->line1 :
5866 (type == ct_LINE2) ? eap->line2 :
5867 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5868 size_t num_len;
5869
5870 sprintf(num_buf, "%ld", num);
5871 num_len = STRLEN(num_buf);
5872 result = num_len;
5873
5874 if (quote)
5875 result += 2;
5876
5877 if (buf != NULL)
5878 {
5879 if (quote)
5880 *buf++ = '"';
5881 STRCPY(buf, num_buf);
5882 buf += num_len;
5883 if (quote)
5884 *buf = '"';
5885 }
5886
5887 break;
5888 }
5889
5890 case ct_REGISTER:
5891 result = eap->regname ? 1 : 0;
5892 if (quote)
5893 result += 2;
5894 if (buf != NULL)
5895 {
5896 if (quote)
5897 *buf++ = '\'';
5898 if (eap->regname)
5899 *buf++ = eap->regname;
5900 if (quote)
5901 *buf = '\'';
5902 }
5903 break;
5904
5905 case ct_LT:
5906 result = 1;
5907 if (buf != NULL)
5908 *buf = '<';
5909 break;
5910
5911 default:
5912 /* Not recognized: just copy the '<' and return -1. */
5913 result = (size_t)-1;
5914 if (buf != NULL)
5915 *buf = '<';
5916 break;
5917 }
5918
5919 return result;
5920}
5921
5922 static void
5923do_ucmd(eap)
5924 exarg_T *eap;
5925{
5926 char_u *buf;
5927 char_u *p;
5928 char_u *q;
5929
5930 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00005931 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00005932 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 size_t len, totlen;
5934
5935 size_t split_len = 0;
5936 char_u *split_buf = NULL;
5937 ucmd_T *cmd;
5938#ifdef FEAT_EVAL
5939 scid_T save_current_SID = current_SID;
5940#endif
5941
5942 if (eap->cmdidx == CMD_USER)
5943 cmd = USER_CMD(eap->useridx);
5944 else
5945 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5946
5947 /*
5948 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00005949 * First round: "buf" is NULL, compute length, allocate "buf".
5950 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 */
5952 buf = NULL;
5953 for (;;)
5954 {
Bram Moolenaara850a712009-01-28 14:42:59 +00005955 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005956 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00005958
5959 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 {
Bram Moolenaara850a712009-01-28 14:42:59 +00005961 start = vim_strchr(p, '<');
5962 if (start != NULL)
5963 end = vim_strchr(start + 1, '>');
5964 if (buf != NULL)
5965 {
5966 ksp = vim_strchr(p, K_SPECIAL);
5967 if (ksp != NULL && (start == NULL || ksp < start || end == NULL)
5968 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
5969# ifdef FEAT_GUI
5970 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
5971# endif
5972 ))
5973 {
5974 /* K_SPECIAL han been put in the buffer as K_SPECIAL
5975 * KS_SPECIAL KE_FILLER, like for mappings, but
5976 * do_cmdline() doesn't handle that, so convert it back.
5977 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
5978 len = ksp - p;
5979 if (len > 0)
5980 {
5981 mch_memmove(q, p, len);
5982 q += len;
5983 }
5984 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
5985 p = ksp + 3;
5986 continue;
5987 }
5988 }
5989
5990 /* break if there no <item> is found */
5991 if (start == NULL || end == NULL)
5992 break;
5993
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 /* Include the '>' */
5995 ++end;
5996
5997 /* Take everything up to the '<' */
5998 len = start - p;
5999 if (buf == NULL)
6000 totlen += len;
6001 else
6002 {
6003 mch_memmove(q, p, len);
6004 q += len;
6005 }
6006
6007 len = uc_check_code(start, end - start, q, cmd, eap,
6008 &split_buf, &split_len);
6009 if (len == (size_t)-1)
6010 {
6011 /* no match, continue after '<' */
6012 p = start + 1;
6013 len = 1;
6014 }
6015 else
6016 p = end;
6017 if (buf == NULL)
6018 totlen += len;
6019 else
6020 q += len;
6021 }
6022 if (buf != NULL) /* second time here, finished */
6023 {
6024 STRCPY(q, p);
6025 break;
6026 }
6027
6028 totlen += STRLEN(p); /* Add on the trailing characters */
6029 buf = alloc((unsigned)(totlen + 1));
6030 if (buf == NULL)
6031 {
6032 vim_free(split_buf);
6033 return;
6034 }
6035 }
6036
6037#ifdef FEAT_EVAL
6038 current_SID = cmd->uc_scriptID;
6039#endif
6040 (void)do_cmdline(buf, eap->getline, eap->cookie,
6041 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6042#ifdef FEAT_EVAL
6043 current_SID = save_current_SID;
6044#endif
6045 vim_free(buf);
6046 vim_free(split_buf);
6047}
6048
6049# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6050 static char_u *
6051get_user_command_name(idx)
6052 int idx;
6053{
6054 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6055}
6056
6057/*
6058 * Function given to ExpandGeneric() to obtain the list of user command names.
6059 */
6060/*ARGSUSED*/
6061 char_u *
6062get_user_commands(xp, idx)
6063 expand_T *xp;
6064 int idx;
6065{
6066 if (idx < curbuf->b_ucmds.ga_len)
6067 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6068 idx -= curbuf->b_ucmds.ga_len;
6069 if (idx < ucmds.ga_len)
6070 return USER_CMD(idx)->uc_name;
6071 return NULL;
6072}
6073
6074/*
6075 * Function given to ExpandGeneric() to obtain the list of user command
6076 * attributes.
6077 */
6078/*ARGSUSED*/
6079 char_u *
6080get_user_cmd_flags(xp, idx)
6081 expand_T *xp;
6082 int idx;
6083{
6084 static char *user_cmd_flags[] =
6085 {"bang", "bar", "buffer", "complete", "count",
6086 "nargs", "range", "register"};
6087
6088 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
6089 return NULL;
6090 return (char_u *)user_cmd_flags[idx];
6091}
6092
6093/*
6094 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6095 */
6096/*ARGSUSED*/
6097 char_u *
6098get_user_cmd_nargs(xp, idx)
6099 expand_T *xp;
6100 int idx;
6101{
6102 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6103
6104 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
6105 return NULL;
6106 return (char_u *)user_cmd_nargs[idx];
6107}
6108
6109/*
6110 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6111 */
6112/*ARGSUSED*/
6113 char_u *
6114get_user_cmd_complete(xp, idx)
6115 expand_T *xp;
6116 int idx;
6117{
6118 return (char_u *)command_complete[idx].name;
6119}
6120# endif /* FEAT_CMDL_COMPL */
6121
6122#endif /* FEAT_USR_CMDS */
6123
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006124#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6125/*
6126 * Parse a completion argument "value[vallen]".
6127 * The detected completion goes in "*complp", argument type in "*argt".
6128 * When there is an argument, for function and user defined completion, it's
6129 * copied to allocated memory and stored in "*compl_arg".
6130 * Returns FAIL if something is wrong.
6131 */
6132 int
6133parse_compl_arg(value, vallen, complp, argt, compl_arg)
6134 char_u *value;
6135 int vallen;
6136 int *complp;
6137 long *argt;
6138 char_u **compl_arg;
6139{
6140 char_u *arg = NULL;
6141 size_t arglen = 0;
6142 int i;
6143 int valend = vallen;
6144
6145 /* Look for any argument part - which is the part after any ',' */
6146 for (i = 0; i < vallen; ++i)
6147 {
6148 if (value[i] == ',')
6149 {
6150 arg = &value[i + 1];
6151 arglen = vallen - i - 1;
6152 valend = i;
6153 break;
6154 }
6155 }
6156
6157 for (i = 0; command_complete[i].expand != 0; ++i)
6158 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006159 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006160 && STRNCMP(value, command_complete[i].name, valend) == 0)
6161 {
6162 *complp = command_complete[i].expand;
6163 if (command_complete[i].expand == EXPAND_BUFFERS)
6164 *argt |= BUFNAME;
6165 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6166 || command_complete[i].expand == EXPAND_FILES)
6167 *argt |= XFILE;
6168 break;
6169 }
6170 }
6171
6172 if (command_complete[i].expand == 0)
6173 {
6174 EMSG2(_("E180: Invalid complete value: %s"), value);
6175 return FAIL;
6176 }
6177
6178# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6179 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6180 && arg != NULL)
6181# else
6182 if (arg != NULL)
6183# endif
6184 {
6185 EMSG(_("E468: Completion argument only allowed for custom completion"));
6186 return FAIL;
6187 }
6188
6189# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6190 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6191 && arg == NULL)
6192 {
6193 EMSG(_("E467: Custom completion requires a function argument"));
6194 return FAIL;
6195 }
6196
6197 if (arg != NULL)
6198 *compl_arg = vim_strnsave(arg, (int)arglen);
6199# endif
6200 return OK;
6201}
6202#endif
6203
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 static void
6205ex_colorscheme(eap)
6206 exarg_T *eap;
6207{
6208 if (load_colors(eap->arg) == FAIL)
6209 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6210}
6211
6212 static void
6213ex_highlight(eap)
6214 exarg_T *eap;
6215{
6216 if (*eap->arg == NUL && eap->cmd[2] == '!')
6217 MSG(_("Greetings, Vim user!"));
6218 do_highlight(eap->arg, eap->forceit, FALSE);
6219}
6220
6221
6222/*
6223 * Call this function if we thought we were going to exit, but we won't
6224 * (because of an error). May need to restore the terminal mode.
6225 */
6226 void
6227not_exiting()
6228{
6229 exiting = FALSE;
6230 settmode(TMODE_RAW);
6231}
6232
6233/*
6234 * ":quit": quit current window, quit Vim if closed the last window.
6235 */
6236 static void
6237ex_quit(eap)
6238 exarg_T *eap;
6239{
6240#ifdef FEAT_CMDWIN
6241 if (cmdwin_type != 0)
6242 {
6243 cmdwin_result = Ctrl_C;
6244 return;
6245 }
6246#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006247 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006248 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006249 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006250 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006251 return;
6252 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006253#ifdef FEAT_AUTOCMD
6254 if (curbuf_locked())
6255 return;
6256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
6258#ifdef FEAT_NETBEANS_INTG
6259 netbeansForcedQuit = eap->forceit;
6260#endif
6261
6262 /*
6263 * If there are more files or windows we won't exit.
6264 */
6265 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6266 exiting = TRUE;
6267 if ((!P_HID(curbuf)
6268 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6269 || check_more(TRUE, eap->forceit) == FAIL
6270 || (only_one_window() && check_changed_any(eap->forceit)))
6271 {
6272 not_exiting();
6273 }
6274 else
6275 {
6276#ifdef FEAT_WINDOWS
6277 if (only_one_window()) /* quit last window */
6278#endif
6279 getout(0);
6280#ifdef FEAT_WINDOWS
6281# ifdef FEAT_GUI
6282 need_mouse_correct = TRUE;
6283# endif
6284 /* close window; may free buffer */
6285 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6286#endif
6287 }
6288}
6289
6290/*
6291 * ":cquit".
6292 */
6293/*ARGSUSED*/
6294 static void
6295ex_cquit(eap)
6296 exarg_T *eap;
6297{
6298 getout(1); /* this does not always pass on the exit code to the Manx
6299 compiler. why? */
6300}
6301
6302/*
6303 * ":qall": try to quit all windows
6304 */
6305 static void
6306ex_quit_all(eap)
6307 exarg_T *eap;
6308{
6309# ifdef FEAT_CMDWIN
6310 if (cmdwin_type != 0)
6311 {
6312 if (eap->forceit)
6313 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6314 else
6315 cmdwin_result = K_XF2;
6316 return;
6317 }
6318# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006319
6320 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006321 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006322 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006323 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006324 return;
6325 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006326#ifdef FEAT_AUTOCMD
6327 if (curbuf_locked())
6328 return;
6329#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006330
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 exiting = TRUE;
6332 if (eap->forceit || !check_changed_any(FALSE))
6333 getout(0);
6334 not_exiting();
6335}
6336
Bram Moolenaard9967712006-03-11 21:18:15 +00006337#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338/*
6339 * ":close": close current window, unless it is the last one
6340 */
6341 static void
6342ex_close(eap)
6343 exarg_T *eap;
6344{
6345# ifdef FEAT_CMDWIN
6346 if (cmdwin_type != 0)
6347 cmdwin_result = K_IGNORE;
6348 else
6349# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006350 if (!text_locked()
6351#ifdef FEAT_AUTOCMD
6352 && !curbuf_locked()
6353#endif
6354 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006355 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356}
6357
Bram Moolenaard9967712006-03-11 21:18:15 +00006358# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006359/*
6360 * ":pclose": Close any preview window.
6361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006363ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006365{
6366 win_T *win;
6367
6368 for (win = firstwin; win != NULL; win = win->w_next)
6369 if (win->w_p_pvw)
6370 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006371 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006372 break;
6373 }
6374}
Bram Moolenaard9967712006-03-11 21:18:15 +00006375# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006376
Bram Moolenaarf740b292006-02-16 22:11:02 +00006377/*
6378 * Close window "win" and take care of handling closing the last window for a
6379 * modified buffer.
6380 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006381 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006382ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006383 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006385 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 int need_hide;
6388 buf_T *buf = win->w_buffer;
6389
6390 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006391 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006393# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 if ((p_confirm || cmdmod.confirm) && p_write)
6395 {
6396 dialog_changed(buf, FALSE);
6397 if (buf_valid(buf) && bufIsChanged(buf))
6398 return;
6399 need_hide = FALSE;
6400 }
6401 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 {
6404 EMSG(_(e_nowrtmsg));
6405 return;
6406 }
6407 }
6408
Bram Moolenaard9967712006-03-11 21:18:15 +00006409# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006411# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006412
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006414 if (tp == NULL)
6415 win_close(win, !need_hide && !P_HID(buf));
6416 else
6417 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418}
6419
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006421 * ":tabclose": close current tab page, unless it is the last one.
6422 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 */
6424 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006425ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 exarg_T *eap;
6427{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006428 tabpage_T *tp;
6429
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006430# ifdef FEAT_CMDWIN
6431 if (cmdwin_type != 0)
6432 cmdwin_result = K_IGNORE;
6433 else
6434# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006435 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006436 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006437 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006439 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006440 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006441 tp = find_tabpage((int)eap->line2);
6442 if (tp == NULL)
6443 {
6444 beep_flush();
6445 return;
6446 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006447 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006448 {
6449 tabpage_close_other(tp, eap->forceit);
6450 return;
6451 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006452 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006453 if (!text_locked()
6454#ifdef FEAT_AUTOCMD
6455 && !curbuf_locked()
6456#endif
6457 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006458 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006460}
6461
6462/*
6463 * ":tabonly": close all tab pages except the current one
6464 */
6465 static void
6466ex_tabonly(eap)
6467 exarg_T *eap;
6468{
6469 tabpage_T *tp;
6470 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006471
6472# ifdef FEAT_CMDWIN
6473 if (cmdwin_type != 0)
6474 cmdwin_result = K_IGNORE;
6475 else
6476# endif
6477 if (first_tabpage->tp_next == NULL)
6478 MSG(_("Already only one tab page"));
6479 else
6480 {
6481 /* Repeat this up to a 1000 times, because autocommands may mess
6482 * up the lists. */
6483 for (done = 0; done < 1000; ++done)
6484 {
6485 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6486 if (tp->tp_topframe != topframe)
6487 {
6488 tabpage_close_other(tp, eap->forceit);
6489 /* if we failed to close it quit */
6490 if (valid_tabpage(tp))
6491 done = 1000;
6492 /* start over, "tp" is now invalid */
6493 break;
6494 }
6495 if (first_tabpage->tp_next == NULL)
6496 break;
6497 }
6498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500
6501/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006502 * Close the current tab page.
6503 */
6504 void
6505tabpage_close(forceit)
6506 int forceit;
6507{
6508 /* First close all the windows but the current one. If that worked then
6509 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006510 if (lastwin != firstwin)
6511 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006512 if (lastwin == firstwin)
6513 ex_win_close(forceit, curwin, NULL);
6514# ifdef FEAT_GUI
6515 need_mouse_correct = TRUE;
6516# endif
6517}
6518
6519/*
6520 * Close tab page "tp", which is not the current tab page.
6521 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006522 * Also takes care of the tab pages line disappearing when closing the
6523 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006524 */
6525 void
6526tabpage_close_other(tp, forceit)
6527 tabpage_T *tp;
6528 int forceit;
6529{
6530 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006531 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006532 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006533
6534 /* Limit to 1000 windows, autocommands may add a window while we close
6535 * one. OK, so I'm paranoid... */
6536 while (++done < 1000)
6537 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006538 wp = tp->tp_firstwin;
6539 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006540
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006541 /* Autocommands may delete the tab page under our fingers and we may
6542 * fail to close a window with a modified buffer. */
6543 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006544 break;
6545 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006546
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006547 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006548 if (h != tabline_height())
6549 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006550}
6551
6552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 * ":only".
6554 */
6555 static void
6556ex_only(eap)
6557 exarg_T *eap;
6558{
6559# ifdef FEAT_GUI
6560 need_mouse_correct = TRUE;
6561# endif
6562 close_others(TRUE, eap->forceit);
6563}
6564
6565/*
6566 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006567 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006569 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570ex_all(eap)
6571 exarg_T *eap;
6572{
6573 if (eap->addr_count == 0)
6574 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006575 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576}
6577#endif /* FEAT_WINDOWS */
6578
6579 static void
6580ex_hide(eap)
6581 exarg_T *eap;
6582{
6583 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6584 eap->errmsg = e_invarg;
6585 else
6586 {
6587 /* ":hide" or ":hide | cmd": hide current window */
6588 eap->nextcmd = check_nextcmd(eap->arg);
6589#ifdef FEAT_WINDOWS
6590 if (!eap->skip)
6591 {
6592# ifdef FEAT_GUI
6593 need_mouse_correct = TRUE;
6594# endif
6595 win_close(curwin, FALSE); /* don't free buffer */
6596 }
6597#endif
6598 }
6599}
6600
6601/*
6602 * ":stop" and ":suspend": Suspend Vim.
6603 */
6604 static void
6605ex_stop(eap)
6606 exarg_T *eap;
6607{
6608 /*
6609 * Disallow suspending for "rvim".
6610 */
6611 if (!check_restricted()
6612#ifdef WIN3264
6613 /*
6614 * Check if external commands are allowed now.
6615 */
6616 && can_end_termcap_mode(TRUE)
6617#endif
6618 )
6619 {
6620 if (!eap->forceit)
6621 autowrite_all();
6622 windgoto((int)Rows - 1, 0);
6623 out_char('\n');
6624 out_flush();
6625 stoptermcap();
6626 out_flush(); /* needed for SUN to restore xterm buffer */
6627#ifdef FEAT_TITLE
6628 mch_restore_title(3); /* restore window titles */
6629#endif
6630 ui_suspend(); /* call machine specific function */
6631#ifdef FEAT_TITLE
6632 maketitle();
6633 resettitle(); /* force updating the title */
6634#endif
6635 starttermcap();
6636 scroll_start(); /* scroll screen before redrawing */
6637 redraw_later_clear();
6638 shell_resized(); /* may have resized window */
6639 }
6640}
6641
6642/*
6643 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6644 */
6645 static void
6646ex_exit(eap)
6647 exarg_T *eap;
6648{
6649#ifdef FEAT_CMDWIN
6650 if (cmdwin_type != 0)
6651 {
6652 cmdwin_result = Ctrl_C;
6653 return;
6654 }
6655#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006656 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006657 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006658 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006659 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006660 return;
6661 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006662#ifdef FEAT_AUTOCMD
6663 if (curbuf_locked())
6664 return;
6665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
6667 /*
6668 * if more files or windows we won't exit
6669 */
6670 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6671 exiting = TRUE;
6672 if ( ((eap->cmdidx == CMD_wq
6673 || curbufIsChanged())
6674 && do_write(eap) == FAIL)
6675 || check_more(TRUE, eap->forceit) == FAIL
6676 || (only_one_window() && check_changed_any(eap->forceit)))
6677 {
6678 not_exiting();
6679 }
6680 else
6681 {
6682#ifdef FEAT_WINDOWS
6683 if (only_one_window()) /* quit last window, exit Vim */
6684#endif
6685 getout(0);
6686#ifdef FEAT_WINDOWS
6687# ifdef FEAT_GUI
6688 need_mouse_correct = TRUE;
6689# endif
6690 /* quit current window, may free buffer */
6691 win_close(curwin, !P_HID(curwin->w_buffer));
6692#endif
6693 }
6694}
6695
6696/*
6697 * ":print", ":list", ":number".
6698 */
6699 static void
6700ex_print(eap)
6701 exarg_T *eap;
6702{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006703 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6704 EMSG(_(e_emptybuf));
6705 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006707 for ( ;!got_int; ui_breakcheck())
6708 {
6709 print_line(eap->line1,
6710 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6711 || (eap->flags & EXFLAG_NR)),
6712 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6713 if (++eap->line1 > eap->line2)
6714 break;
6715 out_flush(); /* show one line at a time */
6716 }
6717 setpcmark();
6718 /* put cursor at last line */
6719 curwin->w_cursor.lnum = eap->line2;
6720 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 }
6722
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724}
6725
6726#ifdef FEAT_BYTEOFF
6727 static void
6728ex_goto(eap)
6729 exarg_T *eap;
6730{
6731 goto_byte(eap->line2);
6732}
6733#endif
6734
6735/*
6736 * ":shell".
6737 */
6738/*ARGSUSED*/
6739 static void
6740ex_shell(eap)
6741 exarg_T *eap;
6742{
6743 do_shell(NULL, 0);
6744}
6745
6746#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6747 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006748 || defined(FEAT_GUI_MSWIN) \
6749 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 || defined(PROTO)
6751
6752/*
6753 * Handle a file drop. The code is here because a drop is *nearly* like an
6754 * :args command, but not quite (we have a list of exact filenames, so we
6755 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6756 * code is very similar to :args and hence needs access to a lot of the static
6757 * functions in this file.
6758 *
6759 * The list should be allocated using alloc(), as should each item in the
6760 * list. This function takes over responsibility for freeing the list.
6761 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006762 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6764 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6765 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6766 * file functionality is (currently) not in EMX this is not presently a
6767 * problem.
6768 */
6769 void
6770handle_drop(filec, filev, split)
6771 int filec; /* the number of files dropped */
6772 char_u **filev; /* the list of files dropped */
6773 int split; /* force splitting the window */
6774{
6775 exarg_T ea;
6776 int save_msg_scroll = msg_scroll;
6777
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006778 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006779 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006781#ifdef FEAT_AUTOCMD
6782 if (curbuf_locked())
6783 return;
6784#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006785 /* When the screen is being updated we should not change buffers and
6786 * windows structures, it may cause freed memory to be used. */
6787 if (updating_screen)
6788 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789
6790 /* Check whether the current buffer is changed. If so, we will need
6791 * to split the current window or data could be lost.
6792 * We don't need to check if the 'hidden' option is set, as in this
6793 * case the buffer won't be lost.
6794 */
6795 if (!P_HID(curbuf) && !split)
6796 {
6797 ++emsg_off;
6798 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6799 --emsg_off;
6800 }
6801 if (split)
6802 {
6803# ifdef FEAT_WINDOWS
6804 if (win_split(0, 0) == FAIL)
6805 return;
6806# ifdef FEAT_SCROLLBIND
6807 curwin->w_p_scb = FALSE;
6808# endif
6809
6810 /* When splitting the window, create a new alist. Otherwise the
6811 * existing one is overwritten. */
6812 alist_unlink(curwin->w_alist);
6813 alist_new();
6814# else
6815 return; /* can't split, always fail */
6816# endif
6817 }
6818
6819 /*
6820 * Set up the new argument list.
6821 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006822 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823
6824 /*
6825 * Move to the first file.
6826 */
6827 /* Fake up a minimal "next" command for do_argfile() */
6828 vim_memset(&ea, 0, sizeof(ea));
6829 ea.cmd = (char_u *)"next";
6830 do_argfile(&ea, 0);
6831
6832 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6833 * mode that is not needed here. */
6834 need_start_insertmode = FALSE;
6835
6836 /* Restore msg_scroll, otherwise a following command may cause scrolling
6837 * unexpectedly. The screen will be redrawn by the caller, thus
6838 * msg_scroll being set by displaying a message is irrelevant. */
6839 msg_scroll = save_msg_scroll;
6840}
6841#endif
6842
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843/*
6844 * Clear an argument list: free all file names and reset it to zero entries.
6845 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006846 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847alist_clear(al)
6848 alist_T *al;
6849{
6850 while (--al->al_ga.ga_len >= 0)
6851 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6852 ga_clear(&al->al_ga);
6853}
6854
6855/*
6856 * Init an argument list.
6857 */
6858 void
6859alist_init(al)
6860 alist_T *al;
6861{
6862 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6863}
6864
6865#if defined(FEAT_WINDOWS) || defined(PROTO)
6866
6867/*
6868 * Remove a reference from an argument list.
6869 * Ignored when the argument list is the global one.
6870 * If the argument list is no longer used by any window, free it.
6871 */
6872 void
6873alist_unlink(al)
6874 alist_T *al;
6875{
6876 if (al != &global_alist && --al->al_refcount <= 0)
6877 {
6878 alist_clear(al);
6879 vim_free(al);
6880 }
6881}
6882
6883# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6884/*
6885 * Create a new argument list and use it for the current window.
6886 */
6887 void
6888alist_new()
6889{
6890 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6891 if (curwin->w_alist == NULL)
6892 {
6893 curwin->w_alist = &global_alist;
6894 ++global_alist.al_refcount;
6895 }
6896 else
6897 {
6898 curwin->w_alist->al_refcount = 1;
6899 alist_init(curwin->w_alist);
6900 }
6901}
6902# endif
6903#endif
6904
6905#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6906/*
6907 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006908 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6909 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 */
6911 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006912alist_expand(fnum_list, fnum_len)
6913 int *fnum_list;
6914 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915{
6916 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006917 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 char_u **new_arg_files;
6919 int new_arg_file_count;
6920 char_u *save_p_su = p_su;
6921 int i;
6922
6923 /* Don't use 'suffixes' here. This should work like the shell did the
6924 * expansion. Also, the vimrc file isn't read yet, thus the user
6925 * can't set the options. */
6926 p_su = empty_option;
6927 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6928 if (old_arg_files != NULL)
6929 {
6930 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006931 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6932 old_arg_count = GARGCOUNT;
6933 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 &new_arg_file_count, &new_arg_files,
6935 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6936 && new_arg_file_count > 0)
6937 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006938 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6939 TRUE, fnum_list, fnum_len);
6940 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 }
6943 p_su = save_p_su;
6944}
6945#endif
6946
6947/*
6948 * Set the argument list for the current window.
6949 * Takes over the allocated files[] and the allocated fnames in it.
6950 */
6951 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006952alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 alist_T *al;
6954 int count;
6955 char_u **files;
6956 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006957 int *fnum_list;
6958 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959{
6960 int i;
6961
6962 alist_clear(al);
6963 if (ga_grow(&al->al_ga, count) == OK)
6964 {
6965 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006966 {
6967 if (got_int)
6968 {
6969 /* When adding many buffers this can take a long time. Allow
6970 * interrupting here. */
6971 while (i < count)
6972 vim_free(files[i++]);
6973 break;
6974 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006975
6976 /* May set buffer name of a buffer previously used for the
6977 * argument list, so that it's re-used by alist_add. */
6978 if (fnum_list != NULL && i < fnum_len)
6979 buf_set_name(fnum_list[i], files[i]);
6980
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006982 ui_breakcheck();
6983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 vim_free(files);
6985 }
6986 else
6987 FreeWild(count, files);
6988#ifdef FEAT_WINDOWS
6989 if (al == &global_alist)
6990#endif
6991 arg_had_last = FALSE;
6992}
6993
6994/*
6995 * Add file "fname" to argument list "al".
6996 * "fname" must have been allocated and "al" must have been checked for room.
6997 */
6998 void
6999alist_add(al, fname, set_fnum)
7000 alist_T *al;
7001 char_u *fname;
7002 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7003{
7004 if (fname == NULL) /* don't add NULL file names */
7005 return;
7006#ifdef BACKSLASH_IN_FILENAME
7007 slash_adjust(fname);
7008#endif
7009 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7010 if (set_fnum > 0)
7011 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7012 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7013 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014}
7015
7016#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7017/*
7018 * Adjust slashes in file names. Called after 'shellslash' was set.
7019 */
7020 void
7021alist_slash_adjust()
7022{
7023 int i;
7024# ifdef FEAT_WINDOWS
7025 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007026 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027# endif
7028
7029 for (i = 0; i < GARGCOUNT; ++i)
7030 if (GARGLIST[i].ae_fname != NULL)
7031 slash_adjust(GARGLIST[i].ae_fname);
7032# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007033 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 if (wp->w_alist != &global_alist)
7035 for (i = 0; i < WARGCOUNT(wp); ++i)
7036 if (WARGLIST(wp)[i].ae_fname != NULL)
7037 slash_adjust(WARGLIST(wp)[i].ae_fname);
7038# endif
7039}
7040#endif
7041
7042/*
7043 * ":preserve".
7044 */
7045/*ARGSUSED*/
7046 static void
7047ex_preserve(eap)
7048 exarg_T *eap;
7049{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007050 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 ml_preserve(curbuf, TRUE);
7052}
7053
7054/*
7055 * ":recover".
7056 */
7057 static void
7058ex_recover(eap)
7059 exarg_T *eap;
7060{
7061 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7062 recoverymode = TRUE;
7063 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7064 && (*eap->arg == NUL
7065 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7066 ml_recover();
7067 recoverymode = FALSE;
7068}
7069
7070/*
7071 * Command modifier used in a wrong way.
7072 */
7073 static void
7074ex_wrongmodifier(eap)
7075 exarg_T *eap;
7076{
7077 eap->errmsg = e_invcmd;
7078}
7079
7080#ifdef FEAT_WINDOWS
7081/*
7082 * :sview [+command] file split window with new file, read-only
7083 * :split [[+command] file] split window with current or new file
7084 * :vsplit [[+command] file] split window vertically with current or new file
7085 * :new [[+command] file] split window with no or new file
7086 * :vnew [[+command] file] split vertically window with no or new file
7087 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007088 *
7089 * :tabedit open new Tab page with empty window
7090 * :tabedit [+command] file open new Tab page and edit "file"
7091 * :tabnew [[+command] file] just like :tabedit
7092 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 */
7094 void
7095ex_splitview(eap)
7096 exarg_T *eap;
7097{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007098 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007099# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007101# endif
7102# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007104# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007106# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7108 {
7109 ex_ni(eap);
7110 return;
7111 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007112# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007114# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007118# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007120 * quickfix windows. But it's OK when doing ":tab split". */
7121 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 {
7123 if (eap->cmdidx == CMD_split)
7124 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007125# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 if (eap->cmdidx == CMD_vsplit)
7127 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007130# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007132# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007133 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {
7135 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7136 FNAME_MESS, TRUE, curbuf->b_ffname);
7137 if (fname == NULL)
7138 goto theend;
7139 eap->arg = fname;
7140 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007141# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007143# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007145# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007147# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007149# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 && eap->cmdidx != CMD_new)
7151 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007152# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007153 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007154# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007155 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007156# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007157 au_has_group((char_u *)"FileExplorer"))
7158 {
7159 /* No browsing supported but we do have the file explorer:
7160 * Edit the directory. */
7161 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7162 eap->arg = (char_u *)".";
7163 }
7164 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007165# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007166 {
7167 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007169 if (fname == NULL)
7170 goto theend;
7171 eap->arg = fname;
7172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 }
7174 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007177 /*
7178 * Either open new tab page or split the window.
7179 */
7180 if (eap->cmdidx == CMD_tabedit
7181 || eap->cmdidx == CMD_tabfind
7182 || eap->cmdidx == CMD_tabnew)
7183 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007184 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7185 : eap->addr_count == 0 ? 0
7186 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007187 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007188 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007189
7190 /* set the alternate buffer for the window we came from */
7191 if (curwin != old_curwin
7192 && win_valid(old_curwin)
7193 && old_curwin->w_buffer != curbuf
7194 && !cmdmod.keepalt)
7195 old_curwin->w_alt_fnum = curbuf->b_fnum;
7196 }
7197 }
7198 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7200 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007201# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 /* Reset 'scrollbind' when editing another file, but keep it when
7203 * doing ":split" without arguments. */
7204 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007205# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 )
7209 curwin->w_p_scb = FALSE;
7210 else
7211 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 do_exedit(eap, old_curwin);
7214 }
7215
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007216# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007220# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221theend:
7222 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007223# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007225
7226/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007227 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007228 */
7229 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007230tabpage_new()
7231{
7232 exarg_T ea;
7233
7234 vim_memset(&ea, 0, sizeof(ea));
7235 ea.cmdidx = CMD_tabnew;
7236 ea.cmd = (char_u *)"tabn";
7237 ea.arg = (char_u *)"";
7238 ex_splitview(&ea);
7239}
7240
7241/*
7242 * :tabnext command
7243 */
7244 static void
7245ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007246 exarg_T *eap;
7247{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007248 switch (eap->cmdidx)
7249 {
7250 case CMD_tabfirst:
7251 case CMD_tabrewind:
7252 goto_tabpage(1);
7253 break;
7254 case CMD_tablast:
7255 goto_tabpage(9999);
7256 break;
7257 case CMD_tabprevious:
7258 case CMD_tabNext:
7259 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7260 break;
7261 default: /* CMD_tabnext */
7262 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7263 break;
7264 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007265}
7266
7267/*
7268 * :tabmove command
7269 */
7270 static void
7271ex_tabmove(eap)
7272 exarg_T *eap;
7273{
7274 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007275}
7276
7277/*
7278 * :tabs command: List tabs and their contents.
7279 */
7280/*ARGSUSED*/
7281 static void
7282ex_tabs(eap)
7283 exarg_T *eap;
7284{
7285 tabpage_T *tp;
7286 win_T *wp;
7287 int tabcount = 1;
7288
7289 msg_start();
7290 msg_scroll = TRUE;
7291 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7292 {
7293 msg_putchar('\n');
7294 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7295 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7296 out_flush(); /* output one line at a time */
7297 ui_breakcheck();
7298
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007299 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007300 wp = firstwin;
7301 else
7302 wp = tp->tp_firstwin;
7303 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7304 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007305 msg_putchar('\n');
7306 msg_putchar(wp == curwin ? '>' : ' ');
7307 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007308 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7309 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007310 if (buf_spname(wp->w_buffer) != NULL)
7311 STRCPY(IObuff, buf_spname(wp->w_buffer));
7312 else
7313 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7314 IObuff, IOSIZE, TRUE);
7315 msg_outtrans(IObuff);
7316 out_flush(); /* output one line at a time */
7317 ui_breakcheck();
7318 }
7319 }
7320}
7321
7322#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323
7324/*
7325 * ":mode": Set screen mode.
7326 * If no argument given, just get the screen size and redraw.
7327 */
7328 static void
7329ex_mode(eap)
7330 exarg_T *eap;
7331{
7332 if (*eap->arg == NUL)
7333 shell_resized();
7334 else
7335 mch_screenmode(eap->arg);
7336}
7337
7338#ifdef FEAT_WINDOWS
7339/*
7340 * ":resize".
7341 * set, increment or decrement current window height
7342 */
7343 static void
7344ex_resize(eap)
7345 exarg_T *eap;
7346{
7347 int n;
7348 win_T *wp = curwin;
7349
7350 if (eap->addr_count > 0)
7351 {
7352 n = eap->line2;
7353 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7354 ;
7355 }
7356
7357#ifdef FEAT_GUI
7358 need_mouse_correct = TRUE;
7359#endif
7360 n = atol((char *)eap->arg);
7361#ifdef FEAT_VERTSPLIT
7362 if (cmdmod.split & WSP_VERT)
7363 {
7364 if (*eap->arg == '-' || *eap->arg == '+')
7365 n += W_WIDTH(curwin);
7366 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7367 n = 9999;
7368 win_setwidth_win((int)n, wp);
7369 }
7370 else
7371#endif
7372 {
7373 if (*eap->arg == '-' || *eap->arg == '+')
7374 n += curwin->w_height;
7375 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7376 n = 9999;
7377 win_setheight_win((int)n, wp);
7378 }
7379}
7380#endif
7381
7382/*
7383 * ":find [+command] <file>" command.
7384 */
7385 static void
7386ex_find(eap)
7387 exarg_T *eap;
7388{
7389#ifdef FEAT_SEARCHPATH
7390 char_u *fname;
7391 int count;
7392
7393 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7394 TRUE, curbuf->b_ffname);
7395 if (eap->addr_count > 0)
7396 {
7397 /* Repeat finding the file "count" times. This matters when it
7398 * appears several times in the path. */
7399 count = eap->line2;
7400 while (fname != NULL && --count > 0)
7401 {
7402 vim_free(fname);
7403 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7404 FALSE, curbuf->b_ffname);
7405 }
7406 }
7407
7408 if (fname != NULL)
7409 {
7410 eap->arg = fname;
7411#endif
7412 do_exedit(eap, NULL);
7413#ifdef FEAT_SEARCHPATH
7414 vim_free(fname);
7415 }
7416#endif
7417}
7418
7419/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007420 * ":open" simulation: for now just work like ":visual".
7421 */
7422 static void
7423ex_open(eap)
7424 exarg_T *eap;
7425{
7426 regmatch_T regmatch;
7427 char_u *p;
7428
7429 curwin->w_cursor.lnum = eap->line2;
7430 beginline(BL_SOL | BL_FIX);
7431 if (*eap->arg == '/')
7432 {
7433 /* ":open /pattern/": put cursor in column found with pattern */
7434 ++eap->arg;
7435 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7436 *p = NUL;
7437 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7438 if (regmatch.regprog != NULL)
7439 {
7440 regmatch.rm_ic = p_ic;
7441 p = ml_get_curline();
7442 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007443 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007444 else
7445 EMSG(_(e_nomatch));
7446 vim_free(regmatch.regprog);
7447 }
7448 /* Move to the NUL, ignore any other arguments. */
7449 eap->arg += STRLEN(eap->arg);
7450 }
7451 check_cursor();
7452
7453 eap->cmdidx = CMD_visual;
7454 do_exedit(eap, NULL);
7455}
7456
7457/*
7458 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 */
7460 static void
7461ex_edit(eap)
7462 exarg_T *eap;
7463{
7464 do_exedit(eap, NULL);
7465}
7466
7467/*
7468 * ":edit <file>" command and alikes.
7469 */
7470/*ARGSUSED*/
7471 void
7472do_exedit(eap, old_curwin)
7473 exarg_T *eap;
7474 win_T *old_curwin; /* curwin before doing a split or NULL */
7475{
7476 int n;
7477#ifdef FEAT_WINDOWS
7478 int need_hide;
7479#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007480 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481
7482 /*
7483 * ":vi" command ends Ex mode.
7484 */
7485 if (exmode_active && (eap->cmdidx == CMD_visual
7486 || eap->cmdidx == CMD_view))
7487 {
7488 exmode_active = FALSE;
7489 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007490 {
7491 /* Special case: ":global/pat/visual\NLvi-commands" */
7492 if (global_busy)
7493 {
7494 int rd = RedrawingDisabled;
7495 int nwr = no_wait_return;
7496 int ms = msg_scroll;
7497#ifdef FEAT_GUI
7498 int he = hold_gui_events;
7499#endif
7500
7501 if (eap->nextcmd != NULL)
7502 {
7503 stuffReadbuff(eap->nextcmd);
7504 eap->nextcmd = NULL;
7505 }
7506
7507 if (exmode_was != EXMODE_VIM)
7508 settmode(TMODE_RAW);
7509 RedrawingDisabled = 0;
7510 no_wait_return = 0;
7511 need_wait_return = FALSE;
7512 msg_scroll = 0;
7513#ifdef FEAT_GUI
7514 hold_gui_events = 0;
7515#endif
7516 must_redraw = CLEAR;
7517
7518 main_loop(FALSE, TRUE);
7519
7520 RedrawingDisabled = rd;
7521 no_wait_return = nwr;
7522 msg_scroll = ms;
7523#ifdef FEAT_GUI
7524 hold_gui_events = he;
7525#endif
7526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 }
7530
7531 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007532 || eap->cmdidx == CMD_tabnew
7533 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534#ifdef FEAT_VERTSPLIT
7535 || eap->cmdidx == CMD_vnew
7536#endif
7537 ) && *eap->arg == NUL)
7538 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007539 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 setpcmark();
7541 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007542 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7543 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 }
7545 else if ((eap->cmdidx != CMD_split
7546#ifdef FEAT_VERTSPLIT
7547 && eap->cmdidx != CMD_vsplit
7548#endif
7549 )
7550 || *eap->arg != NUL
7551#ifdef FEAT_BROWSE
7552 || cmdmod.browse
7553#endif
7554 )
7555 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007556#ifdef FEAT_AUTOCMD
7557 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7558 * can bring us here, others are stopped earlier. */
7559 if (*eap->arg != NUL && curbuf_locked())
7560 return;
7561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 n = readonlymode;
7563 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7564 readonlymode = TRUE;
7565 else if (eap->cmdidx == CMD_enew)
7566 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7567 empty buffer */
7568 setpcmark();
7569 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7570 NULL, eap,
7571 /* ":edit" goes to first line if Vi compatible */
7572 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7573 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7574 ? ECMD_ONE : eap->do_ecmd_lnum,
7575 (P_HID(curbuf) ? ECMD_HIDE : 0)
7576 + (eap->forceit ? ECMD_FORCEIT : 0)
7577#ifdef FEAT_LISTCMDS
7578 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7579#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007580 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 {
7582 /* Editing the file failed. If the window was split, close it. */
7583#ifdef FEAT_WINDOWS
7584 if (old_curwin != NULL)
7585 {
7586 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7587 if (!need_hide || P_HID(curbuf))
7588 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007589# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7590 cleanup_T cs;
7591
7592 /* Reset the error/interrupt/exception state here so that
7593 * aborting() returns FALSE when closing a window. */
7594 enter_cleanup(&cs);
7595# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596# ifdef FEAT_GUI
7597 need_mouse_correct = TRUE;
7598# endif
7599 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007600
7601# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7602 /* Restore the error/interrupt/exception state if not
7603 * discarded by a new aborting error, interrupt, or
7604 * uncaught exception. */
7605 leave_cleanup(&cs);
7606# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 }
7608 }
7609#endif
7610 }
7611 else if (readonlymode && curbuf->b_nwindows == 1)
7612 {
7613 /* When editing an already visited buffer, 'readonly' won't be set
7614 * but the previous value is kept. With ":view" and ":sview" we
7615 * want the file to be readonly, except when another window is
7616 * editing the same buffer. */
7617 curbuf->b_p_ro = TRUE;
7618 }
7619 readonlymode = n;
7620 }
7621 else
7622 {
7623 if (eap->do_ecmd_cmd != NULL)
7624 do_cmdline_cmd(eap->do_ecmd_cmd);
7625#ifdef FEAT_TITLE
7626 n = curwin->w_arg_idx_invalid;
7627#endif
7628 check_arg_idx(curwin);
7629#ifdef FEAT_TITLE
7630 if (n != curwin->w_arg_idx_invalid)
7631 maketitle();
7632#endif
7633 }
7634
7635#ifdef FEAT_WINDOWS
7636 /*
7637 * if ":split file" worked, set alternate file name in old window to new
7638 * file
7639 */
7640 if (old_curwin != NULL
7641 && *eap->arg != NUL
7642 && curwin != old_curwin
7643 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007644 && old_curwin->w_buffer != curbuf
7645 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 old_curwin->w_alt_fnum = curbuf->b_fnum;
7647#endif
7648
7649 ex_no_reprint = TRUE;
7650}
7651
7652#ifndef FEAT_GUI
7653/*
7654 * ":gui" and ":gvim" when there is no GUI.
7655 */
7656 static void
7657ex_nogui(eap)
7658 exarg_T *eap;
7659{
7660 eap->errmsg = e_nogvim;
7661}
7662#endif
7663
7664#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7665 static void
7666ex_tearoff(eap)
7667 exarg_T *eap;
7668{
7669 gui_make_tearoff(eap->arg);
7670}
7671#endif
7672
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007673#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 static void
7675ex_popup(eap)
7676 exarg_T *eap;
7677{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007678 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679}
7680#endif
7681
7682/*ARGSUSED*/
7683 static void
7684ex_swapname(eap)
7685 exarg_T *eap;
7686{
7687 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7688 MSG(_("No swap file"));
7689 else
7690 msg(curbuf->b_ml.ml_mfp->mf_fname);
7691}
7692
7693/*
7694 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7695 * offset.
7696 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7697 */
7698/*ARGSUSED*/
7699 static void
7700ex_syncbind(eap)
7701 exarg_T *eap;
7702{
7703#ifdef FEAT_SCROLLBIND
7704 win_T *wp;
7705 long topline;
7706 long y;
7707 linenr_T old_linenr = curwin->w_cursor.lnum;
7708
7709 setpcmark();
7710
7711 /*
7712 * determine max topline
7713 */
7714 if (curwin->w_p_scb)
7715 {
7716 topline = curwin->w_topline;
7717 for (wp = firstwin; wp; wp = wp->w_next)
7718 {
7719 if (wp->w_p_scb && wp->w_buffer)
7720 {
7721 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7722 if (topline > y)
7723 topline = y;
7724 }
7725 }
7726 if (topline < 1)
7727 topline = 1;
7728 }
7729 else
7730 {
7731 topline = 1;
7732 }
7733
7734
7735 /*
7736 * set all scrollbind windows to the same topline
7737 */
7738 wp = curwin;
7739 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7740 {
7741 if (curwin->w_p_scb)
7742 {
7743 y = topline - curwin->w_topline;
7744 if (y > 0)
7745 scrollup(y, TRUE);
7746 else
7747 scrolldown(-y, TRUE);
7748 curwin->w_scbind_pos = topline;
7749 redraw_later(VALID);
7750 cursor_correct();
7751#ifdef FEAT_WINDOWS
7752 curwin->w_redr_status = TRUE;
7753#endif
7754 }
7755 }
7756 curwin = wp;
7757 if (curwin->w_p_scb)
7758 {
7759 did_syncbind = TRUE;
7760 checkpcmark();
7761 if (old_linenr != curwin->w_cursor.lnum)
7762 {
7763 char_u ctrl_o[2];
7764
7765 ctrl_o[0] = Ctrl_O;
7766 ctrl_o[1] = 0;
7767 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7768 }
7769 }
7770#endif
7771}
7772
7773
7774 static void
7775ex_read(eap)
7776 exarg_T *eap;
7777{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007778 int i;
7779 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7780 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781
7782 if (eap->usefilter) /* :r!cmd */
7783 do_bang(1, eap, FALSE, FALSE, TRUE);
7784 else
7785 {
7786 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7787 return;
7788
7789#ifdef FEAT_BROWSE
7790 if (cmdmod.browse)
7791 {
7792 char_u *browseFile;
7793
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007794 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 NULL, NULL, NULL, curbuf);
7796 if (browseFile != NULL)
7797 {
7798 i = readfile(browseFile, NULL,
7799 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7800 vim_free(browseFile);
7801 }
7802 else
7803 i = OK;
7804 }
7805 else
7806#endif
7807 if (*eap->arg == NUL)
7808 {
7809 if (check_fname() == FAIL) /* check for no file name */
7810 return;
7811 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7812 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7813 }
7814 else
7815 {
7816 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7817 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7818 i = readfile(eap->arg, NULL,
7819 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7820
7821 }
7822 if (i == FAIL)
7823 {
7824#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7825 if (!aborting())
7826#endif
7827 EMSG2(_(e_notopen), eap->arg);
7828 }
7829 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007830 {
7831 if (empty && exmode_active)
7832 {
7833 /* Delete the empty line that remains. Historically ex does
7834 * this but vi doesn't. */
7835 if (eap->line2 == 0)
7836 lnum = curbuf->b_ml.ml_line_count;
7837 else
7838 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007839 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007840 {
7841 ml_delete(lnum, FALSE);
7842 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007843 if (curwin->w_cursor.lnum > 1
7844 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007845 --curwin->w_cursor.lnum;
7846 }
7847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 }
7851}
7852
Bram Moolenaarea408852005-06-25 22:49:46 +00007853static char_u *prev_dir = NULL;
7854
7855#if defined(EXITFREE) || defined(PROTO)
7856 void
7857free_cd_dir()
7858{
7859 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007860 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007861
7862 vim_free(globaldir);
7863 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007864}
7865#endif
7866
7867
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868/*
7869 * ":cd", ":lcd", ":chdir" and ":lchdir".
7870 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007871 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872ex_cd(eap)
7873 exarg_T *eap;
7874{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 char_u *new_dir;
7876 char_u *tofree;
7877
7878 new_dir = eap->arg;
7879#if !defined(UNIX) && !defined(VMS)
7880 /* for non-UNIX ":cd" means: print current directory */
7881 if (*new_dir == NUL)
7882 ex_pwd(NULL);
7883 else
7884#endif
7885 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007886#ifdef FEAT_AUTOCMD
7887 if (allbuf_locked())
7888 return;
7889#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007890 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7891 && !eap->forceit)
7892 {
Bram Moolenaar81870892007-11-11 18:17:28 +00007893 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007894 return;
7895 }
7896
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 /* ":cd -": Change to previous directory */
7898 if (STRCMP(new_dir, "-") == 0)
7899 {
7900 if (prev_dir == NULL)
7901 {
7902 EMSG(_("E186: No previous directory"));
7903 return;
7904 }
7905 new_dir = prev_dir;
7906 }
7907
7908 /* Save current directory for next ":cd -" */
7909 tofree = prev_dir;
7910 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7911 prev_dir = vim_strsave(NameBuff);
7912 else
7913 prev_dir = NULL;
7914
7915#if defined(UNIX) || defined(VMS)
7916 /* for UNIX ":cd" means: go to home directory */
7917 if (*new_dir == NUL)
7918 {
7919 /* use NameBuff for home directory name */
7920# ifdef VMS
7921 char_u *p;
7922
7923 p = mch_getenv((char_u *)"SYS$LOGIN");
7924 if (p == NULL || *p == NUL) /* empty is the same as not set */
7925 NameBuff[0] = NUL;
7926 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007927 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928# else
7929 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7930# endif
7931 new_dir = NameBuff;
7932 }
7933#endif
7934 if (new_dir == NULL || vim_chdir(new_dir))
7935 EMSG(_(e_failed));
7936 else
7937 {
7938 vim_free(curwin->w_localdir);
7939 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7940 {
7941 /* If still in global directory, need to remember current
7942 * directory as global directory. */
7943 if (globaldir == NULL && prev_dir != NULL)
7944 globaldir = vim_strsave(prev_dir);
7945 /* Remember this local directory for the window. */
7946 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7947 curwin->w_localdir = vim_strsave(NameBuff);
7948 }
7949 else
7950 {
7951 /* We are now in the global directory, no need to remember its
7952 * name. */
7953 vim_free(globaldir);
7954 globaldir = NULL;
7955 curwin->w_localdir = NULL;
7956 }
7957
7958 shorten_fnames(TRUE);
7959
7960 /* Echo the new current directory if the command was typed. */
7961 if (KeyTyped)
7962 ex_pwd(eap);
7963 }
7964 vim_free(tofree);
7965 }
7966}
7967
7968/*
7969 * ":pwd".
7970 */
7971/*ARGSUSED*/
7972 static void
7973ex_pwd(eap)
7974 exarg_T *eap;
7975{
7976 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7977 {
7978#ifdef BACKSLASH_IN_FILENAME
7979 slash_adjust(NameBuff);
7980#endif
7981 msg(NameBuff);
7982 }
7983 else
7984 EMSG(_("E187: Unknown"));
7985}
7986
7987/*
7988 * ":=".
7989 */
7990 static void
7991ex_equal(eap)
7992 exarg_T *eap;
7993{
7994 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007995 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996}
7997
7998 static void
7999ex_sleep(eap)
8000 exarg_T *eap;
8001{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008002 int n;
8003 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004
8005 if (cursor_valid())
8006 {
8007 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8008 if (n >= 0)
8009 windgoto((int)n, curwin->w_wcol);
8010 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008011
8012 len = eap->line2;
8013 switch (*eap->arg)
8014 {
8015 case 'm': break;
8016 case NUL: len *= 1000L; break;
8017 default: EMSG2(_(e_invarg2), eap->arg); return;
8018 }
8019 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020}
8021
8022/*
8023 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8024 */
8025 void
8026do_sleep(msec)
8027 long msec;
8028{
8029 long done;
8030
8031 cursor_on();
8032 out_flush();
8033 for (done = 0; !got_int && done < msec; done += 1000L)
8034 {
8035 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8036 ui_breakcheck();
8037 }
8038}
8039
8040 static void
8041do_exmap(eap, isabbrev)
8042 exarg_T *eap;
8043 int isabbrev;
8044{
8045 int mode;
8046 char_u *cmdp;
8047
8048 cmdp = eap->cmd;
8049 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8050
8051 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8052 eap->arg, mode, isabbrev))
8053 {
8054 case 1: EMSG(_(e_invarg));
8055 break;
8056 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8057 break;
8058 }
8059}
8060
8061/*
8062 * ":winsize" command (obsolete).
8063 */
8064 static void
8065ex_winsize(eap)
8066 exarg_T *eap;
8067{
8068 int w, h;
8069 char_u *arg = eap->arg;
8070 char_u *p;
8071
8072 w = getdigits(&arg);
8073 arg = skipwhite(arg);
8074 p = arg;
8075 h = getdigits(&arg);
8076 if (*p != NUL && *arg == NUL)
8077 set_shellsize(w, h, TRUE);
8078 else
8079 EMSG(_("E465: :winsize requires two number arguments"));
8080}
8081
8082#ifdef FEAT_WINDOWS
8083 static void
8084ex_wincmd(eap)
8085 exarg_T *eap;
8086{
8087 int xchar = NUL;
8088 char_u *p;
8089
8090 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8091 {
8092 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8093 if (eap->arg[1] == NUL)
8094 {
8095 EMSG(_(e_invarg));
8096 return;
8097 }
8098 xchar = eap->arg[1];
8099 p = eap->arg + 2;
8100 }
8101 else
8102 p = eap->arg + 1;
8103
8104 eap->nextcmd = check_nextcmd(p);
8105 p = skipwhite(p);
8106 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8107 EMSG(_(e_invarg));
8108 else
8109 {
8110 /* Pass flags on for ":vertical wincmd ]". */
8111 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008112 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8114 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008115 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 }
8117}
8118#endif
8119
Bram Moolenaar843ee412004-06-30 16:16:41 +00008120#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121/*
8122 * ":winpos".
8123 */
8124 static void
8125ex_winpos(eap)
8126 exarg_T *eap;
8127{
8128 int x, y;
8129 char_u *arg = eap->arg;
8130 char_u *p;
8131
8132 if (*arg == NUL)
8133 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008134# if defined(FEAT_GUI) || defined(MSWIN)
8135# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008137# else
8138 if (mch_get_winpos(&x, &y) != FAIL)
8139# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {
8141 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8142 msg(IObuff);
8143 }
8144 else
8145# endif
8146 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8147 }
8148 else
8149 {
8150 x = getdigits(&arg);
8151 arg = skipwhite(arg);
8152 p = arg;
8153 y = getdigits(&arg);
8154 if (*p == NUL || *arg != NUL)
8155 {
8156 EMSG(_("E466: :winpos requires two number arguments"));
8157 return;
8158 }
8159# ifdef FEAT_GUI
8160 if (gui.in_use)
8161 gui_mch_set_winpos(x, y);
8162 else if (gui.starting)
8163 {
8164 /* Remember the coordinates for when the window is opened. */
8165 gui_win_x = x;
8166 gui_win_y = y;
8167 }
8168# ifdef HAVE_TGETENT
8169 else
8170# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008171# else
8172# ifdef MSWIN
8173 mch_set_winpos(x, y);
8174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175# endif
8176# ifdef HAVE_TGETENT
8177 if (*T_CWP)
8178 term_set_winpos(x, y);
8179# endif
8180 }
8181}
8182#endif
8183
8184/*
8185 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8186 */
8187 static void
8188ex_operators(eap)
8189 exarg_T *eap;
8190{
8191 oparg_T oa;
8192
8193 clear_oparg(&oa);
8194 oa.regname = eap->regname;
8195 oa.start.lnum = eap->line1;
8196 oa.end.lnum = eap->line2;
8197 oa.line_count = eap->line2 - eap->line1 + 1;
8198 oa.motion_type = MLINE;
8199#ifdef FEAT_VIRTUALEDIT
8200 virtual_op = FALSE;
8201#endif
8202 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8203 {
8204 setpcmark();
8205 curwin->w_cursor.lnum = eap->line1;
8206 beginline(BL_SOL | BL_FIX);
8207 }
8208
8209 switch (eap->cmdidx)
8210 {
8211 case CMD_delete:
8212 oa.op_type = OP_DELETE;
8213 op_delete(&oa);
8214 break;
8215
8216 case CMD_yank:
8217 oa.op_type = OP_YANK;
8218 (void)op_yank(&oa, FALSE, TRUE);
8219 break;
8220
8221 default: /* CMD_rshift or CMD_lshift */
8222 if ((eap->cmdidx == CMD_rshift)
8223#ifdef FEAT_RIGHTLEFT
8224 ^ curwin->w_p_rl
8225#endif
8226 )
8227 oa.op_type = OP_RSHIFT;
8228 else
8229 oa.op_type = OP_LSHIFT;
8230 op_shift(&oa, FALSE, eap->amount);
8231 break;
8232 }
8233#ifdef FEAT_VIRTUALEDIT
8234 virtual_op = MAYBE;
8235#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008236 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237}
8238
8239/*
8240 * ":put".
8241 */
8242 static void
8243ex_put(eap)
8244 exarg_T *eap;
8245{
8246 /* ":0put" works like ":1put!". */
8247 if (eap->line2 == 0)
8248 {
8249 eap->line2 = 1;
8250 eap->forceit = TRUE;
8251 }
8252 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008253 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8254 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255}
8256
8257/*
8258 * Handle ":copy" and ":move".
8259 */
8260 static void
8261ex_copymove(eap)
8262 exarg_T *eap;
8263{
8264 long n;
8265
8266 n = get_address(&eap->arg, FALSE, FALSE);
8267 if (eap->arg == NULL) /* error detected */
8268 {
8269 eap->nextcmd = NULL;
8270 return;
8271 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008272 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273
8274 /*
8275 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8276 */
8277 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8278 {
8279 EMSG(_(e_invaddr));
8280 return;
8281 }
8282
8283 if (eap->cmdidx == CMD_move)
8284 {
8285 if (do_move(eap->line1, eap->line2, n) == FAIL)
8286 return;
8287 }
8288 else
8289 ex_copy(eap->line1, eap->line2, n);
8290 u_clearline();
8291 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008292 ex_may_print(eap);
8293}
8294
8295/*
8296 * Print the current line if flags were given to the Ex command.
8297 */
8298 static void
8299ex_may_print(eap)
8300 exarg_T *eap;
8301{
8302 if (eap->flags != 0)
8303 {
8304 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8305 (eap->flags & EXFLAG_LIST));
8306 ex_no_reprint = TRUE;
8307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308}
8309
8310/*
8311 * ":smagic" and ":snomagic".
8312 */
8313 static void
8314ex_submagic(eap)
8315 exarg_T *eap;
8316{
8317 int magic_save = p_magic;
8318
8319 p_magic = (eap->cmdidx == CMD_smagic);
8320 do_sub(eap);
8321 p_magic = magic_save;
8322}
8323
8324/*
8325 * ":join".
8326 */
8327 static void
8328ex_join(eap)
8329 exarg_T *eap;
8330{
8331 curwin->w_cursor.lnum = eap->line1;
8332 if (eap->line1 == eap->line2)
8333 {
8334 if (eap->addr_count >= 2) /* :2,2join does nothing */
8335 return;
8336 if (eap->line2 == curbuf->b_ml.ml_line_count)
8337 {
8338 beep_flush();
8339 return;
8340 }
8341 ++eap->line2;
8342 }
8343 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8344 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008345 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346}
8347
8348/*
8349 * ":[addr]@r" or ":[addr]*r": execute register
8350 */
8351 static void
8352ex_at(eap)
8353 exarg_T *eap;
8354{
8355 int c;
8356
8357 curwin->w_cursor.lnum = eap->line2;
8358
8359#ifdef USE_ON_FLY_SCROLL
8360 dont_scroll = TRUE; /* disallow scrolling here */
8361#endif
8362
8363 /* get the register name. No name means to use the previous one */
8364 c = *eap->arg;
8365 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8366 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008367 /* Put the register in the typeahead buffer with the "silent" flag. */
8368 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8369 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 else
8374 {
8375 int save_efr = exec_from_reg;
8376
8377 exec_from_reg = TRUE;
8378
8379 /*
8380 * Execute from the typeahead buffer.
8381 * Originally this didn't check for the typeahead buffer to be empty,
8382 * thus could read more Ex commands from stdin. It's not clear why,
8383 * it is certainly unexpected.
8384 */
8385 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8386 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8387
8388 exec_from_reg = save_efr;
8389 }
8390}
8391
8392/*
8393 * ":!".
8394 */
8395 static void
8396ex_bang(eap)
8397 exarg_T *eap;
8398{
8399 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8400}
8401
8402/*
8403 * ":undo".
8404 */
8405/*ARGSUSED*/
8406 static void
8407ex_undo(eap)
8408 exarg_T *eap;
8409{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008410 if (eap->addr_count == 1) /* :undo 123 */
8411 undo_time(eap->line2, FALSE, TRUE);
8412 else
8413 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414}
8415
8416/*
8417 * ":redo".
8418 */
8419/*ARGSUSED*/
8420 static void
8421ex_redo(eap)
8422 exarg_T *eap;
8423{
8424 u_redo(1);
8425}
8426
8427/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008428 * ":earlier" and ":later".
8429 */
8430/*ARGSUSED*/
8431 static void
8432ex_later(eap)
8433 exarg_T *eap;
8434{
8435 long count = 0;
8436 int sec = FALSE;
8437 char_u *p = eap->arg;
8438
8439 if (*p == NUL)
8440 count = 1;
8441 else if (isdigit(*p))
8442 {
8443 count = getdigits(&p);
8444 switch (*p)
8445 {
8446 case 's': ++p; sec = TRUE; break;
8447 case 'm': ++p; sec = TRUE; count *= 60; break;
8448 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
8449 }
8450 }
8451
8452 if (*p != NUL)
8453 EMSG2(_(e_invarg2), eap->arg);
8454 else
Bram Moolenaard3667a22006-03-16 21:35:52 +00008455 undo_time(eap->cmdidx == CMD_earlier ? -count : count, sec, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008456}
8457
8458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 * ":redir": start/stop redirection.
8460 */
8461 static void
8462ex_redir(eap)
8463 exarg_T *eap;
8464{
8465 char *mode;
8466 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008467 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468
8469 if (STRICMP(eap->arg, "END") == 0)
8470 close_redir();
8471 else
8472 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008473 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008475 ++arg;
8476 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008478 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 mode = "a";
8480 }
8481 else
8482 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008483 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484
8485 close_redir();
8486
8487 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008488 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 if (fname == NULL)
8490 return;
8491#ifdef FEAT_BROWSE
8492 if (cmdmod.browse)
8493 {
8494 char_u *browseFile;
8495
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008496 browseFile = do_browse(BROWSE_SAVE,
8497 (char_u *)_("Save Redirection"),
8498 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 if (browseFile == NULL)
8500 return; /* operation cancelled */
8501 vim_free(fname);
8502 fname = browseFile;
8503 eap->forceit = TRUE; /* since dialog already asked */
8504 }
8505#endif
8506
8507 redir_fd = open_exfile(fname, eap->forceit, mode);
8508 vim_free(fname);
8509 }
8510#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008511 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {
8513 /* redirect to a register a-z (resp. A-Z for appending) */
8514 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008515 ++arg;
8516 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008518 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008519 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008521 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008523 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008524 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008525 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008526 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008528 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008529 if (*arg == '>')
8530 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008531 /* Make register empty when not using @A-@Z and the
8532 * command is valid. */
8533 if (*arg == NUL && !isupper(redir_reg))
8534 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008536 }
8537 if (*arg != NUL)
8538 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008539 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008540 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008541 }
8542 }
8543 else if (*arg == '=' && arg[1] == '>')
8544 {
8545 int append;
8546
8547 /* redirect to a variable */
8548 close_redir();
8549 arg += 2;
8550
8551 if (*arg == '>')
8552 {
8553 ++arg;
8554 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 }
8556 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008557 append = FALSE;
8558
8559 if (var_redir_start(skipwhite(arg), append) == OK)
8560 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 }
8562#endif
8563
8564 /* TODO: redirect to a buffer */
8565
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008567 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008569
8570 /* Make sure redirection is not off. Can happen for cmdline completion
8571 * that indirectly invokes a command to catch its output. */
8572 if (redir_fd != NULL
8573#ifdef FEAT_EVAL
8574 || redir_reg || redir_vname
8575#endif
8576 )
8577 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578}
8579
8580/*
8581 * ":redraw": force redraw
8582 */
8583 static void
8584ex_redraw(eap)
8585 exarg_T *eap;
8586{
8587 int r = RedrawingDisabled;
8588 int p = p_lz;
8589
8590 RedrawingDisabled = 0;
8591 p_lz = FALSE;
8592 update_topline();
8593 update_screen(eap->forceit ? CLEAR :
8594#ifdef FEAT_VISUAL
8595 VIsual_active ? INVERTED :
8596#endif
8597 0);
8598#ifdef FEAT_TITLE
8599 if (need_maketitle)
8600 maketitle();
8601#endif
8602 RedrawingDisabled = r;
8603 p_lz = p;
8604
8605 /* Reset msg_didout, so that a message that's there is overwritten. */
8606 msg_didout = FALSE;
8607 msg_col = 0;
8608
8609 out_flush();
8610}
8611
8612/*
8613 * ":redrawstatus": force redraw of status line(s)
8614 */
8615/*ARGSUSED*/
8616 static void
8617ex_redrawstatus(eap)
8618 exarg_T *eap;
8619{
8620#if defined(FEAT_WINDOWS)
8621 int r = RedrawingDisabled;
8622 int p = p_lz;
8623
8624 RedrawingDisabled = 0;
8625 p_lz = FALSE;
8626 if (eap->forceit)
8627 status_redraw_all();
8628 else
8629 status_redraw_curbuf();
8630 update_screen(
8631# ifdef FEAT_VISUAL
8632 VIsual_active ? INVERTED :
8633# endif
8634 0);
8635 RedrawingDisabled = r;
8636 p_lz = p;
8637 out_flush();
8638#endif
8639}
8640
8641 static void
8642close_redir()
8643{
8644 if (redir_fd != NULL)
8645 {
8646 fclose(redir_fd);
8647 redir_fd = NULL;
8648 }
8649#ifdef FEAT_EVAL
8650 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008651 if (redir_vname)
8652 {
8653 var_redir_stop();
8654 redir_vname = 0;
8655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656#endif
8657}
8658
8659#if defined(FEAT_SESSION) && defined(USE_CRNL)
8660# define MKSESSION_NL
8661static int mksession_nl = FALSE; /* use NL only in put_eol() */
8662#endif
8663
8664/*
8665 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8666 */
8667 static void
8668ex_mkrc(eap)
8669 exarg_T *eap;
8670{
8671 FILE *fd;
8672 int failed = FALSE;
8673 char_u *fname;
8674#ifdef FEAT_BROWSE
8675 char_u *browseFile = NULL;
8676#endif
8677#ifdef FEAT_SESSION
8678 int view_session = FALSE;
8679 int using_vdir = FALSE; /* using 'viewdir'? */
8680 char_u *viewFile = NULL;
8681 unsigned *flagp;
8682#endif
8683
8684 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8685 {
8686#ifdef FEAT_SESSION
8687 view_session = TRUE;
8688#else
8689 ex_ni(eap);
8690 return;
8691#endif
8692 }
8693
8694#ifdef FEAT_SESSION
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008695 did_lcd = FALSE;
8696
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8698 if (eap->cmdidx == CMD_mkview
8699 && (*eap->arg == NUL
8700 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8701 {
8702 eap->forceit = TRUE;
8703 fname = get_view_file(*eap->arg);
8704 if (fname == NULL)
8705 return;
8706 viewFile = fname;
8707 using_vdir = TRUE;
8708 }
8709 else
8710#endif
8711 if (*eap->arg != NUL)
8712 fname = eap->arg;
8713 else if (eap->cmdidx == CMD_mkvimrc)
8714 fname = (char_u *)VIMRC_FILE;
8715#ifdef FEAT_SESSION
8716 else if (eap->cmdidx == CMD_mksession)
8717 fname = (char_u *)SESSION_FILE;
8718#endif
8719 else
8720 fname = (char_u *)EXRC_FILE;
8721
8722#ifdef FEAT_BROWSE
8723 if (cmdmod.browse)
8724 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008725 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726# ifdef FEAT_SESSION
8727 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8728 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8729# endif
8730 (char_u *)_("Save Setup"),
8731 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8732 if (browseFile == NULL)
8733 goto theend;
8734 fname = browseFile;
8735 eap->forceit = TRUE; /* since dialog already asked */
8736 }
8737#endif
8738
8739#if defined(FEAT_SESSION) && defined(vim_mkdir)
8740 /* When using 'viewdir' may have to create the directory. */
8741 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008742 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743#endif
8744
8745 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8746 if (fd != NULL)
8747 {
8748#ifdef FEAT_SESSION
8749 if (eap->cmdidx == CMD_mkview)
8750 flagp = &vop_flags;
8751 else
8752 flagp = &ssop_flags;
8753#endif
8754
8755#ifdef MKSESSION_NL
8756 /* "unix" in 'sessionoptions': use NL line separator */
8757 if (view_session && (*flagp & SSOP_UNIX))
8758 mksession_nl = TRUE;
8759#endif
8760
8761 /* Write the version command for :mkvimrc */
8762 if (eap->cmdidx == CMD_mkvimrc)
8763 (void)put_line(fd, "version 6.0");
8764
8765#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008766 if (eap->cmdidx == CMD_mksession)
8767 {
8768 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8769 failed = TRUE;
8770 }
8771
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 if (eap->cmdidx != CMD_mkview)
8773#endif
8774 {
8775 /* Write setting 'compatible' first, because it has side effects.
8776 * For that same reason only do it when needed. */
8777 if (p_cp)
8778 (void)put_line(fd, "if !&cp | set cp | endif");
8779 else
8780 (void)put_line(fd, "if &cp | set nocp | endif");
8781 }
8782
8783#ifdef FEAT_SESSION
8784 if (!view_session
8785 || (eap->cmdidx == CMD_mksession
8786 && (*flagp & SSOP_OPTIONS)))
8787#endif
8788 failed |= (makemap(fd, NULL) == FAIL
8789 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8790
8791#ifdef FEAT_SESSION
8792 if (!failed && view_session)
8793 {
8794 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8795 failed = TRUE;
8796 if (eap->cmdidx == CMD_mksession)
8797 {
8798 char_u dirnow[MAXPATHL]; /* current directory */
8799
8800 /*
8801 * Change to session file's dir.
8802 */
8803 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8804 || mch_chdir((char *)dirnow) != 0)
8805 *dirnow = NUL;
8806 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8807 {
8808 if (vim_chdirfile(fname) == OK)
8809 shorten_fnames(TRUE);
8810 }
8811 else if (*dirnow != NUL
8812 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8813 {
Bram Moolenaarc0b35652009-03-01 01:45:35 +00008814 if (mch_chdir((char *)globaldir) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00008815 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 }
8817
8818 failed |= (makeopens(fd, dirnow) == FAIL);
8819
8820 /* restore original dir */
8821 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8822 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8823 {
8824 if (mch_chdir((char *)dirnow) != 0)
8825 EMSG(_(e_prev_dir));
8826 shorten_fnames(TRUE);
8827 }
8828 }
8829 else
8830 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008831 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8832 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 }
8834 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8835 == FAIL)
8836 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008837 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8838 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008839 if (eap->cmdidx == CMD_mksession)
8840 {
8841 if (put_line(fd, "unlet SessionLoad") == FAIL)
8842 failed = TRUE;
8843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 }
8845#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008846 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8847 failed = TRUE;
8848
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 failed |= fclose(fd);
8850
8851 if (failed)
8852 EMSG(_(e_write));
8853#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8854 else if (eap->cmdidx == CMD_mksession)
8855 {
8856 /* successful session write - set this_session var */
8857 char_u tbuf[MAXPATHL];
8858
8859 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8860 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8861 }
8862#endif
8863#ifdef MKSESSION_NL
8864 mksession_nl = FALSE;
8865#endif
8866 }
8867
8868#ifdef FEAT_BROWSE
8869theend:
8870 vim_free(browseFile);
8871#endif
8872#ifdef FEAT_SESSION
8873 vim_free(viewFile);
8874#endif
8875}
8876
Bram Moolenaardf177f62005-02-22 08:39:57 +00008877#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8878 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008879/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008880 int
8881vim_mkdir_emsg(name, prot)
8882 char_u *name;
8883 int prot;
8884{
8885 if (vim_mkdir(name, prot) != 0)
8886 {
8887 EMSG2(_("E739: Cannot create directory: %s"), name);
8888 return FAIL;
8889 }
8890 return OK;
8891}
8892#endif
8893
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894/*
8895 * Open a file for writing for an Ex command, with some checks.
8896 * Return file descriptor, or NULL on failure.
8897 */
8898 FILE *
8899open_exfile(fname, forceit, mode)
8900 char_u *fname;
8901 int forceit;
8902 char *mode; /* "w" for create new file or "a" for append */
8903{
8904 FILE *fd;
8905
8906#ifdef UNIX
8907 /* with Unix it is possible to open a directory */
8908 if (mch_isdir(fname))
8909 {
8910 EMSG2(_(e_isadir2), fname);
8911 return NULL;
8912 }
8913#endif
8914 if (!forceit && *mode != 'a' && vim_fexists(fname))
8915 {
8916 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8917 return NULL;
8918 }
8919
8920 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8921 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8922
8923 return fd;
8924}
8925
8926/*
8927 * ":mark" and ":k".
8928 */
8929 static void
8930ex_mark(eap)
8931 exarg_T *eap;
8932{
8933 pos_T pos;
8934
8935 if (*eap->arg == NUL) /* No argument? */
8936 EMSG(_(e_argreq));
8937 else if (eap->arg[1] != NUL) /* more than one character? */
8938 EMSG(_(e_trailing));
8939 else
8940 {
8941 pos = curwin->w_cursor; /* save curwin->w_cursor */
8942 curwin->w_cursor.lnum = eap->line2;
8943 beginline(BL_WHITE | BL_FIX);
8944 if (setmark(*eap->arg) == FAIL) /* set mark */
8945 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8946 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8947 }
8948}
8949
8950/*
8951 * Update w_topline, w_leftcol and the cursor position.
8952 */
8953 void
8954update_topline_cursor()
8955{
8956 check_cursor(); /* put cursor on valid line */
8957 update_topline();
8958 if (!curwin->w_p_wrap)
8959 validate_cursor();
8960 update_curswant();
8961}
8962
8963#ifdef FEAT_EX_EXTRA
8964/*
8965 * ":normal[!] {commands}": Execute normal mode commands.
8966 */
8967 static void
8968ex_normal(eap)
8969 exarg_T *eap;
8970{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 int save_msg_scroll = msg_scroll;
8972 int save_restart_edit = restart_edit;
8973 int save_msg_didout = msg_didout;
8974 int save_State = State;
8975 tasave_T tabuf;
8976 int save_insertmode = p_im;
8977 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00008978 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979#ifdef FEAT_MBYTE
8980 char_u *arg = NULL;
8981 int l;
8982 char_u *p;
8983#endif
8984
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008985 if (ex_normal_lock > 0)
8986 {
8987 EMSG(_(e_secure));
8988 return;
8989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 if (ex_normal_busy >= p_mmd)
8991 {
8992 EMSG(_("E192: Recursive use of :normal too deep"));
8993 return;
8994 }
8995 ++ex_normal_busy;
8996
8997 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8998 restart_edit = 0; /* don't go to Insert mode */
8999 p_im = FALSE; /* don't use 'insertmode' */
9000
9001#ifdef FEAT_MBYTE
9002 /*
9003 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9004 * this for the K_SPECIAL leading byte, otherwise special keys will not
9005 * work.
9006 */
9007 if (has_mbyte)
9008 {
9009 int len = 0;
9010
9011 /* Count the number of characters to be escaped. */
9012 for (p = eap->arg; *p != NUL; ++p)
9013 {
9014# ifdef FEAT_GUI
9015 if (*p == CSI) /* leadbyte CSI */
9016 len += 2;
9017# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009018 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9020# ifdef FEAT_GUI
9021 || *p == CSI
9022# endif
9023 )
9024 len += 2;
9025 }
9026 if (len > 0)
9027 {
9028 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9029 if (arg != NULL)
9030 {
9031 len = 0;
9032 for (p = eap->arg; *p != NUL; ++p)
9033 {
9034 arg[len++] = *p;
9035# ifdef FEAT_GUI
9036 if (*p == CSI)
9037 {
9038 arg[len++] = KS_EXTRA;
9039 arg[len++] = (int)KE_CSI;
9040 }
9041# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009042 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 {
9044 arg[len++] = *++p;
9045 if (*p == K_SPECIAL)
9046 {
9047 arg[len++] = KS_SPECIAL;
9048 arg[len++] = KE_FILLER;
9049 }
9050# ifdef FEAT_GUI
9051 else if (*p == CSI)
9052 {
9053 arg[len++] = KS_EXTRA;
9054 arg[len++] = (int)KE_CSI;
9055 }
9056# endif
9057 }
9058 arg[len] = NUL;
9059 }
9060 }
9061 }
9062 }
9063#endif
9064
9065 /*
9066 * Save the current typeahead. This is required to allow using ":normal"
9067 * from an event handler and makes sure we don't hang when the argument
9068 * ends with half a command.
9069 */
9070 save_typeahead(&tabuf);
9071 if (tabuf.typebuf_valid)
9072 {
9073 /*
9074 * Repeat the :normal command for each line in the range. When no
9075 * range given, execute it just once, without positioning the cursor
9076 * first.
9077 */
9078 do
9079 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 if (eap->addr_count != 0)
9081 {
9082 curwin->w_cursor.lnum = eap->line1++;
9083 curwin->w_cursor.col = 0;
9084 }
9085
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009086 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087#ifdef FEAT_MBYTE
9088 arg != NULL ? arg :
9089#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009090 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 }
9092 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9093 }
9094
9095 /* Might not return to the main loop when in an event handler. */
9096 update_topline_cursor();
9097
9098 /* Restore the previous typeahead. */
9099 restore_typeahead(&tabuf);
9100
9101 --ex_normal_busy;
9102 msg_scroll = save_msg_scroll;
9103 restart_edit = save_restart_edit;
9104 p_im = save_insertmode;
9105 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009106 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9108
9109 /* Restore the state (needed when called from a function executed for
9110 * 'indentexpr'). */
9111 State = save_State;
9112#ifdef FEAT_MBYTE
9113 vim_free(arg);
9114#endif
9115}
9116
9117/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009118 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 */
9120 static void
9121ex_startinsert(eap)
9122 exarg_T *eap;
9123{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009124 if (eap->forceit)
9125 {
9126 coladvance((colnr_T)MAXCOL);
9127 curwin->w_curswant = MAXCOL;
9128 curwin->w_set_curswant = FALSE;
9129 }
9130
Bram Moolenaara40c5002005-01-09 21:16:21 +00009131 /* Ignore the command when already in Insert mode. Inserting an
9132 * expression register that invokes a function can do this. */
9133 if (State & INSERT)
9134 return;
9135
Bram Moolenaar64969662005-12-14 21:59:55 +00009136 if (eap->cmdidx == CMD_startinsert)
9137 restart_edit = 'a';
9138 else if (eap->cmdidx == CMD_startreplace)
9139 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009141 restart_edit = 'V';
9142
9143 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009145 if (eap->cmdidx == CMD_startinsert)
9146 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 curwin->w_curswant = 0; /* avoid MAXCOL */
9148 }
9149}
9150
9151/*
9152 * ":stopinsert"
9153 */
9154/*ARGSUSED*/
9155 static void
9156ex_stopinsert(eap)
9157 exarg_T *eap;
9158{
9159 restart_edit = 0;
9160 stop_insert_mode = TRUE;
9161}
9162#endif
9163
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009164#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9165/*
9166 * Execute normal mode command "cmd".
9167 * "remap" can be REMAP_NONE or REMAP_YES.
9168 */
9169 void
9170exec_normal_cmd(cmd, remap, silent)
9171 char_u *cmd;
9172 int remap;
9173 int silent;
9174{
9175 oparg_T oa;
9176
9177 /*
9178 * Stuff the argument into the typeahead buffer.
9179 * Execute normal_cmd() until there is no typeahead left.
9180 */
9181 clear_oparg(&oa);
9182 finish_op = FALSE;
9183 ins_typebuf(cmd, remap, 0, TRUE, silent);
9184 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9185 && !got_int)
9186 {
9187 update_topline_cursor();
9188 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
9189 }
9190}
9191#endif
9192
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193#ifdef FEAT_FIND_ID
9194 static void
9195ex_checkpath(eap)
9196 exarg_T *eap;
9197{
9198 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9199 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9200 (linenr_T)1, (linenr_T)MAXLNUM);
9201}
9202
9203#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9204/*
9205 * ":psearch"
9206 */
9207 static void
9208ex_psearch(eap)
9209 exarg_T *eap;
9210{
9211 g_do_tagpreview = p_pvh;
9212 ex_findpat(eap);
9213 g_do_tagpreview = 0;
9214}
9215#endif
9216
9217 static void
9218ex_findpat(eap)
9219 exarg_T *eap;
9220{
9221 int whole = TRUE;
9222 long n;
9223 char_u *p;
9224 int action;
9225
9226 switch (cmdnames[eap->cmdidx].cmd_name[2])
9227 {
9228 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9229 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9230 action = ACTION_GOTO;
9231 else
9232 action = ACTION_SHOW;
9233 break;
9234 case 'i': /* ":ilist" and ":dlist" */
9235 action = ACTION_SHOW_ALL;
9236 break;
9237 case 'u': /* ":ijump" and ":djump" */
9238 action = ACTION_GOTO;
9239 break;
9240 default: /* ":isplit" and ":dsplit" */
9241 action = ACTION_SPLIT;
9242 break;
9243 }
9244
9245 n = 1;
9246 if (vim_isdigit(*eap->arg)) /* get count */
9247 {
9248 n = getdigits(&eap->arg);
9249 eap->arg = skipwhite(eap->arg);
9250 }
9251 if (*eap->arg == '/') /* Match regexp, not just whole words */
9252 {
9253 whole = FALSE;
9254 ++eap->arg;
9255 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9256 if (*p)
9257 {
9258 *p++ = NUL;
9259 p = skipwhite(p);
9260
9261 /* Check for trailing illegal characters */
9262 if (!ends_excmd(*p))
9263 eap->errmsg = e_trailing;
9264 else
9265 eap->nextcmd = check_nextcmd(p);
9266 }
9267 }
9268 if (!eap->skip)
9269 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9270 whole, !eap->forceit,
9271 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9272 n, action, eap->line1, eap->line2);
9273}
9274#endif
9275
9276#ifdef FEAT_WINDOWS
9277
9278# ifdef FEAT_QUICKFIX
9279/*
9280 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9281 */
9282 static void
9283ex_ptag(eap)
9284 exarg_T *eap;
9285{
9286 g_do_tagpreview = p_pvh;
9287 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9288}
9289
9290/*
9291 * ":pedit"
9292 */
9293 static void
9294ex_pedit(eap)
9295 exarg_T *eap;
9296{
9297 win_T *curwin_save = curwin;
9298
9299 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009300 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 keep_help_flag = curwin_save->w_buffer->b_help;
9302 do_exedit(eap, NULL);
9303 keep_help_flag = FALSE;
9304 if (curwin != curwin_save && win_valid(curwin_save))
9305 {
9306 /* Return cursor to where we were */
9307 validate_cursor();
9308 redraw_later(VALID);
9309 win_enter(curwin_save, TRUE);
9310 }
9311 g_do_tagpreview = 0;
9312}
9313# endif
9314
9315/*
9316 * ":stag", ":stselect" and ":stjump".
9317 */
9318 static void
9319ex_stag(eap)
9320 exarg_T *eap;
9321{
9322 postponed_split = -1;
9323 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009324 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9326 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009327 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328}
9329#endif
9330
9331/*
9332 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9333 */
9334 static void
9335ex_tag(eap)
9336 exarg_T *eap;
9337{
9338 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9339}
9340
9341 static void
9342ex_tag_cmd(eap, name)
9343 exarg_T *eap;
9344 char_u *name;
9345{
9346 int cmd;
9347
9348 switch (name[1])
9349 {
9350 case 'j': cmd = DT_JUMP; /* ":tjump" */
9351 break;
9352 case 's': cmd = DT_SELECT; /* ":tselect" */
9353 break;
9354 case 'p': cmd = DT_PREV; /* ":tprevious" */
9355 break;
9356 case 'N': cmd = DT_PREV; /* ":tNext" */
9357 break;
9358 case 'n': cmd = DT_NEXT; /* ":tnext" */
9359 break;
9360 case 'o': cmd = DT_POP; /* ":pop" */
9361 break;
9362 case 'f': /* ":tfirst" */
9363 case 'r': cmd = DT_FIRST; /* ":trewind" */
9364 break;
9365 case 'l': cmd = DT_LAST; /* ":tlast" */
9366 break;
9367 default: /* ":tag" */
9368#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009369 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 {
9371 do_cstag(eap);
9372 return;
9373 }
9374#endif
9375 cmd = DT_TAG;
9376 break;
9377 }
9378
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009379 if (name[0] == 'l')
9380 {
9381#ifndef FEAT_QUICKFIX
9382 ex_ni(eap);
9383 return;
9384#else
9385 cmd = DT_LTAG;
9386#endif
9387 }
9388
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9390 eap->forceit, TRUE);
9391}
9392
9393/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009394 * Check "str" for starting with a special cmdline variable.
9395 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9396 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9397 */
9398 int
9399find_cmdline_var(src, usedlen)
9400 char_u *src;
9401 int *usedlen;
9402{
9403 int len;
9404 int i;
9405 static char *(spec_str[]) = {
9406 "%",
9407#define SPEC_PERC 0
9408 "#",
9409#define SPEC_HASH 1
9410 "<cword>", /* cursor word */
9411#define SPEC_CWORD 2
9412 "<cWORD>", /* cursor WORD */
9413#define SPEC_CCWORD 3
9414 "<cfile>", /* cursor path name */
9415#define SPEC_CFILE 4
9416 "<sfile>", /* ":so" file name */
9417#define SPEC_SFILE 5
9418#ifdef FEAT_AUTOCMD
9419 "<afile>", /* autocommand file name */
9420# define SPEC_AFILE 6
9421 "<abuf>", /* autocommand buffer number */
9422# define SPEC_ABUF 7
9423 "<amatch>", /* autocommand match name */
9424# define SPEC_AMATCH 8
9425#endif
9426#ifdef FEAT_CLIENTSERVER
9427 "<client>"
9428# define SPEC_CLIENT 9
9429#endif
9430 };
9431#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9432
9433 for (i = 0; i < SPEC_COUNT; ++i)
9434 {
9435 len = (int)STRLEN(spec_str[i]);
9436 if (STRNCMP(src, spec_str[i], len) == 0)
9437 {
9438 *usedlen = len;
9439 return i;
9440 }
9441 }
9442 return -1;
9443}
9444
9445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 * Evaluate cmdline variables.
9447 *
9448 * change '%' to curbuf->b_ffname
9449 * '#' to curwin->w_altfile
9450 * '<cword>' to word under the cursor
9451 * '<cWORD>' to WORD under the cursor
9452 * '<cfile>' to path name under the cursor
9453 * '<sfile>' to sourced file name
9454 * '<afile>' to file name for autocommand
9455 * '<abuf>' to buffer number for autocommand
9456 * '<amatch>' to matching name for autocommand
9457 *
9458 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9459 * "" for error without a message) and NULL is returned.
9460 * Returns an allocated string if a valid match was found.
9461 * Returns NULL if no match was found. "usedlen" then still contains the
9462 * number of characters to skip.
9463 */
9464 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009465eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009467 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 int *usedlen; /* characters after src that are used */
9469 linenr_T *lnump; /* line number for :e command, or NULL */
9470 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009471 int *escaped; /* return value has escaped white space (can
9472 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473{
9474 int i;
9475 char_u *s;
9476 char_u *result;
9477 char_u *resultbuf = NULL;
9478 int resultlen;
9479 buf_T *buf;
9480 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9481 int spec_idx;
9482#ifdef FEAT_MODIFY_FNAME
9483 int skip_mod = FALSE;
9484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485
9486#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9487 char_u strbuf[30];
9488#endif
9489
9490 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009491 if (escaped != NULL)
9492 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493
9494 /*
9495 * Check if there is something to do.
9496 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009497 spec_idx = find_cmdline_var(src, usedlen);
9498 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 {
9500 *usedlen = 1;
9501 return NULL;
9502 }
9503
9504 /*
9505 * Skip when preceded with a backslash "\%" and "\#".
9506 * Note: In "\\%" the % is also not recognized!
9507 */
9508 if (src > srcstart && src[-1] == '\\')
9509 {
9510 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009511 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 return NULL;
9513 }
9514
9515 /*
9516 * word or WORD under cursor
9517 */
9518 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9519 {
9520 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9521 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9522 if (resultlen == 0)
9523 {
9524 *errormsg = (char_u *)"";
9525 return NULL;
9526 }
9527 }
9528
9529 /*
9530 * '#': Alternate file name
9531 * '%': Current file name
9532 * File name under the cursor
9533 * File name for autocommand
9534 * and following modifiers
9535 */
9536 else
9537 {
9538 switch (spec_idx)
9539 {
9540 case SPEC_PERC: /* '%': current file */
9541 if (curbuf->b_fname == NULL)
9542 {
9543 result = (char_u *)"";
9544 valid = 0; /* Must have ":p:h" to be valid */
9545 }
9546 else
9547#ifdef RISCOS
9548 /* Always use the full path for RISC OS if possible. */
9549 result = curbuf->b_ffname;
9550 if (result == NULL)
9551 result = curbuf->b_fname;
9552#else
9553 result = curbuf->b_fname;
9554#endif
9555 break;
9556
9557 case SPEC_HASH: /* '#' or "#99": alternate file */
9558 if (src[1] == '#') /* "##": the argument list */
9559 {
9560 result = arg_all();
9561 resultbuf = result;
9562 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009563 if (escaped != NULL)
9564 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565#ifdef FEAT_MODIFY_FNAME
9566 skip_mod = TRUE;
9567#endif
9568 break;
9569 }
9570 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009571 if (*s == '<') /* "#<99" uses v:oldfiles */
9572 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 i = (int)getdigits(&s);
9574 *usedlen = (int)(s - src); /* length of what we expand */
9575
Bram Moolenaard812df62008-11-09 12:46:09 +00009576 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009578 if (*usedlen < 2)
9579 {
9580 /* Should we give an error message for #<text? */
9581 *usedlen = 1;
9582 return NULL;
9583 }
9584#ifdef FEAT_EVAL
9585 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9586 (long)i);
9587 if (result == NULL)
9588 {
9589 *errormsg = (char_u *)"";
9590 return NULL;
9591 }
9592#else
9593 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 }
9597 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009598 {
9599 buf = buflist_findnr(i);
9600 if (buf == NULL)
9601 {
9602 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9603 return NULL;
9604 }
9605 if (lnump != NULL)
9606 *lnump = ECMD_LAST;
9607 if (buf->b_fname == NULL)
9608 {
9609 result = (char_u *)"";
9610 valid = 0; /* Must have ":p:h" to be valid */
9611 }
9612 else
9613 result = buf->b_fname;
9614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 break;
9616
9617#ifdef FEAT_SEARCHPATH
9618 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009619 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 if (result == NULL)
9621 {
9622 *errormsg = (char_u *)"";
9623 return NULL;
9624 }
9625 resultbuf = result; /* remember allocated string */
9626 break;
9627#endif
9628
9629#ifdef FEAT_AUTOCMD
9630 case SPEC_AFILE: /* file name for autocommand */
9631 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009632 if (result != NULL && !autocmd_fname_full)
9633 {
9634 /* Still need to turn the fname into a full path. It is
9635 * postponed to avoid a delay when <afile> is not used. */
9636 autocmd_fname_full = TRUE;
9637 result = FullName_save(autocmd_fname, FALSE);
9638 vim_free(autocmd_fname);
9639 autocmd_fname = result;
9640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 if (result == NULL)
9642 {
9643 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9644 return NULL;
9645 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009646 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 break;
9648
9649 case SPEC_ABUF: /* buffer number for autocommand */
9650 if (autocmd_bufnr <= 0)
9651 {
9652 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9653 return NULL;
9654 }
9655 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9656 result = strbuf;
9657 break;
9658
9659 case SPEC_AMATCH: /* match name for autocommand */
9660 result = autocmd_match;
9661 if (result == NULL)
9662 {
9663 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9664 return NULL;
9665 }
9666 break;
9667
9668#endif
9669 case SPEC_SFILE: /* file name for ":so" command */
9670 result = sourcing_name;
9671 if (result == NULL)
9672 {
9673 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9674 return NULL;
9675 }
9676 break;
9677#if defined(FEAT_CLIENTSERVER)
9678 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009679 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9680 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 result = strbuf;
9682 break;
9683#endif
9684 }
9685
9686 resultlen = (int)STRLEN(result); /* length of new string */
9687 if (src[*usedlen] == '<') /* remove the file name extension */
9688 {
9689 ++*usedlen;
9690#ifdef RISCOS
9691 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9692#else
9693 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9694#endif
9695 resultlen = (int)(s - result);
9696 }
9697#ifdef FEAT_MODIFY_FNAME
9698 else if (!skip_mod)
9699 {
9700 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9701 &resultlen);
9702 if (result == NULL)
9703 {
9704 *errormsg = (char_u *)"";
9705 return NULL;
9706 }
9707 }
9708#endif
9709 }
9710
9711 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9712 {
9713 if (valid != VALID_HEAD + VALID_PATH)
9714 /* xgettext:no-c-format */
9715 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9716 else
9717 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9718 result = NULL;
9719 }
9720 else
9721 result = vim_strnsave(result, resultlen);
9722 vim_free(resultbuf);
9723 return result;
9724}
9725
9726/*
9727 * Concatenate all files in the argument list, separated by spaces, and return
9728 * it in one allocated string.
9729 * Spaces and backslashes in the file names are escaped with a backslash.
9730 * Returns NULL when out of memory.
9731 */
9732 static char_u *
9733arg_all()
9734{
9735 int len;
9736 int idx;
9737 char_u *retval = NULL;
9738 char_u *p;
9739
9740 /*
9741 * Do this loop two times:
9742 * first time: compute the total length
9743 * second time: concatenate the names
9744 */
9745 for (;;)
9746 {
9747 len = 0;
9748 for (idx = 0; idx < ARGCOUNT; ++idx)
9749 {
9750 p = alist_name(&ARGLIST[idx]);
9751 if (p != NULL)
9752 {
9753 if (len > 0)
9754 {
9755 /* insert a space in between names */
9756 if (retval != NULL)
9757 retval[len] = ' ';
9758 ++len;
9759 }
9760 for ( ; *p != NUL; ++p)
9761 {
9762 if (*p == ' ' || *p == '\\')
9763 {
9764 /* insert a backslash */
9765 if (retval != NULL)
9766 retval[len] = '\\';
9767 ++len;
9768 }
9769 if (retval != NULL)
9770 retval[len] = *p;
9771 ++len;
9772 }
9773 }
9774 }
9775
9776 /* second time: break here */
9777 if (retval != NULL)
9778 {
9779 retval[len] = NUL;
9780 break;
9781 }
9782
9783 /* allocate memory */
9784 retval = alloc(len + 1);
9785 if (retval == NULL)
9786 break;
9787 }
9788
9789 return retval;
9790}
9791
9792#if defined(FEAT_AUTOCMD) || defined(PROTO)
9793/*
9794 * Expand the <sfile> string in "arg".
9795 *
9796 * Returns an allocated string, or NULL for any error.
9797 */
9798 char_u *
9799expand_sfile(arg)
9800 char_u *arg;
9801{
9802 char_u *errormsg;
9803 int len;
9804 char_u *result;
9805 char_u *newres;
9806 char_u *repl;
9807 int srclen;
9808 char_u *p;
9809
9810 result = vim_strsave(arg);
9811 if (result == NULL)
9812 return NULL;
9813
9814 for (p = result; *p; )
9815 {
9816 if (STRNCMP(p, "<sfile>", 7) != 0)
9817 ++p;
9818 else
9819 {
9820 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009821 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 if (errormsg != NULL)
9823 {
9824 if (*errormsg)
9825 emsg(errormsg);
9826 vim_free(result);
9827 return NULL;
9828 }
9829 if (repl == NULL) /* no match (cannot happen) */
9830 {
9831 p += srclen;
9832 continue;
9833 }
9834 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9835 newres = alloc(len);
9836 if (newres == NULL)
9837 {
9838 vim_free(repl);
9839 vim_free(result);
9840 return NULL;
9841 }
9842 mch_memmove(newres, result, (size_t)(p - result));
9843 STRCPY(newres + (p - result), repl);
9844 len = (int)STRLEN(newres);
9845 STRCAT(newres, p + srclen);
9846 vim_free(repl);
9847 vim_free(result);
9848 result = newres;
9849 p = newres + len; /* continue after the match */
9850 }
9851 }
9852
9853 return result;
9854}
9855#endif
9856
9857#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009858static int ses_winsizes __ARGS((FILE *fd, int restore_size,
9859 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9861static frame_T *ses_skipframe __ARGS((frame_T *fr));
9862static int ses_do_frame __ARGS((frame_T *fr));
9863static int ses_do_win __ARGS((win_T *wp));
9864static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9865static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9866static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9867
9868/*
9869 * Write openfile commands for the current buffers to an .exrc file.
9870 * Return FAIL on error, OK otherwise.
9871 */
9872 static int
9873makeopens(fd, dirnow)
9874 FILE *fd;
9875 char_u *dirnow; /* Current directory name */
9876{
9877 buf_T *buf;
9878 int only_save_windows = TRUE;
9879 int nr;
9880 int cnr = 1;
9881 int restore_size = TRUE;
9882 win_T *wp;
9883 char_u *sname;
9884 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009885 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009886 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009887 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009888 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009889 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890
9891 if (ssop_flags & SSOP_BUFFERS)
9892 only_save_windows = FALSE; /* Save ALL buffers */
9893
9894 /*
9895 * Begin by setting the this_session variable, and then other
9896 * sessionable variables.
9897 */
9898#ifdef FEAT_EVAL
9899 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9900 return FAIL;
9901 if (ssop_flags & SSOP_GLOBALS)
9902 if (store_session_globals(fd) == FAIL)
9903 return FAIL;
9904#endif
9905
9906 /*
9907 * Close all windows but one.
9908 */
9909 if (put_line(fd, "silent only") == FAIL)
9910 return FAIL;
9911
9912 /*
9913 * Now a :cd command to the session directory or the current directory
9914 */
9915 if (ssop_flags & SSOP_SESDIR)
9916 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009917 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9918 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 return FAIL;
9920 }
9921 else if (ssop_flags & SSOP_CURDIR)
9922 {
9923 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9924 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009925 || fputs("cd ", fd) < 0
9926 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9927 || put_eol(fd) == FAIL)
9928 {
9929 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 vim_free(sname);
9933 }
9934
9935 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009936 * If there is an empty, unnamed buffer we will wipe it out later.
9937 * Remember the buffer number.
9938 */
9939 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9940 return FAIL;
9941 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9942 return FAIL;
9943 if (put_line(fd, "endif") == FAIL)
9944 return FAIL;
9945
9946 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 * Now save the current files, current buffer first.
9948 */
9949 if (put_line(fd, "set shortmess=aoO") == FAIL)
9950 return FAIL;
9951
9952 /* Now put the other buffers into the buffer list */
9953 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9954 {
9955 if (!(only_save_windows && buf->b_nwindows == 0)
9956 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9957 && buf->b_fname != NULL
9958 && buf->b_p_bl)
9959 {
9960 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9961 : buf->b_wininfo->wi_fpos.lnum) < 0
9962 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9963 return FAIL;
9964 }
9965 }
9966
9967 /* the global argument list */
9968 if (ses_arglist(fd, "args", &global_alist.al_ga,
9969 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9970 return FAIL;
9971
9972 if (ssop_flags & SSOP_RESIZE)
9973 {
9974 /* Note: after the restore we still check it worked!*/
9975 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9976 || put_eol(fd) == FAIL)
9977 return FAIL;
9978 }
9979
9980#ifdef FEAT_GUI
9981 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9982 {
9983 int x, y;
9984
9985 if (gui_mch_get_winpos(&x, &y) == OK)
9986 {
9987 /* Note: after the restore we still check it worked!*/
9988 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9989 return FAIL;
9990 }
9991 }
9992#endif
9993
9994 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009995 * May repeat putting Windows for each tab, when "tabpages" is in
9996 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009997 * Don't use goto_tabpage(), it may change directory and trigger
9998 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010000 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010001 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010002 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010004 int need_tabnew = FALSE;
10005
Bram Moolenaar18144c82006-04-12 21:52:12 +000010006 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010008 tabpage_T *tp = find_tabpage(tabnr);
10009
10010 if (tp == NULL)
10011 break; /* done all tab pages */
10012 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010013 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010014 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010015 tab_topframe = topframe;
10016 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010017 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010018 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010019 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010020 tab_topframe = tp->tp_topframe;
10021 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010022 if (tabnr > 1)
10023 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025
Bram Moolenaar18144c82006-04-12 21:52:12 +000010026 /*
10027 * Before creating the window layout, try loading one file. If this
10028 * is aborted we don't end up with a number of useless windows.
10029 * This may have side effects! (e.g., compressed or network file).
10030 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010031 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010032 {
10033 if (ses_do_win(wp)
10034 && wp->w_buffer->b_ffname != NULL
10035 && !wp->w_buffer->b_help
10036#ifdef FEAT_QUICKFIX
10037 && !bt_nofile(wp->w_buffer)
10038#endif
10039 )
10040 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010041 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010042 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10043 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010044 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010045 if (!wp->w_arg_idx_invalid)
10046 edited_win = wp;
10047 break;
10048 }
10049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010051 /* If no file got edited create an empty tab page. */
10052 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10053 return FAIL;
10054
Bram Moolenaar18144c82006-04-12 21:52:12 +000010055 /*
10056 * Save current window layout.
10057 */
10058 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010060 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010062 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10063 return FAIL;
10064 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10065 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066
Bram Moolenaar18144c82006-04-12 21:52:12 +000010067 /*
10068 * Check if window sizes can be restored (no windows omitted).
10069 * Remember the window number of the current window after restoring.
10070 */
10071 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010072 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010073 {
10074 if (ses_do_win(wp))
10075 ++nr;
10076 else
10077 restore_size = FALSE;
10078 if (curwin == wp)
10079 cnr = nr;
10080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081
Bram Moolenaar18144c82006-04-12 21:52:12 +000010082 /* Go to the first window. */
10083 if (put_line(fd, "wincmd t") == FAIL)
10084 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010085
Bram Moolenaar18144c82006-04-12 21:52:12 +000010086 /*
10087 * If more than one window, see if sizes can be restored.
10088 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10089 * resized when moving between windows.
10090 * Do this before restoring the view, so that the topline and the
10091 * cursor can be set. This is done again below.
10092 */
10093 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10094 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010095 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010096 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097
Bram Moolenaar18144c82006-04-12 21:52:12 +000010098 /*
10099 * Restore the view of the window (options, file, cursor, etc.).
10100 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010101 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010102 {
10103 if (!ses_do_win(wp))
10104 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010105 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10106 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010107 return FAIL;
10108 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10109 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010110 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010111 }
10112
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010113 /* The argument index in the first tab page is zero, need to set it in
10114 * each window. For further tab pages it's the window where we do
10115 * "tabedit". */
10116 cur_arg_idx = next_arg_idx;
10117
Bram Moolenaar18144c82006-04-12 21:52:12 +000010118 /*
10119 * Restore cursor to the current window if it's not the first one.
10120 */
10121 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10122 || put_eol(fd) == FAIL))
10123 return FAIL;
10124
10125 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010126 * Restore window sizes again after jumping around in windows, because
10127 * the current window has a minimum size while others may not.
10128 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010129 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010130 return FAIL;
10131
Bram Moolenaar18144c82006-04-12 21:52:12 +000010132 /* Don't continue in another tab page when doing only the current one
10133 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010134 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010135 break;
10136 }
10137
10138 if (ssop_flags & SSOP_TABPAGES)
10139 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010140 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10141 || put_eol(fd) == FAIL)
10142 return FAIL;
10143 }
10144
Bram Moolenaar9c102382006-05-03 21:26:49 +000010145 /*
10146 * Wipe out an empty unnamed buffer we started in.
10147 */
10148 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10149 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010150 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010151 return FAIL;
10152 if (put_line(fd, "endif") == FAIL)
10153 return FAIL;
10154 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10155 return FAIL;
10156
10157 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10158 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10159 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10160 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161
10162 /*
10163 * Lastly, execute the x.vim file if it exists.
10164 */
10165 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10166 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010167 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 || put_line(fd, "endif") == FAIL)
10169 return FAIL;
10170
10171 return OK;
10172}
10173
10174 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010175ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 FILE *fd;
10177 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010178 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179{
10180 int n = 0;
10181 win_T *wp;
10182
10183 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10184 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010185 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 {
10187 if (!ses_do_win(wp))
10188 continue;
10189 ++n;
10190
10191 /* restore height when not full height */
10192 if (wp->w_height + wp->w_status_height < topframe->fr_height
10193 && (fprintf(fd,
10194 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10195 n, (long)wp->w_height, Rows / 2, Rows) < 0
10196 || put_eol(fd) == FAIL))
10197 return FAIL;
10198
10199 /* restore width when not full width */
10200 if (wp->w_width < Columns && (fprintf(fd,
10201 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10202 n, (long)wp->w_width, Columns / 2, Columns) < 0
10203 || put_eol(fd) == FAIL))
10204 return FAIL;
10205 }
10206 }
10207 else
10208 {
10209 /* Just equalise window sizes */
10210 if (put_line(fd, "wincmd =") == FAIL)
10211 return FAIL;
10212 }
10213 return OK;
10214}
10215
10216/*
10217 * Write commands to "fd" to recursively create windows for frame "fr",
10218 * horizontally and vertically split.
10219 * After the commands the last window in the frame is the current window.
10220 * Returns FAIL when writing the commands to "fd" fails.
10221 */
10222 static int
10223ses_win_rec(fd, fr)
10224 FILE *fd;
10225 frame_T *fr;
10226{
10227 frame_T *frc;
10228 int count = 0;
10229
10230 if (fr->fr_layout != FR_LEAF)
10231 {
10232 /* Find first frame that's not skipped and then create a window for
10233 * each following one (first frame is already there). */
10234 frc = ses_skipframe(fr->fr_child);
10235 if (frc != NULL)
10236 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10237 {
10238 /* Make window as big as possible so that we have lots of room
10239 * to split. */
10240 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10241 || put_line(fd, fr->fr_layout == FR_COL
10242 ? "split" : "vsplit") == FAIL)
10243 return FAIL;
10244 ++count;
10245 }
10246
10247 /* Go back to the first window. */
10248 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10249 ? "%dwincmd k" : "%dwincmd h", count) < 0
10250 || put_eol(fd) == FAIL))
10251 return FAIL;
10252
10253 /* Recursively create frames/windows in each window of this column or
10254 * row. */
10255 frc = ses_skipframe(fr->fr_child);
10256 while (frc != NULL)
10257 {
10258 ses_win_rec(fd, frc);
10259 frc = ses_skipframe(frc->fr_next);
10260 /* Go to next window. */
10261 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10262 return FAIL;
10263 }
10264 }
10265 return OK;
10266}
10267
10268/*
10269 * Skip frames that don't contain windows we want to save in the Session.
10270 * Returns NULL when there none.
10271 */
10272 static frame_T *
10273ses_skipframe(fr)
10274 frame_T *fr;
10275{
10276 frame_T *frc;
10277
10278 for (frc = fr; frc != NULL; frc = frc->fr_next)
10279 if (ses_do_frame(frc))
10280 break;
10281 return frc;
10282}
10283
10284/*
10285 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10286 * the Session.
10287 */
10288 static int
10289ses_do_frame(fr)
10290 frame_T *fr;
10291{
10292 frame_T *frc;
10293
10294 if (fr->fr_layout == FR_LEAF)
10295 return ses_do_win(fr->fr_win);
10296 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10297 if (ses_do_frame(frc))
10298 return TRUE;
10299 return FALSE;
10300}
10301
10302/*
10303 * Return non-zero if window "wp" is to be stored in the Session.
10304 */
10305 static int
10306ses_do_win(wp)
10307 win_T *wp;
10308{
10309 if (wp->w_buffer->b_fname == NULL
10310#ifdef FEAT_QUICKFIX
10311 /* When 'buftype' is "nofile" can't restore the window contents. */
10312 || bt_nofile(wp->w_buffer)
10313#endif
10314 )
10315 return (ssop_flags & SSOP_BLANK);
10316 if (wp->w_buffer->b_help)
10317 return (ssop_flags & SSOP_HELP);
10318 return TRUE;
10319}
10320
10321/*
10322 * Write commands to "fd" to restore the view of a window.
10323 * Caller must make sure 'scrolloff' is zero.
10324 */
10325 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010326put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 FILE *fd;
10328 win_T *wp;
10329 int add_edit; /* add ":edit" command to view */
10330 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010331 int current_arg_idx; /* current argument index of the window, use
10332 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333{
10334 win_T *save_curwin;
10335 int f;
10336 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010337 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338
10339 /* Always restore cursor position for ":mksession". For ":mkview" only
10340 * when 'viewoptions' contains "cursor". */
10341 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10342
10343 /*
10344 * Local argument list.
10345 */
10346 if (wp->w_alist == &global_alist)
10347 {
10348 if (put_line(fd, "argglobal") == FAIL)
10349 return FAIL;
10350 }
10351 else
10352 {
10353 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10354 flagp == &vop_flags
10355 || !(*flagp & SSOP_CURDIR)
10356 || wp->w_localdir != NULL, flagp) == FAIL)
10357 return FAIL;
10358 }
10359
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010360 /* Only when part of a session: restore the argument index. Some
10361 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010362 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx <= WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010363 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010365 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 || put_eol(fd) == FAIL)
10367 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010368 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 }
10370
10371 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010372 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 {
10374 /*
10375 * Load the file.
10376 */
10377 if (wp->w_buffer->b_ffname != NULL
10378#ifdef FEAT_QUICKFIX
10379 && !bt_nofile(wp->w_buffer)
10380#endif
10381 )
10382 {
10383 /*
10384 * Editing a file in this buffer: use ":edit file".
10385 * This may have side effects! (e.g., compressed or network file).
10386 */
10387 if (fputs("edit ", fd) < 0
10388 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10389 return FAIL;
10390 }
10391 else
10392 {
10393 /* No file in this buffer, just make it empty. */
10394 if (put_line(fd, "enew") == FAIL)
10395 return FAIL;
10396#ifdef FEAT_QUICKFIX
10397 if (wp->w_buffer->b_ffname != NULL)
10398 {
10399 /* The buffer does have a name, but it's not a file name. */
10400 if (fputs("file ", fd) < 0
10401 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10402 return FAIL;
10403 }
10404#endif
10405 do_cursor = FALSE;
10406 }
10407 }
10408
10409 /*
10410 * Local mappings and abbreviations.
10411 */
10412 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10413 && makemap(fd, wp->w_buffer) == FAIL)
10414 return FAIL;
10415
10416 /*
10417 * Local options. Need to go to the window temporarily.
10418 * Store only local values when using ":mkview" and when ":mksession" is
10419 * used and 'sessionoptions' doesn't include "options".
10420 * Some folding options are always stored when "folds" is included,
10421 * otherwise the folds would not be restored correctly.
10422 */
10423 save_curwin = curwin;
10424 curwin = wp;
10425 curbuf = curwin->w_buffer;
10426 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10427 f = makeset(fd, OPT_LOCAL,
10428 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10429#ifdef FEAT_FOLDING
10430 else if (*flagp & SSOP_FOLDS)
10431 f = makefoldset(fd);
10432#endif
10433 else
10434 f = OK;
10435 curwin = save_curwin;
10436 curbuf = curwin->w_buffer;
10437 if (f == FAIL)
10438 return FAIL;
10439
10440#ifdef FEAT_FOLDING
10441 /*
10442 * Save Folds when 'buftype' is empty and for help files.
10443 */
10444 if ((*flagp & SSOP_FOLDS)
10445 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010446# ifdef FEAT_QUICKFIX
10447 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10448# endif
10449 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 {
10451 if (put_folds(fd, wp) == FAIL)
10452 return FAIL;
10453 }
10454#endif
10455
10456 /*
10457 * Set the cursor after creating folds, since that moves the cursor.
10458 */
10459 if (do_cursor)
10460 {
10461
10462 /* Restore the cursor line in the file and relatively in the
10463 * window. Don't use "G", it changes the jumplist. */
10464 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10465 (long)wp->w_cursor.lnum,
10466 (long)(wp->w_cursor.lnum - wp->w_topline),
10467 (long)wp->w_height / 2, (long)wp->w_height) < 0
10468 || put_eol(fd) == FAIL
10469 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10470 || put_line(fd, "exe s:l") == FAIL
10471 || put_line(fd, "normal! zt") == FAIL
10472 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10473 || put_eol(fd) == FAIL)
10474 return FAIL;
10475 /* Restore the cursor column and left offset when not wrapping. */
10476 if (wp->w_cursor.col == 0)
10477 {
10478 if (put_line(fd, "normal! 0") == FAIL)
10479 return FAIL;
10480 }
10481 else
10482 {
10483 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10484 {
10485 if (fprintf(fd,
10486 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10487 (long)wp->w_cursor.col,
10488 (long)(wp->w_cursor.col - wp->w_leftcol),
10489 (long)wp->w_width / 2, (long)wp->w_width) < 0
10490 || put_eol(fd) == FAIL
10491 || put_line(fd, "if s:c > 0") == FAIL
10492 || fprintf(fd,
10493 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10494 (long)wp->w_cursor.col) < 0
10495 || put_eol(fd) == FAIL
10496 || put_line(fd, "else") == FAIL
10497 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10498 || put_eol(fd) == FAIL
10499 || put_line(fd, "endif") == FAIL)
10500 return FAIL;
10501 }
10502 else
10503 {
10504 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10505 || put_eol(fd) == FAIL)
10506 return FAIL;
10507 }
10508 }
10509 }
10510
10511 /*
10512 * Local directory.
10513 */
10514 if (wp->w_localdir != NULL)
10515 {
10516 if (fputs("lcd ", fd) < 0
10517 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10518 || put_eol(fd) == FAIL)
10519 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010520 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 }
10522
10523 return OK;
10524}
10525
10526/*
10527 * Write an argument list to the session file.
10528 * Returns FAIL if writing fails.
10529 */
10530 static int
10531ses_arglist(fd, cmd, gap, fullname, flagp)
10532 FILE *fd;
10533 char *cmd;
10534 garray_T *gap;
10535 int fullname; /* TRUE: use full path name */
10536 unsigned *flagp;
10537{
10538 int i;
10539 char_u buf[MAXPATHL];
10540 char_u *s;
10541
10542 if (gap->ga_len == 0)
10543 return put_line(fd, "silent! argdel *");
10544 if (fputs(cmd, fd) < 0)
10545 return FAIL;
10546 for (i = 0; i < gap->ga_len; ++i)
10547 {
10548 /* NULL file names are skipped (only happens when out of memory). */
10549 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10550 if (s != NULL)
10551 {
10552 if (fullname)
10553 {
10554 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10555 s = buf;
10556 }
10557 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10558 return FAIL;
10559 }
10560 }
10561 return put_eol(fd);
10562}
10563
10564/*
10565 * Write a buffer name to the session file.
10566 * Also ends the line.
10567 * Returns FAIL if writing fails.
10568 */
10569 static int
10570ses_fname(fd, buf, flagp)
10571 FILE *fd;
10572 buf_T *buf;
10573 unsigned *flagp;
10574{
10575 char_u *name;
10576
10577 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010578 * the session file will be sourced.
10579 * Don't do this for ":mkview", we don't know the current directory.
10580 * Don't do this after ":lcd", we don't keep track of what the current
10581 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 if (buf->b_sfname != NULL
10583 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010584 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
10585 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 name = buf->b_sfname;
10587 else
10588 name = buf->b_ffname;
10589 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10590 return FAIL;
10591 return OK;
10592}
10593
10594/*
10595 * Write a file name to the session file.
10596 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10597 * characters.
10598 * Returns FAIL if writing fails.
10599 */
10600 static int
10601ses_put_fname(fd, name, flagp)
10602 FILE *fd;
10603 char_u *name;
10604 unsigned *flagp;
10605{
10606 char_u *sname;
10607 int retval = OK;
10608 int c;
10609
10610 sname = home_replace_save(NULL, name);
10611 if (sname != NULL)
10612 name = sname;
10613 while (*name != NUL)
10614 {
10615#ifdef FEAT_MBYTE
10616 {
10617 int l;
10618
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010619 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 {
10621 /* copy a multibyte char */
10622 while (--l >= 0)
10623 {
10624 if (putc(*name, fd) != *name)
10625 retval = FAIL;
10626 ++name;
10627 }
10628 continue;
10629 }
10630 }
10631#endif
10632 c = *name++;
10633 if (c == '\\' && (*flagp & SSOP_SLASH))
10634 /* change a backslash to a forward slash */
10635 c = '/';
10636 else if ((vim_strchr(escape_chars, c) != NULL
10637#ifdef BACKSLASH_IN_FILENAME
10638 && c != '\\'
10639#endif
10640 ) || c == '#' || c == '%')
10641 {
10642 /* escape a special character with a backslash */
10643 if (putc('\\', fd) != '\\')
10644 retval = FAIL;
10645 }
10646 if (putc(c, fd) != c)
10647 retval = FAIL;
10648 }
10649 vim_free(sname);
10650 return retval;
10651}
10652
10653/*
10654 * ":loadview [nr]"
10655 */
10656 static void
10657ex_loadview(eap)
10658 exarg_T *eap;
10659{
10660 char_u *fname;
10661
10662 fname = get_view_file(*eap->arg);
10663 if (fname != NULL)
10664 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010665 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 vim_free(fname);
10667 }
10668}
10669
10670/*
10671 * Get the name of the view file for the current buffer.
10672 */
10673 static char_u *
10674get_view_file(c)
10675 int c;
10676{
10677 int len = 0;
10678 char_u *p, *s;
10679 char_u *retval;
10680 char_u *sname;
10681
10682 if (curbuf->b_ffname == NULL)
10683 {
10684 EMSG(_(e_noname));
10685 return NULL;
10686 }
10687 sname = home_replace_save(NULL, curbuf->b_ffname);
10688 if (sname == NULL)
10689 return NULL;
10690
10691 /*
10692 * We want a file name without separators, because we're not going to make
10693 * a directory.
10694 * "normal" path separator -> "=+"
10695 * "=" -> "=="
10696 * ":" path separator -> "=-"
10697 */
10698 for (p = sname; *p; ++p)
10699 if (*p == '=' || vim_ispathsep(*p))
10700 ++len;
10701 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10702 if (retval != NULL)
10703 {
10704 STRCPY(retval, p_vdir);
10705 add_pathsep(retval);
10706 s = retval + STRLEN(retval);
10707 for (p = sname; *p; ++p)
10708 {
10709 if (*p == '=')
10710 {
10711 *s++ = '=';
10712 *s++ = '=';
10713 }
10714 else if (vim_ispathsep(*p))
10715 {
10716 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010717#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 || defined(VMS)
10719 if (*p == ':')
10720 *s++ = '-';
10721 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010723 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 }
10725 else
10726 *s++ = *p;
10727 }
10728 *s++ = '=';
10729 *s++ = c;
10730 STRCPY(s, ".vim");
10731 }
10732
10733 vim_free(sname);
10734 return retval;
10735}
10736
10737#endif /* FEAT_SESSION */
10738
10739/*
10740 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10741 * Return FAIL for a write error.
10742 */
10743 int
10744put_eol(fd)
10745 FILE *fd;
10746{
10747 if (
10748#ifdef USE_CRNL
10749 (
10750# ifdef MKSESSION_NL
10751 !mksession_nl &&
10752# endif
10753 (putc('\r', fd) < 0)) ||
10754#endif
10755 (putc('\n', fd) < 0))
10756 return FAIL;
10757 return OK;
10758}
10759
10760/*
10761 * Write a line to "fd".
10762 * Return FAIL for a write error.
10763 */
10764 int
10765put_line(fd, s)
10766 FILE *fd;
10767 char *s;
10768{
10769 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10770 return FAIL;
10771 return OK;
10772}
10773
10774#ifdef FEAT_VIMINFO
10775/*
10776 * ":rviminfo" and ":wviminfo".
10777 */
10778 static void
10779ex_viminfo(eap)
10780 exarg_T *eap;
10781{
10782 char_u *save_viminfo;
10783
10784 save_viminfo = p_viminfo;
10785 if (*p_viminfo == NUL)
10786 p_viminfo = (char_u *)"'100";
10787 if (eap->cmdidx == CMD_rviminfo)
10788 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010789 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10790 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 EMSG(_("E195: Cannot open viminfo file for reading"));
10792 }
10793 else
10794 write_viminfo(eap->arg, eap->forceit);
10795 p_viminfo = save_viminfo;
10796}
10797#endif
10798
10799#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010800/*
10801 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010802 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 void
10805dialog_msg(buff, format, fname)
10806 char_u *buff;
10807 char *format;
10808 char_u *fname;
10809{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 if (fname == NULL)
10811 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010812 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813}
10814#endif
10815
10816/*
10817 * ":behave {mswin,xterm}"
10818 */
10819 static void
10820ex_behave(eap)
10821 exarg_T *eap;
10822{
10823 if (STRCMP(eap->arg, "mswin") == 0)
10824 {
10825 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10826 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10827 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10828 set_option_value((char_u *)"keymodel", 0L,
10829 (char_u *)"startsel,stopsel", 0);
10830 }
10831 else if (STRCMP(eap->arg, "xterm") == 0)
10832 {
10833 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10834 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10835 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10836 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10837 }
10838 else
10839 EMSG2(_(e_invarg2), eap->arg);
10840}
10841
10842#ifdef FEAT_AUTOCMD
10843static int filetype_detect = FALSE;
10844static int filetype_plugin = FALSE;
10845static int filetype_indent = FALSE;
10846
10847/*
10848 * ":filetype [plugin] [indent] {on,off,detect}"
10849 * on: Load the filetype.vim file to install autocommands for file types.
10850 * off: Load the ftoff.vim file to remove all autocommands for file types.
10851 * plugin on: load filetype.vim and ftplugin.vim
10852 * plugin off: load ftplugof.vim
10853 * indent on: load filetype.vim and indent.vim
10854 * indent off: load indoff.vim
10855 */
10856 static void
10857ex_filetype(eap)
10858 exarg_T *eap;
10859{
10860 char_u *arg = eap->arg;
10861 int plugin = FALSE;
10862 int indent = FALSE;
10863
10864 if (*eap->arg == NUL)
10865 {
10866 /* Print current status. */
10867 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10868 filetype_detect ? "ON" : "OFF",
10869 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10870 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10871 return;
10872 }
10873
10874 /* Accept "plugin" and "indent" in any order. */
10875 for (;;)
10876 {
10877 if (STRNCMP(arg, "plugin", 6) == 0)
10878 {
10879 plugin = TRUE;
10880 arg = skipwhite(arg + 6);
10881 continue;
10882 }
10883 if (STRNCMP(arg, "indent", 6) == 0)
10884 {
10885 indent = TRUE;
10886 arg = skipwhite(arg + 6);
10887 continue;
10888 }
10889 break;
10890 }
10891 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10892 {
10893 if (*arg == 'o' || !filetype_detect)
10894 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010895 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 filetype_detect = TRUE;
10897 if (plugin)
10898 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010899 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 filetype_plugin = TRUE;
10901 }
10902 if (indent)
10903 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010904 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 filetype_indent = TRUE;
10906 }
10907 }
10908 if (*arg == 'd')
10909 {
10910 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010911 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 }
10913 }
10914 else if (STRCMP(arg, "off") == 0)
10915 {
10916 if (plugin || indent)
10917 {
10918 if (plugin)
10919 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010920 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 filetype_plugin = FALSE;
10922 }
10923 if (indent)
10924 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010925 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 filetype_indent = FALSE;
10927 }
10928 }
10929 else
10930 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010931 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 filetype_detect = FALSE;
10933 }
10934 }
10935 else
10936 EMSG2(_(e_invarg2), arg);
10937}
10938
10939/*
10940 * ":setfiletype {name}"
10941 */
10942 static void
10943ex_setfiletype(eap)
10944 exarg_T *eap;
10945{
10946 if (!did_filetype)
10947 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10948}
10949#endif
10950
10951/*ARGSUSED*/
10952 static void
10953ex_digraphs(eap)
10954 exarg_T *eap;
10955{
10956#ifdef FEAT_DIGRAPHS
10957 if (*eap->arg != NUL)
10958 putdigraph(eap->arg);
10959 else
10960 listdigraphs();
10961#else
10962 EMSG(_("E196: No digraphs in this version"));
10963#endif
10964}
10965
10966 static void
10967ex_set(eap)
10968 exarg_T *eap;
10969{
10970 int flags = 0;
10971
10972 if (eap->cmdidx == CMD_setlocal)
10973 flags = OPT_LOCAL;
10974 else if (eap->cmdidx == CMD_setglobal)
10975 flags = OPT_GLOBAL;
10976#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10977 if (cmdmod.browse && flags == 0)
10978 ex_options(eap);
10979 else
10980#endif
10981 (void)do_set(eap->arg, flags);
10982}
10983
10984#ifdef FEAT_SEARCH_EXTRA
10985/*
10986 * ":nohlsearch"
10987 */
10988/*ARGSUSED*/
10989 static void
10990ex_nohlsearch(eap)
10991 exarg_T *eap;
10992{
10993 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010994 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995}
10996
10997/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010998 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 * Sets nextcmd to the start of the next command, if any. Also called when
11000 * skipping commands to find the next command.
11001 */
11002 static void
11003ex_match(eap)
11004 exarg_T *eap;
11005{
11006 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011007 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 char_u *end;
11009 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011010 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011011
11012 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011013 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011014 else
11015 {
11016 EMSG(e_invcmd);
11017 return;
11018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019
11020 /* First clear any old pattern. */
11021 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011022 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023
11024 if (ends_excmd(*eap->arg))
11025 end = eap->arg;
11026 else if ((STRNICMP(eap->arg, "none", 4) == 0
11027 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11028 end = eap->arg + 4;
11029 else
11030 {
11031 p = skiptowhite(eap->arg);
11032 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011033 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 p = skipwhite(p);
11035 if (*p == NUL)
11036 {
11037 /* There must be two arguments. */
11038 EMSG2(_(e_invarg2), eap->arg);
11039 return;
11040 }
11041 end = skip_regexp(p + 1, *p, TRUE, NULL);
11042 if (!eap->skip)
11043 {
11044 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11045 {
11046 eap->errmsg = e_trailing;
11047 return;
11048 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011049 if (*end != *p)
11050 {
11051 EMSG2(_(e_invarg2), p);
11052 return;
11053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054
11055 c = *end;
11056 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011057 match_add(curwin, g, p + 1, 10, id);
11058 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011059 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 }
11061 }
11062 eap->nextcmd = find_nextcmd(end);
11063}
11064#endif
11065
11066#ifdef FEAT_CRYPT
11067/*
11068 * ":X": Get crypt key
11069 */
11070/*ARGSUSED*/
11071 static void
11072ex_X(eap)
11073 exarg_T *eap;
11074{
11075 (void)get_crypt_key(TRUE, TRUE);
11076}
11077#endif
11078
11079#ifdef FEAT_FOLDING
11080 static void
11081ex_fold(eap)
11082 exarg_T *eap;
11083{
11084 if (foldManualAllowed(TRUE))
11085 foldCreate(eap->line1, eap->line2);
11086}
11087
11088 static void
11089ex_foldopen(eap)
11090 exarg_T *eap;
11091{
11092 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11093 eap->forceit, FALSE);
11094}
11095
11096 static void
11097ex_folddo(eap)
11098 exarg_T *eap;
11099{
11100 linenr_T lnum;
11101
11102 /* First set the marks for all lines closed/open. */
11103 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11104 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11105 ml_setmarked(lnum);
11106
11107 /* Execute the command on the marked lines. */
11108 global_exe(eap->arg);
11109 ml_clearmarked(); /* clear rest of the marks */
11110}
11111#endif