blob: 495d7397b3b72554236a7145a1654f85825c7c01 [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));
153static void ex_all __ARGS((exarg_T *eap));
154static void ex_resize __ARGS((exarg_T *eap));
155static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000156static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000157static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000158static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#else
160# define ex_close ex_ni
161# define ex_only ex_ni
162# define ex_all ex_ni
163# define ex_resize ex_ni
164# define ex_splitview ex_ni
165# define ex_stag ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000166# define ex_tabedit ex_ni
167# define ex_tab ex_ni
168# define ex_tabs ex_ni
169# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000170# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
172#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
173static void ex_pclose __ARGS((exarg_T *eap));
174static void ex_ptag __ARGS((exarg_T *eap));
175static void ex_pedit __ARGS((exarg_T *eap));
176#else
177# define ex_pclose ex_ni
178# define ex_ptag ex_ni
179# define ex_pedit ex_ni
180#endif
181static void ex_hide __ARGS((exarg_T *eap));
182static void ex_stop __ARGS((exarg_T *eap));
183static void ex_exit __ARGS((exarg_T *eap));
184static void ex_print __ARGS((exarg_T *eap));
185#ifdef FEAT_BYTEOFF
186static void ex_goto __ARGS((exarg_T *eap));
187#else
188# define ex_goto ex_ni
189#endif
190static void ex_shell __ARGS((exarg_T *eap));
191static void ex_preserve __ARGS((exarg_T *eap));
192static void ex_recover __ARGS((exarg_T *eap));
193#ifndef FEAT_LISTCMDS
194# define ex_argedit ex_ni
195# define ex_argadd ex_ni
196# define ex_argdelete ex_ni
197# define ex_listdo ex_ni
198#endif
199static void ex_mode __ARGS((exarg_T *eap));
200static void ex_wrongmodifier __ARGS((exarg_T *eap));
201static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000202static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203static void ex_edit __ARGS((exarg_T *eap));
204#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
205# define ex_drop ex_ni
206#endif
207#ifndef FEAT_GUI
208# define ex_gui ex_nogui
209static void ex_nogui __ARGS((exarg_T *eap));
210#endif
211#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
212static void ex_tearoff __ARGS((exarg_T *eap));
213#else
214# define ex_tearoff ex_ni
215#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000216#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217static void ex_popup __ARGS((exarg_T *eap));
218#else
219# define ex_popup ex_ni
220#endif
221#ifndef FEAT_GUI_MSWIN
222# define ex_simalt ex_ni
223#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000224#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225# define gui_mch_find_dialog ex_ni
226# define gui_mch_replace_dialog ex_ni
227#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000228#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# define ex_helpfind ex_ni
230#endif
231#ifndef FEAT_CSCOPE
232# define do_cscope ex_ni
233# define do_scscope ex_ni
234# define do_cstag ex_ni
235#endif
236#ifndef FEAT_SYN_HL
237# define ex_syntax ex_ni
Bram Moolenaarb765d632005-06-07 21:00:02 +0000238# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000239# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000240# define ex_spelldump ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000241# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000242#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000243#ifndef FEAT_MZSCHEME
244# define ex_mzscheme ex_script_ni
245# define ex_mzfile ex_ni
246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247#ifndef FEAT_PERL
248# define ex_perl ex_script_ni
249# define ex_perldo ex_ni
250#endif
251#ifndef FEAT_PYTHON
252# define ex_python ex_script_ni
253# define ex_pyfile ex_ni
254#endif
255#ifndef FEAT_TCL
256# define ex_tcl ex_script_ni
257# define ex_tcldo ex_ni
258# define ex_tclfile ex_ni
259#endif
260#ifndef FEAT_RUBY
261# define ex_ruby ex_script_ni
262# define ex_rubydo ex_ni
263# define ex_rubyfile ex_ni
264#endif
265#ifndef FEAT_SNIFF
266# define ex_sniff ex_ni
267#endif
268#ifndef FEAT_KEYMAP
269# define ex_loadkeymap ex_ni
270#endif
271static void ex_swapname __ARGS((exarg_T *eap));
272static void ex_syncbind __ARGS((exarg_T *eap));
273static void ex_read __ARGS((exarg_T *eap));
274static void ex_cd __ARGS((exarg_T *eap));
275static void ex_pwd __ARGS((exarg_T *eap));
276static void ex_equal __ARGS((exarg_T *eap));
277static void ex_sleep __ARGS((exarg_T *eap));
278static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
279static void ex_winsize __ARGS((exarg_T *eap));
280#ifdef FEAT_WINDOWS
281static void ex_wincmd __ARGS((exarg_T *eap));
282#else
283# define ex_wincmd ex_ni
284#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000285#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286static void ex_winpos __ARGS((exarg_T *eap));
287#else
288# define ex_winpos ex_ni
289#endif
290static void ex_operators __ARGS((exarg_T *eap));
291static void ex_put __ARGS((exarg_T *eap));
292static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000293static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294static void ex_submagic __ARGS((exarg_T *eap));
295static void ex_join __ARGS((exarg_T *eap));
296static void ex_at __ARGS((exarg_T *eap));
297static void ex_bang __ARGS((exarg_T *eap));
298static void ex_undo __ARGS((exarg_T *eap));
299static void ex_redo __ARGS((exarg_T *eap));
300static void ex_redir __ARGS((exarg_T *eap));
301static void ex_redraw __ARGS((exarg_T *eap));
302static void ex_redrawstatus __ARGS((exarg_T *eap));
303static void close_redir __ARGS((void));
304static void ex_mkrc __ARGS((exarg_T *eap));
305static void ex_mark __ARGS((exarg_T *eap));
306#ifdef FEAT_USR_CMDS
307static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000308static 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 +0000309#endif
310#ifdef FEAT_EX_EXTRA
311static void ex_normal __ARGS((exarg_T *eap));
312static void ex_startinsert __ARGS((exarg_T *eap));
313static void ex_stopinsert __ARGS((exarg_T *eap));
314#else
315# define ex_normal ex_ni
316# define ex_align ex_ni
317# define ex_retab ex_ni
318# define ex_startinsert ex_ni
319# define ex_stopinsert ex_ni
320# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000321# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#endif
323#ifdef FEAT_FIND_ID
324static void ex_checkpath __ARGS((exarg_T *eap));
325static void ex_findpat __ARGS((exarg_T *eap));
326#else
327# define ex_findpat ex_ni
328# define ex_checkpath ex_ni
329#endif
330#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
331static void ex_psearch __ARGS((exarg_T *eap));
332#else
333# define ex_psearch ex_ni
334#endif
335static void ex_tag __ARGS((exarg_T *eap));
336static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
337#ifndef FEAT_EVAL
338# define ex_scriptnames ex_ni
339# define ex_finish ex_ni
340# define ex_echo ex_ni
341# define ex_echohl ex_ni
342# define ex_execute ex_ni
343# define ex_call ex_ni
344# define ex_if ex_ni
345# define ex_endif ex_ni
346# define ex_else ex_ni
347# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000348# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349# define ex_continue ex_ni
350# define ex_break ex_ni
351# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000352# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353# define ex_throw ex_ni
354# define ex_try ex_ni
355# define ex_catch ex_ni
356# define ex_finally ex_ni
357# define ex_endtry ex_ni
358# define ex_endfunction ex_ni
359# define ex_let ex_ni
360# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000361# define ex_lockvar ex_ni
362# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363# define ex_function ex_ni
364# define ex_delfunction ex_ni
365# define ex_return ex_ni
366#endif
367static char_u *arg_all __ARGS((void));
368#ifdef FEAT_SESSION
369static int makeopens __ARGS((FILE *fd, char_u *dirnow));
370static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp));
371static void ex_loadview __ARGS((exarg_T *eap));
372static char_u *get_view_file __ARGS((int c));
373#else
374# define ex_loadview ex_ni
375#endif
376#ifndef FEAT_EVAL
377# define ex_compiler ex_ni
378#endif
379#ifdef FEAT_VIMINFO
380static void ex_viminfo __ARGS((exarg_T *eap));
381#else
382# define ex_viminfo ex_ni
383#endif
384static void ex_behave __ARGS((exarg_T *eap));
385#ifdef FEAT_AUTOCMD
386static void ex_filetype __ARGS((exarg_T *eap));
387static void ex_setfiletype __ARGS((exarg_T *eap));
388#else
389# define ex_filetype ex_ni
390# define ex_setfiletype ex_ni
391#endif
392#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000393# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394# define ex_diffpatch ex_ni
395# define ex_diffgetput ex_ni
396# define ex_diffsplit ex_ni
397# define ex_diffthis ex_ni
398# define ex_diffupdate ex_ni
399#endif
400static void ex_digraphs __ARGS((exarg_T *eap));
401static void ex_set __ARGS((exarg_T *eap));
402#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
403# define ex_options ex_ni
404#endif
405#ifdef FEAT_SEARCH_EXTRA
406static void ex_nohlsearch __ARGS((exarg_T *eap));
407static void ex_match __ARGS((exarg_T *eap));
408#else
409# define ex_nohlsearch ex_ni
410# define ex_match ex_ni
411#endif
412#ifdef FEAT_CRYPT
413static void ex_X __ARGS((exarg_T *eap));
414#else
415# define ex_X ex_ni
416#endif
417#ifdef FEAT_FOLDING
418static void ex_fold __ARGS((exarg_T *eap));
419static void ex_foldopen __ARGS((exarg_T *eap));
420static void ex_folddo __ARGS((exarg_T *eap));
421#else
422# define ex_fold ex_ni
423# define ex_foldopen ex_ni
424# define ex_folddo ex_ni
425#endif
426#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
427 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
428# define ex_language ex_ni
429#endif
430#ifndef FEAT_SIGNS
431# define ex_sign ex_ni
432#endif
433#ifndef FEAT_SUN_WORKSHOP
434# define ex_wsverb ex_ni
435#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000436#ifndef FEAT_NETBEANS_INTG
437# define ex_nbkey ex_ni
438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439
440#ifndef FEAT_EVAL
441# define ex_debug ex_ni
442# define ex_breakadd ex_ni
443# define ex_debuggreedy ex_ni
444# define ex_breakdel ex_ni
445# define ex_breaklist ex_ni
446#endif
447
448#ifndef FEAT_CMDHIST
449# define ex_history ex_ni
450#endif
451#ifndef FEAT_JUMPLIST
452# define ex_jumps ex_ni
453# define ex_changes ex_ni
454#endif
455
Bram Moolenaar05159a02005-02-26 23:04:13 +0000456#ifndef FEAT_PROFILE
457# define ex_profile ex_ni
458#endif
459
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460/*
461 * Declare cmdnames[].
462 */
463#define DO_DECLARE_EXCMD
464#include "ex_cmds.h"
465
466/*
467 * Table used to quickly search for a command, based on its first character.
468 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000469static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 CMD_append,
472 CMD_buffer,
473 CMD_change,
474 CMD_delete,
475 CMD_edit,
476 CMD_file,
477 CMD_global,
478 CMD_help,
479 CMD_insert,
480 CMD_join,
481 CMD_k,
482 CMD_list,
483 CMD_move,
484 CMD_next,
485 CMD_open,
486 CMD_print,
487 CMD_quit,
488 CMD_read,
489 CMD_substitute,
490 CMD_t,
491 CMD_undo,
492 CMD_vglobal,
493 CMD_write,
494 CMD_xit,
495 CMD_yank,
496 CMD_z,
497 CMD_bang
498};
499
500static char_u dollar_command[2] = {'$', 0};
501
502
503#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000504/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505typedef struct
506{
507 char_u *line; /* command line */
508 linenr_T lnum; /* sourcing_lnum of the line */
509} wcmd_T;
510
511/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000512 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000514 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000516struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517{
518 garray_T *lines_gap; /* growarray with line info */
519 int current_line; /* last read line from growarray */
520 int repeating; /* TRUE when looping a second time */
521 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
522 char_u *(*getline) __ARGS((int, void *, int));
523 void *cookie;
524};
525
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000526static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
527static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000529
530/* Struct to save a few things while debugging. Used in do_cmdline() only. */
531struct dbg_stuff
532{
533 int trylevel;
534 int force_abort;
535 except_T *caught_stack;
536 char_u *vv_exception;
537 char_u *vv_throwpoint;
538 int did_emsg;
539 int got_int;
540 int did_throw;
541 int need_rethrow;
542 int check_cstack;
543 except_T *current_exception;
544};
545
546static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
547static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
548
549 static void
550save_dbg_stuff(dsp)
551 struct dbg_stuff *dsp;
552{
553 dsp->trylevel = trylevel; trylevel = 0;
554 dsp->force_abort = force_abort; force_abort = FALSE;
555 dsp->caught_stack = caught_stack; caught_stack = NULL;
556 dsp->vv_exception = v_exception(NULL);
557 dsp->vv_throwpoint = v_throwpoint(NULL);
558
559 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
560 dsp->did_emsg = did_emsg; did_emsg = FALSE;
561 dsp->got_int = got_int; got_int = FALSE;
562 dsp->did_throw = did_throw; did_throw = FALSE;
563 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
564 dsp->check_cstack = check_cstack; check_cstack = FALSE;
565 dsp->current_exception = current_exception; current_exception = NULL;
566}
567
568 static void
569restore_dbg_stuff(dsp)
570 struct dbg_stuff *dsp;
571{
572 suppress_errthrow = FALSE;
573 trylevel = dsp->trylevel;
574 force_abort = dsp->force_abort;
575 caught_stack = dsp->caught_stack;
576 (void)v_exception(dsp->vv_exception);
577 (void)v_throwpoint(dsp->vv_throwpoint);
578 did_emsg = dsp->did_emsg;
579 got_int = dsp->got_int;
580 did_throw = dsp->did_throw;
581 need_rethrow = dsp->need_rethrow;
582 check_cstack = dsp->check_cstack;
583 current_exception = dsp->current_exception;
584}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585#endif
586
587
588/*
589 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
590 * command is given.
591 */
592 void
593do_exmode(improved)
594 int improved; /* TRUE for "improved Ex" mode */
595{
596 int save_msg_scroll;
597 int prev_msg_row;
598 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000599 int changedtick;
600
601 if (improved)
602 exmode_active = EXMODE_VIM;
603 else
604 exmode_active = EXMODE_NORMAL;
605 State = NORMAL;
606
607 /* When using ":global /pat/ visual" and then "Q" we return to continue
608 * the :global command. */
609 if (global_busy)
610 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611
612 save_msg_scroll = msg_scroll;
613 ++RedrawingDisabled; /* don't redisplay the window */
614 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615#ifdef FEAT_GUI
616 /* Ignore scrollbar and mouse events in Ex mode */
617 ++hold_gui_events;
618#endif
619#ifdef FEAT_SNIFF
620 want_sniff_request = 0; /* No K_SNIFF wanted */
621#endif
622
623 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
624 while (exmode_active)
625 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000626#ifdef FEAT_EX_EXTRA
627 /* Check for a ":normal" command and no more characters left. */
628 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
629 {
630 exmode_active = FALSE;
631 break;
632 }
633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 msg_scroll = TRUE;
635 need_wait_return = FALSE;
636 ex_pressedreturn = FALSE;
637 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000638 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 prev_msg_row = msg_row;
640 prev_line = curwin->w_cursor.lnum;
641#ifdef FEAT_SNIFF
642 ProcessSniffRequests();
643#endif
644 if (improved)
645 {
646 cmdline_row = msg_row;
647 do_cmdline(NULL, getexline, NULL, 0);
648 }
649 else
650 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
651 lines_left = Rows - 1;
652
Bram Moolenaardf177f62005-02-22 08:39:57 +0000653 if ((prev_line != curwin->w_cursor.lnum
654 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000656 if (curbuf->b_ml.ml_flags & ML_EMPTY)
657 EMSG(_(e_emptybuf));
658 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000660 if (ex_pressedreturn)
661 {
662 /* go up one line, to overwrite the ":<CR>" line, so the
663 * output doensn't contain empty lines. */
664 msg_row = prev_msg_row;
665 if (prev_msg_row == Rows - 1)
666 msg_row--;
667 }
668 msg_col = 0;
669 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
670 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000673 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
674 {
675 if (curbuf->b_ml.ml_flags & ML_EMPTY)
676 EMSG(_(e_emptybuf));
677 else
678 EMSG(_("E501: At end-of-file"));
679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 }
681
682#ifdef FEAT_GUI
683 --hold_gui_events;
684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 --RedrawingDisabled;
686 --no_wait_return;
687 update_screen(CLEAR);
688 need_wait_return = FALSE;
689 msg_scroll = save_msg_scroll;
690}
691
692/*
693 * Execute a simple command line. Used for translated commands like "*".
694 */
695 int
696do_cmdline_cmd(cmd)
697 char_u *cmd;
698{
699 return do_cmdline(cmd, NULL, NULL,
700 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
701}
702
703/*
704 * do_cmdline(): execute one Ex command line
705 *
706 * 1. Execute "cmdline" when it is not NULL.
707 * If "cmdline" is NULL, or more lines are needed, getline() is used.
708 * 2. Split up in parts separated with '|'.
709 *
710 * This function can be called recursively!
711 *
712 * flags:
713 * DOCMD_VERBOSE - The command will be included in the error message.
714 * DOCMD_NOWAIT - Don't call wait_return() and friends.
715 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
716 * DOCMD_KEYTYPED - Don't reset KeyTyped.
717 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
718 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
719 *
720 * return FAIL if cmdline could not be executed, OK otherwise
721 */
722 int
723do_cmdline(cmdline, getline, cookie, flags)
724 char_u *cmdline;
725 char_u *(*getline) __ARGS((int, void *, int));
726 void *cookie; /* argument for getline() */
727 int flags;
728{
729 char_u *next_cmdline; /* next cmd to execute */
730 char_u *cmdline_copy = NULL; /* copy of cmd line */
731 int used_getline = FALSE; /* used "getline" to obtain command */
732 static int recursive = 0; /* recursive depth */
733 int msg_didout_before_start = 0;
734 int count = 0; /* line number count */
735 int did_inc = FALSE; /* incremented RedrawingDisabled */
736 int retval = OK;
737#ifdef FEAT_EVAL
738 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000739 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 int current_line = 0; /* active line in lines_ga */
741 char_u *fname = NULL; /* function or script name */
742 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
743 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000744 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 int initial_trylevel;
746 struct msglist **saved_msg_list = NULL;
747 struct msglist *private_msg_list;
748
749 /* "getline" and "cookie" passed to do_one_cmd() */
750 char_u *(*cmd_getline) __ARGS((int, void *, int));
751 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000752 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000754 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755#else
756# define cmd_getline getline
757# define cmd_cookie cookie
758#endif
759 static int call_depth = 0; /* recursiveness */
760
761#ifdef FEAT_EVAL
762 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
763 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000764 * This ensures that the do_errthrow() call in do_one_cmd() does not
765 * combine the messages stored by an earlier invocation of do_one_cmd()
766 * with the command name of the later one. This would happen when
767 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 saved_msg_list = msg_list;
769 msg_list = &private_msg_list;
770 private_msg_list = NULL;
771#endif
772
773 /* It's possible to create an endless loop with ":execute", catch that
774 * here. The value of 200 allows nested function calls, ":source", etc. */
775 if (call_depth == 200)
776 {
777 EMSG(_("E169: Command too recursive"));
778#ifdef FEAT_EVAL
779 /* When converting to an exception, we do not include the command name
780 * since this is not an error of the specific command. */
781 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
782 msg_list = saved_msg_list;
783#endif
784 return FAIL;
785 }
786 ++call_depth;
787
788#ifdef FEAT_EVAL
789 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000790 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 cstack.cs_trylevel = 0;
792 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000793 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
795
796 real_cookie = getline_cookie(getline, cookie);
797
798 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799 getline_is_func = getline_equal(getline, cookie, get_func_line);
800 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 ++ex_nesting_level;
802
803 /* Get the function or script name and the address where the next breakpoint
804 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
807 fname = func_name(real_cookie);
808 breakpoint = func_breakpoint(real_cookie);
809 dbg_tick = func_dbg_tick(real_cookie);
810 }
811 else if (getline_equal(getline, cookie, getsourceline))
812 {
813 fname = sourcing_name;
814 breakpoint = source_breakpoint(real_cookie);
815 dbg_tick = source_dbg_tick(real_cookie);
816 }
817
818 /*
819 * Initialize "force_abort" and "suppress_errthrow" at the top level.
820 */
821 if (!recursive)
822 {
823 force_abort = FALSE;
824 suppress_errthrow = FALSE;
825 }
826
827 /*
828 * If requested, store and reset the global values controlling the
829 * exception handling (used when debugging).
830 */
831 else if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000832 save_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
834 initial_trylevel = trylevel;
835
836 /*
837 * "did_throw" will be set to TRUE when an exception is being thrown.
838 */
839 did_throw = FALSE;
840#endif
841 /*
842 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000843 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 * If force_abort is set, we cancel everything.
845 */
846 did_emsg = FALSE;
847
848 /*
849 * KeyTyped is only set when calling vgetc(). Reset it here when not
850 * calling vgetc() (sourced command lines).
851 */
852 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
853 KeyTyped = FALSE;
854
855 /*
856 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000857 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 * - for multiple commands on one line, separated with '|'
859 * - when repeating until there are no more lines (for ":source")
860 */
861 next_cmdline = cmdline;
862 do
863 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000864#ifdef FEAT_EVAL
865 getline_is_func = getline_equal(getline, cookie, get_func_line);
866#endif
867
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000868 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 if (next_cmdline == NULL
870#ifdef FEAT_EVAL
871 && !force_abort
872 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000873 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874#endif
875 )
876 did_emsg = FALSE;
877
878 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000879 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 * 2. If no line given: Get an allocated line with getline().
881 * 3. If a line is given: Make a copy, so we can mess with it.
882 */
883
884#ifdef FEAT_EVAL
885 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000886 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 {
888 /* Each '|' separated command is stored separately in lines_ga, to
889 * be able to jump to it. Don't use next_cmdline now. */
890 vim_free(cmdline_copy);
891 cmdline_copy = NULL;
892
893 /* Check if a function has returned or, unless it has an unclosed
894 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000895 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897# ifdef FEAT_PROFILE
898 if (do_profiling)
899 func_line_end(real_cookie);
900# endif
901 if (func_has_ended(real_cookie))
902 {
903 retval = FAIL;
904 break;
905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000907#ifdef FEAT_PROFILE
908 else if (do_profiling
909 && getline_equal(getline, cookie, getsourceline))
910 script_line_end();
911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
913 /* Check if a sourced file hit a ":finish" command. */
914 if (source_finished(getline, cookie))
915 {
916 retval = FAIL;
917 break;
918 }
919
920 /* If breakpoints have been added/deleted need to check for it. */
921 if (breakpoint != NULL && dbg_tick != NULL
922 && *dbg_tick != debug_tick)
923 {
924 *breakpoint = dbg_find_breakpoint(
925 getline_equal(getline, cookie, getsourceline),
926 fname, sourcing_lnum);
927 *dbg_tick = debug_tick;
928 }
929
930 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
931 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
932
933 /* Did we encounter a breakpoint? */
934 if (breakpoint != NULL && *breakpoint != 0
935 && *breakpoint <= sourcing_lnum)
936 {
937 dbg_breakpoint(fname, sourcing_lnum);
938 /* Find next breakpoint. */
939 *breakpoint = dbg_find_breakpoint(
940 getline_equal(getline, cookie, getsourceline),
941 fname, sourcing_lnum);
942 *dbg_tick = debug_tick;
943 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000944# ifdef FEAT_PROFILE
945 if (do_profiling)
946 {
947 if (getline_is_func)
948 func_line_start(real_cookie);
949 else if (getline_equal(getline, cookie, getsourceline))
950 script_line_start();
951 }
952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
954
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000955 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000957 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 * again. Pass a different "getline" function to do_one_cmd()
959 * below, so that it stores lines in or reads them from
960 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000961 * while/for loop. */
962 cmd_getline = get_loop_line;
963 cmd_cookie = (void *)&cmd_loop_cookie;
964 cmd_loop_cookie.lines_gap = &lines_ga;
965 cmd_loop_cookie.current_line = current_line;
966 cmd_loop_cookie.getline = getline;
967 cmd_loop_cookie.cookie = cookie;
968 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 }
970 else
971 {
972 cmd_getline = getline;
973 cmd_cookie = cookie;
974 }
975#endif
976
977 /* 2. If no line given, get an allocated line with getline(). */
978 if (next_cmdline == NULL)
979 {
980 /*
981 * Need to set msg_didout for the first line after an ":if",
982 * otherwise the ":if" will be overwritten.
983 */
984 if (count == 1 && getline_equal(getline, cookie, getexline))
985 msg_didout = TRUE;
986 if (getline == NULL || (next_cmdline = getline(':', cookie,
987#ifdef FEAT_EVAL
988 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
989#else
990 0
991#endif
992 )) == NULL)
993 {
994 /* Don't call wait_return for aborted command line. The NULL
995 * returned for the end of a sourced file or executed function
996 * doesn't do this. */
997 if (KeyTyped && !(flags & DOCMD_REPEAT))
998 need_wait_return = FALSE;
999 retval = FAIL;
1000 break;
1001 }
1002 used_getline = TRUE;
1003
1004 /*
1005 * Keep the first typed line. Clear it when more lines are typed.
1006 */
1007 if (flags & DOCMD_KEEPLINE)
1008 {
1009 vim_free(repeat_cmdline);
1010 if (count == 0)
1011 repeat_cmdline = vim_strsave(next_cmdline);
1012 else
1013 repeat_cmdline = NULL;
1014 }
1015 }
1016
1017 /* 3. Make a copy of the command so we can mess with it. */
1018 else if (cmdline_copy == NULL)
1019 {
1020 next_cmdline = vim_strsave(next_cmdline);
1021 if (next_cmdline == NULL)
1022 {
1023 EMSG(_(e_outofmem));
1024 retval = FAIL;
1025 break;
1026 }
1027 }
1028 cmdline_copy = next_cmdline;
1029
1030#ifdef FEAT_EVAL
1031 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001032 * Save the current line when inside a ":while" or ":for", and when
1033 * the command looks like a ":while" or ":for", because we may need it
1034 * later. When there is a '|' and another command, it is stored
1035 * separately, because we need to be able to jump back to it from an
1036 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001038 if (current_line == lines_ga.ga_len
1039 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 {
1043 retval = FAIL;
1044 break;
1045 }
1046 }
1047 did_endif = FALSE;
1048#endif
1049
1050 if (count++ == 0)
1051 {
1052 /*
1053 * All output from the commands is put below each other, without
1054 * waiting for a return. Don't do this when executing commands
1055 * from a script or when being called recursive (e.g. for ":e
1056 * +command file").
1057 */
1058 if (!(flags & DOCMD_NOWAIT) && !recursive)
1059 {
1060 msg_didout_before_start = msg_didout;
1061 msg_didany = FALSE; /* no output yet */
1062 msg_start();
1063 msg_scroll = TRUE; /* put messages below each other */
1064 ++no_wait_return; /* dont wait for return until finished */
1065 ++RedrawingDisabled;
1066 did_inc = TRUE;
1067 }
1068 }
1069
1070 if (p_verbose >= 15 && sourcing_name != NULL)
1071 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001073 verbose_enter_scroll();
1074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 smsg((char_u *)_("line %ld: %s"),
1076 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001077 if (msg_silent == 0)
1078 msg_puts((char_u *)"\n"); /* don't overwrite this */
1079
1080 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 --no_wait_return;
1082 }
1083
1084 /*
1085 * 2. Execute one '|' separated command.
1086 * do_one_cmd() will return NULL if there is no trailing '|'.
1087 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1088 */
1089 ++recursive;
1090 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1091#ifdef FEAT_EVAL
1092 &cstack,
1093#endif
1094 cmd_getline, cmd_cookie);
1095 --recursive;
1096
1097#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001098 if (cmd_cookie == (void *)&cmd_loop_cookie)
1099 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001101 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102#endif
1103
1104 if (next_cmdline == NULL)
1105 {
1106 vim_free(cmdline_copy);
1107 cmdline_copy = NULL;
1108#ifdef FEAT_CMDHIST
1109 /*
1110 * If the command was typed, remember it for the ':' register.
1111 * Do this AFTER executing the command to make :@: work.
1112 */
1113 if (getline_equal(getline, cookie, getexline)
1114 && new_last_cmdline != NULL)
1115 {
1116 vim_free(last_cmdline);
1117 last_cmdline = new_last_cmdline;
1118 new_last_cmdline = NULL;
1119 }
1120#endif
1121 }
1122 else
1123 {
1124 /* need to copy the command after the '|' to cmdline_copy, for the
1125 * next do_one_cmd() */
1126 mch_memmove(cmdline_copy, next_cmdline, STRLEN(next_cmdline) + 1);
1127 next_cmdline = cmdline_copy;
1128 }
1129
1130
1131#ifdef FEAT_EVAL
1132 /* reset did_emsg for a function that is not aborted by an error */
1133 if (did_emsg && !force_abort
1134 && getline_equal(getline, cookie, get_func_line)
1135 && !func_has_abort(real_cookie))
1136 did_emsg = FALSE;
1137
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
1140 ++current_line;
1141
1142 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001143 * An ":endwhile", ":endfor" and ":continue" is handled here.
1144 * If we were executing commands, jump back to the ":while" or
1145 * ":for".
1146 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001148 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001150 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001152 /* Jump back to the matching ":while" or ":for". Be careful
1153 * not to use a cs_line[] from an entry that isn't a ":while"
1154 * or ":for": It would make "current_line" invalid and can
1155 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (!did_emsg && !got_int && !did_throw
1157 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001158 && (cstack.cs_flags[cstack.cs_idx]
1159 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 && cstack.cs_line[cstack.cs_idx] >= 0
1161 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1162 {
1163 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001164 /* remember we jumped there */
1165 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 line_breakcheck(); /* check if CTRL-C typed */
1167
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 /* Check for the next breakpoint at or after the ":while"
1169 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (breakpoint != NULL)
1171 {
1172 *breakpoint = dbg_find_breakpoint(
1173 getline_equal(getline, cookie, getsourceline),
1174 fname,
1175 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1176 *dbg_tick = debug_tick;
1177 }
1178 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001179 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001181 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001183 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1184 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 }
1186 }
1187
1188 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001189 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001191 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001193 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1195 }
1196 }
1197
1198 /*
1199 * When not inside any ":while" loop, clear remembered lines.
1200 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 {
1203 if (lines_ga.ga_len > 0)
1204 {
1205 sourcing_lnum =
1206 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1207 free_cmdlines(&lines_ga);
1208 }
1209 current_line = 0;
1210 }
1211
1212 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1214 * being restored at the ":endtry". Reset them here and set the
1215 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1216 * This includes the case where a missing ":endif", ":endwhile" or
1217 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001219 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 cstack.cs_lflags &= ~CSL_HAD_FINA;
1222 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1223 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 did_throw ? (void *)current_exception : NULL);
1225 did_emsg = got_int = did_throw = FALSE;
1226 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1227 }
1228
1229 /* Update global "trylevel" for recursive calls to do_cmdline() from
1230 * within this loop. */
1231 trylevel = initial_trylevel + cstack.cs_trylevel;
1232
1233 /*
1234 * If the outermost try conditional (accross function calls and sourced
1235 * files) is aborted because of an error, an interrupt, or an uncaught
1236 * exception, cancel everything. If it is left normally, reset
1237 * force_abort to get the non-EH compatible abortion behavior for
1238 * the rest of the script.
1239 */
1240 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1241 force_abort = FALSE;
1242
1243 /* Convert an interrupt to an exception if appropriate. */
1244 (void)do_intthrow(&cstack);
1245#endif /* FEAT_EVAL */
1246
1247 }
1248 /*
1249 * Continue executing command lines when:
1250 * - no CTRL-C typed, no aborting error, no exception thrown or try
1251 * conditionals need to be checked for executing finally clauses or
1252 * catching an interrupt exception
1253 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001254 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 * looping for ":source" command or function call.
1256 */
1257 while (!((got_int
1258#ifdef FEAT_EVAL
1259 || (did_emsg && force_abort) || did_throw
1260#endif
1261 )
1262#ifdef FEAT_EVAL
1263 && cstack.cs_trylevel == 0
1264#endif
1265 )
1266 && !(did_emsg && used_getline
1267 && (getline_equal(getline, cookie, getexmodeline)
1268 || getline_equal(getline, cookie, getexline)))
1269 && (next_cmdline != NULL
1270#ifdef FEAT_EVAL
1271 || cstack.cs_idx >= 0
1272#endif
1273 || (flags & DOCMD_REPEAT)));
1274
1275 vim_free(cmdline_copy);
1276#ifdef FEAT_EVAL
1277 free_cmdlines(&lines_ga);
1278 ga_clear(&lines_ga);
1279
1280 if (cstack.cs_idx >= 0)
1281 {
1282 /*
1283 * If a sourced file or executed function ran to its end, report the
1284 * unclosed conditional.
1285 */
1286 if (!got_int && !did_throw
1287 && ((getline_equal(getline, cookie, getsourceline)
1288 && !source_finished(getline, cookie))
1289 || (getline_equal(getline, cookie, get_func_line)
1290 && !func_has_ended(real_cookie))))
1291 {
1292 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1293 EMSG(_(e_endtry));
1294 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1295 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001296 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1297 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 else
1299 EMSG(_(e_endif));
1300 }
1301
1302 /*
1303 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1304 * ":endtry" in a sourced file or executed function. If the try
1305 * conditional is in its finally clause, ignore anything pending.
1306 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001307 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 */
1309 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001310 {
1311 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1312
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001313 if (idx >= 0)
1314 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001315 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1316 &cstack.cs_looplevel);
1317 }
1318 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 trylevel = initial_trylevel;
1320 }
1321
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001322 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1323 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 * exception, do this now after rewinding the cstack. */
1325 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1326 ? (char_u *)"endfunction" : (char_u *)NULL);
1327
1328 if (trylevel == 0)
1329 {
1330 /*
1331 * When an exception is being thrown out of the outermost try
1332 * conditional, discard the uncaught exception, disable the conversion
1333 * of interrupts or errors to exceptions, and ensure that no more
1334 * commands are executed.
1335 */
1336 if (did_throw)
1337 {
1338 void *p = NULL;
1339 char_u *saved_sourcing_name;
1340 int saved_sourcing_lnum;
1341 struct msglist *messages = NULL, *next;
1342
1343 /*
1344 * If the uncaught exception is a user exception, report it as an
1345 * error. If it is an error exception, display the saved error
1346 * message now. For an interrupt exception, do nothing; the
1347 * interrupt message is given elsewhere.
1348 */
1349 switch (current_exception->type)
1350 {
1351 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001352 vim_snprintf((char *)IObuff, IOSIZE,
1353 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 current_exception->value);
1355 p = vim_strsave(IObuff);
1356 break;
1357 case ET_ERROR:
1358 messages = current_exception->messages;
1359 current_exception->messages = NULL;
1360 break;
1361 case ET_INTERRUPT:
1362 break;
1363 default:
1364 p = vim_strsave((char_u *)_(e_internal));
1365 }
1366
1367 saved_sourcing_name = sourcing_name;
1368 saved_sourcing_lnum = sourcing_lnum;
1369 sourcing_name = current_exception->throw_name;
1370 sourcing_lnum = current_exception->throw_lnum;
1371 current_exception->throw_name = NULL;
1372
1373 discard_current_exception(); /* uses IObuff if 'verbose' */
1374 suppress_errthrow = TRUE;
1375 force_abort = TRUE;
1376
1377 if (messages != NULL)
1378 {
1379 do
1380 {
1381 next = messages->next;
1382 emsg(messages->msg);
1383 vim_free(messages->msg);
1384 vim_free(messages);
1385 messages = next;
1386 }
1387 while (messages != NULL);
1388 }
1389 else if (p != NULL)
1390 {
1391 emsg(p);
1392 vim_free(p);
1393 }
1394 vim_free(sourcing_name);
1395 sourcing_name = saved_sourcing_name;
1396 sourcing_lnum = saved_sourcing_lnum;
1397 }
1398
1399 /*
1400 * On an interrupt or an aborting error not converted to an exception,
1401 * disable the conversion of errors to exceptions. (Interrupts are not
1402 * converted any more, here.) This enables also the interrupt message
1403 * when force_abort is set and did_emsg unset in case of an interrupt
1404 * from a finally clause after an error.
1405 */
1406 else if (got_int || (did_emsg && force_abort))
1407 suppress_errthrow = TRUE;
1408 }
1409
1410 /*
1411 * The current cstack will be freed when do_cmdline() returns. An uncaught
1412 * exception will have to be rethrown in the previous cstack. If a function
1413 * has just returned or a script file was just finished and the previous
1414 * cstack belongs to the same function or, respectively, script file, it
1415 * will have to be checked for finally clauses to be executed due to the
1416 * ":return" or ":finish". This is done in do_one_cmd().
1417 */
1418 if (did_throw)
1419 need_rethrow = TRUE;
1420 if ((getline_equal(getline, cookie, getsourceline)
1421 && ex_nesting_level > source_level(real_cookie))
1422 || (getline_equal(getline, cookie, get_func_line)
1423 && ex_nesting_level > func_level(real_cookie) + 1))
1424 {
1425 if (!did_throw)
1426 check_cstack = TRUE;
1427 }
1428 else
1429 {
1430 /* When leaving a function, reduce nesting level. */
1431 if (getline_equal(getline, cookie, get_func_line))
1432 --ex_nesting_level;
1433 /*
1434 * Go to debug mode when returning from a function in which we are
1435 * single-stepping.
1436 */
1437 if ((getline_equal(getline, cookie, getsourceline)
1438 || getline_equal(getline, cookie, get_func_line))
1439 && ex_nesting_level + 1 <= debug_break_level)
1440 do_debug(getline_equal(getline, cookie, getsourceline)
1441 ? (char_u *)_("End of sourced file")
1442 : (char_u *)_("End of function"));
1443 }
1444
1445 /*
1446 * Restore the exception environment (done after returning from the
1447 * debugger).
1448 */
1449 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001450 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451
1452 msg_list = saved_msg_list;
1453#endif /* FEAT_EVAL */
1454
1455 /*
1456 * If there was too much output to fit on the command line, ask the user to
1457 * hit return before redrawing the screen. With the ":global" command we do
1458 * this only once after the command is finished.
1459 */
1460 if (did_inc)
1461 {
1462 --RedrawingDisabled;
1463 --no_wait_return;
1464 msg_scroll = FALSE;
1465
1466 /*
1467 * When just finished an ":if"-":else" which was typed, no need to
1468 * wait for hit-return. Also for an error situation.
1469 */
1470 if (retval == FAIL
1471#ifdef FEAT_EVAL
1472 || (did_endif && KeyTyped && !did_emsg)
1473#endif
1474 )
1475 {
1476 need_wait_return = FALSE;
1477 msg_didany = FALSE; /* don't wait when restarting edit */
1478 }
1479 else if (need_wait_return)
1480 {
1481 /*
1482 * The msg_start() above clears msg_didout. The wait_return we do
1483 * here should not overwrite the command that may be shown before
1484 * doing that.
1485 */
1486 msg_didout |= msg_didout_before_start;
1487 wait_return(FALSE);
1488 }
1489 }
1490
1491#ifndef FEAT_EVAL
1492 /*
1493 * Reset if_level, in case a sourced script file contains more ":if" than
1494 * ":endif" (could be ":if x | foo | endif").
1495 */
1496 if_level = 0;
1497#endif
1498
1499 --call_depth;
1500 return retval;
1501}
1502
1503#ifdef FEAT_EVAL
1504/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001505 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 */
1507 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001508get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 int c;
1510 void *cookie;
1511 int indent;
1512{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001513 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 wcmd_T *wp;
1515 char_u *line;
1516
1517 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1518 {
1519 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001520 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001522 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 if (cp->getline == NULL)
1524 line = getcmdline(c, 0L, indent);
1525 else
1526 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001527 if (store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 ++cp->current_line;
1529
1530 return line;
1531 }
1532
1533 KeyTyped = FALSE;
1534 ++cp->current_line;
1535 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1536 sourcing_lnum = wp->lnum;
1537 return vim_strsave(wp->line);
1538}
1539
1540/*
1541 * Store a line in "gap" so that a ":while" loop can execute it again.
1542 */
1543 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001544store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 garray_T *gap;
1546 char_u *line;
1547{
1548 if (ga_grow(gap, 1) == FAIL)
1549 return FAIL;
1550 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1551 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1552 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 return OK;
1554}
1555
1556/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001557 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 */
1559 static void
1560free_cmdlines(gap)
1561 garray_T *gap;
1562{
1563 while (gap->ga_len > 0)
1564 {
1565 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1566 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 }
1568}
1569#endif
1570
1571/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001572 * If "getline" is get_loop_line(), return TRUE if the getline it uses equals
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 * "func". * Otherwise return TRUE when "getline" equals "func".
1574 */
1575/*ARGSUSED*/
1576 int
1577getline_equal(getline, cookie, func)
1578 char_u *(*getline) __ARGS((int, void *, int));
1579 void *cookie; /* argument for getline() */
1580 char_u *(*func) __ARGS((int, void *, int));
1581{
1582#ifdef FEAT_EVAL
1583 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001584 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001586 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 * function that's orignally used to obtain the lines. This may be nested
1588 * several levels. */
1589 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001590 cp = (struct loop_cookie *)cookie;
1591 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
1593 gp = cp->getline;
1594 cp = cp->cookie;
1595 }
1596 return gp == func;
1597#else
1598 return getline == func;
1599#endif
1600}
1601
1602#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1603/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001604 * If "getline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 * getline function. Otherwise return "cookie".
1606 */
1607/*ARGSUSED*/
1608 void *
1609getline_cookie(getline, cookie)
1610 char_u *(*getline) __ARGS((int, void *, int));
1611 void *cookie; /* argument for getline() */
1612{
1613# ifdef FEAT_EVAL
1614 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001615 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001617 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 * cookie that's orignally used to obtain the lines. This may be nested
1619 * several levels. */
1620 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001621 cp = (struct loop_cookie *)cookie;
1622 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 gp = cp->getline;
1625 cp = cp->cookie;
1626 }
1627 return cp;
1628# else
1629 return cookie;
1630# endif
1631}
1632#endif
1633
1634/*
1635 * Execute one Ex command.
1636 *
1637 * If 'sourcing' is TRUE, the command will be included in the error message.
1638 *
1639 * 1. skip comment lines and leading space
1640 * 2. handle command modifiers
1641 * 3. parse range
1642 * 4. parse command
1643 * 5. parse arguments
1644 * 6. switch on command name
1645 *
1646 * Note: "getline" can be NULL.
1647 *
1648 * This function may be called recursively!
1649 */
1650#if (_MSC_VER == 1200)
1651/*
Bram Moolenaared203462004-06-16 11:19:22 +00001652 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001654 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655#endif
1656 static char_u *
1657do_one_cmd(cmdlinep, sourcing,
1658#ifdef FEAT_EVAL
1659 cstack,
1660#endif
1661 getline, cookie)
1662 char_u **cmdlinep;
1663 int sourcing;
1664#ifdef FEAT_EVAL
1665 struct condstack *cstack;
1666#endif
1667 char_u *(*getline) __ARGS((int, void *, int));
1668 void *cookie; /* argument for getline() */
1669{
1670 char_u *p;
1671 linenr_T lnum;
1672 long n;
1673 char_u *errormsg = NULL; /* error message */
1674 exarg_T ea; /* Ex command arguments */
1675 long verbose_save = -1;
1676 int save_msg_scroll = 0;
1677 int did_silent = 0;
1678 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001679#ifdef HAVE_SANDBOX
1680 int did_sandbox = FALSE;
1681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 cmdmod_T save_cmdmod;
1683 int ni; /* set when Not Implemented */
1684
1685 vim_memset(&ea, 0, sizeof(ea));
1686 ea.line1 = 1;
1687 ea.line2 = 1;
1688#ifdef FEAT_EVAL
1689 ++ex_nesting_level;
1690#endif
1691
1692 /* when not editing the last file :q has to be typed twice */
1693 if (quitmore
1694#ifdef FEAT_EVAL
1695 /* avoid that a function call in 'statusline' does this */
1696 && !getline_equal(getline, cookie, get_func_line)
1697#endif
1698 )
1699 --quitmore;
1700
1701 /*
1702 * Reset browse, confirm, etc.. They are restored when returning, for
1703 * recursive calls.
1704 */
1705 save_cmdmod = cmdmod;
1706 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1707
1708 /*
1709 * Repeat until no more command modifiers are found.
1710 */
1711 ea.cmd = *cmdlinep;
1712 for (;;)
1713 {
1714/*
1715 * 1. skip comment lines and leading white space and colons
1716 */
1717 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1718 ++ea.cmd;
1719
1720 /* in ex mode, an empty line works like :+ */
1721 if (*ea.cmd == NUL && exmode_active
1722 && (getline_equal(getline, cookie, getexmodeline)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001723 || getline_equal(getline, cookie, getexline))
1724 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 {
1726 ea.cmd = (char_u *)"+";
1727 ex_pressedreturn = TRUE;
1728 }
1729
1730 /* ignore comment and empty lines */
1731 if (*ea.cmd == '"' || *ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001732 {
1733 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736
1737/*
1738 * 2. handle command modifiers.
1739 */
1740 p = ea.cmd;
1741 if (VIM_ISDIGIT(*ea.cmd))
1742 p = skipwhite(skipdigits(ea.cmd));
1743 switch (*p)
1744 {
1745 /* When adding an entry, also modify cmd_exists(). */
1746 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1747 break;
1748#ifdef FEAT_WINDOWS
1749 cmdmod.split |= WSP_ABOVE;
1750#endif
1751 continue;
1752
1753 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1754 {
1755#ifdef FEAT_WINDOWS
1756 cmdmod.split |= WSP_BELOW;
1757#endif
1758 continue;
1759 }
1760 if (checkforcmd(&ea.cmd, "browse", 3))
1761 {
1762#ifdef FEAT_BROWSE
1763 cmdmod.browse = TRUE;
1764#endif
1765 continue;
1766 }
1767 if (!checkforcmd(&ea.cmd, "botright", 2))
1768 break;
1769#ifdef FEAT_WINDOWS
1770 cmdmod.split |= WSP_BOT;
1771#endif
1772 continue;
1773
1774 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1775 break;
1776#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1777 cmdmod.confirm = TRUE;
1778#endif
1779 continue;
1780
1781 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1782 {
1783 cmdmod.keepmarks = TRUE;
1784 continue;
1785 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001786 if (checkforcmd(&ea.cmd, "keepalt", 5))
1787 {
1788 cmdmod.keepalt = TRUE;
1789 continue;
1790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1792 break;
1793 cmdmod.keepjumps = TRUE;
1794 continue;
1795
1796 /* ":hide" and ":hide | cmd" are not modifiers */
1797 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1798 || *p == NUL || ends_excmd(*p))
1799 break;
1800 ea.cmd = p;
1801 cmdmod.hide = TRUE;
1802 continue;
1803
1804 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1805 {
1806 cmdmod.lockmarks = TRUE;
1807 continue;
1808 }
1809
1810 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1811 break;
1812#ifdef FEAT_WINDOWS
1813 cmdmod.split |= WSP_ABOVE;
1814#endif
1815 continue;
1816
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001817 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1818 break;
1819#ifdef FEAT_AUTOCMD
1820 if (cmdmod.save_ei == NULL)
1821 {
1822 /* Set 'eventignore' to "all". Don't free the
1823 * existing option value, we restore it later. */
1824 cmdmod.save_ei = vim_strsave(p_ei);
1825 set_string_option_direct((char_u *)"ei", -1,
1826 (char_u *)"all", OPT_FREE);
1827 }
1828#endif
1829 continue;
1830
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1832 break;
1833#ifdef FEAT_WINDOWS
1834 cmdmod.split |= WSP_BELOW;
1835#endif
1836 continue;
1837
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001838 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1839 {
1840#ifdef HAVE_SANDBOX
1841 if (!did_sandbox)
1842 ++sandbox;
1843 did_sandbox = TRUE;
1844#endif
1845 continue;
1846 }
1847 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 break;
1849 ++did_silent;
1850 ++msg_silent;
1851 save_msg_scroll = msg_scroll;
1852 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1853 {
1854 /* ":silent!", but not "silent !cmd" */
1855 ea.cmd = skipwhite(ea.cmd + 1);
1856 ++emsg_silent;
1857 ++did_esilent;
1858 }
1859 continue;
1860
1861 case 't': if (!checkforcmd(&ea.cmd, "topleft", 2))
1862 break;
1863#ifdef FEAT_WINDOWS
1864 cmdmod.split |= WSP_TOP;
1865#endif
1866 continue;
1867
1868 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1869 {
1870#ifdef FEAT_VERTSPLIT
1871 cmdmod.split |= WSP_VERT;
1872#endif
1873 continue;
1874 }
1875 if (!checkforcmd(&p, "verbose", 4))
1876 break;
1877 if (verbose_save < 0)
1878 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001879 if (vim_isdigit(*ea.cmd))
1880 p_verbose = atoi((char *)ea.cmd);
1881 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 p_verbose = 1;
1883 ea.cmd = p;
1884 continue;
1885 }
1886 break;
1887 }
1888
1889#ifdef FEAT_EVAL
1890 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1891 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1892#else
1893 ea.skip = (if_level > 0);
1894#endif
1895
1896#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001897# ifdef FEAT_PROFILE
1898 /* Count this line for profiling if ea.skip is FALSE. */
1899 if (do_profiling && !ea.skip)
1900 {
1901 if (getline_equal(getline, cookie, get_func_line))
1902 func_line_exec(getline_cookie(getline, cookie));
1903 else if (getline_equal(getline, cookie, getsourceline))
1904 script_line_exec();
1905 }
1906#endif
1907
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 /* May go to debug mode. If this happens and the ">quit" debug command is
1909 * used, throw an interrupt exception and skip the next command. */
1910 dbg_check_breakpoint(&ea);
1911 if (!ea.skip && got_int)
1912 {
1913 ea.skip = TRUE;
1914 (void)do_intthrow(cstack);
1915 }
1916#endif
1917
1918/*
1919 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1920 *
1921 * where 'addr' is:
1922 *
1923 * % (entire file)
1924 * $ [+-NUM]
1925 * 'x [+-NUM] (where x denotes a currently defined mark)
1926 * . [+-NUM]
1927 * [+-NUM]..
1928 * NUM
1929 *
1930 * The ea.cmd pointer is updated to point to the first character following the
1931 * range spec. If an initial address is found, but no second, the upper bound
1932 * is equal to the lower.
1933 */
1934
1935 /* repeat for all ',' or ';' separated addresses */
1936 for (;;)
1937 {
1938 ea.line1 = ea.line2;
1939 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1940 ea.cmd = skipwhite(ea.cmd);
1941 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1942 if (ea.cmd == NULL) /* error detected */
1943 goto doend;
1944 if (lnum == MAXLNUM)
1945 {
1946 if (*ea.cmd == '%') /* '%' - all lines */
1947 {
1948 ++ea.cmd;
1949 ea.line1 = 1;
1950 ea.line2 = curbuf->b_ml.ml_line_count;
1951 ++ea.addr_count;
1952 }
1953 /* '*' - visual area */
1954 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1955 {
1956 pos_T *fp;
1957
1958 ++ea.cmd;
1959 if (!ea.skip)
1960 {
1961 fp = getmark('<', FALSE);
1962 if (check_mark(fp) == FAIL)
1963 goto doend;
1964 ea.line1 = fp->lnum;
1965 fp = getmark('>', FALSE);
1966 if (check_mark(fp) == FAIL)
1967 goto doend;
1968 ea.line2 = fp->lnum;
1969 ++ea.addr_count;
1970 }
1971 }
1972 }
1973 else
1974 ea.line2 = lnum;
1975 ea.addr_count++;
1976
1977 if (*ea.cmd == ';')
1978 {
1979 if (!ea.skip)
1980 curwin->w_cursor.lnum = ea.line2;
1981 }
1982 else if (*ea.cmd != ',')
1983 break;
1984 ++ea.cmd;
1985 }
1986
1987 /* One address given: set start and end lines */
1988 if (ea.addr_count == 1)
1989 {
1990 ea.line1 = ea.line2;
1991 /* ... but only implicit: really no address given */
1992 if (lnum == MAXLNUM)
1993 ea.addr_count = 0;
1994 }
1995
1996 /* Don't leave the cursor on an illegal line (caused by ';') */
1997 check_cursor_lnum();
1998
1999/*
2000 * 4. parse command
2001 */
2002
2003 /*
2004 * Skip ':' and any white space
2005 */
2006 ea.cmd = skipwhite(ea.cmd);
2007 while (*ea.cmd == ':')
2008 ea.cmd = skipwhite(ea.cmd + 1);
2009
2010 /*
2011 * If we got a line, but no command, then go to the line.
2012 * If we find a '|' or '\n' we set ea.nextcmd.
2013 */
2014 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2015 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2016 {
2017 /*
2018 * strange vi behaviour:
2019 * ":3" jumps to line 3
2020 * ":3|..." prints line 3
2021 * ":|" prints current line
2022 */
2023 if (ea.skip) /* skip this if inside :if */
2024 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002025 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 {
2027 ea.cmdidx = CMD_print;
2028 ea.argt = RANGE+COUNT+TRLBAR;
2029 if ((errormsg = invalid_range(&ea)) == NULL)
2030 {
2031 correct_range(&ea);
2032 ex_print(&ea);
2033 }
2034 }
2035 else if (ea.addr_count != 0)
2036 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002037 if (ea.line2 > curbuf->b_ml.ml_line_count)
2038 {
2039 /* With '-' in 'cpoptions' a line number past the file is an
2040 * error, otherwise put it at the end of the file. */
2041 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2042 ea.line2 = -1;
2043 else
2044 ea.line2 = curbuf->b_ml.ml_line_count;
2045 }
2046
2047 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002048 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 else
2050 {
2051 if (ea.line2 == 0)
2052 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 else
2054 curwin->w_cursor.lnum = ea.line2;
2055 beginline(BL_SOL | BL_FIX);
2056 }
2057 }
2058 goto doend;
2059 }
2060
2061 /* Find the command and let "p" point to after it. */
2062 p = find_command(&ea, NULL);
2063
2064#ifdef FEAT_USR_CMDS
2065 if (p == NULL)
2066 {
2067 if (!ea.skip)
2068 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2069 goto doend;
2070 }
2071 /* Check for wrong commands. */
2072 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2073 {
2074 errormsg = uc_fun_cmd();
2075 goto doend;
2076 }
2077#endif
2078 if (ea.cmdidx == CMD_SIZE)
2079 {
2080 if (!ea.skip)
2081 {
2082 STRCPY(IObuff, _("E492: Not an editor command"));
2083 if (!sourcing)
2084 {
2085 STRCAT(IObuff, ": ");
2086 STRNCAT(IObuff, *cmdlinep, 40);
2087 }
2088 errormsg = IObuff;
2089 }
2090 goto doend;
2091 }
2092
2093 ni = (
2094#ifdef FEAT_USR_CMDS
2095 !USER_CMDIDX(ea.cmdidx) &&
2096#endif
2097 cmdnames[ea.cmdidx].cmd_func == ex_ni);
2098
2099#ifndef FEAT_EVAL
2100 /*
2101 * When the expression evaluation is disabled, recognize the ":if" and
2102 * ":endif" commands and ignore everything in between it.
2103 */
2104 if (ea.cmdidx == CMD_if)
2105 ++if_level;
2106 if (if_level)
2107 {
2108 if (ea.cmdidx == CMD_endif)
2109 --if_level;
2110 goto doend;
2111 }
2112
2113#endif
2114
2115 if (*p == '!' && ea.cmdidx != CMD_substitute) /* forced commands */
2116 {
2117 ++p;
2118 ea.forceit = TRUE;
2119 }
2120 else
2121 ea.forceit = FALSE;
2122
2123/*
2124 * 5. parse arguments
2125 */
2126#ifdef FEAT_USR_CMDS
2127 if (!USER_CMDIDX(ea.cmdidx))
2128#endif
2129 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
2130
2131 if (!ea.skip)
2132 {
2133#ifdef HAVE_SANDBOX
2134 if (sandbox != 0 && !(ea.argt & SBOXOK))
2135 {
2136 /* Command not allowed in sandbox. */
2137 errormsg = (char_u *)_(e_sandbox);
2138 goto doend;
2139 }
2140#endif
2141 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2142 {
2143 /* Command not allowed in non-'modifiable' buffer */
2144 errormsg = (char_u *)_(e_modifiable);
2145 goto doend;
2146 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002147
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002148 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149# ifdef FEAT_USR_CMDS
2150 && !USER_CMDIDX(ea.cmdidx)
2151# endif
2152 )
2153 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002154 /* Command not allowed when editing the command line. */
2155#ifdef FEAT_CMDWIN
2156 if (cmdwin_type != 0)
2157 errormsg = (char_u *)_(e_cmdwin);
2158 else
2159#endif
2160 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 goto doend;
2162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
2164 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2165 {
2166 /* no range allowed */
2167 errormsg = (char_u *)_(e_norange);
2168 goto doend;
2169 }
2170 }
2171
2172 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2173 {
2174 errormsg = (char_u *)_(e_nobang);
2175 goto doend;
2176 }
2177
2178 /*
2179 * Don't complain about the range if it is not used
2180 * (could happen if line_count is accidentally set to 0).
2181 */
2182 if (!ea.skip && !ni)
2183 {
2184 /*
2185 * If the range is backwards, ask for confirmation and, if given, swap
2186 * ea.line1 & ea.line2 so it's forwards again.
2187 * When global command is busy, don't ask, will fail below.
2188 */
2189 if (!global_busy && ea.line1 > ea.line2)
2190 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002191 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002193 if (sourcing || exmode_active)
2194 {
2195 errormsg = (char_u *)_("E493: Backwards range given");
2196 goto doend;
2197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 if (ask_yesno((char_u *)
2199 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002200 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 }
2202 lnum = ea.line1;
2203 ea.line1 = ea.line2;
2204 ea.line2 = lnum;
2205 }
2206 if ((errormsg = invalid_range(&ea)) != NULL)
2207 goto doend;
2208 }
2209
2210 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2211 ea.line2 = 1;
2212
2213 correct_range(&ea);
2214
2215#ifdef FEAT_FOLDING
2216 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2217 {
2218 /* Put the first line at the start of a closed fold, put the last line
2219 * at the end of a closed fold. */
2220 (void)hasFolding(ea.line1, &ea.line1, NULL);
2221 (void)hasFolding(ea.line2, NULL, &ea.line2);
2222 }
2223#endif
2224
2225#ifdef FEAT_QUICKFIX
2226 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002227 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 * option here, so things like % get expanded.
2229 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002230 p = replace_makeprg(&ea, p, cmdlinep);
2231 if (p == NULL)
2232 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233#endif
2234
2235 /*
2236 * Skip to start of argument.
2237 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2238 */
2239 if (ea.cmdidx == CMD_bang)
2240 ea.arg = p;
2241 else
2242 ea.arg = skipwhite(p);
2243
2244 /*
2245 * Check for "++opt=val" argument.
2246 * Must be first, allow ":w ++enc=utf8 !cmd"
2247 */
2248 if (ea.argt & ARGOPT)
2249 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2250 if (getargopt(&ea) == FAIL && !ni)
2251 {
2252 errormsg = (char_u *)_(e_invarg);
2253 goto doend;
2254 }
2255
2256 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2257 {
2258 if (*ea.arg == '>') /* append */
2259 {
2260 if (*++ea.arg != '>') /* typed wrong */
2261 {
2262 errormsg = (char_u *)_("E494: Use w or w>>");
2263 goto doend;
2264 }
2265 ea.arg = skipwhite(ea.arg + 1);
2266 ea.append = TRUE;
2267 }
2268 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2269 {
2270 ++ea.arg;
2271 ea.usefilter = TRUE;
2272 }
2273 }
2274
2275 if (ea.cmdidx == CMD_read)
2276 {
2277 if (ea.forceit)
2278 {
2279 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2280 ea.forceit = FALSE;
2281 }
2282 else if (*ea.arg == '!') /* :r !filter */
2283 {
2284 ++ea.arg;
2285 ea.usefilter = TRUE;
2286 }
2287 }
2288
2289 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2290 {
2291 ea.amount = 1;
2292 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2293 {
2294 ++ea.arg;
2295 ++ea.amount;
2296 }
2297 ea.arg = skipwhite(ea.arg);
2298 }
2299
2300 /*
2301 * Check for "+command" argument, before checking for next command.
2302 * Don't do this for ":read !cmd" and ":write !cmd".
2303 */
2304 if ((ea.argt & EDITCMD) && !ea.usefilter)
2305 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2306
2307 /*
2308 * Check for '|' to separate commands and '"' to start comments.
2309 * Don't do this for ":read !cmd" and ":write !cmd".
2310 */
2311 if ((ea.argt & TRLBAR) && !ea.usefilter)
2312 separate_nextcmd(&ea);
2313
2314 /*
2315 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002316 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2317 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002319 else if (ea.cmdidx == CMD_bang
2320 || ea.cmdidx == CMD_global
2321 || ea.cmdidx == CMD_vglobal
2322 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
2324 for (p = ea.arg; *p; ++p)
2325 {
2326 /* Remove one backslash before a newline, so that it's possible to
2327 * pass a newline to the shell and also a newline that is preceded
2328 * with a backslash. This makes it impossible to end a shell
2329 * command in a backslash, but that doesn't appear useful.
2330 * Halving the number of backslashes is incompatible with previous
2331 * versions. */
2332 if (*p == '\\' && p[1] == '\n')
2333 mch_memmove(p, p + 1, STRLEN(p));
2334 else if (*p == '\n')
2335 {
2336 ea.nextcmd = p + 1;
2337 *p = NUL;
2338 break;
2339 }
2340 }
2341 }
2342
2343 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2344 {
2345 ea.line1 = 1;
2346 ea.line2 = curbuf->b_ml.ml_line_count;
2347 }
2348
2349 /* accept numbered register only when no count allowed (:put) */
2350 if ( (ea.argt & REGSTR)
2351 && *ea.arg != NUL
2352#ifdef FEAT_USR_CMDS
2353 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2354 && USER_CMDIDX(ea.cmdidx)))
2355 /* Do not allow register = for user commands */
2356 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2357#else
2358 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2359#endif
2360 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2361 {
2362 ea.regname = *ea.arg++;
2363#ifdef FEAT_EVAL
2364 /* for '=' register: accept the rest of the line as an expression */
2365 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2366 {
2367 set_expr_line(vim_strsave(ea.arg));
2368 ea.arg += STRLEN(ea.arg);
2369 }
2370#endif
2371 ea.arg = skipwhite(ea.arg);
2372 }
2373
2374 /*
2375 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2376 * count, it's a buffer name.
2377 */
2378 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2379 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2380 || vim_iswhite(*p)))
2381 {
2382 n = getdigits(&ea.arg);
2383 ea.arg = skipwhite(ea.arg);
2384 if (n <= 0 && !ni)
2385 {
2386 errormsg = (char_u *)_(e_zerocount);
2387 goto doend;
2388 }
2389 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2390 {
2391 ea.line2 = n;
2392 if (ea.addr_count == 0)
2393 ea.addr_count = 1;
2394 }
2395 else
2396 {
2397 ea.line1 = ea.line2;
2398 ea.line2 += n - 1;
2399 ++ea.addr_count;
2400 /*
2401 * Be vi compatible: no error message for out of range.
2402 */
2403 if (ea.line2 > curbuf->b_ml.ml_line_count)
2404 ea.line2 = curbuf->b_ml.ml_line_count;
2405 }
2406 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002407
2408 /*
2409 * Check for flags: 'l', 'p' and '#'.
2410 */
2411 if (ea.argt & EXFLAGS)
2412 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002414 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
2415 && vim_strchr((char_u *)"|\"", *ea.arg) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 {
2417 errormsg = (char_u *)_(e_trailing);
2418 goto doend;
2419 }
2420
2421 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2422 {
2423 errormsg = (char_u *)_(e_argreq);
2424 goto doend;
2425 }
2426
2427#ifdef FEAT_EVAL
2428 /*
2429 * Skip the command when it's not going to be executed.
2430 * The commands like :if, :endif, etc. always need to be executed.
2431 * Also make an exception for commands that handle a trailing command
2432 * themselves.
2433 */
2434 if (ea.skip)
2435 {
2436 switch (ea.cmdidx)
2437 {
2438 /* commands that need evaluation */
2439 case CMD_while:
2440 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002441 case CMD_for:
2442 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 case CMD_if:
2444 case CMD_elseif:
2445 case CMD_else:
2446 case CMD_endif:
2447 case CMD_try:
2448 case CMD_catch:
2449 case CMD_finally:
2450 case CMD_endtry:
2451 case CMD_function:
2452 break;
2453
2454 /* Commands that handle '|' themselves. Check: A command should
2455 * either have the TRLBAR flag, appear in this list or appear in
2456 * the list at ":help :bar". */
2457 case CMD_aboveleft:
2458 case CMD_and:
2459 case CMD_belowright:
2460 case CMD_botright:
2461 case CMD_browse:
2462 case CMD_call:
2463 case CMD_confirm:
2464 case CMD_delfunction:
2465 case CMD_djump:
2466 case CMD_dlist:
2467 case CMD_dsearch:
2468 case CMD_dsplit:
2469 case CMD_echo:
2470 case CMD_echoerr:
2471 case CMD_echomsg:
2472 case CMD_echon:
2473 case CMD_execute:
2474 case CMD_help:
2475 case CMD_hide:
2476 case CMD_ijump:
2477 case CMD_ilist:
2478 case CMD_isearch:
2479 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002480 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 case CMD_keepjumps:
2482 case CMD_keepmarks:
2483 case CMD_leftabove:
2484 case CMD_let:
2485 case CMD_lockmarks:
2486 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002487 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 case CMD_perl:
2489 case CMD_psearch:
2490 case CMD_python:
2491 case CMD_return:
2492 case CMD_rightbelow:
2493 case CMD_ruby:
2494 case CMD_silent:
2495 case CMD_smagic:
2496 case CMD_snomagic:
2497 case CMD_substitute:
2498 case CMD_syntax:
2499 case CMD_tcl:
2500 case CMD_throw:
2501 case CMD_tilde:
2502 case CMD_topleft:
2503 case CMD_unlet:
2504 case CMD_verbose:
2505 case CMD_vertical:
2506 break;
2507
2508 default: goto doend;
2509 }
2510 }
2511#endif
2512
2513 if (ea.argt & XFILE)
2514 {
2515 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2516 goto doend;
2517 }
2518
2519#ifdef FEAT_LISTCMDS
2520 /*
2521 * Accept buffer name. Cannot be used at the same time with a buffer
2522 * number. Don't do this for a user command.
2523 */
2524 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2525# ifdef FEAT_USR_CMDS
2526 && !USER_CMDIDX(ea.cmdidx)
2527# endif
2528 )
2529 {
2530 /*
2531 * :bdelete, :bwipeout and :bunload take several arguments, separated
2532 * by spaces: find next space (skipping over escaped characters).
2533 * The others take one argument: ignore trailing spaces.
2534 */
2535 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2536 || ea.cmdidx == CMD_bunload)
2537 p = skiptowhite_esc(ea.arg);
2538 else
2539 {
2540 p = ea.arg + STRLEN(ea.arg);
2541 while (p > ea.arg && vim_iswhite(p[-1]))
2542 --p;
2543 }
2544 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2545 if (ea.line2 < 0) /* failed */
2546 goto doend;
2547 ea.addr_count = 1;
2548 ea.arg = skipwhite(p);
2549 }
2550#endif
2551
2552/*
2553 * 6. switch on command name
2554 *
2555 * The "ea" structure holds the arguments that can be used.
2556 */
2557 ea.cmdlinep = cmdlinep;
2558 ea.getline = getline;
2559 ea.cookie = cookie;
2560#ifdef FEAT_EVAL
2561 ea.cstack = cstack;
2562#endif
2563
2564#ifdef FEAT_USR_CMDS
2565 if (USER_CMDIDX(ea.cmdidx))
2566 {
2567 /*
2568 * Execute a user-defined command.
2569 */
2570 do_ucmd(&ea);
2571 }
2572 else
2573#endif
2574 {
2575 /*
2576 * Call the function to execute the command.
2577 */
2578 ea.errmsg = NULL;
2579 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2580 if (ea.errmsg != NULL)
2581 errormsg = (char_u *)_(ea.errmsg);
2582 }
2583
2584#ifdef FEAT_EVAL
2585 /*
2586 * If the command just executed called do_cmdline(), any throw or ":return"
2587 * or ":finish" encountered there must also check the cstack of the still
2588 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2589 * exception, or reanimate a returned function or finished script file and
2590 * return or finish it again.
2591 */
2592 if (need_rethrow)
2593 do_throw(cstack);
2594 else if (check_cstack)
2595 {
2596 if (source_finished(getline, cookie))
2597 do_finish(&ea, TRUE);
2598 else if (getline_equal(getline, cookie, get_func_line)
2599 && current_func_returned())
2600 do_return(&ea, TRUE, FALSE, NULL);
2601 }
2602 need_rethrow = check_cstack = FALSE;
2603#endif
2604
2605doend:
2606 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2607 curwin->w_cursor.lnum = 1;
2608
2609 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2610 {
2611 if (sourcing)
2612 {
2613 if (errormsg != IObuff)
2614 {
2615 STRCPY(IObuff, errormsg);
2616 errormsg = IObuff;
2617 }
2618 STRCAT(errormsg, ": ");
2619 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff));
2620 }
2621 emsg(errormsg);
2622 }
2623#ifdef FEAT_EVAL
2624 do_errthrow(cstack,
2625 (ea.cmdidx != CMD_SIZE
2626# ifdef FEAT_USR_CMDS
2627 && !USER_CMDIDX(ea.cmdidx)
2628# endif
2629 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2630#endif
2631
2632 if (verbose_save >= 0)
2633 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002634#ifdef FEAT_AUTOCMD
2635 if (cmdmod.save_ei != NULL)
2636 {
2637 /* Restore 'eventignore' to the value before ":noautocmd". */
2638 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei, OPT_FREE);
2639 free_string_option(cmdmod.save_ei);
2640 }
2641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
2643 cmdmod = save_cmdmod;
2644
2645 if (did_silent > 0)
2646 {
2647 /* messages could be enabled for a serious error, need to check if the
2648 * counters don't become negative */
2649 msg_silent -= did_silent;
2650 if (msg_silent < 0)
2651 msg_silent = 0;
2652 emsg_silent -= did_esilent;
2653 if (emsg_silent < 0)
2654 emsg_silent = 0;
2655 /* Restore msg_scroll, it's set by file I/O commands, even when no
2656 * message is actually displayed. */
2657 msg_scroll = save_msg_scroll;
2658 }
2659
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002660#ifdef HAVE_SANDBOX
2661 if (did_sandbox)
2662 --sandbox;
2663#endif
2664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2666 ea.nextcmd = NULL;
2667
2668#ifdef FEAT_EVAL
2669 --ex_nesting_level;
2670#endif
2671
2672 return ea.nextcmd;
2673}
2674#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002675 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676#endif
2677
2678/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002679 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 * If there is a match advance "pp" to the argument and return TRUE.
2681 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002682 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002684 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 char *cmd; /* name of command */
2686 int len; /* required length */
2687{
2688 int i;
2689
2690 for (i = 0; cmd[i] != NUL; ++i)
2691 if (cmd[i] != (*pp)[i])
2692 break;
2693 if (i >= len && !isalpha((*pp)[i]))
2694 {
2695 *pp = skipwhite(*pp + i);
2696 return TRUE;
2697 }
2698 return FALSE;
2699}
2700
2701/*
2702 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002703 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002705 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 * Returns NULL for an ambiguous user command.
2707 */
2708/*ARGSUSED*/
2709 static char_u *
2710find_command(eap, full)
2711 exarg_T *eap;
2712 int *full;
2713{
2714 int len;
2715 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002716 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
2718 /*
2719 * Isolate the command and search for it in the command table.
2720 * Exeptions:
2721 * - the 'k' command can directly be followed by any character.
2722 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2723 * but :sre[wind] is another command, as are :scrip[tnames],
2724 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002725 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 */
2727 p = eap->cmd;
2728 if (*p == 'k')
2729 {
2730 eap->cmdidx = CMD_k;
2731 ++p;
2732 }
2733 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002734 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2735 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 || p[1] == 'g'
2737 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2738 || p[1] == 'I'
2739 || (p[1] == 'r' && p[2] != 'e')))
2740 {
2741 eap->cmdidx = CMD_substitute;
2742 ++p;
2743 }
2744 else
2745 {
2746 while (ASCII_ISALPHA(*p))
2747 ++p;
2748 /* check for non-alpha command */
2749 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2750 ++p;
2751 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002752 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2753 {
2754 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2755 * :delete with the 'l' flag. Same for 'p'. */
2756 for (i = 0; i < len; ++i)
2757 if (eap->cmd[i] != "delete"[i])
2758 break;
2759 if (i == len - 1)
2760 {
2761 --len;
2762 if (p[-1] == 'l')
2763 eap->flags |= EXFLAG_LIST;
2764 else
2765 eap->flags |= EXFLAG_PRINT;
2766 }
2767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
2769 if (ASCII_ISLOWER(*eap->cmd))
2770 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2771 else
2772 eap->cmdidx = cmdidxs[26];
2773
2774 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2775 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2776 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2777 (size_t)len) == 0)
2778 {
2779#ifdef FEAT_EVAL
2780 if (full != NULL
2781 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2782 *full = TRUE;
2783#endif
2784 break;
2785 }
2786
2787#ifdef FEAT_USR_CMDS
2788 /* Look for a user defined command as a last resort */
2789 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2790 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002791 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 while (ASCII_ISALNUM(*p))
2793 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002794 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 }
2796#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002797 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 eap->cmdidx = CMD_SIZE;
2799 }
2800
2801 return p;
2802}
2803
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002804#ifdef FEAT_USR_CMDS
2805/*
2806 * Search for a user command that matches "eap->cmd".
2807 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2808 * Return a pointer to just after the command.
2809 * Return NULL if there is no matching command.
2810 */
2811 static char_u *
2812find_ucmd(eap, p, full, xp, compl)
2813 exarg_T *eap;
2814 char_u *p; /* end of the command (possibly including count) */
2815 int *full; /* set to TRUE for a full match */
2816 expand_T *xp; /* used for completion, NULL otherwise */
2817 int *compl; /* completion flags or NULL */
2818{
2819 int len = (int)(p - eap->cmd);
2820 int j, k, matchlen = 0;
2821 ucmd_T *uc;
2822 int found = FALSE;
2823 int possible = FALSE;
2824 char_u *cp, *np; /* Point into typed cmd and test name */
2825 garray_T *gap;
2826 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2827 only full match global is accepted. */
2828
2829 /*
2830 * Look for buffer-local user commands first, then global ones.
2831 */
2832 gap = &curbuf->b_ucmds;
2833 for (;;)
2834 {
2835 for (j = 0; j < gap->ga_len; ++j)
2836 {
2837 uc = USER_CMD_GA(gap, j);
2838 cp = eap->cmd;
2839 np = uc->uc_name;
2840 k = 0;
2841 while (k < len && *np != NUL && *cp++ == *np++)
2842 k++;
2843 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2844 {
2845 /* If finding a second match, the command is ambiguous. But
2846 * not if a buffer-local command wasn't a full match and a
2847 * global command is a full match. */
2848 if (k == len && found && *np != NUL)
2849 {
2850 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002851 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002852 amb_local = TRUE;
2853 }
2854
2855 if (!found || (k == len && *np == NUL))
2856 {
2857 /* If we matched up to a digit, then there could
2858 * be another command including the digit that we
2859 * should use instead.
2860 */
2861 if (k == len)
2862 found = TRUE;
2863 else
2864 possible = TRUE;
2865
2866 if (gap == &ucmds)
2867 eap->cmdidx = CMD_USER;
2868 else
2869 eap->cmdidx = CMD_USER_BUF;
2870 eap->argt = uc->uc_argt;
2871 eap->useridx = j;
2872
2873# ifdef FEAT_CMDL_COMPL
2874 if (compl != NULL)
2875 *compl = uc->uc_compl;
2876# ifdef FEAT_EVAL
2877 if (xp != NULL)
2878 {
2879 xp->xp_arg = uc->uc_compl_arg;
2880 xp->xp_scriptID = uc->uc_scriptID;
2881 }
2882# endif
2883# endif
2884 /* Do not search for further abbreviations
2885 * if this is an exact match. */
2886 matchlen = k;
2887 if (k == len && *np == NUL)
2888 {
2889 if (full != NULL)
2890 *full = TRUE;
2891 amb_local = FALSE;
2892 break;
2893 }
2894 }
2895 }
2896 }
2897
2898 /* Stop if we found a full match or searched all. */
2899 if (j < gap->ga_len || gap == &ucmds)
2900 break;
2901 gap = &ucmds;
2902 }
2903
2904 /* Only found ambiguous matches. */
2905 if (amb_local)
2906 {
2907 if (xp != NULL)
2908 xp->xp_context = EXPAND_UNSUCCESSFUL;
2909 return NULL;
2910 }
2911
2912 /* The match we found may be followed immediately by a number. Move "p"
2913 * back to point to it. */
2914 if (found || possible)
2915 return p + (matchlen - len);
2916 return p;
2917}
2918#endif
2919
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920#if defined(FEAT_EVAL) || defined(PROTO)
2921/*
2922 * Return > 0 if an Ex command "name" exists.
2923 * Return 2 if there is an exact match.
2924 * Return 3 if there is an ambiguous match.
2925 */
2926 int
2927cmd_exists(name)
2928 char_u *name;
2929{
2930 exarg_T ea;
2931 int full = FALSE;
2932 int i;
2933 int j;
2934 static struct cmdmod
2935 {
2936 char *name;
2937 int minlen;
2938 } cmdmods[] = {
2939 {"aboveleft", 3},
2940 {"belowright", 3},
2941 {"botright", 2},
2942 {"browse", 3},
2943 {"confirm", 4},
2944 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002945 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 {"keepjumps", 5},
2947 {"keepmarks", 3},
2948 {"leftabove", 5},
2949 {"lockmarks", 3},
2950 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002951 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {"silent", 3},
2953 {"topleft", 2},
2954 {"verbose", 4},
2955 {"vertical", 4},
2956 };
2957
2958 /* Check command modifiers. */
2959 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2960 {
2961 for (j = 0; name[j] != NUL; ++j)
2962 if (name[j] != cmdmods[i].name[j])
2963 break;
2964 if (name[j] == NUL && j >= cmdmods[i].minlen)
2965 return (cmdmods[i].name[j] == NUL ? 2 : 1);
2966 }
2967
2968 /* Check built-in commands and user defined commands. */
2969 ea.cmd = name;
2970 ea.cmdidx = (cmdidx_T)0;
2971 if (find_command(&ea, &full) == NULL)
2972 return 3;
2973 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
2974}
2975#endif
2976
2977/*
2978 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2979 * we don't need/want deleted. Maybe this could be done better if we didn't
2980 * repeat all this stuff. The only problem is that they may not stay
2981 * perfectly compatible with each other, but then the command line syntax
2982 * probably won't change that much -- webb.
2983 */
2984 char_u *
2985set_one_cmd_context(xp, buff)
2986 expand_T *xp;
2987 char_u *buff; /* buffer for command string */
2988{
2989 char_u *p;
2990 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002991 int len = 0;
2992 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
2994 int compl = EXPAND_NOTHING;
2995#endif
2996#ifdef FEAT_CMDL_COMPL
2997 int delim;
2998#endif
2999 int forceit = FALSE;
3000 int usefilter = FALSE; /* filter instead of file name */
3001
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003002 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 xp->xp_pattern = buff;
3004 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003005 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
3007/*
3008 * 2. skip comment lines and leading space, colons or bars
3009 */
3010 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3011 ;
3012 xp->xp_pattern = cmd;
3013
3014 if (*cmd == NUL)
3015 return NULL;
3016 if (*cmd == '"') /* ignore comment lines */
3017 {
3018 xp->xp_context = EXPAND_NOTHING;
3019 return NULL;
3020 }
3021
3022/*
3023 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3024 */
3025 cmd = skip_range(cmd, &xp->xp_context);
3026
3027/*
3028 * 4. parse command
3029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 xp->xp_pattern = cmd;
3031 if (*cmd == NUL)
3032 return NULL;
3033 if (*cmd == '"')
3034 {
3035 xp->xp_context = EXPAND_NOTHING;
3036 return NULL;
3037 }
3038
3039 if (*cmd == '|' || *cmd == '\n')
3040 return cmd + 1; /* There's another command */
3041
3042 /*
3043 * Isolate the command and search for it in the command table.
3044 * Exceptions:
3045 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003046 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3048 */
3049 if (*cmd == 'k' && cmd[1] != 'e')
3050 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003051 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 p = cmd + 1;
3053 }
3054 else
3055 {
3056 p = cmd;
3057 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3058 ++p;
3059 /* check for non-alpha command */
3060 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3061 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003062 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003064 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 {
3066 xp->xp_context = EXPAND_UNSUCCESSFUL;
3067 return NULL;
3068 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003069 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3070 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3071 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 break;
3073
3074#ifdef FEAT_USR_CMDS
3075 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3076 {
3077 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3078 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003079 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 }
3081#endif
3082 }
3083
3084 /*
3085 * If the cursor is touching the command, and it ends in an alpha-numeric
3086 * character, complete the command name.
3087 */
3088 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3089 return NULL;
3090
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003091 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 {
3093 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3094 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003095 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 p = cmd + 1;
3097 }
3098#ifdef FEAT_USR_CMDS
3099 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3100 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003101 ea.cmd = cmd;
3102 p = find_ucmd(&ea, p, NULL, xp,
3103# if defined(FEAT_CMDL_COMPL)
3104 &compl
3105# else
3106 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003108 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003109 if (p == NULL)
3110 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 }
3112#endif
3113 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003114 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 {
3116 /* Not still touching the command and it was an illegal one */
3117 xp->xp_context = EXPAND_UNSUCCESSFUL;
3118 return NULL;
3119 }
3120
3121 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3122
3123 if (*p == '!') /* forced commands */
3124 {
3125 forceit = TRUE;
3126 ++p;
3127 }
3128
3129/*
3130 * 5. parse arguments
3131 */
3132#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003133 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003135 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136
3137 arg = skipwhite(p);
3138
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003139 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 {
3141 if (*arg == '>') /* append */
3142 {
3143 if (*++arg == '>')
3144 ++arg;
3145 arg = skipwhite(arg);
3146 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003147 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 {
3149 ++arg;
3150 usefilter = TRUE;
3151 }
3152 }
3153
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003154 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 {
3156 usefilter = forceit; /* :r! filter if forced */
3157 if (*arg == '!') /* :r !filter */
3158 {
3159 ++arg;
3160 usefilter = TRUE;
3161 }
3162 }
3163
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003164 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 {
3166 while (*arg == *cmd) /* allow any number of '>' or '<' */
3167 ++arg;
3168 arg = skipwhite(arg);
3169 }
3170
3171 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003172 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
3174 /* Check if we're in the +command */
3175 p = arg + 1;
3176 arg = skip_cmd_arg(arg, FALSE);
3177
3178 /* Still touching the command after '+'? */
3179 if (*arg == NUL)
3180 return p;
3181
3182 /* Skip space(s) after +command to get to the real argument */
3183 arg = skipwhite(arg);
3184 }
3185
3186 /*
3187 * Check for '|' to separate commands and '"' to start comments.
3188 * Don't do this for ":read !cmd" and ":write !cmd".
3189 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003190 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
3192 p = arg;
3193 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003194 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 p += 2;
3196 while (*p)
3197 {
3198 if (*p == Ctrl_V)
3199 {
3200 if (p[1] != NUL)
3201 ++p;
3202 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003203 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 || *p == '|' || *p == '\n')
3205 {
3206 if (*(p - 1) != '\\')
3207 {
3208 if (*p == '|' || *p == '\n')
3209 return p + 1;
3210 return NULL; /* It's a comment */
3211 }
3212 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003213 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 }
3215 }
3216
3217 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003218 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 vim_strchr((char_u *)"|\"", *arg) == NULL)
3220 return NULL;
3221
3222 /* Find start of last argument (argument just before cursor): */
3223 p = buff + STRLEN(buff);
3224 while (p != arg && *p != ' ' && *p != TAB)
3225 p--;
3226 if (*p == ' ' || *p == TAB)
3227 p++;
3228 xp->xp_pattern = p;
3229
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003230 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 {
3232 int in_quote = FALSE;
3233 char_u *bow = NULL; /* Beginning of word */
3234
3235 /*
3236 * Allow spaces within back-quotes to count as part of the argument
3237 * being expanded.
3238 */
3239 xp->xp_pattern = skipwhite(arg);
3240 for (p = xp->xp_pattern; *p; )
3241 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003242 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 ++p;
3244#ifdef SPACE_IN_FILENAME
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003245 else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246#else
3247 else if (vim_iswhite(*p))
3248#endif
3249 {
3250 p = skipwhite(p);
3251 if (in_quote)
3252 bow = p;
3253 else
3254 xp->xp_pattern = p;
3255 --p;
3256 }
3257 else if (*p == '`')
3258 {
3259 if (!in_quote)
3260 {
3261 xp->xp_pattern = p;
3262 bow = p + 1;
3263 }
3264 in_quote = !in_quote;
3265 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003266 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
3268
3269 /*
3270 * If we are still inside the quotes, and we passed a space, just
3271 * expand from there.
3272 */
3273 if (bow != NULL && in_quote)
3274 xp->xp_pattern = bow;
3275 xp->xp_context = EXPAND_FILES;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003276#ifndef BACKSLASH_IN_FILENAME
3277 /* For a shell command more chars need to be escaped. */
3278 if (usefilter || ea.cmdidx == CMD_bang)
3279 xp->xp_shell = TRUE;
3280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281
3282 /* Check for environment variable */
3283 if (*xp->xp_pattern == '$'
3284#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3285 || *xp->xp_pattern == '%'
3286#endif
3287 )
3288 {
3289 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3290 if (!vim_isIDc(*p))
3291 break;
3292 if (*p == NUL)
3293 {
3294 xp->xp_context = EXPAND_ENV_VARS;
3295 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003296#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3297 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003298 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003299 compl = EXPAND_ENV_VARS;
3300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
3302 }
3303 }
3304
3305/*
3306 * 6. switch on command name
3307 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003308 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
3310 case CMD_cd:
3311 case CMD_chdir:
3312 case CMD_lcd:
3313 case CMD_lchdir:
3314 if (xp->xp_context == EXPAND_FILES)
3315 xp->xp_context = EXPAND_DIRECTORIES;
3316 break;
3317 case CMD_help:
3318 xp->xp_context = EXPAND_HELP;
3319 xp->xp_pattern = arg;
3320 break;
3321
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003322 /* Command modifiers: return the argument.
3323 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003325 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 case CMD_belowright:
3327 case CMD_botright:
3328 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003329 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003331 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 case CMD_folddoclosed:
3333 case CMD_folddoopen:
3334 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003335 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 case CMD_keepjumps:
3337 case CMD_keepmarks:
3338 case CMD_leftabove:
3339 case CMD_lockmarks:
3340 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003341 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 case CMD_silent:
3343 case CMD_topleft:
3344 case CMD_verbose:
3345 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003346 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 return arg;
3348
3349#ifdef FEAT_SEARCH_EXTRA
3350 case CMD_match:
3351 if (*arg == NUL || !ends_excmd(*arg))
3352 {
3353 /* Dummy call to clear variables. */
3354 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3355 xp->xp_context = EXPAND_HIGHLIGHT;
3356 xp->xp_pattern = arg;
3357 arg = skipwhite(skiptowhite(arg));
3358 if (*arg != NUL)
3359 {
3360 xp->xp_context = EXPAND_NOTHING;
3361 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3362 }
3363 }
3364 return find_nextcmd(arg);
3365#endif
3366
3367#ifdef FEAT_CMDL_COMPL
3368/*
3369 * All completion for the +cmdline_compl feature goes here.
3370 */
3371
3372# ifdef FEAT_USR_CMDS
3373 case CMD_command:
3374 /* Check for attributes */
3375 while (*arg == '-')
3376 {
3377 arg++; /* Skip "-" */
3378 p = skiptowhite(arg);
3379 if (*p == NUL)
3380 {
3381 /* Cursor is still in the attribute */
3382 p = vim_strchr(arg, '=');
3383 if (p == NULL)
3384 {
3385 /* No "=", so complete attribute names */
3386 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3387 xp->xp_pattern = arg;
3388 return NULL;
3389 }
3390
3391 /* For the -complete and -nargs attributes, we complete
3392 * their arguments as well.
3393 */
3394 if (STRNICMP(arg, "complete", p - arg) == 0)
3395 {
3396 xp->xp_context = EXPAND_USER_COMPLETE;
3397 xp->xp_pattern = p + 1;
3398 return NULL;
3399 }
3400 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3401 {
3402 xp->xp_context = EXPAND_USER_NARGS;
3403 xp->xp_pattern = p + 1;
3404 return NULL;
3405 }
3406 return NULL;
3407 }
3408 arg = skipwhite(p);
3409 }
3410
3411 /* After the attributes comes the new command name */
3412 p = skiptowhite(arg);
3413 if (*p == NUL)
3414 {
3415 xp->xp_context = EXPAND_USER_COMMANDS;
3416 xp->xp_pattern = arg;
3417 break;
3418 }
3419
3420 /* And finally comes a normal command */
3421 return skipwhite(p);
3422
3423 case CMD_delcommand:
3424 xp->xp_context = EXPAND_USER_COMMANDS;
3425 xp->xp_pattern = arg;
3426 break;
3427# endif
3428
3429 case CMD_global:
3430 case CMD_vglobal:
3431 delim = *arg; /* get the delimiter */
3432 if (delim)
3433 ++arg; /* skip delimiter if there is one */
3434
3435 while (arg[0] != NUL && arg[0] != delim)
3436 {
3437 if (arg[0] == '\\' && arg[1] != NUL)
3438 ++arg;
3439 ++arg;
3440 }
3441 if (arg[0] != NUL)
3442 return arg + 1;
3443 break;
3444 case CMD_and:
3445 case CMD_substitute:
3446 delim = *arg;
3447 if (delim)
3448 {
3449 /* skip "from" part */
3450 ++arg;
3451 arg = skip_regexp(arg, delim, p_magic, NULL);
3452 }
3453 /* skip "to" part */
3454 while (arg[0] != NUL && arg[0] != delim)
3455 {
3456 if (arg[0] == '\\' && arg[1] != NUL)
3457 ++arg;
3458 ++arg;
3459 }
3460 if (arg[0] != NUL) /* skip delimiter */
3461 ++arg;
3462 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3463 ++arg;
3464 if (arg[0] != NUL)
3465 return arg;
3466 break;
3467 case CMD_isearch:
3468 case CMD_dsearch:
3469 case CMD_ilist:
3470 case CMD_dlist:
3471 case CMD_ijump:
3472 case CMD_psearch:
3473 case CMD_djump:
3474 case CMD_isplit:
3475 case CMD_dsplit:
3476 arg = skipwhite(skipdigits(arg)); /* skip count */
3477 if (*arg == '/') /* Match regexp, not just whole words */
3478 {
3479 for (++arg; *arg && *arg != '/'; arg++)
3480 if (*arg == '\\' && arg[1] != NUL)
3481 arg++;
3482 if (*arg)
3483 {
3484 arg = skipwhite(arg + 1);
3485
3486 /* Check for trailing illegal characters */
3487 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3488 xp->xp_context = EXPAND_NOTHING;
3489 else
3490 return arg;
3491 }
3492 }
3493 break;
3494#ifdef FEAT_AUTOCMD
3495 case CMD_autocmd:
3496 return set_context_in_autocmd(xp, arg, FALSE);
3497
3498 case CMD_doautocmd:
3499 return set_context_in_autocmd(xp, arg, TRUE);
3500#endif
3501 case CMD_set:
3502 set_context_in_set_cmd(xp, arg, 0);
3503 break;
3504 case CMD_setglobal:
3505 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3506 break;
3507 case CMD_setlocal:
3508 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3509 break;
3510 case CMD_tag:
3511 case CMD_stag:
3512 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003513 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 case CMD_tselect:
3515 case CMD_stselect:
3516 case CMD_ptselect:
3517 case CMD_tjump:
3518 case CMD_stjump:
3519 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003520 if (*p_wop != NUL)
3521 xp->xp_context = EXPAND_TAGS_LISTFILES;
3522 else
3523 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 xp->xp_pattern = arg;
3525 break;
3526 case CMD_augroup:
3527 xp->xp_context = EXPAND_AUGROUP;
3528 xp->xp_pattern = arg;
3529 break;
3530#ifdef FEAT_SYN_HL
3531 case CMD_syntax:
3532 set_context_in_syntax_cmd(xp, arg);
3533 break;
3534#endif
3535#ifdef FEAT_EVAL
3536 case CMD_let:
3537 case CMD_if:
3538 case CMD_elseif:
3539 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003540 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 case CMD_echo:
3542 case CMD_echon:
3543 case CMD_execute:
3544 case CMD_echomsg:
3545 case CMD_echoerr:
3546 case CMD_call:
3547 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003548 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 break;
3550
3551 case CMD_unlet:
3552 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3553 arg = xp->xp_pattern + 1;
3554 xp->xp_context = EXPAND_USER_VARS;
3555 xp->xp_pattern = arg;
3556 break;
3557
3558 case CMD_function:
3559 case CMD_delfunction:
3560 xp->xp_context = EXPAND_USER_FUNC;
3561 xp->xp_pattern = arg;
3562 break;
3563
3564 case CMD_echohl:
3565 xp->xp_context = EXPAND_HIGHLIGHT;
3566 xp->xp_pattern = arg;
3567 break;
3568#endif
3569 case CMD_highlight:
3570 set_context_in_highlight_cmd(xp, arg);
3571 break;
3572#ifdef FEAT_LISTCMDS
3573 case CMD_bdelete:
3574 case CMD_bwipeout:
3575 case CMD_bunload:
3576 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3577 arg = xp->xp_pattern + 1;
3578 /*FALLTHROUGH*/
3579 case CMD_buffer:
3580 case CMD_sbuffer:
3581 case CMD_checktime:
3582 xp->xp_context = EXPAND_BUFFERS;
3583 xp->xp_pattern = arg;
3584 break;
3585#endif
3586#ifdef FEAT_USR_CMDS
3587 case CMD_USER:
3588 case CMD_USER_BUF:
3589 if (compl != EXPAND_NOTHING)
3590 {
3591 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003592 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
3594# ifdef FEAT_MENU
3595 if (compl == EXPAND_MENUS)
3596 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3597# endif
3598 if (compl == EXPAND_COMMANDS)
3599 return arg;
3600 if (compl == EXPAND_MAPPINGS)
3601 return set_context_in_map_cmd(xp, (char_u *)"map",
3602 arg, forceit, FALSE, FALSE, CMD_map);
3603 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3604 arg = xp->xp_pattern + 1;
3605 xp->xp_pattern = arg;
3606 }
3607 xp->xp_context = compl;
3608 }
3609 break;
3610#endif
3611 case CMD_map: case CMD_noremap:
3612 case CMD_nmap: case CMD_nnoremap:
3613 case CMD_vmap: case CMD_vnoremap:
3614 case CMD_omap: case CMD_onoremap:
3615 case CMD_imap: case CMD_inoremap:
3616 case CMD_cmap: case CMD_cnoremap:
3617 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003618 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 case CMD_unmap:
3620 case CMD_nunmap:
3621 case CMD_vunmap:
3622 case CMD_ounmap:
3623 case CMD_iunmap:
3624 case CMD_cunmap:
3625 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003626 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 case CMD_abbreviate: case CMD_noreabbrev:
3628 case CMD_cabbrev: case CMD_cnoreabbrev:
3629 case CMD_iabbrev: case CMD_inoreabbrev:
3630 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003631 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 case CMD_unabbreviate:
3633 case CMD_cunabbrev:
3634 case CMD_iunabbrev:
3635 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003636 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637#ifdef FEAT_MENU
3638 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3639 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3640 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3641 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3642 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3643 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3644 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3645 case CMD_tmenu: case CMD_tunmenu:
3646 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3647 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3648#endif
3649
3650 case CMD_colorscheme:
3651 xp->xp_context = EXPAND_COLORS;
3652 xp->xp_pattern = arg;
3653 break;
3654
3655 case CMD_compiler:
3656 xp->xp_context = EXPAND_COMPILER;
3657 xp->xp_pattern = arg;
3658 break;
3659
3660#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3661 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3662 case CMD_language:
3663 if (*skiptowhite(arg) == NUL)
3664 {
3665 xp->xp_context = EXPAND_LANGUAGE;
3666 xp->xp_pattern = arg;
3667 }
3668 else
3669 xp->xp_context = EXPAND_NOTHING;
3670 break;
3671#endif
3672
3673#endif /* FEAT_CMDL_COMPL */
3674
3675 default:
3676 break;
3677 }
3678 return NULL;
3679}
3680
3681/*
3682 * skip a range specifier of the form: addr [,addr] [;addr] ..
3683 *
3684 * Backslashed delimiters after / or ? will be skipped, and commands will
3685 * not be expanded between /'s and ?'s or after "'".
3686 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003687 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 * Returns the "cmd" pointer advanced to beyond the range.
3689 */
3690 char_u *
3691skip_range(cmd, ctx)
3692 char_u *cmd;
3693 int *ctx; /* pointer to xp_context or NULL */
3694{
3695 int delim;
3696
Bram Moolenaardf177f62005-02-22 08:39:57 +00003697 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
3699 if (*cmd == '\'')
3700 {
3701 if (*++cmd == NUL && ctx != NULL)
3702 *ctx = EXPAND_NOTHING;
3703 }
3704 else if (*cmd == '/' || *cmd == '?')
3705 {
3706 delim = *cmd++;
3707 while (*cmd != NUL && *cmd != delim)
3708 if (*cmd++ == '\\' && *cmd != NUL)
3709 ++cmd;
3710 if (*cmd == NUL && ctx != NULL)
3711 *ctx = EXPAND_NOTHING;
3712 }
3713 if (*cmd != NUL)
3714 ++cmd;
3715 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003716
3717 /* Skip ":" and white space. */
3718 while (*cmd == ':')
3719 cmd = skipwhite(cmd + 1);
3720
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 return cmd;
3722}
3723
3724/*
3725 * get a single EX address
3726 *
3727 * Set ptr to the next character after the part that was interpreted.
3728 * Set ptr to NULL when an error is encountered.
3729 *
3730 * Return MAXLNUM when no Ex address was found.
3731 */
3732 static linenr_T
3733get_address(ptr, skip, to_other_file)
3734 char_u **ptr;
3735 int skip; /* only skip the address, don't use it */
3736 int to_other_file; /* flag: may jump to other file */
3737{
3738 int c;
3739 int i;
3740 long n;
3741 char_u *cmd;
3742 pos_T pos;
3743 pos_T *fp;
3744 linenr_T lnum;
3745
3746 cmd = skipwhite(*ptr);
3747 lnum = MAXLNUM;
3748 do
3749 {
3750 switch (*cmd)
3751 {
3752 case '.': /* '.' - Cursor position */
3753 ++cmd;
3754 lnum = curwin->w_cursor.lnum;
3755 break;
3756
3757 case '$': /* '$' - last line */
3758 ++cmd;
3759 lnum = curbuf->b_ml.ml_line_count;
3760 break;
3761
3762 case '\'': /* ''' - mark */
3763 if (*++cmd == NUL)
3764 {
3765 cmd = NULL;
3766 goto error;
3767 }
3768 if (skip)
3769 ++cmd;
3770 else
3771 {
3772 /* Only accept a mark in another file when it is
3773 * used by itself: ":'M". */
3774 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3775 ++cmd;
3776 if (fp == (pos_T *)-1)
3777 /* Jumped to another file. */
3778 lnum = curwin->w_cursor.lnum;
3779 else
3780 {
3781 if (check_mark(fp) == FAIL)
3782 {
3783 cmd = NULL;
3784 goto error;
3785 }
3786 lnum = fp->lnum;
3787 }
3788 }
3789 break;
3790
3791 case '/':
3792 case '?': /* '/' or '?' - search */
3793 c = *cmd++;
3794 if (skip) /* skip "/pat/" */
3795 {
3796 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3797 if (*cmd == c)
3798 ++cmd;
3799 }
3800 else
3801 {
3802 pos = curwin->w_cursor; /* save curwin->w_cursor */
3803 /*
3804 * When '/' or '?' follows another address, start
3805 * from there.
3806 */
3807 if (lnum != MAXLNUM)
3808 curwin->w_cursor.lnum = lnum;
3809 /*
3810 * Start a forward search at the end of the line.
3811 * Start a backward search at the start of the line.
3812 * This makes sure we never match in the current
3813 * line, and can match anywhere in the
3814 * next/previous line.
3815 */
3816 if (c == '/')
3817 curwin->w_cursor.col = MAXCOL;
3818 else
3819 curwin->w_cursor.col = 0;
3820 searchcmdlen = 0;
3821 if (!do_search(NULL, c, cmd, 1L,
3822 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3823 {
3824 curwin->w_cursor = pos;
3825 cmd = NULL;
3826 goto error;
3827 }
3828 lnum = curwin->w_cursor.lnum;
3829 curwin->w_cursor = pos;
3830 /* adjust command string pointer */
3831 cmd += searchcmdlen;
3832 }
3833 break;
3834
3835 case '\\': /* "\?", "\/" or "\&", repeat search */
3836 ++cmd;
3837 if (*cmd == '&')
3838 i = RE_SUBST;
3839 else if (*cmd == '?' || *cmd == '/')
3840 i = RE_SEARCH;
3841 else
3842 {
3843 EMSG(_(e_backslash));
3844 cmd = NULL;
3845 goto error;
3846 }
3847
3848 if (!skip)
3849 {
3850 /*
3851 * When search follows another address, start from
3852 * there.
3853 */
3854 if (lnum != MAXLNUM)
3855 pos.lnum = lnum;
3856 else
3857 pos.lnum = curwin->w_cursor.lnum;
3858
3859 /*
3860 * Start the search just like for the above
3861 * do_search().
3862 */
3863 if (*cmd != '?')
3864 pos.col = MAXCOL;
3865 else
3866 pos.col = 0;
3867 if (searchit(curwin, curbuf, &pos,
3868 *cmd == '?' ? BACKWARD : FORWARD,
3869 (char_u *)"", 1L,
3870 SEARCH_MSG + SEARCH_START, i) != FAIL)
3871 lnum = pos.lnum;
3872 else
3873 {
3874 cmd = NULL;
3875 goto error;
3876 }
3877 }
3878 ++cmd;
3879 break;
3880
3881 default:
3882 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3883 lnum = getdigits(&cmd);
3884 }
3885
3886 for (;;)
3887 {
3888 cmd = skipwhite(cmd);
3889 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3890 break;
3891
3892 if (lnum == MAXLNUM)
3893 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3894 if (VIM_ISDIGIT(*cmd))
3895 i = '+'; /* "number" is same as "+number" */
3896 else
3897 i = *cmd++;
3898 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3899 n = 1;
3900 else
3901 n = getdigits(&cmd);
3902 if (i == '-')
3903 lnum -= n;
3904 else
3905 lnum += n;
3906 }
3907 } while (*cmd == '/' || *cmd == '?');
3908
3909error:
3910 *ptr = cmd;
3911 return lnum;
3912}
3913
3914/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003915 * Get flags from an Ex command argument.
3916 */
3917 static void
3918get_flags(eap)
3919 exarg_T *eap;
3920{
3921 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3922 {
3923 if (*eap->arg == 'l')
3924 eap->flags |= EXFLAG_LIST;
3925 else if (*eap->arg == 'p')
3926 eap->flags |= EXFLAG_PRINT;
3927 else
3928 eap->flags |= EXFLAG_NR;
3929 eap->arg = skipwhite(eap->arg + 1);
3930 }
3931}
3932
3933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 * Function called for command which is Not Implemented. NI!
3935 */
3936 void
3937ex_ni(eap)
3938 exarg_T *eap;
3939{
3940 if (!eap->skip)
3941 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3942}
3943
3944#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003945 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946/*
3947 * Function called for script command which is Not Implemented. NI!
3948 * Skips over ":perl <<EOF" constructs.
3949 */
3950 static void
3951ex_script_ni(eap)
3952 exarg_T *eap;
3953{
3954 if (!eap->skip)
3955 ex_ni(eap);
3956 else
3957 vim_free(script_get(eap, eap->arg));
3958}
3959#endif
3960
3961/*
3962 * Check range in Ex command for validity.
3963 * Return NULL when valid, error message when invalid.
3964 */
3965 static char_u *
3966invalid_range(eap)
3967 exarg_T *eap;
3968{
3969 if ( eap->line1 < 0
3970 || eap->line2 < 0
3971 || eap->line1 > eap->line2
3972 || ((eap->argt & RANGE)
3973 && !(eap->argt & NOTADR)
3974 && eap->line2 > curbuf->b_ml.ml_line_count
3975#ifdef FEAT_DIFF
3976 + (eap->cmdidx == CMD_diffget)
3977#endif
3978 ))
3979 return (char_u *)_(e_invrange);
3980 return NULL;
3981}
3982
3983/*
3984 * Correct the range for zero line number, if required.
3985 */
3986 static void
3987correct_range(eap)
3988 exarg_T *eap;
3989{
3990 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
3991 {
3992 if (eap->line1 == 0)
3993 eap->line1 = 1;
3994 if (eap->line2 == 0)
3995 eap->line2 = 1;
3996 }
3997}
3998
Bram Moolenaar748bf032005-02-02 23:04:36 +00003999#ifdef FEAT_QUICKFIX
4000static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4001
4002/*
4003 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4004 * pattern. Otherwise return eap->arg.
4005 */
4006 static char_u *
4007skip_grep_pat(eap)
4008 exarg_T *eap;
4009{
4010 char_u *p = eap->arg;
4011
Bram Moolenaara37420f2006-02-04 22:37:47 +00004012 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4013 || eap->cmdidx == CMD_vimgrepadd
4014 || eap->cmdidx == CMD_lvimgrepadd
4015 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004016 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004017 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004018 if (p == NULL)
4019 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004020 }
4021 return p;
4022}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004023
4024/*
4025 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4026 * in the command line, so that things like % get expanded.
4027 */
4028 static char_u *
4029replace_makeprg(eap, p, cmdlinep)
4030 exarg_T *eap;
4031 char_u *p;
4032 char_u **cmdlinep;
4033{
4034 char_u *new_cmdline;
4035 char_u *program;
4036 char_u *pos;
4037 char_u *ptr;
4038 int len;
4039 int i;
4040
4041 /*
4042 * Don't do it when ":vimgrep" is used for ":grep".
4043 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004044 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4045 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4046 || eap->cmdidx == CMD_grepadd
4047 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004048 && !grep_internal(eap->cmdidx))
4049 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004050 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4051 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004052 {
4053 if (*curbuf->b_p_gp == NUL)
4054 program = p_gp;
4055 else
4056 program = curbuf->b_p_gp;
4057 }
4058 else
4059 {
4060 if (*curbuf->b_p_mp == NUL)
4061 program = p_mp;
4062 else
4063 program = curbuf->b_p_mp;
4064 }
4065
4066 p = skipwhite(p);
4067
4068 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4069 {
4070 /* replace $* by given arguments */
4071 i = 1;
4072 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4073 ++i;
4074 len = (int)STRLEN(p);
4075 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4076 if (new_cmdline == NULL)
4077 return NULL; /* out of memory */
4078 ptr = new_cmdline;
4079 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4080 {
4081 i = (int)(pos - program);
4082 STRNCPY(ptr, program, i);
4083 STRCPY(ptr += i, p);
4084 ptr += len;
4085 program = pos + 2;
4086 }
4087 STRCPY(ptr, program);
4088 }
4089 else
4090 {
4091 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4092 if (new_cmdline == NULL)
4093 return NULL; /* out of memory */
4094 STRCPY(new_cmdline, program);
4095 STRCAT(new_cmdline, " ");
4096 STRCAT(new_cmdline, p);
4097 }
4098 msg_make(p);
4099
4100 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4101 vim_free(*cmdlinep);
4102 *cmdlinep = new_cmdline;
4103 p = new_cmdline;
4104 }
4105 return p;
4106}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004107#endif
4108
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109/*
4110 * Expand file name in Ex command argument.
4111 * Return FAIL for failure, OK otherwise.
4112 */
4113 int
4114expand_filename(eap, cmdlinep, errormsgp)
4115 exarg_T *eap;
4116 char_u **cmdlinep;
4117 char_u **errormsgp;
4118{
4119 int has_wildcards; /* need to expand wildcards */
4120 char_u *repl;
4121 int srclen;
4122 char_u *p;
4123 int n;
4124
Bram Moolenaar748bf032005-02-02 23:04:36 +00004125#ifdef FEAT_QUICKFIX
4126 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4127 p = skip_grep_pat(eap);
4128#else
4129 p = eap->arg;
4130#endif
4131
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 /*
4133 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4134 * the file name contains a wildcard it should not cause expanding.
4135 * (it will be expanded anyway if there is a wildcard before replacing).
4136 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004137 has_wildcards = mch_has_wildcard(p);
4138 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004140#ifdef FEAT_EVAL
4141 /* Skip over `=expr`, wildcards in it are not expanded. */
4142 if (p[0] == '`' && p[1] == '=')
4143 {
4144 p += 2;
4145 (void)skip_expr(&p);
4146 if (*p == '`')
4147 ++p;
4148 continue;
4149 }
4150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 /*
4152 * Quick check if this cannot be the start of a special string.
4153 * Also removes backslash before '%', '#' and '<'.
4154 */
4155 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4156 {
4157 ++p;
4158 continue;
4159 }
4160
4161 /*
4162 * Try to find a match at this position.
4163 */
4164 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4165 if (*errormsgp != NULL) /* error detected */
4166 return FAIL;
4167 if (repl == NULL) /* no match found */
4168 {
4169 p += srclen;
4170 continue;
4171 }
4172
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004173 /* Wildcards won't be expanded below, the replacement is taken
4174 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4175 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4176 {
4177 char_u *l = repl;
4178
4179 repl = expand_env_save(repl);
4180 vim_free(l);
4181 }
4182
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 /* Need to escape white space et al. with a backslash. Don't do this
4184 * for shell commands (may have to use quotes instead). Don't do this
4185 * for non-unix systems when there is a single argument (spaces don't
4186 * separate arguments then). */
4187 if (!eap->usefilter
4188 && eap->cmdidx != CMD_bang
4189 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004190 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004192 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004194 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195#ifndef UNIX
4196 && !(eap->argt & NOSPC)
4197#endif
4198 )
4199 {
4200 char_u *l;
4201#ifdef BACKSLASH_IN_FILENAME
4202 /* Don't escape a backslash here, because rem_backslash() doesn't
4203 * remove it later. */
4204 static char_u *nobslash = (char_u *)" \t\"|";
4205# define ESCAPE_CHARS nobslash
4206#else
4207# define ESCAPE_CHARS escape_chars
4208#endif
4209
4210 for (l = repl; *l; ++l)
4211 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4212 {
4213 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4214 if (l != NULL)
4215 {
4216 vim_free(repl);
4217 repl = l;
4218 }
4219 break;
4220 }
4221 }
4222
4223 /* For a shell command a '!' must be escaped. */
4224 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004225 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
4227 char_u *l;
4228
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004229 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 if (l != NULL)
4231 {
4232 vim_free(repl);
4233 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004234 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 if (strstr((char *)p_sh, "sh") != NULL)
4236 {
4237 l = vim_strsave_escaped(repl, (char_u *)"!");
4238 if (l != NULL)
4239 {
4240 vim_free(repl);
4241 repl = l;
4242 }
4243 }
4244 }
4245 }
4246
4247 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4248 vim_free(repl);
4249 if (p == NULL)
4250 return FAIL;
4251 }
4252
4253 /*
4254 * One file argument: Expand wildcards.
4255 * Don't do this with ":r !command" or ":w !command".
4256 */
4257 if ((eap->argt & NOSPC) && !eap->usefilter)
4258 {
4259 /*
4260 * May do this twice:
4261 * 1. Replace environment variables.
4262 * 2. Replace any other wildcards, remove backslashes.
4263 */
4264 for (n = 1; n <= 2; ++n)
4265 {
4266 if (n == 2)
4267 {
4268#ifdef UNIX
4269 /*
4270 * Only for Unix we check for more than one file name.
4271 * For other systems spaces are considered to be part
4272 * of the file name.
4273 * Only check here if there is no wildcard, otherwise
4274 * ExpandOne() will check for errors. This allows
4275 * ":e `ls ve*.c`" on Unix.
4276 */
4277 if (!has_wildcards)
4278 for (p = eap->arg; *p; ++p)
4279 {
4280 /* skip escaped characters */
4281 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4282 ++p;
4283 else if (vim_iswhite(*p))
4284 {
4285 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4286 return FAIL;
4287 }
4288 }
4289#endif
4290
4291 /*
4292 * Halve the number of backslashes (this is Vi compatible).
4293 * For Unix and OS/2, when wildcards are expanded, this is
4294 * done by ExpandOne() below.
4295 */
4296#if defined(UNIX) || defined(OS2)
4297 if (!has_wildcards)
4298#endif
4299 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301
4302 if (has_wildcards)
4303 {
4304 if (n == 1)
4305 {
4306 /*
4307 * First loop: May expand environment variables. This
4308 * can be done much faster with expand_env() than with
4309 * something else (e.g., calling a shell).
4310 * After expanding environment variables, check again
4311 * if there are still wildcards present.
4312 */
4313 if (vim_strchr(eap->arg, '$') != NULL
4314 || vim_strchr(eap->arg, '~') != NULL)
4315 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004316 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4317 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 has_wildcards = mch_has_wildcard(NameBuff);
4319 p = NameBuff;
4320 }
4321 else
4322 p = NULL;
4323 }
4324 else /* n == 2 */
4325 {
4326 expand_T xpc;
4327
4328 ExpandInit(&xpc);
4329 xpc.xp_context = EXPAND_FILES;
4330 p = ExpandOne(&xpc, eap->arg, NULL,
4331 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4332 WILD_EXPAND_FREE);
4333 ExpandCleanup(&xpc);
4334 if (p == NULL)
4335 return FAIL;
4336 }
4337 if (p != NULL)
4338 {
4339 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4340 p, cmdlinep);
4341 if (n == 2) /* p came from ExpandOne() */
4342 vim_free(p);
4343 }
4344 }
4345 }
4346 }
4347 return OK;
4348}
4349
4350/*
4351 * Replace part of the command line, keeping eap->cmd, eap->arg and
4352 * eap->nextcmd correct.
4353 * "src" points to the part that is to be replaced, of length "srclen".
4354 * "repl" is the replacement string.
4355 * Returns a pointer to the character after the replaced string.
4356 * Returns NULL for failure.
4357 */
4358 static char_u *
4359repl_cmdline(eap, src, srclen, repl, cmdlinep)
4360 exarg_T *eap;
4361 char_u *src;
4362 int srclen;
4363 char_u *repl;
4364 char_u **cmdlinep;
4365{
4366 int len;
4367 int i;
4368 char_u *new_cmdline;
4369
4370 /*
4371 * The new command line is build in new_cmdline[].
4372 * First allocate it.
4373 * Careful: a "+cmd" argument may have been NUL terminated.
4374 */
4375 len = (int)STRLEN(repl);
4376 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
4377 if (eap->nextcmd)
4378 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4379 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4380 return NULL; /* out of memory! */
4381
4382 /*
4383 * Copy the stuff before the expanded part.
4384 * Copy the expanded stuff.
4385 * Copy what came after the expanded part.
4386 * Copy the next commands, if there are any.
4387 */
4388 i = (int)(src - *cmdlinep); /* length of part before match */
4389 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004390
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 mch_memmove(new_cmdline + i, repl, (size_t)len);
4392 i += len; /* remember the end of the string */
4393 STRCPY(new_cmdline + i, src + srclen);
4394 src = new_cmdline + i; /* remember where to continue */
4395
4396 if (eap->nextcmd) /* append next command */
4397 {
4398 i = (int)STRLEN(new_cmdline) + 1;
4399 STRCPY(new_cmdline + i, eap->nextcmd);
4400 eap->nextcmd = new_cmdline + i;
4401 }
4402 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4403 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4404 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4405 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4406 vim_free(*cmdlinep);
4407 *cmdlinep = new_cmdline;
4408
4409 return src;
4410}
4411
4412/*
4413 * Check for '|' to separate commands and '"' to start comments.
4414 */
4415 void
4416separate_nextcmd(eap)
4417 exarg_T *eap;
4418{
4419 char_u *p;
4420
Bram Moolenaar86b68352004-12-27 21:59:20 +00004421#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004422 p = skip_grep_pat(eap);
4423#else
4424 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004425#endif
4426
4427 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 {
4429 if (*p == Ctrl_V)
4430 {
4431 if (eap->argt & (USECTRLV | XFILE))
4432 ++p; /* skip CTRL-V and next char */
4433 else
4434 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4435 if (*p == NUL) /* stop at NUL after CTRL-V */
4436 break;
4437 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004438
4439#ifdef FEAT_EVAL
4440 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004441 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004442 {
4443 p += 2;
4444 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004445 }
4446#endif
4447
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 /* Check for '"': start of comment or '|': next command */
4449 /* :@" and :*" do not start a comment!
4450 * :redir @" doesn't either. */
4451 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4452 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4453 || p != eap->arg)
4454 && (eap->cmdidx != CMD_redir
4455 || p != eap->arg + 1 || p[-1] != '@'))
4456 || *p == '|' || *p == '\n')
4457 {
4458 /*
4459 * We remove the '\' before the '|', unless USECTRLV is used
4460 * AND 'b' is present in 'cpoptions'.
4461 */
4462 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4463 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4464 {
4465 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4466 --p;
4467 }
4468 else
4469 {
4470 eap->nextcmd = check_nextcmd(p);
4471 *p = NUL;
4472 break;
4473 }
4474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004476
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4478 del_trailing_spaces(eap->arg);
4479}
4480
4481/*
4482 * get + command from ex argument
4483 */
4484 static char_u *
4485getargcmd(argp)
4486 char_u **argp;
4487{
4488 char_u *arg = *argp;
4489 char_u *command = NULL;
4490
4491 if (*arg == '+') /* +[command] */
4492 {
4493 ++arg;
4494 if (vim_isspace(*arg))
4495 command = dollar_command;
4496 else
4497 {
4498 command = arg;
4499 arg = skip_cmd_arg(command, TRUE);
4500 if (*arg != NUL)
4501 *arg++ = NUL; /* terminate command with NUL */
4502 }
4503
4504 arg = skipwhite(arg); /* skip over spaces */
4505 *argp = arg;
4506 }
4507 return command;
4508}
4509
4510/*
4511 * Find end of "+command" argument. Skip over "\ " and "\\".
4512 */
4513 static char_u *
4514skip_cmd_arg(p, rembs)
4515 char_u *p;
4516 int rembs; /* TRUE to halve the number of backslashes */
4517{
4518 while (*p && !vim_isspace(*p))
4519 {
4520 if (*p == '\\' && p[1] != NUL)
4521 {
4522 if (rembs)
4523 mch_memmove(p, p + 1, STRLEN(p));
4524 else
4525 ++p;
4526 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004527 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 }
4529 return p;
4530}
4531
4532/*
4533 * Get "++opt=arg" argument.
4534 * Return FAIL or OK.
4535 */
4536 static int
4537getargopt(eap)
4538 exarg_T *eap;
4539{
4540 char_u *arg = eap->arg + 2;
4541 int *pp = NULL;
4542#ifdef FEAT_MBYTE
4543 char_u *p;
4544#endif
4545
4546 /* ":edit ++[no]bin[ary] file" */
4547 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4548 {
4549 if (*arg == 'n')
4550 {
4551 arg += 2;
4552 eap->force_bin = FORCE_NOBIN;
4553 }
4554 else
4555 eap->force_bin = FORCE_BIN;
4556 if (!checkforcmd(&arg, "binary", 3))
4557 return FAIL;
4558 eap->arg = skipwhite(arg);
4559 return OK;
4560 }
4561
4562 if (STRNCMP(arg, "ff", 2) == 0)
4563 {
4564 arg += 2;
4565 pp = &eap->force_ff;
4566 }
4567 else if (STRNCMP(arg, "fileformat", 10) == 0)
4568 {
4569 arg += 10;
4570 pp = &eap->force_ff;
4571 }
4572#ifdef FEAT_MBYTE
4573 else if (STRNCMP(arg, "enc", 3) == 0)
4574 {
4575 arg += 3;
4576 pp = &eap->force_enc;
4577 }
4578 else if (STRNCMP(arg, "encoding", 8) == 0)
4579 {
4580 arg += 8;
4581 pp = &eap->force_enc;
4582 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004583 else if (STRNCMP(arg, "bad", 3) == 0)
4584 {
4585 arg += 3;
4586 pp = &eap->bad_char;
4587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588#endif
4589
4590 if (pp == NULL || *arg != '=')
4591 return FAIL;
4592
4593 ++arg;
4594 *pp = (int)(arg - eap->cmd);
4595 arg = skip_cmd_arg(arg, FALSE);
4596 eap->arg = skipwhite(arg);
4597 *arg = NUL;
4598
4599#ifdef FEAT_MBYTE
4600 if (pp == &eap->force_ff)
4601 {
4602#endif
4603 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4604 return FAIL;
4605#ifdef FEAT_MBYTE
4606 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004607 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {
4609 /* Make 'fileencoding' lower case. */
4610 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4611 *p = TOLOWER_ASC(*p);
4612 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004613 else
4614 {
4615 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4616 * "drop". */
4617 p = eap->cmd + eap->bad_char;
4618 if (STRICMP(p, "keep") == 0)
4619 eap->bad_char = BAD_KEEP;
4620 else if (STRICMP(p, "drop") == 0)
4621 eap->bad_char = BAD_DROP;
4622 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4623 eap->bad_char = *p;
4624 else
4625 return FAIL;
4626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627#endif
4628
4629 return OK;
4630}
4631
4632/*
4633 * ":abbreviate" and friends.
4634 */
4635 static void
4636ex_abbreviate(eap)
4637 exarg_T *eap;
4638{
4639 do_exmap(eap, TRUE); /* almost the same as mapping */
4640}
4641
4642/*
4643 * ":map" and friends.
4644 */
4645 static void
4646ex_map(eap)
4647 exarg_T *eap;
4648{
4649 /*
4650 * If we are sourcing .exrc or .vimrc in current directory we
4651 * print the mappings for security reasons.
4652 */
4653 if (secure)
4654 {
4655 secure = 2;
4656 msg_outtrans(eap->cmd);
4657 msg_putchar('\n');
4658 }
4659 do_exmap(eap, FALSE);
4660}
4661
4662/*
4663 * ":unmap" and friends.
4664 */
4665 static void
4666ex_unmap(eap)
4667 exarg_T *eap;
4668{
4669 do_exmap(eap, FALSE);
4670}
4671
4672/*
4673 * ":mapclear" and friends.
4674 */
4675 static void
4676ex_mapclear(eap)
4677 exarg_T *eap;
4678{
4679 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4680}
4681
4682/*
4683 * ":abclear" and friends.
4684 */
4685 static void
4686ex_abclear(eap)
4687 exarg_T *eap;
4688{
4689 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4690}
4691
4692#ifdef FEAT_AUTOCMD
4693 static void
4694ex_autocmd(eap)
4695 exarg_T *eap;
4696{
4697 /*
4698 * Disallow auto commands from .exrc and .vimrc in current
4699 * directory for security reasons.
4700 */
4701 if (secure)
4702 {
4703 secure = 2;
4704 eap->errmsg = e_curdir;
4705 }
4706 else if (eap->cmdidx == CMD_autocmd)
4707 do_autocmd(eap->arg, eap->forceit);
4708 else
4709 do_augroup(eap->arg, eap->forceit);
4710}
4711
4712/*
4713 * ":doautocmd": Apply the automatic commands to the current buffer.
4714 */
4715 static void
4716ex_doautocmd(eap)
4717 exarg_T *eap;
4718{
4719 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004720 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721}
4722#endif
4723
4724#ifdef FEAT_LISTCMDS
4725/*
4726 * :[N]bunload[!] [N] [bufname] unload buffer
4727 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4728 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4729 */
4730 static void
4731ex_bunload(eap)
4732 exarg_T *eap;
4733{
4734 eap->errmsg = do_bufdel(
4735 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4736 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4737 : DOBUF_UNLOAD, eap->arg,
4738 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4739}
4740
4741/*
4742 * :[N]buffer [N] to buffer N
4743 * :[N]sbuffer [N] to buffer N
4744 */
4745 static void
4746ex_buffer(eap)
4747 exarg_T *eap;
4748{
4749 if (*eap->arg)
4750 eap->errmsg = e_trailing;
4751 else
4752 {
4753 if (eap->addr_count == 0) /* default is current buffer */
4754 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4755 else
4756 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4757 }
4758}
4759
4760/*
4761 * :[N]bmodified [N] to next mod. buffer
4762 * :[N]sbmodified [N] to next mod. buffer
4763 */
4764 static void
4765ex_bmodified(eap)
4766 exarg_T *eap;
4767{
4768 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4769}
4770
4771/*
4772 * :[N]bnext [N] to next buffer
4773 * :[N]sbnext [N] split and to next buffer
4774 */
4775 static void
4776ex_bnext(eap)
4777 exarg_T *eap;
4778{
4779 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4780}
4781
4782/*
4783 * :[N]bNext [N] to previous buffer
4784 * :[N]bprevious [N] to previous buffer
4785 * :[N]sbNext [N] split and to previous buffer
4786 * :[N]sbprevious [N] split and to previous buffer
4787 */
4788 static void
4789ex_bprevious(eap)
4790 exarg_T *eap;
4791{
4792 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4793}
4794
4795/*
4796 * :brewind to first buffer
4797 * :bfirst to first buffer
4798 * :sbrewind split and to first buffer
4799 * :sbfirst split and to first buffer
4800 */
4801 static void
4802ex_brewind(eap)
4803 exarg_T *eap;
4804{
4805 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4806}
4807
4808/*
4809 * :blast to last buffer
4810 * :sblast split and to last buffer
4811 */
4812 static void
4813ex_blast(eap)
4814 exarg_T *eap;
4815{
4816 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4817}
4818#endif
4819
4820 int
4821ends_excmd(c)
4822 int c;
4823{
4824 return (c == NUL || c == '|' || c == '"' || c == '\n');
4825}
4826
4827#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4828 || defined(PROTO)
4829/*
4830 * Return the next command, after the first '|' or '\n'.
4831 * Return NULL if not found.
4832 */
4833 char_u *
4834find_nextcmd(p)
4835 char_u *p;
4836{
4837 while (*p != '|' && *p != '\n')
4838 {
4839 if (*p == NUL)
4840 return NULL;
4841 ++p;
4842 }
4843 return (p + 1);
4844}
4845#endif
4846
4847/*
4848 * Check if *p is a separator between Ex commands.
4849 * Return NULL if it isn't, (p + 1) if it is.
4850 */
4851 char_u *
4852check_nextcmd(p)
4853 char_u *p;
4854{
4855 p = skipwhite(p);
4856 if (*p == '|' || *p == '\n')
4857 return (p + 1);
4858 else
4859 return NULL;
4860}
4861
4862/*
4863 * - if there are more files to edit
4864 * - and this is the last window
4865 * - and forceit not used
4866 * - and not repeated twice on a row
4867 * return FAIL and give error message if 'message' TRUE
4868 * return OK otherwise
4869 */
4870 static int
4871check_more(message, forceit)
4872 int message; /* when FALSE check only, no messages */
4873 int forceit;
4874{
4875 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4876
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004877 if (!forceit && only_one_window()
4878 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
4880 if (message)
4881 {
4882#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4883 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4884 {
4885 char_u buff[IOSIZE];
4886
4887 if (n == 1)
4888 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4889 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004890 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 _("%d more files to edit. Quit anyway?"), n);
4892 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4893 return OK;
4894 return FAIL;
4895 }
4896#endif
4897 if (n == 1)
4898 EMSG(_("E173: 1 more file to edit"));
4899 else
4900 EMSGN(_("E173: %ld more files to edit"), n);
4901 quitmore = 2; /* next try to quit is allowed */
4902 }
4903 return FAIL;
4904 }
4905 return OK;
4906}
4907
4908#ifdef FEAT_CMDL_COMPL
4909/*
4910 * Function given to ExpandGeneric() to obtain the list of command names.
4911 */
4912/*ARGSUSED*/
4913 char_u *
4914get_command_name(xp, idx)
4915 expand_T *xp;
4916 int idx;
4917{
4918 if (idx >= (int)CMD_SIZE)
4919# ifdef FEAT_USR_CMDS
4920 return get_user_command_name(idx);
4921# else
4922 return NULL;
4923# endif
4924 return cmdnames[idx].cmd_name;
4925}
4926#endif
4927
4928#if defined(FEAT_USR_CMDS) || defined(PROTO)
4929static 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));
4930static void uc_list __ARGS((char_u *name, size_t name_len));
4931static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4932static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4933static 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));
4934
4935 static int
4936uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4937 char_u *name;
4938 size_t name_len;
4939 char_u *rep;
4940 long argt;
4941 long def;
4942 int flags;
4943 int compl;
4944 char_u *compl_arg;
4945 int force;
4946{
4947 ucmd_T *cmd = NULL;
4948 char_u *p;
4949 int i;
4950 int cmp = 1;
4951 char_u *rep_buf = NULL;
4952 garray_T *gap;
4953
4954 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4955 if (rep_buf == NULL)
4956 {
4957 /* Can't replace termcodes - try using the string as is */
4958 rep_buf = vim_strsave(rep);
4959
4960 /* Give up if out of memory */
4961 if (rep_buf == NULL)
4962 return FAIL;
4963 }
4964
4965 /* get address of growarray: global or in curbuf */
4966 if (flags & UC_BUFFER)
4967 {
4968 gap = &curbuf->b_ucmds;
4969 if (gap->ga_itemsize == 0)
4970 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4971 }
4972 else
4973 gap = &ucmds;
4974
4975 /* Search for the command in the already defined commands. */
4976 for (i = 0; i < gap->ga_len; ++i)
4977 {
4978 size_t len;
4979
4980 cmd = USER_CMD_GA(gap, i);
4981 len = STRLEN(cmd->uc_name);
4982 cmp = STRNCMP(name, cmd->uc_name, name_len);
4983 if (cmp == 0)
4984 {
4985 if (name_len < len)
4986 cmp = -1;
4987 else if (name_len > len)
4988 cmp = 1;
4989 }
4990
4991 if (cmp == 0)
4992 {
4993 if (!force)
4994 {
4995 EMSG(_("E174: Command already exists: add ! to replace it"));
4996 goto fail;
4997 }
4998
4999 vim_free(cmd->uc_rep);
5000 cmd->uc_rep = 0;
5001 break;
5002 }
5003
5004 /* Stop as soon as we pass the name to add */
5005 if (cmp < 0)
5006 break;
5007 }
5008
5009 /* Extend the array unless we're replacing an existing command */
5010 if (cmp != 0)
5011 {
5012 if (ga_grow(gap, 1) != OK)
5013 goto fail;
5014 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5015 goto fail;
5016
5017 cmd = USER_CMD_GA(gap, i);
5018 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5019
5020 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021
5022 cmd->uc_name = p;
5023 }
5024
5025 cmd->uc_rep = rep_buf;
5026 cmd->uc_argt = argt;
5027 cmd->uc_def = def;
5028 cmd->uc_compl = compl;
5029#ifdef FEAT_EVAL
5030 cmd->uc_scriptID = current_SID;
5031# ifdef FEAT_CMDL_COMPL
5032 cmd->uc_compl_arg = compl_arg;
5033# endif
5034#endif
5035
5036 return OK;
5037
5038fail:
5039 vim_free(rep_buf);
5040#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5041 vim_free(compl_arg);
5042#endif
5043 return FAIL;
5044}
5045
5046/*
5047 * List of names for completion for ":command" with the EXPAND_ flag.
5048 * Must be alphabetical for completion.
5049 */
5050static struct
5051{
5052 int expand;
5053 char *name;
5054} command_complete[] =
5055{
5056 {EXPAND_AUGROUP, "augroup"},
5057 {EXPAND_BUFFERS, "buffer"},
5058 {EXPAND_COMMANDS, "command"},
5059#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5060 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005061 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062#endif
5063 {EXPAND_DIRECTORIES, "dir"},
5064 {EXPAND_ENV_VARS, "environment"},
5065 {EXPAND_EVENTS, "event"},
5066 {EXPAND_EXPRESSION, "expression"},
5067 {EXPAND_FILES, "file"},
5068 {EXPAND_FUNCTIONS, "function"},
5069 {EXPAND_HELP, "help"},
5070 {EXPAND_HIGHLIGHT, "highlight"},
5071 {EXPAND_MAPPINGS, "mapping"},
5072 {EXPAND_MENUS, "menu"},
5073 {EXPAND_SETTINGS, "option"},
5074 {EXPAND_TAGS, "tag"},
5075 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5076 {EXPAND_USER_VARS, "var"},
5077 {0, NULL}
5078};
5079
5080 static void
5081uc_list(name, name_len)
5082 char_u *name;
5083 size_t name_len;
5084{
5085 int i, j;
5086 int found = FALSE;
5087 ucmd_T *cmd;
5088 int len;
5089 long a;
5090 garray_T *gap;
5091
5092 gap = &curbuf->b_ucmds;
5093 for (;;)
5094 {
5095 for (i = 0; i < gap->ga_len; ++i)
5096 {
5097 cmd = USER_CMD_GA(gap, i);
5098 a = cmd->uc_argt;
5099
5100 /* Skip commands which don't match the requested prefix */
5101 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5102 continue;
5103
5104 /* Put out the title first time */
5105 if (!found)
5106 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5107 found = TRUE;
5108 msg_putchar('\n');
5109 if (got_int)
5110 break;
5111
5112 /* Special cases */
5113 msg_putchar(a & BANG ? '!' : ' ');
5114 msg_putchar(a & REGSTR ? '"' : ' ');
5115 msg_putchar(gap != &ucmds ? 'b' : ' ');
5116 msg_putchar(' ');
5117
5118 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5119 len = (int)STRLEN(cmd->uc_name) + 4;
5120
5121 do {
5122 msg_putchar(' ');
5123 ++len;
5124 } while (len < 16);
5125
5126 len = 0;
5127
5128 /* Arguments */
5129 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5130 {
5131 case 0: IObuff[len++] = '0'; break;
5132 case (EXTRA): IObuff[len++] = '*'; break;
5133 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5134 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5135 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5136 }
5137
5138 do {
5139 IObuff[len++] = ' ';
5140 } while (len < 5);
5141
5142 /* Range */
5143 if (a & (RANGE|COUNT))
5144 {
5145 if (a & COUNT)
5146 {
5147 /* -count=N */
5148 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5149 len += (int)STRLEN(IObuff + len);
5150 }
5151 else if (a & DFLALL)
5152 IObuff[len++] = '%';
5153 else if (cmd->uc_def >= 0)
5154 {
5155 /* -range=N */
5156 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5157 len += (int)STRLEN(IObuff + len);
5158 }
5159 else
5160 IObuff[len++] = '.';
5161 }
5162
5163 do {
5164 IObuff[len++] = ' ';
5165 } while (len < 11);
5166
5167 /* Completion */
5168 for (j = 0; command_complete[j].expand != 0; ++j)
5169 if (command_complete[j].expand == cmd->uc_compl)
5170 {
5171 STRCPY(IObuff + len, command_complete[j].name);
5172 len += (int)STRLEN(IObuff + len);
5173 break;
5174 }
5175
5176 do {
5177 IObuff[len++] = ' ';
5178 } while (len < 21);
5179
5180 IObuff[len] = '\0';
5181 msg_outtrans(IObuff);
5182
5183 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005184#ifdef FEAT_EVAL
5185 if (p_verbose > 0)
5186 last_set_msg(cmd->uc_scriptID);
5187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 out_flush();
5189 ui_breakcheck();
5190 if (got_int)
5191 break;
5192 }
5193 if (gap == &ucmds || i < gap->ga_len)
5194 break;
5195 gap = &ucmds;
5196 }
5197
5198 if (!found)
5199 MSG(_("No user-defined commands found"));
5200}
5201
5202 static char_u *
5203uc_fun_cmd()
5204{
5205 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5206 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5207 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5208 0xb9, 0x7f, 0};
5209 int i;
5210
5211 for (i = 0; fcmd[i]; ++i)
5212 IObuff[i] = fcmd[i] - 0x40;
5213 IObuff[i] = 0;
5214 return IObuff;
5215}
5216
5217 static int
5218uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5219 char_u *attr;
5220 size_t len;
5221 long *argt;
5222 long *def;
5223 int *flags;
5224 int *compl;
5225 char_u **compl_arg;
5226{
5227 char_u *p;
5228
5229 if (len == 0)
5230 {
5231 EMSG(_("E175: No attribute specified"));
5232 return FAIL;
5233 }
5234
5235 /* First, try the simple attributes (no arguments) */
5236 if (STRNICMP(attr, "bang", len) == 0)
5237 *argt |= BANG;
5238 else if (STRNICMP(attr, "buffer", len) == 0)
5239 *flags |= UC_BUFFER;
5240 else if (STRNICMP(attr, "register", len) == 0)
5241 *argt |= REGSTR;
5242 else if (STRNICMP(attr, "bar", len) == 0)
5243 *argt |= TRLBAR;
5244 else
5245 {
5246 int i;
5247 char_u *val = NULL;
5248 size_t vallen = 0;
5249 size_t attrlen = len;
5250
5251 /* Look for the attribute name - which is the part before any '=' */
5252 for (i = 0; i < (int)len; ++i)
5253 {
5254 if (attr[i] == '=')
5255 {
5256 val = &attr[i + 1];
5257 vallen = len - i - 1;
5258 attrlen = i;
5259 break;
5260 }
5261 }
5262
5263 if (STRNICMP(attr, "nargs", attrlen) == 0)
5264 {
5265 if (vallen == 1)
5266 {
5267 if (*val == '0')
5268 /* Do nothing - this is the default */;
5269 else if (*val == '1')
5270 *argt |= (EXTRA | NOSPC | NEEDARG);
5271 else if (*val == '*')
5272 *argt |= EXTRA;
5273 else if (*val == '?')
5274 *argt |= (EXTRA | NOSPC);
5275 else if (*val == '+')
5276 *argt |= (EXTRA | NEEDARG);
5277 else
5278 goto wrong_nargs;
5279 }
5280 else
5281 {
5282wrong_nargs:
5283 EMSG(_("E176: Invalid number of arguments"));
5284 return FAIL;
5285 }
5286 }
5287 else if (STRNICMP(attr, "range", attrlen) == 0)
5288 {
5289 *argt |= RANGE;
5290 if (vallen == 1 && *val == '%')
5291 *argt |= DFLALL;
5292 else if (val != NULL)
5293 {
5294 p = val;
5295 if (*def >= 0)
5296 {
5297two_count:
5298 EMSG(_("E177: Count cannot be specified twice"));
5299 return FAIL;
5300 }
5301
5302 *def = getdigits(&p);
5303 *argt |= (ZEROR | NOTADR);
5304
5305 if (p != val + vallen || vallen == 0)
5306 {
5307invalid_count:
5308 EMSG(_("E178: Invalid default value for count"));
5309 return FAIL;
5310 }
5311 }
5312 }
5313 else if (STRNICMP(attr, "count", attrlen) == 0)
5314 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005315 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
5317 if (val != NULL)
5318 {
5319 p = val;
5320 if (*def >= 0)
5321 goto two_count;
5322
5323 *def = getdigits(&p);
5324
5325 if (p != val + vallen)
5326 goto invalid_count;
5327 }
5328
5329 if (*def < 0)
5330 *def = 0;
5331 }
5332 else if (STRNICMP(attr, "complete", attrlen) == 0)
5333 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 if (val == NULL)
5335 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005336 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 return FAIL;
5338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005340 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5341 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 }
5344 else
5345 {
5346 char_u ch = attr[len];
5347 attr[len] = '\0';
5348 EMSG2(_("E181: Invalid attribute: %s"), attr);
5349 attr[len] = ch;
5350 return FAIL;
5351 }
5352 }
5353
5354 return OK;
5355}
5356
5357 static void
5358ex_command(eap)
5359 exarg_T *eap;
5360{
5361 char_u *name;
5362 char_u *end;
5363 char_u *p;
5364 long argt = 0;
5365 long def = -1;
5366 int flags = 0;
5367 int compl = EXPAND_NOTHING;
5368 char_u *compl_arg = NULL;
5369 int has_attr = (eap->arg[0] == '-');
5370
5371 p = eap->arg;
5372
5373 /* Check for attributes */
5374 while (*p == '-')
5375 {
5376 ++p;
5377 end = skiptowhite(p);
5378 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5379 == FAIL)
5380 return;
5381 p = skipwhite(end);
5382 }
5383
5384 /* Get the name (if any) and skip to the following argument */
5385 name = p;
5386 if (ASCII_ISALPHA(*p))
5387 while (ASCII_ISALNUM(*p))
5388 ++p;
5389 if (!ends_excmd(*p) && !vim_iswhite(*p))
5390 {
5391 EMSG(_("E182: Invalid command name"));
5392 return;
5393 }
5394 end = p;
5395
5396 /* If there is nothing after the name, and no attributes were specified,
5397 * we are listing commands
5398 */
5399 p = skipwhite(end);
5400 if (!has_attr && ends_excmd(*p))
5401 {
5402 uc_list(name, end - name);
5403 }
5404 else if (!ASCII_ISUPPER(*name))
5405 {
5406 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5407 return;
5408 }
5409 else
5410 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5411 eap->forceit);
5412}
5413
5414/*
5415 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 * Clear all user commands, global and for current buffer.
5417 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005418/*ARGSUSED*/
5419 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420ex_comclear(eap)
5421 exarg_T *eap;
5422{
5423 uc_clear(&ucmds);
5424 uc_clear(&curbuf->b_ucmds);
5425}
5426
5427/*
5428 * Clear all user commands for "gap".
5429 */
5430 void
5431uc_clear(gap)
5432 garray_T *gap;
5433{
5434 int i;
5435 ucmd_T *cmd;
5436
5437 for (i = 0; i < gap->ga_len; ++i)
5438 {
5439 cmd = USER_CMD_GA(gap, i);
5440 vim_free(cmd->uc_name);
5441 vim_free(cmd->uc_rep);
5442# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5443 vim_free(cmd->uc_compl_arg);
5444# endif
5445 }
5446 ga_clear(gap);
5447}
5448
5449 static void
5450ex_delcommand(eap)
5451 exarg_T *eap;
5452{
5453 int i = 0;
5454 ucmd_T *cmd = NULL;
5455 int cmp = -1;
5456 garray_T *gap;
5457
5458 gap = &curbuf->b_ucmds;
5459 for (;;)
5460 {
5461 for (i = 0; i < gap->ga_len; ++i)
5462 {
5463 cmd = USER_CMD_GA(gap, i);
5464 cmp = STRCMP(eap->arg, cmd->uc_name);
5465 if (cmp <= 0)
5466 break;
5467 }
5468 if (gap == &ucmds || cmp == 0)
5469 break;
5470 gap = &ucmds;
5471 }
5472
5473 if (cmp != 0)
5474 {
5475 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5476 return;
5477 }
5478
5479 vim_free(cmd->uc_name);
5480 vim_free(cmd->uc_rep);
5481# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5482 vim_free(cmd->uc_compl_arg);
5483# endif
5484
5485 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
5487 if (i < gap->ga_len)
5488 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5489}
5490
5491 static char_u *
5492uc_split_args(arg, lenp)
5493 char_u *arg;
5494 size_t *lenp;
5495{
5496 char_u *buf;
5497 char_u *p;
5498 char_u *q;
5499 int len;
5500
5501 /* Precalculate length */
5502 p = arg;
5503 len = 2; /* Initial and final quotes */
5504
5505 while (*p)
5506 {
5507 if (p[0] == '\\' && vim_iswhite(p[1]))
5508 {
5509 len += 1;
5510 p += 2;
5511 }
5512 else if (*p == '\\' || *p == '"')
5513 {
5514 len += 2;
5515 p += 1;
5516 }
5517 else if (vim_iswhite(*p))
5518 {
5519 p = skipwhite(p);
5520 if (*p == NUL)
5521 break;
5522 len += 3; /* "," */
5523 }
5524 else
5525 {
5526 ++len;
5527 ++p;
5528 }
5529 }
5530
5531 buf = alloc(len + 1);
5532 if (buf == NULL)
5533 {
5534 *lenp = 0;
5535 return buf;
5536 }
5537
5538 p = arg;
5539 q = buf;
5540 *q++ = '"';
5541 while (*p)
5542 {
5543 if (p[0] == '\\' && vim_iswhite(p[1]))
5544 {
5545 *q++ = p[1];
5546 p += 2;
5547 }
5548 else if (*p == '\\' || *p == '"')
5549 {
5550 *q++ = '\\';
5551 *q++ = *p++;
5552 }
5553 else if (vim_iswhite(*p))
5554 {
5555 p = skipwhite(p);
5556 if (*p == NUL)
5557 break;
5558 *q++ = '"';
5559 *q++ = ',';
5560 *q++ = '"';
5561 }
5562 else
5563 {
5564 *q++ = *p++;
5565 }
5566 }
5567 *q++ = '"';
5568 *q = 0;
5569
5570 *lenp = len;
5571 return buf;
5572}
5573
5574/*
5575 * Check for a <> code in a user command.
5576 * "code" points to the '<'. "len" the length of the <> (inclusive).
5577 * "buf" is where the result is to be added.
5578 * "split_buf" points to a buffer used for splitting, caller should free it.
5579 * "split_len" is the length of what "split_buf" contains.
5580 * Returns the length of the replacement, which has been added to "buf".
5581 * Returns -1 if there was no match, and only the "<" has been copied.
5582 */
5583 static size_t
5584uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5585 char_u *code;
5586 size_t len;
5587 char_u *buf;
5588 ucmd_T *cmd; /* the user command we're expanding */
5589 exarg_T *eap; /* ex arguments */
5590 char_u **split_buf;
5591 size_t *split_len;
5592{
5593 size_t result = 0;
5594 char_u *p = code + 1;
5595 size_t l = len - 2;
5596 int quote = 0;
5597 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5598 ct_LT, ct_NONE } type = ct_NONE;
5599
5600 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5601 {
5602 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5603 p += 2;
5604 l -= 2;
5605 }
5606
5607 if (l < 1)
5608 type = ct_NONE;
5609 else if (STRNICMP(p, "args", l) == 0)
5610 type = ct_ARGS;
5611 else if (STRNICMP(p, "bang", l) == 0)
5612 type = ct_BANG;
5613 else if (STRNICMP(p, "count", l) == 0)
5614 type = ct_COUNT;
5615 else if (STRNICMP(p, "line1", l) == 0)
5616 type = ct_LINE1;
5617 else if (STRNICMP(p, "line2", l) == 0)
5618 type = ct_LINE2;
5619 else if (STRNICMP(p, "lt", l) == 0)
5620 type = ct_LT;
5621 else if (STRNICMP(p, "register", l) == 0)
5622 type = ct_REGISTER;
5623
5624 switch (type)
5625 {
5626 case ct_ARGS:
5627 /* Simple case first */
5628 if (*eap->arg == NUL)
5629 {
5630 if (quote == 1)
5631 {
5632 result = 2;
5633 if (buf != NULL)
5634 STRCPY(buf, "''");
5635 }
5636 else
5637 result = 0;
5638 break;
5639 }
5640
5641 /* When specified there is a single argument don't split it.
5642 * Works for ":Cmd %" when % is "a b c". */
5643 if ((eap->argt & NOSPC) && quote == 2)
5644 quote = 1;
5645
5646 switch (quote)
5647 {
5648 case 0: /* No quoting, no splitting */
5649 result = STRLEN(eap->arg);
5650 if (buf != NULL)
5651 STRCPY(buf, eap->arg);
5652 break;
5653 case 1: /* Quote, but don't split */
5654 result = STRLEN(eap->arg) + 2;
5655 for (p = eap->arg; *p; ++p)
5656 {
5657 if (*p == '\\' || *p == '"')
5658 ++result;
5659 }
5660
5661 if (buf != NULL)
5662 {
5663 *buf++ = '"';
5664 for (p = eap->arg; *p; ++p)
5665 {
5666 if (*p == '\\' || *p == '"')
5667 *buf++ = '\\';
5668 *buf++ = *p;
5669 }
5670 *buf = '"';
5671 }
5672
5673 break;
5674 case 2: /* Quote and split */
5675 /* This is hard, so only do it once, and cache the result */
5676 if (*split_buf == NULL)
5677 *split_buf = uc_split_args(eap->arg, split_len);
5678
5679 result = *split_len;
5680 if (buf != NULL && result != 0)
5681 STRCPY(buf, *split_buf);
5682
5683 break;
5684 }
5685 break;
5686
5687 case ct_BANG:
5688 result = eap->forceit ? 1 : 0;
5689 if (quote)
5690 result += 2;
5691 if (buf != NULL)
5692 {
5693 if (quote)
5694 *buf++ = '"';
5695 if (eap->forceit)
5696 *buf++ = '!';
5697 if (quote)
5698 *buf = '"';
5699 }
5700 break;
5701
5702 case ct_LINE1:
5703 case ct_LINE2:
5704 case ct_COUNT:
5705 {
5706 char num_buf[20];
5707 long num = (type == ct_LINE1) ? eap->line1 :
5708 (type == ct_LINE2) ? eap->line2 :
5709 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5710 size_t num_len;
5711
5712 sprintf(num_buf, "%ld", num);
5713 num_len = STRLEN(num_buf);
5714 result = num_len;
5715
5716 if (quote)
5717 result += 2;
5718
5719 if (buf != NULL)
5720 {
5721 if (quote)
5722 *buf++ = '"';
5723 STRCPY(buf, num_buf);
5724 buf += num_len;
5725 if (quote)
5726 *buf = '"';
5727 }
5728
5729 break;
5730 }
5731
5732 case ct_REGISTER:
5733 result = eap->regname ? 1 : 0;
5734 if (quote)
5735 result += 2;
5736 if (buf != NULL)
5737 {
5738 if (quote)
5739 *buf++ = '\'';
5740 if (eap->regname)
5741 *buf++ = eap->regname;
5742 if (quote)
5743 *buf = '\'';
5744 }
5745 break;
5746
5747 case ct_LT:
5748 result = 1;
5749 if (buf != NULL)
5750 *buf = '<';
5751 break;
5752
5753 default:
5754 /* Not recognized: just copy the '<' and return -1. */
5755 result = (size_t)-1;
5756 if (buf != NULL)
5757 *buf = '<';
5758 break;
5759 }
5760
5761 return result;
5762}
5763
5764 static void
5765do_ucmd(eap)
5766 exarg_T *eap;
5767{
5768 char_u *buf;
5769 char_u *p;
5770 char_u *q;
5771
5772 char_u *start;
5773 char_u *end;
5774 size_t len, totlen;
5775
5776 size_t split_len = 0;
5777 char_u *split_buf = NULL;
5778 ucmd_T *cmd;
5779#ifdef FEAT_EVAL
5780 scid_T save_current_SID = current_SID;
5781#endif
5782
5783 if (eap->cmdidx == CMD_USER)
5784 cmd = USER_CMD(eap->useridx);
5785 else
5786 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5787
5788 /*
5789 * Replace <> in the command by the arguments.
5790 */
5791 buf = NULL;
5792 for (;;)
5793 {
5794 p = cmd->uc_rep;
5795 q = buf;
5796 totlen = 0;
5797 while ((start = vim_strchr(p, '<')) != NULL
5798 && (end = vim_strchr(start + 1, '>')) != NULL)
5799 {
5800 /* Include the '>' */
5801 ++end;
5802
5803 /* Take everything up to the '<' */
5804 len = start - p;
5805 if (buf == NULL)
5806 totlen += len;
5807 else
5808 {
5809 mch_memmove(q, p, len);
5810 q += len;
5811 }
5812
5813 len = uc_check_code(start, end - start, q, cmd, eap,
5814 &split_buf, &split_len);
5815 if (len == (size_t)-1)
5816 {
5817 /* no match, continue after '<' */
5818 p = start + 1;
5819 len = 1;
5820 }
5821 else
5822 p = end;
5823 if (buf == NULL)
5824 totlen += len;
5825 else
5826 q += len;
5827 }
5828 if (buf != NULL) /* second time here, finished */
5829 {
5830 STRCPY(q, p);
5831 break;
5832 }
5833
5834 totlen += STRLEN(p); /* Add on the trailing characters */
5835 buf = alloc((unsigned)(totlen + 1));
5836 if (buf == NULL)
5837 {
5838 vim_free(split_buf);
5839 return;
5840 }
5841 }
5842
5843#ifdef FEAT_EVAL
5844 current_SID = cmd->uc_scriptID;
5845#endif
5846 (void)do_cmdline(buf, eap->getline, eap->cookie,
5847 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5848#ifdef FEAT_EVAL
5849 current_SID = save_current_SID;
5850#endif
5851 vim_free(buf);
5852 vim_free(split_buf);
5853}
5854
5855# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5856 static char_u *
5857get_user_command_name(idx)
5858 int idx;
5859{
5860 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5861}
5862
5863/*
5864 * Function given to ExpandGeneric() to obtain the list of user command names.
5865 */
5866/*ARGSUSED*/
5867 char_u *
5868get_user_commands(xp, idx)
5869 expand_T *xp;
5870 int idx;
5871{
5872 if (idx < curbuf->b_ucmds.ga_len)
5873 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5874 idx -= curbuf->b_ucmds.ga_len;
5875 if (idx < ucmds.ga_len)
5876 return USER_CMD(idx)->uc_name;
5877 return NULL;
5878}
5879
5880/*
5881 * Function given to ExpandGeneric() to obtain the list of user command
5882 * attributes.
5883 */
5884/*ARGSUSED*/
5885 char_u *
5886get_user_cmd_flags(xp, idx)
5887 expand_T *xp;
5888 int idx;
5889{
5890 static char *user_cmd_flags[] =
5891 {"bang", "bar", "buffer", "complete", "count",
5892 "nargs", "range", "register"};
5893
5894 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5895 return NULL;
5896 return (char_u *)user_cmd_flags[idx];
5897}
5898
5899/*
5900 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5901 */
5902/*ARGSUSED*/
5903 char_u *
5904get_user_cmd_nargs(xp, idx)
5905 expand_T *xp;
5906 int idx;
5907{
5908 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5909
5910 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5911 return NULL;
5912 return (char_u *)user_cmd_nargs[idx];
5913}
5914
5915/*
5916 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5917 */
5918/*ARGSUSED*/
5919 char_u *
5920get_user_cmd_complete(xp, idx)
5921 expand_T *xp;
5922 int idx;
5923{
5924 return (char_u *)command_complete[idx].name;
5925}
5926# endif /* FEAT_CMDL_COMPL */
5927
5928#endif /* FEAT_USR_CMDS */
5929
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005930#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
5931/*
5932 * Parse a completion argument "value[vallen]".
5933 * The detected completion goes in "*complp", argument type in "*argt".
5934 * When there is an argument, for function and user defined completion, it's
5935 * copied to allocated memory and stored in "*compl_arg".
5936 * Returns FAIL if something is wrong.
5937 */
5938 int
5939parse_compl_arg(value, vallen, complp, argt, compl_arg)
5940 char_u *value;
5941 int vallen;
5942 int *complp;
5943 long *argt;
5944 char_u **compl_arg;
5945{
5946 char_u *arg = NULL;
5947 size_t arglen = 0;
5948 int i;
5949 int valend = vallen;
5950
5951 /* Look for any argument part - which is the part after any ',' */
5952 for (i = 0; i < vallen; ++i)
5953 {
5954 if (value[i] == ',')
5955 {
5956 arg = &value[i + 1];
5957 arglen = vallen - i - 1;
5958 valend = i;
5959 break;
5960 }
5961 }
5962
5963 for (i = 0; command_complete[i].expand != 0; ++i)
5964 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005965 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005966 && STRNCMP(value, command_complete[i].name, valend) == 0)
5967 {
5968 *complp = command_complete[i].expand;
5969 if (command_complete[i].expand == EXPAND_BUFFERS)
5970 *argt |= BUFNAME;
5971 else if (command_complete[i].expand == EXPAND_DIRECTORIES
5972 || command_complete[i].expand == EXPAND_FILES)
5973 *argt |= XFILE;
5974 break;
5975 }
5976 }
5977
5978 if (command_complete[i].expand == 0)
5979 {
5980 EMSG2(_("E180: Invalid complete value: %s"), value);
5981 return FAIL;
5982 }
5983
5984# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5985 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
5986 && arg != NULL)
5987# else
5988 if (arg != NULL)
5989# endif
5990 {
5991 EMSG(_("E468: Completion argument only allowed for custom completion"));
5992 return FAIL;
5993 }
5994
5995# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5996 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
5997 && arg == NULL)
5998 {
5999 EMSG(_("E467: Custom completion requires a function argument"));
6000 return FAIL;
6001 }
6002
6003 if (arg != NULL)
6004 *compl_arg = vim_strnsave(arg, (int)arglen);
6005# endif
6006 return OK;
6007}
6008#endif
6009
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 static void
6011ex_colorscheme(eap)
6012 exarg_T *eap;
6013{
6014 if (load_colors(eap->arg) == FAIL)
6015 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6016}
6017
6018 static void
6019ex_highlight(eap)
6020 exarg_T *eap;
6021{
6022 if (*eap->arg == NUL && eap->cmd[2] == '!')
6023 MSG(_("Greetings, Vim user!"));
6024 do_highlight(eap->arg, eap->forceit, FALSE);
6025}
6026
6027
6028/*
6029 * Call this function if we thought we were going to exit, but we won't
6030 * (because of an error). May need to restore the terminal mode.
6031 */
6032 void
6033not_exiting()
6034{
6035 exiting = FALSE;
6036 settmode(TMODE_RAW);
6037}
6038
6039/*
6040 * ":quit": quit current window, quit Vim if closed the last window.
6041 */
6042 static void
6043ex_quit(eap)
6044 exarg_T *eap;
6045{
6046#ifdef FEAT_CMDWIN
6047 if (cmdwin_type != 0)
6048 {
6049 cmdwin_result = Ctrl_C;
6050 return;
6051 }
6052#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006053 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006054 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006055 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006056 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006057 return;
6058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059
6060#ifdef FEAT_NETBEANS_INTG
6061 netbeansForcedQuit = eap->forceit;
6062#endif
6063
6064 /*
6065 * If there are more files or windows we won't exit.
6066 */
6067 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6068 exiting = TRUE;
6069 if ((!P_HID(curbuf)
6070 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6071 || check_more(TRUE, eap->forceit) == FAIL
6072 || (only_one_window() && check_changed_any(eap->forceit)))
6073 {
6074 not_exiting();
6075 }
6076 else
6077 {
6078#ifdef FEAT_WINDOWS
6079 if (only_one_window()) /* quit last window */
6080#endif
6081 getout(0);
6082#ifdef FEAT_WINDOWS
6083# ifdef FEAT_GUI
6084 need_mouse_correct = TRUE;
6085# endif
6086 /* close window; may free buffer */
6087 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6088#endif
6089 }
6090}
6091
6092/*
6093 * ":cquit".
6094 */
6095/*ARGSUSED*/
6096 static void
6097ex_cquit(eap)
6098 exarg_T *eap;
6099{
6100 getout(1); /* this does not always pass on the exit code to the Manx
6101 compiler. why? */
6102}
6103
6104/*
6105 * ":qall": try to quit all windows
6106 */
6107 static void
6108ex_quit_all(eap)
6109 exarg_T *eap;
6110{
6111# ifdef FEAT_CMDWIN
6112 if (cmdwin_type != 0)
6113 {
6114 if (eap->forceit)
6115 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6116 else
6117 cmdwin_result = K_XF2;
6118 return;
6119 }
6120# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006121
6122 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006123 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006124 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006125 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006126 return;
6127 }
6128
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 exiting = TRUE;
6130 if (eap->forceit || !check_changed_any(FALSE))
6131 getout(0);
6132 not_exiting();
6133}
6134
6135#ifdef FEAT_WINDOWS
6136/*
6137 * ":close": close current window, unless it is the last one
6138 */
6139 static void
6140ex_close(eap)
6141 exarg_T *eap;
6142{
6143# ifdef FEAT_CMDWIN
6144 if (cmdwin_type != 0)
6145 cmdwin_result = K_IGNORE;
6146 else
6147# endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006148 if (!text_locked())
Bram Moolenaarf740b292006-02-16 22:11:02 +00006149 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150}
6151
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006152#ifdef FEAT_QUICKFIX
6153/*
6154 * ":pclose": Close any preview window.
6155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006157ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006159{
6160 win_T *win;
6161
6162 for (win = firstwin; win != NULL; win = win->w_next)
6163 if (win->w_p_pvw)
6164 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006165 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006166 break;
6167 }
6168}
6169#endif
6170
Bram Moolenaarf740b292006-02-16 22:11:02 +00006171/*
6172 * Close window "win" and take care of handling closing the last window for a
6173 * modified buffer.
6174 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006175 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006176ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006177 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006179 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180{
6181 int need_hide;
6182 buf_T *buf = win->w_buffer;
6183
6184 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006185 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 {
6187#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6188 if ((p_confirm || cmdmod.confirm) && p_write)
6189 {
6190 dialog_changed(buf, FALSE);
6191 if (buf_valid(buf) && bufIsChanged(buf))
6192 return;
6193 need_hide = FALSE;
6194 }
6195 else
6196#endif
6197 {
6198 EMSG(_(e_nowrtmsg));
6199 return;
6200 }
6201 }
6202
6203#ifdef FEAT_GUI
6204 need_mouse_correct = TRUE;
6205#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006206
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006208 if (tp == NULL)
6209 win_close(win, !need_hide && !P_HID(buf));
6210 else
6211 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212}
6213
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006215 * ":tabclose": close current tab page, unless it is the last one.
6216 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 */
6218 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006219ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 exarg_T *eap;
6221{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006222 tabpage_T *tp;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006223 int h = tabpageline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006224
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006225# ifdef FEAT_CMDWIN
6226 if (cmdwin_type != 0)
6227 cmdwin_result = K_IGNORE;
6228 else
6229# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006230 if (first_tabpage->tp_next == NULL)
6231 EMSG(_("E999: Cannot close last tab page"));
6232 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006234 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006235 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006236 tp = find_tabpage((int)eap->line2);
6237 if (tp == NULL)
6238 {
6239 beep_flush();
6240 return;
6241 }
6242 if (tp->tp_topframe != topframe)
6243 {
6244 tabpage_close_other(tp, eap->forceit);
6245 return;
6246 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006247 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00006248 if (!text_locked())
6249 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006251
6252 if (h != tabpageline_height())
6253 shell_new_rows();
6254}
6255
6256/*
6257 * ":tabonly": close all tab pages except the current one
6258 */
6259 static void
6260ex_tabonly(eap)
6261 exarg_T *eap;
6262{
6263 tabpage_T *tp;
6264 int done;
6265 int h = tabpageline_height();
6266
6267# ifdef FEAT_CMDWIN
6268 if (cmdwin_type != 0)
6269 cmdwin_result = K_IGNORE;
6270 else
6271# endif
6272 if (first_tabpage->tp_next == NULL)
6273 MSG(_("Already only one tab page"));
6274 else
6275 {
6276 /* Repeat this up to a 1000 times, because autocommands may mess
6277 * up the lists. */
6278 for (done = 0; done < 1000; ++done)
6279 {
6280 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6281 if (tp->tp_topframe != topframe)
6282 {
6283 tabpage_close_other(tp, eap->forceit);
6284 /* if we failed to close it quit */
6285 if (valid_tabpage(tp))
6286 done = 1000;
6287 /* start over, "tp" is now invalid */
6288 break;
6289 }
6290 if (first_tabpage->tp_next == NULL)
6291 break;
6292 }
6293 }
6294
6295 if (h != tabpageline_height())
6296 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298
6299/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006300 * Close the current tab page.
6301 */
6302 void
6303tabpage_close(forceit)
6304 int forceit;
6305{
6306 /* First close all the windows but the current one. If that worked then
6307 * close the last window in this tab, that will close it. */
6308 close_others(TRUE, forceit);
6309 if (lastwin == firstwin)
6310 ex_win_close(forceit, curwin, NULL);
6311# ifdef FEAT_GUI
6312 need_mouse_correct = TRUE;
6313# endif
6314}
6315
6316/*
6317 * Close tab page "tp", which is not the current tab page.
6318 * Note that autocommands may make "tp" invalid.
6319 */
6320 void
6321tabpage_close_other(tp, forceit)
6322 tabpage_T *tp;
6323 int forceit;
6324{
6325 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006326 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006327
6328 /* Limit to 1000 windows, autocommands may add a window while we close
6329 * one. OK, so I'm paranoid... */
6330 while (++done < 1000)
6331 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006332 wp = tp->tp_firstwin;
6333 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006334
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006335 /* Autocommands may delete the tab page under our fingers and we may
6336 * fail to close a window with a modified buffer. */
6337 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006338 break;
6339 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006340 redraw_tabline = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006341}
6342
6343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 * ":only".
6345 */
6346 static void
6347ex_only(eap)
6348 exarg_T *eap;
6349{
6350# ifdef FEAT_GUI
6351 need_mouse_correct = TRUE;
6352# endif
6353 close_others(TRUE, eap->forceit);
6354}
6355
6356/*
6357 * ":all" and ":sall".
6358 */
6359 static void
6360ex_all(eap)
6361 exarg_T *eap;
6362{
6363 if (eap->addr_count == 0)
6364 eap->line2 = 9999;
6365 do_arg_all((int)eap->line2, eap->forceit);
6366}
6367#endif /* FEAT_WINDOWS */
6368
6369 static void
6370ex_hide(eap)
6371 exarg_T *eap;
6372{
6373 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6374 eap->errmsg = e_invarg;
6375 else
6376 {
6377 /* ":hide" or ":hide | cmd": hide current window */
6378 eap->nextcmd = check_nextcmd(eap->arg);
6379#ifdef FEAT_WINDOWS
6380 if (!eap->skip)
6381 {
6382# ifdef FEAT_GUI
6383 need_mouse_correct = TRUE;
6384# endif
6385 win_close(curwin, FALSE); /* don't free buffer */
6386 }
6387#endif
6388 }
6389}
6390
6391/*
6392 * ":stop" and ":suspend": Suspend Vim.
6393 */
6394 static void
6395ex_stop(eap)
6396 exarg_T *eap;
6397{
6398 /*
6399 * Disallow suspending for "rvim".
6400 */
6401 if (!check_restricted()
6402#ifdef WIN3264
6403 /*
6404 * Check if external commands are allowed now.
6405 */
6406 && can_end_termcap_mode(TRUE)
6407#endif
6408 )
6409 {
6410 if (!eap->forceit)
6411 autowrite_all();
6412 windgoto((int)Rows - 1, 0);
6413 out_char('\n');
6414 out_flush();
6415 stoptermcap();
6416 out_flush(); /* needed for SUN to restore xterm buffer */
6417#ifdef FEAT_TITLE
6418 mch_restore_title(3); /* restore window titles */
6419#endif
6420 ui_suspend(); /* call machine specific function */
6421#ifdef FEAT_TITLE
6422 maketitle();
6423 resettitle(); /* force updating the title */
6424#endif
6425 starttermcap();
6426 scroll_start(); /* scroll screen before redrawing */
6427 redraw_later_clear();
6428 shell_resized(); /* may have resized window */
6429 }
6430}
6431
6432/*
6433 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6434 */
6435 static void
6436ex_exit(eap)
6437 exarg_T *eap;
6438{
6439#ifdef FEAT_CMDWIN
6440 if (cmdwin_type != 0)
6441 {
6442 cmdwin_result = Ctrl_C;
6443 return;
6444 }
6445#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006446 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006447 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006448 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006449 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006450 return;
6451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452
6453 /*
6454 * if more files or windows we won't exit
6455 */
6456 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6457 exiting = TRUE;
6458 if ( ((eap->cmdidx == CMD_wq
6459 || curbufIsChanged())
6460 && do_write(eap) == FAIL)
6461 || check_more(TRUE, eap->forceit) == FAIL
6462 || (only_one_window() && check_changed_any(eap->forceit)))
6463 {
6464 not_exiting();
6465 }
6466 else
6467 {
6468#ifdef FEAT_WINDOWS
6469 if (only_one_window()) /* quit last window, exit Vim */
6470#endif
6471 getout(0);
6472#ifdef FEAT_WINDOWS
6473# ifdef FEAT_GUI
6474 need_mouse_correct = TRUE;
6475# endif
6476 /* quit current window, may free buffer */
6477 win_close(curwin, !P_HID(curwin->w_buffer));
6478#endif
6479 }
6480}
6481
6482/*
6483 * ":print", ":list", ":number".
6484 */
6485 static void
6486ex_print(eap)
6487 exarg_T *eap;
6488{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006489 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6490 EMSG(_(e_emptybuf));
6491 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006493 for ( ;!got_int; ui_breakcheck())
6494 {
6495 print_line(eap->line1,
6496 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6497 || (eap->flags & EXFLAG_NR)),
6498 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6499 if (++eap->line1 > eap->line2)
6500 break;
6501 out_flush(); /* show one line at a time */
6502 }
6503 setpcmark();
6504 /* put cursor at last line */
6505 curwin->w_cursor.lnum = eap->line2;
6506 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 }
6508
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510}
6511
6512#ifdef FEAT_BYTEOFF
6513 static void
6514ex_goto(eap)
6515 exarg_T *eap;
6516{
6517 goto_byte(eap->line2);
6518}
6519#endif
6520
6521/*
6522 * ":shell".
6523 */
6524/*ARGSUSED*/
6525 static void
6526ex_shell(eap)
6527 exarg_T *eap;
6528{
6529 do_shell(NULL, 0);
6530}
6531
6532#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6533 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006534 || defined(FEAT_GUI_MSWIN) \
6535 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 || defined(PROTO)
6537
6538/*
6539 * Handle a file drop. The code is here because a drop is *nearly* like an
6540 * :args command, but not quite (we have a list of exact filenames, so we
6541 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6542 * code is very similar to :args and hence needs access to a lot of the static
6543 * functions in this file.
6544 *
6545 * The list should be allocated using alloc(), as should each item in the
6546 * list. This function takes over responsibility for freeing the list.
6547 *
6548 * XXX The list is made into the arggument list. This is freed using
6549 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6550 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6551 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6552 * file functionality is (currently) not in EMX this is not presently a
6553 * problem.
6554 */
6555 void
6556handle_drop(filec, filev, split)
6557 int filec; /* the number of files dropped */
6558 char_u **filev; /* the list of files dropped */
6559 int split; /* force splitting the window */
6560{
6561 exarg_T ea;
6562 int save_msg_scroll = msg_scroll;
6563
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006564 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006565 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
6568 /* Check whether the current buffer is changed. If so, we will need
6569 * to split the current window or data could be lost.
6570 * We don't need to check if the 'hidden' option is set, as in this
6571 * case the buffer won't be lost.
6572 */
6573 if (!P_HID(curbuf) && !split)
6574 {
6575 ++emsg_off;
6576 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6577 --emsg_off;
6578 }
6579 if (split)
6580 {
6581# ifdef FEAT_WINDOWS
6582 if (win_split(0, 0) == FAIL)
6583 return;
6584# ifdef FEAT_SCROLLBIND
6585 curwin->w_p_scb = FALSE;
6586# endif
6587
6588 /* When splitting the window, create a new alist. Otherwise the
6589 * existing one is overwritten. */
6590 alist_unlink(curwin->w_alist);
6591 alist_new();
6592# else
6593 return; /* can't split, always fail */
6594# endif
6595 }
6596
6597 /*
6598 * Set up the new argument list.
6599 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006600 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601
6602 /*
6603 * Move to the first file.
6604 */
6605 /* Fake up a minimal "next" command for do_argfile() */
6606 vim_memset(&ea, 0, sizeof(ea));
6607 ea.cmd = (char_u *)"next";
6608 do_argfile(&ea, 0);
6609
6610 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6611 * mode that is not needed here. */
6612 need_start_insertmode = FALSE;
6613
6614 /* Restore msg_scroll, otherwise a following command may cause scrolling
6615 * unexpectedly. The screen will be redrawn by the caller, thus
6616 * msg_scroll being set by displaying a message is irrelevant. */
6617 msg_scroll = save_msg_scroll;
6618}
6619#endif
6620
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621/*
6622 * Clear an argument list: free all file names and reset it to zero entries.
6623 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006624 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625alist_clear(al)
6626 alist_T *al;
6627{
6628 while (--al->al_ga.ga_len >= 0)
6629 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6630 ga_clear(&al->al_ga);
6631}
6632
6633/*
6634 * Init an argument list.
6635 */
6636 void
6637alist_init(al)
6638 alist_T *al;
6639{
6640 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6641}
6642
6643#if defined(FEAT_WINDOWS) || defined(PROTO)
6644
6645/*
6646 * Remove a reference from an argument list.
6647 * Ignored when the argument list is the global one.
6648 * If the argument list is no longer used by any window, free it.
6649 */
6650 void
6651alist_unlink(al)
6652 alist_T *al;
6653{
6654 if (al != &global_alist && --al->al_refcount <= 0)
6655 {
6656 alist_clear(al);
6657 vim_free(al);
6658 }
6659}
6660
6661# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6662/*
6663 * Create a new argument list and use it for the current window.
6664 */
6665 void
6666alist_new()
6667{
6668 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6669 if (curwin->w_alist == NULL)
6670 {
6671 curwin->w_alist = &global_alist;
6672 ++global_alist.al_refcount;
6673 }
6674 else
6675 {
6676 curwin->w_alist->al_refcount = 1;
6677 alist_init(curwin->w_alist);
6678 }
6679}
6680# endif
6681#endif
6682
6683#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6684/*
6685 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006686 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6687 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 */
6689 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006690alist_expand(fnum_list, fnum_len)
6691 int *fnum_list;
6692 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693{
6694 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006695 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 char_u **new_arg_files;
6697 int new_arg_file_count;
6698 char_u *save_p_su = p_su;
6699 int i;
6700
6701 /* Don't use 'suffixes' here. This should work like the shell did the
6702 * expansion. Also, the vimrc file isn't read yet, thus the user
6703 * can't set the options. */
6704 p_su = empty_option;
6705 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6706 if (old_arg_files != NULL)
6707 {
6708 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006709 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6710 old_arg_count = GARGCOUNT;
6711 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 &new_arg_file_count, &new_arg_files,
6713 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6714 && new_arg_file_count > 0)
6715 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006716 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6717 TRUE, fnum_list, fnum_len);
6718 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 }
6721 p_su = save_p_su;
6722}
6723#endif
6724
6725/*
6726 * Set the argument list for the current window.
6727 * Takes over the allocated files[] and the allocated fnames in it.
6728 */
6729 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006730alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 alist_T *al;
6732 int count;
6733 char_u **files;
6734 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006735 int *fnum_list;
6736 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737{
6738 int i;
6739
6740 alist_clear(al);
6741 if (ga_grow(&al->al_ga, count) == OK)
6742 {
6743 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006744 {
6745 if (got_int)
6746 {
6747 /* When adding many buffers this can take a long time. Allow
6748 * interrupting here. */
6749 while (i < count)
6750 vim_free(files[i++]);
6751 break;
6752 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006753
6754 /* May set buffer name of a buffer previously used for the
6755 * argument list, so that it's re-used by alist_add. */
6756 if (fnum_list != NULL && i < fnum_len)
6757 buf_set_name(fnum_list[i], files[i]);
6758
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006760 ui_breakcheck();
6761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 vim_free(files);
6763 }
6764 else
6765 FreeWild(count, files);
6766#ifdef FEAT_WINDOWS
6767 if (al == &global_alist)
6768#endif
6769 arg_had_last = FALSE;
6770}
6771
6772/*
6773 * Add file "fname" to argument list "al".
6774 * "fname" must have been allocated and "al" must have been checked for room.
6775 */
6776 void
6777alist_add(al, fname, set_fnum)
6778 alist_T *al;
6779 char_u *fname;
6780 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6781{
6782 if (fname == NULL) /* don't add NULL file names */
6783 return;
6784#ifdef BACKSLASH_IN_FILENAME
6785 slash_adjust(fname);
6786#endif
6787 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6788 if (set_fnum > 0)
6789 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6790 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6791 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792}
6793
6794#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6795/*
6796 * Adjust slashes in file names. Called after 'shellslash' was set.
6797 */
6798 void
6799alist_slash_adjust()
6800{
6801 int i;
6802# ifdef FEAT_WINDOWS
6803 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006804 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805# endif
6806
6807 for (i = 0; i < GARGCOUNT; ++i)
6808 if (GARGLIST[i].ae_fname != NULL)
6809 slash_adjust(GARGLIST[i].ae_fname);
6810# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006811 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 if (wp->w_alist != &global_alist)
6813 for (i = 0; i < WARGCOUNT(wp); ++i)
6814 if (WARGLIST(wp)[i].ae_fname != NULL)
6815 slash_adjust(WARGLIST(wp)[i].ae_fname);
6816# endif
6817}
6818#endif
6819
6820/*
6821 * ":preserve".
6822 */
6823/*ARGSUSED*/
6824 static void
6825ex_preserve(eap)
6826 exarg_T *eap;
6827{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006828 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 ml_preserve(curbuf, TRUE);
6830}
6831
6832/*
6833 * ":recover".
6834 */
6835 static void
6836ex_recover(eap)
6837 exarg_T *eap;
6838{
6839 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6840 recoverymode = TRUE;
6841 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6842 && (*eap->arg == NUL
6843 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6844 ml_recover();
6845 recoverymode = FALSE;
6846}
6847
6848/*
6849 * Command modifier used in a wrong way.
6850 */
6851 static void
6852ex_wrongmodifier(eap)
6853 exarg_T *eap;
6854{
6855 eap->errmsg = e_invcmd;
6856}
6857
6858#ifdef FEAT_WINDOWS
6859/*
6860 * :sview [+command] file split window with new file, read-only
6861 * :split [[+command] file] split window with current or new file
6862 * :vsplit [[+command] file] split window vertically with current or new file
6863 * :new [[+command] file] split window with no or new file
6864 * :vnew [[+command] file] split vertically window with no or new file
6865 * :sfind [+command] file split window with file in 'path'
6866 */
6867 void
6868ex_splitview(eap)
6869 exarg_T *eap;
6870{
6871 win_T *old_curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006872# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006874# endif
6875# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006879# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6881 {
6882 ex_ni(eap);
6883 return;
6884 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886
6887 old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006888# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006892# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 /* A ":split" in the quickfix window works like ":new". Don't want two
6894 * quickfix windows. */
6895 if (bt_quickfix(curbuf))
6896 {
6897 if (eap->cmdidx == CMD_split)
6898 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006899# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 if (eap->cmdidx == CMD_vsplit)
6901 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006906# ifdef FEAT_SEARCHPATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 if (eap->cmdidx == CMD_sfind)
6908 {
6909 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6910 FNAME_MESS, TRUE, curbuf->b_ffname);
6911 if (fname == NULL)
6912 goto theend;
6913 eap->arg = fname;
6914 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006915# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006919# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006921# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 && eap->cmdidx != CMD_new)
6925 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006926 if (
6927# ifdef FEAT_GUI
6928 !gui.in_use &&
6929# endif
6930 au_has_group((char_u *)"FileExplorer"))
6931 {
6932 /* No browsing supported but we do have the file explorer:
6933 * Edit the directory. */
6934 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6935 eap->arg = (char_u *)".";
6936 }
6937 else
6938 {
6939 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006941 if (fname == NULL)
6942 goto theend;
6943 eap->arg = fname;
6944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 }
6946 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006947# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948
6949 if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
6950 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6951 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006952# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 /* Reset 'scrollbind' when editing another file, but keep it when
6954 * doing ":split" without arguments. */
6955 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006956# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006958# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 )
6960 curwin->w_p_scb = FALSE;
6961 else
6962 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 do_exedit(eap, old_curwin);
6965 }
6966
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006967# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006969# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006971# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972theend:
6973 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006974# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006976
6977/*
6978 * :tabedit [[+command] file] open new Tab page with empty window
6979 * :tabedit [[+command] file] open new Tab page and edit "file"
6980 * :tabfind [[+command] file] open new Tab page and find "file"
6981 */
6982 void
6983ex_tabedit(eap)
6984 exarg_T *eap;
6985{
6986# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6987 char_u *fname = NULL;
6988# endif
6989# ifdef FEAT_BROWSE
6990 int browse_flag = cmdmod.browse;
6991# endif
6992
6993# ifdef FEAT_GUI
6994 need_mouse_correct = TRUE;
6995# endif
6996
6997# ifdef FEAT_SEARCHPATH
6998 if (eap->cmdidx == CMD_tabfind)
6999 {
7000 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7001 FNAME_MESS, TRUE, curbuf->b_ffname);
7002 if (fname == NULL)
7003 goto theend;
7004 eap->arg = fname;
7005 }
7006# ifdef FEAT_BROWSE
7007 else
7008# endif
7009# endif
7010# ifdef FEAT_BROWSE
7011 if (cmdmod.browse)
7012 {
7013 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 tab page"),
7027 eap->arg, NULL, NULL, NULL, curbuf);
7028 if (fname == NULL)
7029 goto theend;
7030 eap->arg = fname;
7031 }
7032 }
7033 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
7034# endif
7035
7036 if (win_new_tabpage() != FAIL)
7037 {
7038# ifdef FEAT_SCROLLBIND
7039 curwin->w_p_scb = FALSE;
7040# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007041 if (*eap->arg != NUL)
7042 do_exedit(eap, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007043 }
7044
7045# ifdef FEAT_BROWSE
7046 cmdmod.browse = browse_flag;
7047# endif
7048
7049# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
7050theend:
7051 vim_free(fname);
7052# endif
7053}
7054
7055/*
7056 * :tab command
7057 */
7058 void
7059ex_tab(eap)
7060 exarg_T *eap;
7061{
7062 goto_tabpage((int)eap->line2);
7063}
7064
7065/*
7066 * :tabs command: List tabs and their contents.
7067 */
7068/*ARGSUSED*/
7069 static void
7070ex_tabs(eap)
7071 exarg_T *eap;
7072{
7073 tabpage_T *tp;
7074 win_T *wp;
7075 int tabcount = 1;
7076
7077 msg_start();
7078 msg_scroll = TRUE;
7079 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7080 {
7081 msg_putchar('\n');
7082 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7083 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7084 out_flush(); /* output one line at a time */
7085 ui_breakcheck();
7086
7087 if (tp->tp_topframe == topframe)
7088 wp = firstwin;
7089 else
7090 wp = tp->tp_firstwin;
7091 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7092 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007093 msg_puts((char_u *)"\n ");
7094 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7095 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007096 if (buf_spname(wp->w_buffer) != NULL)
7097 STRCPY(IObuff, buf_spname(wp->w_buffer));
7098 else
7099 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7100 IObuff, IOSIZE, TRUE);
7101 msg_outtrans(IObuff);
7102 out_flush(); /* output one line at a time */
7103 ui_breakcheck();
7104 }
7105 }
7106}
7107
7108#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109
7110/*
7111 * ":mode": Set screen mode.
7112 * If no argument given, just get the screen size and redraw.
7113 */
7114 static void
7115ex_mode(eap)
7116 exarg_T *eap;
7117{
7118 if (*eap->arg == NUL)
7119 shell_resized();
7120 else
7121 mch_screenmode(eap->arg);
7122}
7123
7124#ifdef FEAT_WINDOWS
7125/*
7126 * ":resize".
7127 * set, increment or decrement current window height
7128 */
7129 static void
7130ex_resize(eap)
7131 exarg_T *eap;
7132{
7133 int n;
7134 win_T *wp = curwin;
7135
7136 if (eap->addr_count > 0)
7137 {
7138 n = eap->line2;
7139 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7140 ;
7141 }
7142
7143#ifdef FEAT_GUI
7144 need_mouse_correct = TRUE;
7145#endif
7146 n = atol((char *)eap->arg);
7147#ifdef FEAT_VERTSPLIT
7148 if (cmdmod.split & WSP_VERT)
7149 {
7150 if (*eap->arg == '-' || *eap->arg == '+')
7151 n += W_WIDTH(curwin);
7152 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7153 n = 9999;
7154 win_setwidth_win((int)n, wp);
7155 }
7156 else
7157#endif
7158 {
7159 if (*eap->arg == '-' || *eap->arg == '+')
7160 n += curwin->w_height;
7161 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7162 n = 9999;
7163 win_setheight_win((int)n, wp);
7164 }
7165}
7166#endif
7167
7168/*
7169 * ":find [+command] <file>" command.
7170 */
7171 static void
7172ex_find(eap)
7173 exarg_T *eap;
7174{
7175#ifdef FEAT_SEARCHPATH
7176 char_u *fname;
7177 int count;
7178
7179 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7180 TRUE, curbuf->b_ffname);
7181 if (eap->addr_count > 0)
7182 {
7183 /* Repeat finding the file "count" times. This matters when it
7184 * appears several times in the path. */
7185 count = eap->line2;
7186 while (fname != NULL && --count > 0)
7187 {
7188 vim_free(fname);
7189 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7190 FALSE, curbuf->b_ffname);
7191 }
7192 }
7193
7194 if (fname != NULL)
7195 {
7196 eap->arg = fname;
7197#endif
7198 do_exedit(eap, NULL);
7199#ifdef FEAT_SEARCHPATH
7200 vim_free(fname);
7201 }
7202#endif
7203}
7204
7205/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007206 * ":open" simulation: for now just work like ":visual".
7207 */
7208 static void
7209ex_open(eap)
7210 exarg_T *eap;
7211{
7212 regmatch_T regmatch;
7213 char_u *p;
7214
7215 curwin->w_cursor.lnum = eap->line2;
7216 beginline(BL_SOL | BL_FIX);
7217 if (*eap->arg == '/')
7218 {
7219 /* ":open /pattern/": put cursor in column found with pattern */
7220 ++eap->arg;
7221 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7222 *p = NUL;
7223 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7224 if (regmatch.regprog != NULL)
7225 {
7226 regmatch.rm_ic = p_ic;
7227 p = ml_get_curline();
7228 if (vim_regexec(&regmatch, p, (colnr_T)0))
7229 curwin->w_cursor.col = regmatch.startp[0] - p;
7230 else
7231 EMSG(_(e_nomatch));
7232 vim_free(regmatch.regprog);
7233 }
7234 /* Move to the NUL, ignore any other arguments. */
7235 eap->arg += STRLEN(eap->arg);
7236 }
7237 check_cursor();
7238
7239 eap->cmdidx = CMD_visual;
7240 do_exedit(eap, NULL);
7241}
7242
7243/*
7244 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 */
7246 static void
7247ex_edit(eap)
7248 exarg_T *eap;
7249{
7250 do_exedit(eap, NULL);
7251}
7252
7253/*
7254 * ":edit <file>" command and alikes.
7255 */
7256/*ARGSUSED*/
7257 void
7258do_exedit(eap, old_curwin)
7259 exarg_T *eap;
7260 win_T *old_curwin; /* curwin before doing a split or NULL */
7261{
7262 int n;
7263#ifdef FEAT_WINDOWS
7264 int need_hide;
7265#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007266 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267
7268 /*
7269 * ":vi" command ends Ex mode.
7270 */
7271 if (exmode_active && (eap->cmdidx == CMD_visual
7272 || eap->cmdidx == CMD_view))
7273 {
7274 exmode_active = FALSE;
7275 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007276 {
7277 /* Special case: ":global/pat/visual\NLvi-commands" */
7278 if (global_busy)
7279 {
7280 int rd = RedrawingDisabled;
7281 int nwr = no_wait_return;
7282 int ms = msg_scroll;
7283#ifdef FEAT_GUI
7284 int he = hold_gui_events;
7285#endif
7286
7287 if (eap->nextcmd != NULL)
7288 {
7289 stuffReadbuff(eap->nextcmd);
7290 eap->nextcmd = NULL;
7291 }
7292
7293 if (exmode_was != EXMODE_VIM)
7294 settmode(TMODE_RAW);
7295 RedrawingDisabled = 0;
7296 no_wait_return = 0;
7297 need_wait_return = FALSE;
7298 msg_scroll = 0;
7299#ifdef FEAT_GUI
7300 hold_gui_events = 0;
7301#endif
7302 must_redraw = CLEAR;
7303
7304 main_loop(FALSE, TRUE);
7305
7306 RedrawingDisabled = rd;
7307 no_wait_return = nwr;
7308 msg_scroll = ms;
7309#ifdef FEAT_GUI
7310 hold_gui_events = he;
7311#endif
7312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 }
7316
7317 if ((eap->cmdidx == CMD_new
7318#ifdef FEAT_VERTSPLIT
7319 || eap->cmdidx == CMD_vnew
7320#endif
7321 ) && *eap->arg == NUL)
7322 {
7323 /* ":new" without argument: edit an new empty buffer */
7324 setpcmark();
7325 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
7326 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
7327 }
7328 else if ((eap->cmdidx != CMD_split
7329#ifdef FEAT_VERTSPLIT
7330 && eap->cmdidx != CMD_vsplit
7331#endif
7332 )
7333 || *eap->arg != NUL
7334#ifdef FEAT_BROWSE
7335 || cmdmod.browse
7336#endif
7337 )
7338 {
7339 n = readonlymode;
7340 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7341 readonlymode = TRUE;
7342 else if (eap->cmdidx == CMD_enew)
7343 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7344 empty buffer */
7345 setpcmark();
7346 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7347 NULL, eap,
7348 /* ":edit" goes to first line if Vi compatible */
7349 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7350 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7351 ? ECMD_ONE : eap->do_ecmd_lnum,
7352 (P_HID(curbuf) ? ECMD_HIDE : 0)
7353 + (eap->forceit ? ECMD_FORCEIT : 0)
7354#ifdef FEAT_LISTCMDS
7355 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7356#endif
7357 ) == FAIL)
7358 {
7359 /* Editing the file failed. If the window was split, close it. */
7360#ifdef FEAT_WINDOWS
7361 if (old_curwin != NULL)
7362 {
7363 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7364 if (!need_hide || P_HID(curbuf))
7365 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007366# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7367 cleanup_T cs;
7368
7369 /* Reset the error/interrupt/exception state here so that
7370 * aborting() returns FALSE when closing a window. */
7371 enter_cleanup(&cs);
7372# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373# ifdef FEAT_GUI
7374 need_mouse_correct = TRUE;
7375# endif
7376 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007377
7378# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7379 /* Restore the error/interrupt/exception state if not
7380 * discarded by a new aborting error, interrupt, or
7381 * uncaught exception. */
7382 leave_cleanup(&cs);
7383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 }
7385 }
7386#endif
7387 }
7388 else if (readonlymode && curbuf->b_nwindows == 1)
7389 {
7390 /* When editing an already visited buffer, 'readonly' won't be set
7391 * but the previous value is kept. With ":view" and ":sview" we
7392 * want the file to be readonly, except when another window is
7393 * editing the same buffer. */
7394 curbuf->b_p_ro = TRUE;
7395 }
7396 readonlymode = n;
7397 }
7398 else
7399 {
7400 if (eap->do_ecmd_cmd != NULL)
7401 do_cmdline_cmd(eap->do_ecmd_cmd);
7402#ifdef FEAT_TITLE
7403 n = curwin->w_arg_idx_invalid;
7404#endif
7405 check_arg_idx(curwin);
7406#ifdef FEAT_TITLE
7407 if (n != curwin->w_arg_idx_invalid)
7408 maketitle();
7409#endif
7410 }
7411
7412#ifdef FEAT_WINDOWS
7413 /*
7414 * if ":split file" worked, set alternate file name in old window to new
7415 * file
7416 */
7417 if (old_curwin != NULL
7418 && *eap->arg != NUL
7419 && curwin != old_curwin
7420 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007421 && old_curwin->w_buffer != curbuf
7422 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 old_curwin->w_alt_fnum = curbuf->b_fnum;
7424#endif
7425
7426 ex_no_reprint = TRUE;
7427}
7428
7429#ifndef FEAT_GUI
7430/*
7431 * ":gui" and ":gvim" when there is no GUI.
7432 */
7433 static void
7434ex_nogui(eap)
7435 exarg_T *eap;
7436{
7437 eap->errmsg = e_nogvim;
7438}
7439#endif
7440
7441#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7442 static void
7443ex_tearoff(eap)
7444 exarg_T *eap;
7445{
7446 gui_make_tearoff(eap->arg);
7447}
7448#endif
7449
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007450#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 static void
7452ex_popup(eap)
7453 exarg_T *eap;
7454{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007455 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456}
7457#endif
7458
7459/*ARGSUSED*/
7460 static void
7461ex_swapname(eap)
7462 exarg_T *eap;
7463{
7464 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7465 MSG(_("No swap file"));
7466 else
7467 msg(curbuf->b_ml.ml_mfp->mf_fname);
7468}
7469
7470/*
7471 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7472 * offset.
7473 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7474 */
7475/*ARGSUSED*/
7476 static void
7477ex_syncbind(eap)
7478 exarg_T *eap;
7479{
7480#ifdef FEAT_SCROLLBIND
7481 win_T *wp;
7482 long topline;
7483 long y;
7484 linenr_T old_linenr = curwin->w_cursor.lnum;
7485
7486 setpcmark();
7487
7488 /*
7489 * determine max topline
7490 */
7491 if (curwin->w_p_scb)
7492 {
7493 topline = curwin->w_topline;
7494 for (wp = firstwin; wp; wp = wp->w_next)
7495 {
7496 if (wp->w_p_scb && wp->w_buffer)
7497 {
7498 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7499 if (topline > y)
7500 topline = y;
7501 }
7502 }
7503 if (topline < 1)
7504 topline = 1;
7505 }
7506 else
7507 {
7508 topline = 1;
7509 }
7510
7511
7512 /*
7513 * set all scrollbind windows to the same topline
7514 */
7515 wp = curwin;
7516 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7517 {
7518 if (curwin->w_p_scb)
7519 {
7520 y = topline - curwin->w_topline;
7521 if (y > 0)
7522 scrollup(y, TRUE);
7523 else
7524 scrolldown(-y, TRUE);
7525 curwin->w_scbind_pos = topline;
7526 redraw_later(VALID);
7527 cursor_correct();
7528#ifdef FEAT_WINDOWS
7529 curwin->w_redr_status = TRUE;
7530#endif
7531 }
7532 }
7533 curwin = wp;
7534 if (curwin->w_p_scb)
7535 {
7536 did_syncbind = TRUE;
7537 checkpcmark();
7538 if (old_linenr != curwin->w_cursor.lnum)
7539 {
7540 char_u ctrl_o[2];
7541
7542 ctrl_o[0] = Ctrl_O;
7543 ctrl_o[1] = 0;
7544 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7545 }
7546 }
7547#endif
7548}
7549
7550
7551 static void
7552ex_read(eap)
7553 exarg_T *eap;
7554{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007555 int i;
7556 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7557 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558
7559 if (eap->usefilter) /* :r!cmd */
7560 do_bang(1, eap, FALSE, FALSE, TRUE);
7561 else
7562 {
7563 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7564 return;
7565
7566#ifdef FEAT_BROWSE
7567 if (cmdmod.browse)
7568 {
7569 char_u *browseFile;
7570
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007571 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 NULL, NULL, NULL, curbuf);
7573 if (browseFile != NULL)
7574 {
7575 i = readfile(browseFile, NULL,
7576 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7577 vim_free(browseFile);
7578 }
7579 else
7580 i = OK;
7581 }
7582 else
7583#endif
7584 if (*eap->arg == NUL)
7585 {
7586 if (check_fname() == FAIL) /* check for no file name */
7587 return;
7588 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7589 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7590 }
7591 else
7592 {
7593 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7594 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7595 i = readfile(eap->arg, NULL,
7596 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7597
7598 }
7599 if (i == FAIL)
7600 {
7601#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7602 if (!aborting())
7603#endif
7604 EMSG2(_(e_notopen), eap->arg);
7605 }
7606 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007607 {
7608 if (empty && exmode_active)
7609 {
7610 /* Delete the empty line that remains. Historically ex does
7611 * this but vi doesn't. */
7612 if (eap->line2 == 0)
7613 lnum = curbuf->b_ml.ml_line_count;
7614 else
7615 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007616 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007617 {
7618 ml_delete(lnum, FALSE);
7619 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007620 if (curwin->w_cursor.lnum > 1
7621 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007622 --curwin->w_cursor.lnum;
7623 }
7624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 }
7628}
7629
Bram Moolenaarea408852005-06-25 22:49:46 +00007630static char_u *prev_dir = NULL;
7631
7632#if defined(EXITFREE) || defined(PROTO)
7633 void
7634free_cd_dir()
7635{
7636 vim_free(prev_dir);
7637}
7638#endif
7639
7640
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641/*
7642 * ":cd", ":lcd", ":chdir" and ":lchdir".
7643 */
7644 static void
7645ex_cd(eap)
7646 exarg_T *eap;
7647{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 char_u *new_dir;
7649 char_u *tofree;
7650
7651 new_dir = eap->arg;
7652#if !defined(UNIX) && !defined(VMS)
7653 /* for non-UNIX ":cd" means: print current directory */
7654 if (*new_dir == NUL)
7655 ex_pwd(NULL);
7656 else
7657#endif
7658 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007659 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7660 && !eap->forceit)
7661 {
7662 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7663 return;
7664 }
7665
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 /* ":cd -": Change to previous directory */
7667 if (STRCMP(new_dir, "-") == 0)
7668 {
7669 if (prev_dir == NULL)
7670 {
7671 EMSG(_("E186: No previous directory"));
7672 return;
7673 }
7674 new_dir = prev_dir;
7675 }
7676
7677 /* Save current directory for next ":cd -" */
7678 tofree = prev_dir;
7679 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7680 prev_dir = vim_strsave(NameBuff);
7681 else
7682 prev_dir = NULL;
7683
7684#if defined(UNIX) || defined(VMS)
7685 /* for UNIX ":cd" means: go to home directory */
7686 if (*new_dir == NUL)
7687 {
7688 /* use NameBuff for home directory name */
7689# ifdef VMS
7690 char_u *p;
7691
7692 p = mch_getenv((char_u *)"SYS$LOGIN");
7693 if (p == NULL || *p == NUL) /* empty is the same as not set */
7694 NameBuff[0] = NUL;
7695 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007696 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697# else
7698 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7699# endif
7700 new_dir = NameBuff;
7701 }
7702#endif
7703 if (new_dir == NULL || vim_chdir(new_dir))
7704 EMSG(_(e_failed));
7705 else
7706 {
7707 vim_free(curwin->w_localdir);
7708 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7709 {
7710 /* If still in global directory, need to remember current
7711 * directory as global directory. */
7712 if (globaldir == NULL && prev_dir != NULL)
7713 globaldir = vim_strsave(prev_dir);
7714 /* Remember this local directory for the window. */
7715 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7716 curwin->w_localdir = vim_strsave(NameBuff);
7717 }
7718 else
7719 {
7720 /* We are now in the global directory, no need to remember its
7721 * name. */
7722 vim_free(globaldir);
7723 globaldir = NULL;
7724 curwin->w_localdir = NULL;
7725 }
7726
7727 shorten_fnames(TRUE);
7728
7729 /* Echo the new current directory if the command was typed. */
7730 if (KeyTyped)
7731 ex_pwd(eap);
7732 }
7733 vim_free(tofree);
7734 }
7735}
7736
7737/*
7738 * ":pwd".
7739 */
7740/*ARGSUSED*/
7741 static void
7742ex_pwd(eap)
7743 exarg_T *eap;
7744{
7745 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7746 {
7747#ifdef BACKSLASH_IN_FILENAME
7748 slash_adjust(NameBuff);
7749#endif
7750 msg(NameBuff);
7751 }
7752 else
7753 EMSG(_("E187: Unknown"));
7754}
7755
7756/*
7757 * ":=".
7758 */
7759 static void
7760ex_equal(eap)
7761 exarg_T *eap;
7762{
7763 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007764 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765}
7766
7767 static void
7768ex_sleep(eap)
7769 exarg_T *eap;
7770{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007771 int n;
7772 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773
7774 if (cursor_valid())
7775 {
7776 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7777 if (n >= 0)
7778 windgoto((int)n, curwin->w_wcol);
7779 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007780
7781 len = eap->line2;
7782 switch (*eap->arg)
7783 {
7784 case 'm': break;
7785 case NUL: len *= 1000L; break;
7786 default: EMSG2(_(e_invarg2), eap->arg); return;
7787 }
7788 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789}
7790
7791/*
7792 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7793 */
7794 void
7795do_sleep(msec)
7796 long msec;
7797{
7798 long done;
7799
7800 cursor_on();
7801 out_flush();
7802 for (done = 0; !got_int && done < msec; done += 1000L)
7803 {
7804 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7805 ui_breakcheck();
7806 }
7807}
7808
7809 static void
7810do_exmap(eap, isabbrev)
7811 exarg_T *eap;
7812 int isabbrev;
7813{
7814 int mode;
7815 char_u *cmdp;
7816
7817 cmdp = eap->cmd;
7818 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7819
7820 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7821 eap->arg, mode, isabbrev))
7822 {
7823 case 1: EMSG(_(e_invarg));
7824 break;
7825 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7826 break;
7827 }
7828}
7829
7830/*
7831 * ":winsize" command (obsolete).
7832 */
7833 static void
7834ex_winsize(eap)
7835 exarg_T *eap;
7836{
7837 int w, h;
7838 char_u *arg = eap->arg;
7839 char_u *p;
7840
7841 w = getdigits(&arg);
7842 arg = skipwhite(arg);
7843 p = arg;
7844 h = getdigits(&arg);
7845 if (*p != NUL && *arg == NUL)
7846 set_shellsize(w, h, TRUE);
7847 else
7848 EMSG(_("E465: :winsize requires two number arguments"));
7849}
7850
7851#ifdef FEAT_WINDOWS
7852 static void
7853ex_wincmd(eap)
7854 exarg_T *eap;
7855{
7856 int xchar = NUL;
7857 char_u *p;
7858
7859 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7860 {
7861 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7862 if (eap->arg[1] == NUL)
7863 {
7864 EMSG(_(e_invarg));
7865 return;
7866 }
7867 xchar = eap->arg[1];
7868 p = eap->arg + 2;
7869 }
7870 else
7871 p = eap->arg + 1;
7872
7873 eap->nextcmd = check_nextcmd(p);
7874 p = skipwhite(p);
7875 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7876 EMSG(_(e_invarg));
7877 else
7878 {
7879 /* Pass flags on for ":vertical wincmd ]". */
7880 postponed_split_flags = cmdmod.split;
7881 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7882 postponed_split_flags = 0;
7883 }
7884}
7885#endif
7886
Bram Moolenaar843ee412004-06-30 16:16:41 +00007887#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888/*
7889 * ":winpos".
7890 */
7891 static void
7892ex_winpos(eap)
7893 exarg_T *eap;
7894{
7895 int x, y;
7896 char_u *arg = eap->arg;
7897 char_u *p;
7898
7899 if (*arg == NUL)
7900 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007901# if defined(FEAT_GUI) || defined(MSWIN)
7902# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007904# else
7905 if (mch_get_winpos(&x, &y) != FAIL)
7906# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 {
7908 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7909 msg(IObuff);
7910 }
7911 else
7912# endif
7913 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7914 }
7915 else
7916 {
7917 x = getdigits(&arg);
7918 arg = skipwhite(arg);
7919 p = arg;
7920 y = getdigits(&arg);
7921 if (*p == NUL || *arg != NUL)
7922 {
7923 EMSG(_("E466: :winpos requires two number arguments"));
7924 return;
7925 }
7926# ifdef FEAT_GUI
7927 if (gui.in_use)
7928 gui_mch_set_winpos(x, y);
7929 else if (gui.starting)
7930 {
7931 /* Remember the coordinates for when the window is opened. */
7932 gui_win_x = x;
7933 gui_win_y = y;
7934 }
7935# ifdef HAVE_TGETENT
7936 else
7937# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007938# else
7939# ifdef MSWIN
7940 mch_set_winpos(x, y);
7941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942# endif
7943# ifdef HAVE_TGETENT
7944 if (*T_CWP)
7945 term_set_winpos(x, y);
7946# endif
7947 }
7948}
7949#endif
7950
7951/*
7952 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7953 */
7954 static void
7955ex_operators(eap)
7956 exarg_T *eap;
7957{
7958 oparg_T oa;
7959
7960 clear_oparg(&oa);
7961 oa.regname = eap->regname;
7962 oa.start.lnum = eap->line1;
7963 oa.end.lnum = eap->line2;
7964 oa.line_count = eap->line2 - eap->line1 + 1;
7965 oa.motion_type = MLINE;
7966#ifdef FEAT_VIRTUALEDIT
7967 virtual_op = FALSE;
7968#endif
7969 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7970 {
7971 setpcmark();
7972 curwin->w_cursor.lnum = eap->line1;
7973 beginline(BL_SOL | BL_FIX);
7974 }
7975
7976 switch (eap->cmdidx)
7977 {
7978 case CMD_delete:
7979 oa.op_type = OP_DELETE;
7980 op_delete(&oa);
7981 break;
7982
7983 case CMD_yank:
7984 oa.op_type = OP_YANK;
7985 (void)op_yank(&oa, FALSE, TRUE);
7986 break;
7987
7988 default: /* CMD_rshift or CMD_lshift */
7989 if ((eap->cmdidx == CMD_rshift)
7990#ifdef FEAT_RIGHTLEFT
7991 ^ curwin->w_p_rl
7992#endif
7993 )
7994 oa.op_type = OP_RSHIFT;
7995 else
7996 oa.op_type = OP_LSHIFT;
7997 op_shift(&oa, FALSE, eap->amount);
7998 break;
7999 }
8000#ifdef FEAT_VIRTUALEDIT
8001 virtual_op = MAYBE;
8002#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008003 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004}
8005
8006/*
8007 * ":put".
8008 */
8009 static void
8010ex_put(eap)
8011 exarg_T *eap;
8012{
8013 /* ":0put" works like ":1put!". */
8014 if (eap->line2 == 0)
8015 {
8016 eap->line2 = 1;
8017 eap->forceit = TRUE;
8018 }
8019 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008020 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8021 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022}
8023
8024/*
8025 * Handle ":copy" and ":move".
8026 */
8027 static void
8028ex_copymove(eap)
8029 exarg_T *eap;
8030{
8031 long n;
8032
8033 n = get_address(&eap->arg, FALSE, FALSE);
8034 if (eap->arg == NULL) /* error detected */
8035 {
8036 eap->nextcmd = NULL;
8037 return;
8038 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008039 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040
8041 /*
8042 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8043 */
8044 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8045 {
8046 EMSG(_(e_invaddr));
8047 return;
8048 }
8049
8050 if (eap->cmdidx == CMD_move)
8051 {
8052 if (do_move(eap->line1, eap->line2, n) == FAIL)
8053 return;
8054 }
8055 else
8056 ex_copy(eap->line1, eap->line2, n);
8057 u_clearline();
8058 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008059 ex_may_print(eap);
8060}
8061
8062/*
8063 * Print the current line if flags were given to the Ex command.
8064 */
8065 static void
8066ex_may_print(eap)
8067 exarg_T *eap;
8068{
8069 if (eap->flags != 0)
8070 {
8071 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8072 (eap->flags & EXFLAG_LIST));
8073 ex_no_reprint = TRUE;
8074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075}
8076
8077/*
8078 * ":smagic" and ":snomagic".
8079 */
8080 static void
8081ex_submagic(eap)
8082 exarg_T *eap;
8083{
8084 int magic_save = p_magic;
8085
8086 p_magic = (eap->cmdidx == CMD_smagic);
8087 do_sub(eap);
8088 p_magic = magic_save;
8089}
8090
8091/*
8092 * ":join".
8093 */
8094 static void
8095ex_join(eap)
8096 exarg_T *eap;
8097{
8098 curwin->w_cursor.lnum = eap->line1;
8099 if (eap->line1 == eap->line2)
8100 {
8101 if (eap->addr_count >= 2) /* :2,2join does nothing */
8102 return;
8103 if (eap->line2 == curbuf->b_ml.ml_line_count)
8104 {
8105 beep_flush();
8106 return;
8107 }
8108 ++eap->line2;
8109 }
8110 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8111 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008112 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113}
8114
8115/*
8116 * ":[addr]@r" or ":[addr]*r": execute register
8117 */
8118 static void
8119ex_at(eap)
8120 exarg_T *eap;
8121{
8122 int c;
8123
8124 curwin->w_cursor.lnum = eap->line2;
8125
8126#ifdef USE_ON_FLY_SCROLL
8127 dont_scroll = TRUE; /* disallow scrolling here */
8128#endif
8129
8130 /* get the register name. No name means to use the previous one */
8131 c = *eap->arg;
8132 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8133 c = '@';
8134 /* put the register in mapbuf */
8135 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 else
8140 {
8141 int save_efr = exec_from_reg;
8142
8143 exec_from_reg = TRUE;
8144
8145 /*
8146 * Execute from the typeahead buffer.
8147 * Originally this didn't check for the typeahead buffer to be empty,
8148 * thus could read more Ex commands from stdin. It's not clear why,
8149 * it is certainly unexpected.
8150 */
8151 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8152 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8153
8154 exec_from_reg = save_efr;
8155 }
8156}
8157
8158/*
8159 * ":!".
8160 */
8161 static void
8162ex_bang(eap)
8163 exarg_T *eap;
8164{
8165 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8166}
8167
8168/*
8169 * ":undo".
8170 */
8171/*ARGSUSED*/
8172 static void
8173ex_undo(eap)
8174 exarg_T *eap;
8175{
8176 u_undo(1);
8177}
8178
8179/*
8180 * ":redo".
8181 */
8182/*ARGSUSED*/
8183 static void
8184ex_redo(eap)
8185 exarg_T *eap;
8186{
8187 u_redo(1);
8188}
8189
8190/*
8191 * ":redir": start/stop redirection.
8192 */
8193 static void
8194ex_redir(eap)
8195 exarg_T *eap;
8196{
8197 char *mode;
8198 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008199 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200
8201 if (STRICMP(eap->arg, "END") == 0)
8202 close_redir();
8203 else
8204 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008205 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008207 ++arg;
8208 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008210 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 mode = "a";
8212 }
8213 else
8214 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008215 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216
8217 close_redir();
8218
8219 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008220 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 if (fname == NULL)
8222 return;
8223#ifdef FEAT_BROWSE
8224 if (cmdmod.browse)
8225 {
8226 char_u *browseFile;
8227
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008228 browseFile = do_browse(BROWSE_SAVE,
8229 (char_u *)_("Save Redirection"),
8230 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 if (browseFile == NULL)
8232 return; /* operation cancelled */
8233 vim_free(fname);
8234 fname = browseFile;
8235 eap->forceit = TRUE; /* since dialog already asked */
8236 }
8237#endif
8238
8239 redir_fd = open_exfile(fname, eap->forceit, mode);
8240 vim_free(fname);
8241 }
8242#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008243 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 {
8245 /* redirect to a register a-z (resp. A-Z for appending) */
8246 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008247 ++arg;
8248 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008250 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008252 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008254 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008255 if (*arg == '>' && arg[1] == '>')
8256 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008257 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
8258 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008260 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008262 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008264 if (*arg == '>')
8265 arg++;
8266
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 /* make register empty */
8268 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
8269 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008270 }
8271 if (*arg != NUL)
8272 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008273 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008274 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008275 }
8276 }
8277 else if (*arg == '=' && arg[1] == '>')
8278 {
8279 int append;
8280
8281 /* redirect to a variable */
8282 close_redir();
8283 arg += 2;
8284
8285 if (*arg == '>')
8286 {
8287 ++arg;
8288 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 }
8290 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008291 append = FALSE;
8292
8293 if (var_redir_start(skipwhite(arg), append) == OK)
8294 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 }
8296#endif
8297
8298 /* TODO: redirect to a buffer */
8299
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008301 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 }
8303}
8304
8305/*
8306 * ":redraw": force redraw
8307 */
8308 static void
8309ex_redraw(eap)
8310 exarg_T *eap;
8311{
8312 int r = RedrawingDisabled;
8313 int p = p_lz;
8314
8315 RedrawingDisabled = 0;
8316 p_lz = FALSE;
8317 update_topline();
8318 update_screen(eap->forceit ? CLEAR :
8319#ifdef FEAT_VISUAL
8320 VIsual_active ? INVERTED :
8321#endif
8322 0);
8323#ifdef FEAT_TITLE
8324 if (need_maketitle)
8325 maketitle();
8326#endif
8327 RedrawingDisabled = r;
8328 p_lz = p;
8329
8330 /* Reset msg_didout, so that a message that's there is overwritten. */
8331 msg_didout = FALSE;
8332 msg_col = 0;
8333
8334 out_flush();
8335}
8336
8337/*
8338 * ":redrawstatus": force redraw of status line(s)
8339 */
8340/*ARGSUSED*/
8341 static void
8342ex_redrawstatus(eap)
8343 exarg_T *eap;
8344{
8345#if defined(FEAT_WINDOWS)
8346 int r = RedrawingDisabled;
8347 int p = p_lz;
8348
8349 RedrawingDisabled = 0;
8350 p_lz = FALSE;
8351 if (eap->forceit)
8352 status_redraw_all();
8353 else
8354 status_redraw_curbuf();
8355 update_screen(
8356# ifdef FEAT_VISUAL
8357 VIsual_active ? INVERTED :
8358# endif
8359 0);
8360 RedrawingDisabled = r;
8361 p_lz = p;
8362 out_flush();
8363#endif
8364}
8365
8366 static void
8367close_redir()
8368{
8369 if (redir_fd != NULL)
8370 {
8371 fclose(redir_fd);
8372 redir_fd = NULL;
8373 }
8374#ifdef FEAT_EVAL
8375 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008376 if (redir_vname)
8377 {
8378 var_redir_stop();
8379 redir_vname = 0;
8380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381#endif
8382}
8383
8384#if defined(FEAT_SESSION) && defined(USE_CRNL)
8385# define MKSESSION_NL
8386static int mksession_nl = FALSE; /* use NL only in put_eol() */
8387#endif
8388
8389/*
8390 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8391 */
8392 static void
8393ex_mkrc(eap)
8394 exarg_T *eap;
8395{
8396 FILE *fd;
8397 int failed = FALSE;
8398 char_u *fname;
8399#ifdef FEAT_BROWSE
8400 char_u *browseFile = NULL;
8401#endif
8402#ifdef FEAT_SESSION
8403 int view_session = FALSE;
8404 int using_vdir = FALSE; /* using 'viewdir'? */
8405 char_u *viewFile = NULL;
8406 unsigned *flagp;
8407#endif
8408
8409 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8410 {
8411#ifdef FEAT_SESSION
8412 view_session = TRUE;
8413#else
8414 ex_ni(eap);
8415 return;
8416#endif
8417 }
8418
8419#ifdef FEAT_SESSION
8420 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8421 if (eap->cmdidx == CMD_mkview
8422 && (*eap->arg == NUL
8423 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8424 {
8425 eap->forceit = TRUE;
8426 fname = get_view_file(*eap->arg);
8427 if (fname == NULL)
8428 return;
8429 viewFile = fname;
8430 using_vdir = TRUE;
8431 }
8432 else
8433#endif
8434 if (*eap->arg != NUL)
8435 fname = eap->arg;
8436 else if (eap->cmdidx == CMD_mkvimrc)
8437 fname = (char_u *)VIMRC_FILE;
8438#ifdef FEAT_SESSION
8439 else if (eap->cmdidx == CMD_mksession)
8440 fname = (char_u *)SESSION_FILE;
8441#endif
8442 else
8443 fname = (char_u *)EXRC_FILE;
8444
8445#ifdef FEAT_BROWSE
8446 if (cmdmod.browse)
8447 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008448 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449# ifdef FEAT_SESSION
8450 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8451 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8452# endif
8453 (char_u *)_("Save Setup"),
8454 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8455 if (browseFile == NULL)
8456 goto theend;
8457 fname = browseFile;
8458 eap->forceit = TRUE; /* since dialog already asked */
8459 }
8460#endif
8461
8462#if defined(FEAT_SESSION) && defined(vim_mkdir)
8463 /* When using 'viewdir' may have to create the directory. */
8464 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008465 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466#endif
8467
8468 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8469 if (fd != NULL)
8470 {
8471#ifdef FEAT_SESSION
8472 if (eap->cmdidx == CMD_mkview)
8473 flagp = &vop_flags;
8474 else
8475 flagp = &ssop_flags;
8476#endif
8477
8478#ifdef MKSESSION_NL
8479 /* "unix" in 'sessionoptions': use NL line separator */
8480 if (view_session && (*flagp & SSOP_UNIX))
8481 mksession_nl = TRUE;
8482#endif
8483
8484 /* Write the version command for :mkvimrc */
8485 if (eap->cmdidx == CMD_mkvimrc)
8486 (void)put_line(fd, "version 6.0");
8487
8488#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008489 if (eap->cmdidx == CMD_mksession)
8490 {
8491 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8492 failed = TRUE;
8493 }
8494
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 if (eap->cmdidx != CMD_mkview)
8496#endif
8497 {
8498 /* Write setting 'compatible' first, because it has side effects.
8499 * For that same reason only do it when needed. */
8500 if (p_cp)
8501 (void)put_line(fd, "if !&cp | set cp | endif");
8502 else
8503 (void)put_line(fd, "if &cp | set nocp | endif");
8504 }
8505
8506#ifdef FEAT_SESSION
8507 if (!view_session
8508 || (eap->cmdidx == CMD_mksession
8509 && (*flagp & SSOP_OPTIONS)))
8510#endif
8511 failed |= (makemap(fd, NULL) == FAIL
8512 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8513
8514#ifdef FEAT_SESSION
8515 if (!failed && view_session)
8516 {
8517 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8518 failed = TRUE;
8519 if (eap->cmdidx == CMD_mksession)
8520 {
8521 char_u dirnow[MAXPATHL]; /* current directory */
8522
8523 /*
8524 * Change to session file's dir.
8525 */
8526 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8527 || mch_chdir((char *)dirnow) != 0)
8528 *dirnow = NUL;
8529 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8530 {
8531 if (vim_chdirfile(fname) == OK)
8532 shorten_fnames(TRUE);
8533 }
8534 else if (*dirnow != NUL
8535 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8536 {
8537 (void)mch_chdir((char *)globaldir);
8538 shorten_fnames(TRUE);
8539 }
8540
8541 failed |= (makeopens(fd, dirnow) == FAIL);
8542
8543 /* restore original dir */
8544 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8545 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8546 {
8547 if (mch_chdir((char *)dirnow) != 0)
8548 EMSG(_(e_prev_dir));
8549 shorten_fnames(TRUE);
8550 }
8551 }
8552 else
8553 {
8554 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8555 }
8556 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8557 == FAIL)
8558 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008559 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8560 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008561 if (eap->cmdidx == CMD_mksession)
8562 {
8563 if (put_line(fd, "unlet SessionLoad") == FAIL)
8564 failed = TRUE;
8565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 }
8567#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008568 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8569 failed = TRUE;
8570
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 failed |= fclose(fd);
8572
8573 if (failed)
8574 EMSG(_(e_write));
8575#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8576 else if (eap->cmdidx == CMD_mksession)
8577 {
8578 /* successful session write - set this_session var */
8579 char_u tbuf[MAXPATHL];
8580
8581 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8582 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8583 }
8584#endif
8585#ifdef MKSESSION_NL
8586 mksession_nl = FALSE;
8587#endif
8588 }
8589
8590#ifdef FEAT_BROWSE
8591theend:
8592 vim_free(browseFile);
8593#endif
8594#ifdef FEAT_SESSION
8595 vim_free(viewFile);
8596#endif
8597}
8598
Bram Moolenaardf177f62005-02-22 08:39:57 +00008599#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8600 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008601/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008602 int
8603vim_mkdir_emsg(name, prot)
8604 char_u *name;
8605 int prot;
8606{
8607 if (vim_mkdir(name, prot) != 0)
8608 {
8609 EMSG2(_("E739: Cannot create directory: %s"), name);
8610 return FAIL;
8611 }
8612 return OK;
8613}
8614#endif
8615
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616/*
8617 * Open a file for writing for an Ex command, with some checks.
8618 * Return file descriptor, or NULL on failure.
8619 */
8620 FILE *
8621open_exfile(fname, forceit, mode)
8622 char_u *fname;
8623 int forceit;
8624 char *mode; /* "w" for create new file or "a" for append */
8625{
8626 FILE *fd;
8627
8628#ifdef UNIX
8629 /* with Unix it is possible to open a directory */
8630 if (mch_isdir(fname))
8631 {
8632 EMSG2(_(e_isadir2), fname);
8633 return NULL;
8634 }
8635#endif
8636 if (!forceit && *mode != 'a' && vim_fexists(fname))
8637 {
8638 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8639 return NULL;
8640 }
8641
8642 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8643 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8644
8645 return fd;
8646}
8647
8648/*
8649 * ":mark" and ":k".
8650 */
8651 static void
8652ex_mark(eap)
8653 exarg_T *eap;
8654{
8655 pos_T pos;
8656
8657 if (*eap->arg == NUL) /* No argument? */
8658 EMSG(_(e_argreq));
8659 else if (eap->arg[1] != NUL) /* more than one character? */
8660 EMSG(_(e_trailing));
8661 else
8662 {
8663 pos = curwin->w_cursor; /* save curwin->w_cursor */
8664 curwin->w_cursor.lnum = eap->line2;
8665 beginline(BL_WHITE | BL_FIX);
8666 if (setmark(*eap->arg) == FAIL) /* set mark */
8667 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8668 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8669 }
8670}
8671
8672/*
8673 * Update w_topline, w_leftcol and the cursor position.
8674 */
8675 void
8676update_topline_cursor()
8677{
8678 check_cursor(); /* put cursor on valid line */
8679 update_topline();
8680 if (!curwin->w_p_wrap)
8681 validate_cursor();
8682 update_curswant();
8683}
8684
8685#ifdef FEAT_EX_EXTRA
8686/*
8687 * ":normal[!] {commands}": Execute normal mode commands.
8688 */
8689 static void
8690ex_normal(eap)
8691 exarg_T *eap;
8692{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 int save_msg_scroll = msg_scroll;
8694 int save_restart_edit = restart_edit;
8695 int save_msg_didout = msg_didout;
8696 int save_State = State;
8697 tasave_T tabuf;
8698 int save_insertmode = p_im;
8699 int save_finish_op = finish_op;
8700#ifdef FEAT_MBYTE
8701 char_u *arg = NULL;
8702 int l;
8703 char_u *p;
8704#endif
8705
8706 if (ex_normal_busy >= p_mmd)
8707 {
8708 EMSG(_("E192: Recursive use of :normal too deep"));
8709 return;
8710 }
8711 ++ex_normal_busy;
8712
8713 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8714 restart_edit = 0; /* don't go to Insert mode */
8715 p_im = FALSE; /* don't use 'insertmode' */
8716
8717#ifdef FEAT_MBYTE
8718 /*
8719 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8720 * this for the K_SPECIAL leading byte, otherwise special keys will not
8721 * work.
8722 */
8723 if (has_mbyte)
8724 {
8725 int len = 0;
8726
8727 /* Count the number of characters to be escaped. */
8728 for (p = eap->arg; *p != NUL; ++p)
8729 {
8730# ifdef FEAT_GUI
8731 if (*p == CSI) /* leadbyte CSI */
8732 len += 2;
8733# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008734 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8736# ifdef FEAT_GUI
8737 || *p == CSI
8738# endif
8739 )
8740 len += 2;
8741 }
8742 if (len > 0)
8743 {
8744 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8745 if (arg != NULL)
8746 {
8747 len = 0;
8748 for (p = eap->arg; *p != NUL; ++p)
8749 {
8750 arg[len++] = *p;
8751# ifdef FEAT_GUI
8752 if (*p == CSI)
8753 {
8754 arg[len++] = KS_EXTRA;
8755 arg[len++] = (int)KE_CSI;
8756 }
8757# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008758 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 {
8760 arg[len++] = *++p;
8761 if (*p == K_SPECIAL)
8762 {
8763 arg[len++] = KS_SPECIAL;
8764 arg[len++] = KE_FILLER;
8765 }
8766# ifdef FEAT_GUI
8767 else if (*p == CSI)
8768 {
8769 arg[len++] = KS_EXTRA;
8770 arg[len++] = (int)KE_CSI;
8771 }
8772# endif
8773 }
8774 arg[len] = NUL;
8775 }
8776 }
8777 }
8778 }
8779#endif
8780
8781 /*
8782 * Save the current typeahead. This is required to allow using ":normal"
8783 * from an event handler and makes sure we don't hang when the argument
8784 * ends with half a command.
8785 */
8786 save_typeahead(&tabuf);
8787 if (tabuf.typebuf_valid)
8788 {
8789 /*
8790 * Repeat the :normal command for each line in the range. When no
8791 * range given, execute it just once, without positioning the cursor
8792 * first.
8793 */
8794 do
8795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 if (eap->addr_count != 0)
8797 {
8798 curwin->w_cursor.lnum = eap->line1++;
8799 curwin->w_cursor.col = 0;
8800 }
8801
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008802 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803#ifdef FEAT_MBYTE
8804 arg != NULL ? arg :
8805#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008806 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 }
8808 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8809 }
8810
8811 /* Might not return to the main loop when in an event handler. */
8812 update_topline_cursor();
8813
8814 /* Restore the previous typeahead. */
8815 restore_typeahead(&tabuf);
8816
8817 --ex_normal_busy;
8818 msg_scroll = save_msg_scroll;
8819 restart_edit = save_restart_edit;
8820 p_im = save_insertmode;
8821 finish_op = save_finish_op;
8822 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8823
8824 /* Restore the state (needed when called from a function executed for
8825 * 'indentexpr'). */
8826 State = save_State;
8827#ifdef FEAT_MBYTE
8828 vim_free(arg);
8829#endif
8830}
8831
8832/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008833 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 */
8835 static void
8836ex_startinsert(eap)
8837 exarg_T *eap;
8838{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008839 if (eap->forceit)
8840 {
8841 coladvance((colnr_T)MAXCOL);
8842 curwin->w_curswant = MAXCOL;
8843 curwin->w_set_curswant = FALSE;
8844 }
8845
Bram Moolenaara40c5002005-01-09 21:16:21 +00008846 /* Ignore the command when already in Insert mode. Inserting an
8847 * expression register that invokes a function can do this. */
8848 if (State & INSERT)
8849 return;
8850
Bram Moolenaar64969662005-12-14 21:59:55 +00008851 if (eap->cmdidx == CMD_startinsert)
8852 restart_edit = 'a';
8853 else if (eap->cmdidx == CMD_startreplace)
8854 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008856 restart_edit = 'V';
8857
8858 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008860 if (eap->cmdidx == CMD_startinsert)
8861 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 curwin->w_curswant = 0; /* avoid MAXCOL */
8863 }
8864}
8865
8866/*
8867 * ":stopinsert"
8868 */
8869/*ARGSUSED*/
8870 static void
8871ex_stopinsert(eap)
8872 exarg_T *eap;
8873{
8874 restart_edit = 0;
8875 stop_insert_mode = TRUE;
8876}
8877#endif
8878
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008879#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8880/*
8881 * Execute normal mode command "cmd".
8882 * "remap" can be REMAP_NONE or REMAP_YES.
8883 */
8884 void
8885exec_normal_cmd(cmd, remap, silent)
8886 char_u *cmd;
8887 int remap;
8888 int silent;
8889{
8890 oparg_T oa;
8891
8892 /*
8893 * Stuff the argument into the typeahead buffer.
8894 * Execute normal_cmd() until there is no typeahead left.
8895 */
8896 clear_oparg(&oa);
8897 finish_op = FALSE;
8898 ins_typebuf(cmd, remap, 0, TRUE, silent);
8899 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8900 && !got_int)
8901 {
8902 update_topline_cursor();
8903 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8904 }
8905}
8906#endif
8907
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908#ifdef FEAT_FIND_ID
8909 static void
8910ex_checkpath(eap)
8911 exarg_T *eap;
8912{
8913 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8914 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8915 (linenr_T)1, (linenr_T)MAXLNUM);
8916}
8917
8918#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8919/*
8920 * ":psearch"
8921 */
8922 static void
8923ex_psearch(eap)
8924 exarg_T *eap;
8925{
8926 g_do_tagpreview = p_pvh;
8927 ex_findpat(eap);
8928 g_do_tagpreview = 0;
8929}
8930#endif
8931
8932 static void
8933ex_findpat(eap)
8934 exarg_T *eap;
8935{
8936 int whole = TRUE;
8937 long n;
8938 char_u *p;
8939 int action;
8940
8941 switch (cmdnames[eap->cmdidx].cmd_name[2])
8942 {
8943 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8944 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8945 action = ACTION_GOTO;
8946 else
8947 action = ACTION_SHOW;
8948 break;
8949 case 'i': /* ":ilist" and ":dlist" */
8950 action = ACTION_SHOW_ALL;
8951 break;
8952 case 'u': /* ":ijump" and ":djump" */
8953 action = ACTION_GOTO;
8954 break;
8955 default: /* ":isplit" and ":dsplit" */
8956 action = ACTION_SPLIT;
8957 break;
8958 }
8959
8960 n = 1;
8961 if (vim_isdigit(*eap->arg)) /* get count */
8962 {
8963 n = getdigits(&eap->arg);
8964 eap->arg = skipwhite(eap->arg);
8965 }
8966 if (*eap->arg == '/') /* Match regexp, not just whole words */
8967 {
8968 whole = FALSE;
8969 ++eap->arg;
8970 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8971 if (*p)
8972 {
8973 *p++ = NUL;
8974 p = skipwhite(p);
8975
8976 /* Check for trailing illegal characters */
8977 if (!ends_excmd(*p))
8978 eap->errmsg = e_trailing;
8979 else
8980 eap->nextcmd = check_nextcmd(p);
8981 }
8982 }
8983 if (!eap->skip)
8984 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8985 whole, !eap->forceit,
8986 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8987 n, action, eap->line1, eap->line2);
8988}
8989#endif
8990
8991#ifdef FEAT_WINDOWS
8992
8993# ifdef FEAT_QUICKFIX
8994/*
8995 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8996 */
8997 static void
8998ex_ptag(eap)
8999 exarg_T *eap;
9000{
9001 g_do_tagpreview = p_pvh;
9002 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9003}
9004
9005/*
9006 * ":pedit"
9007 */
9008 static void
9009ex_pedit(eap)
9010 exarg_T *eap;
9011{
9012 win_T *curwin_save = curwin;
9013
9014 g_do_tagpreview = p_pvh;
9015 prepare_tagpreview();
9016 keep_help_flag = curwin_save->w_buffer->b_help;
9017 do_exedit(eap, NULL);
9018 keep_help_flag = FALSE;
9019 if (curwin != curwin_save && win_valid(curwin_save))
9020 {
9021 /* Return cursor to where we were */
9022 validate_cursor();
9023 redraw_later(VALID);
9024 win_enter(curwin_save, TRUE);
9025 }
9026 g_do_tagpreview = 0;
9027}
9028# endif
9029
9030/*
9031 * ":stag", ":stselect" and ":stjump".
9032 */
9033 static void
9034ex_stag(eap)
9035 exarg_T *eap;
9036{
9037 postponed_split = -1;
9038 postponed_split_flags = cmdmod.split;
9039 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9040 postponed_split_flags = 0;
9041}
9042#endif
9043
9044/*
9045 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9046 */
9047 static void
9048ex_tag(eap)
9049 exarg_T *eap;
9050{
9051 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9052}
9053
9054 static void
9055ex_tag_cmd(eap, name)
9056 exarg_T *eap;
9057 char_u *name;
9058{
9059 int cmd;
9060
9061 switch (name[1])
9062 {
9063 case 'j': cmd = DT_JUMP; /* ":tjump" */
9064 break;
9065 case 's': cmd = DT_SELECT; /* ":tselect" */
9066 break;
9067 case 'p': cmd = DT_PREV; /* ":tprevious" */
9068 break;
9069 case 'N': cmd = DT_PREV; /* ":tNext" */
9070 break;
9071 case 'n': cmd = DT_NEXT; /* ":tnext" */
9072 break;
9073 case 'o': cmd = DT_POP; /* ":pop" */
9074 break;
9075 case 'f': /* ":tfirst" */
9076 case 'r': cmd = DT_FIRST; /* ":trewind" */
9077 break;
9078 case 'l': cmd = DT_LAST; /* ":tlast" */
9079 break;
9080 default: /* ":tag" */
9081#ifdef FEAT_CSCOPE
9082 if (p_cst)
9083 {
9084 do_cstag(eap);
9085 return;
9086 }
9087#endif
9088 cmd = DT_TAG;
9089 break;
9090 }
9091
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009092 if (name[0] == 'l')
9093 {
9094#ifndef FEAT_QUICKFIX
9095 ex_ni(eap);
9096 return;
9097#else
9098 cmd = DT_LTAG;
9099#endif
9100 }
9101
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9103 eap->forceit, TRUE);
9104}
9105
9106/*
9107 * Evaluate cmdline variables.
9108 *
9109 * change '%' to curbuf->b_ffname
9110 * '#' to curwin->w_altfile
9111 * '<cword>' to word under the cursor
9112 * '<cWORD>' to WORD under the cursor
9113 * '<cfile>' to path name under the cursor
9114 * '<sfile>' to sourced file name
9115 * '<afile>' to file name for autocommand
9116 * '<abuf>' to buffer number for autocommand
9117 * '<amatch>' to matching name for autocommand
9118 *
9119 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9120 * "" for error without a message) and NULL is returned.
9121 * Returns an allocated string if a valid match was found.
9122 * Returns NULL if no match was found. "usedlen" then still contains the
9123 * number of characters to skip.
9124 */
9125 char_u *
9126eval_vars(src, usedlen, lnump, errormsg, srcstart)
9127 char_u *src; /* pointer into commandline */
9128 int *usedlen; /* characters after src that are used */
9129 linenr_T *lnump; /* line number for :e command, or NULL */
9130 char_u **errormsg; /* pointer to error message */
9131 char_u *srcstart; /* beginning of valid memory for src */
9132{
9133 int i;
9134 char_u *s;
9135 char_u *result;
9136 char_u *resultbuf = NULL;
9137 int resultlen;
9138 buf_T *buf;
9139 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9140 int spec_idx;
9141#ifdef FEAT_MODIFY_FNAME
9142 int skip_mod = FALSE;
9143#endif
9144 static char *(spec_str[]) =
9145 {
9146 "%",
9147#define SPEC_PERC 0
9148 "#",
9149#define SPEC_HASH 1
9150 "<cword>", /* cursor word */
9151#define SPEC_CWORD 2
9152 "<cWORD>", /* cursor WORD */
9153#define SPEC_CCWORD 3
9154 "<cfile>", /* cursor path name */
9155#define SPEC_CFILE 4
9156 "<sfile>", /* ":so" file name */
9157#define SPEC_SFILE 5
9158#ifdef FEAT_AUTOCMD
9159 "<afile>", /* autocommand file name */
9160# define SPEC_AFILE 6
9161 "<abuf>", /* autocommand buffer number */
9162# define SPEC_ABUF 7
9163 "<amatch>", /* autocommand match name */
9164# define SPEC_AMATCH 8
9165#endif
9166#ifdef FEAT_CLIENTSERVER
9167 "<client>"
9168# define SPEC_CLIENT 9
9169#endif
9170 };
9171#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9172
9173#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9174 char_u strbuf[30];
9175#endif
9176
9177 *errormsg = NULL;
9178
9179 /*
9180 * Check if there is something to do.
9181 */
9182 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
9183 {
9184 *usedlen = (int)STRLEN(spec_str[spec_idx]);
9185 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
9186 break;
9187 }
9188 if (spec_idx == SPEC_COUNT) /* no match */
9189 {
9190 *usedlen = 1;
9191 return NULL;
9192 }
9193
9194 /*
9195 * Skip when preceded with a backslash "\%" and "\#".
9196 * Note: In "\\%" the % is also not recognized!
9197 */
9198 if (src > srcstart && src[-1] == '\\')
9199 {
9200 *usedlen = 0;
9201 STRCPY(src - 1, src); /* remove backslash */
9202 return NULL;
9203 }
9204
9205 /*
9206 * word or WORD under cursor
9207 */
9208 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9209 {
9210 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9211 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9212 if (resultlen == 0)
9213 {
9214 *errormsg = (char_u *)"";
9215 return NULL;
9216 }
9217 }
9218
9219 /*
9220 * '#': Alternate file name
9221 * '%': Current file name
9222 * File name under the cursor
9223 * File name for autocommand
9224 * and following modifiers
9225 */
9226 else
9227 {
9228 switch (spec_idx)
9229 {
9230 case SPEC_PERC: /* '%': current file */
9231 if (curbuf->b_fname == NULL)
9232 {
9233 result = (char_u *)"";
9234 valid = 0; /* Must have ":p:h" to be valid */
9235 }
9236 else
9237#ifdef RISCOS
9238 /* Always use the full path for RISC OS if possible. */
9239 result = curbuf->b_ffname;
9240 if (result == NULL)
9241 result = curbuf->b_fname;
9242#else
9243 result = curbuf->b_fname;
9244#endif
9245 break;
9246
9247 case SPEC_HASH: /* '#' or "#99": alternate file */
9248 if (src[1] == '#') /* "##": the argument list */
9249 {
9250 result = arg_all();
9251 resultbuf = result;
9252 *usedlen = 2;
9253#ifdef FEAT_MODIFY_FNAME
9254 skip_mod = TRUE;
9255#endif
9256 break;
9257 }
9258 s = src + 1;
9259 i = (int)getdigits(&s);
9260 *usedlen = (int)(s - src); /* length of what we expand */
9261
9262 buf = buflist_findnr(i);
9263 if (buf == NULL)
9264 {
9265 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9266 return NULL;
9267 }
9268 if (lnump != NULL)
9269 *lnump = ECMD_LAST;
9270 if (buf->b_fname == NULL)
9271 {
9272 result = (char_u *)"";
9273 valid = 0; /* Must have ":p:h" to be valid */
9274 }
9275 else
9276 result = buf->b_fname;
9277 break;
9278
9279#ifdef FEAT_SEARCHPATH
9280 case SPEC_CFILE: /* file name under cursor */
9281 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L);
9282 if (result == NULL)
9283 {
9284 *errormsg = (char_u *)"";
9285 return NULL;
9286 }
9287 resultbuf = result; /* remember allocated string */
9288 break;
9289#endif
9290
9291#ifdef FEAT_AUTOCMD
9292 case SPEC_AFILE: /* file name for autocommand */
9293 result = autocmd_fname;
9294 if (result == NULL)
9295 {
9296 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9297 return NULL;
9298 }
9299 break;
9300
9301 case SPEC_ABUF: /* buffer number for autocommand */
9302 if (autocmd_bufnr <= 0)
9303 {
9304 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9305 return NULL;
9306 }
9307 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9308 result = strbuf;
9309 break;
9310
9311 case SPEC_AMATCH: /* match name for autocommand */
9312 result = autocmd_match;
9313 if (result == NULL)
9314 {
9315 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9316 return NULL;
9317 }
9318 break;
9319
9320#endif
9321 case SPEC_SFILE: /* file name for ":so" command */
9322 result = sourcing_name;
9323 if (result == NULL)
9324 {
9325 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9326 return NULL;
9327 }
9328 break;
9329#if defined(FEAT_CLIENTSERVER)
9330 case SPEC_CLIENT: /* Source of last submitted input */
9331 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
9332 result = strbuf;
9333 break;
9334#endif
9335 }
9336
9337 resultlen = (int)STRLEN(result); /* length of new string */
9338 if (src[*usedlen] == '<') /* remove the file name extension */
9339 {
9340 ++*usedlen;
9341#ifdef RISCOS
9342 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9343#else
9344 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9345#endif
9346 resultlen = (int)(s - result);
9347 }
9348#ifdef FEAT_MODIFY_FNAME
9349 else if (!skip_mod)
9350 {
9351 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9352 &resultlen);
9353 if (result == NULL)
9354 {
9355 *errormsg = (char_u *)"";
9356 return NULL;
9357 }
9358 }
9359#endif
9360 }
9361
9362 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9363 {
9364 if (valid != VALID_HEAD + VALID_PATH)
9365 /* xgettext:no-c-format */
9366 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9367 else
9368 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9369 result = NULL;
9370 }
9371 else
9372 result = vim_strnsave(result, resultlen);
9373 vim_free(resultbuf);
9374 return result;
9375}
9376
9377/*
9378 * Concatenate all files in the argument list, separated by spaces, and return
9379 * it in one allocated string.
9380 * Spaces and backslashes in the file names are escaped with a backslash.
9381 * Returns NULL when out of memory.
9382 */
9383 static char_u *
9384arg_all()
9385{
9386 int len;
9387 int idx;
9388 char_u *retval = NULL;
9389 char_u *p;
9390
9391 /*
9392 * Do this loop two times:
9393 * first time: compute the total length
9394 * second time: concatenate the names
9395 */
9396 for (;;)
9397 {
9398 len = 0;
9399 for (idx = 0; idx < ARGCOUNT; ++idx)
9400 {
9401 p = alist_name(&ARGLIST[idx]);
9402 if (p != NULL)
9403 {
9404 if (len > 0)
9405 {
9406 /* insert a space in between names */
9407 if (retval != NULL)
9408 retval[len] = ' ';
9409 ++len;
9410 }
9411 for ( ; *p != NUL; ++p)
9412 {
9413 if (*p == ' ' || *p == '\\')
9414 {
9415 /* insert a backslash */
9416 if (retval != NULL)
9417 retval[len] = '\\';
9418 ++len;
9419 }
9420 if (retval != NULL)
9421 retval[len] = *p;
9422 ++len;
9423 }
9424 }
9425 }
9426
9427 /* second time: break here */
9428 if (retval != NULL)
9429 {
9430 retval[len] = NUL;
9431 break;
9432 }
9433
9434 /* allocate memory */
9435 retval = alloc(len + 1);
9436 if (retval == NULL)
9437 break;
9438 }
9439
9440 return retval;
9441}
9442
9443#if defined(FEAT_AUTOCMD) || defined(PROTO)
9444/*
9445 * Expand the <sfile> string in "arg".
9446 *
9447 * Returns an allocated string, or NULL for any error.
9448 */
9449 char_u *
9450expand_sfile(arg)
9451 char_u *arg;
9452{
9453 char_u *errormsg;
9454 int len;
9455 char_u *result;
9456 char_u *newres;
9457 char_u *repl;
9458 int srclen;
9459 char_u *p;
9460
9461 result = vim_strsave(arg);
9462 if (result == NULL)
9463 return NULL;
9464
9465 for (p = result; *p; )
9466 {
9467 if (STRNCMP(p, "<sfile>", 7) != 0)
9468 ++p;
9469 else
9470 {
9471 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9472 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9473 if (errormsg != NULL)
9474 {
9475 if (*errormsg)
9476 emsg(errormsg);
9477 vim_free(result);
9478 return NULL;
9479 }
9480 if (repl == NULL) /* no match (cannot happen) */
9481 {
9482 p += srclen;
9483 continue;
9484 }
9485 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9486 newres = alloc(len);
9487 if (newres == NULL)
9488 {
9489 vim_free(repl);
9490 vim_free(result);
9491 return NULL;
9492 }
9493 mch_memmove(newres, result, (size_t)(p - result));
9494 STRCPY(newres + (p - result), repl);
9495 len = (int)STRLEN(newres);
9496 STRCAT(newres, p + srclen);
9497 vim_free(repl);
9498 vim_free(result);
9499 result = newres;
9500 p = newres + len; /* continue after the match */
9501 }
9502 }
9503
9504 return result;
9505}
9506#endif
9507
9508#ifdef FEAT_SESSION
9509static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9510static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9511static frame_T *ses_skipframe __ARGS((frame_T *fr));
9512static int ses_do_frame __ARGS((frame_T *fr));
9513static int ses_do_win __ARGS((win_T *wp));
9514static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9515static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9516static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9517
9518/*
9519 * Write openfile commands for the current buffers to an .exrc file.
9520 * Return FAIL on error, OK otherwise.
9521 */
9522 static int
9523makeopens(fd, dirnow)
9524 FILE *fd;
9525 char_u *dirnow; /* Current directory name */
9526{
9527 buf_T *buf;
9528 int only_save_windows = TRUE;
9529 int nr;
9530 int cnr = 1;
9531 int restore_size = TRUE;
9532 win_T *wp;
9533 char_u *sname;
9534 win_T *edited_win = NULL;
9535
9536 if (ssop_flags & SSOP_BUFFERS)
9537 only_save_windows = FALSE; /* Save ALL buffers */
9538
9539 /*
9540 * Begin by setting the this_session variable, and then other
9541 * sessionable variables.
9542 */
9543#ifdef FEAT_EVAL
9544 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9545 return FAIL;
9546 if (ssop_flags & SSOP_GLOBALS)
9547 if (store_session_globals(fd) == FAIL)
9548 return FAIL;
9549#endif
9550
9551 /*
9552 * Close all windows but one.
9553 */
9554 if (put_line(fd, "silent only") == FAIL)
9555 return FAIL;
9556
9557 /*
9558 * Now a :cd command to the session directory or the current directory
9559 */
9560 if (ssop_flags & SSOP_SESDIR)
9561 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009562 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9563 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 return FAIL;
9565 }
9566 else if (ssop_flags & SSOP_CURDIR)
9567 {
9568 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9569 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009570 || fputs("cd ", fd) < 0
9571 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9572 || put_eol(fd) == FAIL)
9573 {
9574 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 vim_free(sname);
9578 }
9579
9580 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009581 * If there is an empty, unnamed buffer we will wipe it out later.
9582 * Remember the buffer number.
9583 */
9584 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9585 return FAIL;
9586 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9587 return FAIL;
9588 if (put_line(fd, "endif") == FAIL)
9589 return FAIL;
9590
9591 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 * Now save the current files, current buffer first.
9593 */
9594 if (put_line(fd, "set shortmess=aoO") == FAIL)
9595 return FAIL;
9596
9597 /* Now put the other buffers into the buffer list */
9598 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9599 {
9600 if (!(only_save_windows && buf->b_nwindows == 0)
9601 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9602 && buf->b_fname != NULL
9603 && buf->b_p_bl)
9604 {
9605 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9606 : buf->b_wininfo->wi_fpos.lnum) < 0
9607 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9608 return FAIL;
9609 }
9610 }
9611
9612 /* the global argument list */
9613 if (ses_arglist(fd, "args", &global_alist.al_ga,
9614 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9615 return FAIL;
9616
9617 if (ssop_flags & SSOP_RESIZE)
9618 {
9619 /* Note: after the restore we still check it worked!*/
9620 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9621 || put_eol(fd) == FAIL)
9622 return FAIL;
9623 }
9624
9625#ifdef FEAT_GUI
9626 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9627 {
9628 int x, y;
9629
9630 if (gui_mch_get_winpos(&x, &y) == OK)
9631 {
9632 /* Note: after the restore we still check it worked!*/
9633 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9634 return FAIL;
9635 }
9636 }
9637#endif
9638
9639 /*
9640 * Before creating the window layout, try loading one file. If this is
9641 * aborted we don't end up with a number of useless windows.
9642 * This may have side effects! (e.g., compressed or network file).
9643 */
9644 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9645 {
9646 if (ses_do_win(wp)
9647 && wp->w_buffer->b_ffname != NULL
9648 && !wp->w_buffer->b_help
9649#ifdef FEAT_QUICKFIX
9650 && !bt_nofile(wp->w_buffer)
9651#endif
9652 )
9653 {
9654 if (fputs("edit ", fd) < 0
9655 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9656 return FAIL;
9657 if (!wp->w_arg_idx_invalid)
9658 edited_win = wp;
9659 break;
9660 }
9661 }
9662
9663 /*
9664 * Save current window layout.
9665 */
9666 if (put_line(fd, "set splitbelow splitright") == FAIL)
9667 return FAIL;
9668 if (ses_win_rec(fd, topframe) == FAIL)
9669 return FAIL;
9670 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9671 return FAIL;
9672 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9673 return FAIL;
9674
9675 /*
9676 * Check if window sizes can be restored (no windows omitted).
9677 * Remember the window number of the current window after restoring.
9678 */
9679 nr = 0;
9680 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9681 {
9682 if (ses_do_win(wp))
9683 ++nr;
9684 else
9685 restore_size = FALSE;
9686 if (curwin == wp)
9687 cnr = nr;
9688 }
9689
9690 /* Go to the first window. */
9691 if (put_line(fd, "wincmd t") == FAIL)
9692 return FAIL;
9693
9694 /*
9695 * If more than one window, see if sizes can be restored.
9696 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9697 * resized when moving between windows.
9698 * Do this before restoring the view, so that the topline and the cursor
9699 * can be set. This is done again below.
9700 */
9701 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9702 return FAIL;
9703 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9704 return FAIL;
9705
9706 /*
9707 * Restore the view of the window (options, file, cursor, etc.).
9708 */
9709 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9710 {
9711 if (!ses_do_win(wp))
9712 continue;
9713 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9714 return FAIL;
9715 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9716 return FAIL;
9717 }
9718
9719 /*
9720 * Restore cursor to the current window if it's not the first one.
9721 */
9722 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9723 return FAIL;
9724
9725 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009726 * Wipe out an empty unnamed buffer we started in.
9727 */
9728 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9729 return FAIL;
9730 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9731 return FAIL;
9732 if (put_line(fd, "endif") == FAIL)
9733 return FAIL;
9734 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9735 return FAIL;
9736
9737 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 * Restore window sizes again after jumping around in windows, because the
9739 * current window has a minimum size while others may not.
9740 */
9741 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9742 return FAIL;
9743
9744 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9745 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9746 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9747 return FAIL;
9748
9749 /*
9750 * Lastly, execute the x.vim file if it exists.
9751 */
9752 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9753 || put_line(fd, "if file_readable(s:sx)") == FAIL
9754 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9755 || put_line(fd, "endif") == FAIL)
9756 return FAIL;
9757
9758 return OK;
9759}
9760
9761 static int
9762ses_winsizes(fd, restore_size)
9763 FILE *fd;
9764 int restore_size;
9765{
9766 int n = 0;
9767 win_T *wp;
9768
9769 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9770 {
9771 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9772 {
9773 if (!ses_do_win(wp))
9774 continue;
9775 ++n;
9776
9777 /* restore height when not full height */
9778 if (wp->w_height + wp->w_status_height < topframe->fr_height
9779 && (fprintf(fd,
9780 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9781 n, (long)wp->w_height, Rows / 2, Rows) < 0
9782 || put_eol(fd) == FAIL))
9783 return FAIL;
9784
9785 /* restore width when not full width */
9786 if (wp->w_width < Columns && (fprintf(fd,
9787 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9788 n, (long)wp->w_width, Columns / 2, Columns) < 0
9789 || put_eol(fd) == FAIL))
9790 return FAIL;
9791 }
9792 }
9793 else
9794 {
9795 /* Just equalise window sizes */
9796 if (put_line(fd, "wincmd =") == FAIL)
9797 return FAIL;
9798 }
9799 return OK;
9800}
9801
9802/*
9803 * Write commands to "fd" to recursively create windows for frame "fr",
9804 * horizontally and vertically split.
9805 * After the commands the last window in the frame is the current window.
9806 * Returns FAIL when writing the commands to "fd" fails.
9807 */
9808 static int
9809ses_win_rec(fd, fr)
9810 FILE *fd;
9811 frame_T *fr;
9812{
9813 frame_T *frc;
9814 int count = 0;
9815
9816 if (fr->fr_layout != FR_LEAF)
9817 {
9818 /* Find first frame that's not skipped and then create a window for
9819 * each following one (first frame is already there). */
9820 frc = ses_skipframe(fr->fr_child);
9821 if (frc != NULL)
9822 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9823 {
9824 /* Make window as big as possible so that we have lots of room
9825 * to split. */
9826 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9827 || put_line(fd, fr->fr_layout == FR_COL
9828 ? "split" : "vsplit") == FAIL)
9829 return FAIL;
9830 ++count;
9831 }
9832
9833 /* Go back to the first window. */
9834 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9835 ? "%dwincmd k" : "%dwincmd h", count) < 0
9836 || put_eol(fd) == FAIL))
9837 return FAIL;
9838
9839 /* Recursively create frames/windows in each window of this column or
9840 * row. */
9841 frc = ses_skipframe(fr->fr_child);
9842 while (frc != NULL)
9843 {
9844 ses_win_rec(fd, frc);
9845 frc = ses_skipframe(frc->fr_next);
9846 /* Go to next window. */
9847 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9848 return FAIL;
9849 }
9850 }
9851 return OK;
9852}
9853
9854/*
9855 * Skip frames that don't contain windows we want to save in the Session.
9856 * Returns NULL when there none.
9857 */
9858 static frame_T *
9859ses_skipframe(fr)
9860 frame_T *fr;
9861{
9862 frame_T *frc;
9863
9864 for (frc = fr; frc != NULL; frc = frc->fr_next)
9865 if (ses_do_frame(frc))
9866 break;
9867 return frc;
9868}
9869
9870/*
9871 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9872 * the Session.
9873 */
9874 static int
9875ses_do_frame(fr)
9876 frame_T *fr;
9877{
9878 frame_T *frc;
9879
9880 if (fr->fr_layout == FR_LEAF)
9881 return ses_do_win(fr->fr_win);
9882 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9883 if (ses_do_frame(frc))
9884 return TRUE;
9885 return FALSE;
9886}
9887
9888/*
9889 * Return non-zero if window "wp" is to be stored in the Session.
9890 */
9891 static int
9892ses_do_win(wp)
9893 win_T *wp;
9894{
9895 if (wp->w_buffer->b_fname == NULL
9896#ifdef FEAT_QUICKFIX
9897 /* When 'buftype' is "nofile" can't restore the window contents. */
9898 || bt_nofile(wp->w_buffer)
9899#endif
9900 )
9901 return (ssop_flags & SSOP_BLANK);
9902 if (wp->w_buffer->b_help)
9903 return (ssop_flags & SSOP_HELP);
9904 return TRUE;
9905}
9906
9907/*
9908 * Write commands to "fd" to restore the view of a window.
9909 * Caller must make sure 'scrolloff' is zero.
9910 */
9911 static int
9912put_view(fd, wp, add_edit, flagp)
9913 FILE *fd;
9914 win_T *wp;
9915 int add_edit; /* add ":edit" command to view */
9916 unsigned *flagp; /* vop_flags or ssop_flags */
9917{
9918 win_T *save_curwin;
9919 int f;
9920 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009921 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922
9923 /* Always restore cursor position for ":mksession". For ":mkview" only
9924 * when 'viewoptions' contains "cursor". */
9925 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9926
9927 /*
9928 * Local argument list.
9929 */
9930 if (wp->w_alist == &global_alist)
9931 {
9932 if (put_line(fd, "argglobal") == FAIL)
9933 return FAIL;
9934 }
9935 else
9936 {
9937 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9938 flagp == &vop_flags
9939 || !(*flagp & SSOP_CURDIR)
9940 || wp->w_localdir != NULL, flagp) == FAIL)
9941 return FAIL;
9942 }
9943
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009944 /* Only when part of a session: restore the argument index. Some
9945 * arguments may have been deleted, check if the index is valid. */
9946 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
9947 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 {
9949 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9950 || put_eol(fd) == FAIL)
9951 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009952 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 }
9954
9955 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009956 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 {
9958 /*
9959 * Load the file.
9960 */
9961 if (wp->w_buffer->b_ffname != NULL
9962#ifdef FEAT_QUICKFIX
9963 && !bt_nofile(wp->w_buffer)
9964#endif
9965 )
9966 {
9967 /*
9968 * Editing a file in this buffer: use ":edit file".
9969 * This may have side effects! (e.g., compressed or network file).
9970 */
9971 if (fputs("edit ", fd) < 0
9972 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9973 return FAIL;
9974 }
9975 else
9976 {
9977 /* No file in this buffer, just make it empty. */
9978 if (put_line(fd, "enew") == FAIL)
9979 return FAIL;
9980#ifdef FEAT_QUICKFIX
9981 if (wp->w_buffer->b_ffname != NULL)
9982 {
9983 /* The buffer does have a name, but it's not a file name. */
9984 if (fputs("file ", fd) < 0
9985 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9986 return FAIL;
9987 }
9988#endif
9989 do_cursor = FALSE;
9990 }
9991 }
9992
9993 /*
9994 * Local mappings and abbreviations.
9995 */
9996 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9997 && makemap(fd, wp->w_buffer) == FAIL)
9998 return FAIL;
9999
10000 /*
10001 * Local options. Need to go to the window temporarily.
10002 * Store only local values when using ":mkview" and when ":mksession" is
10003 * used and 'sessionoptions' doesn't include "options".
10004 * Some folding options are always stored when "folds" is included,
10005 * otherwise the folds would not be restored correctly.
10006 */
10007 save_curwin = curwin;
10008 curwin = wp;
10009 curbuf = curwin->w_buffer;
10010 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10011 f = makeset(fd, OPT_LOCAL,
10012 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10013#ifdef FEAT_FOLDING
10014 else if (*flagp & SSOP_FOLDS)
10015 f = makefoldset(fd);
10016#endif
10017 else
10018 f = OK;
10019 curwin = save_curwin;
10020 curbuf = curwin->w_buffer;
10021 if (f == FAIL)
10022 return FAIL;
10023
10024#ifdef FEAT_FOLDING
10025 /*
10026 * Save Folds when 'buftype' is empty and for help files.
10027 */
10028 if ((*flagp & SSOP_FOLDS)
10029 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010030# ifdef FEAT_QUICKFIX
10031 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10032# endif
10033 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 {
10035 if (put_folds(fd, wp) == FAIL)
10036 return FAIL;
10037 }
10038#endif
10039
10040 /*
10041 * Set the cursor after creating folds, since that moves the cursor.
10042 */
10043 if (do_cursor)
10044 {
10045
10046 /* Restore the cursor line in the file and relatively in the
10047 * window. Don't use "G", it changes the jumplist. */
10048 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10049 (long)wp->w_cursor.lnum,
10050 (long)(wp->w_cursor.lnum - wp->w_topline),
10051 (long)wp->w_height / 2, (long)wp->w_height) < 0
10052 || put_eol(fd) == FAIL
10053 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10054 || put_line(fd, "exe s:l") == FAIL
10055 || put_line(fd, "normal! zt") == FAIL
10056 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10057 || put_eol(fd) == FAIL)
10058 return FAIL;
10059 /* Restore the cursor column and left offset when not wrapping. */
10060 if (wp->w_cursor.col == 0)
10061 {
10062 if (put_line(fd, "normal! 0") == FAIL)
10063 return FAIL;
10064 }
10065 else
10066 {
10067 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10068 {
10069 if (fprintf(fd,
10070 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10071 (long)wp->w_cursor.col,
10072 (long)(wp->w_cursor.col - wp->w_leftcol),
10073 (long)wp->w_width / 2, (long)wp->w_width) < 0
10074 || put_eol(fd) == FAIL
10075 || put_line(fd, "if s:c > 0") == FAIL
10076 || fprintf(fd,
10077 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10078 (long)wp->w_cursor.col) < 0
10079 || put_eol(fd) == FAIL
10080 || put_line(fd, "else") == FAIL
10081 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10082 || put_eol(fd) == FAIL
10083 || put_line(fd, "endif") == FAIL)
10084 return FAIL;
10085 }
10086 else
10087 {
10088 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10089 || put_eol(fd) == FAIL)
10090 return FAIL;
10091 }
10092 }
10093 }
10094
10095 /*
10096 * Local directory.
10097 */
10098 if (wp->w_localdir != NULL)
10099 {
10100 if (fputs("lcd ", fd) < 0
10101 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10102 || put_eol(fd) == FAIL)
10103 return FAIL;
10104 }
10105
10106 return OK;
10107}
10108
10109/*
10110 * Write an argument list to the session file.
10111 * Returns FAIL if writing fails.
10112 */
10113 static int
10114ses_arglist(fd, cmd, gap, fullname, flagp)
10115 FILE *fd;
10116 char *cmd;
10117 garray_T *gap;
10118 int fullname; /* TRUE: use full path name */
10119 unsigned *flagp;
10120{
10121 int i;
10122 char_u buf[MAXPATHL];
10123 char_u *s;
10124
10125 if (gap->ga_len == 0)
10126 return put_line(fd, "silent! argdel *");
10127 if (fputs(cmd, fd) < 0)
10128 return FAIL;
10129 for (i = 0; i < gap->ga_len; ++i)
10130 {
10131 /* NULL file names are skipped (only happens when out of memory). */
10132 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10133 if (s != NULL)
10134 {
10135 if (fullname)
10136 {
10137 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10138 s = buf;
10139 }
10140 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10141 return FAIL;
10142 }
10143 }
10144 return put_eol(fd);
10145}
10146
10147/*
10148 * Write a buffer name to the session file.
10149 * Also ends the line.
10150 * Returns FAIL if writing fails.
10151 */
10152 static int
10153ses_fname(fd, buf, flagp)
10154 FILE *fd;
10155 buf_T *buf;
10156 unsigned *flagp;
10157{
10158 char_u *name;
10159
10160 /* Use the short file name if the current directory is known at the time
10161 * the session file will be sourced. Don't do this for ":mkview", we
10162 * don't know the current directory. */
10163 if (buf->b_sfname != NULL
10164 && flagp == &ssop_flags
10165 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
10166 name = buf->b_sfname;
10167 else
10168 name = buf->b_ffname;
10169 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10170 return FAIL;
10171 return OK;
10172}
10173
10174/*
10175 * Write a file name to the session file.
10176 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10177 * characters.
10178 * Returns FAIL if writing fails.
10179 */
10180 static int
10181ses_put_fname(fd, name, flagp)
10182 FILE *fd;
10183 char_u *name;
10184 unsigned *flagp;
10185{
10186 char_u *sname;
10187 int retval = OK;
10188 int c;
10189
10190 sname = home_replace_save(NULL, name);
10191 if (sname != NULL)
10192 name = sname;
10193 while (*name != NUL)
10194 {
10195#ifdef FEAT_MBYTE
10196 {
10197 int l;
10198
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010199 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 {
10201 /* copy a multibyte char */
10202 while (--l >= 0)
10203 {
10204 if (putc(*name, fd) != *name)
10205 retval = FAIL;
10206 ++name;
10207 }
10208 continue;
10209 }
10210 }
10211#endif
10212 c = *name++;
10213 if (c == '\\' && (*flagp & SSOP_SLASH))
10214 /* change a backslash to a forward slash */
10215 c = '/';
10216 else if ((vim_strchr(escape_chars, c) != NULL
10217#ifdef BACKSLASH_IN_FILENAME
10218 && c != '\\'
10219#endif
10220 ) || c == '#' || c == '%')
10221 {
10222 /* escape a special character with a backslash */
10223 if (putc('\\', fd) != '\\')
10224 retval = FAIL;
10225 }
10226 if (putc(c, fd) != c)
10227 retval = FAIL;
10228 }
10229 vim_free(sname);
10230 return retval;
10231}
10232
10233/*
10234 * ":loadview [nr]"
10235 */
10236 static void
10237ex_loadview(eap)
10238 exarg_T *eap;
10239{
10240 char_u *fname;
10241
10242 fname = get_view_file(*eap->arg);
10243 if (fname != NULL)
10244 {
10245 do_source(fname, FALSE, FALSE);
10246 vim_free(fname);
10247 }
10248}
10249
10250/*
10251 * Get the name of the view file for the current buffer.
10252 */
10253 static char_u *
10254get_view_file(c)
10255 int c;
10256{
10257 int len = 0;
10258 char_u *p, *s;
10259 char_u *retval;
10260 char_u *sname;
10261
10262 if (curbuf->b_ffname == NULL)
10263 {
10264 EMSG(_(e_noname));
10265 return NULL;
10266 }
10267 sname = home_replace_save(NULL, curbuf->b_ffname);
10268 if (sname == NULL)
10269 return NULL;
10270
10271 /*
10272 * We want a file name without separators, because we're not going to make
10273 * a directory.
10274 * "normal" path separator -> "=+"
10275 * "=" -> "=="
10276 * ":" path separator -> "=-"
10277 */
10278 for (p = sname; *p; ++p)
10279 if (*p == '=' || vim_ispathsep(*p))
10280 ++len;
10281 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10282 if (retval != NULL)
10283 {
10284 STRCPY(retval, p_vdir);
10285 add_pathsep(retval);
10286 s = retval + STRLEN(retval);
10287 for (p = sname; *p; ++p)
10288 {
10289 if (*p == '=')
10290 {
10291 *s++ = '=';
10292 *s++ = '=';
10293 }
10294 else if (vim_ispathsep(*p))
10295 {
10296 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010297#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 || defined(VMS)
10299 if (*p == ':')
10300 *s++ = '-';
10301 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010303 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 }
10305 else
10306 *s++ = *p;
10307 }
10308 *s++ = '=';
10309 *s++ = c;
10310 STRCPY(s, ".vim");
10311 }
10312
10313 vim_free(sname);
10314 return retval;
10315}
10316
10317#endif /* FEAT_SESSION */
10318
10319/*
10320 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10321 * Return FAIL for a write error.
10322 */
10323 int
10324put_eol(fd)
10325 FILE *fd;
10326{
10327 if (
10328#ifdef USE_CRNL
10329 (
10330# ifdef MKSESSION_NL
10331 !mksession_nl &&
10332# endif
10333 (putc('\r', fd) < 0)) ||
10334#endif
10335 (putc('\n', fd) < 0))
10336 return FAIL;
10337 return OK;
10338}
10339
10340/*
10341 * Write a line to "fd".
10342 * Return FAIL for a write error.
10343 */
10344 int
10345put_line(fd, s)
10346 FILE *fd;
10347 char *s;
10348{
10349 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10350 return FAIL;
10351 return OK;
10352}
10353
10354#ifdef FEAT_VIMINFO
10355/*
10356 * ":rviminfo" and ":wviminfo".
10357 */
10358 static void
10359ex_viminfo(eap)
10360 exarg_T *eap;
10361{
10362 char_u *save_viminfo;
10363
10364 save_viminfo = p_viminfo;
10365 if (*p_viminfo == NUL)
10366 p_viminfo = (char_u *)"'100";
10367 if (eap->cmdidx == CMD_rviminfo)
10368 {
10369 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
10370 EMSG(_("E195: Cannot open viminfo file for reading"));
10371 }
10372 else
10373 write_viminfo(eap->arg, eap->forceit);
10374 p_viminfo = save_viminfo;
10375}
10376#endif
10377
10378#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010379/*
10380 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010381 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 void
10384dialog_msg(buff, format, fname)
10385 char_u *buff;
10386 char *format;
10387 char_u *fname;
10388{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 if (fname == NULL)
10390 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010391 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392}
10393#endif
10394
10395/*
10396 * ":behave {mswin,xterm}"
10397 */
10398 static void
10399ex_behave(eap)
10400 exarg_T *eap;
10401{
10402 if (STRCMP(eap->arg, "mswin") == 0)
10403 {
10404 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10405 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10406 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10407 set_option_value((char_u *)"keymodel", 0L,
10408 (char_u *)"startsel,stopsel", 0);
10409 }
10410 else if (STRCMP(eap->arg, "xterm") == 0)
10411 {
10412 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10413 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10414 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10415 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10416 }
10417 else
10418 EMSG2(_(e_invarg2), eap->arg);
10419}
10420
10421#ifdef FEAT_AUTOCMD
10422static int filetype_detect = FALSE;
10423static int filetype_plugin = FALSE;
10424static int filetype_indent = FALSE;
10425
10426/*
10427 * ":filetype [plugin] [indent] {on,off,detect}"
10428 * on: Load the filetype.vim file to install autocommands for file types.
10429 * off: Load the ftoff.vim file to remove all autocommands for file types.
10430 * plugin on: load filetype.vim and ftplugin.vim
10431 * plugin off: load ftplugof.vim
10432 * indent on: load filetype.vim and indent.vim
10433 * indent off: load indoff.vim
10434 */
10435 static void
10436ex_filetype(eap)
10437 exarg_T *eap;
10438{
10439 char_u *arg = eap->arg;
10440 int plugin = FALSE;
10441 int indent = FALSE;
10442
10443 if (*eap->arg == NUL)
10444 {
10445 /* Print current status. */
10446 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10447 filetype_detect ? "ON" : "OFF",
10448 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10449 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10450 return;
10451 }
10452
10453 /* Accept "plugin" and "indent" in any order. */
10454 for (;;)
10455 {
10456 if (STRNCMP(arg, "plugin", 6) == 0)
10457 {
10458 plugin = TRUE;
10459 arg = skipwhite(arg + 6);
10460 continue;
10461 }
10462 if (STRNCMP(arg, "indent", 6) == 0)
10463 {
10464 indent = TRUE;
10465 arg = skipwhite(arg + 6);
10466 continue;
10467 }
10468 break;
10469 }
10470 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10471 {
10472 if (*arg == 'o' || !filetype_detect)
10473 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010474 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 filetype_detect = TRUE;
10476 if (plugin)
10477 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010478 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 filetype_plugin = TRUE;
10480 }
10481 if (indent)
10482 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010483 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 filetype_indent = TRUE;
10485 }
10486 }
10487 if (*arg == 'd')
10488 {
10489 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +000010490 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 }
10492 }
10493 else if (STRCMP(arg, "off") == 0)
10494 {
10495 if (plugin || indent)
10496 {
10497 if (plugin)
10498 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010499 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 filetype_plugin = FALSE;
10501 }
10502 if (indent)
10503 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010504 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 filetype_indent = FALSE;
10506 }
10507 }
10508 else
10509 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010510 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 filetype_detect = FALSE;
10512 }
10513 }
10514 else
10515 EMSG2(_(e_invarg2), arg);
10516}
10517
10518/*
10519 * ":setfiletype {name}"
10520 */
10521 static void
10522ex_setfiletype(eap)
10523 exarg_T *eap;
10524{
10525 if (!did_filetype)
10526 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10527}
10528#endif
10529
10530/*ARGSUSED*/
10531 static void
10532ex_digraphs(eap)
10533 exarg_T *eap;
10534{
10535#ifdef FEAT_DIGRAPHS
10536 if (*eap->arg != NUL)
10537 putdigraph(eap->arg);
10538 else
10539 listdigraphs();
10540#else
10541 EMSG(_("E196: No digraphs in this version"));
10542#endif
10543}
10544
10545 static void
10546ex_set(eap)
10547 exarg_T *eap;
10548{
10549 int flags = 0;
10550
10551 if (eap->cmdidx == CMD_setlocal)
10552 flags = OPT_LOCAL;
10553 else if (eap->cmdidx == CMD_setglobal)
10554 flags = OPT_GLOBAL;
10555#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10556 if (cmdmod.browse && flags == 0)
10557 ex_options(eap);
10558 else
10559#endif
10560 (void)do_set(eap->arg, flags);
10561}
10562
10563#ifdef FEAT_SEARCH_EXTRA
10564/*
10565 * ":nohlsearch"
10566 */
10567/*ARGSUSED*/
10568 static void
10569ex_nohlsearch(eap)
10570 exarg_T *eap;
10571{
10572 no_hlsearch = TRUE;
10573 redraw_all_later(NOT_VALID);
10574}
10575
10576/*
10577 * ":match {group} {pattern}"
10578 * Sets nextcmd to the start of the next command, if any. Also called when
10579 * skipping commands to find the next command.
10580 */
10581 static void
10582ex_match(eap)
10583 exarg_T *eap;
10584{
10585 char_u *p;
10586 char_u *end;
10587 int c;
10588
10589 /* First clear any old pattern. */
10590 if (!eap->skip)
10591 {
10592 vim_free(curwin->w_match.regprog);
10593 curwin->w_match.regprog = NULL;
10594 redraw_later(NOT_VALID); /* always need a redraw */
10595 }
10596
10597 if (ends_excmd(*eap->arg))
10598 end = eap->arg;
10599 else if ((STRNICMP(eap->arg, "none", 4) == 0
10600 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10601 end = eap->arg + 4;
10602 else
10603 {
10604 p = skiptowhite(eap->arg);
10605 if (!eap->skip)
10606 {
10607 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg));
10608 if (curwin->w_match_id == 0)
10609 {
10610 EMSG2(_(e_nogroup), eap->arg);
10611 return;
10612 }
10613 }
10614 p = skipwhite(p);
10615 if (*p == NUL)
10616 {
10617 /* There must be two arguments. */
10618 EMSG2(_(e_invarg2), eap->arg);
10619 return;
10620 }
10621 end = skip_regexp(p + 1, *p, TRUE, NULL);
10622 if (!eap->skip)
10623 {
10624 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10625 {
10626 eap->errmsg = e_trailing;
10627 return;
10628 }
10629
10630 c = *end;
10631 *end = NUL;
10632 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC);
10633 *end = c;
10634 if (curwin->w_match.regprog == NULL)
10635 {
10636 EMSG2(_(e_invarg2), p);
10637 return;
10638 }
10639 }
10640 }
10641 eap->nextcmd = find_nextcmd(end);
10642}
10643#endif
10644
10645#ifdef FEAT_CRYPT
10646/*
10647 * ":X": Get crypt key
10648 */
10649/*ARGSUSED*/
10650 static void
10651ex_X(eap)
10652 exarg_T *eap;
10653{
10654 (void)get_crypt_key(TRUE, TRUE);
10655}
10656#endif
10657
10658#ifdef FEAT_FOLDING
10659 static void
10660ex_fold(eap)
10661 exarg_T *eap;
10662{
10663 if (foldManualAllowed(TRUE))
10664 foldCreate(eap->line1, eap->line2);
10665}
10666
10667 static void
10668ex_foldopen(eap)
10669 exarg_T *eap;
10670{
10671 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10672 eap->forceit, FALSE);
10673}
10674
10675 static void
10676ex_folddo(eap)
10677 exarg_T *eap;
10678{
10679 linenr_T lnum;
10680
10681 /* First set the marks for all lines closed/open. */
10682 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10683 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10684 ml_setmarked(lnum);
10685
10686 /* Execute the command on the marked lines. */
10687 global_exe(eap->arg);
10688 ml_clearmarked(); /* clear rest of the marks */
10689}
10690#endif