blob: f5477715551d6b73c63d1a7459f2f88e5a79ba2f [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
16#ifdef HAVE_FCNTL_H
17# include <fcntl.h> /* for chdir() */
18#endif
19
20static int quitmore = 0;
21static int ex_pressedreturn = FALSE;
22#ifndef FEAT_PRINTER
23# define ex_hardcopy ex_ni
24#endif
25
26#ifdef FEAT_USR_CMDS
27typedef struct ucmd
28{
29 char_u *uc_name; /* The command name */
30 long_u uc_argt; /* The argument type */
31 char_u *uc_rep; /* The command's replacement string */
32 long uc_def; /* The default value for a range/count */
33 scid_T uc_scriptID; /* SID where the command was defined */
34 int uc_compl; /* completion type */
35# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
36 char_u *uc_compl_arg; /* completion argument if any */
37# endif
38} ucmd_T;
39
40#define UC_BUFFER 1 /* -buffer: local to current buffer */
41
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000042static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
44#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
45#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
46
47static void do_ucmd __ARGS((exarg_T *eap));
48static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000049static void ex_delcommand __ARGS((exarg_T *eap));
50# ifdef FEAT_CMDL_COMPL
51static char_u *get_user_command_name __ARGS((int idx));
52# endif
53
54#else
55# define ex_command ex_ni
56# define ex_comclear ex_ni
57# define ex_delcommand ex_ni
58#endif
59
60#ifdef FEAT_EVAL
61static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*getline)(int, void *, int), void *cookie));
62#else
63static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*getline)(int, void *, int), void *cookie));
64static int if_level = 0; /* depth in :if */
65#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static char_u *find_command __ARGS((exarg_T *eap, int *full));
67
68static void ex_abbreviate __ARGS((exarg_T *eap));
69static void ex_map __ARGS((exarg_T *eap));
70static void ex_unmap __ARGS((exarg_T *eap));
71static void ex_mapclear __ARGS((exarg_T *eap));
72static void ex_abclear __ARGS((exarg_T *eap));
73#ifndef FEAT_MENU
74# define ex_emenu ex_ni
75# define ex_menu ex_ni
76# define ex_menutranslate ex_ni
77#endif
78#ifdef FEAT_AUTOCMD
79static void ex_autocmd __ARGS((exarg_T *eap));
80static void ex_doautocmd __ARGS((exarg_T *eap));
81#else
82# define ex_autocmd ex_ni
83# define ex_doautocmd ex_ni
84# define ex_doautoall ex_ni
85#endif
86#ifdef FEAT_LISTCMDS
87static void ex_bunload __ARGS((exarg_T *eap));
88static void ex_buffer __ARGS((exarg_T *eap));
89static void ex_bmodified __ARGS((exarg_T *eap));
90static void ex_bnext __ARGS((exarg_T *eap));
91static void ex_bprevious __ARGS((exarg_T *eap));
92static void ex_brewind __ARGS((exarg_T *eap));
93static void ex_blast __ARGS((exarg_T *eap));
94#else
95# define ex_bunload ex_ni
96# define ex_buffer ex_ni
97# define ex_bmodified ex_ni
98# define ex_bnext ex_ni
99# define ex_bprevious ex_ni
100# define ex_brewind ex_ni
101# define ex_blast ex_ni
102# define buflist_list ex_ni
103# define ex_checktime ex_ni
104#endif
105#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
106# define ex_buffer_all ex_ni
107#endif
108static char_u *getargcmd __ARGS((char_u **));
109static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
110static int getargopt __ARGS((exarg_T *eap));
111#ifndef FEAT_QUICKFIX
112# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000113# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114# define ex_cc ex_ni
115# define ex_cnext ex_ni
116# define ex_cfile ex_ni
117# define qf_list ex_ni
118# define qf_age ex_ni
119# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000120# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#endif
122#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
123# define ex_cclose ex_ni
124# define ex_copen ex_ni
125# define ex_cwindow ex_ni
126#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000127#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
128# define ex_cexpr ex_ni
129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
131static int check_more __ARGS((int, int));
132static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000133static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000135 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136static void ex_script_ni __ARGS((exarg_T *eap));
137#endif
138static char_u *invalid_range __ARGS((exarg_T *eap));
139static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000140#ifdef FEAT_QUICKFIX
141static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
144static void ex_highlight __ARGS((exarg_T *eap));
145static void ex_colorscheme __ARGS((exarg_T *eap));
146static void ex_quit __ARGS((exarg_T *eap));
147static void ex_cquit __ARGS((exarg_T *eap));
148static void ex_quit_all __ARGS((exarg_T *eap));
149#ifdef FEAT_WINDOWS
150static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000151static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static void ex_resize __ARGS((exarg_T *eap));
154static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000155static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000156static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000157static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000158static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#else
161# define ex_close ex_ni
162# define ex_only ex_ni
163# define ex_all ex_ni
164# define ex_resize ex_ni
165# define ex_splitview ex_ni
166# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000167# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000168# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000169# define ex_tabs ex_ni
170# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000171# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
174static void ex_pclose __ARGS((exarg_T *eap));
175static void ex_ptag __ARGS((exarg_T *eap));
176static void ex_pedit __ARGS((exarg_T *eap));
177#else
178# define ex_pclose ex_ni
179# define ex_ptag ex_ni
180# define ex_pedit ex_ni
181#endif
182static void ex_hide __ARGS((exarg_T *eap));
183static void ex_stop __ARGS((exarg_T *eap));
184static void ex_exit __ARGS((exarg_T *eap));
185static void ex_print __ARGS((exarg_T *eap));
186#ifdef FEAT_BYTEOFF
187static void ex_goto __ARGS((exarg_T *eap));
188#else
189# define ex_goto ex_ni
190#endif
191static void ex_shell __ARGS((exarg_T *eap));
192static void ex_preserve __ARGS((exarg_T *eap));
193static void ex_recover __ARGS((exarg_T *eap));
194#ifndef FEAT_LISTCMDS
195# define ex_argedit ex_ni
196# define ex_argadd ex_ni
197# define ex_argdelete ex_ni
198# define ex_listdo ex_ni
199#endif
200static void ex_mode __ARGS((exarg_T *eap));
201static void ex_wrongmodifier __ARGS((exarg_T *eap));
202static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000203static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204static void ex_edit __ARGS((exarg_T *eap));
205#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
206# define ex_drop ex_ni
207#endif
208#ifndef FEAT_GUI
209# define ex_gui ex_nogui
210static void ex_nogui __ARGS((exarg_T *eap));
211#endif
212#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
213static void ex_tearoff __ARGS((exarg_T *eap));
214#else
215# define ex_tearoff ex_ni
216#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000217#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218static void ex_popup __ARGS((exarg_T *eap));
219#else
220# define ex_popup ex_ni
221#endif
222#ifndef FEAT_GUI_MSWIN
223# define ex_simalt ex_ni
224#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000225#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226# define gui_mch_find_dialog ex_ni
227# define gui_mch_replace_dialog ex_ni
228#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000229#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define ex_helpfind ex_ni
231#endif
232#ifndef FEAT_CSCOPE
233# define do_cscope ex_ni
234# define do_scscope ex_ni
235# define do_cstag ex_ni
236#endif
237#ifndef FEAT_SYN_HL
238# define ex_syntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000239#endif
240#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000241# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000242# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000243# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000244# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000245# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000246#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000247#ifndef FEAT_MZSCHEME
248# define ex_mzscheme ex_script_ni
249# define ex_mzfile ex_ni
250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifndef FEAT_PERL
252# define ex_perl ex_script_ni
253# define ex_perldo ex_ni
254#endif
255#ifndef FEAT_PYTHON
256# define ex_python ex_script_ni
257# define ex_pyfile ex_ni
258#endif
259#ifndef FEAT_TCL
260# define ex_tcl ex_script_ni
261# define ex_tcldo ex_ni
262# define ex_tclfile ex_ni
263#endif
264#ifndef FEAT_RUBY
265# define ex_ruby ex_script_ni
266# define ex_rubydo ex_ni
267# define ex_rubyfile ex_ni
268#endif
269#ifndef FEAT_SNIFF
270# define ex_sniff ex_ni
271#endif
272#ifndef FEAT_KEYMAP
273# define ex_loadkeymap ex_ni
274#endif
275static void ex_swapname __ARGS((exarg_T *eap));
276static void ex_syncbind __ARGS((exarg_T *eap));
277static void ex_read __ARGS((exarg_T *eap));
278static void ex_cd __ARGS((exarg_T *eap));
279static void ex_pwd __ARGS((exarg_T *eap));
280static void ex_equal __ARGS((exarg_T *eap));
281static void ex_sleep __ARGS((exarg_T *eap));
282static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
283static void ex_winsize __ARGS((exarg_T *eap));
284#ifdef FEAT_WINDOWS
285static void ex_wincmd __ARGS((exarg_T *eap));
286#else
287# define ex_wincmd ex_ni
288#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000289#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290static void ex_winpos __ARGS((exarg_T *eap));
291#else
292# define ex_winpos ex_ni
293#endif
294static void ex_operators __ARGS((exarg_T *eap));
295static void ex_put __ARGS((exarg_T *eap));
296static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000297static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298static void ex_submagic __ARGS((exarg_T *eap));
299static void ex_join __ARGS((exarg_T *eap));
300static void ex_at __ARGS((exarg_T *eap));
301static void ex_bang __ARGS((exarg_T *eap));
302static void ex_undo __ARGS((exarg_T *eap));
303static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000304static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305static void ex_redir __ARGS((exarg_T *eap));
306static void ex_redraw __ARGS((exarg_T *eap));
307static void ex_redrawstatus __ARGS((exarg_T *eap));
308static void close_redir __ARGS((void));
309static void ex_mkrc __ARGS((exarg_T *eap));
310static void ex_mark __ARGS((exarg_T *eap));
311#ifdef FEAT_USR_CMDS
312static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000313static 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 +0000314#endif
315#ifdef FEAT_EX_EXTRA
316static void ex_normal __ARGS((exarg_T *eap));
317static void ex_startinsert __ARGS((exarg_T *eap));
318static void ex_stopinsert __ARGS((exarg_T *eap));
319#else
320# define ex_normal ex_ni
321# define ex_align ex_ni
322# define ex_retab ex_ni
323# define ex_startinsert ex_ni
324# define ex_stopinsert ex_ni
325# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000326# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#endif
328#ifdef FEAT_FIND_ID
329static void ex_checkpath __ARGS((exarg_T *eap));
330static void ex_findpat __ARGS((exarg_T *eap));
331#else
332# define ex_findpat ex_ni
333# define ex_checkpath ex_ni
334#endif
335#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
336static void ex_psearch __ARGS((exarg_T *eap));
337#else
338# define ex_psearch ex_ni
339#endif
340static void ex_tag __ARGS((exarg_T *eap));
341static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
342#ifndef FEAT_EVAL
343# define ex_scriptnames ex_ni
344# define ex_finish ex_ni
345# define ex_echo ex_ni
346# define ex_echohl ex_ni
347# define ex_execute ex_ni
348# define ex_call ex_ni
349# define ex_if ex_ni
350# define ex_endif ex_ni
351# define ex_else ex_ni
352# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000353# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354# define ex_continue ex_ni
355# define ex_break ex_ni
356# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000357# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358# define ex_throw ex_ni
359# define ex_try ex_ni
360# define ex_catch ex_ni
361# define ex_finally ex_ni
362# define ex_endtry ex_ni
363# define ex_endfunction ex_ni
364# define ex_let ex_ni
365# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000366# define ex_lockvar ex_ni
367# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368# define ex_function ex_ni
369# define ex_delfunction ex_ni
370# define ex_return ex_ni
371#endif
372static char_u *arg_all __ARGS((void));
373#ifdef FEAT_SESSION
374static int makeopens __ARGS((FILE *fd, char_u *dirnow));
375static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp));
376static void ex_loadview __ARGS((exarg_T *eap));
377static char_u *get_view_file __ARGS((int c));
378#else
379# define ex_loadview ex_ni
380#endif
381#ifndef FEAT_EVAL
382# define ex_compiler ex_ni
383#endif
384#ifdef FEAT_VIMINFO
385static void ex_viminfo __ARGS((exarg_T *eap));
386#else
387# define ex_viminfo ex_ni
388#endif
389static void ex_behave __ARGS((exarg_T *eap));
390#ifdef FEAT_AUTOCMD
391static void ex_filetype __ARGS((exarg_T *eap));
392static void ex_setfiletype __ARGS((exarg_T *eap));
393#else
394# define ex_filetype ex_ni
395# define ex_setfiletype ex_ni
396#endif
397#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000398# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399# define ex_diffpatch ex_ni
400# define ex_diffgetput ex_ni
401# define ex_diffsplit ex_ni
402# define ex_diffthis ex_ni
403# define ex_diffupdate ex_ni
404#endif
405static void ex_digraphs __ARGS((exarg_T *eap));
406static void ex_set __ARGS((exarg_T *eap));
407#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
408# define ex_options ex_ni
409#endif
410#ifdef FEAT_SEARCH_EXTRA
411static void ex_nohlsearch __ARGS((exarg_T *eap));
412static void ex_match __ARGS((exarg_T *eap));
413#else
414# define ex_nohlsearch ex_ni
415# define ex_match ex_ni
416#endif
417#ifdef FEAT_CRYPT
418static void ex_X __ARGS((exarg_T *eap));
419#else
420# define ex_X ex_ni
421#endif
422#ifdef FEAT_FOLDING
423static void ex_fold __ARGS((exarg_T *eap));
424static void ex_foldopen __ARGS((exarg_T *eap));
425static void ex_folddo __ARGS((exarg_T *eap));
426#else
427# define ex_fold ex_ni
428# define ex_foldopen ex_ni
429# define ex_folddo ex_ni
430#endif
431#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
432 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
433# define ex_language ex_ni
434#endif
435#ifndef FEAT_SIGNS
436# define ex_sign ex_ni
437#endif
438#ifndef FEAT_SUN_WORKSHOP
439# define ex_wsverb ex_ni
440#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000441#ifndef FEAT_NETBEANS_INTG
442# define ex_nbkey ex_ni
443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
445#ifndef FEAT_EVAL
446# define ex_debug ex_ni
447# define ex_breakadd ex_ni
448# define ex_debuggreedy ex_ni
449# define ex_breakdel ex_ni
450# define ex_breaklist ex_ni
451#endif
452
453#ifndef FEAT_CMDHIST
454# define ex_history ex_ni
455#endif
456#ifndef FEAT_JUMPLIST
457# define ex_jumps ex_ni
458# define ex_changes ex_ni
459#endif
460
Bram Moolenaar05159a02005-02-26 23:04:13 +0000461#ifndef FEAT_PROFILE
462# define ex_profile ex_ni
463#endif
464
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465/*
466 * Declare cmdnames[].
467 */
468#define DO_DECLARE_EXCMD
469#include "ex_cmds.h"
470
471/*
472 * Table used to quickly search for a command, based on its first character.
473 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000474static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
476 CMD_append,
477 CMD_buffer,
478 CMD_change,
479 CMD_delete,
480 CMD_edit,
481 CMD_file,
482 CMD_global,
483 CMD_help,
484 CMD_insert,
485 CMD_join,
486 CMD_k,
487 CMD_list,
488 CMD_move,
489 CMD_next,
490 CMD_open,
491 CMD_print,
492 CMD_quit,
493 CMD_read,
494 CMD_substitute,
495 CMD_t,
496 CMD_undo,
497 CMD_vglobal,
498 CMD_write,
499 CMD_xit,
500 CMD_yank,
501 CMD_z,
502 CMD_bang
503};
504
505static char_u dollar_command[2] = {'$', 0};
506
507
508#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000509/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510typedef struct
511{
512 char_u *line; /* command line */
513 linenr_T lnum; /* sourcing_lnum of the line */
514} wcmd_T;
515
516/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000517 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000519 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000521struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
523 garray_T *lines_gap; /* growarray with line info */
524 int current_line; /* last read line from growarray */
525 int repeating; /* TRUE when looping a second time */
526 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
527 char_u *(*getline) __ARGS((int, void *, int));
528 void *cookie;
529};
530
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000531static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
532static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000534
535/* Struct to save a few things while debugging. Used in do_cmdline() only. */
536struct dbg_stuff
537{
538 int trylevel;
539 int force_abort;
540 except_T *caught_stack;
541 char_u *vv_exception;
542 char_u *vv_throwpoint;
543 int did_emsg;
544 int got_int;
545 int did_throw;
546 int need_rethrow;
547 int check_cstack;
548 except_T *current_exception;
549};
550
551static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
552static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
553
554 static void
555save_dbg_stuff(dsp)
556 struct dbg_stuff *dsp;
557{
558 dsp->trylevel = trylevel; trylevel = 0;
559 dsp->force_abort = force_abort; force_abort = FALSE;
560 dsp->caught_stack = caught_stack; caught_stack = NULL;
561 dsp->vv_exception = v_exception(NULL);
562 dsp->vv_throwpoint = v_throwpoint(NULL);
563
564 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
565 dsp->did_emsg = did_emsg; did_emsg = FALSE;
566 dsp->got_int = got_int; got_int = FALSE;
567 dsp->did_throw = did_throw; did_throw = FALSE;
568 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
569 dsp->check_cstack = check_cstack; check_cstack = FALSE;
570 dsp->current_exception = current_exception; current_exception = NULL;
571}
572
573 static void
574restore_dbg_stuff(dsp)
575 struct dbg_stuff *dsp;
576{
577 suppress_errthrow = FALSE;
578 trylevel = dsp->trylevel;
579 force_abort = dsp->force_abort;
580 caught_stack = dsp->caught_stack;
581 (void)v_exception(dsp->vv_exception);
582 (void)v_throwpoint(dsp->vv_throwpoint);
583 did_emsg = dsp->did_emsg;
584 got_int = dsp->got_int;
585 did_throw = dsp->did_throw;
586 need_rethrow = dsp->need_rethrow;
587 check_cstack = dsp->check_cstack;
588 current_exception = dsp->current_exception;
589}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590#endif
591
592
593/*
594 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
595 * command is given.
596 */
597 void
598do_exmode(improved)
599 int improved; /* TRUE for "improved Ex" mode */
600{
601 int save_msg_scroll;
602 int prev_msg_row;
603 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000604 int changedtick;
605
606 if (improved)
607 exmode_active = EXMODE_VIM;
608 else
609 exmode_active = EXMODE_NORMAL;
610 State = NORMAL;
611
612 /* When using ":global /pat/ visual" and then "Q" we return to continue
613 * the :global command. */
614 if (global_busy)
615 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
617 save_msg_scroll = msg_scroll;
618 ++RedrawingDisabled; /* don't redisplay the window */
619 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620#ifdef FEAT_GUI
621 /* Ignore scrollbar and mouse events in Ex mode */
622 ++hold_gui_events;
623#endif
624#ifdef FEAT_SNIFF
625 want_sniff_request = 0; /* No K_SNIFF wanted */
626#endif
627
628 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
629 while (exmode_active)
630 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000631#ifdef FEAT_EX_EXTRA
632 /* Check for a ":normal" command and no more characters left. */
633 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
634 {
635 exmode_active = FALSE;
636 break;
637 }
638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 msg_scroll = TRUE;
640 need_wait_return = FALSE;
641 ex_pressedreturn = FALSE;
642 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000643 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 prev_msg_row = msg_row;
645 prev_line = curwin->w_cursor.lnum;
646#ifdef FEAT_SNIFF
647 ProcessSniffRequests();
648#endif
649 if (improved)
650 {
651 cmdline_row = msg_row;
652 do_cmdline(NULL, getexline, NULL, 0);
653 }
654 else
655 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
656 lines_left = Rows - 1;
657
Bram Moolenaardf177f62005-02-22 08:39:57 +0000658 if ((prev_line != curwin->w_cursor.lnum
659 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000661 if (curbuf->b_ml.ml_flags & ML_EMPTY)
662 EMSG(_(e_emptybuf));
663 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000665 if (ex_pressedreturn)
666 {
667 /* go up one line, to overwrite the ":<CR>" line, so the
668 * output doensn't contain empty lines. */
669 msg_row = prev_msg_row;
670 if (prev_msg_row == Rows - 1)
671 msg_row--;
672 }
673 msg_col = 0;
674 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
675 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000678 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
679 {
680 if (curbuf->b_ml.ml_flags & ML_EMPTY)
681 EMSG(_(e_emptybuf));
682 else
683 EMSG(_("E501: At end-of-file"));
684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 }
686
687#ifdef FEAT_GUI
688 --hold_gui_events;
689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 --RedrawingDisabled;
691 --no_wait_return;
692 update_screen(CLEAR);
693 need_wait_return = FALSE;
694 msg_scroll = save_msg_scroll;
695}
696
697/*
698 * Execute a simple command line. Used for translated commands like "*".
699 */
700 int
701do_cmdline_cmd(cmd)
702 char_u *cmd;
703{
704 return do_cmdline(cmd, NULL, NULL,
705 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
706}
707
708/*
709 * do_cmdline(): execute one Ex command line
710 *
711 * 1. Execute "cmdline" when it is not NULL.
712 * If "cmdline" is NULL, or more lines are needed, getline() is used.
713 * 2. Split up in parts separated with '|'.
714 *
715 * This function can be called recursively!
716 *
717 * flags:
718 * DOCMD_VERBOSE - The command will be included in the error message.
719 * DOCMD_NOWAIT - Don't call wait_return() and friends.
720 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
721 * DOCMD_KEYTYPED - Don't reset KeyTyped.
722 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
723 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
724 *
725 * return FAIL if cmdline could not be executed, OK otherwise
726 */
727 int
728do_cmdline(cmdline, getline, cookie, flags)
729 char_u *cmdline;
730 char_u *(*getline) __ARGS((int, void *, int));
731 void *cookie; /* argument for getline() */
732 int flags;
733{
734 char_u *next_cmdline; /* next cmd to execute */
735 char_u *cmdline_copy = NULL; /* copy of cmd line */
736 int used_getline = FALSE; /* used "getline" to obtain command */
737 static int recursive = 0; /* recursive depth */
738 int msg_didout_before_start = 0;
739 int count = 0; /* line number count */
740 int did_inc = FALSE; /* incremented RedrawingDisabled */
741 int retval = OK;
742#ifdef FEAT_EVAL
743 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000744 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 int current_line = 0; /* active line in lines_ga */
746 char_u *fname = NULL; /* function or script name */
747 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
748 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000749 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 int initial_trylevel;
751 struct msglist **saved_msg_list = NULL;
752 struct msglist *private_msg_list;
753
754 /* "getline" and "cookie" passed to do_one_cmd() */
755 char_u *(*cmd_getline) __ARGS((int, void *, int));
756 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000757 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000759 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760#else
761# define cmd_getline getline
762# define cmd_cookie cookie
763#endif
764 static int call_depth = 0; /* recursiveness */
765
766#ifdef FEAT_EVAL
767 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
768 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000769 * This ensures that the do_errthrow() call in do_one_cmd() does not
770 * combine the messages stored by an earlier invocation of do_one_cmd()
771 * with the command name of the later one. This would happen when
772 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 saved_msg_list = msg_list;
774 msg_list = &private_msg_list;
775 private_msg_list = NULL;
776#endif
777
778 /* It's possible to create an endless loop with ":execute", catch that
779 * here. The value of 200 allows nested function calls, ":source", etc. */
780 if (call_depth == 200)
781 {
782 EMSG(_("E169: Command too recursive"));
783#ifdef FEAT_EVAL
784 /* When converting to an exception, we do not include the command name
785 * since this is not an error of the specific command. */
786 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
787 msg_list = saved_msg_list;
788#endif
789 return FAIL;
790 }
791 ++call_depth;
792
793#ifdef FEAT_EVAL
794 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000795 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 cstack.cs_trylevel = 0;
797 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000798 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
800
801 real_cookie = getline_cookie(getline, cookie);
802
803 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804 getline_is_func = getline_equal(getline, cookie, get_func_line);
805 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 ++ex_nesting_level;
807
808 /* Get the function or script name and the address where the next breakpoint
809 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 {
812 fname = func_name(real_cookie);
813 breakpoint = func_breakpoint(real_cookie);
814 dbg_tick = func_dbg_tick(real_cookie);
815 }
816 else if (getline_equal(getline, cookie, getsourceline))
817 {
818 fname = sourcing_name;
819 breakpoint = source_breakpoint(real_cookie);
820 dbg_tick = source_dbg_tick(real_cookie);
821 }
822
823 /*
824 * Initialize "force_abort" and "suppress_errthrow" at the top level.
825 */
826 if (!recursive)
827 {
828 force_abort = FALSE;
829 suppress_errthrow = FALSE;
830 }
831
832 /*
833 * If requested, store and reset the global values controlling the
834 * exception handling (used when debugging).
835 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000836 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000837 save_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
839 initial_trylevel = trylevel;
840
841 /*
842 * "did_throw" will be set to TRUE when an exception is being thrown.
843 */
844 did_throw = FALSE;
845#endif
846 /*
847 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000848 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 * If force_abort is set, we cancel everything.
850 */
851 did_emsg = FALSE;
852
853 /*
854 * KeyTyped is only set when calling vgetc(). Reset it here when not
855 * calling vgetc() (sourced command lines).
856 */
857 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
858 KeyTyped = FALSE;
859
860 /*
861 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000862 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * - for multiple commands on one line, separated with '|'
864 * - when repeating until there are no more lines (for ":source")
865 */
866 next_cmdline = cmdline;
867 do
868 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000869#ifdef FEAT_EVAL
870 getline_is_func = getline_equal(getline, cookie, get_func_line);
871#endif
872
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000873 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (next_cmdline == NULL
875#ifdef FEAT_EVAL
876 && !force_abort
877 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000878 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#endif
880 )
881 did_emsg = FALSE;
882
883 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000884 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 * 2. If no line given: Get an allocated line with getline().
886 * 3. If a line is given: Make a copy, so we can mess with it.
887 */
888
889#ifdef FEAT_EVAL
890 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000891 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
893 /* Each '|' separated command is stored separately in lines_ga, to
894 * be able to jump to it. Don't use next_cmdline now. */
895 vim_free(cmdline_copy);
896 cmdline_copy = NULL;
897
898 /* Check if a function has returned or, unless it has an unclosed
899 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000900 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000902# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000903 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000904 func_line_end(real_cookie);
905# endif
906 if (func_has_ended(real_cookie))
907 {
908 retval = FAIL;
909 break;
910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000912#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000913 else if (do_profiling == PROF_YES
Bram Moolenaar05159a02005-02-26 23:04:13 +0000914 && getline_equal(getline, cookie, getsourceline))
915 script_line_end();
916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
918 /* Check if a sourced file hit a ":finish" command. */
919 if (source_finished(getline, cookie))
920 {
921 retval = FAIL;
922 break;
923 }
924
925 /* If breakpoints have been added/deleted need to check for it. */
926 if (breakpoint != NULL && dbg_tick != NULL
927 && *dbg_tick != debug_tick)
928 {
929 *breakpoint = dbg_find_breakpoint(
930 getline_equal(getline, cookie, getsourceline),
931 fname, sourcing_lnum);
932 *dbg_tick = debug_tick;
933 }
934
935 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
936 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
937
938 /* Did we encounter a breakpoint? */
939 if (breakpoint != NULL && *breakpoint != 0
940 && *breakpoint <= sourcing_lnum)
941 {
942 dbg_breakpoint(fname, sourcing_lnum);
943 /* Find next breakpoint. */
944 *breakpoint = dbg_find_breakpoint(
945 getline_equal(getline, cookie, getsourceline),
946 fname, sourcing_lnum);
947 *dbg_tick = debug_tick;
948 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000949# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000950 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000951 {
952 if (getline_is_func)
953 func_line_start(real_cookie);
954 else if (getline_equal(getline, cookie, getsourceline))
955 script_line_start();
956 }
957# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000960 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000962 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 * again. Pass a different "getline" function to do_one_cmd()
964 * below, so that it stores lines in or reads them from
965 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000966 * while/for loop. */
967 cmd_getline = get_loop_line;
968 cmd_cookie = (void *)&cmd_loop_cookie;
969 cmd_loop_cookie.lines_gap = &lines_ga;
970 cmd_loop_cookie.current_line = current_line;
971 cmd_loop_cookie.getline = getline;
972 cmd_loop_cookie.cookie = cookie;
973 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 }
975 else
976 {
977 cmd_getline = getline;
978 cmd_cookie = cookie;
979 }
980#endif
981
982 /* 2. If no line given, get an allocated line with getline(). */
983 if (next_cmdline == NULL)
984 {
985 /*
986 * Need to set msg_didout for the first line after an ":if",
987 * otherwise the ":if" will be overwritten.
988 */
989 if (count == 1 && getline_equal(getline, cookie, getexline))
990 msg_didout = TRUE;
991 if (getline == NULL || (next_cmdline = getline(':', cookie,
992#ifdef FEAT_EVAL
993 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
994#else
995 0
996#endif
997 )) == NULL)
998 {
999 /* Don't call wait_return for aborted command line. The NULL
1000 * returned for the end of a sourced file or executed function
1001 * doesn't do this. */
1002 if (KeyTyped && !(flags & DOCMD_REPEAT))
1003 need_wait_return = FALSE;
1004 retval = FAIL;
1005 break;
1006 }
1007 used_getline = TRUE;
1008
1009 /*
1010 * Keep the first typed line. Clear it when more lines are typed.
1011 */
1012 if (flags & DOCMD_KEEPLINE)
1013 {
1014 vim_free(repeat_cmdline);
1015 if (count == 0)
1016 repeat_cmdline = vim_strsave(next_cmdline);
1017 else
1018 repeat_cmdline = NULL;
1019 }
1020 }
1021
1022 /* 3. Make a copy of the command so we can mess with it. */
1023 else if (cmdline_copy == NULL)
1024 {
1025 next_cmdline = vim_strsave(next_cmdline);
1026 if (next_cmdline == NULL)
1027 {
1028 EMSG(_(e_outofmem));
1029 retval = FAIL;
1030 break;
1031 }
1032 }
1033 cmdline_copy = next_cmdline;
1034
1035#ifdef FEAT_EVAL
1036 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 * Save the current line when inside a ":while" or ":for", and when
1038 * the command looks like a ":while" or ":for", because we may need it
1039 * later. When there is a '|' and another command, it is stored
1040 * separately, because we need to be able to jump back to it from an
1041 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001043 if (current_line == lines_ga.ga_len
1044 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001046 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 {
1048 retval = FAIL;
1049 break;
1050 }
1051 }
1052 did_endif = FALSE;
1053#endif
1054
1055 if (count++ == 0)
1056 {
1057 /*
1058 * All output from the commands is put below each other, without
1059 * waiting for a return. Don't do this when executing commands
1060 * from a script or when being called recursive (e.g. for ":e
1061 * +command file").
1062 */
1063 if (!(flags & DOCMD_NOWAIT) && !recursive)
1064 {
1065 msg_didout_before_start = msg_didout;
1066 msg_didany = FALSE; /* no output yet */
1067 msg_start();
1068 msg_scroll = TRUE; /* put messages below each other */
1069 ++no_wait_return; /* dont wait for return until finished */
1070 ++RedrawingDisabled;
1071 did_inc = TRUE;
1072 }
1073 }
1074
1075 if (p_verbose >= 15 && sourcing_name != NULL)
1076 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001078 verbose_enter_scroll();
1079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 smsg((char_u *)_("line %ld: %s"),
1081 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001082 if (msg_silent == 0)
1083 msg_puts((char_u *)"\n"); /* don't overwrite this */
1084
1085 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 --no_wait_return;
1087 }
1088
1089 /*
1090 * 2. Execute one '|' separated command.
1091 * do_one_cmd() will return NULL if there is no trailing '|'.
1092 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1093 */
1094 ++recursive;
1095 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1096#ifdef FEAT_EVAL
1097 &cstack,
1098#endif
1099 cmd_getline, cmd_cookie);
1100 --recursive;
1101
1102#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 if (cmd_cookie == (void *)&cmd_loop_cookie)
1104 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001106 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107#endif
1108
1109 if (next_cmdline == NULL)
1110 {
1111 vim_free(cmdline_copy);
1112 cmdline_copy = NULL;
1113#ifdef FEAT_CMDHIST
1114 /*
1115 * If the command was typed, remember it for the ':' register.
1116 * Do this AFTER executing the command to make :@: work.
1117 */
1118 if (getline_equal(getline, cookie, getexline)
1119 && new_last_cmdline != NULL)
1120 {
1121 vim_free(last_cmdline);
1122 last_cmdline = new_last_cmdline;
1123 new_last_cmdline = NULL;
1124 }
1125#endif
1126 }
1127 else
1128 {
1129 /* need to copy the command after the '|' to cmdline_copy, for the
1130 * next do_one_cmd() */
1131 mch_memmove(cmdline_copy, next_cmdline, STRLEN(next_cmdline) + 1);
1132 next_cmdline = cmdline_copy;
1133 }
1134
1135
1136#ifdef FEAT_EVAL
1137 /* reset did_emsg for a function that is not aborted by an error */
1138 if (did_emsg && !force_abort
1139 && getline_equal(getline, cookie, get_func_line)
1140 && !func_has_abort(real_cookie))
1141 did_emsg = FALSE;
1142
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001143 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 {
1145 ++current_line;
1146
1147 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001148 * An ":endwhile", ":endfor" and ":continue" is handled here.
1149 * If we were executing commands, jump back to the ":while" or
1150 * ":for".
1151 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001153 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001155 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 /* Jump back to the matching ":while" or ":for". Be careful
1158 * not to use a cs_line[] from an entry that isn't a ":while"
1159 * or ":for": It would make "current_line" invalid and can
1160 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (!did_emsg && !got_int && !did_throw
1162 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 && (cstack.cs_flags[cstack.cs_idx]
1164 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 && cstack.cs_line[cstack.cs_idx] >= 0
1166 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1167 {
1168 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001169 /* remember we jumped there */
1170 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 line_breakcheck(); /* check if CTRL-C typed */
1172
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 /* Check for the next breakpoint at or after the ":while"
1174 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (breakpoint != NULL)
1176 {
1177 *breakpoint = dbg_find_breakpoint(
1178 getline_equal(getline, cookie, getsourceline),
1179 fname,
1180 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1181 *dbg_tick = debug_tick;
1182 }
1183 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001186 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001188 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1189 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 }
1191 }
1192
1193 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1200 }
1201 }
1202
1203 /*
1204 * When not inside any ":while" loop, clear remembered lines.
1205 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001206 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 {
1208 if (lines_ga.ga_len > 0)
1209 {
1210 sourcing_lnum =
1211 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1212 free_cmdlines(&lines_ga);
1213 }
1214 current_line = 0;
1215 }
1216
1217 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1219 * being restored at the ":endtry". Reset them here and set the
1220 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1221 * This includes the case where a missing ":endif", ":endwhile" or
1222 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001224 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001226 cstack.cs_lflags &= ~CSL_HAD_FINA;
1227 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1228 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 did_throw ? (void *)current_exception : NULL);
1230 did_emsg = got_int = did_throw = FALSE;
1231 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1232 }
1233
1234 /* Update global "trylevel" for recursive calls to do_cmdline() from
1235 * within this loop. */
1236 trylevel = initial_trylevel + cstack.cs_trylevel;
1237
1238 /*
1239 * If the outermost try conditional (accross function calls and sourced
1240 * files) is aborted because of an error, an interrupt, or an uncaught
1241 * exception, cancel everything. If it is left normally, reset
1242 * force_abort to get the non-EH compatible abortion behavior for
1243 * the rest of the script.
1244 */
1245 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1246 force_abort = FALSE;
1247
1248 /* Convert an interrupt to an exception if appropriate. */
1249 (void)do_intthrow(&cstack);
1250#endif /* FEAT_EVAL */
1251
1252 }
1253 /*
1254 * Continue executing command lines when:
1255 * - no CTRL-C typed, no aborting error, no exception thrown or try
1256 * conditionals need to be checked for executing finally clauses or
1257 * catching an interrupt exception
1258 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001259 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 * looping for ":source" command or function call.
1261 */
1262 while (!((got_int
1263#ifdef FEAT_EVAL
1264 || (did_emsg && force_abort) || did_throw
1265#endif
1266 )
1267#ifdef FEAT_EVAL
1268 && cstack.cs_trylevel == 0
1269#endif
1270 )
1271 && !(did_emsg && used_getline
1272 && (getline_equal(getline, cookie, getexmodeline)
1273 || getline_equal(getline, cookie, getexline)))
1274 && (next_cmdline != NULL
1275#ifdef FEAT_EVAL
1276 || cstack.cs_idx >= 0
1277#endif
1278 || (flags & DOCMD_REPEAT)));
1279
1280 vim_free(cmdline_copy);
1281#ifdef FEAT_EVAL
1282 free_cmdlines(&lines_ga);
1283 ga_clear(&lines_ga);
1284
1285 if (cstack.cs_idx >= 0)
1286 {
1287 /*
1288 * If a sourced file or executed function ran to its end, report the
1289 * unclosed conditional.
1290 */
1291 if (!got_int && !did_throw
1292 && ((getline_equal(getline, cookie, getsourceline)
1293 && !source_finished(getline, cookie))
1294 || (getline_equal(getline, cookie, get_func_line)
1295 && !func_has_ended(real_cookie))))
1296 {
1297 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1298 EMSG(_(e_endtry));
1299 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1300 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001301 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1302 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 else
1304 EMSG(_(e_endif));
1305 }
1306
1307 /*
1308 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1309 * ":endtry" in a sourced file or executed function. If the try
1310 * conditional is in its finally clause, ignore anything pending.
1311 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001312 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 */
1314 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001315 {
1316 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1317
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001318 if (idx >= 0)
1319 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001320 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1321 &cstack.cs_looplevel);
1322 }
1323 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 trylevel = initial_trylevel;
1325 }
1326
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001327 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1328 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 * exception, do this now after rewinding the cstack. */
1330 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1331 ? (char_u *)"endfunction" : (char_u *)NULL);
1332
1333 if (trylevel == 0)
1334 {
1335 /*
1336 * When an exception is being thrown out of the outermost try
1337 * conditional, discard the uncaught exception, disable the conversion
1338 * of interrupts or errors to exceptions, and ensure that no more
1339 * commands are executed.
1340 */
1341 if (did_throw)
1342 {
1343 void *p = NULL;
1344 char_u *saved_sourcing_name;
1345 int saved_sourcing_lnum;
1346 struct msglist *messages = NULL, *next;
1347
1348 /*
1349 * If the uncaught exception is a user exception, report it as an
1350 * error. If it is an error exception, display the saved error
1351 * message now. For an interrupt exception, do nothing; the
1352 * interrupt message is given elsewhere.
1353 */
1354 switch (current_exception->type)
1355 {
1356 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001357 vim_snprintf((char *)IObuff, IOSIZE,
1358 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 current_exception->value);
1360 p = vim_strsave(IObuff);
1361 break;
1362 case ET_ERROR:
1363 messages = current_exception->messages;
1364 current_exception->messages = NULL;
1365 break;
1366 case ET_INTERRUPT:
1367 break;
1368 default:
1369 p = vim_strsave((char_u *)_(e_internal));
1370 }
1371
1372 saved_sourcing_name = sourcing_name;
1373 saved_sourcing_lnum = sourcing_lnum;
1374 sourcing_name = current_exception->throw_name;
1375 sourcing_lnum = current_exception->throw_lnum;
1376 current_exception->throw_name = NULL;
1377
1378 discard_current_exception(); /* uses IObuff if 'verbose' */
1379 suppress_errthrow = TRUE;
1380 force_abort = TRUE;
1381
1382 if (messages != NULL)
1383 {
1384 do
1385 {
1386 next = messages->next;
1387 emsg(messages->msg);
1388 vim_free(messages->msg);
1389 vim_free(messages);
1390 messages = next;
1391 }
1392 while (messages != NULL);
1393 }
1394 else if (p != NULL)
1395 {
1396 emsg(p);
1397 vim_free(p);
1398 }
1399 vim_free(sourcing_name);
1400 sourcing_name = saved_sourcing_name;
1401 sourcing_lnum = saved_sourcing_lnum;
1402 }
1403
1404 /*
1405 * On an interrupt or an aborting error not converted to an exception,
1406 * disable the conversion of errors to exceptions. (Interrupts are not
1407 * converted any more, here.) This enables also the interrupt message
1408 * when force_abort is set and did_emsg unset in case of an interrupt
1409 * from a finally clause after an error.
1410 */
1411 else if (got_int || (did_emsg && force_abort))
1412 suppress_errthrow = TRUE;
1413 }
1414
1415 /*
1416 * The current cstack will be freed when do_cmdline() returns. An uncaught
1417 * exception will have to be rethrown in the previous cstack. If a function
1418 * has just returned or a script file was just finished and the previous
1419 * cstack belongs to the same function or, respectively, script file, it
1420 * will have to be checked for finally clauses to be executed due to the
1421 * ":return" or ":finish". This is done in do_one_cmd().
1422 */
1423 if (did_throw)
1424 need_rethrow = TRUE;
1425 if ((getline_equal(getline, cookie, getsourceline)
1426 && ex_nesting_level > source_level(real_cookie))
1427 || (getline_equal(getline, cookie, get_func_line)
1428 && ex_nesting_level > func_level(real_cookie) + 1))
1429 {
1430 if (!did_throw)
1431 check_cstack = TRUE;
1432 }
1433 else
1434 {
1435 /* When leaving a function, reduce nesting level. */
1436 if (getline_equal(getline, cookie, get_func_line))
1437 --ex_nesting_level;
1438 /*
1439 * Go to debug mode when returning from a function in which we are
1440 * single-stepping.
1441 */
1442 if ((getline_equal(getline, cookie, getsourceline)
1443 || getline_equal(getline, cookie, get_func_line))
1444 && ex_nesting_level + 1 <= debug_break_level)
1445 do_debug(getline_equal(getline, cookie, getsourceline)
1446 ? (char_u *)_("End of sourced file")
1447 : (char_u *)_("End of function"));
1448 }
1449
1450 /*
1451 * Restore the exception environment (done after returning from the
1452 * debugger).
1453 */
1454 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001455 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
1457 msg_list = saved_msg_list;
1458#endif /* FEAT_EVAL */
1459
1460 /*
1461 * If there was too much output to fit on the command line, ask the user to
1462 * hit return before redrawing the screen. With the ":global" command we do
1463 * this only once after the command is finished.
1464 */
1465 if (did_inc)
1466 {
1467 --RedrawingDisabled;
1468 --no_wait_return;
1469 msg_scroll = FALSE;
1470
1471 /*
1472 * When just finished an ":if"-":else" which was typed, no need to
1473 * wait for hit-return. Also for an error situation.
1474 */
1475 if (retval == FAIL
1476#ifdef FEAT_EVAL
1477 || (did_endif && KeyTyped && !did_emsg)
1478#endif
1479 )
1480 {
1481 need_wait_return = FALSE;
1482 msg_didany = FALSE; /* don't wait when restarting edit */
1483 }
1484 else if (need_wait_return)
1485 {
1486 /*
1487 * The msg_start() above clears msg_didout. The wait_return we do
1488 * here should not overwrite the command that may be shown before
1489 * doing that.
1490 */
1491 msg_didout |= msg_didout_before_start;
1492 wait_return(FALSE);
1493 }
1494 }
1495
1496#ifndef FEAT_EVAL
1497 /*
1498 * Reset if_level, in case a sourced script file contains more ":if" than
1499 * ":endif" (could be ":if x | foo | endif").
1500 */
1501 if_level = 0;
1502#endif
1503
1504 --call_depth;
1505 return retval;
1506}
1507
1508#ifdef FEAT_EVAL
1509/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001510 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 */
1512 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001513get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 int c;
1515 void *cookie;
1516 int indent;
1517{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001518 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 wcmd_T *wp;
1520 char_u *line;
1521
1522 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1523 {
1524 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001525 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001527 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (cp->getline == NULL)
1529 line = getcmdline(c, 0L, indent);
1530 else
1531 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001532 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 ++cp->current_line;
1534
1535 return line;
1536 }
1537
1538 KeyTyped = FALSE;
1539 ++cp->current_line;
1540 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1541 sourcing_lnum = wp->lnum;
1542 return vim_strsave(wp->line);
1543}
1544
1545/*
1546 * Store a line in "gap" so that a ":while" loop can execute it again.
1547 */
1548 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001549store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 garray_T *gap;
1551 char_u *line;
1552{
1553 if (ga_grow(gap, 1) == FAIL)
1554 return FAIL;
1555 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1556 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1557 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 return OK;
1559}
1560
1561/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001562 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 */
1564 static void
1565free_cmdlines(gap)
1566 garray_T *gap;
1567{
1568 while (gap->ga_len > 0)
1569 {
1570 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1571 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 }
1573}
1574#endif
1575
1576/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001577 * If "getline" is get_loop_line(), return TRUE if the getline it uses equals
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 * "func". * Otherwise return TRUE when "getline" equals "func".
1579 */
1580/*ARGSUSED*/
1581 int
1582getline_equal(getline, cookie, func)
1583 char_u *(*getline) __ARGS((int, void *, int));
1584 void *cookie; /* argument for getline() */
1585 char_u *(*func) __ARGS((int, void *, int));
1586{
1587#ifdef FEAT_EVAL
1588 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001589 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001591 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 * function that's orignally used to obtain the lines. This may be nested
1593 * several levels. */
1594 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001595 cp = (struct loop_cookie *)cookie;
1596 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
1598 gp = cp->getline;
1599 cp = cp->cookie;
1600 }
1601 return gp == func;
1602#else
1603 return getline == func;
1604#endif
1605}
1606
1607#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1608/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001609 * If "getline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 * getline function. Otherwise return "cookie".
1611 */
1612/*ARGSUSED*/
1613 void *
1614getline_cookie(getline, cookie)
1615 char_u *(*getline) __ARGS((int, void *, int));
1616 void *cookie; /* argument for getline() */
1617{
1618# ifdef FEAT_EVAL
1619 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001620 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001622 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 * cookie that's orignally used to obtain the lines. This may be nested
1624 * several levels. */
1625 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001626 cp = (struct loop_cookie *)cookie;
1627 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {
1629 gp = cp->getline;
1630 cp = cp->cookie;
1631 }
1632 return cp;
1633# else
1634 return cookie;
1635# endif
1636}
1637#endif
1638
1639/*
1640 * Execute one Ex command.
1641 *
1642 * If 'sourcing' is TRUE, the command will be included in the error message.
1643 *
1644 * 1. skip comment lines and leading space
1645 * 2. handle command modifiers
1646 * 3. parse range
1647 * 4. parse command
1648 * 5. parse arguments
1649 * 6. switch on command name
1650 *
1651 * Note: "getline" can be NULL.
1652 *
1653 * This function may be called recursively!
1654 */
1655#if (_MSC_VER == 1200)
1656/*
Bram Moolenaared203462004-06-16 11:19:22 +00001657 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001659 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660#endif
1661 static char_u *
1662do_one_cmd(cmdlinep, sourcing,
1663#ifdef FEAT_EVAL
1664 cstack,
1665#endif
1666 getline, cookie)
1667 char_u **cmdlinep;
1668 int sourcing;
1669#ifdef FEAT_EVAL
1670 struct condstack *cstack;
1671#endif
1672 char_u *(*getline) __ARGS((int, void *, int));
1673 void *cookie; /* argument for getline() */
1674{
1675 char_u *p;
1676 linenr_T lnum;
1677 long n;
1678 char_u *errormsg = NULL; /* error message */
1679 exarg_T ea; /* Ex command arguments */
1680 long verbose_save = -1;
1681 int save_msg_scroll = 0;
1682 int did_silent = 0;
1683 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001684#ifdef HAVE_SANDBOX
1685 int did_sandbox = FALSE;
1686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 cmdmod_T save_cmdmod;
1688 int ni; /* set when Not Implemented */
1689
1690 vim_memset(&ea, 0, sizeof(ea));
1691 ea.line1 = 1;
1692 ea.line2 = 1;
1693#ifdef FEAT_EVAL
1694 ++ex_nesting_level;
1695#endif
1696
1697 /* when not editing the last file :q has to be typed twice */
1698 if (quitmore
1699#ifdef FEAT_EVAL
1700 /* avoid that a function call in 'statusline' does this */
1701 && !getline_equal(getline, cookie, get_func_line)
1702#endif
1703 )
1704 --quitmore;
1705
1706 /*
1707 * Reset browse, confirm, etc.. They are restored when returning, for
1708 * recursive calls.
1709 */
1710 save_cmdmod = cmdmod;
1711 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1712
1713 /*
1714 * Repeat until no more command modifiers are found.
1715 */
1716 ea.cmd = *cmdlinep;
1717 for (;;)
1718 {
1719/*
1720 * 1. skip comment lines and leading white space and colons
1721 */
1722 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1723 ++ea.cmd;
1724
1725 /* in ex mode, an empty line works like :+ */
1726 if (*ea.cmd == NUL && exmode_active
1727 && (getline_equal(getline, cookie, getexmodeline)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001728 || getline_equal(getline, cookie, getexline))
1729 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 {
1731 ea.cmd = (char_u *)"+";
1732 ex_pressedreturn = TRUE;
1733 }
1734
1735 /* ignore comment and empty lines */
1736 if (*ea.cmd == '"' || *ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001737 {
1738 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
1742/*
1743 * 2. handle command modifiers.
1744 */
1745 p = ea.cmd;
1746 if (VIM_ISDIGIT(*ea.cmd))
1747 p = skipwhite(skipdigits(ea.cmd));
1748 switch (*p)
1749 {
1750 /* When adding an entry, also modify cmd_exists(). */
1751 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1752 break;
1753#ifdef FEAT_WINDOWS
1754 cmdmod.split |= WSP_ABOVE;
1755#endif
1756 continue;
1757
1758 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1759 {
1760#ifdef FEAT_WINDOWS
1761 cmdmod.split |= WSP_BELOW;
1762#endif
1763 continue;
1764 }
1765 if (checkforcmd(&ea.cmd, "browse", 3))
1766 {
1767#ifdef FEAT_BROWSE
1768 cmdmod.browse = TRUE;
1769#endif
1770 continue;
1771 }
1772 if (!checkforcmd(&ea.cmd, "botright", 2))
1773 break;
1774#ifdef FEAT_WINDOWS
1775 cmdmod.split |= WSP_BOT;
1776#endif
1777 continue;
1778
1779 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1780 break;
1781#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1782 cmdmod.confirm = TRUE;
1783#endif
1784 continue;
1785
1786 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1787 {
1788 cmdmod.keepmarks = TRUE;
1789 continue;
1790 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001791 if (checkforcmd(&ea.cmd, "keepalt", 5))
1792 {
1793 cmdmod.keepalt = TRUE;
1794 continue;
1795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1797 break;
1798 cmdmod.keepjumps = TRUE;
1799 continue;
1800
1801 /* ":hide" and ":hide | cmd" are not modifiers */
1802 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1803 || *p == NUL || ends_excmd(*p))
1804 break;
1805 ea.cmd = p;
1806 cmdmod.hide = TRUE;
1807 continue;
1808
1809 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1810 {
1811 cmdmod.lockmarks = TRUE;
1812 continue;
1813 }
1814
1815 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1816 break;
1817#ifdef FEAT_WINDOWS
1818 cmdmod.split |= WSP_ABOVE;
1819#endif
1820 continue;
1821
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001822 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1823 break;
1824#ifdef FEAT_AUTOCMD
1825 if (cmdmod.save_ei == NULL)
1826 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001827 /* Set 'eventignore' to "all". Restore the
1828 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001829 cmdmod.save_ei = vim_strsave(p_ei);
1830 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001831 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001832 }
1833#endif
1834 continue;
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1837 break;
1838#ifdef FEAT_WINDOWS
1839 cmdmod.split |= WSP_BELOW;
1840#endif
1841 continue;
1842
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001843 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1844 {
1845#ifdef HAVE_SANDBOX
1846 if (!did_sandbox)
1847 ++sandbox;
1848 did_sandbox = TRUE;
1849#endif
1850 continue;
1851 }
1852 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 break;
1854 ++did_silent;
1855 ++msg_silent;
1856 save_msg_scroll = msg_scroll;
1857 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1858 {
1859 /* ":silent!", but not "silent !cmd" */
1860 ea.cmd = skipwhite(ea.cmd + 1);
1861 ++emsg_silent;
1862 ++did_esilent;
1863 }
1864 continue;
1865
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001866 case 't': if (checkforcmd(&p, "tab", 3))
1867 {
1868#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001869 if (vim_isdigit(*ea.cmd))
1870 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1871 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001872 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001873 ea.cmd = p;
1874#endif
1875 continue;
1876 }
1877 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 break;
1879#ifdef FEAT_WINDOWS
1880 cmdmod.split |= WSP_TOP;
1881#endif
1882 continue;
1883
1884 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1885 {
1886#ifdef FEAT_VERTSPLIT
1887 cmdmod.split |= WSP_VERT;
1888#endif
1889 continue;
1890 }
1891 if (!checkforcmd(&p, "verbose", 4))
1892 break;
1893 if (verbose_save < 0)
1894 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001895 if (vim_isdigit(*ea.cmd))
1896 p_verbose = atoi((char *)ea.cmd);
1897 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 p_verbose = 1;
1899 ea.cmd = p;
1900 continue;
1901 }
1902 break;
1903 }
1904
1905#ifdef FEAT_EVAL
1906 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1907 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1908#else
1909 ea.skip = (if_level > 0);
1910#endif
1911
1912#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001913# ifdef FEAT_PROFILE
1914 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001915 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001916 {
1917 if (getline_equal(getline, cookie, get_func_line))
1918 func_line_exec(getline_cookie(getline, cookie));
1919 else if (getline_equal(getline, cookie, getsourceline))
1920 script_line_exec();
1921 }
1922#endif
1923
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 /* May go to debug mode. If this happens and the ">quit" debug command is
1925 * used, throw an interrupt exception and skip the next command. */
1926 dbg_check_breakpoint(&ea);
1927 if (!ea.skip && got_int)
1928 {
1929 ea.skip = TRUE;
1930 (void)do_intthrow(cstack);
1931 }
1932#endif
1933
1934/*
1935 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1936 *
1937 * where 'addr' is:
1938 *
1939 * % (entire file)
1940 * $ [+-NUM]
1941 * 'x [+-NUM] (where x denotes a currently defined mark)
1942 * . [+-NUM]
1943 * [+-NUM]..
1944 * NUM
1945 *
1946 * The ea.cmd pointer is updated to point to the first character following the
1947 * range spec. If an initial address is found, but no second, the upper bound
1948 * is equal to the lower.
1949 */
1950
1951 /* repeat for all ',' or ';' separated addresses */
1952 for (;;)
1953 {
1954 ea.line1 = ea.line2;
1955 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1956 ea.cmd = skipwhite(ea.cmd);
1957 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1958 if (ea.cmd == NULL) /* error detected */
1959 goto doend;
1960 if (lnum == MAXLNUM)
1961 {
1962 if (*ea.cmd == '%') /* '%' - all lines */
1963 {
1964 ++ea.cmd;
1965 ea.line1 = 1;
1966 ea.line2 = curbuf->b_ml.ml_line_count;
1967 ++ea.addr_count;
1968 }
1969 /* '*' - visual area */
1970 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1971 {
1972 pos_T *fp;
1973
1974 ++ea.cmd;
1975 if (!ea.skip)
1976 {
1977 fp = getmark('<', FALSE);
1978 if (check_mark(fp) == FAIL)
1979 goto doend;
1980 ea.line1 = fp->lnum;
1981 fp = getmark('>', FALSE);
1982 if (check_mark(fp) == FAIL)
1983 goto doend;
1984 ea.line2 = fp->lnum;
1985 ++ea.addr_count;
1986 }
1987 }
1988 }
1989 else
1990 ea.line2 = lnum;
1991 ea.addr_count++;
1992
1993 if (*ea.cmd == ';')
1994 {
1995 if (!ea.skip)
1996 curwin->w_cursor.lnum = ea.line2;
1997 }
1998 else if (*ea.cmd != ',')
1999 break;
2000 ++ea.cmd;
2001 }
2002
2003 /* One address given: set start and end lines */
2004 if (ea.addr_count == 1)
2005 {
2006 ea.line1 = ea.line2;
2007 /* ... but only implicit: really no address given */
2008 if (lnum == MAXLNUM)
2009 ea.addr_count = 0;
2010 }
2011
2012 /* Don't leave the cursor on an illegal line (caused by ';') */
2013 check_cursor_lnum();
2014
2015/*
2016 * 4. parse command
2017 */
2018
2019 /*
2020 * Skip ':' and any white space
2021 */
2022 ea.cmd = skipwhite(ea.cmd);
2023 while (*ea.cmd == ':')
2024 ea.cmd = skipwhite(ea.cmd + 1);
2025
2026 /*
2027 * If we got a line, but no command, then go to the line.
2028 * If we find a '|' or '\n' we set ea.nextcmd.
2029 */
2030 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2031 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2032 {
2033 /*
2034 * strange vi behaviour:
2035 * ":3" jumps to line 3
2036 * ":3|..." prints line 3
2037 * ":|" prints current line
2038 */
2039 if (ea.skip) /* skip this if inside :if */
2040 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002041 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
2043 ea.cmdidx = CMD_print;
2044 ea.argt = RANGE+COUNT+TRLBAR;
2045 if ((errormsg = invalid_range(&ea)) == NULL)
2046 {
2047 correct_range(&ea);
2048 ex_print(&ea);
2049 }
2050 }
2051 else if (ea.addr_count != 0)
2052 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002053 if (ea.line2 > curbuf->b_ml.ml_line_count)
2054 {
2055 /* With '-' in 'cpoptions' a line number past the file is an
2056 * error, otherwise put it at the end of the file. */
2057 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2058 ea.line2 = -1;
2059 else
2060 ea.line2 = curbuf->b_ml.ml_line_count;
2061 }
2062
2063 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002064 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 else
2066 {
2067 if (ea.line2 == 0)
2068 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 else
2070 curwin->w_cursor.lnum = ea.line2;
2071 beginline(BL_SOL | BL_FIX);
2072 }
2073 }
2074 goto doend;
2075 }
2076
2077 /* Find the command and let "p" point to after it. */
2078 p = find_command(&ea, NULL);
2079
2080#ifdef FEAT_USR_CMDS
2081 if (p == NULL)
2082 {
2083 if (!ea.skip)
2084 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2085 goto doend;
2086 }
2087 /* Check for wrong commands. */
2088 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2089 {
2090 errormsg = uc_fun_cmd();
2091 goto doend;
2092 }
2093#endif
2094 if (ea.cmdidx == CMD_SIZE)
2095 {
2096 if (!ea.skip)
2097 {
2098 STRCPY(IObuff, _("E492: Not an editor command"));
2099 if (!sourcing)
2100 {
2101 STRCAT(IObuff, ": ");
2102 STRNCAT(IObuff, *cmdlinep, 40);
2103 }
2104 errormsg = IObuff;
2105 }
2106 goto doend;
2107 }
2108
2109 ni = (
2110#ifdef FEAT_USR_CMDS
2111 !USER_CMDIDX(ea.cmdidx) &&
2112#endif
2113 cmdnames[ea.cmdidx].cmd_func == ex_ni);
2114
2115#ifndef FEAT_EVAL
2116 /*
2117 * When the expression evaluation is disabled, recognize the ":if" and
2118 * ":endif" commands and ignore everything in between it.
2119 */
2120 if (ea.cmdidx == CMD_if)
2121 ++if_level;
2122 if (if_level)
2123 {
2124 if (ea.cmdidx == CMD_endif)
2125 --if_level;
2126 goto doend;
2127 }
2128
2129#endif
2130
2131 if (*p == '!' && ea.cmdidx != CMD_substitute) /* forced commands */
2132 {
2133 ++p;
2134 ea.forceit = TRUE;
2135 }
2136 else
2137 ea.forceit = FALSE;
2138
2139/*
2140 * 5. parse arguments
2141 */
2142#ifdef FEAT_USR_CMDS
2143 if (!USER_CMDIDX(ea.cmdidx))
2144#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002145 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
2147 if (!ea.skip)
2148 {
2149#ifdef HAVE_SANDBOX
2150 if (sandbox != 0 && !(ea.argt & SBOXOK))
2151 {
2152 /* Command not allowed in sandbox. */
2153 errormsg = (char_u *)_(e_sandbox);
2154 goto doend;
2155 }
2156#endif
2157 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2158 {
2159 /* Command not allowed in non-'modifiable' buffer */
2160 errormsg = (char_u *)_(e_modifiable);
2161 goto doend;
2162 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002163
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002164 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165# ifdef FEAT_USR_CMDS
2166 && !USER_CMDIDX(ea.cmdidx)
2167# endif
2168 )
2169 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002170 /* Command not allowed when editing the command line. */
2171#ifdef FEAT_CMDWIN
2172 if (cmdwin_type != 0)
2173 errormsg = (char_u *)_(e_cmdwin);
2174 else
2175#endif
2176 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 goto doend;
2178 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002180 /* Disallow editing another buffer when "curbuf_lock" is set.
2181 * Do allow ":edit" (check for argument later).
2182 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002183 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002184 && ea.cmdidx != CMD_edit
2185 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002186# ifdef FEAT_USR_CMDS
2187 && !USER_CMDIDX(ea.cmdidx)
2188# endif
2189 && curbuf_locked())
2190 goto doend;
2191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
2193 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2194 {
2195 /* no range allowed */
2196 errormsg = (char_u *)_(e_norange);
2197 goto doend;
2198 }
2199 }
2200
2201 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2202 {
2203 errormsg = (char_u *)_(e_nobang);
2204 goto doend;
2205 }
2206
2207 /*
2208 * Don't complain about the range if it is not used
2209 * (could happen if line_count is accidentally set to 0).
2210 */
2211 if (!ea.skip && !ni)
2212 {
2213 /*
2214 * If the range is backwards, ask for confirmation and, if given, swap
2215 * ea.line1 & ea.line2 so it's forwards again.
2216 * When global command is busy, don't ask, will fail below.
2217 */
2218 if (!global_busy && ea.line1 > ea.line2)
2219 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002220 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002222 if (sourcing || exmode_active)
2223 {
2224 errormsg = (char_u *)_("E493: Backwards range given");
2225 goto doend;
2226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 if (ask_yesno((char_u *)
2228 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002229 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 }
2231 lnum = ea.line1;
2232 ea.line1 = ea.line2;
2233 ea.line2 = lnum;
2234 }
2235 if ((errormsg = invalid_range(&ea)) != NULL)
2236 goto doend;
2237 }
2238
2239 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2240 ea.line2 = 1;
2241
2242 correct_range(&ea);
2243
2244#ifdef FEAT_FOLDING
2245 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2246 {
2247 /* Put the first line at the start of a closed fold, put the last line
2248 * at the end of a closed fold. */
2249 (void)hasFolding(ea.line1, &ea.line1, NULL);
2250 (void)hasFolding(ea.line2, NULL, &ea.line2);
2251 }
2252#endif
2253
2254#ifdef FEAT_QUICKFIX
2255 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002256 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 * option here, so things like % get expanded.
2258 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002259 p = replace_makeprg(&ea, p, cmdlinep);
2260 if (p == NULL)
2261 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262#endif
2263
2264 /*
2265 * Skip to start of argument.
2266 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2267 */
2268 if (ea.cmdidx == CMD_bang)
2269 ea.arg = p;
2270 else
2271 ea.arg = skipwhite(p);
2272
2273 /*
2274 * Check for "++opt=val" argument.
2275 * Must be first, allow ":w ++enc=utf8 !cmd"
2276 */
2277 if (ea.argt & ARGOPT)
2278 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2279 if (getargopt(&ea) == FAIL && !ni)
2280 {
2281 errormsg = (char_u *)_(e_invarg);
2282 goto doend;
2283 }
2284
2285 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2286 {
2287 if (*ea.arg == '>') /* append */
2288 {
2289 if (*++ea.arg != '>') /* typed wrong */
2290 {
2291 errormsg = (char_u *)_("E494: Use w or w>>");
2292 goto doend;
2293 }
2294 ea.arg = skipwhite(ea.arg + 1);
2295 ea.append = TRUE;
2296 }
2297 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2298 {
2299 ++ea.arg;
2300 ea.usefilter = TRUE;
2301 }
2302 }
2303
2304 if (ea.cmdidx == CMD_read)
2305 {
2306 if (ea.forceit)
2307 {
2308 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2309 ea.forceit = FALSE;
2310 }
2311 else if (*ea.arg == '!') /* :r !filter */
2312 {
2313 ++ea.arg;
2314 ea.usefilter = TRUE;
2315 }
2316 }
2317
2318 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2319 {
2320 ea.amount = 1;
2321 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2322 {
2323 ++ea.arg;
2324 ++ea.amount;
2325 }
2326 ea.arg = skipwhite(ea.arg);
2327 }
2328
2329 /*
2330 * Check for "+command" argument, before checking for next command.
2331 * Don't do this for ":read !cmd" and ":write !cmd".
2332 */
2333 if ((ea.argt & EDITCMD) && !ea.usefilter)
2334 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2335
2336 /*
2337 * Check for '|' to separate commands and '"' to start comments.
2338 * Don't do this for ":read !cmd" and ":write !cmd".
2339 */
2340 if ((ea.argt & TRLBAR) && !ea.usefilter)
2341 separate_nextcmd(&ea);
2342
2343 /*
2344 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002345 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2346 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002348 else if (ea.cmdidx == CMD_bang
2349 || ea.cmdidx == CMD_global
2350 || ea.cmdidx == CMD_vglobal
2351 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
2353 for (p = ea.arg; *p; ++p)
2354 {
2355 /* Remove one backslash before a newline, so that it's possible to
2356 * pass a newline to the shell and also a newline that is preceded
2357 * with a backslash. This makes it impossible to end a shell
2358 * command in a backslash, but that doesn't appear useful.
2359 * Halving the number of backslashes is incompatible with previous
2360 * versions. */
2361 if (*p == '\\' && p[1] == '\n')
2362 mch_memmove(p, p + 1, STRLEN(p));
2363 else if (*p == '\n')
2364 {
2365 ea.nextcmd = p + 1;
2366 *p = NUL;
2367 break;
2368 }
2369 }
2370 }
2371
2372 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2373 {
2374 ea.line1 = 1;
2375 ea.line2 = curbuf->b_ml.ml_line_count;
2376 }
2377
2378 /* accept numbered register only when no count allowed (:put) */
2379 if ( (ea.argt & REGSTR)
2380 && *ea.arg != NUL
2381#ifdef FEAT_USR_CMDS
2382 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2383 && USER_CMDIDX(ea.cmdidx)))
2384 /* Do not allow register = for user commands */
2385 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2386#else
2387 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2388#endif
2389 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2390 {
2391 ea.regname = *ea.arg++;
2392#ifdef FEAT_EVAL
2393 /* for '=' register: accept the rest of the line as an expression */
2394 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2395 {
2396 set_expr_line(vim_strsave(ea.arg));
2397 ea.arg += STRLEN(ea.arg);
2398 }
2399#endif
2400 ea.arg = skipwhite(ea.arg);
2401 }
2402
2403 /*
2404 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2405 * count, it's a buffer name.
2406 */
2407 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2408 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2409 || vim_iswhite(*p)))
2410 {
2411 n = getdigits(&ea.arg);
2412 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002413 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
2415 errormsg = (char_u *)_(e_zerocount);
2416 goto doend;
2417 }
2418 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2419 {
2420 ea.line2 = n;
2421 if (ea.addr_count == 0)
2422 ea.addr_count = 1;
2423 }
2424 else
2425 {
2426 ea.line1 = ea.line2;
2427 ea.line2 += n - 1;
2428 ++ea.addr_count;
2429 /*
2430 * Be vi compatible: no error message for out of range.
2431 */
2432 if (ea.line2 > curbuf->b_ml.ml_line_count)
2433 ea.line2 = curbuf->b_ml.ml_line_count;
2434 }
2435 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002436
2437 /*
2438 * Check for flags: 'l', 'p' and '#'.
2439 */
2440 if (ea.argt & EXFLAGS)
2441 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002443 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002444 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
2446 errormsg = (char_u *)_(e_trailing);
2447 goto doend;
2448 }
2449
2450 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2451 {
2452 errormsg = (char_u *)_(e_argreq);
2453 goto doend;
2454 }
2455
2456#ifdef FEAT_EVAL
2457 /*
2458 * Skip the command when it's not going to be executed.
2459 * The commands like :if, :endif, etc. always need to be executed.
2460 * Also make an exception for commands that handle a trailing command
2461 * themselves.
2462 */
2463 if (ea.skip)
2464 {
2465 switch (ea.cmdidx)
2466 {
2467 /* commands that need evaluation */
2468 case CMD_while:
2469 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002470 case CMD_for:
2471 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 case CMD_if:
2473 case CMD_elseif:
2474 case CMD_else:
2475 case CMD_endif:
2476 case CMD_try:
2477 case CMD_catch:
2478 case CMD_finally:
2479 case CMD_endtry:
2480 case CMD_function:
2481 break;
2482
2483 /* Commands that handle '|' themselves. Check: A command should
2484 * either have the TRLBAR flag, appear in this list or appear in
2485 * the list at ":help :bar". */
2486 case CMD_aboveleft:
2487 case CMD_and:
2488 case CMD_belowright:
2489 case CMD_botright:
2490 case CMD_browse:
2491 case CMD_call:
2492 case CMD_confirm:
2493 case CMD_delfunction:
2494 case CMD_djump:
2495 case CMD_dlist:
2496 case CMD_dsearch:
2497 case CMD_dsplit:
2498 case CMD_echo:
2499 case CMD_echoerr:
2500 case CMD_echomsg:
2501 case CMD_echon:
2502 case CMD_execute:
2503 case CMD_help:
2504 case CMD_hide:
2505 case CMD_ijump:
2506 case CMD_ilist:
2507 case CMD_isearch:
2508 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002509 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 case CMD_keepjumps:
2511 case CMD_keepmarks:
2512 case CMD_leftabove:
2513 case CMD_let:
2514 case CMD_lockmarks:
2515 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002516 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 case CMD_perl:
2518 case CMD_psearch:
2519 case CMD_python:
2520 case CMD_return:
2521 case CMD_rightbelow:
2522 case CMD_ruby:
2523 case CMD_silent:
2524 case CMD_smagic:
2525 case CMD_snomagic:
2526 case CMD_substitute:
2527 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002528 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 case CMD_tcl:
2530 case CMD_throw:
2531 case CMD_tilde:
2532 case CMD_topleft:
2533 case CMD_unlet:
2534 case CMD_verbose:
2535 case CMD_vertical:
2536 break;
2537
2538 default: goto doend;
2539 }
2540 }
2541#endif
2542
2543 if (ea.argt & XFILE)
2544 {
2545 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2546 goto doend;
2547 }
2548
2549#ifdef FEAT_LISTCMDS
2550 /*
2551 * Accept buffer name. Cannot be used at the same time with a buffer
2552 * number. Don't do this for a user command.
2553 */
2554 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2555# ifdef FEAT_USR_CMDS
2556 && !USER_CMDIDX(ea.cmdidx)
2557# endif
2558 )
2559 {
2560 /*
2561 * :bdelete, :bwipeout and :bunload take several arguments, separated
2562 * by spaces: find next space (skipping over escaped characters).
2563 * The others take one argument: ignore trailing spaces.
2564 */
2565 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2566 || ea.cmdidx == CMD_bunload)
2567 p = skiptowhite_esc(ea.arg);
2568 else
2569 {
2570 p = ea.arg + STRLEN(ea.arg);
2571 while (p > ea.arg && vim_iswhite(p[-1]))
2572 --p;
2573 }
2574 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2575 if (ea.line2 < 0) /* failed */
2576 goto doend;
2577 ea.addr_count = 1;
2578 ea.arg = skipwhite(p);
2579 }
2580#endif
2581
2582/*
2583 * 6. switch on command name
2584 *
2585 * The "ea" structure holds the arguments that can be used.
2586 */
2587 ea.cmdlinep = cmdlinep;
2588 ea.getline = getline;
2589 ea.cookie = cookie;
2590#ifdef FEAT_EVAL
2591 ea.cstack = cstack;
2592#endif
2593
2594#ifdef FEAT_USR_CMDS
2595 if (USER_CMDIDX(ea.cmdidx))
2596 {
2597 /*
2598 * Execute a user-defined command.
2599 */
2600 do_ucmd(&ea);
2601 }
2602 else
2603#endif
2604 {
2605 /*
2606 * Call the function to execute the command.
2607 */
2608 ea.errmsg = NULL;
2609 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2610 if (ea.errmsg != NULL)
2611 errormsg = (char_u *)_(ea.errmsg);
2612 }
2613
2614#ifdef FEAT_EVAL
2615 /*
2616 * If the command just executed called do_cmdline(), any throw or ":return"
2617 * or ":finish" encountered there must also check the cstack of the still
2618 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2619 * exception, or reanimate a returned function or finished script file and
2620 * return or finish it again.
2621 */
2622 if (need_rethrow)
2623 do_throw(cstack);
2624 else if (check_cstack)
2625 {
2626 if (source_finished(getline, cookie))
2627 do_finish(&ea, TRUE);
2628 else if (getline_equal(getline, cookie, get_func_line)
2629 && current_func_returned())
2630 do_return(&ea, TRUE, FALSE, NULL);
2631 }
2632 need_rethrow = check_cstack = FALSE;
2633#endif
2634
2635doend:
2636 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2637 curwin->w_cursor.lnum = 1;
2638
2639 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2640 {
2641 if (sourcing)
2642 {
2643 if (errormsg != IObuff)
2644 {
2645 STRCPY(IObuff, errormsg);
2646 errormsg = IObuff;
2647 }
2648 STRCAT(errormsg, ": ");
2649 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff));
2650 }
2651 emsg(errormsg);
2652 }
2653#ifdef FEAT_EVAL
2654 do_errthrow(cstack,
2655 (ea.cmdidx != CMD_SIZE
2656# ifdef FEAT_USR_CMDS
2657 && !USER_CMDIDX(ea.cmdidx)
2658# endif
2659 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2660#endif
2661
2662 if (verbose_save >= 0)
2663 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002664#ifdef FEAT_AUTOCMD
2665 if (cmdmod.save_ei != NULL)
2666 {
2667 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002668 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2669 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002670 free_string_option(cmdmod.save_ei);
2671 }
2672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
2674 cmdmod = save_cmdmod;
2675
2676 if (did_silent > 0)
2677 {
2678 /* messages could be enabled for a serious error, need to check if the
2679 * counters don't become negative */
2680 msg_silent -= did_silent;
2681 if (msg_silent < 0)
2682 msg_silent = 0;
2683 emsg_silent -= did_esilent;
2684 if (emsg_silent < 0)
2685 emsg_silent = 0;
2686 /* Restore msg_scroll, it's set by file I/O commands, even when no
2687 * message is actually displayed. */
2688 msg_scroll = save_msg_scroll;
2689 }
2690
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002691#ifdef HAVE_SANDBOX
2692 if (did_sandbox)
2693 --sandbox;
2694#endif
2695
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2697 ea.nextcmd = NULL;
2698
2699#ifdef FEAT_EVAL
2700 --ex_nesting_level;
2701#endif
2702
2703 return ea.nextcmd;
2704}
2705#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002706 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707#endif
2708
2709/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002710 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 * If there is a match advance "pp" to the argument and return TRUE.
2712 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002713 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002715 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 char *cmd; /* name of command */
2717 int len; /* required length */
2718{
2719 int i;
2720
2721 for (i = 0; cmd[i] != NUL; ++i)
2722 if (cmd[i] != (*pp)[i])
2723 break;
2724 if (i >= len && !isalpha((*pp)[i]))
2725 {
2726 *pp = skipwhite(*pp + i);
2727 return TRUE;
2728 }
2729 return FALSE;
2730}
2731
2732/*
2733 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002734 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002736 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 * Returns NULL for an ambiguous user command.
2738 */
2739/*ARGSUSED*/
2740 static char_u *
2741find_command(eap, full)
2742 exarg_T *eap;
2743 int *full;
2744{
2745 int len;
2746 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002747 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
2749 /*
2750 * Isolate the command and search for it in the command table.
2751 * Exeptions:
2752 * - the 'k' command can directly be followed by any character.
2753 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2754 * but :sre[wind] is another command, as are :scrip[tnames],
2755 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002756 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 */
2758 p = eap->cmd;
2759 if (*p == 'k')
2760 {
2761 eap->cmdidx = CMD_k;
2762 ++p;
2763 }
2764 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002765 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2766 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 || p[1] == 'g'
2768 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2769 || p[1] == 'I'
2770 || (p[1] == 'r' && p[2] != 'e')))
2771 {
2772 eap->cmdidx = CMD_substitute;
2773 ++p;
2774 }
2775 else
2776 {
2777 while (ASCII_ISALPHA(*p))
2778 ++p;
2779 /* check for non-alpha command */
2780 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2781 ++p;
2782 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002783 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2784 {
2785 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2786 * :delete with the 'l' flag. Same for 'p'. */
2787 for (i = 0; i < len; ++i)
2788 if (eap->cmd[i] != "delete"[i])
2789 break;
2790 if (i == len - 1)
2791 {
2792 --len;
2793 if (p[-1] == 'l')
2794 eap->flags |= EXFLAG_LIST;
2795 else
2796 eap->flags |= EXFLAG_PRINT;
2797 }
2798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800 if (ASCII_ISLOWER(*eap->cmd))
2801 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2802 else
2803 eap->cmdidx = cmdidxs[26];
2804
2805 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2806 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2807 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2808 (size_t)len) == 0)
2809 {
2810#ifdef FEAT_EVAL
2811 if (full != NULL
2812 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2813 *full = TRUE;
2814#endif
2815 break;
2816 }
2817
2818#ifdef FEAT_USR_CMDS
2819 /* Look for a user defined command as a last resort */
2820 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2821 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002822 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 while (ASCII_ISALNUM(*p))
2824 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002825 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
2827#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002828 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 eap->cmdidx = CMD_SIZE;
2830 }
2831
2832 return p;
2833}
2834
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002835#ifdef FEAT_USR_CMDS
2836/*
2837 * Search for a user command that matches "eap->cmd".
2838 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2839 * Return a pointer to just after the command.
2840 * Return NULL if there is no matching command.
2841 */
2842 static char_u *
2843find_ucmd(eap, p, full, xp, compl)
2844 exarg_T *eap;
2845 char_u *p; /* end of the command (possibly including count) */
2846 int *full; /* set to TRUE for a full match */
2847 expand_T *xp; /* used for completion, NULL otherwise */
2848 int *compl; /* completion flags or NULL */
2849{
2850 int len = (int)(p - eap->cmd);
2851 int j, k, matchlen = 0;
2852 ucmd_T *uc;
2853 int found = FALSE;
2854 int possible = FALSE;
2855 char_u *cp, *np; /* Point into typed cmd and test name */
2856 garray_T *gap;
2857 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2858 only full match global is accepted. */
2859
2860 /*
2861 * Look for buffer-local user commands first, then global ones.
2862 */
2863 gap = &curbuf->b_ucmds;
2864 for (;;)
2865 {
2866 for (j = 0; j < gap->ga_len; ++j)
2867 {
2868 uc = USER_CMD_GA(gap, j);
2869 cp = eap->cmd;
2870 np = uc->uc_name;
2871 k = 0;
2872 while (k < len && *np != NUL && *cp++ == *np++)
2873 k++;
2874 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2875 {
2876 /* If finding a second match, the command is ambiguous. But
2877 * not if a buffer-local command wasn't a full match and a
2878 * global command is a full match. */
2879 if (k == len && found && *np != NUL)
2880 {
2881 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002882 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002883 amb_local = TRUE;
2884 }
2885
2886 if (!found || (k == len && *np == NUL))
2887 {
2888 /* If we matched up to a digit, then there could
2889 * be another command including the digit that we
2890 * should use instead.
2891 */
2892 if (k == len)
2893 found = TRUE;
2894 else
2895 possible = TRUE;
2896
2897 if (gap == &ucmds)
2898 eap->cmdidx = CMD_USER;
2899 else
2900 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002901 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002902 eap->useridx = j;
2903
2904# ifdef FEAT_CMDL_COMPL
2905 if (compl != NULL)
2906 *compl = uc->uc_compl;
2907# ifdef FEAT_EVAL
2908 if (xp != NULL)
2909 {
2910 xp->xp_arg = uc->uc_compl_arg;
2911 xp->xp_scriptID = uc->uc_scriptID;
2912 }
2913# endif
2914# endif
2915 /* Do not search for further abbreviations
2916 * if this is an exact match. */
2917 matchlen = k;
2918 if (k == len && *np == NUL)
2919 {
2920 if (full != NULL)
2921 *full = TRUE;
2922 amb_local = FALSE;
2923 break;
2924 }
2925 }
2926 }
2927 }
2928
2929 /* Stop if we found a full match or searched all. */
2930 if (j < gap->ga_len || gap == &ucmds)
2931 break;
2932 gap = &ucmds;
2933 }
2934
2935 /* Only found ambiguous matches. */
2936 if (amb_local)
2937 {
2938 if (xp != NULL)
2939 xp->xp_context = EXPAND_UNSUCCESSFUL;
2940 return NULL;
2941 }
2942
2943 /* The match we found may be followed immediately by a number. Move "p"
2944 * back to point to it. */
2945 if (found || possible)
2946 return p + (matchlen - len);
2947 return p;
2948}
2949#endif
2950
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951#if defined(FEAT_EVAL) || defined(PROTO)
2952/*
2953 * Return > 0 if an Ex command "name" exists.
2954 * Return 2 if there is an exact match.
2955 * Return 3 if there is an ambiguous match.
2956 */
2957 int
2958cmd_exists(name)
2959 char_u *name;
2960{
2961 exarg_T ea;
2962 int full = FALSE;
2963 int i;
2964 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00002965 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 static struct cmdmod
2967 {
2968 char *name;
2969 int minlen;
2970 } cmdmods[] = {
2971 {"aboveleft", 3},
2972 {"belowright", 3},
2973 {"botright", 2},
2974 {"browse", 3},
2975 {"confirm", 4},
2976 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002977 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 {"keepjumps", 5},
2979 {"keepmarks", 3},
2980 {"leftabove", 5},
2981 {"lockmarks", 3},
2982 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002983 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 {"silent", 3},
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002985 {"tab", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 {"topleft", 2},
2987 {"verbose", 4},
2988 {"vertical", 4},
2989 };
2990
2991 /* Check command modifiers. */
2992 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2993 {
2994 for (j = 0; name[j] != NUL; ++j)
2995 if (name[j] != cmdmods[i].name[j])
2996 break;
2997 if (name[j] == NUL && j >= cmdmods[i].minlen)
2998 return (cmdmods[i].name[j] == NUL ? 2 : 1);
2999 }
3000
Bram Moolenaara9587612006-05-04 21:47:50 +00003001 /* Check built-in commands and user defined commands.
3002 * For ":2match" and ":3match" we need to skip the number. */
3003 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003005 p = find_command(&ea, &full);
3006 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003008 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3009 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003010 if (*skipwhite(p) != NUL)
3011 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3013}
3014#endif
3015
3016/*
3017 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3018 * we don't need/want deleted. Maybe this could be done better if we didn't
3019 * repeat all this stuff. The only problem is that they may not stay
3020 * perfectly compatible with each other, but then the command line syntax
3021 * probably won't change that much -- webb.
3022 */
3023 char_u *
3024set_one_cmd_context(xp, buff)
3025 expand_T *xp;
3026 char_u *buff; /* buffer for command string */
3027{
3028 char_u *p;
3029 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003030 int len = 0;
3031 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3033 int compl = EXPAND_NOTHING;
3034#endif
3035#ifdef FEAT_CMDL_COMPL
3036 int delim;
3037#endif
3038 int forceit = FALSE;
3039 int usefilter = FALSE; /* filter instead of file name */
3040
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003041 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 xp->xp_pattern = buff;
3043 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003044 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045
3046/*
3047 * 2. skip comment lines and leading space, colons or bars
3048 */
3049 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3050 ;
3051 xp->xp_pattern = cmd;
3052
3053 if (*cmd == NUL)
3054 return NULL;
3055 if (*cmd == '"') /* ignore comment lines */
3056 {
3057 xp->xp_context = EXPAND_NOTHING;
3058 return NULL;
3059 }
3060
3061/*
3062 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3063 */
3064 cmd = skip_range(cmd, &xp->xp_context);
3065
3066/*
3067 * 4. parse command
3068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 xp->xp_pattern = cmd;
3070 if (*cmd == NUL)
3071 return NULL;
3072 if (*cmd == '"')
3073 {
3074 xp->xp_context = EXPAND_NOTHING;
3075 return NULL;
3076 }
3077
3078 if (*cmd == '|' || *cmd == '\n')
3079 return cmd + 1; /* There's another command */
3080
3081 /*
3082 * Isolate the command and search for it in the command table.
3083 * Exceptions:
3084 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003085 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3087 */
3088 if (*cmd == 'k' && cmd[1] != 'e')
3089 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003090 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 p = cmd + 1;
3092 }
3093 else
3094 {
3095 p = cmd;
3096 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3097 ++p;
3098 /* check for non-alpha command */
3099 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3100 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003101 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003103 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 {
3105 xp->xp_context = EXPAND_UNSUCCESSFUL;
3106 return NULL;
3107 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003108 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3109 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3110 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 break;
3112
3113#ifdef FEAT_USR_CMDS
3114 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3115 {
3116 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3117 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003118 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 }
3120#endif
3121 }
3122
3123 /*
3124 * If the cursor is touching the command, and it ends in an alpha-numeric
3125 * character, complete the command name.
3126 */
3127 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3128 return NULL;
3129
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003130 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 {
3132 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3133 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003134 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 p = cmd + 1;
3136 }
3137#ifdef FEAT_USR_CMDS
3138 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3139 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003140 ea.cmd = cmd;
3141 p = find_ucmd(&ea, p, NULL, xp,
3142# if defined(FEAT_CMDL_COMPL)
3143 &compl
3144# else
3145 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003147 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003148 if (p == NULL)
3149 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 }
3151#endif
3152 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003153 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
3155 /* Not still touching the command and it was an illegal one */
3156 xp->xp_context = EXPAND_UNSUCCESSFUL;
3157 return NULL;
3158 }
3159
3160 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3161
3162 if (*p == '!') /* forced commands */
3163 {
3164 forceit = TRUE;
3165 ++p;
3166 }
3167
3168/*
3169 * 5. parse arguments
3170 */
3171#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003172 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003174 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 arg = skipwhite(p);
3177
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003178 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
3180 if (*arg == '>') /* append */
3181 {
3182 if (*++arg == '>')
3183 ++arg;
3184 arg = skipwhite(arg);
3185 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003186 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
3188 ++arg;
3189 usefilter = TRUE;
3190 }
3191 }
3192
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003193 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
3195 usefilter = forceit; /* :r! filter if forced */
3196 if (*arg == '!') /* :r !filter */
3197 {
3198 ++arg;
3199 usefilter = TRUE;
3200 }
3201 }
3202
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003203 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
3205 while (*arg == *cmd) /* allow any number of '>' or '<' */
3206 ++arg;
3207 arg = skipwhite(arg);
3208 }
3209
3210 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003211 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 {
3213 /* Check if we're in the +command */
3214 p = arg + 1;
3215 arg = skip_cmd_arg(arg, FALSE);
3216
3217 /* Still touching the command after '+'? */
3218 if (*arg == NUL)
3219 return p;
3220
3221 /* Skip space(s) after +command to get to the real argument */
3222 arg = skipwhite(arg);
3223 }
3224
3225 /*
3226 * Check for '|' to separate commands and '"' to start comments.
3227 * Don't do this for ":read !cmd" and ":write !cmd".
3228 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003229 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
3231 p = arg;
3232 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003233 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 p += 2;
3235 while (*p)
3236 {
3237 if (*p == Ctrl_V)
3238 {
3239 if (p[1] != NUL)
3240 ++p;
3241 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003242 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 || *p == '|' || *p == '\n')
3244 {
3245 if (*(p - 1) != '\\')
3246 {
3247 if (*p == '|' || *p == '\n')
3248 return p + 1;
3249 return NULL; /* It's a comment */
3250 }
3251 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003252 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 }
3254 }
3255
3256 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003257 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 vim_strchr((char_u *)"|\"", *arg) == NULL)
3259 return NULL;
3260
3261 /* Find start of last argument (argument just before cursor): */
3262 p = buff + STRLEN(buff);
3263 while (p != arg && *p != ' ' && *p != TAB)
3264 p--;
3265 if (*p == ' ' || *p == TAB)
3266 p++;
3267 xp->xp_pattern = p;
3268
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003269 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
3271 int in_quote = FALSE;
3272 char_u *bow = NULL; /* Beginning of word */
3273
3274 /*
3275 * Allow spaces within back-quotes to count as part of the argument
3276 * being expanded.
3277 */
3278 xp->xp_pattern = skipwhite(arg);
3279 for (p = xp->xp_pattern; *p; )
3280 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003281 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 ++p;
3283#ifdef SPACE_IN_FILENAME
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003284 else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285#else
3286 else if (vim_iswhite(*p))
3287#endif
3288 {
3289 p = skipwhite(p);
3290 if (in_quote)
3291 bow = p;
3292 else
3293 xp->xp_pattern = p;
3294 --p;
3295 }
3296 else if (*p == '`')
3297 {
3298 if (!in_quote)
3299 {
3300 xp->xp_pattern = p;
3301 bow = p + 1;
3302 }
3303 in_quote = !in_quote;
3304 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003305 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
3307
3308 /*
3309 * If we are still inside the quotes, and we passed a space, just
3310 * expand from there.
3311 */
3312 if (bow != NULL && in_quote)
3313 xp->xp_pattern = bow;
3314 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003315
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003316#ifndef BACKSLASH_IN_FILENAME
3317 /* For a shell command more chars need to be escaped. */
3318 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003319 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003320 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003321
3322 /* When still after the command name expand executables. */
3323 if (xp->xp_pattern == skipwhite(arg))
3324 xp->xp_context = EXPAND_SHELLCMD;
3325 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
3328 /* Check for environment variable */
3329 if (*xp->xp_pattern == '$'
3330#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3331 || *xp->xp_pattern == '%'
3332#endif
3333 )
3334 {
3335 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3336 if (!vim_isIDc(*p))
3337 break;
3338 if (*p == NUL)
3339 {
3340 xp->xp_context = EXPAND_ENV_VARS;
3341 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003342#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3343 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003344 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003345 compl = EXPAND_ENV_VARS;
3346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 }
3348 }
3349 }
3350
3351/*
3352 * 6. switch on command name
3353 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003354 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 {
3356 case CMD_cd:
3357 case CMD_chdir:
3358 case CMD_lcd:
3359 case CMD_lchdir:
3360 if (xp->xp_context == EXPAND_FILES)
3361 xp->xp_context = EXPAND_DIRECTORIES;
3362 break;
3363 case CMD_help:
3364 xp->xp_context = EXPAND_HELP;
3365 xp->xp_pattern = arg;
3366 break;
3367
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003368 /* Command modifiers: return the argument.
3369 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003371 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 case CMD_belowright:
3373 case CMD_botright:
3374 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003375 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003377 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 case CMD_folddoclosed:
3379 case CMD_folddoopen:
3380 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003381 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 case CMD_keepjumps:
3383 case CMD_keepmarks:
3384 case CMD_leftabove:
3385 case CMD_lockmarks:
3386 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003387 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003389 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 case CMD_topleft:
3391 case CMD_verbose:
3392 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003393 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 return arg;
3395
3396#ifdef FEAT_SEARCH_EXTRA
3397 case CMD_match:
3398 if (*arg == NUL || !ends_excmd(*arg))
3399 {
3400 /* Dummy call to clear variables. */
3401 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3402 xp->xp_context = EXPAND_HIGHLIGHT;
3403 xp->xp_pattern = arg;
3404 arg = skipwhite(skiptowhite(arg));
3405 if (*arg != NUL)
3406 {
3407 xp->xp_context = EXPAND_NOTHING;
3408 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3409 }
3410 }
3411 return find_nextcmd(arg);
3412#endif
3413
3414#ifdef FEAT_CMDL_COMPL
3415/*
3416 * All completion for the +cmdline_compl feature goes here.
3417 */
3418
3419# ifdef FEAT_USR_CMDS
3420 case CMD_command:
3421 /* Check for attributes */
3422 while (*arg == '-')
3423 {
3424 arg++; /* Skip "-" */
3425 p = skiptowhite(arg);
3426 if (*p == NUL)
3427 {
3428 /* Cursor is still in the attribute */
3429 p = vim_strchr(arg, '=');
3430 if (p == NULL)
3431 {
3432 /* No "=", so complete attribute names */
3433 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3434 xp->xp_pattern = arg;
3435 return NULL;
3436 }
3437
3438 /* For the -complete and -nargs attributes, we complete
3439 * their arguments as well.
3440 */
3441 if (STRNICMP(arg, "complete", p - arg) == 0)
3442 {
3443 xp->xp_context = EXPAND_USER_COMPLETE;
3444 xp->xp_pattern = p + 1;
3445 return NULL;
3446 }
3447 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3448 {
3449 xp->xp_context = EXPAND_USER_NARGS;
3450 xp->xp_pattern = p + 1;
3451 return NULL;
3452 }
3453 return NULL;
3454 }
3455 arg = skipwhite(p);
3456 }
3457
3458 /* After the attributes comes the new command name */
3459 p = skiptowhite(arg);
3460 if (*p == NUL)
3461 {
3462 xp->xp_context = EXPAND_USER_COMMANDS;
3463 xp->xp_pattern = arg;
3464 break;
3465 }
3466
3467 /* And finally comes a normal command */
3468 return skipwhite(p);
3469
3470 case CMD_delcommand:
3471 xp->xp_context = EXPAND_USER_COMMANDS;
3472 xp->xp_pattern = arg;
3473 break;
3474# endif
3475
3476 case CMD_global:
3477 case CMD_vglobal:
3478 delim = *arg; /* get the delimiter */
3479 if (delim)
3480 ++arg; /* skip delimiter if there is one */
3481
3482 while (arg[0] != NUL && arg[0] != delim)
3483 {
3484 if (arg[0] == '\\' && arg[1] != NUL)
3485 ++arg;
3486 ++arg;
3487 }
3488 if (arg[0] != NUL)
3489 return arg + 1;
3490 break;
3491 case CMD_and:
3492 case CMD_substitute:
3493 delim = *arg;
3494 if (delim)
3495 {
3496 /* skip "from" part */
3497 ++arg;
3498 arg = skip_regexp(arg, delim, p_magic, NULL);
3499 }
3500 /* skip "to" part */
3501 while (arg[0] != NUL && arg[0] != delim)
3502 {
3503 if (arg[0] == '\\' && arg[1] != NUL)
3504 ++arg;
3505 ++arg;
3506 }
3507 if (arg[0] != NUL) /* skip delimiter */
3508 ++arg;
3509 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3510 ++arg;
3511 if (arg[0] != NUL)
3512 return arg;
3513 break;
3514 case CMD_isearch:
3515 case CMD_dsearch:
3516 case CMD_ilist:
3517 case CMD_dlist:
3518 case CMD_ijump:
3519 case CMD_psearch:
3520 case CMD_djump:
3521 case CMD_isplit:
3522 case CMD_dsplit:
3523 arg = skipwhite(skipdigits(arg)); /* skip count */
3524 if (*arg == '/') /* Match regexp, not just whole words */
3525 {
3526 for (++arg; *arg && *arg != '/'; arg++)
3527 if (*arg == '\\' && arg[1] != NUL)
3528 arg++;
3529 if (*arg)
3530 {
3531 arg = skipwhite(arg + 1);
3532
3533 /* Check for trailing illegal characters */
3534 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3535 xp->xp_context = EXPAND_NOTHING;
3536 else
3537 return arg;
3538 }
3539 }
3540 break;
3541#ifdef FEAT_AUTOCMD
3542 case CMD_autocmd:
3543 return set_context_in_autocmd(xp, arg, FALSE);
3544
3545 case CMD_doautocmd:
3546 return set_context_in_autocmd(xp, arg, TRUE);
3547#endif
3548 case CMD_set:
3549 set_context_in_set_cmd(xp, arg, 0);
3550 break;
3551 case CMD_setglobal:
3552 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3553 break;
3554 case CMD_setlocal:
3555 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3556 break;
3557 case CMD_tag:
3558 case CMD_stag:
3559 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003560 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 case CMD_tselect:
3562 case CMD_stselect:
3563 case CMD_ptselect:
3564 case CMD_tjump:
3565 case CMD_stjump:
3566 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003567 if (*p_wop != NUL)
3568 xp->xp_context = EXPAND_TAGS_LISTFILES;
3569 else
3570 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 xp->xp_pattern = arg;
3572 break;
3573 case CMD_augroup:
3574 xp->xp_context = EXPAND_AUGROUP;
3575 xp->xp_pattern = arg;
3576 break;
3577#ifdef FEAT_SYN_HL
3578 case CMD_syntax:
3579 set_context_in_syntax_cmd(xp, arg);
3580 break;
3581#endif
3582#ifdef FEAT_EVAL
3583 case CMD_let:
3584 case CMD_if:
3585 case CMD_elseif:
3586 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003587 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 case CMD_echo:
3589 case CMD_echon:
3590 case CMD_execute:
3591 case CMD_echomsg:
3592 case CMD_echoerr:
3593 case CMD_call:
3594 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003595 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 break;
3597
3598 case CMD_unlet:
3599 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3600 arg = xp->xp_pattern + 1;
3601 xp->xp_context = EXPAND_USER_VARS;
3602 xp->xp_pattern = arg;
3603 break;
3604
3605 case CMD_function:
3606 case CMD_delfunction:
3607 xp->xp_context = EXPAND_USER_FUNC;
3608 xp->xp_pattern = arg;
3609 break;
3610
3611 case CMD_echohl:
3612 xp->xp_context = EXPAND_HIGHLIGHT;
3613 xp->xp_pattern = arg;
3614 break;
3615#endif
3616 case CMD_highlight:
3617 set_context_in_highlight_cmd(xp, arg);
3618 break;
3619#ifdef FEAT_LISTCMDS
3620 case CMD_bdelete:
3621 case CMD_bwipeout:
3622 case CMD_bunload:
3623 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3624 arg = xp->xp_pattern + 1;
3625 /*FALLTHROUGH*/
3626 case CMD_buffer:
3627 case CMD_sbuffer:
3628 case CMD_checktime:
3629 xp->xp_context = EXPAND_BUFFERS;
3630 xp->xp_pattern = arg;
3631 break;
3632#endif
3633#ifdef FEAT_USR_CMDS
3634 case CMD_USER:
3635 case CMD_USER_BUF:
3636 if (compl != EXPAND_NOTHING)
3637 {
3638 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003639 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
3641# ifdef FEAT_MENU
3642 if (compl == EXPAND_MENUS)
3643 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3644# endif
3645 if (compl == EXPAND_COMMANDS)
3646 return arg;
3647 if (compl == EXPAND_MAPPINGS)
3648 return set_context_in_map_cmd(xp, (char_u *)"map",
3649 arg, forceit, FALSE, FALSE, CMD_map);
3650 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3651 arg = xp->xp_pattern + 1;
3652 xp->xp_pattern = arg;
3653 }
3654 xp->xp_context = compl;
3655 }
3656 break;
3657#endif
3658 case CMD_map: case CMD_noremap:
3659 case CMD_nmap: case CMD_nnoremap:
3660 case CMD_vmap: case CMD_vnoremap:
3661 case CMD_omap: case CMD_onoremap:
3662 case CMD_imap: case CMD_inoremap:
3663 case CMD_cmap: case CMD_cnoremap:
3664 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003665 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 case CMD_unmap:
3667 case CMD_nunmap:
3668 case CMD_vunmap:
3669 case CMD_ounmap:
3670 case CMD_iunmap:
3671 case CMD_cunmap:
3672 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003673 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 case CMD_abbreviate: case CMD_noreabbrev:
3675 case CMD_cabbrev: case CMD_cnoreabbrev:
3676 case CMD_iabbrev: case CMD_inoreabbrev:
3677 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003678 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 case CMD_unabbreviate:
3680 case CMD_cunabbrev:
3681 case CMD_iunabbrev:
3682 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003683 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684#ifdef FEAT_MENU
3685 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3686 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3687 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3688 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3689 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3690 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3691 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3692 case CMD_tmenu: case CMD_tunmenu:
3693 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3694 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3695#endif
3696
3697 case CMD_colorscheme:
3698 xp->xp_context = EXPAND_COLORS;
3699 xp->xp_pattern = arg;
3700 break;
3701
3702 case CMD_compiler:
3703 xp->xp_context = EXPAND_COMPILER;
3704 xp->xp_pattern = arg;
3705 break;
3706
3707#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3708 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3709 case CMD_language:
3710 if (*skiptowhite(arg) == NUL)
3711 {
3712 xp->xp_context = EXPAND_LANGUAGE;
3713 xp->xp_pattern = arg;
3714 }
3715 else
3716 xp->xp_context = EXPAND_NOTHING;
3717 break;
3718#endif
3719
3720#endif /* FEAT_CMDL_COMPL */
3721
3722 default:
3723 break;
3724 }
3725 return NULL;
3726}
3727
3728/*
3729 * skip a range specifier of the form: addr [,addr] [;addr] ..
3730 *
3731 * Backslashed delimiters after / or ? will be skipped, and commands will
3732 * not be expanded between /'s and ?'s or after "'".
3733 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003734 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 * Returns the "cmd" pointer advanced to beyond the range.
3736 */
3737 char_u *
3738skip_range(cmd, ctx)
3739 char_u *cmd;
3740 int *ctx; /* pointer to xp_context or NULL */
3741{
3742 int delim;
3743
Bram Moolenaardf177f62005-02-22 08:39:57 +00003744 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 {
3746 if (*cmd == '\'')
3747 {
3748 if (*++cmd == NUL && ctx != NULL)
3749 *ctx = EXPAND_NOTHING;
3750 }
3751 else if (*cmd == '/' || *cmd == '?')
3752 {
3753 delim = *cmd++;
3754 while (*cmd != NUL && *cmd != delim)
3755 if (*cmd++ == '\\' && *cmd != NUL)
3756 ++cmd;
3757 if (*cmd == NUL && ctx != NULL)
3758 *ctx = EXPAND_NOTHING;
3759 }
3760 if (*cmd != NUL)
3761 ++cmd;
3762 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003763
3764 /* Skip ":" and white space. */
3765 while (*cmd == ':')
3766 cmd = skipwhite(cmd + 1);
3767
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 return cmd;
3769}
3770
3771/*
3772 * get a single EX address
3773 *
3774 * Set ptr to the next character after the part that was interpreted.
3775 * Set ptr to NULL when an error is encountered.
3776 *
3777 * Return MAXLNUM when no Ex address was found.
3778 */
3779 static linenr_T
3780get_address(ptr, skip, to_other_file)
3781 char_u **ptr;
3782 int skip; /* only skip the address, don't use it */
3783 int to_other_file; /* flag: may jump to other file */
3784{
3785 int c;
3786 int i;
3787 long n;
3788 char_u *cmd;
3789 pos_T pos;
3790 pos_T *fp;
3791 linenr_T lnum;
3792
3793 cmd = skipwhite(*ptr);
3794 lnum = MAXLNUM;
3795 do
3796 {
3797 switch (*cmd)
3798 {
3799 case '.': /* '.' - Cursor position */
3800 ++cmd;
3801 lnum = curwin->w_cursor.lnum;
3802 break;
3803
3804 case '$': /* '$' - last line */
3805 ++cmd;
3806 lnum = curbuf->b_ml.ml_line_count;
3807 break;
3808
3809 case '\'': /* ''' - mark */
3810 if (*++cmd == NUL)
3811 {
3812 cmd = NULL;
3813 goto error;
3814 }
3815 if (skip)
3816 ++cmd;
3817 else
3818 {
3819 /* Only accept a mark in another file when it is
3820 * used by itself: ":'M". */
3821 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3822 ++cmd;
3823 if (fp == (pos_T *)-1)
3824 /* Jumped to another file. */
3825 lnum = curwin->w_cursor.lnum;
3826 else
3827 {
3828 if (check_mark(fp) == FAIL)
3829 {
3830 cmd = NULL;
3831 goto error;
3832 }
3833 lnum = fp->lnum;
3834 }
3835 }
3836 break;
3837
3838 case '/':
3839 case '?': /* '/' or '?' - search */
3840 c = *cmd++;
3841 if (skip) /* skip "/pat/" */
3842 {
3843 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3844 if (*cmd == c)
3845 ++cmd;
3846 }
3847 else
3848 {
3849 pos = curwin->w_cursor; /* save curwin->w_cursor */
3850 /*
3851 * When '/' or '?' follows another address, start
3852 * from there.
3853 */
3854 if (lnum != MAXLNUM)
3855 curwin->w_cursor.lnum = lnum;
3856 /*
3857 * Start a forward search at the end of the line.
3858 * Start a backward search at the start of the line.
3859 * This makes sure we never match in the current
3860 * line, and can match anywhere in the
3861 * next/previous line.
3862 */
3863 if (c == '/')
3864 curwin->w_cursor.col = MAXCOL;
3865 else
3866 curwin->w_cursor.col = 0;
3867 searchcmdlen = 0;
3868 if (!do_search(NULL, c, cmd, 1L,
3869 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3870 {
3871 curwin->w_cursor = pos;
3872 cmd = NULL;
3873 goto error;
3874 }
3875 lnum = curwin->w_cursor.lnum;
3876 curwin->w_cursor = pos;
3877 /* adjust command string pointer */
3878 cmd += searchcmdlen;
3879 }
3880 break;
3881
3882 case '\\': /* "\?", "\/" or "\&", repeat search */
3883 ++cmd;
3884 if (*cmd == '&')
3885 i = RE_SUBST;
3886 else if (*cmd == '?' || *cmd == '/')
3887 i = RE_SEARCH;
3888 else
3889 {
3890 EMSG(_(e_backslash));
3891 cmd = NULL;
3892 goto error;
3893 }
3894
3895 if (!skip)
3896 {
3897 /*
3898 * When search follows another address, start from
3899 * there.
3900 */
3901 if (lnum != MAXLNUM)
3902 pos.lnum = lnum;
3903 else
3904 pos.lnum = curwin->w_cursor.lnum;
3905
3906 /*
3907 * Start the search just like for the above
3908 * do_search().
3909 */
3910 if (*cmd != '?')
3911 pos.col = MAXCOL;
3912 else
3913 pos.col = 0;
3914 if (searchit(curwin, curbuf, &pos,
3915 *cmd == '?' ? BACKWARD : FORWARD,
3916 (char_u *)"", 1L,
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003917 SEARCH_MSG + SEARCH_START,
3918 i, (linenr_T)0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 lnum = pos.lnum;
3920 else
3921 {
3922 cmd = NULL;
3923 goto error;
3924 }
3925 }
3926 ++cmd;
3927 break;
3928
3929 default:
3930 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3931 lnum = getdigits(&cmd);
3932 }
3933
3934 for (;;)
3935 {
3936 cmd = skipwhite(cmd);
3937 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3938 break;
3939
3940 if (lnum == MAXLNUM)
3941 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3942 if (VIM_ISDIGIT(*cmd))
3943 i = '+'; /* "number" is same as "+number" */
3944 else
3945 i = *cmd++;
3946 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3947 n = 1;
3948 else
3949 n = getdigits(&cmd);
3950 if (i == '-')
3951 lnum -= n;
3952 else
3953 lnum += n;
3954 }
3955 } while (*cmd == '/' || *cmd == '?');
3956
3957error:
3958 *ptr = cmd;
3959 return lnum;
3960}
3961
3962/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003963 * Get flags from an Ex command argument.
3964 */
3965 static void
3966get_flags(eap)
3967 exarg_T *eap;
3968{
3969 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3970 {
3971 if (*eap->arg == 'l')
3972 eap->flags |= EXFLAG_LIST;
3973 else if (*eap->arg == 'p')
3974 eap->flags |= EXFLAG_PRINT;
3975 else
3976 eap->flags |= EXFLAG_NR;
3977 eap->arg = skipwhite(eap->arg + 1);
3978 }
3979}
3980
3981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 * Function called for command which is Not Implemented. NI!
3983 */
3984 void
3985ex_ni(eap)
3986 exarg_T *eap;
3987{
3988 if (!eap->skip)
3989 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3990}
3991
3992#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003993 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994/*
3995 * Function called for script command which is Not Implemented. NI!
3996 * Skips over ":perl <<EOF" constructs.
3997 */
3998 static void
3999ex_script_ni(eap)
4000 exarg_T *eap;
4001{
4002 if (!eap->skip)
4003 ex_ni(eap);
4004 else
4005 vim_free(script_get(eap, eap->arg));
4006}
4007#endif
4008
4009/*
4010 * Check range in Ex command for validity.
4011 * Return NULL when valid, error message when invalid.
4012 */
4013 static char_u *
4014invalid_range(eap)
4015 exarg_T *eap;
4016{
4017 if ( eap->line1 < 0
4018 || eap->line2 < 0
4019 || eap->line1 > eap->line2
4020 || ((eap->argt & RANGE)
4021 && !(eap->argt & NOTADR)
4022 && eap->line2 > curbuf->b_ml.ml_line_count
4023#ifdef FEAT_DIFF
4024 + (eap->cmdidx == CMD_diffget)
4025#endif
4026 ))
4027 return (char_u *)_(e_invrange);
4028 return NULL;
4029}
4030
4031/*
4032 * Correct the range for zero line number, if required.
4033 */
4034 static void
4035correct_range(eap)
4036 exarg_T *eap;
4037{
4038 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4039 {
4040 if (eap->line1 == 0)
4041 eap->line1 = 1;
4042 if (eap->line2 == 0)
4043 eap->line2 = 1;
4044 }
4045}
4046
Bram Moolenaar748bf032005-02-02 23:04:36 +00004047#ifdef FEAT_QUICKFIX
4048static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4049
4050/*
4051 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4052 * pattern. Otherwise return eap->arg.
4053 */
4054 static char_u *
4055skip_grep_pat(eap)
4056 exarg_T *eap;
4057{
4058 char_u *p = eap->arg;
4059
Bram Moolenaara37420f2006-02-04 22:37:47 +00004060 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4061 || eap->cmdidx == CMD_vimgrepadd
4062 || eap->cmdidx == CMD_lvimgrepadd
4063 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004064 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004065 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004066 if (p == NULL)
4067 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004068 }
4069 return p;
4070}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004071
4072/*
4073 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4074 * in the command line, so that things like % get expanded.
4075 */
4076 static char_u *
4077replace_makeprg(eap, p, cmdlinep)
4078 exarg_T *eap;
4079 char_u *p;
4080 char_u **cmdlinep;
4081{
4082 char_u *new_cmdline;
4083 char_u *program;
4084 char_u *pos;
4085 char_u *ptr;
4086 int len;
4087 int i;
4088
4089 /*
4090 * Don't do it when ":vimgrep" is used for ":grep".
4091 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004092 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4093 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4094 || eap->cmdidx == CMD_grepadd
4095 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004096 && !grep_internal(eap->cmdidx))
4097 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004098 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4099 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004100 {
4101 if (*curbuf->b_p_gp == NUL)
4102 program = p_gp;
4103 else
4104 program = curbuf->b_p_gp;
4105 }
4106 else
4107 {
4108 if (*curbuf->b_p_mp == NUL)
4109 program = p_mp;
4110 else
4111 program = curbuf->b_p_mp;
4112 }
4113
4114 p = skipwhite(p);
4115
4116 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4117 {
4118 /* replace $* by given arguments */
4119 i = 1;
4120 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4121 ++i;
4122 len = (int)STRLEN(p);
4123 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4124 if (new_cmdline == NULL)
4125 return NULL; /* out of memory */
4126 ptr = new_cmdline;
4127 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4128 {
4129 i = (int)(pos - program);
4130 STRNCPY(ptr, program, i);
4131 STRCPY(ptr += i, p);
4132 ptr += len;
4133 program = pos + 2;
4134 }
4135 STRCPY(ptr, program);
4136 }
4137 else
4138 {
4139 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4140 if (new_cmdline == NULL)
4141 return NULL; /* out of memory */
4142 STRCPY(new_cmdline, program);
4143 STRCAT(new_cmdline, " ");
4144 STRCAT(new_cmdline, p);
4145 }
4146 msg_make(p);
4147
4148 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4149 vim_free(*cmdlinep);
4150 *cmdlinep = new_cmdline;
4151 p = new_cmdline;
4152 }
4153 return p;
4154}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004155#endif
4156
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157/*
4158 * Expand file name in Ex command argument.
4159 * Return FAIL for failure, OK otherwise.
4160 */
4161 int
4162expand_filename(eap, cmdlinep, errormsgp)
4163 exarg_T *eap;
4164 char_u **cmdlinep;
4165 char_u **errormsgp;
4166{
4167 int has_wildcards; /* need to expand wildcards */
4168 char_u *repl;
4169 int srclen;
4170 char_u *p;
4171 int n;
4172
Bram Moolenaar748bf032005-02-02 23:04:36 +00004173#ifdef FEAT_QUICKFIX
4174 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4175 p = skip_grep_pat(eap);
4176#else
4177 p = eap->arg;
4178#endif
4179
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 /*
4181 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4182 * the file name contains a wildcard it should not cause expanding.
4183 * (it will be expanded anyway if there is a wildcard before replacing).
4184 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004185 has_wildcards = mch_has_wildcard(p);
4186 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004188#ifdef FEAT_EVAL
4189 /* Skip over `=expr`, wildcards in it are not expanded. */
4190 if (p[0] == '`' && p[1] == '=')
4191 {
4192 p += 2;
4193 (void)skip_expr(&p);
4194 if (*p == '`')
4195 ++p;
4196 continue;
4197 }
4198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 /*
4200 * Quick check if this cannot be the start of a special string.
4201 * Also removes backslash before '%', '#' and '<'.
4202 */
4203 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4204 {
4205 ++p;
4206 continue;
4207 }
4208
4209 /*
4210 * Try to find a match at this position.
4211 */
4212 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4213 if (*errormsgp != NULL) /* error detected */
4214 return FAIL;
4215 if (repl == NULL) /* no match found */
4216 {
4217 p += srclen;
4218 continue;
4219 }
4220
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004221 /* Wildcards won't be expanded below, the replacement is taken
4222 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4223 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4224 {
4225 char_u *l = repl;
4226
4227 repl = expand_env_save(repl);
4228 vim_free(l);
4229 }
4230
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 /* Need to escape white space et al. with a backslash. Don't do this
4232 * for shell commands (may have to use quotes instead). Don't do this
4233 * for non-unix systems when there is a single argument (spaces don't
4234 * separate arguments then). */
4235 if (!eap->usefilter
4236 && eap->cmdidx != CMD_bang
4237 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004238 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004240 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004242 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243#ifndef UNIX
4244 && !(eap->argt & NOSPC)
4245#endif
4246 )
4247 {
4248 char_u *l;
4249#ifdef BACKSLASH_IN_FILENAME
4250 /* Don't escape a backslash here, because rem_backslash() doesn't
4251 * remove it later. */
4252 static char_u *nobslash = (char_u *)" \t\"|";
4253# define ESCAPE_CHARS nobslash
4254#else
4255# define ESCAPE_CHARS escape_chars
4256#endif
4257
4258 for (l = repl; *l; ++l)
4259 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4260 {
4261 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4262 if (l != NULL)
4263 {
4264 vim_free(repl);
4265 repl = l;
4266 }
4267 break;
4268 }
4269 }
4270
4271 /* For a shell command a '!' must be escaped. */
4272 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004273 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 {
4275 char_u *l;
4276
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004277 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 if (l != NULL)
4279 {
4280 vim_free(repl);
4281 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004282 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 if (strstr((char *)p_sh, "sh") != NULL)
4284 {
4285 l = vim_strsave_escaped(repl, (char_u *)"!");
4286 if (l != NULL)
4287 {
4288 vim_free(repl);
4289 repl = l;
4290 }
4291 }
4292 }
4293 }
4294
4295 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4296 vim_free(repl);
4297 if (p == NULL)
4298 return FAIL;
4299 }
4300
4301 /*
4302 * One file argument: Expand wildcards.
4303 * Don't do this with ":r !command" or ":w !command".
4304 */
4305 if ((eap->argt & NOSPC) && !eap->usefilter)
4306 {
4307 /*
4308 * May do this twice:
4309 * 1. Replace environment variables.
4310 * 2. Replace any other wildcards, remove backslashes.
4311 */
4312 for (n = 1; n <= 2; ++n)
4313 {
4314 if (n == 2)
4315 {
4316#ifdef UNIX
4317 /*
4318 * Only for Unix we check for more than one file name.
4319 * For other systems spaces are considered to be part
4320 * of the file name.
4321 * Only check here if there is no wildcard, otherwise
4322 * ExpandOne() will check for errors. This allows
4323 * ":e `ls ve*.c`" on Unix.
4324 */
4325 if (!has_wildcards)
4326 for (p = eap->arg; *p; ++p)
4327 {
4328 /* skip escaped characters */
4329 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4330 ++p;
4331 else if (vim_iswhite(*p))
4332 {
4333 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4334 return FAIL;
4335 }
4336 }
4337#endif
4338
4339 /*
4340 * Halve the number of backslashes (this is Vi compatible).
4341 * For Unix and OS/2, when wildcards are expanded, this is
4342 * done by ExpandOne() below.
4343 */
4344#if defined(UNIX) || defined(OS2)
4345 if (!has_wildcards)
4346#endif
4347 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349
4350 if (has_wildcards)
4351 {
4352 if (n == 1)
4353 {
4354 /*
4355 * First loop: May expand environment variables. This
4356 * can be done much faster with expand_env() than with
4357 * something else (e.g., calling a shell).
4358 * After expanding environment variables, check again
4359 * if there are still wildcards present.
4360 */
4361 if (vim_strchr(eap->arg, '$') != NULL
4362 || vim_strchr(eap->arg, '~') != NULL)
4363 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004364 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4365 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 has_wildcards = mch_has_wildcard(NameBuff);
4367 p = NameBuff;
4368 }
4369 else
4370 p = NULL;
4371 }
4372 else /* n == 2 */
4373 {
4374 expand_T xpc;
4375
4376 ExpandInit(&xpc);
4377 xpc.xp_context = EXPAND_FILES;
4378 p = ExpandOne(&xpc, eap->arg, NULL,
4379 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4380 WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 if (p == NULL)
4382 return FAIL;
4383 }
4384 if (p != NULL)
4385 {
4386 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4387 p, cmdlinep);
4388 if (n == 2) /* p came from ExpandOne() */
4389 vim_free(p);
4390 }
4391 }
4392 }
4393 }
4394 return OK;
4395}
4396
4397/*
4398 * Replace part of the command line, keeping eap->cmd, eap->arg and
4399 * eap->nextcmd correct.
4400 * "src" points to the part that is to be replaced, of length "srclen".
4401 * "repl" is the replacement string.
4402 * Returns a pointer to the character after the replaced string.
4403 * Returns NULL for failure.
4404 */
4405 static char_u *
4406repl_cmdline(eap, src, srclen, repl, cmdlinep)
4407 exarg_T *eap;
4408 char_u *src;
4409 int srclen;
4410 char_u *repl;
4411 char_u **cmdlinep;
4412{
4413 int len;
4414 int i;
4415 char_u *new_cmdline;
4416
4417 /*
4418 * The new command line is build in new_cmdline[].
4419 * First allocate it.
4420 * Careful: a "+cmd" argument may have been NUL terminated.
4421 */
4422 len = (int)STRLEN(repl);
4423 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004424 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4426 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4427 return NULL; /* out of memory! */
4428
4429 /*
4430 * Copy the stuff before the expanded part.
4431 * Copy the expanded stuff.
4432 * Copy what came after the expanded part.
4433 * Copy the next commands, if there are any.
4434 */
4435 i = (int)(src - *cmdlinep); /* length of part before match */
4436 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004437
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 mch_memmove(new_cmdline + i, repl, (size_t)len);
4439 i += len; /* remember the end of the string */
4440 STRCPY(new_cmdline + i, src + srclen);
4441 src = new_cmdline + i; /* remember where to continue */
4442
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004443 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
4445 i = (int)STRLEN(new_cmdline) + 1;
4446 STRCPY(new_cmdline + i, eap->nextcmd);
4447 eap->nextcmd = new_cmdline + i;
4448 }
4449 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4450 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4451 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4452 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4453 vim_free(*cmdlinep);
4454 *cmdlinep = new_cmdline;
4455
4456 return src;
4457}
4458
4459/*
4460 * Check for '|' to separate commands and '"' to start comments.
4461 */
4462 void
4463separate_nextcmd(eap)
4464 exarg_T *eap;
4465{
4466 char_u *p;
4467
Bram Moolenaar86b68352004-12-27 21:59:20 +00004468#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004469 p = skip_grep_pat(eap);
4470#else
4471 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004472#endif
4473
4474 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 {
4476 if (*p == Ctrl_V)
4477 {
4478 if (eap->argt & (USECTRLV | XFILE))
4479 ++p; /* skip CTRL-V and next char */
4480 else
4481 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4482 if (*p == NUL) /* stop at NUL after CTRL-V */
4483 break;
4484 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004485
4486#ifdef FEAT_EVAL
4487 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004488 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004489 {
4490 p += 2;
4491 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004492 }
4493#endif
4494
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 /* Check for '"': start of comment or '|': next command */
4496 /* :@" and :*" do not start a comment!
4497 * :redir @" doesn't either. */
4498 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4499 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4500 || p != eap->arg)
4501 && (eap->cmdidx != CMD_redir
4502 || p != eap->arg + 1 || p[-1] != '@'))
4503 || *p == '|' || *p == '\n')
4504 {
4505 /*
4506 * We remove the '\' before the '|', unless USECTRLV is used
4507 * AND 'b' is present in 'cpoptions'.
4508 */
4509 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4510 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4511 {
4512 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4513 --p;
4514 }
4515 else
4516 {
4517 eap->nextcmd = check_nextcmd(p);
4518 *p = NUL;
4519 break;
4520 }
4521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004523
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4525 del_trailing_spaces(eap->arg);
4526}
4527
4528/*
4529 * get + command from ex argument
4530 */
4531 static char_u *
4532getargcmd(argp)
4533 char_u **argp;
4534{
4535 char_u *arg = *argp;
4536 char_u *command = NULL;
4537
4538 if (*arg == '+') /* +[command] */
4539 {
4540 ++arg;
4541 if (vim_isspace(*arg))
4542 command = dollar_command;
4543 else
4544 {
4545 command = arg;
4546 arg = skip_cmd_arg(command, TRUE);
4547 if (*arg != NUL)
4548 *arg++ = NUL; /* terminate command with NUL */
4549 }
4550
4551 arg = skipwhite(arg); /* skip over spaces */
4552 *argp = arg;
4553 }
4554 return command;
4555}
4556
4557/*
4558 * Find end of "+command" argument. Skip over "\ " and "\\".
4559 */
4560 static char_u *
4561skip_cmd_arg(p, rembs)
4562 char_u *p;
4563 int rembs; /* TRUE to halve the number of backslashes */
4564{
4565 while (*p && !vim_isspace(*p))
4566 {
4567 if (*p == '\\' && p[1] != NUL)
4568 {
4569 if (rembs)
4570 mch_memmove(p, p + 1, STRLEN(p));
4571 else
4572 ++p;
4573 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004574 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 }
4576 return p;
4577}
4578
4579/*
4580 * Get "++opt=arg" argument.
4581 * Return FAIL or OK.
4582 */
4583 static int
4584getargopt(eap)
4585 exarg_T *eap;
4586{
4587 char_u *arg = eap->arg + 2;
4588 int *pp = NULL;
4589#ifdef FEAT_MBYTE
4590 char_u *p;
4591#endif
4592
4593 /* ":edit ++[no]bin[ary] file" */
4594 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4595 {
4596 if (*arg == 'n')
4597 {
4598 arg += 2;
4599 eap->force_bin = FORCE_NOBIN;
4600 }
4601 else
4602 eap->force_bin = FORCE_BIN;
4603 if (!checkforcmd(&arg, "binary", 3))
4604 return FAIL;
4605 eap->arg = skipwhite(arg);
4606 return OK;
4607 }
4608
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004609 /* ":read ++edit file" */
4610 if (STRNCMP(arg, "edit", 4) == 0)
4611 {
4612 eap->read_edit = TRUE;
4613 eap->arg = skipwhite(arg + 4);
4614 return OK;
4615 }
4616
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (STRNCMP(arg, "ff", 2) == 0)
4618 {
4619 arg += 2;
4620 pp = &eap->force_ff;
4621 }
4622 else if (STRNCMP(arg, "fileformat", 10) == 0)
4623 {
4624 arg += 10;
4625 pp = &eap->force_ff;
4626 }
4627#ifdef FEAT_MBYTE
4628 else if (STRNCMP(arg, "enc", 3) == 0)
4629 {
4630 arg += 3;
4631 pp = &eap->force_enc;
4632 }
4633 else if (STRNCMP(arg, "encoding", 8) == 0)
4634 {
4635 arg += 8;
4636 pp = &eap->force_enc;
4637 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004638 else if (STRNCMP(arg, "bad", 3) == 0)
4639 {
4640 arg += 3;
4641 pp = &eap->bad_char;
4642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#endif
4644
4645 if (pp == NULL || *arg != '=')
4646 return FAIL;
4647
4648 ++arg;
4649 *pp = (int)(arg - eap->cmd);
4650 arg = skip_cmd_arg(arg, FALSE);
4651 eap->arg = skipwhite(arg);
4652 *arg = NUL;
4653
4654#ifdef FEAT_MBYTE
4655 if (pp == &eap->force_ff)
4656 {
4657#endif
4658 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4659 return FAIL;
4660#ifdef FEAT_MBYTE
4661 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004662 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 {
4664 /* Make 'fileencoding' lower case. */
4665 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4666 *p = TOLOWER_ASC(*p);
4667 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004668 else
4669 {
4670 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4671 * "drop". */
4672 p = eap->cmd + eap->bad_char;
4673 if (STRICMP(p, "keep") == 0)
4674 eap->bad_char = BAD_KEEP;
4675 else if (STRICMP(p, "drop") == 0)
4676 eap->bad_char = BAD_DROP;
4677 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4678 eap->bad_char = *p;
4679 else
4680 return FAIL;
4681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682#endif
4683
4684 return OK;
4685}
4686
4687/*
4688 * ":abbreviate" and friends.
4689 */
4690 static void
4691ex_abbreviate(eap)
4692 exarg_T *eap;
4693{
4694 do_exmap(eap, TRUE); /* almost the same as mapping */
4695}
4696
4697/*
4698 * ":map" and friends.
4699 */
4700 static void
4701ex_map(eap)
4702 exarg_T *eap;
4703{
4704 /*
4705 * If we are sourcing .exrc or .vimrc in current directory we
4706 * print the mappings for security reasons.
4707 */
4708 if (secure)
4709 {
4710 secure = 2;
4711 msg_outtrans(eap->cmd);
4712 msg_putchar('\n');
4713 }
4714 do_exmap(eap, FALSE);
4715}
4716
4717/*
4718 * ":unmap" and friends.
4719 */
4720 static void
4721ex_unmap(eap)
4722 exarg_T *eap;
4723{
4724 do_exmap(eap, FALSE);
4725}
4726
4727/*
4728 * ":mapclear" and friends.
4729 */
4730 static void
4731ex_mapclear(eap)
4732 exarg_T *eap;
4733{
4734 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4735}
4736
4737/*
4738 * ":abclear" and friends.
4739 */
4740 static void
4741ex_abclear(eap)
4742 exarg_T *eap;
4743{
4744 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4745}
4746
4747#ifdef FEAT_AUTOCMD
4748 static void
4749ex_autocmd(eap)
4750 exarg_T *eap;
4751{
4752 /*
4753 * Disallow auto commands from .exrc and .vimrc in current
4754 * directory for security reasons.
4755 */
4756 if (secure)
4757 {
4758 secure = 2;
4759 eap->errmsg = e_curdir;
4760 }
4761 else if (eap->cmdidx == CMD_autocmd)
4762 do_autocmd(eap->arg, eap->forceit);
4763 else
4764 do_augroup(eap->arg, eap->forceit);
4765}
4766
4767/*
4768 * ":doautocmd": Apply the automatic commands to the current buffer.
4769 */
4770 static void
4771ex_doautocmd(eap)
4772 exarg_T *eap;
4773{
4774 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004775 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776}
4777#endif
4778
4779#ifdef FEAT_LISTCMDS
4780/*
4781 * :[N]bunload[!] [N] [bufname] unload buffer
4782 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4783 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4784 */
4785 static void
4786ex_bunload(eap)
4787 exarg_T *eap;
4788{
4789 eap->errmsg = do_bufdel(
4790 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4791 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4792 : DOBUF_UNLOAD, eap->arg,
4793 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4794}
4795
4796/*
4797 * :[N]buffer [N] to buffer N
4798 * :[N]sbuffer [N] to buffer N
4799 */
4800 static void
4801ex_buffer(eap)
4802 exarg_T *eap;
4803{
4804 if (*eap->arg)
4805 eap->errmsg = e_trailing;
4806 else
4807 {
4808 if (eap->addr_count == 0) /* default is current buffer */
4809 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4810 else
4811 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4812 }
4813}
4814
4815/*
4816 * :[N]bmodified [N] to next mod. buffer
4817 * :[N]sbmodified [N] to next mod. buffer
4818 */
4819 static void
4820ex_bmodified(eap)
4821 exarg_T *eap;
4822{
4823 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4824}
4825
4826/*
4827 * :[N]bnext [N] to next buffer
4828 * :[N]sbnext [N] split and to next buffer
4829 */
4830 static void
4831ex_bnext(eap)
4832 exarg_T *eap;
4833{
4834 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4835}
4836
4837/*
4838 * :[N]bNext [N] to previous buffer
4839 * :[N]bprevious [N] to previous buffer
4840 * :[N]sbNext [N] split and to previous buffer
4841 * :[N]sbprevious [N] split and to previous buffer
4842 */
4843 static void
4844ex_bprevious(eap)
4845 exarg_T *eap;
4846{
4847 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4848}
4849
4850/*
4851 * :brewind to first buffer
4852 * :bfirst to first buffer
4853 * :sbrewind split and to first buffer
4854 * :sbfirst split and to first buffer
4855 */
4856 static void
4857ex_brewind(eap)
4858 exarg_T *eap;
4859{
4860 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4861}
4862
4863/*
4864 * :blast to last buffer
4865 * :sblast split and to last buffer
4866 */
4867 static void
4868ex_blast(eap)
4869 exarg_T *eap;
4870{
4871 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4872}
4873#endif
4874
4875 int
4876ends_excmd(c)
4877 int c;
4878{
4879 return (c == NUL || c == '|' || c == '"' || c == '\n');
4880}
4881
4882#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4883 || defined(PROTO)
4884/*
4885 * Return the next command, after the first '|' or '\n'.
4886 * Return NULL if not found.
4887 */
4888 char_u *
4889find_nextcmd(p)
4890 char_u *p;
4891{
4892 while (*p != '|' && *p != '\n')
4893 {
4894 if (*p == NUL)
4895 return NULL;
4896 ++p;
4897 }
4898 return (p + 1);
4899}
4900#endif
4901
4902/*
4903 * Check if *p is a separator between Ex commands.
4904 * Return NULL if it isn't, (p + 1) if it is.
4905 */
4906 char_u *
4907check_nextcmd(p)
4908 char_u *p;
4909{
4910 p = skipwhite(p);
4911 if (*p == '|' || *p == '\n')
4912 return (p + 1);
4913 else
4914 return NULL;
4915}
4916
4917/*
4918 * - if there are more files to edit
4919 * - and this is the last window
4920 * - and forceit not used
4921 * - and not repeated twice on a row
4922 * return FAIL and give error message if 'message' TRUE
4923 * return OK otherwise
4924 */
4925 static int
4926check_more(message, forceit)
4927 int message; /* when FALSE check only, no messages */
4928 int forceit;
4929{
4930 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4931
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004932 if (!forceit && only_one_window()
4933 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 {
4935 if (message)
4936 {
4937#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4938 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4939 {
4940 char_u buff[IOSIZE];
4941
4942 if (n == 1)
4943 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4944 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004945 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 _("%d more files to edit. Quit anyway?"), n);
4947 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4948 return OK;
4949 return FAIL;
4950 }
4951#endif
4952 if (n == 1)
4953 EMSG(_("E173: 1 more file to edit"));
4954 else
4955 EMSGN(_("E173: %ld more files to edit"), n);
4956 quitmore = 2; /* next try to quit is allowed */
4957 }
4958 return FAIL;
4959 }
4960 return OK;
4961}
4962
4963#ifdef FEAT_CMDL_COMPL
4964/*
4965 * Function given to ExpandGeneric() to obtain the list of command names.
4966 */
4967/*ARGSUSED*/
4968 char_u *
4969get_command_name(xp, idx)
4970 expand_T *xp;
4971 int idx;
4972{
4973 if (idx >= (int)CMD_SIZE)
4974# ifdef FEAT_USR_CMDS
4975 return get_user_command_name(idx);
4976# else
4977 return NULL;
4978# endif
4979 return cmdnames[idx].cmd_name;
4980}
4981#endif
4982
4983#if defined(FEAT_USR_CMDS) || defined(PROTO)
4984static 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));
4985static void uc_list __ARGS((char_u *name, size_t name_len));
4986static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4987static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4988static 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));
4989
4990 static int
4991uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4992 char_u *name;
4993 size_t name_len;
4994 char_u *rep;
4995 long argt;
4996 long def;
4997 int flags;
4998 int compl;
4999 char_u *compl_arg;
5000 int force;
5001{
5002 ucmd_T *cmd = NULL;
5003 char_u *p;
5004 int i;
5005 int cmp = 1;
5006 char_u *rep_buf = NULL;
5007 garray_T *gap;
5008
Bram Moolenaar9c102382006-05-03 21:26:49 +00005009 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 if (rep_buf == NULL)
5011 {
5012 /* Can't replace termcodes - try using the string as is */
5013 rep_buf = vim_strsave(rep);
5014
5015 /* Give up if out of memory */
5016 if (rep_buf == NULL)
5017 return FAIL;
5018 }
5019
5020 /* get address of growarray: global or in curbuf */
5021 if (flags & UC_BUFFER)
5022 {
5023 gap = &curbuf->b_ucmds;
5024 if (gap->ga_itemsize == 0)
5025 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5026 }
5027 else
5028 gap = &ucmds;
5029
5030 /* Search for the command in the already defined commands. */
5031 for (i = 0; i < gap->ga_len; ++i)
5032 {
5033 size_t len;
5034
5035 cmd = USER_CMD_GA(gap, i);
5036 len = STRLEN(cmd->uc_name);
5037 cmp = STRNCMP(name, cmd->uc_name, name_len);
5038 if (cmp == 0)
5039 {
5040 if (name_len < len)
5041 cmp = -1;
5042 else if (name_len > len)
5043 cmp = 1;
5044 }
5045
5046 if (cmp == 0)
5047 {
5048 if (!force)
5049 {
5050 EMSG(_("E174: Command already exists: add ! to replace it"));
5051 goto fail;
5052 }
5053
5054 vim_free(cmd->uc_rep);
5055 cmd->uc_rep = 0;
5056 break;
5057 }
5058
5059 /* Stop as soon as we pass the name to add */
5060 if (cmp < 0)
5061 break;
5062 }
5063
5064 /* Extend the array unless we're replacing an existing command */
5065 if (cmp != 0)
5066 {
5067 if (ga_grow(gap, 1) != OK)
5068 goto fail;
5069 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5070 goto fail;
5071
5072 cmd = USER_CMD_GA(gap, i);
5073 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5074
5075 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076
5077 cmd->uc_name = p;
5078 }
5079
5080 cmd->uc_rep = rep_buf;
5081 cmd->uc_argt = argt;
5082 cmd->uc_def = def;
5083 cmd->uc_compl = compl;
5084#ifdef FEAT_EVAL
5085 cmd->uc_scriptID = current_SID;
5086# ifdef FEAT_CMDL_COMPL
5087 cmd->uc_compl_arg = compl_arg;
5088# endif
5089#endif
5090
5091 return OK;
5092
5093fail:
5094 vim_free(rep_buf);
5095#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5096 vim_free(compl_arg);
5097#endif
5098 return FAIL;
5099}
5100
5101/*
5102 * List of names for completion for ":command" with the EXPAND_ flag.
5103 * Must be alphabetical for completion.
5104 */
5105static struct
5106{
5107 int expand;
5108 char *name;
5109} command_complete[] =
5110{
5111 {EXPAND_AUGROUP, "augroup"},
5112 {EXPAND_BUFFERS, "buffer"},
5113 {EXPAND_COMMANDS, "command"},
5114#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5115 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005116 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117#endif
5118 {EXPAND_DIRECTORIES, "dir"},
5119 {EXPAND_ENV_VARS, "environment"},
5120 {EXPAND_EVENTS, "event"},
5121 {EXPAND_EXPRESSION, "expression"},
5122 {EXPAND_FILES, "file"},
5123 {EXPAND_FUNCTIONS, "function"},
5124 {EXPAND_HELP, "help"},
5125 {EXPAND_HIGHLIGHT, "highlight"},
5126 {EXPAND_MAPPINGS, "mapping"},
5127 {EXPAND_MENUS, "menu"},
5128 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005129 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {EXPAND_TAGS, "tag"},
5131 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5132 {EXPAND_USER_VARS, "var"},
5133 {0, NULL}
5134};
5135
5136 static void
5137uc_list(name, name_len)
5138 char_u *name;
5139 size_t name_len;
5140{
5141 int i, j;
5142 int found = FALSE;
5143 ucmd_T *cmd;
5144 int len;
5145 long a;
5146 garray_T *gap;
5147
5148 gap = &curbuf->b_ucmds;
5149 for (;;)
5150 {
5151 for (i = 0; i < gap->ga_len; ++i)
5152 {
5153 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005154 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155
5156 /* Skip commands which don't match the requested prefix */
5157 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5158 continue;
5159
5160 /* Put out the title first time */
5161 if (!found)
5162 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5163 found = TRUE;
5164 msg_putchar('\n');
5165 if (got_int)
5166 break;
5167
5168 /* Special cases */
5169 msg_putchar(a & BANG ? '!' : ' ');
5170 msg_putchar(a & REGSTR ? '"' : ' ');
5171 msg_putchar(gap != &ucmds ? 'b' : ' ');
5172 msg_putchar(' ');
5173
5174 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5175 len = (int)STRLEN(cmd->uc_name) + 4;
5176
5177 do {
5178 msg_putchar(' ');
5179 ++len;
5180 } while (len < 16);
5181
5182 len = 0;
5183
5184 /* Arguments */
5185 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5186 {
5187 case 0: IObuff[len++] = '0'; break;
5188 case (EXTRA): IObuff[len++] = '*'; break;
5189 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5190 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5191 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5192 }
5193
5194 do {
5195 IObuff[len++] = ' ';
5196 } while (len < 5);
5197
5198 /* Range */
5199 if (a & (RANGE|COUNT))
5200 {
5201 if (a & COUNT)
5202 {
5203 /* -count=N */
5204 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5205 len += (int)STRLEN(IObuff + len);
5206 }
5207 else if (a & DFLALL)
5208 IObuff[len++] = '%';
5209 else if (cmd->uc_def >= 0)
5210 {
5211 /* -range=N */
5212 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5213 len += (int)STRLEN(IObuff + len);
5214 }
5215 else
5216 IObuff[len++] = '.';
5217 }
5218
5219 do {
5220 IObuff[len++] = ' ';
5221 } while (len < 11);
5222
5223 /* Completion */
5224 for (j = 0; command_complete[j].expand != 0; ++j)
5225 if (command_complete[j].expand == cmd->uc_compl)
5226 {
5227 STRCPY(IObuff + len, command_complete[j].name);
5228 len += (int)STRLEN(IObuff + len);
5229 break;
5230 }
5231
5232 do {
5233 IObuff[len++] = ' ';
5234 } while (len < 21);
5235
5236 IObuff[len] = '\0';
5237 msg_outtrans(IObuff);
5238
5239 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005240#ifdef FEAT_EVAL
5241 if (p_verbose > 0)
5242 last_set_msg(cmd->uc_scriptID);
5243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 out_flush();
5245 ui_breakcheck();
5246 if (got_int)
5247 break;
5248 }
5249 if (gap == &ucmds || i < gap->ga_len)
5250 break;
5251 gap = &ucmds;
5252 }
5253
5254 if (!found)
5255 MSG(_("No user-defined commands found"));
5256}
5257
5258 static char_u *
5259uc_fun_cmd()
5260{
5261 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5262 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5263 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5264 0xb9, 0x7f, 0};
5265 int i;
5266
5267 for (i = 0; fcmd[i]; ++i)
5268 IObuff[i] = fcmd[i] - 0x40;
5269 IObuff[i] = 0;
5270 return IObuff;
5271}
5272
5273 static int
5274uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5275 char_u *attr;
5276 size_t len;
5277 long *argt;
5278 long *def;
5279 int *flags;
5280 int *compl;
5281 char_u **compl_arg;
5282{
5283 char_u *p;
5284
5285 if (len == 0)
5286 {
5287 EMSG(_("E175: No attribute specified"));
5288 return FAIL;
5289 }
5290
5291 /* First, try the simple attributes (no arguments) */
5292 if (STRNICMP(attr, "bang", len) == 0)
5293 *argt |= BANG;
5294 else if (STRNICMP(attr, "buffer", len) == 0)
5295 *flags |= UC_BUFFER;
5296 else if (STRNICMP(attr, "register", len) == 0)
5297 *argt |= REGSTR;
5298 else if (STRNICMP(attr, "bar", len) == 0)
5299 *argt |= TRLBAR;
5300 else
5301 {
5302 int i;
5303 char_u *val = NULL;
5304 size_t vallen = 0;
5305 size_t attrlen = len;
5306
5307 /* Look for the attribute name - which is the part before any '=' */
5308 for (i = 0; i < (int)len; ++i)
5309 {
5310 if (attr[i] == '=')
5311 {
5312 val = &attr[i + 1];
5313 vallen = len - i - 1;
5314 attrlen = i;
5315 break;
5316 }
5317 }
5318
5319 if (STRNICMP(attr, "nargs", attrlen) == 0)
5320 {
5321 if (vallen == 1)
5322 {
5323 if (*val == '0')
5324 /* Do nothing - this is the default */;
5325 else if (*val == '1')
5326 *argt |= (EXTRA | NOSPC | NEEDARG);
5327 else if (*val == '*')
5328 *argt |= EXTRA;
5329 else if (*val == '?')
5330 *argt |= (EXTRA | NOSPC);
5331 else if (*val == '+')
5332 *argt |= (EXTRA | NEEDARG);
5333 else
5334 goto wrong_nargs;
5335 }
5336 else
5337 {
5338wrong_nargs:
5339 EMSG(_("E176: Invalid number of arguments"));
5340 return FAIL;
5341 }
5342 }
5343 else if (STRNICMP(attr, "range", attrlen) == 0)
5344 {
5345 *argt |= RANGE;
5346 if (vallen == 1 && *val == '%')
5347 *argt |= DFLALL;
5348 else if (val != NULL)
5349 {
5350 p = val;
5351 if (*def >= 0)
5352 {
5353two_count:
5354 EMSG(_("E177: Count cannot be specified twice"));
5355 return FAIL;
5356 }
5357
5358 *def = getdigits(&p);
5359 *argt |= (ZEROR | NOTADR);
5360
5361 if (p != val + vallen || vallen == 0)
5362 {
5363invalid_count:
5364 EMSG(_("E178: Invalid default value for count"));
5365 return FAIL;
5366 }
5367 }
5368 }
5369 else if (STRNICMP(attr, "count", attrlen) == 0)
5370 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005371 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
5373 if (val != NULL)
5374 {
5375 p = val;
5376 if (*def >= 0)
5377 goto two_count;
5378
5379 *def = getdigits(&p);
5380
5381 if (p != val + vallen)
5382 goto invalid_count;
5383 }
5384
5385 if (*def < 0)
5386 *def = 0;
5387 }
5388 else if (STRNICMP(attr, "complete", attrlen) == 0)
5389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 if (val == NULL)
5391 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005392 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 return FAIL;
5394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005396 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5397 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 }
5400 else
5401 {
5402 char_u ch = attr[len];
5403 attr[len] = '\0';
5404 EMSG2(_("E181: Invalid attribute: %s"), attr);
5405 attr[len] = ch;
5406 return FAIL;
5407 }
5408 }
5409
5410 return OK;
5411}
5412
5413 static void
5414ex_command(eap)
5415 exarg_T *eap;
5416{
5417 char_u *name;
5418 char_u *end;
5419 char_u *p;
5420 long argt = 0;
5421 long def = -1;
5422 int flags = 0;
5423 int compl = EXPAND_NOTHING;
5424 char_u *compl_arg = NULL;
5425 int has_attr = (eap->arg[0] == '-');
5426
5427 p = eap->arg;
5428
5429 /* Check for attributes */
5430 while (*p == '-')
5431 {
5432 ++p;
5433 end = skiptowhite(p);
5434 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5435 == FAIL)
5436 return;
5437 p = skipwhite(end);
5438 }
5439
5440 /* Get the name (if any) and skip to the following argument */
5441 name = p;
5442 if (ASCII_ISALPHA(*p))
5443 while (ASCII_ISALNUM(*p))
5444 ++p;
5445 if (!ends_excmd(*p) && !vim_iswhite(*p))
5446 {
5447 EMSG(_("E182: Invalid command name"));
5448 return;
5449 }
5450 end = p;
5451
5452 /* If there is nothing after the name, and no attributes were specified,
5453 * we are listing commands
5454 */
5455 p = skipwhite(end);
5456 if (!has_attr && ends_excmd(*p))
5457 {
5458 uc_list(name, end - name);
5459 }
5460 else if (!ASCII_ISUPPER(*name))
5461 {
5462 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5463 return;
5464 }
5465 else
5466 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5467 eap->forceit);
5468}
5469
5470/*
5471 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 * Clear all user commands, global and for current buffer.
5473 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005474/*ARGSUSED*/
5475 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476ex_comclear(eap)
5477 exarg_T *eap;
5478{
5479 uc_clear(&ucmds);
5480 uc_clear(&curbuf->b_ucmds);
5481}
5482
5483/*
5484 * Clear all user commands for "gap".
5485 */
5486 void
5487uc_clear(gap)
5488 garray_T *gap;
5489{
5490 int i;
5491 ucmd_T *cmd;
5492
5493 for (i = 0; i < gap->ga_len; ++i)
5494 {
5495 cmd = USER_CMD_GA(gap, i);
5496 vim_free(cmd->uc_name);
5497 vim_free(cmd->uc_rep);
5498# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5499 vim_free(cmd->uc_compl_arg);
5500# endif
5501 }
5502 ga_clear(gap);
5503}
5504
5505 static void
5506ex_delcommand(eap)
5507 exarg_T *eap;
5508{
5509 int i = 0;
5510 ucmd_T *cmd = NULL;
5511 int cmp = -1;
5512 garray_T *gap;
5513
5514 gap = &curbuf->b_ucmds;
5515 for (;;)
5516 {
5517 for (i = 0; i < gap->ga_len; ++i)
5518 {
5519 cmd = USER_CMD_GA(gap, i);
5520 cmp = STRCMP(eap->arg, cmd->uc_name);
5521 if (cmp <= 0)
5522 break;
5523 }
5524 if (gap == &ucmds || cmp == 0)
5525 break;
5526 gap = &ucmds;
5527 }
5528
5529 if (cmp != 0)
5530 {
5531 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5532 return;
5533 }
5534
5535 vim_free(cmd->uc_name);
5536 vim_free(cmd->uc_rep);
5537# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5538 vim_free(cmd->uc_compl_arg);
5539# endif
5540
5541 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542
5543 if (i < gap->ga_len)
5544 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5545}
5546
5547 static char_u *
5548uc_split_args(arg, lenp)
5549 char_u *arg;
5550 size_t *lenp;
5551{
5552 char_u *buf;
5553 char_u *p;
5554 char_u *q;
5555 int len;
5556
5557 /* Precalculate length */
5558 p = arg;
5559 len = 2; /* Initial and final quotes */
5560
5561 while (*p)
5562 {
5563 if (p[0] == '\\' && vim_iswhite(p[1]))
5564 {
5565 len += 1;
5566 p += 2;
5567 }
5568 else if (*p == '\\' || *p == '"')
5569 {
5570 len += 2;
5571 p += 1;
5572 }
5573 else if (vim_iswhite(*p))
5574 {
5575 p = skipwhite(p);
5576 if (*p == NUL)
5577 break;
5578 len += 3; /* "," */
5579 }
5580 else
5581 {
5582 ++len;
5583 ++p;
5584 }
5585 }
5586
5587 buf = alloc(len + 1);
5588 if (buf == NULL)
5589 {
5590 *lenp = 0;
5591 return buf;
5592 }
5593
5594 p = arg;
5595 q = buf;
5596 *q++ = '"';
5597 while (*p)
5598 {
5599 if (p[0] == '\\' && vim_iswhite(p[1]))
5600 {
5601 *q++ = p[1];
5602 p += 2;
5603 }
5604 else if (*p == '\\' || *p == '"')
5605 {
5606 *q++ = '\\';
5607 *q++ = *p++;
5608 }
5609 else if (vim_iswhite(*p))
5610 {
5611 p = skipwhite(p);
5612 if (*p == NUL)
5613 break;
5614 *q++ = '"';
5615 *q++ = ',';
5616 *q++ = '"';
5617 }
5618 else
5619 {
5620 *q++ = *p++;
5621 }
5622 }
5623 *q++ = '"';
5624 *q = 0;
5625
5626 *lenp = len;
5627 return buf;
5628}
5629
5630/*
5631 * Check for a <> code in a user command.
5632 * "code" points to the '<'. "len" the length of the <> (inclusive).
5633 * "buf" is where the result is to be added.
5634 * "split_buf" points to a buffer used for splitting, caller should free it.
5635 * "split_len" is the length of what "split_buf" contains.
5636 * Returns the length of the replacement, which has been added to "buf".
5637 * Returns -1 if there was no match, and only the "<" has been copied.
5638 */
5639 static size_t
5640uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5641 char_u *code;
5642 size_t len;
5643 char_u *buf;
5644 ucmd_T *cmd; /* the user command we're expanding */
5645 exarg_T *eap; /* ex arguments */
5646 char_u **split_buf;
5647 size_t *split_len;
5648{
5649 size_t result = 0;
5650 char_u *p = code + 1;
5651 size_t l = len - 2;
5652 int quote = 0;
5653 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5654 ct_LT, ct_NONE } type = ct_NONE;
5655
5656 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5657 {
5658 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5659 p += 2;
5660 l -= 2;
5661 }
5662
Bram Moolenaar371d5402006-03-20 21:47:49 +00005663 ++l;
5664 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005666 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005668 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005670 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005672 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005674 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005676 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005678 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 type = ct_REGISTER;
5680
5681 switch (type)
5682 {
5683 case ct_ARGS:
5684 /* Simple case first */
5685 if (*eap->arg == NUL)
5686 {
5687 if (quote == 1)
5688 {
5689 result = 2;
5690 if (buf != NULL)
5691 STRCPY(buf, "''");
5692 }
5693 else
5694 result = 0;
5695 break;
5696 }
5697
5698 /* When specified there is a single argument don't split it.
5699 * Works for ":Cmd %" when % is "a b c". */
5700 if ((eap->argt & NOSPC) && quote == 2)
5701 quote = 1;
5702
5703 switch (quote)
5704 {
5705 case 0: /* No quoting, no splitting */
5706 result = STRLEN(eap->arg);
5707 if (buf != NULL)
5708 STRCPY(buf, eap->arg);
5709 break;
5710 case 1: /* Quote, but don't split */
5711 result = STRLEN(eap->arg) + 2;
5712 for (p = eap->arg; *p; ++p)
5713 {
5714 if (*p == '\\' || *p == '"')
5715 ++result;
5716 }
5717
5718 if (buf != NULL)
5719 {
5720 *buf++ = '"';
5721 for (p = eap->arg; *p; ++p)
5722 {
5723 if (*p == '\\' || *p == '"')
5724 *buf++ = '\\';
5725 *buf++ = *p;
5726 }
5727 *buf = '"';
5728 }
5729
5730 break;
5731 case 2: /* Quote and split */
5732 /* This is hard, so only do it once, and cache the result */
5733 if (*split_buf == NULL)
5734 *split_buf = uc_split_args(eap->arg, split_len);
5735
5736 result = *split_len;
5737 if (buf != NULL && result != 0)
5738 STRCPY(buf, *split_buf);
5739
5740 break;
5741 }
5742 break;
5743
5744 case ct_BANG:
5745 result = eap->forceit ? 1 : 0;
5746 if (quote)
5747 result += 2;
5748 if (buf != NULL)
5749 {
5750 if (quote)
5751 *buf++ = '"';
5752 if (eap->forceit)
5753 *buf++ = '!';
5754 if (quote)
5755 *buf = '"';
5756 }
5757 break;
5758
5759 case ct_LINE1:
5760 case ct_LINE2:
5761 case ct_COUNT:
5762 {
5763 char num_buf[20];
5764 long num = (type == ct_LINE1) ? eap->line1 :
5765 (type == ct_LINE2) ? eap->line2 :
5766 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5767 size_t num_len;
5768
5769 sprintf(num_buf, "%ld", num);
5770 num_len = STRLEN(num_buf);
5771 result = num_len;
5772
5773 if (quote)
5774 result += 2;
5775
5776 if (buf != NULL)
5777 {
5778 if (quote)
5779 *buf++ = '"';
5780 STRCPY(buf, num_buf);
5781 buf += num_len;
5782 if (quote)
5783 *buf = '"';
5784 }
5785
5786 break;
5787 }
5788
5789 case ct_REGISTER:
5790 result = eap->regname ? 1 : 0;
5791 if (quote)
5792 result += 2;
5793 if (buf != NULL)
5794 {
5795 if (quote)
5796 *buf++ = '\'';
5797 if (eap->regname)
5798 *buf++ = eap->regname;
5799 if (quote)
5800 *buf = '\'';
5801 }
5802 break;
5803
5804 case ct_LT:
5805 result = 1;
5806 if (buf != NULL)
5807 *buf = '<';
5808 break;
5809
5810 default:
5811 /* Not recognized: just copy the '<' and return -1. */
5812 result = (size_t)-1;
5813 if (buf != NULL)
5814 *buf = '<';
5815 break;
5816 }
5817
5818 return result;
5819}
5820
5821 static void
5822do_ucmd(eap)
5823 exarg_T *eap;
5824{
5825 char_u *buf;
5826 char_u *p;
5827 char_u *q;
5828
5829 char_u *start;
5830 char_u *end;
5831 size_t len, totlen;
5832
5833 size_t split_len = 0;
5834 char_u *split_buf = NULL;
5835 ucmd_T *cmd;
5836#ifdef FEAT_EVAL
5837 scid_T save_current_SID = current_SID;
5838#endif
5839
5840 if (eap->cmdidx == CMD_USER)
5841 cmd = USER_CMD(eap->useridx);
5842 else
5843 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5844
5845 /*
5846 * Replace <> in the command by the arguments.
5847 */
5848 buf = NULL;
5849 for (;;)
5850 {
5851 p = cmd->uc_rep;
5852 q = buf;
5853 totlen = 0;
5854 while ((start = vim_strchr(p, '<')) != NULL
5855 && (end = vim_strchr(start + 1, '>')) != NULL)
5856 {
5857 /* Include the '>' */
5858 ++end;
5859
5860 /* Take everything up to the '<' */
5861 len = start - p;
5862 if (buf == NULL)
5863 totlen += len;
5864 else
5865 {
5866 mch_memmove(q, p, len);
5867 q += len;
5868 }
5869
5870 len = uc_check_code(start, end - start, q, cmd, eap,
5871 &split_buf, &split_len);
5872 if (len == (size_t)-1)
5873 {
5874 /* no match, continue after '<' */
5875 p = start + 1;
5876 len = 1;
5877 }
5878 else
5879 p = end;
5880 if (buf == NULL)
5881 totlen += len;
5882 else
5883 q += len;
5884 }
5885 if (buf != NULL) /* second time here, finished */
5886 {
5887 STRCPY(q, p);
5888 break;
5889 }
5890
5891 totlen += STRLEN(p); /* Add on the trailing characters */
5892 buf = alloc((unsigned)(totlen + 1));
5893 if (buf == NULL)
5894 {
5895 vim_free(split_buf);
5896 return;
5897 }
5898 }
5899
5900#ifdef FEAT_EVAL
5901 current_SID = cmd->uc_scriptID;
5902#endif
5903 (void)do_cmdline(buf, eap->getline, eap->cookie,
5904 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5905#ifdef FEAT_EVAL
5906 current_SID = save_current_SID;
5907#endif
5908 vim_free(buf);
5909 vim_free(split_buf);
5910}
5911
5912# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5913 static char_u *
5914get_user_command_name(idx)
5915 int idx;
5916{
5917 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5918}
5919
5920/*
5921 * Function given to ExpandGeneric() to obtain the list of user command names.
5922 */
5923/*ARGSUSED*/
5924 char_u *
5925get_user_commands(xp, idx)
5926 expand_T *xp;
5927 int idx;
5928{
5929 if (idx < curbuf->b_ucmds.ga_len)
5930 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5931 idx -= curbuf->b_ucmds.ga_len;
5932 if (idx < ucmds.ga_len)
5933 return USER_CMD(idx)->uc_name;
5934 return NULL;
5935}
5936
5937/*
5938 * Function given to ExpandGeneric() to obtain the list of user command
5939 * attributes.
5940 */
5941/*ARGSUSED*/
5942 char_u *
5943get_user_cmd_flags(xp, idx)
5944 expand_T *xp;
5945 int idx;
5946{
5947 static char *user_cmd_flags[] =
5948 {"bang", "bar", "buffer", "complete", "count",
5949 "nargs", "range", "register"};
5950
5951 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5952 return NULL;
5953 return (char_u *)user_cmd_flags[idx];
5954}
5955
5956/*
5957 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5958 */
5959/*ARGSUSED*/
5960 char_u *
5961get_user_cmd_nargs(xp, idx)
5962 expand_T *xp;
5963 int idx;
5964{
5965 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5966
5967 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5968 return NULL;
5969 return (char_u *)user_cmd_nargs[idx];
5970}
5971
5972/*
5973 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5974 */
5975/*ARGSUSED*/
5976 char_u *
5977get_user_cmd_complete(xp, idx)
5978 expand_T *xp;
5979 int idx;
5980{
5981 return (char_u *)command_complete[idx].name;
5982}
5983# endif /* FEAT_CMDL_COMPL */
5984
5985#endif /* FEAT_USR_CMDS */
5986
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005987#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
5988/*
5989 * Parse a completion argument "value[vallen]".
5990 * The detected completion goes in "*complp", argument type in "*argt".
5991 * When there is an argument, for function and user defined completion, it's
5992 * copied to allocated memory and stored in "*compl_arg".
5993 * Returns FAIL if something is wrong.
5994 */
5995 int
5996parse_compl_arg(value, vallen, complp, argt, compl_arg)
5997 char_u *value;
5998 int vallen;
5999 int *complp;
6000 long *argt;
6001 char_u **compl_arg;
6002{
6003 char_u *arg = NULL;
6004 size_t arglen = 0;
6005 int i;
6006 int valend = vallen;
6007
6008 /* Look for any argument part - which is the part after any ',' */
6009 for (i = 0; i < vallen; ++i)
6010 {
6011 if (value[i] == ',')
6012 {
6013 arg = &value[i + 1];
6014 arglen = vallen - i - 1;
6015 valend = i;
6016 break;
6017 }
6018 }
6019
6020 for (i = 0; command_complete[i].expand != 0; ++i)
6021 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006022 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006023 && STRNCMP(value, command_complete[i].name, valend) == 0)
6024 {
6025 *complp = command_complete[i].expand;
6026 if (command_complete[i].expand == EXPAND_BUFFERS)
6027 *argt |= BUFNAME;
6028 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6029 || command_complete[i].expand == EXPAND_FILES)
6030 *argt |= XFILE;
6031 break;
6032 }
6033 }
6034
6035 if (command_complete[i].expand == 0)
6036 {
6037 EMSG2(_("E180: Invalid complete value: %s"), value);
6038 return FAIL;
6039 }
6040
6041# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6042 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6043 && arg != NULL)
6044# else
6045 if (arg != NULL)
6046# endif
6047 {
6048 EMSG(_("E468: Completion argument only allowed for custom completion"));
6049 return FAIL;
6050 }
6051
6052# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6053 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6054 && arg == NULL)
6055 {
6056 EMSG(_("E467: Custom completion requires a function argument"));
6057 return FAIL;
6058 }
6059
6060 if (arg != NULL)
6061 *compl_arg = vim_strnsave(arg, (int)arglen);
6062# endif
6063 return OK;
6064}
6065#endif
6066
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 static void
6068ex_colorscheme(eap)
6069 exarg_T *eap;
6070{
6071 if (load_colors(eap->arg) == FAIL)
6072 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6073}
6074
6075 static void
6076ex_highlight(eap)
6077 exarg_T *eap;
6078{
6079 if (*eap->arg == NUL && eap->cmd[2] == '!')
6080 MSG(_("Greetings, Vim user!"));
6081 do_highlight(eap->arg, eap->forceit, FALSE);
6082}
6083
6084
6085/*
6086 * Call this function if we thought we were going to exit, but we won't
6087 * (because of an error). May need to restore the terminal mode.
6088 */
6089 void
6090not_exiting()
6091{
6092 exiting = FALSE;
6093 settmode(TMODE_RAW);
6094}
6095
6096/*
6097 * ":quit": quit current window, quit Vim if closed the last window.
6098 */
6099 static void
6100ex_quit(eap)
6101 exarg_T *eap;
6102{
6103#ifdef FEAT_CMDWIN
6104 if (cmdwin_type != 0)
6105 {
6106 cmdwin_result = Ctrl_C;
6107 return;
6108 }
6109#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006110 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006111 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006112 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006113 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006114 return;
6115 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006116#ifdef FEAT_AUTOCMD
6117 if (curbuf_locked())
6118 return;
6119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
6121#ifdef FEAT_NETBEANS_INTG
6122 netbeansForcedQuit = eap->forceit;
6123#endif
6124
6125 /*
6126 * If there are more files or windows we won't exit.
6127 */
6128 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6129 exiting = TRUE;
6130 if ((!P_HID(curbuf)
6131 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6132 || check_more(TRUE, eap->forceit) == FAIL
6133 || (only_one_window() && check_changed_any(eap->forceit)))
6134 {
6135 not_exiting();
6136 }
6137 else
6138 {
6139#ifdef FEAT_WINDOWS
6140 if (only_one_window()) /* quit last window */
6141#endif
6142 getout(0);
6143#ifdef FEAT_WINDOWS
6144# ifdef FEAT_GUI
6145 need_mouse_correct = TRUE;
6146# endif
6147 /* close window; may free buffer */
6148 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6149#endif
6150 }
6151}
6152
6153/*
6154 * ":cquit".
6155 */
6156/*ARGSUSED*/
6157 static void
6158ex_cquit(eap)
6159 exarg_T *eap;
6160{
6161 getout(1); /* this does not always pass on the exit code to the Manx
6162 compiler. why? */
6163}
6164
6165/*
6166 * ":qall": try to quit all windows
6167 */
6168 static void
6169ex_quit_all(eap)
6170 exarg_T *eap;
6171{
6172# ifdef FEAT_CMDWIN
6173 if (cmdwin_type != 0)
6174 {
6175 if (eap->forceit)
6176 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6177 else
6178 cmdwin_result = K_XF2;
6179 return;
6180 }
6181# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006182
6183 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006184 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006185 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006186 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006187 return;
6188 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006189#ifdef FEAT_AUTOCMD
6190 if (curbuf_locked())
6191 return;
6192#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006193
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 exiting = TRUE;
6195 if (eap->forceit || !check_changed_any(FALSE))
6196 getout(0);
6197 not_exiting();
6198}
6199
Bram Moolenaard9967712006-03-11 21:18:15 +00006200#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201/*
6202 * ":close": close current window, unless it is the last one
6203 */
6204 static void
6205ex_close(eap)
6206 exarg_T *eap;
6207{
6208# ifdef FEAT_CMDWIN
6209 if (cmdwin_type != 0)
6210 cmdwin_result = K_IGNORE;
6211 else
6212# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006213 if (!text_locked()
6214#ifdef FEAT_AUTOCMD
6215 && !curbuf_locked()
6216#endif
6217 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006218 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219}
6220
Bram Moolenaard9967712006-03-11 21:18:15 +00006221# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006222/*
6223 * ":pclose": Close any preview window.
6224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006226ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006228{
6229 win_T *win;
6230
6231 for (win = firstwin; win != NULL; win = win->w_next)
6232 if (win->w_p_pvw)
6233 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006234 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006235 break;
6236 }
6237}
Bram Moolenaard9967712006-03-11 21:18:15 +00006238# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006239
Bram Moolenaarf740b292006-02-16 22:11:02 +00006240/*
6241 * Close window "win" and take care of handling closing the last window for a
6242 * modified buffer.
6243 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006244 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006245ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006246 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006248 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
6250 int need_hide;
6251 buf_T *buf = win->w_buffer;
6252
6253 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006254 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006256# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 if ((p_confirm || cmdmod.confirm) && p_write)
6258 {
6259 dialog_changed(buf, FALSE);
6260 if (buf_valid(buf) && bufIsChanged(buf))
6261 return;
6262 need_hide = FALSE;
6263 }
6264 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 {
6267 EMSG(_(e_nowrtmsg));
6268 return;
6269 }
6270 }
6271
Bram Moolenaard9967712006-03-11 21:18:15 +00006272# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006274# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006275
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006277 if (tp == NULL)
6278 win_close(win, !need_hide && !P_HID(buf));
6279 else
6280 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281}
6282
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006284 * ":tabclose": close current tab page, unless it is the last one.
6285 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 */
6287 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006288ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 exarg_T *eap;
6290{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006291 tabpage_T *tp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006292 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006293
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006294# ifdef FEAT_CMDWIN
6295 if (cmdwin_type != 0)
6296 cmdwin_result = K_IGNORE;
6297 else
6298# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006299 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006300 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006301 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006303 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006304 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006305 tp = find_tabpage((int)eap->line2);
6306 if (tp == NULL)
6307 {
6308 beep_flush();
6309 return;
6310 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006311 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006312 {
6313 tabpage_close_other(tp, eap->forceit);
6314 return;
6315 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006316 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006317 if (!text_locked()
6318#ifdef FEAT_AUTOCMD
6319 && !curbuf_locked()
6320#endif
6321 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006322 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006324
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006325 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006326 shell_new_rows();
6327}
6328
6329/*
6330 * ":tabonly": close all tab pages except the current one
6331 */
6332 static void
6333ex_tabonly(eap)
6334 exarg_T *eap;
6335{
6336 tabpage_T *tp;
6337 int done;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006338 int h = tabline_height();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006339
6340# ifdef FEAT_CMDWIN
6341 if (cmdwin_type != 0)
6342 cmdwin_result = K_IGNORE;
6343 else
6344# endif
6345 if (first_tabpage->tp_next == NULL)
6346 MSG(_("Already only one tab page"));
6347 else
6348 {
6349 /* Repeat this up to a 1000 times, because autocommands may mess
6350 * up the lists. */
6351 for (done = 0; done < 1000; ++done)
6352 {
6353 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6354 if (tp->tp_topframe != topframe)
6355 {
6356 tabpage_close_other(tp, eap->forceit);
6357 /* if we failed to close it quit */
6358 if (valid_tabpage(tp))
6359 done = 1000;
6360 /* start over, "tp" is now invalid */
6361 break;
6362 }
6363 if (first_tabpage->tp_next == NULL)
6364 break;
6365 }
6366 }
6367
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006368 if (h != tabline_height())
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006369 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371
6372/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006373 * Close the current tab page.
6374 */
6375 void
6376tabpage_close(forceit)
6377 int forceit;
6378{
6379 /* First close all the windows but the current one. If that worked then
6380 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006381 if (lastwin != firstwin)
6382 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006383 if (lastwin == firstwin)
6384 ex_win_close(forceit, curwin, NULL);
6385# ifdef FEAT_GUI
6386 need_mouse_correct = TRUE;
6387# endif
6388}
6389
6390/*
6391 * Close tab page "tp", which is not the current tab page.
6392 * Note that autocommands may make "tp" invalid.
6393 */
6394 void
6395tabpage_close_other(tp, forceit)
6396 tabpage_T *tp;
6397 int forceit;
6398{
6399 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006400 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006401
6402 /* Limit to 1000 windows, autocommands may add a window while we close
6403 * one. OK, so I'm paranoid... */
6404 while (++done < 1000)
6405 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006406 wp = tp->tp_firstwin;
6407 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006408
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006409 /* Autocommands may delete the tab page under our fingers and we may
6410 * fail to close a window with a modified buffer. */
6411 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006412 break;
6413 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006414 redraw_tabline = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006415}
6416
6417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 * ":only".
6419 */
6420 static void
6421ex_only(eap)
6422 exarg_T *eap;
6423{
6424# ifdef FEAT_GUI
6425 need_mouse_correct = TRUE;
6426# endif
6427 close_others(TRUE, eap->forceit);
6428}
6429
6430/*
6431 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006432 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006434 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435ex_all(eap)
6436 exarg_T *eap;
6437{
6438 if (eap->addr_count == 0)
6439 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006440 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441}
6442#endif /* FEAT_WINDOWS */
6443
6444 static void
6445ex_hide(eap)
6446 exarg_T *eap;
6447{
6448 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6449 eap->errmsg = e_invarg;
6450 else
6451 {
6452 /* ":hide" or ":hide | cmd": hide current window */
6453 eap->nextcmd = check_nextcmd(eap->arg);
6454#ifdef FEAT_WINDOWS
6455 if (!eap->skip)
6456 {
6457# ifdef FEAT_GUI
6458 need_mouse_correct = TRUE;
6459# endif
6460 win_close(curwin, FALSE); /* don't free buffer */
6461 }
6462#endif
6463 }
6464}
6465
6466/*
6467 * ":stop" and ":suspend": Suspend Vim.
6468 */
6469 static void
6470ex_stop(eap)
6471 exarg_T *eap;
6472{
6473 /*
6474 * Disallow suspending for "rvim".
6475 */
6476 if (!check_restricted()
6477#ifdef WIN3264
6478 /*
6479 * Check if external commands are allowed now.
6480 */
6481 && can_end_termcap_mode(TRUE)
6482#endif
6483 )
6484 {
6485 if (!eap->forceit)
6486 autowrite_all();
6487 windgoto((int)Rows - 1, 0);
6488 out_char('\n');
6489 out_flush();
6490 stoptermcap();
6491 out_flush(); /* needed for SUN to restore xterm buffer */
6492#ifdef FEAT_TITLE
6493 mch_restore_title(3); /* restore window titles */
6494#endif
6495 ui_suspend(); /* call machine specific function */
6496#ifdef FEAT_TITLE
6497 maketitle();
6498 resettitle(); /* force updating the title */
6499#endif
6500 starttermcap();
6501 scroll_start(); /* scroll screen before redrawing */
6502 redraw_later_clear();
6503 shell_resized(); /* may have resized window */
6504 }
6505}
6506
6507/*
6508 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6509 */
6510 static void
6511ex_exit(eap)
6512 exarg_T *eap;
6513{
6514#ifdef FEAT_CMDWIN
6515 if (cmdwin_type != 0)
6516 {
6517 cmdwin_result = Ctrl_C;
6518 return;
6519 }
6520#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006521 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006522 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006523 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006524 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006525 return;
6526 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006527#ifdef FEAT_AUTOCMD
6528 if (curbuf_locked())
6529 return;
6530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531
6532 /*
6533 * if more files or windows we won't exit
6534 */
6535 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6536 exiting = TRUE;
6537 if ( ((eap->cmdidx == CMD_wq
6538 || curbufIsChanged())
6539 && do_write(eap) == FAIL)
6540 || check_more(TRUE, eap->forceit) == FAIL
6541 || (only_one_window() && check_changed_any(eap->forceit)))
6542 {
6543 not_exiting();
6544 }
6545 else
6546 {
6547#ifdef FEAT_WINDOWS
6548 if (only_one_window()) /* quit last window, exit Vim */
6549#endif
6550 getout(0);
6551#ifdef FEAT_WINDOWS
6552# ifdef FEAT_GUI
6553 need_mouse_correct = TRUE;
6554# endif
6555 /* quit current window, may free buffer */
6556 win_close(curwin, !P_HID(curwin->w_buffer));
6557#endif
6558 }
6559}
6560
6561/*
6562 * ":print", ":list", ":number".
6563 */
6564 static void
6565ex_print(eap)
6566 exarg_T *eap;
6567{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006568 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6569 EMSG(_(e_emptybuf));
6570 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006572 for ( ;!got_int; ui_breakcheck())
6573 {
6574 print_line(eap->line1,
6575 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6576 || (eap->flags & EXFLAG_NR)),
6577 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6578 if (++eap->line1 > eap->line2)
6579 break;
6580 out_flush(); /* show one line at a time */
6581 }
6582 setpcmark();
6583 /* put cursor at last line */
6584 curwin->w_cursor.lnum = eap->line2;
6585 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 }
6587
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589}
6590
6591#ifdef FEAT_BYTEOFF
6592 static void
6593ex_goto(eap)
6594 exarg_T *eap;
6595{
6596 goto_byte(eap->line2);
6597}
6598#endif
6599
6600/*
6601 * ":shell".
6602 */
6603/*ARGSUSED*/
6604 static void
6605ex_shell(eap)
6606 exarg_T *eap;
6607{
6608 do_shell(NULL, 0);
6609}
6610
6611#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6612 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006613 || defined(FEAT_GUI_MSWIN) \
6614 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 || defined(PROTO)
6616
6617/*
6618 * Handle a file drop. The code is here because a drop is *nearly* like an
6619 * :args command, but not quite (we have a list of exact filenames, so we
6620 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6621 * code is very similar to :args and hence needs access to a lot of the static
6622 * functions in this file.
6623 *
6624 * The list should be allocated using alloc(), as should each item in the
6625 * list. This function takes over responsibility for freeing the list.
6626 *
6627 * XXX The list is made into the arggument list. This is freed using
6628 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6629 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6630 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6631 * file functionality is (currently) not in EMX this is not presently a
6632 * problem.
6633 */
6634 void
6635handle_drop(filec, filev, split)
6636 int filec; /* the number of files dropped */
6637 char_u **filev; /* the list of files dropped */
6638 int split; /* force splitting the window */
6639{
6640 exarg_T ea;
6641 int save_msg_scroll = msg_scroll;
6642
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006643 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006644 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006646#ifdef FEAT_AUTOCMD
6647 if (curbuf_locked())
6648 return;
6649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650
6651 /* Check whether the current buffer is changed. If so, we will need
6652 * to split the current window or data could be lost.
6653 * We don't need to check if the 'hidden' option is set, as in this
6654 * case the buffer won't be lost.
6655 */
6656 if (!P_HID(curbuf) && !split)
6657 {
6658 ++emsg_off;
6659 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6660 --emsg_off;
6661 }
6662 if (split)
6663 {
6664# ifdef FEAT_WINDOWS
6665 if (win_split(0, 0) == FAIL)
6666 return;
6667# ifdef FEAT_SCROLLBIND
6668 curwin->w_p_scb = FALSE;
6669# endif
6670
6671 /* When splitting the window, create a new alist. Otherwise the
6672 * existing one is overwritten. */
6673 alist_unlink(curwin->w_alist);
6674 alist_new();
6675# else
6676 return; /* can't split, always fail */
6677# endif
6678 }
6679
6680 /*
6681 * Set up the new argument list.
6682 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006683 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684
6685 /*
6686 * Move to the first file.
6687 */
6688 /* Fake up a minimal "next" command for do_argfile() */
6689 vim_memset(&ea, 0, sizeof(ea));
6690 ea.cmd = (char_u *)"next";
6691 do_argfile(&ea, 0);
6692
6693 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6694 * mode that is not needed here. */
6695 need_start_insertmode = FALSE;
6696
6697 /* Restore msg_scroll, otherwise a following command may cause scrolling
6698 * unexpectedly. The screen will be redrawn by the caller, thus
6699 * msg_scroll being set by displaying a message is irrelevant. */
6700 msg_scroll = save_msg_scroll;
6701}
6702#endif
6703
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704/*
6705 * Clear an argument list: free all file names and reset it to zero entries.
6706 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006707 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708alist_clear(al)
6709 alist_T *al;
6710{
6711 while (--al->al_ga.ga_len >= 0)
6712 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6713 ga_clear(&al->al_ga);
6714}
6715
6716/*
6717 * Init an argument list.
6718 */
6719 void
6720alist_init(al)
6721 alist_T *al;
6722{
6723 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6724}
6725
6726#if defined(FEAT_WINDOWS) || defined(PROTO)
6727
6728/*
6729 * Remove a reference from an argument list.
6730 * Ignored when the argument list is the global one.
6731 * If the argument list is no longer used by any window, free it.
6732 */
6733 void
6734alist_unlink(al)
6735 alist_T *al;
6736{
6737 if (al != &global_alist && --al->al_refcount <= 0)
6738 {
6739 alist_clear(al);
6740 vim_free(al);
6741 }
6742}
6743
6744# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6745/*
6746 * Create a new argument list and use it for the current window.
6747 */
6748 void
6749alist_new()
6750{
6751 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6752 if (curwin->w_alist == NULL)
6753 {
6754 curwin->w_alist = &global_alist;
6755 ++global_alist.al_refcount;
6756 }
6757 else
6758 {
6759 curwin->w_alist->al_refcount = 1;
6760 alist_init(curwin->w_alist);
6761 }
6762}
6763# endif
6764#endif
6765
6766#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6767/*
6768 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006769 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6770 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 */
6772 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006773alist_expand(fnum_list, fnum_len)
6774 int *fnum_list;
6775 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
6777 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006778 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 char_u **new_arg_files;
6780 int new_arg_file_count;
6781 char_u *save_p_su = p_su;
6782 int i;
6783
6784 /* Don't use 'suffixes' here. This should work like the shell did the
6785 * expansion. Also, the vimrc file isn't read yet, thus the user
6786 * can't set the options. */
6787 p_su = empty_option;
6788 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6789 if (old_arg_files != NULL)
6790 {
6791 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006792 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6793 old_arg_count = GARGCOUNT;
6794 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 &new_arg_file_count, &new_arg_files,
6796 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6797 && new_arg_file_count > 0)
6798 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006799 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6800 TRUE, fnum_list, fnum_len);
6801 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 }
6804 p_su = save_p_su;
6805}
6806#endif
6807
6808/*
6809 * Set the argument list for the current window.
6810 * Takes over the allocated files[] and the allocated fnames in it.
6811 */
6812 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006813alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 alist_T *al;
6815 int count;
6816 char_u **files;
6817 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006818 int *fnum_list;
6819 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820{
6821 int i;
6822
6823 alist_clear(al);
6824 if (ga_grow(&al->al_ga, count) == OK)
6825 {
6826 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006827 {
6828 if (got_int)
6829 {
6830 /* When adding many buffers this can take a long time. Allow
6831 * interrupting here. */
6832 while (i < count)
6833 vim_free(files[i++]);
6834 break;
6835 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006836
6837 /* May set buffer name of a buffer previously used for the
6838 * argument list, so that it's re-used by alist_add. */
6839 if (fnum_list != NULL && i < fnum_len)
6840 buf_set_name(fnum_list[i], files[i]);
6841
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006843 ui_breakcheck();
6844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 vim_free(files);
6846 }
6847 else
6848 FreeWild(count, files);
6849#ifdef FEAT_WINDOWS
6850 if (al == &global_alist)
6851#endif
6852 arg_had_last = FALSE;
6853}
6854
6855/*
6856 * Add file "fname" to argument list "al".
6857 * "fname" must have been allocated and "al" must have been checked for room.
6858 */
6859 void
6860alist_add(al, fname, set_fnum)
6861 alist_T *al;
6862 char_u *fname;
6863 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6864{
6865 if (fname == NULL) /* don't add NULL file names */
6866 return;
6867#ifdef BACKSLASH_IN_FILENAME
6868 slash_adjust(fname);
6869#endif
6870 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6871 if (set_fnum > 0)
6872 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6873 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6874 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875}
6876
6877#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6878/*
6879 * Adjust slashes in file names. Called after 'shellslash' was set.
6880 */
6881 void
6882alist_slash_adjust()
6883{
6884 int i;
6885# ifdef FEAT_WINDOWS
6886 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006887 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888# endif
6889
6890 for (i = 0; i < GARGCOUNT; ++i)
6891 if (GARGLIST[i].ae_fname != NULL)
6892 slash_adjust(GARGLIST[i].ae_fname);
6893# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006894 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 if (wp->w_alist != &global_alist)
6896 for (i = 0; i < WARGCOUNT(wp); ++i)
6897 if (WARGLIST(wp)[i].ae_fname != NULL)
6898 slash_adjust(WARGLIST(wp)[i].ae_fname);
6899# endif
6900}
6901#endif
6902
6903/*
6904 * ":preserve".
6905 */
6906/*ARGSUSED*/
6907 static void
6908ex_preserve(eap)
6909 exarg_T *eap;
6910{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006911 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 ml_preserve(curbuf, TRUE);
6913}
6914
6915/*
6916 * ":recover".
6917 */
6918 static void
6919ex_recover(eap)
6920 exarg_T *eap;
6921{
6922 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6923 recoverymode = TRUE;
6924 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6925 && (*eap->arg == NUL
6926 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6927 ml_recover();
6928 recoverymode = FALSE;
6929}
6930
6931/*
6932 * Command modifier used in a wrong way.
6933 */
6934 static void
6935ex_wrongmodifier(eap)
6936 exarg_T *eap;
6937{
6938 eap->errmsg = e_invcmd;
6939}
6940
6941#ifdef FEAT_WINDOWS
6942/*
6943 * :sview [+command] file split window with new file, read-only
6944 * :split [[+command] file] split window with current or new file
6945 * :vsplit [[+command] file] split window vertically with current or new file
6946 * :new [[+command] file] split window with no or new file
6947 * :vnew [[+command] file] split vertically window with no or new file
6948 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006949 *
6950 * :tabedit open new Tab page with empty window
6951 * :tabedit [+command] file open new Tab page and edit "file"
6952 * :tabnew [[+command] file] just like :tabedit
6953 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 */
6955 void
6956ex_splitview(eap)
6957 exarg_T *eap;
6958{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006959 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006960# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006962# endif
6963# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006965# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006967# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6969 {
6970 ex_ni(eap);
6971 return;
6972 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006975# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006977# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006979# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 /* A ":split" in the quickfix window works like ":new". Don't want two
6981 * quickfix windows. */
6982 if (bt_quickfix(curbuf))
6983 {
6984 if (eap->cmdidx == CMD_split)
6985 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006986# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 if (eap->cmdidx == CMD_vsplit)
6988 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006989# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006991# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006993# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006994 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 {
6996 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6997 FNAME_MESS, TRUE, curbuf->b_ffname);
6998 if (fname == NULL)
6999 goto theend;
7000 eap->arg = fname;
7001 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007002# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007004# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007006# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007008# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007010# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 && eap->cmdidx != CMD_new)
7012 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007013 if (
7014# ifdef FEAT_GUI
7015 !gui.in_use &&
7016# endif
7017 au_has_group((char_u *)"FileExplorer"))
7018 {
7019 /* No browsing supported but we do have the file explorer:
7020 * Edit the directory. */
7021 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7022 eap->arg = (char_u *)".";
7023 }
7024 else
7025 {
7026 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007028 if (fname == NULL)
7029 goto theend;
7030 eap->arg = fname;
7031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 }
7033 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007036 /*
7037 * Either open new tab page or split the window.
7038 */
7039 if (eap->cmdidx == CMD_tabedit
7040 || eap->cmdidx == CMD_tabfind
7041 || eap->cmdidx == CMD_tabnew)
7042 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007043 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7044 : eap->addr_count == 0 ? 0
7045 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007046 {
7047 do_exedit(eap, NULL);
7048
7049 /* set the alternate buffer for the window we came from */
7050 if (curwin != old_curwin
7051 && win_valid(old_curwin)
7052 && old_curwin->w_buffer != curbuf
7053 && !cmdmod.keepalt)
7054 old_curwin->w_alt_fnum = curbuf->b_fnum;
7055 }
7056 }
7057 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7059 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007060# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 /* Reset 'scrollbind' when editing another file, but keep it when
7062 * doing ":split" without arguments. */
7063 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007064# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007066# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 )
7068 curwin->w_p_scb = FALSE;
7069 else
7070 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007071# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 do_exedit(eap, old_curwin);
7073 }
7074
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007075# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007079# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080theend:
7081 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007082# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007084
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00007085#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007086/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007087 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007088 */
7089 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007090tabpage_new()
7091{
7092 exarg_T ea;
7093
7094 vim_memset(&ea, 0, sizeof(ea));
7095 ea.cmdidx = CMD_tabnew;
7096 ea.cmd = (char_u *)"tabn";
7097 ea.arg = (char_u *)"";
7098 ex_splitview(&ea);
7099}
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00007100#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007101
7102/*
7103 * :tabnext command
7104 */
7105 static void
7106ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007107 exarg_T *eap;
7108{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007109 switch (eap->cmdidx)
7110 {
7111 case CMD_tabfirst:
7112 case CMD_tabrewind:
7113 goto_tabpage(1);
7114 break;
7115 case CMD_tablast:
7116 goto_tabpage(9999);
7117 break;
7118 case CMD_tabprevious:
7119 case CMD_tabNext:
7120 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7121 break;
7122 default: /* CMD_tabnext */
7123 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7124 break;
7125 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007126}
7127
7128/*
7129 * :tabmove command
7130 */
7131 static void
7132ex_tabmove(eap)
7133 exarg_T *eap;
7134{
7135 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007136}
7137
7138/*
7139 * :tabs command: List tabs and their contents.
7140 */
7141/*ARGSUSED*/
7142 static void
7143ex_tabs(eap)
7144 exarg_T *eap;
7145{
7146 tabpage_T *tp;
7147 win_T *wp;
7148 int tabcount = 1;
7149
7150 msg_start();
7151 msg_scroll = TRUE;
7152 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7153 {
7154 msg_putchar('\n');
7155 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7156 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7157 out_flush(); /* output one line at a time */
7158 ui_breakcheck();
7159
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007160 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007161 wp = firstwin;
7162 else
7163 wp = tp->tp_firstwin;
7164 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7165 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007166 msg_putchar('\n');
7167 msg_putchar(wp == curwin ? '>' : ' ');
7168 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007169 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7170 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007171 if (buf_spname(wp->w_buffer) != NULL)
7172 STRCPY(IObuff, buf_spname(wp->w_buffer));
7173 else
7174 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7175 IObuff, IOSIZE, TRUE);
7176 msg_outtrans(IObuff);
7177 out_flush(); /* output one line at a time */
7178 ui_breakcheck();
7179 }
7180 }
7181}
7182
7183#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
7185/*
7186 * ":mode": Set screen mode.
7187 * If no argument given, just get the screen size and redraw.
7188 */
7189 static void
7190ex_mode(eap)
7191 exarg_T *eap;
7192{
7193 if (*eap->arg == NUL)
7194 shell_resized();
7195 else
7196 mch_screenmode(eap->arg);
7197}
7198
7199#ifdef FEAT_WINDOWS
7200/*
7201 * ":resize".
7202 * set, increment or decrement current window height
7203 */
7204 static void
7205ex_resize(eap)
7206 exarg_T *eap;
7207{
7208 int n;
7209 win_T *wp = curwin;
7210
7211 if (eap->addr_count > 0)
7212 {
7213 n = eap->line2;
7214 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7215 ;
7216 }
7217
7218#ifdef FEAT_GUI
7219 need_mouse_correct = TRUE;
7220#endif
7221 n = atol((char *)eap->arg);
7222#ifdef FEAT_VERTSPLIT
7223 if (cmdmod.split & WSP_VERT)
7224 {
7225 if (*eap->arg == '-' || *eap->arg == '+')
7226 n += W_WIDTH(curwin);
7227 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7228 n = 9999;
7229 win_setwidth_win((int)n, wp);
7230 }
7231 else
7232#endif
7233 {
7234 if (*eap->arg == '-' || *eap->arg == '+')
7235 n += curwin->w_height;
7236 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7237 n = 9999;
7238 win_setheight_win((int)n, wp);
7239 }
7240}
7241#endif
7242
7243/*
7244 * ":find [+command] <file>" command.
7245 */
7246 static void
7247ex_find(eap)
7248 exarg_T *eap;
7249{
7250#ifdef FEAT_SEARCHPATH
7251 char_u *fname;
7252 int count;
7253
7254 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7255 TRUE, curbuf->b_ffname);
7256 if (eap->addr_count > 0)
7257 {
7258 /* Repeat finding the file "count" times. This matters when it
7259 * appears several times in the path. */
7260 count = eap->line2;
7261 while (fname != NULL && --count > 0)
7262 {
7263 vim_free(fname);
7264 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7265 FALSE, curbuf->b_ffname);
7266 }
7267 }
7268
7269 if (fname != NULL)
7270 {
7271 eap->arg = fname;
7272#endif
7273 do_exedit(eap, NULL);
7274#ifdef FEAT_SEARCHPATH
7275 vim_free(fname);
7276 }
7277#endif
7278}
7279
7280/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007281 * ":open" simulation: for now just work like ":visual".
7282 */
7283 static void
7284ex_open(eap)
7285 exarg_T *eap;
7286{
7287 regmatch_T regmatch;
7288 char_u *p;
7289
7290 curwin->w_cursor.lnum = eap->line2;
7291 beginline(BL_SOL | BL_FIX);
7292 if (*eap->arg == '/')
7293 {
7294 /* ":open /pattern/": put cursor in column found with pattern */
7295 ++eap->arg;
7296 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7297 *p = NUL;
7298 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7299 if (regmatch.regprog != NULL)
7300 {
7301 regmatch.rm_ic = p_ic;
7302 p = ml_get_curline();
7303 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007304 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007305 else
7306 EMSG(_(e_nomatch));
7307 vim_free(regmatch.regprog);
7308 }
7309 /* Move to the NUL, ignore any other arguments. */
7310 eap->arg += STRLEN(eap->arg);
7311 }
7312 check_cursor();
7313
7314 eap->cmdidx = CMD_visual;
7315 do_exedit(eap, NULL);
7316}
7317
7318/*
7319 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 */
7321 static void
7322ex_edit(eap)
7323 exarg_T *eap;
7324{
7325 do_exedit(eap, NULL);
7326}
7327
7328/*
7329 * ":edit <file>" command and alikes.
7330 */
7331/*ARGSUSED*/
7332 void
7333do_exedit(eap, old_curwin)
7334 exarg_T *eap;
7335 win_T *old_curwin; /* curwin before doing a split or NULL */
7336{
7337 int n;
7338#ifdef FEAT_WINDOWS
7339 int need_hide;
7340#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007341 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342
7343 /*
7344 * ":vi" command ends Ex mode.
7345 */
7346 if (exmode_active && (eap->cmdidx == CMD_visual
7347 || eap->cmdidx == CMD_view))
7348 {
7349 exmode_active = FALSE;
7350 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007351 {
7352 /* Special case: ":global/pat/visual\NLvi-commands" */
7353 if (global_busy)
7354 {
7355 int rd = RedrawingDisabled;
7356 int nwr = no_wait_return;
7357 int ms = msg_scroll;
7358#ifdef FEAT_GUI
7359 int he = hold_gui_events;
7360#endif
7361
7362 if (eap->nextcmd != NULL)
7363 {
7364 stuffReadbuff(eap->nextcmd);
7365 eap->nextcmd = NULL;
7366 }
7367
7368 if (exmode_was != EXMODE_VIM)
7369 settmode(TMODE_RAW);
7370 RedrawingDisabled = 0;
7371 no_wait_return = 0;
7372 need_wait_return = FALSE;
7373 msg_scroll = 0;
7374#ifdef FEAT_GUI
7375 hold_gui_events = 0;
7376#endif
7377 must_redraw = CLEAR;
7378
7379 main_loop(FALSE, TRUE);
7380
7381 RedrawingDisabled = rd;
7382 no_wait_return = nwr;
7383 msg_scroll = ms;
7384#ifdef FEAT_GUI
7385 hold_gui_events = he;
7386#endif
7387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 }
7391
7392 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007393 || eap->cmdidx == CMD_tabnew
7394 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395#ifdef FEAT_VERTSPLIT
7396 || eap->cmdidx == CMD_vnew
7397#endif
7398 ) && *eap->arg == NUL)
7399 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007400 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 setpcmark();
7402 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
7403 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
7404 }
7405 else if ((eap->cmdidx != CMD_split
7406#ifdef FEAT_VERTSPLIT
7407 && eap->cmdidx != CMD_vsplit
7408#endif
7409 )
7410 || *eap->arg != NUL
7411#ifdef FEAT_BROWSE
7412 || cmdmod.browse
7413#endif
7414 )
7415 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007416#ifdef FEAT_AUTOCMD
7417 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7418 * can bring us here, others are stopped earlier. */
7419 if (*eap->arg != NUL && curbuf_locked())
7420 return;
7421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 n = readonlymode;
7423 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7424 readonlymode = TRUE;
7425 else if (eap->cmdidx == CMD_enew)
7426 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7427 empty buffer */
7428 setpcmark();
7429 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7430 NULL, eap,
7431 /* ":edit" goes to first line if Vi compatible */
7432 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7433 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7434 ? ECMD_ONE : eap->do_ecmd_lnum,
7435 (P_HID(curbuf) ? ECMD_HIDE : 0)
7436 + (eap->forceit ? ECMD_FORCEIT : 0)
7437#ifdef FEAT_LISTCMDS
7438 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7439#endif
7440 ) == FAIL)
7441 {
7442 /* Editing the file failed. If the window was split, close it. */
7443#ifdef FEAT_WINDOWS
7444 if (old_curwin != NULL)
7445 {
7446 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7447 if (!need_hide || P_HID(curbuf))
7448 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007449# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7450 cleanup_T cs;
7451
7452 /* Reset the error/interrupt/exception state here so that
7453 * aborting() returns FALSE when closing a window. */
7454 enter_cleanup(&cs);
7455# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456# ifdef FEAT_GUI
7457 need_mouse_correct = TRUE;
7458# endif
7459 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007460
7461# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7462 /* Restore the error/interrupt/exception state if not
7463 * discarded by a new aborting error, interrupt, or
7464 * uncaught exception. */
7465 leave_cleanup(&cs);
7466# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 }
7468 }
7469#endif
7470 }
7471 else if (readonlymode && curbuf->b_nwindows == 1)
7472 {
7473 /* When editing an already visited buffer, 'readonly' won't be set
7474 * but the previous value is kept. With ":view" and ":sview" we
7475 * want the file to be readonly, except when another window is
7476 * editing the same buffer. */
7477 curbuf->b_p_ro = TRUE;
7478 }
7479 readonlymode = n;
7480 }
7481 else
7482 {
7483 if (eap->do_ecmd_cmd != NULL)
7484 do_cmdline_cmd(eap->do_ecmd_cmd);
7485#ifdef FEAT_TITLE
7486 n = curwin->w_arg_idx_invalid;
7487#endif
7488 check_arg_idx(curwin);
7489#ifdef FEAT_TITLE
7490 if (n != curwin->w_arg_idx_invalid)
7491 maketitle();
7492#endif
7493 }
7494
7495#ifdef FEAT_WINDOWS
7496 /*
7497 * if ":split file" worked, set alternate file name in old window to new
7498 * file
7499 */
7500 if (old_curwin != NULL
7501 && *eap->arg != NUL
7502 && curwin != old_curwin
7503 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007504 && old_curwin->w_buffer != curbuf
7505 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 old_curwin->w_alt_fnum = curbuf->b_fnum;
7507#endif
7508
7509 ex_no_reprint = TRUE;
7510}
7511
7512#ifndef FEAT_GUI
7513/*
7514 * ":gui" and ":gvim" when there is no GUI.
7515 */
7516 static void
7517ex_nogui(eap)
7518 exarg_T *eap;
7519{
7520 eap->errmsg = e_nogvim;
7521}
7522#endif
7523
7524#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7525 static void
7526ex_tearoff(eap)
7527 exarg_T *eap;
7528{
7529 gui_make_tearoff(eap->arg);
7530}
7531#endif
7532
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007533#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 static void
7535ex_popup(eap)
7536 exarg_T *eap;
7537{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007538 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539}
7540#endif
7541
7542/*ARGSUSED*/
7543 static void
7544ex_swapname(eap)
7545 exarg_T *eap;
7546{
7547 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7548 MSG(_("No swap file"));
7549 else
7550 msg(curbuf->b_ml.ml_mfp->mf_fname);
7551}
7552
7553/*
7554 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7555 * offset.
7556 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7557 */
7558/*ARGSUSED*/
7559 static void
7560ex_syncbind(eap)
7561 exarg_T *eap;
7562{
7563#ifdef FEAT_SCROLLBIND
7564 win_T *wp;
7565 long topline;
7566 long y;
7567 linenr_T old_linenr = curwin->w_cursor.lnum;
7568
7569 setpcmark();
7570
7571 /*
7572 * determine max topline
7573 */
7574 if (curwin->w_p_scb)
7575 {
7576 topline = curwin->w_topline;
7577 for (wp = firstwin; wp; wp = wp->w_next)
7578 {
7579 if (wp->w_p_scb && wp->w_buffer)
7580 {
7581 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7582 if (topline > y)
7583 topline = y;
7584 }
7585 }
7586 if (topline < 1)
7587 topline = 1;
7588 }
7589 else
7590 {
7591 topline = 1;
7592 }
7593
7594
7595 /*
7596 * set all scrollbind windows to the same topline
7597 */
7598 wp = curwin;
7599 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7600 {
7601 if (curwin->w_p_scb)
7602 {
7603 y = topline - curwin->w_topline;
7604 if (y > 0)
7605 scrollup(y, TRUE);
7606 else
7607 scrolldown(-y, TRUE);
7608 curwin->w_scbind_pos = topline;
7609 redraw_later(VALID);
7610 cursor_correct();
7611#ifdef FEAT_WINDOWS
7612 curwin->w_redr_status = TRUE;
7613#endif
7614 }
7615 }
7616 curwin = wp;
7617 if (curwin->w_p_scb)
7618 {
7619 did_syncbind = TRUE;
7620 checkpcmark();
7621 if (old_linenr != curwin->w_cursor.lnum)
7622 {
7623 char_u ctrl_o[2];
7624
7625 ctrl_o[0] = Ctrl_O;
7626 ctrl_o[1] = 0;
7627 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7628 }
7629 }
7630#endif
7631}
7632
7633
7634 static void
7635ex_read(eap)
7636 exarg_T *eap;
7637{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007638 int i;
7639 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7640 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641
7642 if (eap->usefilter) /* :r!cmd */
7643 do_bang(1, eap, FALSE, FALSE, TRUE);
7644 else
7645 {
7646 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7647 return;
7648
7649#ifdef FEAT_BROWSE
7650 if (cmdmod.browse)
7651 {
7652 char_u *browseFile;
7653
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007654 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 NULL, NULL, NULL, curbuf);
7656 if (browseFile != NULL)
7657 {
7658 i = readfile(browseFile, NULL,
7659 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7660 vim_free(browseFile);
7661 }
7662 else
7663 i = OK;
7664 }
7665 else
7666#endif
7667 if (*eap->arg == NUL)
7668 {
7669 if (check_fname() == FAIL) /* check for no file name */
7670 return;
7671 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7672 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7673 }
7674 else
7675 {
7676 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7677 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7678 i = readfile(eap->arg, NULL,
7679 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7680
7681 }
7682 if (i == FAIL)
7683 {
7684#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7685 if (!aborting())
7686#endif
7687 EMSG2(_(e_notopen), eap->arg);
7688 }
7689 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007690 {
7691 if (empty && exmode_active)
7692 {
7693 /* Delete the empty line that remains. Historically ex does
7694 * this but vi doesn't. */
7695 if (eap->line2 == 0)
7696 lnum = curbuf->b_ml.ml_line_count;
7697 else
7698 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007699 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007700 {
7701 ml_delete(lnum, FALSE);
7702 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007703 if (curwin->w_cursor.lnum > 1
7704 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007705 --curwin->w_cursor.lnum;
7706 }
7707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 }
7711}
7712
Bram Moolenaarea408852005-06-25 22:49:46 +00007713static char_u *prev_dir = NULL;
7714
7715#if defined(EXITFREE) || defined(PROTO)
7716 void
7717free_cd_dir()
7718{
7719 vim_free(prev_dir);
7720}
7721#endif
7722
7723
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724/*
7725 * ":cd", ":lcd", ":chdir" and ":lchdir".
7726 */
7727 static void
7728ex_cd(eap)
7729 exarg_T *eap;
7730{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 char_u *new_dir;
7732 char_u *tofree;
7733
7734 new_dir = eap->arg;
7735#if !defined(UNIX) && !defined(VMS)
7736 /* for non-UNIX ":cd" means: print current directory */
7737 if (*new_dir == NUL)
7738 ex_pwd(NULL);
7739 else
7740#endif
7741 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007742 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7743 && !eap->forceit)
7744 {
7745 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7746 return;
7747 }
7748
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 /* ":cd -": Change to previous directory */
7750 if (STRCMP(new_dir, "-") == 0)
7751 {
7752 if (prev_dir == NULL)
7753 {
7754 EMSG(_("E186: No previous directory"));
7755 return;
7756 }
7757 new_dir = prev_dir;
7758 }
7759
7760 /* Save current directory for next ":cd -" */
7761 tofree = prev_dir;
7762 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7763 prev_dir = vim_strsave(NameBuff);
7764 else
7765 prev_dir = NULL;
7766
7767#if defined(UNIX) || defined(VMS)
7768 /* for UNIX ":cd" means: go to home directory */
7769 if (*new_dir == NUL)
7770 {
7771 /* use NameBuff for home directory name */
7772# ifdef VMS
7773 char_u *p;
7774
7775 p = mch_getenv((char_u *)"SYS$LOGIN");
7776 if (p == NULL || *p == NUL) /* empty is the same as not set */
7777 NameBuff[0] = NUL;
7778 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007779 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780# else
7781 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7782# endif
7783 new_dir = NameBuff;
7784 }
7785#endif
7786 if (new_dir == NULL || vim_chdir(new_dir))
7787 EMSG(_(e_failed));
7788 else
7789 {
7790 vim_free(curwin->w_localdir);
7791 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7792 {
7793 /* If still in global directory, need to remember current
7794 * directory as global directory. */
7795 if (globaldir == NULL && prev_dir != NULL)
7796 globaldir = vim_strsave(prev_dir);
7797 /* Remember this local directory for the window. */
7798 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7799 curwin->w_localdir = vim_strsave(NameBuff);
7800 }
7801 else
7802 {
7803 /* We are now in the global directory, no need to remember its
7804 * name. */
7805 vim_free(globaldir);
7806 globaldir = NULL;
7807 curwin->w_localdir = NULL;
7808 }
7809
7810 shorten_fnames(TRUE);
7811
7812 /* Echo the new current directory if the command was typed. */
7813 if (KeyTyped)
7814 ex_pwd(eap);
7815 }
7816 vim_free(tofree);
7817 }
7818}
7819
7820/*
7821 * ":pwd".
7822 */
7823/*ARGSUSED*/
7824 static void
7825ex_pwd(eap)
7826 exarg_T *eap;
7827{
7828 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7829 {
7830#ifdef BACKSLASH_IN_FILENAME
7831 slash_adjust(NameBuff);
7832#endif
7833 msg(NameBuff);
7834 }
7835 else
7836 EMSG(_("E187: Unknown"));
7837}
7838
7839/*
7840 * ":=".
7841 */
7842 static void
7843ex_equal(eap)
7844 exarg_T *eap;
7845{
7846 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007847 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848}
7849
7850 static void
7851ex_sleep(eap)
7852 exarg_T *eap;
7853{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007854 int n;
7855 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856
7857 if (cursor_valid())
7858 {
7859 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7860 if (n >= 0)
7861 windgoto((int)n, curwin->w_wcol);
7862 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007863
7864 len = eap->line2;
7865 switch (*eap->arg)
7866 {
7867 case 'm': break;
7868 case NUL: len *= 1000L; break;
7869 default: EMSG2(_(e_invarg2), eap->arg); return;
7870 }
7871 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872}
7873
7874/*
7875 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7876 */
7877 void
7878do_sleep(msec)
7879 long msec;
7880{
7881 long done;
7882
7883 cursor_on();
7884 out_flush();
7885 for (done = 0; !got_int && done < msec; done += 1000L)
7886 {
7887 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7888 ui_breakcheck();
7889 }
7890}
7891
7892 static void
7893do_exmap(eap, isabbrev)
7894 exarg_T *eap;
7895 int isabbrev;
7896{
7897 int mode;
7898 char_u *cmdp;
7899
7900 cmdp = eap->cmd;
7901 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7902
7903 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7904 eap->arg, mode, isabbrev))
7905 {
7906 case 1: EMSG(_(e_invarg));
7907 break;
7908 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7909 break;
7910 }
7911}
7912
7913/*
7914 * ":winsize" command (obsolete).
7915 */
7916 static void
7917ex_winsize(eap)
7918 exarg_T *eap;
7919{
7920 int w, h;
7921 char_u *arg = eap->arg;
7922 char_u *p;
7923
7924 w = getdigits(&arg);
7925 arg = skipwhite(arg);
7926 p = arg;
7927 h = getdigits(&arg);
7928 if (*p != NUL && *arg == NUL)
7929 set_shellsize(w, h, TRUE);
7930 else
7931 EMSG(_("E465: :winsize requires two number arguments"));
7932}
7933
7934#ifdef FEAT_WINDOWS
7935 static void
7936ex_wincmd(eap)
7937 exarg_T *eap;
7938{
7939 int xchar = NUL;
7940 char_u *p;
7941
7942 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7943 {
7944 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7945 if (eap->arg[1] == NUL)
7946 {
7947 EMSG(_(e_invarg));
7948 return;
7949 }
7950 xchar = eap->arg[1];
7951 p = eap->arg + 2;
7952 }
7953 else
7954 p = eap->arg + 1;
7955
7956 eap->nextcmd = check_nextcmd(p);
7957 p = skipwhite(p);
7958 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7959 EMSG(_(e_invarg));
7960 else
7961 {
7962 /* Pass flags on for ":vertical wincmd ]". */
7963 postponed_split_flags = cmdmod.split;
7964 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7965 postponed_split_flags = 0;
7966 }
7967}
7968#endif
7969
Bram Moolenaar843ee412004-06-30 16:16:41 +00007970#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971/*
7972 * ":winpos".
7973 */
7974 static void
7975ex_winpos(eap)
7976 exarg_T *eap;
7977{
7978 int x, y;
7979 char_u *arg = eap->arg;
7980 char_u *p;
7981
7982 if (*arg == NUL)
7983 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007984# if defined(FEAT_GUI) || defined(MSWIN)
7985# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007987# else
7988 if (mch_get_winpos(&x, &y) != FAIL)
7989# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {
7991 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7992 msg(IObuff);
7993 }
7994 else
7995# endif
7996 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7997 }
7998 else
7999 {
8000 x = getdigits(&arg);
8001 arg = skipwhite(arg);
8002 p = arg;
8003 y = getdigits(&arg);
8004 if (*p == NUL || *arg != NUL)
8005 {
8006 EMSG(_("E466: :winpos requires two number arguments"));
8007 return;
8008 }
8009# ifdef FEAT_GUI
8010 if (gui.in_use)
8011 gui_mch_set_winpos(x, y);
8012 else if (gui.starting)
8013 {
8014 /* Remember the coordinates for when the window is opened. */
8015 gui_win_x = x;
8016 gui_win_y = y;
8017 }
8018# ifdef HAVE_TGETENT
8019 else
8020# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008021# else
8022# ifdef MSWIN
8023 mch_set_winpos(x, y);
8024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025# endif
8026# ifdef HAVE_TGETENT
8027 if (*T_CWP)
8028 term_set_winpos(x, y);
8029# endif
8030 }
8031}
8032#endif
8033
8034/*
8035 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8036 */
8037 static void
8038ex_operators(eap)
8039 exarg_T *eap;
8040{
8041 oparg_T oa;
8042
8043 clear_oparg(&oa);
8044 oa.regname = eap->regname;
8045 oa.start.lnum = eap->line1;
8046 oa.end.lnum = eap->line2;
8047 oa.line_count = eap->line2 - eap->line1 + 1;
8048 oa.motion_type = MLINE;
8049#ifdef FEAT_VIRTUALEDIT
8050 virtual_op = FALSE;
8051#endif
8052 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8053 {
8054 setpcmark();
8055 curwin->w_cursor.lnum = eap->line1;
8056 beginline(BL_SOL | BL_FIX);
8057 }
8058
8059 switch (eap->cmdidx)
8060 {
8061 case CMD_delete:
8062 oa.op_type = OP_DELETE;
8063 op_delete(&oa);
8064 break;
8065
8066 case CMD_yank:
8067 oa.op_type = OP_YANK;
8068 (void)op_yank(&oa, FALSE, TRUE);
8069 break;
8070
8071 default: /* CMD_rshift or CMD_lshift */
8072 if ((eap->cmdidx == CMD_rshift)
8073#ifdef FEAT_RIGHTLEFT
8074 ^ curwin->w_p_rl
8075#endif
8076 )
8077 oa.op_type = OP_RSHIFT;
8078 else
8079 oa.op_type = OP_LSHIFT;
8080 op_shift(&oa, FALSE, eap->amount);
8081 break;
8082 }
8083#ifdef FEAT_VIRTUALEDIT
8084 virtual_op = MAYBE;
8085#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008086 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087}
8088
8089/*
8090 * ":put".
8091 */
8092 static void
8093ex_put(eap)
8094 exarg_T *eap;
8095{
8096 /* ":0put" works like ":1put!". */
8097 if (eap->line2 == 0)
8098 {
8099 eap->line2 = 1;
8100 eap->forceit = TRUE;
8101 }
8102 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008103 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8104 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105}
8106
8107/*
8108 * Handle ":copy" and ":move".
8109 */
8110 static void
8111ex_copymove(eap)
8112 exarg_T *eap;
8113{
8114 long n;
8115
8116 n = get_address(&eap->arg, FALSE, FALSE);
8117 if (eap->arg == NULL) /* error detected */
8118 {
8119 eap->nextcmd = NULL;
8120 return;
8121 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008122 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123
8124 /*
8125 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8126 */
8127 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8128 {
8129 EMSG(_(e_invaddr));
8130 return;
8131 }
8132
8133 if (eap->cmdidx == CMD_move)
8134 {
8135 if (do_move(eap->line1, eap->line2, n) == FAIL)
8136 return;
8137 }
8138 else
8139 ex_copy(eap->line1, eap->line2, n);
8140 u_clearline();
8141 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008142 ex_may_print(eap);
8143}
8144
8145/*
8146 * Print the current line if flags were given to the Ex command.
8147 */
8148 static void
8149ex_may_print(eap)
8150 exarg_T *eap;
8151{
8152 if (eap->flags != 0)
8153 {
8154 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8155 (eap->flags & EXFLAG_LIST));
8156 ex_no_reprint = TRUE;
8157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158}
8159
8160/*
8161 * ":smagic" and ":snomagic".
8162 */
8163 static void
8164ex_submagic(eap)
8165 exarg_T *eap;
8166{
8167 int magic_save = p_magic;
8168
8169 p_magic = (eap->cmdidx == CMD_smagic);
8170 do_sub(eap);
8171 p_magic = magic_save;
8172}
8173
8174/*
8175 * ":join".
8176 */
8177 static void
8178ex_join(eap)
8179 exarg_T *eap;
8180{
8181 curwin->w_cursor.lnum = eap->line1;
8182 if (eap->line1 == eap->line2)
8183 {
8184 if (eap->addr_count >= 2) /* :2,2join does nothing */
8185 return;
8186 if (eap->line2 == curbuf->b_ml.ml_line_count)
8187 {
8188 beep_flush();
8189 return;
8190 }
8191 ++eap->line2;
8192 }
8193 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8194 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008195 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196}
8197
8198/*
8199 * ":[addr]@r" or ":[addr]*r": execute register
8200 */
8201 static void
8202ex_at(eap)
8203 exarg_T *eap;
8204{
8205 int c;
8206
8207 curwin->w_cursor.lnum = eap->line2;
8208
8209#ifdef USE_ON_FLY_SCROLL
8210 dont_scroll = TRUE; /* disallow scrolling here */
8211#endif
8212
8213 /* get the register name. No name means to use the previous one */
8214 c = *eap->arg;
8215 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8216 c = '@';
8217 /* put the register in mapbuf */
8218 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 else
8223 {
8224 int save_efr = exec_from_reg;
8225
8226 exec_from_reg = TRUE;
8227
8228 /*
8229 * Execute from the typeahead buffer.
8230 * Originally this didn't check for the typeahead buffer to be empty,
8231 * thus could read more Ex commands from stdin. It's not clear why,
8232 * it is certainly unexpected.
8233 */
8234 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8235 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8236
8237 exec_from_reg = save_efr;
8238 }
8239}
8240
8241/*
8242 * ":!".
8243 */
8244 static void
8245ex_bang(eap)
8246 exarg_T *eap;
8247{
8248 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8249}
8250
8251/*
8252 * ":undo".
8253 */
8254/*ARGSUSED*/
8255 static void
8256ex_undo(eap)
8257 exarg_T *eap;
8258{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008259 if (eap->addr_count == 1) /* :undo 123 */
8260 undo_time(eap->line2, FALSE, TRUE);
8261 else
8262 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263}
8264
8265/*
8266 * ":redo".
8267 */
8268/*ARGSUSED*/
8269 static void
8270ex_redo(eap)
8271 exarg_T *eap;
8272{
8273 u_redo(1);
8274}
8275
8276/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008277 * ":earlier" and ":later".
8278 */
8279/*ARGSUSED*/
8280 static void
8281ex_later(eap)
8282 exarg_T *eap;
8283{
8284 long count = 0;
8285 int sec = FALSE;
8286 char_u *p = eap->arg;
8287
8288 if (*p == NUL)
8289 count = 1;
8290 else if (isdigit(*p))
8291 {
8292 count = getdigits(&p);
8293 switch (*p)
8294 {
8295 case 's': ++p; sec = TRUE; break;
8296 case 'm': ++p; sec = TRUE; count *= 60; break;
8297 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
8298 }
8299 }
8300
8301 if (*p != NUL)
8302 EMSG2(_(e_invarg2), eap->arg);
8303 else
Bram Moolenaard3667a22006-03-16 21:35:52 +00008304 undo_time(eap->cmdidx == CMD_earlier ? -count : count, sec, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008305}
8306
8307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 * ":redir": start/stop redirection.
8309 */
8310 static void
8311ex_redir(eap)
8312 exarg_T *eap;
8313{
8314 char *mode;
8315 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008316 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317
8318 if (STRICMP(eap->arg, "END") == 0)
8319 close_redir();
8320 else
8321 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008322 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008324 ++arg;
8325 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008327 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 mode = "a";
8329 }
8330 else
8331 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008332 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333
8334 close_redir();
8335
8336 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008337 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 if (fname == NULL)
8339 return;
8340#ifdef FEAT_BROWSE
8341 if (cmdmod.browse)
8342 {
8343 char_u *browseFile;
8344
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008345 browseFile = do_browse(BROWSE_SAVE,
8346 (char_u *)_("Save Redirection"),
8347 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 if (browseFile == NULL)
8349 return; /* operation cancelled */
8350 vim_free(fname);
8351 fname = browseFile;
8352 eap->forceit = TRUE; /* since dialog already asked */
8353 }
8354#endif
8355
8356 redir_fd = open_exfile(fname, eap->forceit, mode);
8357 vim_free(fname);
8358 }
8359#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008360 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {
8362 /* redirect to a register a-z (resp. A-Z for appending) */
8363 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008364 ++arg;
8365 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008367 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008369 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008371 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008372 if (*arg == '>' && arg[1] == '>')
8373 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008374 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
8375 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008377 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008379 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008381 if (*arg == '>')
8382 arg++;
8383
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 /* make register empty */
8385 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
8386 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008387 }
8388 if (*arg != NUL)
8389 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008390 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008391 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008392 }
8393 }
8394 else if (*arg == '=' && arg[1] == '>')
8395 {
8396 int append;
8397
8398 /* redirect to a variable */
8399 close_redir();
8400 arg += 2;
8401
8402 if (*arg == '>')
8403 {
8404 ++arg;
8405 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 }
8407 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008408 append = FALSE;
8409
8410 if (var_redir_start(skipwhite(arg), append) == OK)
8411 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 }
8413#endif
8414
8415 /* TODO: redirect to a buffer */
8416
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008418 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 }
8420}
8421
8422/*
8423 * ":redraw": force redraw
8424 */
8425 static void
8426ex_redraw(eap)
8427 exarg_T *eap;
8428{
8429 int r = RedrawingDisabled;
8430 int p = p_lz;
8431
8432 RedrawingDisabled = 0;
8433 p_lz = FALSE;
8434 update_topline();
8435 update_screen(eap->forceit ? CLEAR :
8436#ifdef FEAT_VISUAL
8437 VIsual_active ? INVERTED :
8438#endif
8439 0);
8440#ifdef FEAT_TITLE
8441 if (need_maketitle)
8442 maketitle();
8443#endif
8444 RedrawingDisabled = r;
8445 p_lz = p;
8446
8447 /* Reset msg_didout, so that a message that's there is overwritten. */
8448 msg_didout = FALSE;
8449 msg_col = 0;
8450
8451 out_flush();
8452}
8453
8454/*
8455 * ":redrawstatus": force redraw of status line(s)
8456 */
8457/*ARGSUSED*/
8458 static void
8459ex_redrawstatus(eap)
8460 exarg_T *eap;
8461{
8462#if defined(FEAT_WINDOWS)
8463 int r = RedrawingDisabled;
8464 int p = p_lz;
8465
8466 RedrawingDisabled = 0;
8467 p_lz = FALSE;
8468 if (eap->forceit)
8469 status_redraw_all();
8470 else
8471 status_redraw_curbuf();
8472 update_screen(
8473# ifdef FEAT_VISUAL
8474 VIsual_active ? INVERTED :
8475# endif
8476 0);
8477 RedrawingDisabled = r;
8478 p_lz = p;
8479 out_flush();
8480#endif
8481}
8482
8483 static void
8484close_redir()
8485{
8486 if (redir_fd != NULL)
8487 {
8488 fclose(redir_fd);
8489 redir_fd = NULL;
8490 }
8491#ifdef FEAT_EVAL
8492 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008493 if (redir_vname)
8494 {
8495 var_redir_stop();
8496 redir_vname = 0;
8497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498#endif
8499}
8500
8501#if defined(FEAT_SESSION) && defined(USE_CRNL)
8502# define MKSESSION_NL
8503static int mksession_nl = FALSE; /* use NL only in put_eol() */
8504#endif
8505
8506/*
8507 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8508 */
8509 static void
8510ex_mkrc(eap)
8511 exarg_T *eap;
8512{
8513 FILE *fd;
8514 int failed = FALSE;
8515 char_u *fname;
8516#ifdef FEAT_BROWSE
8517 char_u *browseFile = NULL;
8518#endif
8519#ifdef FEAT_SESSION
8520 int view_session = FALSE;
8521 int using_vdir = FALSE; /* using 'viewdir'? */
8522 char_u *viewFile = NULL;
8523 unsigned *flagp;
8524#endif
8525
8526 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8527 {
8528#ifdef FEAT_SESSION
8529 view_session = TRUE;
8530#else
8531 ex_ni(eap);
8532 return;
8533#endif
8534 }
8535
8536#ifdef FEAT_SESSION
8537 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8538 if (eap->cmdidx == CMD_mkview
8539 && (*eap->arg == NUL
8540 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8541 {
8542 eap->forceit = TRUE;
8543 fname = get_view_file(*eap->arg);
8544 if (fname == NULL)
8545 return;
8546 viewFile = fname;
8547 using_vdir = TRUE;
8548 }
8549 else
8550#endif
8551 if (*eap->arg != NUL)
8552 fname = eap->arg;
8553 else if (eap->cmdidx == CMD_mkvimrc)
8554 fname = (char_u *)VIMRC_FILE;
8555#ifdef FEAT_SESSION
8556 else if (eap->cmdidx == CMD_mksession)
8557 fname = (char_u *)SESSION_FILE;
8558#endif
8559 else
8560 fname = (char_u *)EXRC_FILE;
8561
8562#ifdef FEAT_BROWSE
8563 if (cmdmod.browse)
8564 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008565 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566# ifdef FEAT_SESSION
8567 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8568 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8569# endif
8570 (char_u *)_("Save Setup"),
8571 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8572 if (browseFile == NULL)
8573 goto theend;
8574 fname = browseFile;
8575 eap->forceit = TRUE; /* since dialog already asked */
8576 }
8577#endif
8578
8579#if defined(FEAT_SESSION) && defined(vim_mkdir)
8580 /* When using 'viewdir' may have to create the directory. */
8581 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008582 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583#endif
8584
8585 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8586 if (fd != NULL)
8587 {
8588#ifdef FEAT_SESSION
8589 if (eap->cmdidx == CMD_mkview)
8590 flagp = &vop_flags;
8591 else
8592 flagp = &ssop_flags;
8593#endif
8594
8595#ifdef MKSESSION_NL
8596 /* "unix" in 'sessionoptions': use NL line separator */
8597 if (view_session && (*flagp & SSOP_UNIX))
8598 mksession_nl = TRUE;
8599#endif
8600
8601 /* Write the version command for :mkvimrc */
8602 if (eap->cmdidx == CMD_mkvimrc)
8603 (void)put_line(fd, "version 6.0");
8604
8605#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008606 if (eap->cmdidx == CMD_mksession)
8607 {
8608 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8609 failed = TRUE;
8610 }
8611
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 if (eap->cmdidx != CMD_mkview)
8613#endif
8614 {
8615 /* Write setting 'compatible' first, because it has side effects.
8616 * For that same reason only do it when needed. */
8617 if (p_cp)
8618 (void)put_line(fd, "if !&cp | set cp | endif");
8619 else
8620 (void)put_line(fd, "if &cp | set nocp | endif");
8621 }
8622
8623#ifdef FEAT_SESSION
8624 if (!view_session
8625 || (eap->cmdidx == CMD_mksession
8626 && (*flagp & SSOP_OPTIONS)))
8627#endif
8628 failed |= (makemap(fd, NULL) == FAIL
8629 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8630
8631#ifdef FEAT_SESSION
8632 if (!failed && view_session)
8633 {
8634 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8635 failed = TRUE;
8636 if (eap->cmdidx == CMD_mksession)
8637 {
8638 char_u dirnow[MAXPATHL]; /* current directory */
8639
8640 /*
8641 * Change to session file's dir.
8642 */
8643 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8644 || mch_chdir((char *)dirnow) != 0)
8645 *dirnow = NUL;
8646 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8647 {
8648 if (vim_chdirfile(fname) == OK)
8649 shorten_fnames(TRUE);
8650 }
8651 else if (*dirnow != NUL
8652 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8653 {
8654 (void)mch_chdir((char *)globaldir);
8655 shorten_fnames(TRUE);
8656 }
8657
8658 failed |= (makeopens(fd, dirnow) == FAIL);
8659
8660 /* restore original dir */
8661 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8662 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8663 {
8664 if (mch_chdir((char *)dirnow) != 0)
8665 EMSG(_(e_prev_dir));
8666 shorten_fnames(TRUE);
8667 }
8668 }
8669 else
8670 {
8671 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8672 }
8673 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8674 == FAIL)
8675 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008676 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8677 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008678 if (eap->cmdidx == CMD_mksession)
8679 {
8680 if (put_line(fd, "unlet SessionLoad") == FAIL)
8681 failed = TRUE;
8682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 }
8684#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008685 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8686 failed = TRUE;
8687
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 failed |= fclose(fd);
8689
8690 if (failed)
8691 EMSG(_(e_write));
8692#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8693 else if (eap->cmdidx == CMD_mksession)
8694 {
8695 /* successful session write - set this_session var */
8696 char_u tbuf[MAXPATHL];
8697
8698 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8699 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8700 }
8701#endif
8702#ifdef MKSESSION_NL
8703 mksession_nl = FALSE;
8704#endif
8705 }
8706
8707#ifdef FEAT_BROWSE
8708theend:
8709 vim_free(browseFile);
8710#endif
8711#ifdef FEAT_SESSION
8712 vim_free(viewFile);
8713#endif
8714}
8715
Bram Moolenaardf177f62005-02-22 08:39:57 +00008716#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8717 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008718/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008719 int
8720vim_mkdir_emsg(name, prot)
8721 char_u *name;
8722 int prot;
8723{
8724 if (vim_mkdir(name, prot) != 0)
8725 {
8726 EMSG2(_("E739: Cannot create directory: %s"), name);
8727 return FAIL;
8728 }
8729 return OK;
8730}
8731#endif
8732
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733/*
8734 * Open a file for writing for an Ex command, with some checks.
8735 * Return file descriptor, or NULL on failure.
8736 */
8737 FILE *
8738open_exfile(fname, forceit, mode)
8739 char_u *fname;
8740 int forceit;
8741 char *mode; /* "w" for create new file or "a" for append */
8742{
8743 FILE *fd;
8744
8745#ifdef UNIX
8746 /* with Unix it is possible to open a directory */
8747 if (mch_isdir(fname))
8748 {
8749 EMSG2(_(e_isadir2), fname);
8750 return NULL;
8751 }
8752#endif
8753 if (!forceit && *mode != 'a' && vim_fexists(fname))
8754 {
8755 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8756 return NULL;
8757 }
8758
8759 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8760 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8761
8762 return fd;
8763}
8764
8765/*
8766 * ":mark" and ":k".
8767 */
8768 static void
8769ex_mark(eap)
8770 exarg_T *eap;
8771{
8772 pos_T pos;
8773
8774 if (*eap->arg == NUL) /* No argument? */
8775 EMSG(_(e_argreq));
8776 else if (eap->arg[1] != NUL) /* more than one character? */
8777 EMSG(_(e_trailing));
8778 else
8779 {
8780 pos = curwin->w_cursor; /* save curwin->w_cursor */
8781 curwin->w_cursor.lnum = eap->line2;
8782 beginline(BL_WHITE | BL_FIX);
8783 if (setmark(*eap->arg) == FAIL) /* set mark */
8784 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8785 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8786 }
8787}
8788
8789/*
8790 * Update w_topline, w_leftcol and the cursor position.
8791 */
8792 void
8793update_topline_cursor()
8794{
8795 check_cursor(); /* put cursor on valid line */
8796 update_topline();
8797 if (!curwin->w_p_wrap)
8798 validate_cursor();
8799 update_curswant();
8800}
8801
8802#ifdef FEAT_EX_EXTRA
8803/*
8804 * ":normal[!] {commands}": Execute normal mode commands.
8805 */
8806 static void
8807ex_normal(eap)
8808 exarg_T *eap;
8809{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 int save_msg_scroll = msg_scroll;
8811 int save_restart_edit = restart_edit;
8812 int save_msg_didout = msg_didout;
8813 int save_State = State;
8814 tasave_T tabuf;
8815 int save_insertmode = p_im;
8816 int save_finish_op = finish_op;
8817#ifdef FEAT_MBYTE
8818 char_u *arg = NULL;
8819 int l;
8820 char_u *p;
8821#endif
8822
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008823 if (ex_normal_lock > 0)
8824 {
8825 EMSG(_(e_secure));
8826 return;
8827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 if (ex_normal_busy >= p_mmd)
8829 {
8830 EMSG(_("E192: Recursive use of :normal too deep"));
8831 return;
8832 }
8833 ++ex_normal_busy;
8834
8835 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8836 restart_edit = 0; /* don't go to Insert mode */
8837 p_im = FALSE; /* don't use 'insertmode' */
8838
8839#ifdef FEAT_MBYTE
8840 /*
8841 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8842 * this for the K_SPECIAL leading byte, otherwise special keys will not
8843 * work.
8844 */
8845 if (has_mbyte)
8846 {
8847 int len = 0;
8848
8849 /* Count the number of characters to be escaped. */
8850 for (p = eap->arg; *p != NUL; ++p)
8851 {
8852# ifdef FEAT_GUI
8853 if (*p == CSI) /* leadbyte CSI */
8854 len += 2;
8855# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008856 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8858# ifdef FEAT_GUI
8859 || *p == CSI
8860# endif
8861 )
8862 len += 2;
8863 }
8864 if (len > 0)
8865 {
8866 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8867 if (arg != NULL)
8868 {
8869 len = 0;
8870 for (p = eap->arg; *p != NUL; ++p)
8871 {
8872 arg[len++] = *p;
8873# ifdef FEAT_GUI
8874 if (*p == CSI)
8875 {
8876 arg[len++] = KS_EXTRA;
8877 arg[len++] = (int)KE_CSI;
8878 }
8879# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008880 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 {
8882 arg[len++] = *++p;
8883 if (*p == K_SPECIAL)
8884 {
8885 arg[len++] = KS_SPECIAL;
8886 arg[len++] = KE_FILLER;
8887 }
8888# ifdef FEAT_GUI
8889 else if (*p == CSI)
8890 {
8891 arg[len++] = KS_EXTRA;
8892 arg[len++] = (int)KE_CSI;
8893 }
8894# endif
8895 }
8896 arg[len] = NUL;
8897 }
8898 }
8899 }
8900 }
8901#endif
8902
8903 /*
8904 * Save the current typeahead. This is required to allow using ":normal"
8905 * from an event handler and makes sure we don't hang when the argument
8906 * ends with half a command.
8907 */
8908 save_typeahead(&tabuf);
8909 if (tabuf.typebuf_valid)
8910 {
8911 /*
8912 * Repeat the :normal command for each line in the range. When no
8913 * range given, execute it just once, without positioning the cursor
8914 * first.
8915 */
8916 do
8917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 if (eap->addr_count != 0)
8919 {
8920 curwin->w_cursor.lnum = eap->line1++;
8921 curwin->w_cursor.col = 0;
8922 }
8923
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008924 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925#ifdef FEAT_MBYTE
8926 arg != NULL ? arg :
8927#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008928 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 }
8930 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8931 }
8932
8933 /* Might not return to the main loop when in an event handler. */
8934 update_topline_cursor();
8935
8936 /* Restore the previous typeahead. */
8937 restore_typeahead(&tabuf);
8938
8939 --ex_normal_busy;
8940 msg_scroll = save_msg_scroll;
8941 restart_edit = save_restart_edit;
8942 p_im = save_insertmode;
8943 finish_op = save_finish_op;
8944 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8945
8946 /* Restore the state (needed when called from a function executed for
8947 * 'indentexpr'). */
8948 State = save_State;
8949#ifdef FEAT_MBYTE
8950 vim_free(arg);
8951#endif
8952}
8953
8954/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008955 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 */
8957 static void
8958ex_startinsert(eap)
8959 exarg_T *eap;
8960{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008961 if (eap->forceit)
8962 {
8963 coladvance((colnr_T)MAXCOL);
8964 curwin->w_curswant = MAXCOL;
8965 curwin->w_set_curswant = FALSE;
8966 }
8967
Bram Moolenaara40c5002005-01-09 21:16:21 +00008968 /* Ignore the command when already in Insert mode. Inserting an
8969 * expression register that invokes a function can do this. */
8970 if (State & INSERT)
8971 return;
8972
Bram Moolenaar64969662005-12-14 21:59:55 +00008973 if (eap->cmdidx == CMD_startinsert)
8974 restart_edit = 'a';
8975 else if (eap->cmdidx == CMD_startreplace)
8976 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008978 restart_edit = 'V';
8979
8980 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008982 if (eap->cmdidx == CMD_startinsert)
8983 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 curwin->w_curswant = 0; /* avoid MAXCOL */
8985 }
8986}
8987
8988/*
8989 * ":stopinsert"
8990 */
8991/*ARGSUSED*/
8992 static void
8993ex_stopinsert(eap)
8994 exarg_T *eap;
8995{
8996 restart_edit = 0;
8997 stop_insert_mode = TRUE;
8998}
8999#endif
9000
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009001#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9002/*
9003 * Execute normal mode command "cmd".
9004 * "remap" can be REMAP_NONE or REMAP_YES.
9005 */
9006 void
9007exec_normal_cmd(cmd, remap, silent)
9008 char_u *cmd;
9009 int remap;
9010 int silent;
9011{
9012 oparg_T oa;
9013
9014 /*
9015 * Stuff the argument into the typeahead buffer.
9016 * Execute normal_cmd() until there is no typeahead left.
9017 */
9018 clear_oparg(&oa);
9019 finish_op = FALSE;
9020 ins_typebuf(cmd, remap, 0, TRUE, silent);
9021 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9022 && !got_int)
9023 {
9024 update_topline_cursor();
9025 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
9026 }
9027}
9028#endif
9029
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030#ifdef FEAT_FIND_ID
9031 static void
9032ex_checkpath(eap)
9033 exarg_T *eap;
9034{
9035 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9036 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9037 (linenr_T)1, (linenr_T)MAXLNUM);
9038}
9039
9040#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9041/*
9042 * ":psearch"
9043 */
9044 static void
9045ex_psearch(eap)
9046 exarg_T *eap;
9047{
9048 g_do_tagpreview = p_pvh;
9049 ex_findpat(eap);
9050 g_do_tagpreview = 0;
9051}
9052#endif
9053
9054 static void
9055ex_findpat(eap)
9056 exarg_T *eap;
9057{
9058 int whole = TRUE;
9059 long n;
9060 char_u *p;
9061 int action;
9062
9063 switch (cmdnames[eap->cmdidx].cmd_name[2])
9064 {
9065 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9066 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9067 action = ACTION_GOTO;
9068 else
9069 action = ACTION_SHOW;
9070 break;
9071 case 'i': /* ":ilist" and ":dlist" */
9072 action = ACTION_SHOW_ALL;
9073 break;
9074 case 'u': /* ":ijump" and ":djump" */
9075 action = ACTION_GOTO;
9076 break;
9077 default: /* ":isplit" and ":dsplit" */
9078 action = ACTION_SPLIT;
9079 break;
9080 }
9081
9082 n = 1;
9083 if (vim_isdigit(*eap->arg)) /* get count */
9084 {
9085 n = getdigits(&eap->arg);
9086 eap->arg = skipwhite(eap->arg);
9087 }
9088 if (*eap->arg == '/') /* Match regexp, not just whole words */
9089 {
9090 whole = FALSE;
9091 ++eap->arg;
9092 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9093 if (*p)
9094 {
9095 *p++ = NUL;
9096 p = skipwhite(p);
9097
9098 /* Check for trailing illegal characters */
9099 if (!ends_excmd(*p))
9100 eap->errmsg = e_trailing;
9101 else
9102 eap->nextcmd = check_nextcmd(p);
9103 }
9104 }
9105 if (!eap->skip)
9106 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9107 whole, !eap->forceit,
9108 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9109 n, action, eap->line1, eap->line2);
9110}
9111#endif
9112
9113#ifdef FEAT_WINDOWS
9114
9115# ifdef FEAT_QUICKFIX
9116/*
9117 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9118 */
9119 static void
9120ex_ptag(eap)
9121 exarg_T *eap;
9122{
9123 g_do_tagpreview = p_pvh;
9124 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9125}
9126
9127/*
9128 * ":pedit"
9129 */
9130 static void
9131ex_pedit(eap)
9132 exarg_T *eap;
9133{
9134 win_T *curwin_save = curwin;
9135
9136 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009137 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 keep_help_flag = curwin_save->w_buffer->b_help;
9139 do_exedit(eap, NULL);
9140 keep_help_flag = FALSE;
9141 if (curwin != curwin_save && win_valid(curwin_save))
9142 {
9143 /* Return cursor to where we were */
9144 validate_cursor();
9145 redraw_later(VALID);
9146 win_enter(curwin_save, TRUE);
9147 }
9148 g_do_tagpreview = 0;
9149}
9150# endif
9151
9152/*
9153 * ":stag", ":stselect" and ":stjump".
9154 */
9155 static void
9156ex_stag(eap)
9157 exarg_T *eap;
9158{
9159 postponed_split = -1;
9160 postponed_split_flags = cmdmod.split;
9161 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9162 postponed_split_flags = 0;
9163}
9164#endif
9165
9166/*
9167 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9168 */
9169 static void
9170ex_tag(eap)
9171 exarg_T *eap;
9172{
9173 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9174}
9175
9176 static void
9177ex_tag_cmd(eap, name)
9178 exarg_T *eap;
9179 char_u *name;
9180{
9181 int cmd;
9182
9183 switch (name[1])
9184 {
9185 case 'j': cmd = DT_JUMP; /* ":tjump" */
9186 break;
9187 case 's': cmd = DT_SELECT; /* ":tselect" */
9188 break;
9189 case 'p': cmd = DT_PREV; /* ":tprevious" */
9190 break;
9191 case 'N': cmd = DT_PREV; /* ":tNext" */
9192 break;
9193 case 'n': cmd = DT_NEXT; /* ":tnext" */
9194 break;
9195 case 'o': cmd = DT_POP; /* ":pop" */
9196 break;
9197 case 'f': /* ":tfirst" */
9198 case 'r': cmd = DT_FIRST; /* ":trewind" */
9199 break;
9200 case 'l': cmd = DT_LAST; /* ":tlast" */
9201 break;
9202 default: /* ":tag" */
9203#ifdef FEAT_CSCOPE
9204 if (p_cst)
9205 {
9206 do_cstag(eap);
9207 return;
9208 }
9209#endif
9210 cmd = DT_TAG;
9211 break;
9212 }
9213
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009214 if (name[0] == 'l')
9215 {
9216#ifndef FEAT_QUICKFIX
9217 ex_ni(eap);
9218 return;
9219#else
9220 cmd = DT_LTAG;
9221#endif
9222 }
9223
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9225 eap->forceit, TRUE);
9226}
9227
9228/*
9229 * Evaluate cmdline variables.
9230 *
9231 * change '%' to curbuf->b_ffname
9232 * '#' to curwin->w_altfile
9233 * '<cword>' to word under the cursor
9234 * '<cWORD>' to WORD under the cursor
9235 * '<cfile>' to path name under the cursor
9236 * '<sfile>' to sourced file name
9237 * '<afile>' to file name for autocommand
9238 * '<abuf>' to buffer number for autocommand
9239 * '<amatch>' to matching name for autocommand
9240 *
9241 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9242 * "" for error without a message) and NULL is returned.
9243 * Returns an allocated string if a valid match was found.
9244 * Returns NULL if no match was found. "usedlen" then still contains the
9245 * number of characters to skip.
9246 */
9247 char_u *
9248eval_vars(src, usedlen, lnump, errormsg, srcstart)
9249 char_u *src; /* pointer into commandline */
9250 int *usedlen; /* characters after src that are used */
9251 linenr_T *lnump; /* line number for :e command, or NULL */
9252 char_u **errormsg; /* pointer to error message */
9253 char_u *srcstart; /* beginning of valid memory for src */
9254{
9255 int i;
9256 char_u *s;
9257 char_u *result;
9258 char_u *resultbuf = NULL;
9259 int resultlen;
9260 buf_T *buf;
9261 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9262 int spec_idx;
9263#ifdef FEAT_MODIFY_FNAME
9264 int skip_mod = FALSE;
9265#endif
9266 static char *(spec_str[]) =
9267 {
9268 "%",
9269#define SPEC_PERC 0
9270 "#",
9271#define SPEC_HASH 1
9272 "<cword>", /* cursor word */
9273#define SPEC_CWORD 2
9274 "<cWORD>", /* cursor WORD */
9275#define SPEC_CCWORD 3
9276 "<cfile>", /* cursor path name */
9277#define SPEC_CFILE 4
9278 "<sfile>", /* ":so" file name */
9279#define SPEC_SFILE 5
9280#ifdef FEAT_AUTOCMD
9281 "<afile>", /* autocommand file name */
9282# define SPEC_AFILE 6
9283 "<abuf>", /* autocommand buffer number */
9284# define SPEC_ABUF 7
9285 "<amatch>", /* autocommand match name */
9286# define SPEC_AMATCH 8
9287#endif
9288#ifdef FEAT_CLIENTSERVER
9289 "<client>"
9290# define SPEC_CLIENT 9
9291#endif
9292 };
9293#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9294
9295#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9296 char_u strbuf[30];
9297#endif
9298
9299 *errormsg = NULL;
9300
9301 /*
9302 * Check if there is something to do.
9303 */
9304 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
9305 {
9306 *usedlen = (int)STRLEN(spec_str[spec_idx]);
9307 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
9308 break;
9309 }
9310 if (spec_idx == SPEC_COUNT) /* no match */
9311 {
9312 *usedlen = 1;
9313 return NULL;
9314 }
9315
9316 /*
9317 * Skip when preceded with a backslash "\%" and "\#".
9318 * Note: In "\\%" the % is also not recognized!
9319 */
9320 if (src > srcstart && src[-1] == '\\')
9321 {
9322 *usedlen = 0;
9323 STRCPY(src - 1, src); /* remove backslash */
9324 return NULL;
9325 }
9326
9327 /*
9328 * word or WORD under cursor
9329 */
9330 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9331 {
9332 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9333 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9334 if (resultlen == 0)
9335 {
9336 *errormsg = (char_u *)"";
9337 return NULL;
9338 }
9339 }
9340
9341 /*
9342 * '#': Alternate file name
9343 * '%': Current file name
9344 * File name under the cursor
9345 * File name for autocommand
9346 * and following modifiers
9347 */
9348 else
9349 {
9350 switch (spec_idx)
9351 {
9352 case SPEC_PERC: /* '%': current file */
9353 if (curbuf->b_fname == NULL)
9354 {
9355 result = (char_u *)"";
9356 valid = 0; /* Must have ":p:h" to be valid */
9357 }
9358 else
9359#ifdef RISCOS
9360 /* Always use the full path for RISC OS if possible. */
9361 result = curbuf->b_ffname;
9362 if (result == NULL)
9363 result = curbuf->b_fname;
9364#else
9365 result = curbuf->b_fname;
9366#endif
9367 break;
9368
9369 case SPEC_HASH: /* '#' or "#99": alternate file */
9370 if (src[1] == '#') /* "##": the argument list */
9371 {
9372 result = arg_all();
9373 resultbuf = result;
9374 *usedlen = 2;
9375#ifdef FEAT_MODIFY_FNAME
9376 skip_mod = TRUE;
9377#endif
9378 break;
9379 }
9380 s = src + 1;
9381 i = (int)getdigits(&s);
9382 *usedlen = (int)(s - src); /* length of what we expand */
9383
9384 buf = buflist_findnr(i);
9385 if (buf == NULL)
9386 {
9387 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9388 return NULL;
9389 }
9390 if (lnump != NULL)
9391 *lnump = ECMD_LAST;
9392 if (buf->b_fname == NULL)
9393 {
9394 result = (char_u *)"";
9395 valid = 0; /* Must have ":p:h" to be valid */
9396 }
9397 else
9398 result = buf->b_fname;
9399 break;
9400
9401#ifdef FEAT_SEARCHPATH
9402 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009403 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 if (result == NULL)
9405 {
9406 *errormsg = (char_u *)"";
9407 return NULL;
9408 }
9409 resultbuf = result; /* remember allocated string */
9410 break;
9411#endif
9412
9413#ifdef FEAT_AUTOCMD
9414 case SPEC_AFILE: /* file name for autocommand */
9415 result = autocmd_fname;
9416 if (result == NULL)
9417 {
9418 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9419 return NULL;
9420 }
9421 break;
9422
9423 case SPEC_ABUF: /* buffer number for autocommand */
9424 if (autocmd_bufnr <= 0)
9425 {
9426 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9427 return NULL;
9428 }
9429 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9430 result = strbuf;
9431 break;
9432
9433 case SPEC_AMATCH: /* match name for autocommand */
9434 result = autocmd_match;
9435 if (result == NULL)
9436 {
9437 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9438 return NULL;
9439 }
9440 break;
9441
9442#endif
9443 case SPEC_SFILE: /* file name for ":so" command */
9444 result = sourcing_name;
9445 if (result == NULL)
9446 {
9447 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9448 return NULL;
9449 }
9450 break;
9451#if defined(FEAT_CLIENTSERVER)
9452 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009453 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9454 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 result = strbuf;
9456 break;
9457#endif
9458 }
9459
9460 resultlen = (int)STRLEN(result); /* length of new string */
9461 if (src[*usedlen] == '<') /* remove the file name extension */
9462 {
9463 ++*usedlen;
9464#ifdef RISCOS
9465 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9466#else
9467 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9468#endif
9469 resultlen = (int)(s - result);
9470 }
9471#ifdef FEAT_MODIFY_FNAME
9472 else if (!skip_mod)
9473 {
9474 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9475 &resultlen);
9476 if (result == NULL)
9477 {
9478 *errormsg = (char_u *)"";
9479 return NULL;
9480 }
9481 }
9482#endif
9483 }
9484
9485 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9486 {
9487 if (valid != VALID_HEAD + VALID_PATH)
9488 /* xgettext:no-c-format */
9489 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9490 else
9491 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9492 result = NULL;
9493 }
9494 else
9495 result = vim_strnsave(result, resultlen);
9496 vim_free(resultbuf);
9497 return result;
9498}
9499
9500/*
9501 * Concatenate all files in the argument list, separated by spaces, and return
9502 * it in one allocated string.
9503 * Spaces and backslashes in the file names are escaped with a backslash.
9504 * Returns NULL when out of memory.
9505 */
9506 static char_u *
9507arg_all()
9508{
9509 int len;
9510 int idx;
9511 char_u *retval = NULL;
9512 char_u *p;
9513
9514 /*
9515 * Do this loop two times:
9516 * first time: compute the total length
9517 * second time: concatenate the names
9518 */
9519 for (;;)
9520 {
9521 len = 0;
9522 for (idx = 0; idx < ARGCOUNT; ++idx)
9523 {
9524 p = alist_name(&ARGLIST[idx]);
9525 if (p != NULL)
9526 {
9527 if (len > 0)
9528 {
9529 /* insert a space in between names */
9530 if (retval != NULL)
9531 retval[len] = ' ';
9532 ++len;
9533 }
9534 for ( ; *p != NUL; ++p)
9535 {
9536 if (*p == ' ' || *p == '\\')
9537 {
9538 /* insert a backslash */
9539 if (retval != NULL)
9540 retval[len] = '\\';
9541 ++len;
9542 }
9543 if (retval != NULL)
9544 retval[len] = *p;
9545 ++len;
9546 }
9547 }
9548 }
9549
9550 /* second time: break here */
9551 if (retval != NULL)
9552 {
9553 retval[len] = NUL;
9554 break;
9555 }
9556
9557 /* allocate memory */
9558 retval = alloc(len + 1);
9559 if (retval == NULL)
9560 break;
9561 }
9562
9563 return retval;
9564}
9565
9566#if defined(FEAT_AUTOCMD) || defined(PROTO)
9567/*
9568 * Expand the <sfile> string in "arg".
9569 *
9570 * Returns an allocated string, or NULL for any error.
9571 */
9572 char_u *
9573expand_sfile(arg)
9574 char_u *arg;
9575{
9576 char_u *errormsg;
9577 int len;
9578 char_u *result;
9579 char_u *newres;
9580 char_u *repl;
9581 int srclen;
9582 char_u *p;
9583
9584 result = vim_strsave(arg);
9585 if (result == NULL)
9586 return NULL;
9587
9588 for (p = result; *p; )
9589 {
9590 if (STRNCMP(p, "<sfile>", 7) != 0)
9591 ++p;
9592 else
9593 {
9594 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9595 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9596 if (errormsg != NULL)
9597 {
9598 if (*errormsg)
9599 emsg(errormsg);
9600 vim_free(result);
9601 return NULL;
9602 }
9603 if (repl == NULL) /* no match (cannot happen) */
9604 {
9605 p += srclen;
9606 continue;
9607 }
9608 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9609 newres = alloc(len);
9610 if (newres == NULL)
9611 {
9612 vim_free(repl);
9613 vim_free(result);
9614 return NULL;
9615 }
9616 mch_memmove(newres, result, (size_t)(p - result));
9617 STRCPY(newres + (p - result), repl);
9618 len = (int)STRLEN(newres);
9619 STRCAT(newres, p + srclen);
9620 vim_free(repl);
9621 vim_free(result);
9622 result = newres;
9623 p = newres + len; /* continue after the match */
9624 }
9625 }
9626
9627 return result;
9628}
9629#endif
9630
9631#ifdef FEAT_SESSION
9632static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9633static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9634static frame_T *ses_skipframe __ARGS((frame_T *fr));
9635static int ses_do_frame __ARGS((frame_T *fr));
9636static int ses_do_win __ARGS((win_T *wp));
9637static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9638static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9639static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9640
9641/*
9642 * Write openfile commands for the current buffers to an .exrc file.
9643 * Return FAIL on error, OK otherwise.
9644 */
9645 static int
9646makeopens(fd, dirnow)
9647 FILE *fd;
9648 char_u *dirnow; /* Current directory name */
9649{
9650 buf_T *buf;
9651 int only_save_windows = TRUE;
9652 int nr;
9653 int cnr = 1;
9654 int restore_size = TRUE;
9655 win_T *wp;
9656 char_u *sname;
9657 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009658 tabpage_T *old_curtab = curtab;
9659 int tabnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660
9661 if (ssop_flags & SSOP_BUFFERS)
9662 only_save_windows = FALSE; /* Save ALL buffers */
9663
9664 /*
9665 * Begin by setting the this_session variable, and then other
9666 * sessionable variables.
9667 */
9668#ifdef FEAT_EVAL
9669 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9670 return FAIL;
9671 if (ssop_flags & SSOP_GLOBALS)
9672 if (store_session_globals(fd) == FAIL)
9673 return FAIL;
9674#endif
9675
9676 /*
9677 * Close all windows but one.
9678 */
9679 if (put_line(fd, "silent only") == FAIL)
9680 return FAIL;
9681
9682 /*
9683 * Now a :cd command to the session directory or the current directory
9684 */
9685 if (ssop_flags & SSOP_SESDIR)
9686 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009687 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9688 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 return FAIL;
9690 }
9691 else if (ssop_flags & SSOP_CURDIR)
9692 {
9693 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9694 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009695 || fputs("cd ", fd) < 0
9696 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9697 || put_eol(fd) == FAIL)
9698 {
9699 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 vim_free(sname);
9703 }
9704
9705 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009706 * If there is an empty, unnamed buffer we will wipe it out later.
9707 * Remember the buffer number.
9708 */
9709 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9710 return FAIL;
9711 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9712 return FAIL;
9713 if (put_line(fd, "endif") == FAIL)
9714 return FAIL;
9715
9716 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 * Now save the current files, current buffer first.
9718 */
9719 if (put_line(fd, "set shortmess=aoO") == FAIL)
9720 return FAIL;
9721
9722 /* Now put the other buffers into the buffer list */
9723 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9724 {
9725 if (!(only_save_windows && buf->b_nwindows == 0)
9726 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9727 && buf->b_fname != NULL
9728 && buf->b_p_bl)
9729 {
9730 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9731 : buf->b_wininfo->wi_fpos.lnum) < 0
9732 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9733 return FAIL;
9734 }
9735 }
9736
9737 /* the global argument list */
9738 if (ses_arglist(fd, "args", &global_alist.al_ga,
9739 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9740 return FAIL;
9741
9742 if (ssop_flags & SSOP_RESIZE)
9743 {
9744 /* Note: after the restore we still check it worked!*/
9745 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9746 || put_eol(fd) == FAIL)
9747 return FAIL;
9748 }
9749
9750#ifdef FEAT_GUI
9751 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9752 {
9753 int x, y;
9754
9755 if (gui_mch_get_winpos(&x, &y) == OK)
9756 {
9757 /* Note: after the restore we still check it worked!*/
9758 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9759 return FAIL;
9760 }
9761 }
9762#endif
9763
9764 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009765 * May repeat putting Windows for each tab, when "tabpages" is in
9766 * 'sessionoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009768 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00009770 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00009772 goto_tabpage(tabnr);
9773 if (tabnr > 1 && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776
Bram Moolenaar18144c82006-04-12 21:52:12 +00009777 /*
9778 * Before creating the window layout, try loading one file. If this
9779 * is aborted we don't end up with a number of useless windows.
9780 * This may have side effects! (e.g., compressed or network file).
9781 */
9782 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9783 {
9784 if (ses_do_win(wp)
9785 && wp->w_buffer->b_ffname != NULL
9786 && !wp->w_buffer->b_help
9787#ifdef FEAT_QUICKFIX
9788 && !bt_nofile(wp->w_buffer)
9789#endif
9790 )
9791 {
9792 if (fputs("edit ", fd) < 0
9793 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9794 return FAIL;
9795 if (!wp->w_arg_idx_invalid)
9796 edited_win = wp;
9797 break;
9798 }
9799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800
Bram Moolenaar18144c82006-04-12 21:52:12 +00009801 /*
9802 * Save current window layout.
9803 */
9804 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009806 if (ses_win_rec(fd, topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009808 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9809 return FAIL;
9810 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9811 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812
Bram Moolenaar18144c82006-04-12 21:52:12 +00009813 /*
9814 * Check if window sizes can be restored (no windows omitted).
9815 * Remember the window number of the current window after restoring.
9816 */
9817 nr = 0;
9818 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9819 {
9820 if (ses_do_win(wp))
9821 ++nr;
9822 else
9823 restore_size = FALSE;
9824 if (curwin == wp)
9825 cnr = nr;
9826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827
Bram Moolenaar18144c82006-04-12 21:52:12 +00009828 /* Go to the first window. */
9829 if (put_line(fd, "wincmd t") == FAIL)
9830 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009831
Bram Moolenaar18144c82006-04-12 21:52:12 +00009832 /*
9833 * If more than one window, see if sizes can be restored.
9834 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9835 * resized when moving between windows.
9836 * Do this before restoring the view, so that the topline and the
9837 * cursor can be set. This is done again below.
9838 */
9839 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9840 return FAIL;
9841 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9842 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843
Bram Moolenaar18144c82006-04-12 21:52:12 +00009844 /*
9845 * Restore the view of the window (options, file, cursor, etc.).
9846 */
9847 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9848 {
9849 if (!ses_do_win(wp))
9850 continue;
9851 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9852 return FAIL;
9853 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9854 return FAIL;
9855 }
9856
9857 /*
9858 * Restore cursor to the current window if it's not the first one.
9859 */
9860 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
9861 || put_eol(fd) == FAIL))
9862 return FAIL;
9863
9864 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009865 * Restore window sizes again after jumping around in windows, because
9866 * the current window has a minimum size while others may not.
9867 */
9868 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9869 return FAIL;
9870
Bram Moolenaar18144c82006-04-12 21:52:12 +00009871 /* Don't continue in another tab page when doing only the current one
9872 * or when at the last tab page. */
9873 if (!(ssop_flags & SSOP_TABPAGES) || curtab->tp_next == NULL)
9874 break;
9875 }
9876
9877 if (ssop_flags & SSOP_TABPAGES)
9878 {
9879 if (valid_tabpage(old_curtab))
9880 goto_tabpage_tp(old_curtab);
9881 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
9882 || put_eol(fd) == FAIL)
9883 return FAIL;
9884 }
9885
Bram Moolenaar9c102382006-05-03 21:26:49 +00009886 /*
9887 * Wipe out an empty unnamed buffer we started in.
9888 */
9889 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9890 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00009891 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +00009892 return FAIL;
9893 if (put_line(fd, "endif") == FAIL)
9894 return FAIL;
9895 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9896 return FAIL;
9897
9898 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9899 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9900 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9901 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902
9903 /*
9904 * Lastly, execute the x.vim file if it exists.
9905 */
9906 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9907 || put_line(fd, "if file_readable(s:sx)") == FAIL
9908 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9909 || put_line(fd, "endif") == FAIL)
9910 return FAIL;
9911
9912 return OK;
9913}
9914
9915 static int
9916ses_winsizes(fd, restore_size)
9917 FILE *fd;
9918 int restore_size;
9919{
9920 int n = 0;
9921 win_T *wp;
9922
9923 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9924 {
9925 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9926 {
9927 if (!ses_do_win(wp))
9928 continue;
9929 ++n;
9930
9931 /* restore height when not full height */
9932 if (wp->w_height + wp->w_status_height < topframe->fr_height
9933 && (fprintf(fd,
9934 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9935 n, (long)wp->w_height, Rows / 2, Rows) < 0
9936 || put_eol(fd) == FAIL))
9937 return FAIL;
9938
9939 /* restore width when not full width */
9940 if (wp->w_width < Columns && (fprintf(fd,
9941 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9942 n, (long)wp->w_width, Columns / 2, Columns) < 0
9943 || put_eol(fd) == FAIL))
9944 return FAIL;
9945 }
9946 }
9947 else
9948 {
9949 /* Just equalise window sizes */
9950 if (put_line(fd, "wincmd =") == FAIL)
9951 return FAIL;
9952 }
9953 return OK;
9954}
9955
9956/*
9957 * Write commands to "fd" to recursively create windows for frame "fr",
9958 * horizontally and vertically split.
9959 * After the commands the last window in the frame is the current window.
9960 * Returns FAIL when writing the commands to "fd" fails.
9961 */
9962 static int
9963ses_win_rec(fd, fr)
9964 FILE *fd;
9965 frame_T *fr;
9966{
9967 frame_T *frc;
9968 int count = 0;
9969
9970 if (fr->fr_layout != FR_LEAF)
9971 {
9972 /* Find first frame that's not skipped and then create a window for
9973 * each following one (first frame is already there). */
9974 frc = ses_skipframe(fr->fr_child);
9975 if (frc != NULL)
9976 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9977 {
9978 /* Make window as big as possible so that we have lots of room
9979 * to split. */
9980 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9981 || put_line(fd, fr->fr_layout == FR_COL
9982 ? "split" : "vsplit") == FAIL)
9983 return FAIL;
9984 ++count;
9985 }
9986
9987 /* Go back to the first window. */
9988 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9989 ? "%dwincmd k" : "%dwincmd h", count) < 0
9990 || put_eol(fd) == FAIL))
9991 return FAIL;
9992
9993 /* Recursively create frames/windows in each window of this column or
9994 * row. */
9995 frc = ses_skipframe(fr->fr_child);
9996 while (frc != NULL)
9997 {
9998 ses_win_rec(fd, frc);
9999 frc = ses_skipframe(frc->fr_next);
10000 /* Go to next window. */
10001 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10002 return FAIL;
10003 }
10004 }
10005 return OK;
10006}
10007
10008/*
10009 * Skip frames that don't contain windows we want to save in the Session.
10010 * Returns NULL when there none.
10011 */
10012 static frame_T *
10013ses_skipframe(fr)
10014 frame_T *fr;
10015{
10016 frame_T *frc;
10017
10018 for (frc = fr; frc != NULL; frc = frc->fr_next)
10019 if (ses_do_frame(frc))
10020 break;
10021 return frc;
10022}
10023
10024/*
10025 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10026 * the Session.
10027 */
10028 static int
10029ses_do_frame(fr)
10030 frame_T *fr;
10031{
10032 frame_T *frc;
10033
10034 if (fr->fr_layout == FR_LEAF)
10035 return ses_do_win(fr->fr_win);
10036 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10037 if (ses_do_frame(frc))
10038 return TRUE;
10039 return FALSE;
10040}
10041
10042/*
10043 * Return non-zero if window "wp" is to be stored in the Session.
10044 */
10045 static int
10046ses_do_win(wp)
10047 win_T *wp;
10048{
10049 if (wp->w_buffer->b_fname == NULL
10050#ifdef FEAT_QUICKFIX
10051 /* When 'buftype' is "nofile" can't restore the window contents. */
10052 || bt_nofile(wp->w_buffer)
10053#endif
10054 )
10055 return (ssop_flags & SSOP_BLANK);
10056 if (wp->w_buffer->b_help)
10057 return (ssop_flags & SSOP_HELP);
10058 return TRUE;
10059}
10060
10061/*
10062 * Write commands to "fd" to restore the view of a window.
10063 * Caller must make sure 'scrolloff' is zero.
10064 */
10065 static int
10066put_view(fd, wp, add_edit, flagp)
10067 FILE *fd;
10068 win_T *wp;
10069 int add_edit; /* add ":edit" command to view */
10070 unsigned *flagp; /* vop_flags or ssop_flags */
10071{
10072 win_T *save_curwin;
10073 int f;
10074 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010075 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076
10077 /* Always restore cursor position for ":mksession". For ":mkview" only
10078 * when 'viewoptions' contains "cursor". */
10079 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10080
10081 /*
10082 * Local argument list.
10083 */
10084 if (wp->w_alist == &global_alist)
10085 {
10086 if (put_line(fd, "argglobal") == FAIL)
10087 return FAIL;
10088 }
10089 else
10090 {
10091 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10092 flagp == &vop_flags
10093 || !(*flagp & SSOP_CURDIR)
10094 || wp->w_localdir != NULL, flagp) == FAIL)
10095 return FAIL;
10096 }
10097
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010098 /* Only when part of a session: restore the argument index. Some
10099 * arguments may have been deleted, check if the index is valid. */
10100 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
10101 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 {
10103 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
10104 || put_eol(fd) == FAIL)
10105 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010106 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 }
10108
10109 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010110 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 {
10112 /*
10113 * Load the file.
10114 */
10115 if (wp->w_buffer->b_ffname != NULL
10116#ifdef FEAT_QUICKFIX
10117 && !bt_nofile(wp->w_buffer)
10118#endif
10119 )
10120 {
10121 /*
10122 * Editing a file in this buffer: use ":edit file".
10123 * This may have side effects! (e.g., compressed or network file).
10124 */
10125 if (fputs("edit ", fd) < 0
10126 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10127 return FAIL;
10128 }
10129 else
10130 {
10131 /* No file in this buffer, just make it empty. */
10132 if (put_line(fd, "enew") == FAIL)
10133 return FAIL;
10134#ifdef FEAT_QUICKFIX
10135 if (wp->w_buffer->b_ffname != NULL)
10136 {
10137 /* The buffer does have a name, but it's not a file name. */
10138 if (fputs("file ", fd) < 0
10139 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10140 return FAIL;
10141 }
10142#endif
10143 do_cursor = FALSE;
10144 }
10145 }
10146
10147 /*
10148 * Local mappings and abbreviations.
10149 */
10150 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10151 && makemap(fd, wp->w_buffer) == FAIL)
10152 return FAIL;
10153
10154 /*
10155 * Local options. Need to go to the window temporarily.
10156 * Store only local values when using ":mkview" and when ":mksession" is
10157 * used and 'sessionoptions' doesn't include "options".
10158 * Some folding options are always stored when "folds" is included,
10159 * otherwise the folds would not be restored correctly.
10160 */
10161 save_curwin = curwin;
10162 curwin = wp;
10163 curbuf = curwin->w_buffer;
10164 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10165 f = makeset(fd, OPT_LOCAL,
10166 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10167#ifdef FEAT_FOLDING
10168 else if (*flagp & SSOP_FOLDS)
10169 f = makefoldset(fd);
10170#endif
10171 else
10172 f = OK;
10173 curwin = save_curwin;
10174 curbuf = curwin->w_buffer;
10175 if (f == FAIL)
10176 return FAIL;
10177
10178#ifdef FEAT_FOLDING
10179 /*
10180 * Save Folds when 'buftype' is empty and for help files.
10181 */
10182 if ((*flagp & SSOP_FOLDS)
10183 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010184# ifdef FEAT_QUICKFIX
10185 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10186# endif
10187 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 {
10189 if (put_folds(fd, wp) == FAIL)
10190 return FAIL;
10191 }
10192#endif
10193
10194 /*
10195 * Set the cursor after creating folds, since that moves the cursor.
10196 */
10197 if (do_cursor)
10198 {
10199
10200 /* Restore the cursor line in the file and relatively in the
10201 * window. Don't use "G", it changes the jumplist. */
10202 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10203 (long)wp->w_cursor.lnum,
10204 (long)(wp->w_cursor.lnum - wp->w_topline),
10205 (long)wp->w_height / 2, (long)wp->w_height) < 0
10206 || put_eol(fd) == FAIL
10207 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10208 || put_line(fd, "exe s:l") == FAIL
10209 || put_line(fd, "normal! zt") == FAIL
10210 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10211 || put_eol(fd) == FAIL)
10212 return FAIL;
10213 /* Restore the cursor column and left offset when not wrapping. */
10214 if (wp->w_cursor.col == 0)
10215 {
10216 if (put_line(fd, "normal! 0") == FAIL)
10217 return FAIL;
10218 }
10219 else
10220 {
10221 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10222 {
10223 if (fprintf(fd,
10224 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10225 (long)wp->w_cursor.col,
10226 (long)(wp->w_cursor.col - wp->w_leftcol),
10227 (long)wp->w_width / 2, (long)wp->w_width) < 0
10228 || put_eol(fd) == FAIL
10229 || put_line(fd, "if s:c > 0") == FAIL
10230 || fprintf(fd,
10231 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10232 (long)wp->w_cursor.col) < 0
10233 || put_eol(fd) == FAIL
10234 || put_line(fd, "else") == FAIL
10235 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10236 || put_eol(fd) == FAIL
10237 || put_line(fd, "endif") == FAIL)
10238 return FAIL;
10239 }
10240 else
10241 {
10242 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10243 || put_eol(fd) == FAIL)
10244 return FAIL;
10245 }
10246 }
10247 }
10248
10249 /*
10250 * Local directory.
10251 */
10252 if (wp->w_localdir != NULL)
10253 {
10254 if (fputs("lcd ", fd) < 0
10255 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10256 || put_eol(fd) == FAIL)
10257 return FAIL;
10258 }
10259
10260 return OK;
10261}
10262
10263/*
10264 * Write an argument list to the session file.
10265 * Returns FAIL if writing fails.
10266 */
10267 static int
10268ses_arglist(fd, cmd, gap, fullname, flagp)
10269 FILE *fd;
10270 char *cmd;
10271 garray_T *gap;
10272 int fullname; /* TRUE: use full path name */
10273 unsigned *flagp;
10274{
10275 int i;
10276 char_u buf[MAXPATHL];
10277 char_u *s;
10278
10279 if (gap->ga_len == 0)
10280 return put_line(fd, "silent! argdel *");
10281 if (fputs(cmd, fd) < 0)
10282 return FAIL;
10283 for (i = 0; i < gap->ga_len; ++i)
10284 {
10285 /* NULL file names are skipped (only happens when out of memory). */
10286 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10287 if (s != NULL)
10288 {
10289 if (fullname)
10290 {
10291 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10292 s = buf;
10293 }
10294 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10295 return FAIL;
10296 }
10297 }
10298 return put_eol(fd);
10299}
10300
10301/*
10302 * Write a buffer name to the session file.
10303 * Also ends the line.
10304 * Returns FAIL if writing fails.
10305 */
10306 static int
10307ses_fname(fd, buf, flagp)
10308 FILE *fd;
10309 buf_T *buf;
10310 unsigned *flagp;
10311{
10312 char_u *name;
10313
10314 /* Use the short file name if the current directory is known at the time
10315 * the session file will be sourced. Don't do this for ":mkview", we
10316 * don't know the current directory. */
10317 if (buf->b_sfname != NULL
10318 && flagp == &ssop_flags
10319 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
10320 name = buf->b_sfname;
10321 else
10322 name = buf->b_ffname;
10323 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10324 return FAIL;
10325 return OK;
10326}
10327
10328/*
10329 * Write a file name to the session file.
10330 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10331 * characters.
10332 * Returns FAIL if writing fails.
10333 */
10334 static int
10335ses_put_fname(fd, name, flagp)
10336 FILE *fd;
10337 char_u *name;
10338 unsigned *flagp;
10339{
10340 char_u *sname;
10341 int retval = OK;
10342 int c;
10343
10344 sname = home_replace_save(NULL, name);
10345 if (sname != NULL)
10346 name = sname;
10347 while (*name != NUL)
10348 {
10349#ifdef FEAT_MBYTE
10350 {
10351 int l;
10352
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010353 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 {
10355 /* copy a multibyte char */
10356 while (--l >= 0)
10357 {
10358 if (putc(*name, fd) != *name)
10359 retval = FAIL;
10360 ++name;
10361 }
10362 continue;
10363 }
10364 }
10365#endif
10366 c = *name++;
10367 if (c == '\\' && (*flagp & SSOP_SLASH))
10368 /* change a backslash to a forward slash */
10369 c = '/';
10370 else if ((vim_strchr(escape_chars, c) != NULL
10371#ifdef BACKSLASH_IN_FILENAME
10372 && c != '\\'
10373#endif
10374 ) || c == '#' || c == '%')
10375 {
10376 /* escape a special character with a backslash */
10377 if (putc('\\', fd) != '\\')
10378 retval = FAIL;
10379 }
10380 if (putc(c, fd) != c)
10381 retval = FAIL;
10382 }
10383 vim_free(sname);
10384 return retval;
10385}
10386
10387/*
10388 * ":loadview [nr]"
10389 */
10390 static void
10391ex_loadview(eap)
10392 exarg_T *eap;
10393{
10394 char_u *fname;
10395
10396 fname = get_view_file(*eap->arg);
10397 if (fname != NULL)
10398 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010399 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 vim_free(fname);
10401 }
10402}
10403
10404/*
10405 * Get the name of the view file for the current buffer.
10406 */
10407 static char_u *
10408get_view_file(c)
10409 int c;
10410{
10411 int len = 0;
10412 char_u *p, *s;
10413 char_u *retval;
10414 char_u *sname;
10415
10416 if (curbuf->b_ffname == NULL)
10417 {
10418 EMSG(_(e_noname));
10419 return NULL;
10420 }
10421 sname = home_replace_save(NULL, curbuf->b_ffname);
10422 if (sname == NULL)
10423 return NULL;
10424
10425 /*
10426 * We want a file name without separators, because we're not going to make
10427 * a directory.
10428 * "normal" path separator -> "=+"
10429 * "=" -> "=="
10430 * ":" path separator -> "=-"
10431 */
10432 for (p = sname; *p; ++p)
10433 if (*p == '=' || vim_ispathsep(*p))
10434 ++len;
10435 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10436 if (retval != NULL)
10437 {
10438 STRCPY(retval, p_vdir);
10439 add_pathsep(retval);
10440 s = retval + STRLEN(retval);
10441 for (p = sname; *p; ++p)
10442 {
10443 if (*p == '=')
10444 {
10445 *s++ = '=';
10446 *s++ = '=';
10447 }
10448 else if (vim_ispathsep(*p))
10449 {
10450 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010451#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 || defined(VMS)
10453 if (*p == ':')
10454 *s++ = '-';
10455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010457 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 }
10459 else
10460 *s++ = *p;
10461 }
10462 *s++ = '=';
10463 *s++ = c;
10464 STRCPY(s, ".vim");
10465 }
10466
10467 vim_free(sname);
10468 return retval;
10469}
10470
10471#endif /* FEAT_SESSION */
10472
10473/*
10474 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10475 * Return FAIL for a write error.
10476 */
10477 int
10478put_eol(fd)
10479 FILE *fd;
10480{
10481 if (
10482#ifdef USE_CRNL
10483 (
10484# ifdef MKSESSION_NL
10485 !mksession_nl &&
10486# endif
10487 (putc('\r', fd) < 0)) ||
10488#endif
10489 (putc('\n', fd) < 0))
10490 return FAIL;
10491 return OK;
10492}
10493
10494/*
10495 * Write a line to "fd".
10496 * Return FAIL for a write error.
10497 */
10498 int
10499put_line(fd, s)
10500 FILE *fd;
10501 char *s;
10502{
10503 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10504 return FAIL;
10505 return OK;
10506}
10507
10508#ifdef FEAT_VIMINFO
10509/*
10510 * ":rviminfo" and ":wviminfo".
10511 */
10512 static void
10513ex_viminfo(eap)
10514 exarg_T *eap;
10515{
10516 char_u *save_viminfo;
10517
10518 save_viminfo = p_viminfo;
10519 if (*p_viminfo == NUL)
10520 p_viminfo = (char_u *)"'100";
10521 if (eap->cmdidx == CMD_rviminfo)
10522 {
10523 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
10524 EMSG(_("E195: Cannot open viminfo file for reading"));
10525 }
10526 else
10527 write_viminfo(eap->arg, eap->forceit);
10528 p_viminfo = save_viminfo;
10529}
10530#endif
10531
10532#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010533/*
10534 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010535 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 void
10538dialog_msg(buff, format, fname)
10539 char_u *buff;
10540 char *format;
10541 char_u *fname;
10542{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 if (fname == NULL)
10544 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010545 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546}
10547#endif
10548
10549/*
10550 * ":behave {mswin,xterm}"
10551 */
10552 static void
10553ex_behave(eap)
10554 exarg_T *eap;
10555{
10556 if (STRCMP(eap->arg, "mswin") == 0)
10557 {
10558 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10559 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10560 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10561 set_option_value((char_u *)"keymodel", 0L,
10562 (char_u *)"startsel,stopsel", 0);
10563 }
10564 else if (STRCMP(eap->arg, "xterm") == 0)
10565 {
10566 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10567 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10568 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10569 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10570 }
10571 else
10572 EMSG2(_(e_invarg2), eap->arg);
10573}
10574
10575#ifdef FEAT_AUTOCMD
10576static int filetype_detect = FALSE;
10577static int filetype_plugin = FALSE;
10578static int filetype_indent = FALSE;
10579
10580/*
10581 * ":filetype [plugin] [indent] {on,off,detect}"
10582 * on: Load the filetype.vim file to install autocommands for file types.
10583 * off: Load the ftoff.vim file to remove all autocommands for file types.
10584 * plugin on: load filetype.vim and ftplugin.vim
10585 * plugin off: load ftplugof.vim
10586 * indent on: load filetype.vim and indent.vim
10587 * indent off: load indoff.vim
10588 */
10589 static void
10590ex_filetype(eap)
10591 exarg_T *eap;
10592{
10593 char_u *arg = eap->arg;
10594 int plugin = FALSE;
10595 int indent = FALSE;
10596
10597 if (*eap->arg == NUL)
10598 {
10599 /* Print current status. */
10600 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10601 filetype_detect ? "ON" : "OFF",
10602 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10603 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10604 return;
10605 }
10606
10607 /* Accept "plugin" and "indent" in any order. */
10608 for (;;)
10609 {
10610 if (STRNCMP(arg, "plugin", 6) == 0)
10611 {
10612 plugin = TRUE;
10613 arg = skipwhite(arg + 6);
10614 continue;
10615 }
10616 if (STRNCMP(arg, "indent", 6) == 0)
10617 {
10618 indent = TRUE;
10619 arg = skipwhite(arg + 6);
10620 continue;
10621 }
10622 break;
10623 }
10624 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10625 {
10626 if (*arg == 'o' || !filetype_detect)
10627 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010628 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 filetype_detect = TRUE;
10630 if (plugin)
10631 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010632 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633 filetype_plugin = TRUE;
10634 }
10635 if (indent)
10636 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010637 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 filetype_indent = TRUE;
10639 }
10640 }
10641 if (*arg == 'd')
10642 {
10643 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010644 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 }
10646 }
10647 else if (STRCMP(arg, "off") == 0)
10648 {
10649 if (plugin || indent)
10650 {
10651 if (plugin)
10652 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010653 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 filetype_plugin = FALSE;
10655 }
10656 if (indent)
10657 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010658 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 filetype_indent = FALSE;
10660 }
10661 }
10662 else
10663 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010664 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 filetype_detect = FALSE;
10666 }
10667 }
10668 else
10669 EMSG2(_(e_invarg2), arg);
10670}
10671
10672/*
10673 * ":setfiletype {name}"
10674 */
10675 static void
10676ex_setfiletype(eap)
10677 exarg_T *eap;
10678{
10679 if (!did_filetype)
10680 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10681}
10682#endif
10683
10684/*ARGSUSED*/
10685 static void
10686ex_digraphs(eap)
10687 exarg_T *eap;
10688{
10689#ifdef FEAT_DIGRAPHS
10690 if (*eap->arg != NUL)
10691 putdigraph(eap->arg);
10692 else
10693 listdigraphs();
10694#else
10695 EMSG(_("E196: No digraphs in this version"));
10696#endif
10697}
10698
10699 static void
10700ex_set(eap)
10701 exarg_T *eap;
10702{
10703 int flags = 0;
10704
10705 if (eap->cmdidx == CMD_setlocal)
10706 flags = OPT_LOCAL;
10707 else if (eap->cmdidx == CMD_setglobal)
10708 flags = OPT_GLOBAL;
10709#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10710 if (cmdmod.browse && flags == 0)
10711 ex_options(eap);
10712 else
10713#endif
10714 (void)do_set(eap->arg, flags);
10715}
10716
10717#ifdef FEAT_SEARCH_EXTRA
10718/*
10719 * ":nohlsearch"
10720 */
10721/*ARGSUSED*/
10722 static void
10723ex_nohlsearch(eap)
10724 exarg_T *eap;
10725{
10726 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010727 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728}
10729
10730/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010731 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 * Sets nextcmd to the start of the next command, if any. Also called when
10733 * skipping commands to find the next command.
10734 */
10735 static void
10736ex_match(eap)
10737 exarg_T *eap;
10738{
10739 char_u *p;
10740 char_u *end;
10741 int c;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010742 int mi;
10743
10744 if (eap->line2 <= 3)
10745 mi = eap->line2 - 1;
10746 else
10747 {
10748 EMSG(e_invcmd);
10749 return;
10750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751
10752 /* First clear any old pattern. */
10753 if (!eap->skip)
10754 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010755 vim_free(curwin->w_match[mi].regprog);
10756 curwin->w_match[mi].regprog = NULL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010757 vim_free(curwin->w_match_pat[mi]);
10758 curwin->w_match_pat[mi] = NULL;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010759 redraw_later(SOME_VALID); /* always need a redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 }
10761
10762 if (ends_excmd(*eap->arg))
10763 end = eap->arg;
10764 else if ((STRNICMP(eap->arg, "none", 4) == 0
10765 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10766 end = eap->arg + 4;
10767 else
10768 {
10769 p = skiptowhite(eap->arg);
10770 if (!eap->skip)
10771 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010772 curwin->w_match_id[mi] = syn_namen2id(eap->arg,
10773 (int)(p - eap->arg));
10774 if (curwin->w_match_id[mi] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 {
10776 EMSG2(_(e_nogroup), eap->arg);
10777 return;
10778 }
10779 }
10780 p = skipwhite(p);
10781 if (*p == NUL)
10782 {
10783 /* There must be two arguments. */
10784 EMSG2(_(e_invarg2), eap->arg);
10785 return;
10786 }
10787 end = skip_regexp(p + 1, *p, TRUE, NULL);
10788 if (!eap->skip)
10789 {
10790 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10791 {
10792 eap->errmsg = e_trailing;
10793 return;
10794 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010795 if (*end != *p)
10796 {
10797 EMSG2(_(e_invarg2), p);
10798 return;
10799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800
10801 c = *end;
10802 *end = NUL;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010803 curwin->w_match[mi].regprog = vim_regcomp(p + 1, RE_MAGIC);
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010804 if (curwin->w_match[mi].regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 {
10806 EMSG2(_(e_invarg2), p);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010807 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 return;
10809 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010810 curwin->w_match_pat[mi] = vim_strsave(p + 1);
10811 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 }
10813 }
10814 eap->nextcmd = find_nextcmd(end);
10815}
10816#endif
10817
10818#ifdef FEAT_CRYPT
10819/*
10820 * ":X": Get crypt key
10821 */
10822/*ARGSUSED*/
10823 static void
10824ex_X(eap)
10825 exarg_T *eap;
10826{
10827 (void)get_crypt_key(TRUE, TRUE);
10828}
10829#endif
10830
10831#ifdef FEAT_FOLDING
10832 static void
10833ex_fold(eap)
10834 exarg_T *eap;
10835{
10836 if (foldManualAllowed(TRUE))
10837 foldCreate(eap->line1, eap->line2);
10838}
10839
10840 static void
10841ex_foldopen(eap)
10842 exarg_T *eap;
10843{
10844 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10845 eap->forceit, FALSE);
10846}
10847
10848 static void
10849ex_folddo(eap)
10850 exarg_T *eap;
10851{
10852 linenr_T lnum;
10853
10854 /* First set the marks for all lines closed/open. */
10855 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10856 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10857 ml_setmarked(lnum);
10858
10859 /* Execute the command on the marked lines. */
10860 global_exe(eap->arg);
10861 ml_clearmarked(); /* clear rest of the marks */
10862}
10863#endif