blob: 1a8788684f2244a7295001d9d2eb4ecfeab2a2c9 [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)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006231 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006232 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 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006242 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006243 {
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/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006978 * :tabedit open new Tab page with empty window
6979 * :tabedit [+command] file open new Tab page and edit "file"
6980 * :tabnew [[+command] file] just like :tabedit
6981 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006982 */
6983 void
6984ex_tabedit(eap)
6985 exarg_T *eap;
6986{
6987# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6988 char_u *fname = NULL;
6989# endif
6990# ifdef FEAT_BROWSE
6991 int browse_flag = cmdmod.browse;
6992# endif
6993
6994# ifdef FEAT_GUI
6995 need_mouse_correct = TRUE;
6996# endif
6997
6998# ifdef FEAT_SEARCHPATH
6999 if (eap->cmdidx == CMD_tabfind)
7000 {
7001 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7002 FNAME_MESS, TRUE, curbuf->b_ffname);
7003 if (fname == NULL)
7004 goto theend;
7005 eap->arg = fname;
7006 }
7007# ifdef FEAT_BROWSE
7008 else
7009# endif
7010# endif
7011# ifdef FEAT_BROWSE
7012 if (cmdmod.browse)
7013 {
7014 if (
7015# ifdef FEAT_GUI
7016 !gui.in_use &&
7017# endif
7018 au_has_group((char_u *)"FileExplorer"))
7019 {
7020 /* No browsing supported but we do have the file explorer:
7021 * Edit the directory. */
7022 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7023 eap->arg = (char_u *)".";
7024 }
7025 else
7026 {
7027 fname = do_browse(0, (char_u *)_("Edit File in new tab page"),
7028 eap->arg, NULL, NULL, NULL, curbuf);
7029 if (fname == NULL)
7030 goto theend;
7031 eap->arg = fname;
7032 }
7033 }
7034 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
7035# endif
7036
7037 if (win_new_tabpage() != FAIL)
7038 {
7039# ifdef FEAT_SCROLLBIND
7040 curwin->w_p_scb = FALSE;
7041# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007042 if (*eap->arg != NUL)
7043 do_exedit(eap, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007044 }
7045
7046# ifdef FEAT_BROWSE
7047 cmdmod.browse = browse_flag;
7048# endif
7049
7050# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
7051theend:
7052 vim_free(fname);
7053# endif
7054}
7055
7056/*
7057 * :tab command
7058 */
7059 void
7060ex_tab(eap)
7061 exarg_T *eap;
7062{
7063 goto_tabpage((int)eap->line2);
7064}
7065
7066/*
7067 * :tabs command: List tabs and their contents.
7068 */
7069/*ARGSUSED*/
7070 static void
7071ex_tabs(eap)
7072 exarg_T *eap;
7073{
7074 tabpage_T *tp;
7075 win_T *wp;
7076 int tabcount = 1;
7077
7078 msg_start();
7079 msg_scroll = TRUE;
7080 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7081 {
7082 msg_putchar('\n');
7083 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7084 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7085 out_flush(); /* output one line at a time */
7086 ui_breakcheck();
7087
7088 if (tp->tp_topframe == topframe)
7089 wp = firstwin;
7090 else
7091 wp = tp->tp_firstwin;
7092 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7093 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007094 msg_puts((char_u *)"\n ");
7095 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7096 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007097 if (buf_spname(wp->w_buffer) != NULL)
7098 STRCPY(IObuff, buf_spname(wp->w_buffer));
7099 else
7100 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7101 IObuff, IOSIZE, TRUE);
7102 msg_outtrans(IObuff);
7103 out_flush(); /* output one line at a time */
7104 ui_breakcheck();
7105 }
7106 }
7107}
7108
7109#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110
7111/*
7112 * ":mode": Set screen mode.
7113 * If no argument given, just get the screen size and redraw.
7114 */
7115 static void
7116ex_mode(eap)
7117 exarg_T *eap;
7118{
7119 if (*eap->arg == NUL)
7120 shell_resized();
7121 else
7122 mch_screenmode(eap->arg);
7123}
7124
7125#ifdef FEAT_WINDOWS
7126/*
7127 * ":resize".
7128 * set, increment or decrement current window height
7129 */
7130 static void
7131ex_resize(eap)
7132 exarg_T *eap;
7133{
7134 int n;
7135 win_T *wp = curwin;
7136
7137 if (eap->addr_count > 0)
7138 {
7139 n = eap->line2;
7140 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7141 ;
7142 }
7143
7144#ifdef FEAT_GUI
7145 need_mouse_correct = TRUE;
7146#endif
7147 n = atol((char *)eap->arg);
7148#ifdef FEAT_VERTSPLIT
7149 if (cmdmod.split & WSP_VERT)
7150 {
7151 if (*eap->arg == '-' || *eap->arg == '+')
7152 n += W_WIDTH(curwin);
7153 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7154 n = 9999;
7155 win_setwidth_win((int)n, wp);
7156 }
7157 else
7158#endif
7159 {
7160 if (*eap->arg == '-' || *eap->arg == '+')
7161 n += curwin->w_height;
7162 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7163 n = 9999;
7164 win_setheight_win((int)n, wp);
7165 }
7166}
7167#endif
7168
7169/*
7170 * ":find [+command] <file>" command.
7171 */
7172 static void
7173ex_find(eap)
7174 exarg_T *eap;
7175{
7176#ifdef FEAT_SEARCHPATH
7177 char_u *fname;
7178 int count;
7179
7180 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7181 TRUE, curbuf->b_ffname);
7182 if (eap->addr_count > 0)
7183 {
7184 /* Repeat finding the file "count" times. This matters when it
7185 * appears several times in the path. */
7186 count = eap->line2;
7187 while (fname != NULL && --count > 0)
7188 {
7189 vim_free(fname);
7190 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7191 FALSE, curbuf->b_ffname);
7192 }
7193 }
7194
7195 if (fname != NULL)
7196 {
7197 eap->arg = fname;
7198#endif
7199 do_exedit(eap, NULL);
7200#ifdef FEAT_SEARCHPATH
7201 vim_free(fname);
7202 }
7203#endif
7204}
7205
7206/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007207 * ":open" simulation: for now just work like ":visual".
7208 */
7209 static void
7210ex_open(eap)
7211 exarg_T *eap;
7212{
7213 regmatch_T regmatch;
7214 char_u *p;
7215
7216 curwin->w_cursor.lnum = eap->line2;
7217 beginline(BL_SOL | BL_FIX);
7218 if (*eap->arg == '/')
7219 {
7220 /* ":open /pattern/": put cursor in column found with pattern */
7221 ++eap->arg;
7222 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7223 *p = NUL;
7224 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7225 if (regmatch.regprog != NULL)
7226 {
7227 regmatch.rm_ic = p_ic;
7228 p = ml_get_curline();
7229 if (vim_regexec(&regmatch, p, (colnr_T)0))
7230 curwin->w_cursor.col = regmatch.startp[0] - p;
7231 else
7232 EMSG(_(e_nomatch));
7233 vim_free(regmatch.regprog);
7234 }
7235 /* Move to the NUL, ignore any other arguments. */
7236 eap->arg += STRLEN(eap->arg);
7237 }
7238 check_cursor();
7239
7240 eap->cmdidx = CMD_visual;
7241 do_exedit(eap, NULL);
7242}
7243
7244/*
7245 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 */
7247 static void
7248ex_edit(eap)
7249 exarg_T *eap;
7250{
7251 do_exedit(eap, NULL);
7252}
7253
7254/*
7255 * ":edit <file>" command and alikes.
7256 */
7257/*ARGSUSED*/
7258 void
7259do_exedit(eap, old_curwin)
7260 exarg_T *eap;
7261 win_T *old_curwin; /* curwin before doing a split or NULL */
7262{
7263 int n;
7264#ifdef FEAT_WINDOWS
7265 int need_hide;
7266#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007267 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268
7269 /*
7270 * ":vi" command ends Ex mode.
7271 */
7272 if (exmode_active && (eap->cmdidx == CMD_visual
7273 || eap->cmdidx == CMD_view))
7274 {
7275 exmode_active = FALSE;
7276 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007277 {
7278 /* Special case: ":global/pat/visual\NLvi-commands" */
7279 if (global_busy)
7280 {
7281 int rd = RedrawingDisabled;
7282 int nwr = no_wait_return;
7283 int ms = msg_scroll;
7284#ifdef FEAT_GUI
7285 int he = hold_gui_events;
7286#endif
7287
7288 if (eap->nextcmd != NULL)
7289 {
7290 stuffReadbuff(eap->nextcmd);
7291 eap->nextcmd = NULL;
7292 }
7293
7294 if (exmode_was != EXMODE_VIM)
7295 settmode(TMODE_RAW);
7296 RedrawingDisabled = 0;
7297 no_wait_return = 0;
7298 need_wait_return = FALSE;
7299 msg_scroll = 0;
7300#ifdef FEAT_GUI
7301 hold_gui_events = 0;
7302#endif
7303 must_redraw = CLEAR;
7304
7305 main_loop(FALSE, TRUE);
7306
7307 RedrawingDisabled = rd;
7308 no_wait_return = nwr;
7309 msg_scroll = ms;
7310#ifdef FEAT_GUI
7311 hold_gui_events = he;
7312#endif
7313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 }
7317
7318 if ((eap->cmdidx == CMD_new
7319#ifdef FEAT_VERTSPLIT
7320 || eap->cmdidx == CMD_vnew
7321#endif
7322 ) && *eap->arg == NUL)
7323 {
7324 /* ":new" without argument: edit an new empty buffer */
7325 setpcmark();
7326 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
7327 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
7328 }
7329 else if ((eap->cmdidx != CMD_split
7330#ifdef FEAT_VERTSPLIT
7331 && eap->cmdidx != CMD_vsplit
7332#endif
7333 )
7334 || *eap->arg != NUL
7335#ifdef FEAT_BROWSE
7336 || cmdmod.browse
7337#endif
7338 )
7339 {
7340 n = readonlymode;
7341 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7342 readonlymode = TRUE;
7343 else if (eap->cmdidx == CMD_enew)
7344 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7345 empty buffer */
7346 setpcmark();
7347 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7348 NULL, eap,
7349 /* ":edit" goes to first line if Vi compatible */
7350 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7351 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7352 ? ECMD_ONE : eap->do_ecmd_lnum,
7353 (P_HID(curbuf) ? ECMD_HIDE : 0)
7354 + (eap->forceit ? ECMD_FORCEIT : 0)
7355#ifdef FEAT_LISTCMDS
7356 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7357#endif
7358 ) == FAIL)
7359 {
7360 /* Editing the file failed. If the window was split, close it. */
7361#ifdef FEAT_WINDOWS
7362 if (old_curwin != NULL)
7363 {
7364 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7365 if (!need_hide || P_HID(curbuf))
7366 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007367# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7368 cleanup_T cs;
7369
7370 /* Reset the error/interrupt/exception state here so that
7371 * aborting() returns FALSE when closing a window. */
7372 enter_cleanup(&cs);
7373# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374# ifdef FEAT_GUI
7375 need_mouse_correct = TRUE;
7376# endif
7377 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007378
7379# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7380 /* Restore the error/interrupt/exception state if not
7381 * discarded by a new aborting error, interrupt, or
7382 * uncaught exception. */
7383 leave_cleanup(&cs);
7384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 }
7386 }
7387#endif
7388 }
7389 else if (readonlymode && curbuf->b_nwindows == 1)
7390 {
7391 /* When editing an already visited buffer, 'readonly' won't be set
7392 * but the previous value is kept. With ":view" and ":sview" we
7393 * want the file to be readonly, except when another window is
7394 * editing the same buffer. */
7395 curbuf->b_p_ro = TRUE;
7396 }
7397 readonlymode = n;
7398 }
7399 else
7400 {
7401 if (eap->do_ecmd_cmd != NULL)
7402 do_cmdline_cmd(eap->do_ecmd_cmd);
7403#ifdef FEAT_TITLE
7404 n = curwin->w_arg_idx_invalid;
7405#endif
7406 check_arg_idx(curwin);
7407#ifdef FEAT_TITLE
7408 if (n != curwin->w_arg_idx_invalid)
7409 maketitle();
7410#endif
7411 }
7412
7413#ifdef FEAT_WINDOWS
7414 /*
7415 * if ":split file" worked, set alternate file name in old window to new
7416 * file
7417 */
7418 if (old_curwin != NULL
7419 && *eap->arg != NUL
7420 && curwin != old_curwin
7421 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007422 && old_curwin->w_buffer != curbuf
7423 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 old_curwin->w_alt_fnum = curbuf->b_fnum;
7425#endif
7426
7427 ex_no_reprint = TRUE;
7428}
7429
7430#ifndef FEAT_GUI
7431/*
7432 * ":gui" and ":gvim" when there is no GUI.
7433 */
7434 static void
7435ex_nogui(eap)
7436 exarg_T *eap;
7437{
7438 eap->errmsg = e_nogvim;
7439}
7440#endif
7441
7442#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7443 static void
7444ex_tearoff(eap)
7445 exarg_T *eap;
7446{
7447 gui_make_tearoff(eap->arg);
7448}
7449#endif
7450
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007451#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 static void
7453ex_popup(eap)
7454 exarg_T *eap;
7455{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007456 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457}
7458#endif
7459
7460/*ARGSUSED*/
7461 static void
7462ex_swapname(eap)
7463 exarg_T *eap;
7464{
7465 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7466 MSG(_("No swap file"));
7467 else
7468 msg(curbuf->b_ml.ml_mfp->mf_fname);
7469}
7470
7471/*
7472 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7473 * offset.
7474 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7475 */
7476/*ARGSUSED*/
7477 static void
7478ex_syncbind(eap)
7479 exarg_T *eap;
7480{
7481#ifdef FEAT_SCROLLBIND
7482 win_T *wp;
7483 long topline;
7484 long y;
7485 linenr_T old_linenr = curwin->w_cursor.lnum;
7486
7487 setpcmark();
7488
7489 /*
7490 * determine max topline
7491 */
7492 if (curwin->w_p_scb)
7493 {
7494 topline = curwin->w_topline;
7495 for (wp = firstwin; wp; wp = wp->w_next)
7496 {
7497 if (wp->w_p_scb && wp->w_buffer)
7498 {
7499 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7500 if (topline > y)
7501 topline = y;
7502 }
7503 }
7504 if (topline < 1)
7505 topline = 1;
7506 }
7507 else
7508 {
7509 topline = 1;
7510 }
7511
7512
7513 /*
7514 * set all scrollbind windows to the same topline
7515 */
7516 wp = curwin;
7517 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7518 {
7519 if (curwin->w_p_scb)
7520 {
7521 y = topline - curwin->w_topline;
7522 if (y > 0)
7523 scrollup(y, TRUE);
7524 else
7525 scrolldown(-y, TRUE);
7526 curwin->w_scbind_pos = topline;
7527 redraw_later(VALID);
7528 cursor_correct();
7529#ifdef FEAT_WINDOWS
7530 curwin->w_redr_status = TRUE;
7531#endif
7532 }
7533 }
7534 curwin = wp;
7535 if (curwin->w_p_scb)
7536 {
7537 did_syncbind = TRUE;
7538 checkpcmark();
7539 if (old_linenr != curwin->w_cursor.lnum)
7540 {
7541 char_u ctrl_o[2];
7542
7543 ctrl_o[0] = Ctrl_O;
7544 ctrl_o[1] = 0;
7545 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7546 }
7547 }
7548#endif
7549}
7550
7551
7552 static void
7553ex_read(eap)
7554 exarg_T *eap;
7555{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007556 int i;
7557 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7558 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559
7560 if (eap->usefilter) /* :r!cmd */
7561 do_bang(1, eap, FALSE, FALSE, TRUE);
7562 else
7563 {
7564 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7565 return;
7566
7567#ifdef FEAT_BROWSE
7568 if (cmdmod.browse)
7569 {
7570 char_u *browseFile;
7571
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007572 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 NULL, NULL, NULL, curbuf);
7574 if (browseFile != NULL)
7575 {
7576 i = readfile(browseFile, NULL,
7577 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7578 vim_free(browseFile);
7579 }
7580 else
7581 i = OK;
7582 }
7583 else
7584#endif
7585 if (*eap->arg == NUL)
7586 {
7587 if (check_fname() == FAIL) /* check for no file name */
7588 return;
7589 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7590 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7591 }
7592 else
7593 {
7594 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7595 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7596 i = readfile(eap->arg, NULL,
7597 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7598
7599 }
7600 if (i == FAIL)
7601 {
7602#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7603 if (!aborting())
7604#endif
7605 EMSG2(_(e_notopen), eap->arg);
7606 }
7607 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007608 {
7609 if (empty && exmode_active)
7610 {
7611 /* Delete the empty line that remains. Historically ex does
7612 * this but vi doesn't. */
7613 if (eap->line2 == 0)
7614 lnum = curbuf->b_ml.ml_line_count;
7615 else
7616 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007617 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007618 {
7619 ml_delete(lnum, FALSE);
7620 deleted_lines_mark(lnum, 1L);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007621 if (curwin->w_cursor.lnum > 1
7622 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007623 --curwin->w_cursor.lnum;
7624 }
7625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 }
7629}
7630
Bram Moolenaarea408852005-06-25 22:49:46 +00007631static char_u *prev_dir = NULL;
7632
7633#if defined(EXITFREE) || defined(PROTO)
7634 void
7635free_cd_dir()
7636{
7637 vim_free(prev_dir);
7638}
7639#endif
7640
7641
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642/*
7643 * ":cd", ":lcd", ":chdir" and ":lchdir".
7644 */
7645 static void
7646ex_cd(eap)
7647 exarg_T *eap;
7648{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 char_u *new_dir;
7650 char_u *tofree;
7651
7652 new_dir = eap->arg;
7653#if !defined(UNIX) && !defined(VMS)
7654 /* for non-UNIX ":cd" means: print current directory */
7655 if (*new_dir == NUL)
7656 ex_pwd(NULL);
7657 else
7658#endif
7659 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007660 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7661 && !eap->forceit)
7662 {
7663 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7664 return;
7665 }
7666
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 /* ":cd -": Change to previous directory */
7668 if (STRCMP(new_dir, "-") == 0)
7669 {
7670 if (prev_dir == NULL)
7671 {
7672 EMSG(_("E186: No previous directory"));
7673 return;
7674 }
7675 new_dir = prev_dir;
7676 }
7677
7678 /* Save current directory for next ":cd -" */
7679 tofree = prev_dir;
7680 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7681 prev_dir = vim_strsave(NameBuff);
7682 else
7683 prev_dir = NULL;
7684
7685#if defined(UNIX) || defined(VMS)
7686 /* for UNIX ":cd" means: go to home directory */
7687 if (*new_dir == NUL)
7688 {
7689 /* use NameBuff for home directory name */
7690# ifdef VMS
7691 char_u *p;
7692
7693 p = mch_getenv((char_u *)"SYS$LOGIN");
7694 if (p == NULL || *p == NUL) /* empty is the same as not set */
7695 NameBuff[0] = NUL;
7696 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007697 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698# else
7699 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7700# endif
7701 new_dir = NameBuff;
7702 }
7703#endif
7704 if (new_dir == NULL || vim_chdir(new_dir))
7705 EMSG(_(e_failed));
7706 else
7707 {
7708 vim_free(curwin->w_localdir);
7709 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7710 {
7711 /* If still in global directory, need to remember current
7712 * directory as global directory. */
7713 if (globaldir == NULL && prev_dir != NULL)
7714 globaldir = vim_strsave(prev_dir);
7715 /* Remember this local directory for the window. */
7716 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7717 curwin->w_localdir = vim_strsave(NameBuff);
7718 }
7719 else
7720 {
7721 /* We are now in the global directory, no need to remember its
7722 * name. */
7723 vim_free(globaldir);
7724 globaldir = NULL;
7725 curwin->w_localdir = NULL;
7726 }
7727
7728 shorten_fnames(TRUE);
7729
7730 /* Echo the new current directory if the command was typed. */
7731 if (KeyTyped)
7732 ex_pwd(eap);
7733 }
7734 vim_free(tofree);
7735 }
7736}
7737
7738/*
7739 * ":pwd".
7740 */
7741/*ARGSUSED*/
7742 static void
7743ex_pwd(eap)
7744 exarg_T *eap;
7745{
7746 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7747 {
7748#ifdef BACKSLASH_IN_FILENAME
7749 slash_adjust(NameBuff);
7750#endif
7751 msg(NameBuff);
7752 }
7753 else
7754 EMSG(_("E187: Unknown"));
7755}
7756
7757/*
7758 * ":=".
7759 */
7760 static void
7761ex_equal(eap)
7762 exarg_T *eap;
7763{
7764 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007765 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766}
7767
7768 static void
7769ex_sleep(eap)
7770 exarg_T *eap;
7771{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007772 int n;
7773 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774
7775 if (cursor_valid())
7776 {
7777 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7778 if (n >= 0)
7779 windgoto((int)n, curwin->w_wcol);
7780 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007781
7782 len = eap->line2;
7783 switch (*eap->arg)
7784 {
7785 case 'm': break;
7786 case NUL: len *= 1000L; break;
7787 default: EMSG2(_(e_invarg2), eap->arg); return;
7788 }
7789 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790}
7791
7792/*
7793 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7794 */
7795 void
7796do_sleep(msec)
7797 long msec;
7798{
7799 long done;
7800
7801 cursor_on();
7802 out_flush();
7803 for (done = 0; !got_int && done < msec; done += 1000L)
7804 {
7805 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7806 ui_breakcheck();
7807 }
7808}
7809
7810 static void
7811do_exmap(eap, isabbrev)
7812 exarg_T *eap;
7813 int isabbrev;
7814{
7815 int mode;
7816 char_u *cmdp;
7817
7818 cmdp = eap->cmd;
7819 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7820
7821 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7822 eap->arg, mode, isabbrev))
7823 {
7824 case 1: EMSG(_(e_invarg));
7825 break;
7826 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7827 break;
7828 }
7829}
7830
7831/*
7832 * ":winsize" command (obsolete).
7833 */
7834 static void
7835ex_winsize(eap)
7836 exarg_T *eap;
7837{
7838 int w, h;
7839 char_u *arg = eap->arg;
7840 char_u *p;
7841
7842 w = getdigits(&arg);
7843 arg = skipwhite(arg);
7844 p = arg;
7845 h = getdigits(&arg);
7846 if (*p != NUL && *arg == NUL)
7847 set_shellsize(w, h, TRUE);
7848 else
7849 EMSG(_("E465: :winsize requires two number arguments"));
7850}
7851
7852#ifdef FEAT_WINDOWS
7853 static void
7854ex_wincmd(eap)
7855 exarg_T *eap;
7856{
7857 int xchar = NUL;
7858 char_u *p;
7859
7860 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7861 {
7862 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7863 if (eap->arg[1] == NUL)
7864 {
7865 EMSG(_(e_invarg));
7866 return;
7867 }
7868 xchar = eap->arg[1];
7869 p = eap->arg + 2;
7870 }
7871 else
7872 p = eap->arg + 1;
7873
7874 eap->nextcmd = check_nextcmd(p);
7875 p = skipwhite(p);
7876 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7877 EMSG(_(e_invarg));
7878 else
7879 {
7880 /* Pass flags on for ":vertical wincmd ]". */
7881 postponed_split_flags = cmdmod.split;
7882 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7883 postponed_split_flags = 0;
7884 }
7885}
7886#endif
7887
Bram Moolenaar843ee412004-06-30 16:16:41 +00007888#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889/*
7890 * ":winpos".
7891 */
7892 static void
7893ex_winpos(eap)
7894 exarg_T *eap;
7895{
7896 int x, y;
7897 char_u *arg = eap->arg;
7898 char_u *p;
7899
7900 if (*arg == NUL)
7901 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007902# if defined(FEAT_GUI) || defined(MSWIN)
7903# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007905# else
7906 if (mch_get_winpos(&x, &y) != FAIL)
7907# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 {
7909 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7910 msg(IObuff);
7911 }
7912 else
7913# endif
7914 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7915 }
7916 else
7917 {
7918 x = getdigits(&arg);
7919 arg = skipwhite(arg);
7920 p = arg;
7921 y = getdigits(&arg);
7922 if (*p == NUL || *arg != NUL)
7923 {
7924 EMSG(_("E466: :winpos requires two number arguments"));
7925 return;
7926 }
7927# ifdef FEAT_GUI
7928 if (gui.in_use)
7929 gui_mch_set_winpos(x, y);
7930 else if (gui.starting)
7931 {
7932 /* Remember the coordinates for when the window is opened. */
7933 gui_win_x = x;
7934 gui_win_y = y;
7935 }
7936# ifdef HAVE_TGETENT
7937 else
7938# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007939# else
7940# ifdef MSWIN
7941 mch_set_winpos(x, y);
7942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943# endif
7944# ifdef HAVE_TGETENT
7945 if (*T_CWP)
7946 term_set_winpos(x, y);
7947# endif
7948 }
7949}
7950#endif
7951
7952/*
7953 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7954 */
7955 static void
7956ex_operators(eap)
7957 exarg_T *eap;
7958{
7959 oparg_T oa;
7960
7961 clear_oparg(&oa);
7962 oa.regname = eap->regname;
7963 oa.start.lnum = eap->line1;
7964 oa.end.lnum = eap->line2;
7965 oa.line_count = eap->line2 - eap->line1 + 1;
7966 oa.motion_type = MLINE;
7967#ifdef FEAT_VIRTUALEDIT
7968 virtual_op = FALSE;
7969#endif
7970 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7971 {
7972 setpcmark();
7973 curwin->w_cursor.lnum = eap->line1;
7974 beginline(BL_SOL | BL_FIX);
7975 }
7976
7977 switch (eap->cmdidx)
7978 {
7979 case CMD_delete:
7980 oa.op_type = OP_DELETE;
7981 op_delete(&oa);
7982 break;
7983
7984 case CMD_yank:
7985 oa.op_type = OP_YANK;
7986 (void)op_yank(&oa, FALSE, TRUE);
7987 break;
7988
7989 default: /* CMD_rshift or CMD_lshift */
7990 if ((eap->cmdidx == CMD_rshift)
7991#ifdef FEAT_RIGHTLEFT
7992 ^ curwin->w_p_rl
7993#endif
7994 )
7995 oa.op_type = OP_RSHIFT;
7996 else
7997 oa.op_type = OP_LSHIFT;
7998 op_shift(&oa, FALSE, eap->amount);
7999 break;
8000 }
8001#ifdef FEAT_VIRTUALEDIT
8002 virtual_op = MAYBE;
8003#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008004 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005}
8006
8007/*
8008 * ":put".
8009 */
8010 static void
8011ex_put(eap)
8012 exarg_T *eap;
8013{
8014 /* ":0put" works like ":1put!". */
8015 if (eap->line2 == 0)
8016 {
8017 eap->line2 = 1;
8018 eap->forceit = TRUE;
8019 }
8020 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008021 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8022 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023}
8024
8025/*
8026 * Handle ":copy" and ":move".
8027 */
8028 static void
8029ex_copymove(eap)
8030 exarg_T *eap;
8031{
8032 long n;
8033
8034 n = get_address(&eap->arg, FALSE, FALSE);
8035 if (eap->arg == NULL) /* error detected */
8036 {
8037 eap->nextcmd = NULL;
8038 return;
8039 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008040 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041
8042 /*
8043 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8044 */
8045 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8046 {
8047 EMSG(_(e_invaddr));
8048 return;
8049 }
8050
8051 if (eap->cmdidx == CMD_move)
8052 {
8053 if (do_move(eap->line1, eap->line2, n) == FAIL)
8054 return;
8055 }
8056 else
8057 ex_copy(eap->line1, eap->line2, n);
8058 u_clearline();
8059 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008060 ex_may_print(eap);
8061}
8062
8063/*
8064 * Print the current line if flags were given to the Ex command.
8065 */
8066 static void
8067ex_may_print(eap)
8068 exarg_T *eap;
8069{
8070 if (eap->flags != 0)
8071 {
8072 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8073 (eap->flags & EXFLAG_LIST));
8074 ex_no_reprint = TRUE;
8075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076}
8077
8078/*
8079 * ":smagic" and ":snomagic".
8080 */
8081 static void
8082ex_submagic(eap)
8083 exarg_T *eap;
8084{
8085 int magic_save = p_magic;
8086
8087 p_magic = (eap->cmdidx == CMD_smagic);
8088 do_sub(eap);
8089 p_magic = magic_save;
8090}
8091
8092/*
8093 * ":join".
8094 */
8095 static void
8096ex_join(eap)
8097 exarg_T *eap;
8098{
8099 curwin->w_cursor.lnum = eap->line1;
8100 if (eap->line1 == eap->line2)
8101 {
8102 if (eap->addr_count >= 2) /* :2,2join does nothing */
8103 return;
8104 if (eap->line2 == curbuf->b_ml.ml_line_count)
8105 {
8106 beep_flush();
8107 return;
8108 }
8109 ++eap->line2;
8110 }
8111 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
8112 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008113 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114}
8115
8116/*
8117 * ":[addr]@r" or ":[addr]*r": execute register
8118 */
8119 static void
8120ex_at(eap)
8121 exarg_T *eap;
8122{
8123 int c;
8124
8125 curwin->w_cursor.lnum = eap->line2;
8126
8127#ifdef USE_ON_FLY_SCROLL
8128 dont_scroll = TRUE; /* disallow scrolling here */
8129#endif
8130
8131 /* get the register name. No name means to use the previous one */
8132 c = *eap->arg;
8133 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8134 c = '@';
8135 /* put the register in mapbuf */
8136 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 else
8141 {
8142 int save_efr = exec_from_reg;
8143
8144 exec_from_reg = TRUE;
8145
8146 /*
8147 * Execute from the typeahead buffer.
8148 * Originally this didn't check for the typeahead buffer to be empty,
8149 * thus could read more Ex commands from stdin. It's not clear why,
8150 * it is certainly unexpected.
8151 */
8152 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
8153 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8154
8155 exec_from_reg = save_efr;
8156 }
8157}
8158
8159/*
8160 * ":!".
8161 */
8162 static void
8163ex_bang(eap)
8164 exarg_T *eap;
8165{
8166 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8167}
8168
8169/*
8170 * ":undo".
8171 */
8172/*ARGSUSED*/
8173 static void
8174ex_undo(eap)
8175 exarg_T *eap;
8176{
8177 u_undo(1);
8178}
8179
8180/*
8181 * ":redo".
8182 */
8183/*ARGSUSED*/
8184 static void
8185ex_redo(eap)
8186 exarg_T *eap;
8187{
8188 u_redo(1);
8189}
8190
8191/*
8192 * ":redir": start/stop redirection.
8193 */
8194 static void
8195ex_redir(eap)
8196 exarg_T *eap;
8197{
8198 char *mode;
8199 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008200 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201
8202 if (STRICMP(eap->arg, "END") == 0)
8203 close_redir();
8204 else
8205 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008206 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008208 ++arg;
8209 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008211 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 mode = "a";
8213 }
8214 else
8215 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008216 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217
8218 close_redir();
8219
8220 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008221 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 if (fname == NULL)
8223 return;
8224#ifdef FEAT_BROWSE
8225 if (cmdmod.browse)
8226 {
8227 char_u *browseFile;
8228
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008229 browseFile = do_browse(BROWSE_SAVE,
8230 (char_u *)_("Save Redirection"),
8231 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 if (browseFile == NULL)
8233 return; /* operation cancelled */
8234 vim_free(fname);
8235 fname = browseFile;
8236 eap->forceit = TRUE; /* since dialog already asked */
8237 }
8238#endif
8239
8240 redir_fd = open_exfile(fname, eap->forceit, mode);
8241 vim_free(fname);
8242 }
8243#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008244 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {
8246 /* redirect to a register a-z (resp. A-Z for appending) */
8247 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008248 ++arg;
8249 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008251 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008253 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008255 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008256 if (*arg == '>' && arg[1] == '>')
8257 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008258 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
8259 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008261 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008263 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008265 if (*arg == '>')
8266 arg++;
8267
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 /* make register empty */
8269 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
8270 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008271 }
8272 if (*arg != NUL)
8273 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008274 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008275 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008276 }
8277 }
8278 else if (*arg == '=' && arg[1] == '>')
8279 {
8280 int append;
8281
8282 /* redirect to a variable */
8283 close_redir();
8284 arg += 2;
8285
8286 if (*arg == '>')
8287 {
8288 ++arg;
8289 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 }
8291 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008292 append = FALSE;
8293
8294 if (var_redir_start(skipwhite(arg), append) == OK)
8295 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 }
8297#endif
8298
8299 /* TODO: redirect to a buffer */
8300
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008302 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 }
8304}
8305
8306/*
8307 * ":redraw": force redraw
8308 */
8309 static void
8310ex_redraw(eap)
8311 exarg_T *eap;
8312{
8313 int r = RedrawingDisabled;
8314 int p = p_lz;
8315
8316 RedrawingDisabled = 0;
8317 p_lz = FALSE;
8318 update_topline();
8319 update_screen(eap->forceit ? CLEAR :
8320#ifdef FEAT_VISUAL
8321 VIsual_active ? INVERTED :
8322#endif
8323 0);
8324#ifdef FEAT_TITLE
8325 if (need_maketitle)
8326 maketitle();
8327#endif
8328 RedrawingDisabled = r;
8329 p_lz = p;
8330
8331 /* Reset msg_didout, so that a message that's there is overwritten. */
8332 msg_didout = FALSE;
8333 msg_col = 0;
8334
8335 out_flush();
8336}
8337
8338/*
8339 * ":redrawstatus": force redraw of status line(s)
8340 */
8341/*ARGSUSED*/
8342 static void
8343ex_redrawstatus(eap)
8344 exarg_T *eap;
8345{
8346#if defined(FEAT_WINDOWS)
8347 int r = RedrawingDisabled;
8348 int p = p_lz;
8349
8350 RedrawingDisabled = 0;
8351 p_lz = FALSE;
8352 if (eap->forceit)
8353 status_redraw_all();
8354 else
8355 status_redraw_curbuf();
8356 update_screen(
8357# ifdef FEAT_VISUAL
8358 VIsual_active ? INVERTED :
8359# endif
8360 0);
8361 RedrawingDisabled = r;
8362 p_lz = p;
8363 out_flush();
8364#endif
8365}
8366
8367 static void
8368close_redir()
8369{
8370 if (redir_fd != NULL)
8371 {
8372 fclose(redir_fd);
8373 redir_fd = NULL;
8374 }
8375#ifdef FEAT_EVAL
8376 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008377 if (redir_vname)
8378 {
8379 var_redir_stop();
8380 redir_vname = 0;
8381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382#endif
8383}
8384
8385#if defined(FEAT_SESSION) && defined(USE_CRNL)
8386# define MKSESSION_NL
8387static int mksession_nl = FALSE; /* use NL only in put_eol() */
8388#endif
8389
8390/*
8391 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8392 */
8393 static void
8394ex_mkrc(eap)
8395 exarg_T *eap;
8396{
8397 FILE *fd;
8398 int failed = FALSE;
8399 char_u *fname;
8400#ifdef FEAT_BROWSE
8401 char_u *browseFile = NULL;
8402#endif
8403#ifdef FEAT_SESSION
8404 int view_session = FALSE;
8405 int using_vdir = FALSE; /* using 'viewdir'? */
8406 char_u *viewFile = NULL;
8407 unsigned *flagp;
8408#endif
8409
8410 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8411 {
8412#ifdef FEAT_SESSION
8413 view_session = TRUE;
8414#else
8415 ex_ni(eap);
8416 return;
8417#endif
8418 }
8419
8420#ifdef FEAT_SESSION
8421 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8422 if (eap->cmdidx == CMD_mkview
8423 && (*eap->arg == NUL
8424 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8425 {
8426 eap->forceit = TRUE;
8427 fname = get_view_file(*eap->arg);
8428 if (fname == NULL)
8429 return;
8430 viewFile = fname;
8431 using_vdir = TRUE;
8432 }
8433 else
8434#endif
8435 if (*eap->arg != NUL)
8436 fname = eap->arg;
8437 else if (eap->cmdidx == CMD_mkvimrc)
8438 fname = (char_u *)VIMRC_FILE;
8439#ifdef FEAT_SESSION
8440 else if (eap->cmdidx == CMD_mksession)
8441 fname = (char_u *)SESSION_FILE;
8442#endif
8443 else
8444 fname = (char_u *)EXRC_FILE;
8445
8446#ifdef FEAT_BROWSE
8447 if (cmdmod.browse)
8448 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008449 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450# ifdef FEAT_SESSION
8451 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8452 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8453# endif
8454 (char_u *)_("Save Setup"),
8455 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8456 if (browseFile == NULL)
8457 goto theend;
8458 fname = browseFile;
8459 eap->forceit = TRUE; /* since dialog already asked */
8460 }
8461#endif
8462
8463#if defined(FEAT_SESSION) && defined(vim_mkdir)
8464 /* When using 'viewdir' may have to create the directory. */
8465 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008466 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467#endif
8468
8469 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8470 if (fd != NULL)
8471 {
8472#ifdef FEAT_SESSION
8473 if (eap->cmdidx == CMD_mkview)
8474 flagp = &vop_flags;
8475 else
8476 flagp = &ssop_flags;
8477#endif
8478
8479#ifdef MKSESSION_NL
8480 /* "unix" in 'sessionoptions': use NL line separator */
8481 if (view_session && (*flagp & SSOP_UNIX))
8482 mksession_nl = TRUE;
8483#endif
8484
8485 /* Write the version command for :mkvimrc */
8486 if (eap->cmdidx == CMD_mkvimrc)
8487 (void)put_line(fd, "version 6.0");
8488
8489#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008490 if (eap->cmdidx == CMD_mksession)
8491 {
8492 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8493 failed = TRUE;
8494 }
8495
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 if (eap->cmdidx != CMD_mkview)
8497#endif
8498 {
8499 /* Write setting 'compatible' first, because it has side effects.
8500 * For that same reason only do it when needed. */
8501 if (p_cp)
8502 (void)put_line(fd, "if !&cp | set cp | endif");
8503 else
8504 (void)put_line(fd, "if &cp | set nocp | endif");
8505 }
8506
8507#ifdef FEAT_SESSION
8508 if (!view_session
8509 || (eap->cmdidx == CMD_mksession
8510 && (*flagp & SSOP_OPTIONS)))
8511#endif
8512 failed |= (makemap(fd, NULL) == FAIL
8513 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8514
8515#ifdef FEAT_SESSION
8516 if (!failed && view_session)
8517 {
8518 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8519 failed = TRUE;
8520 if (eap->cmdidx == CMD_mksession)
8521 {
8522 char_u dirnow[MAXPATHL]; /* current directory */
8523
8524 /*
8525 * Change to session file's dir.
8526 */
8527 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8528 || mch_chdir((char *)dirnow) != 0)
8529 *dirnow = NUL;
8530 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8531 {
8532 if (vim_chdirfile(fname) == OK)
8533 shorten_fnames(TRUE);
8534 }
8535 else if (*dirnow != NUL
8536 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8537 {
8538 (void)mch_chdir((char *)globaldir);
8539 shorten_fnames(TRUE);
8540 }
8541
8542 failed |= (makeopens(fd, dirnow) == FAIL);
8543
8544 /* restore original dir */
8545 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8546 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8547 {
8548 if (mch_chdir((char *)dirnow) != 0)
8549 EMSG(_(e_prev_dir));
8550 shorten_fnames(TRUE);
8551 }
8552 }
8553 else
8554 {
8555 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8556 }
8557 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8558 == FAIL)
8559 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008560 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8561 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008562 if (eap->cmdidx == CMD_mksession)
8563 {
8564 if (put_line(fd, "unlet SessionLoad") == FAIL)
8565 failed = TRUE;
8566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 }
8568#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008569 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8570 failed = TRUE;
8571
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 failed |= fclose(fd);
8573
8574 if (failed)
8575 EMSG(_(e_write));
8576#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8577 else if (eap->cmdidx == CMD_mksession)
8578 {
8579 /* successful session write - set this_session var */
8580 char_u tbuf[MAXPATHL];
8581
8582 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8583 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8584 }
8585#endif
8586#ifdef MKSESSION_NL
8587 mksession_nl = FALSE;
8588#endif
8589 }
8590
8591#ifdef FEAT_BROWSE
8592theend:
8593 vim_free(browseFile);
8594#endif
8595#ifdef FEAT_SESSION
8596 vim_free(viewFile);
8597#endif
8598}
8599
Bram Moolenaardf177f62005-02-22 08:39:57 +00008600#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8601 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008602/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008603 int
8604vim_mkdir_emsg(name, prot)
8605 char_u *name;
8606 int prot;
8607{
8608 if (vim_mkdir(name, prot) != 0)
8609 {
8610 EMSG2(_("E739: Cannot create directory: %s"), name);
8611 return FAIL;
8612 }
8613 return OK;
8614}
8615#endif
8616
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617/*
8618 * Open a file for writing for an Ex command, with some checks.
8619 * Return file descriptor, or NULL on failure.
8620 */
8621 FILE *
8622open_exfile(fname, forceit, mode)
8623 char_u *fname;
8624 int forceit;
8625 char *mode; /* "w" for create new file or "a" for append */
8626{
8627 FILE *fd;
8628
8629#ifdef UNIX
8630 /* with Unix it is possible to open a directory */
8631 if (mch_isdir(fname))
8632 {
8633 EMSG2(_(e_isadir2), fname);
8634 return NULL;
8635 }
8636#endif
8637 if (!forceit && *mode != 'a' && vim_fexists(fname))
8638 {
8639 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8640 return NULL;
8641 }
8642
8643 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8644 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8645
8646 return fd;
8647}
8648
8649/*
8650 * ":mark" and ":k".
8651 */
8652 static void
8653ex_mark(eap)
8654 exarg_T *eap;
8655{
8656 pos_T pos;
8657
8658 if (*eap->arg == NUL) /* No argument? */
8659 EMSG(_(e_argreq));
8660 else if (eap->arg[1] != NUL) /* more than one character? */
8661 EMSG(_(e_trailing));
8662 else
8663 {
8664 pos = curwin->w_cursor; /* save curwin->w_cursor */
8665 curwin->w_cursor.lnum = eap->line2;
8666 beginline(BL_WHITE | BL_FIX);
8667 if (setmark(*eap->arg) == FAIL) /* set mark */
8668 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8669 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8670 }
8671}
8672
8673/*
8674 * Update w_topline, w_leftcol and the cursor position.
8675 */
8676 void
8677update_topline_cursor()
8678{
8679 check_cursor(); /* put cursor on valid line */
8680 update_topline();
8681 if (!curwin->w_p_wrap)
8682 validate_cursor();
8683 update_curswant();
8684}
8685
8686#ifdef FEAT_EX_EXTRA
8687/*
8688 * ":normal[!] {commands}": Execute normal mode commands.
8689 */
8690 static void
8691ex_normal(eap)
8692 exarg_T *eap;
8693{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 int save_msg_scroll = msg_scroll;
8695 int save_restart_edit = restart_edit;
8696 int save_msg_didout = msg_didout;
8697 int save_State = State;
8698 tasave_T tabuf;
8699 int save_insertmode = p_im;
8700 int save_finish_op = finish_op;
8701#ifdef FEAT_MBYTE
8702 char_u *arg = NULL;
8703 int l;
8704 char_u *p;
8705#endif
8706
8707 if (ex_normal_busy >= p_mmd)
8708 {
8709 EMSG(_("E192: Recursive use of :normal too deep"));
8710 return;
8711 }
8712 ++ex_normal_busy;
8713
8714 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8715 restart_edit = 0; /* don't go to Insert mode */
8716 p_im = FALSE; /* don't use 'insertmode' */
8717
8718#ifdef FEAT_MBYTE
8719 /*
8720 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8721 * this for the K_SPECIAL leading byte, otherwise special keys will not
8722 * work.
8723 */
8724 if (has_mbyte)
8725 {
8726 int len = 0;
8727
8728 /* Count the number of characters to be escaped. */
8729 for (p = eap->arg; *p != NUL; ++p)
8730 {
8731# ifdef FEAT_GUI
8732 if (*p == CSI) /* leadbyte CSI */
8733 len += 2;
8734# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008735 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8737# ifdef FEAT_GUI
8738 || *p == CSI
8739# endif
8740 )
8741 len += 2;
8742 }
8743 if (len > 0)
8744 {
8745 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8746 if (arg != NULL)
8747 {
8748 len = 0;
8749 for (p = eap->arg; *p != NUL; ++p)
8750 {
8751 arg[len++] = *p;
8752# ifdef FEAT_GUI
8753 if (*p == CSI)
8754 {
8755 arg[len++] = KS_EXTRA;
8756 arg[len++] = (int)KE_CSI;
8757 }
8758# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008759 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 {
8761 arg[len++] = *++p;
8762 if (*p == K_SPECIAL)
8763 {
8764 arg[len++] = KS_SPECIAL;
8765 arg[len++] = KE_FILLER;
8766 }
8767# ifdef FEAT_GUI
8768 else if (*p == CSI)
8769 {
8770 arg[len++] = KS_EXTRA;
8771 arg[len++] = (int)KE_CSI;
8772 }
8773# endif
8774 }
8775 arg[len] = NUL;
8776 }
8777 }
8778 }
8779 }
8780#endif
8781
8782 /*
8783 * Save the current typeahead. This is required to allow using ":normal"
8784 * from an event handler and makes sure we don't hang when the argument
8785 * ends with half a command.
8786 */
8787 save_typeahead(&tabuf);
8788 if (tabuf.typebuf_valid)
8789 {
8790 /*
8791 * Repeat the :normal command for each line in the range. When no
8792 * range given, execute it just once, without positioning the cursor
8793 * first.
8794 */
8795 do
8796 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 if (eap->addr_count != 0)
8798 {
8799 curwin->w_cursor.lnum = eap->line1++;
8800 curwin->w_cursor.col = 0;
8801 }
8802
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008803 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804#ifdef FEAT_MBYTE
8805 arg != NULL ? arg :
8806#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008807 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 }
8809 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8810 }
8811
8812 /* Might not return to the main loop when in an event handler. */
8813 update_topline_cursor();
8814
8815 /* Restore the previous typeahead. */
8816 restore_typeahead(&tabuf);
8817
8818 --ex_normal_busy;
8819 msg_scroll = save_msg_scroll;
8820 restart_edit = save_restart_edit;
8821 p_im = save_insertmode;
8822 finish_op = save_finish_op;
8823 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8824
8825 /* Restore the state (needed when called from a function executed for
8826 * 'indentexpr'). */
8827 State = save_State;
8828#ifdef FEAT_MBYTE
8829 vim_free(arg);
8830#endif
8831}
8832
8833/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008834 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 */
8836 static void
8837ex_startinsert(eap)
8838 exarg_T *eap;
8839{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008840 if (eap->forceit)
8841 {
8842 coladvance((colnr_T)MAXCOL);
8843 curwin->w_curswant = MAXCOL;
8844 curwin->w_set_curswant = FALSE;
8845 }
8846
Bram Moolenaara40c5002005-01-09 21:16:21 +00008847 /* Ignore the command when already in Insert mode. Inserting an
8848 * expression register that invokes a function can do this. */
8849 if (State & INSERT)
8850 return;
8851
Bram Moolenaar64969662005-12-14 21:59:55 +00008852 if (eap->cmdidx == CMD_startinsert)
8853 restart_edit = 'a';
8854 else if (eap->cmdidx == CMD_startreplace)
8855 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008857 restart_edit = 'V';
8858
8859 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008861 if (eap->cmdidx == CMD_startinsert)
8862 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 curwin->w_curswant = 0; /* avoid MAXCOL */
8864 }
8865}
8866
8867/*
8868 * ":stopinsert"
8869 */
8870/*ARGSUSED*/
8871 static void
8872ex_stopinsert(eap)
8873 exarg_T *eap;
8874{
8875 restart_edit = 0;
8876 stop_insert_mode = TRUE;
8877}
8878#endif
8879
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008880#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8881/*
8882 * Execute normal mode command "cmd".
8883 * "remap" can be REMAP_NONE or REMAP_YES.
8884 */
8885 void
8886exec_normal_cmd(cmd, remap, silent)
8887 char_u *cmd;
8888 int remap;
8889 int silent;
8890{
8891 oparg_T oa;
8892
8893 /*
8894 * Stuff the argument into the typeahead buffer.
8895 * Execute normal_cmd() until there is no typeahead left.
8896 */
8897 clear_oparg(&oa);
8898 finish_op = FALSE;
8899 ins_typebuf(cmd, remap, 0, TRUE, silent);
8900 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8901 && !got_int)
8902 {
8903 update_topline_cursor();
8904 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8905 }
8906}
8907#endif
8908
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909#ifdef FEAT_FIND_ID
8910 static void
8911ex_checkpath(eap)
8912 exarg_T *eap;
8913{
8914 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8915 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8916 (linenr_T)1, (linenr_T)MAXLNUM);
8917}
8918
8919#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8920/*
8921 * ":psearch"
8922 */
8923 static void
8924ex_psearch(eap)
8925 exarg_T *eap;
8926{
8927 g_do_tagpreview = p_pvh;
8928 ex_findpat(eap);
8929 g_do_tagpreview = 0;
8930}
8931#endif
8932
8933 static void
8934ex_findpat(eap)
8935 exarg_T *eap;
8936{
8937 int whole = TRUE;
8938 long n;
8939 char_u *p;
8940 int action;
8941
8942 switch (cmdnames[eap->cmdidx].cmd_name[2])
8943 {
8944 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8945 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8946 action = ACTION_GOTO;
8947 else
8948 action = ACTION_SHOW;
8949 break;
8950 case 'i': /* ":ilist" and ":dlist" */
8951 action = ACTION_SHOW_ALL;
8952 break;
8953 case 'u': /* ":ijump" and ":djump" */
8954 action = ACTION_GOTO;
8955 break;
8956 default: /* ":isplit" and ":dsplit" */
8957 action = ACTION_SPLIT;
8958 break;
8959 }
8960
8961 n = 1;
8962 if (vim_isdigit(*eap->arg)) /* get count */
8963 {
8964 n = getdigits(&eap->arg);
8965 eap->arg = skipwhite(eap->arg);
8966 }
8967 if (*eap->arg == '/') /* Match regexp, not just whole words */
8968 {
8969 whole = FALSE;
8970 ++eap->arg;
8971 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8972 if (*p)
8973 {
8974 *p++ = NUL;
8975 p = skipwhite(p);
8976
8977 /* Check for trailing illegal characters */
8978 if (!ends_excmd(*p))
8979 eap->errmsg = e_trailing;
8980 else
8981 eap->nextcmd = check_nextcmd(p);
8982 }
8983 }
8984 if (!eap->skip)
8985 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8986 whole, !eap->forceit,
8987 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8988 n, action, eap->line1, eap->line2);
8989}
8990#endif
8991
8992#ifdef FEAT_WINDOWS
8993
8994# ifdef FEAT_QUICKFIX
8995/*
8996 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8997 */
8998 static void
8999ex_ptag(eap)
9000 exarg_T *eap;
9001{
9002 g_do_tagpreview = p_pvh;
9003 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9004}
9005
9006/*
9007 * ":pedit"
9008 */
9009 static void
9010ex_pedit(eap)
9011 exarg_T *eap;
9012{
9013 win_T *curwin_save = curwin;
9014
9015 g_do_tagpreview = p_pvh;
9016 prepare_tagpreview();
9017 keep_help_flag = curwin_save->w_buffer->b_help;
9018 do_exedit(eap, NULL);
9019 keep_help_flag = FALSE;
9020 if (curwin != curwin_save && win_valid(curwin_save))
9021 {
9022 /* Return cursor to where we were */
9023 validate_cursor();
9024 redraw_later(VALID);
9025 win_enter(curwin_save, TRUE);
9026 }
9027 g_do_tagpreview = 0;
9028}
9029# endif
9030
9031/*
9032 * ":stag", ":stselect" and ":stjump".
9033 */
9034 static void
9035ex_stag(eap)
9036 exarg_T *eap;
9037{
9038 postponed_split = -1;
9039 postponed_split_flags = cmdmod.split;
9040 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9041 postponed_split_flags = 0;
9042}
9043#endif
9044
9045/*
9046 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9047 */
9048 static void
9049ex_tag(eap)
9050 exarg_T *eap;
9051{
9052 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9053}
9054
9055 static void
9056ex_tag_cmd(eap, name)
9057 exarg_T *eap;
9058 char_u *name;
9059{
9060 int cmd;
9061
9062 switch (name[1])
9063 {
9064 case 'j': cmd = DT_JUMP; /* ":tjump" */
9065 break;
9066 case 's': cmd = DT_SELECT; /* ":tselect" */
9067 break;
9068 case 'p': cmd = DT_PREV; /* ":tprevious" */
9069 break;
9070 case 'N': cmd = DT_PREV; /* ":tNext" */
9071 break;
9072 case 'n': cmd = DT_NEXT; /* ":tnext" */
9073 break;
9074 case 'o': cmd = DT_POP; /* ":pop" */
9075 break;
9076 case 'f': /* ":tfirst" */
9077 case 'r': cmd = DT_FIRST; /* ":trewind" */
9078 break;
9079 case 'l': cmd = DT_LAST; /* ":tlast" */
9080 break;
9081 default: /* ":tag" */
9082#ifdef FEAT_CSCOPE
9083 if (p_cst)
9084 {
9085 do_cstag(eap);
9086 return;
9087 }
9088#endif
9089 cmd = DT_TAG;
9090 break;
9091 }
9092
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009093 if (name[0] == 'l')
9094 {
9095#ifndef FEAT_QUICKFIX
9096 ex_ni(eap);
9097 return;
9098#else
9099 cmd = DT_LTAG;
9100#endif
9101 }
9102
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9104 eap->forceit, TRUE);
9105}
9106
9107/*
9108 * Evaluate cmdline variables.
9109 *
9110 * change '%' to curbuf->b_ffname
9111 * '#' to curwin->w_altfile
9112 * '<cword>' to word under the cursor
9113 * '<cWORD>' to WORD under the cursor
9114 * '<cfile>' to path name under the cursor
9115 * '<sfile>' to sourced file name
9116 * '<afile>' to file name for autocommand
9117 * '<abuf>' to buffer number for autocommand
9118 * '<amatch>' to matching name for autocommand
9119 *
9120 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9121 * "" for error without a message) and NULL is returned.
9122 * Returns an allocated string if a valid match was found.
9123 * Returns NULL if no match was found. "usedlen" then still contains the
9124 * number of characters to skip.
9125 */
9126 char_u *
9127eval_vars(src, usedlen, lnump, errormsg, srcstart)
9128 char_u *src; /* pointer into commandline */
9129 int *usedlen; /* characters after src that are used */
9130 linenr_T *lnump; /* line number for :e command, or NULL */
9131 char_u **errormsg; /* pointer to error message */
9132 char_u *srcstart; /* beginning of valid memory for src */
9133{
9134 int i;
9135 char_u *s;
9136 char_u *result;
9137 char_u *resultbuf = NULL;
9138 int resultlen;
9139 buf_T *buf;
9140 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9141 int spec_idx;
9142#ifdef FEAT_MODIFY_FNAME
9143 int skip_mod = FALSE;
9144#endif
9145 static char *(spec_str[]) =
9146 {
9147 "%",
9148#define SPEC_PERC 0
9149 "#",
9150#define SPEC_HASH 1
9151 "<cword>", /* cursor word */
9152#define SPEC_CWORD 2
9153 "<cWORD>", /* cursor WORD */
9154#define SPEC_CCWORD 3
9155 "<cfile>", /* cursor path name */
9156#define SPEC_CFILE 4
9157 "<sfile>", /* ":so" file name */
9158#define SPEC_SFILE 5
9159#ifdef FEAT_AUTOCMD
9160 "<afile>", /* autocommand file name */
9161# define SPEC_AFILE 6
9162 "<abuf>", /* autocommand buffer number */
9163# define SPEC_ABUF 7
9164 "<amatch>", /* autocommand match name */
9165# define SPEC_AMATCH 8
9166#endif
9167#ifdef FEAT_CLIENTSERVER
9168 "<client>"
9169# define SPEC_CLIENT 9
9170#endif
9171 };
9172#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
9173
9174#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9175 char_u strbuf[30];
9176#endif
9177
9178 *errormsg = NULL;
9179
9180 /*
9181 * Check if there is something to do.
9182 */
9183 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
9184 {
9185 *usedlen = (int)STRLEN(spec_str[spec_idx]);
9186 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
9187 break;
9188 }
9189 if (spec_idx == SPEC_COUNT) /* no match */
9190 {
9191 *usedlen = 1;
9192 return NULL;
9193 }
9194
9195 /*
9196 * Skip when preceded with a backslash "\%" and "\#".
9197 * Note: In "\\%" the % is also not recognized!
9198 */
9199 if (src > srcstart && src[-1] == '\\')
9200 {
9201 *usedlen = 0;
9202 STRCPY(src - 1, src); /* remove backslash */
9203 return NULL;
9204 }
9205
9206 /*
9207 * word or WORD under cursor
9208 */
9209 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9210 {
9211 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9212 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9213 if (resultlen == 0)
9214 {
9215 *errormsg = (char_u *)"";
9216 return NULL;
9217 }
9218 }
9219
9220 /*
9221 * '#': Alternate file name
9222 * '%': Current file name
9223 * File name under the cursor
9224 * File name for autocommand
9225 * and following modifiers
9226 */
9227 else
9228 {
9229 switch (spec_idx)
9230 {
9231 case SPEC_PERC: /* '%': current file */
9232 if (curbuf->b_fname == NULL)
9233 {
9234 result = (char_u *)"";
9235 valid = 0; /* Must have ":p:h" to be valid */
9236 }
9237 else
9238#ifdef RISCOS
9239 /* Always use the full path for RISC OS if possible. */
9240 result = curbuf->b_ffname;
9241 if (result == NULL)
9242 result = curbuf->b_fname;
9243#else
9244 result = curbuf->b_fname;
9245#endif
9246 break;
9247
9248 case SPEC_HASH: /* '#' or "#99": alternate file */
9249 if (src[1] == '#') /* "##": the argument list */
9250 {
9251 result = arg_all();
9252 resultbuf = result;
9253 *usedlen = 2;
9254#ifdef FEAT_MODIFY_FNAME
9255 skip_mod = TRUE;
9256#endif
9257 break;
9258 }
9259 s = src + 1;
9260 i = (int)getdigits(&s);
9261 *usedlen = (int)(s - src); /* length of what we expand */
9262
9263 buf = buflist_findnr(i);
9264 if (buf == NULL)
9265 {
9266 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9267 return NULL;
9268 }
9269 if (lnump != NULL)
9270 *lnump = ECMD_LAST;
9271 if (buf->b_fname == NULL)
9272 {
9273 result = (char_u *)"";
9274 valid = 0; /* Must have ":p:h" to be valid */
9275 }
9276 else
9277 result = buf->b_fname;
9278 break;
9279
9280#ifdef FEAT_SEARCHPATH
9281 case SPEC_CFILE: /* file name under cursor */
9282 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L);
9283 if (result == NULL)
9284 {
9285 *errormsg = (char_u *)"";
9286 return NULL;
9287 }
9288 resultbuf = result; /* remember allocated string */
9289 break;
9290#endif
9291
9292#ifdef FEAT_AUTOCMD
9293 case SPEC_AFILE: /* file name for autocommand */
9294 result = autocmd_fname;
9295 if (result == NULL)
9296 {
9297 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9298 return NULL;
9299 }
9300 break;
9301
9302 case SPEC_ABUF: /* buffer number for autocommand */
9303 if (autocmd_bufnr <= 0)
9304 {
9305 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9306 return NULL;
9307 }
9308 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9309 result = strbuf;
9310 break;
9311
9312 case SPEC_AMATCH: /* match name for autocommand */
9313 result = autocmd_match;
9314 if (result == NULL)
9315 {
9316 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9317 return NULL;
9318 }
9319 break;
9320
9321#endif
9322 case SPEC_SFILE: /* file name for ":so" command */
9323 result = sourcing_name;
9324 if (result == NULL)
9325 {
9326 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9327 return NULL;
9328 }
9329 break;
9330#if defined(FEAT_CLIENTSERVER)
9331 case SPEC_CLIENT: /* Source of last submitted input */
9332 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
9333 result = strbuf;
9334 break;
9335#endif
9336 }
9337
9338 resultlen = (int)STRLEN(result); /* length of new string */
9339 if (src[*usedlen] == '<') /* remove the file name extension */
9340 {
9341 ++*usedlen;
9342#ifdef RISCOS
9343 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9344#else
9345 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9346#endif
9347 resultlen = (int)(s - result);
9348 }
9349#ifdef FEAT_MODIFY_FNAME
9350 else if (!skip_mod)
9351 {
9352 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9353 &resultlen);
9354 if (result == NULL)
9355 {
9356 *errormsg = (char_u *)"";
9357 return NULL;
9358 }
9359 }
9360#endif
9361 }
9362
9363 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9364 {
9365 if (valid != VALID_HEAD + VALID_PATH)
9366 /* xgettext:no-c-format */
9367 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9368 else
9369 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9370 result = NULL;
9371 }
9372 else
9373 result = vim_strnsave(result, resultlen);
9374 vim_free(resultbuf);
9375 return result;
9376}
9377
9378/*
9379 * Concatenate all files in the argument list, separated by spaces, and return
9380 * it in one allocated string.
9381 * Spaces and backslashes in the file names are escaped with a backslash.
9382 * Returns NULL when out of memory.
9383 */
9384 static char_u *
9385arg_all()
9386{
9387 int len;
9388 int idx;
9389 char_u *retval = NULL;
9390 char_u *p;
9391
9392 /*
9393 * Do this loop two times:
9394 * first time: compute the total length
9395 * second time: concatenate the names
9396 */
9397 for (;;)
9398 {
9399 len = 0;
9400 for (idx = 0; idx < ARGCOUNT; ++idx)
9401 {
9402 p = alist_name(&ARGLIST[idx]);
9403 if (p != NULL)
9404 {
9405 if (len > 0)
9406 {
9407 /* insert a space in between names */
9408 if (retval != NULL)
9409 retval[len] = ' ';
9410 ++len;
9411 }
9412 for ( ; *p != NUL; ++p)
9413 {
9414 if (*p == ' ' || *p == '\\')
9415 {
9416 /* insert a backslash */
9417 if (retval != NULL)
9418 retval[len] = '\\';
9419 ++len;
9420 }
9421 if (retval != NULL)
9422 retval[len] = *p;
9423 ++len;
9424 }
9425 }
9426 }
9427
9428 /* second time: break here */
9429 if (retval != NULL)
9430 {
9431 retval[len] = NUL;
9432 break;
9433 }
9434
9435 /* allocate memory */
9436 retval = alloc(len + 1);
9437 if (retval == NULL)
9438 break;
9439 }
9440
9441 return retval;
9442}
9443
9444#if defined(FEAT_AUTOCMD) || defined(PROTO)
9445/*
9446 * Expand the <sfile> string in "arg".
9447 *
9448 * Returns an allocated string, or NULL for any error.
9449 */
9450 char_u *
9451expand_sfile(arg)
9452 char_u *arg;
9453{
9454 char_u *errormsg;
9455 int len;
9456 char_u *result;
9457 char_u *newres;
9458 char_u *repl;
9459 int srclen;
9460 char_u *p;
9461
9462 result = vim_strsave(arg);
9463 if (result == NULL)
9464 return NULL;
9465
9466 for (p = result; *p; )
9467 {
9468 if (STRNCMP(p, "<sfile>", 7) != 0)
9469 ++p;
9470 else
9471 {
9472 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9473 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9474 if (errormsg != NULL)
9475 {
9476 if (*errormsg)
9477 emsg(errormsg);
9478 vim_free(result);
9479 return NULL;
9480 }
9481 if (repl == NULL) /* no match (cannot happen) */
9482 {
9483 p += srclen;
9484 continue;
9485 }
9486 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9487 newres = alloc(len);
9488 if (newres == NULL)
9489 {
9490 vim_free(repl);
9491 vim_free(result);
9492 return NULL;
9493 }
9494 mch_memmove(newres, result, (size_t)(p - result));
9495 STRCPY(newres + (p - result), repl);
9496 len = (int)STRLEN(newres);
9497 STRCAT(newres, p + srclen);
9498 vim_free(repl);
9499 vim_free(result);
9500 result = newres;
9501 p = newres + len; /* continue after the match */
9502 }
9503 }
9504
9505 return result;
9506}
9507#endif
9508
9509#ifdef FEAT_SESSION
9510static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9511static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9512static frame_T *ses_skipframe __ARGS((frame_T *fr));
9513static int ses_do_frame __ARGS((frame_T *fr));
9514static int ses_do_win __ARGS((win_T *wp));
9515static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9516static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9517static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9518
9519/*
9520 * Write openfile commands for the current buffers to an .exrc file.
9521 * Return FAIL on error, OK otherwise.
9522 */
9523 static int
9524makeopens(fd, dirnow)
9525 FILE *fd;
9526 char_u *dirnow; /* Current directory name */
9527{
9528 buf_T *buf;
9529 int only_save_windows = TRUE;
9530 int nr;
9531 int cnr = 1;
9532 int restore_size = TRUE;
9533 win_T *wp;
9534 char_u *sname;
9535 win_T *edited_win = NULL;
9536
9537 if (ssop_flags & SSOP_BUFFERS)
9538 only_save_windows = FALSE; /* Save ALL buffers */
9539
9540 /*
9541 * Begin by setting the this_session variable, and then other
9542 * sessionable variables.
9543 */
9544#ifdef FEAT_EVAL
9545 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9546 return FAIL;
9547 if (ssop_flags & SSOP_GLOBALS)
9548 if (store_session_globals(fd) == FAIL)
9549 return FAIL;
9550#endif
9551
9552 /*
9553 * Close all windows but one.
9554 */
9555 if (put_line(fd, "silent only") == FAIL)
9556 return FAIL;
9557
9558 /*
9559 * Now a :cd command to the session directory or the current directory
9560 */
9561 if (ssop_flags & SSOP_SESDIR)
9562 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009563 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9564 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 return FAIL;
9566 }
9567 else if (ssop_flags & SSOP_CURDIR)
9568 {
9569 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9570 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009571 || fputs("cd ", fd) < 0
9572 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9573 || put_eol(fd) == FAIL)
9574 {
9575 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 vim_free(sname);
9579 }
9580
9581 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009582 * If there is an empty, unnamed buffer we will wipe it out later.
9583 * Remember the buffer number.
9584 */
9585 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9586 return FAIL;
9587 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9588 return FAIL;
9589 if (put_line(fd, "endif") == FAIL)
9590 return FAIL;
9591
9592 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 * Now save the current files, current buffer first.
9594 */
9595 if (put_line(fd, "set shortmess=aoO") == FAIL)
9596 return FAIL;
9597
9598 /* Now put the other buffers into the buffer list */
9599 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9600 {
9601 if (!(only_save_windows && buf->b_nwindows == 0)
9602 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9603 && buf->b_fname != NULL
9604 && buf->b_p_bl)
9605 {
9606 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9607 : buf->b_wininfo->wi_fpos.lnum) < 0
9608 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9609 return FAIL;
9610 }
9611 }
9612
9613 /* the global argument list */
9614 if (ses_arglist(fd, "args", &global_alist.al_ga,
9615 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9616 return FAIL;
9617
9618 if (ssop_flags & SSOP_RESIZE)
9619 {
9620 /* Note: after the restore we still check it worked!*/
9621 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9622 || put_eol(fd) == FAIL)
9623 return FAIL;
9624 }
9625
9626#ifdef FEAT_GUI
9627 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9628 {
9629 int x, y;
9630
9631 if (gui_mch_get_winpos(&x, &y) == OK)
9632 {
9633 /* Note: after the restore we still check it worked!*/
9634 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9635 return FAIL;
9636 }
9637 }
9638#endif
9639
9640 /*
9641 * Before creating the window layout, try loading one file. If this is
9642 * aborted we don't end up with a number of useless windows.
9643 * This may have side effects! (e.g., compressed or network file).
9644 */
9645 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9646 {
9647 if (ses_do_win(wp)
9648 && wp->w_buffer->b_ffname != NULL
9649 && !wp->w_buffer->b_help
9650#ifdef FEAT_QUICKFIX
9651 && !bt_nofile(wp->w_buffer)
9652#endif
9653 )
9654 {
9655 if (fputs("edit ", fd) < 0
9656 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9657 return FAIL;
9658 if (!wp->w_arg_idx_invalid)
9659 edited_win = wp;
9660 break;
9661 }
9662 }
9663
9664 /*
9665 * Save current window layout.
9666 */
9667 if (put_line(fd, "set splitbelow splitright") == FAIL)
9668 return FAIL;
9669 if (ses_win_rec(fd, topframe) == FAIL)
9670 return FAIL;
9671 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9672 return FAIL;
9673 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9674 return FAIL;
9675
9676 /*
9677 * Check if window sizes can be restored (no windows omitted).
9678 * Remember the window number of the current window after restoring.
9679 */
9680 nr = 0;
9681 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9682 {
9683 if (ses_do_win(wp))
9684 ++nr;
9685 else
9686 restore_size = FALSE;
9687 if (curwin == wp)
9688 cnr = nr;
9689 }
9690
9691 /* Go to the first window. */
9692 if (put_line(fd, "wincmd t") == FAIL)
9693 return FAIL;
9694
9695 /*
9696 * If more than one window, see if sizes can be restored.
9697 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9698 * resized when moving between windows.
9699 * Do this before restoring the view, so that the topline and the cursor
9700 * can be set. This is done again below.
9701 */
9702 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9703 return FAIL;
9704 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9705 return FAIL;
9706
9707 /*
9708 * Restore the view of the window (options, file, cursor, etc.).
9709 */
9710 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9711 {
9712 if (!ses_do_win(wp))
9713 continue;
9714 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9715 return FAIL;
9716 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9717 return FAIL;
9718 }
9719
9720 /*
9721 * Restore cursor to the current window if it's not the first one.
9722 */
9723 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9724 return FAIL;
9725
9726 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009727 * Wipe out an empty unnamed buffer we started in.
9728 */
9729 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9730 return FAIL;
9731 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9732 return FAIL;
9733 if (put_line(fd, "endif") == FAIL)
9734 return FAIL;
9735 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9736 return FAIL;
9737
9738 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 * Restore window sizes again after jumping around in windows, because the
9740 * current window has a minimum size while others may not.
9741 */
9742 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9743 return FAIL;
9744
9745 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9746 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9747 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9748 return FAIL;
9749
9750 /*
9751 * Lastly, execute the x.vim file if it exists.
9752 */
9753 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9754 || put_line(fd, "if file_readable(s:sx)") == FAIL
9755 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9756 || put_line(fd, "endif") == FAIL)
9757 return FAIL;
9758
9759 return OK;
9760}
9761
9762 static int
9763ses_winsizes(fd, restore_size)
9764 FILE *fd;
9765 int restore_size;
9766{
9767 int n = 0;
9768 win_T *wp;
9769
9770 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9771 {
9772 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9773 {
9774 if (!ses_do_win(wp))
9775 continue;
9776 ++n;
9777
9778 /* restore height when not full height */
9779 if (wp->w_height + wp->w_status_height < topframe->fr_height
9780 && (fprintf(fd,
9781 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9782 n, (long)wp->w_height, Rows / 2, Rows) < 0
9783 || put_eol(fd) == FAIL))
9784 return FAIL;
9785
9786 /* restore width when not full width */
9787 if (wp->w_width < Columns && (fprintf(fd,
9788 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9789 n, (long)wp->w_width, Columns / 2, Columns) < 0
9790 || put_eol(fd) == FAIL))
9791 return FAIL;
9792 }
9793 }
9794 else
9795 {
9796 /* Just equalise window sizes */
9797 if (put_line(fd, "wincmd =") == FAIL)
9798 return FAIL;
9799 }
9800 return OK;
9801}
9802
9803/*
9804 * Write commands to "fd" to recursively create windows for frame "fr",
9805 * horizontally and vertically split.
9806 * After the commands the last window in the frame is the current window.
9807 * Returns FAIL when writing the commands to "fd" fails.
9808 */
9809 static int
9810ses_win_rec(fd, fr)
9811 FILE *fd;
9812 frame_T *fr;
9813{
9814 frame_T *frc;
9815 int count = 0;
9816
9817 if (fr->fr_layout != FR_LEAF)
9818 {
9819 /* Find first frame that's not skipped and then create a window for
9820 * each following one (first frame is already there). */
9821 frc = ses_skipframe(fr->fr_child);
9822 if (frc != NULL)
9823 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9824 {
9825 /* Make window as big as possible so that we have lots of room
9826 * to split. */
9827 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9828 || put_line(fd, fr->fr_layout == FR_COL
9829 ? "split" : "vsplit") == FAIL)
9830 return FAIL;
9831 ++count;
9832 }
9833
9834 /* Go back to the first window. */
9835 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9836 ? "%dwincmd k" : "%dwincmd h", count) < 0
9837 || put_eol(fd) == FAIL))
9838 return FAIL;
9839
9840 /* Recursively create frames/windows in each window of this column or
9841 * row. */
9842 frc = ses_skipframe(fr->fr_child);
9843 while (frc != NULL)
9844 {
9845 ses_win_rec(fd, frc);
9846 frc = ses_skipframe(frc->fr_next);
9847 /* Go to next window. */
9848 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9849 return FAIL;
9850 }
9851 }
9852 return OK;
9853}
9854
9855/*
9856 * Skip frames that don't contain windows we want to save in the Session.
9857 * Returns NULL when there none.
9858 */
9859 static frame_T *
9860ses_skipframe(fr)
9861 frame_T *fr;
9862{
9863 frame_T *frc;
9864
9865 for (frc = fr; frc != NULL; frc = frc->fr_next)
9866 if (ses_do_frame(frc))
9867 break;
9868 return frc;
9869}
9870
9871/*
9872 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9873 * the Session.
9874 */
9875 static int
9876ses_do_frame(fr)
9877 frame_T *fr;
9878{
9879 frame_T *frc;
9880
9881 if (fr->fr_layout == FR_LEAF)
9882 return ses_do_win(fr->fr_win);
9883 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9884 if (ses_do_frame(frc))
9885 return TRUE;
9886 return FALSE;
9887}
9888
9889/*
9890 * Return non-zero if window "wp" is to be stored in the Session.
9891 */
9892 static int
9893ses_do_win(wp)
9894 win_T *wp;
9895{
9896 if (wp->w_buffer->b_fname == NULL
9897#ifdef FEAT_QUICKFIX
9898 /* When 'buftype' is "nofile" can't restore the window contents. */
9899 || bt_nofile(wp->w_buffer)
9900#endif
9901 )
9902 return (ssop_flags & SSOP_BLANK);
9903 if (wp->w_buffer->b_help)
9904 return (ssop_flags & SSOP_HELP);
9905 return TRUE;
9906}
9907
9908/*
9909 * Write commands to "fd" to restore the view of a window.
9910 * Caller must make sure 'scrolloff' is zero.
9911 */
9912 static int
9913put_view(fd, wp, add_edit, flagp)
9914 FILE *fd;
9915 win_T *wp;
9916 int add_edit; /* add ":edit" command to view */
9917 unsigned *flagp; /* vop_flags or ssop_flags */
9918{
9919 win_T *save_curwin;
9920 int f;
9921 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009922 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923
9924 /* Always restore cursor position for ":mksession". For ":mkview" only
9925 * when 'viewoptions' contains "cursor". */
9926 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9927
9928 /*
9929 * Local argument list.
9930 */
9931 if (wp->w_alist == &global_alist)
9932 {
9933 if (put_line(fd, "argglobal") == FAIL)
9934 return FAIL;
9935 }
9936 else
9937 {
9938 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9939 flagp == &vop_flags
9940 || !(*flagp & SSOP_CURDIR)
9941 || wp->w_localdir != NULL, flagp) == FAIL)
9942 return FAIL;
9943 }
9944
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009945 /* Only when part of a session: restore the argument index. Some
9946 * arguments may have been deleted, check if the index is valid. */
9947 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
9948 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 {
9950 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9951 || put_eol(fd) == FAIL)
9952 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009953 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 }
9955
9956 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009957 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 {
9959 /*
9960 * Load the file.
9961 */
9962 if (wp->w_buffer->b_ffname != NULL
9963#ifdef FEAT_QUICKFIX
9964 && !bt_nofile(wp->w_buffer)
9965#endif
9966 )
9967 {
9968 /*
9969 * Editing a file in this buffer: use ":edit file".
9970 * This may have side effects! (e.g., compressed or network file).
9971 */
9972 if (fputs("edit ", fd) < 0
9973 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9974 return FAIL;
9975 }
9976 else
9977 {
9978 /* No file in this buffer, just make it empty. */
9979 if (put_line(fd, "enew") == FAIL)
9980 return FAIL;
9981#ifdef FEAT_QUICKFIX
9982 if (wp->w_buffer->b_ffname != NULL)
9983 {
9984 /* The buffer does have a name, but it's not a file name. */
9985 if (fputs("file ", fd) < 0
9986 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9987 return FAIL;
9988 }
9989#endif
9990 do_cursor = FALSE;
9991 }
9992 }
9993
9994 /*
9995 * Local mappings and abbreviations.
9996 */
9997 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9998 && makemap(fd, wp->w_buffer) == FAIL)
9999 return FAIL;
10000
10001 /*
10002 * Local options. Need to go to the window temporarily.
10003 * Store only local values when using ":mkview" and when ":mksession" is
10004 * used and 'sessionoptions' doesn't include "options".
10005 * Some folding options are always stored when "folds" is included,
10006 * otherwise the folds would not be restored correctly.
10007 */
10008 save_curwin = curwin;
10009 curwin = wp;
10010 curbuf = curwin->w_buffer;
10011 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10012 f = makeset(fd, OPT_LOCAL,
10013 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10014#ifdef FEAT_FOLDING
10015 else if (*flagp & SSOP_FOLDS)
10016 f = makefoldset(fd);
10017#endif
10018 else
10019 f = OK;
10020 curwin = save_curwin;
10021 curbuf = curwin->w_buffer;
10022 if (f == FAIL)
10023 return FAIL;
10024
10025#ifdef FEAT_FOLDING
10026 /*
10027 * Save Folds when 'buftype' is empty and for help files.
10028 */
10029 if ((*flagp & SSOP_FOLDS)
10030 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010031# ifdef FEAT_QUICKFIX
10032 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10033# endif
10034 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 {
10036 if (put_folds(fd, wp) == FAIL)
10037 return FAIL;
10038 }
10039#endif
10040
10041 /*
10042 * Set the cursor after creating folds, since that moves the cursor.
10043 */
10044 if (do_cursor)
10045 {
10046
10047 /* Restore the cursor line in the file and relatively in the
10048 * window. Don't use "G", it changes the jumplist. */
10049 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10050 (long)wp->w_cursor.lnum,
10051 (long)(wp->w_cursor.lnum - wp->w_topline),
10052 (long)wp->w_height / 2, (long)wp->w_height) < 0
10053 || put_eol(fd) == FAIL
10054 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10055 || put_line(fd, "exe s:l") == FAIL
10056 || put_line(fd, "normal! zt") == FAIL
10057 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10058 || put_eol(fd) == FAIL)
10059 return FAIL;
10060 /* Restore the cursor column and left offset when not wrapping. */
10061 if (wp->w_cursor.col == 0)
10062 {
10063 if (put_line(fd, "normal! 0") == FAIL)
10064 return FAIL;
10065 }
10066 else
10067 {
10068 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10069 {
10070 if (fprintf(fd,
10071 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10072 (long)wp->w_cursor.col,
10073 (long)(wp->w_cursor.col - wp->w_leftcol),
10074 (long)wp->w_width / 2, (long)wp->w_width) < 0
10075 || put_eol(fd) == FAIL
10076 || put_line(fd, "if s:c > 0") == FAIL
10077 || fprintf(fd,
10078 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10079 (long)wp->w_cursor.col) < 0
10080 || put_eol(fd) == FAIL
10081 || put_line(fd, "else") == FAIL
10082 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10083 || put_eol(fd) == FAIL
10084 || put_line(fd, "endif") == FAIL)
10085 return FAIL;
10086 }
10087 else
10088 {
10089 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10090 || put_eol(fd) == FAIL)
10091 return FAIL;
10092 }
10093 }
10094 }
10095
10096 /*
10097 * Local directory.
10098 */
10099 if (wp->w_localdir != NULL)
10100 {
10101 if (fputs("lcd ", fd) < 0
10102 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10103 || put_eol(fd) == FAIL)
10104 return FAIL;
10105 }
10106
10107 return OK;
10108}
10109
10110/*
10111 * Write an argument list to the session file.
10112 * Returns FAIL if writing fails.
10113 */
10114 static int
10115ses_arglist(fd, cmd, gap, fullname, flagp)
10116 FILE *fd;
10117 char *cmd;
10118 garray_T *gap;
10119 int fullname; /* TRUE: use full path name */
10120 unsigned *flagp;
10121{
10122 int i;
10123 char_u buf[MAXPATHL];
10124 char_u *s;
10125
10126 if (gap->ga_len == 0)
10127 return put_line(fd, "silent! argdel *");
10128 if (fputs(cmd, fd) < 0)
10129 return FAIL;
10130 for (i = 0; i < gap->ga_len; ++i)
10131 {
10132 /* NULL file names are skipped (only happens when out of memory). */
10133 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10134 if (s != NULL)
10135 {
10136 if (fullname)
10137 {
10138 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10139 s = buf;
10140 }
10141 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10142 return FAIL;
10143 }
10144 }
10145 return put_eol(fd);
10146}
10147
10148/*
10149 * Write a buffer name to the session file.
10150 * Also ends the line.
10151 * Returns FAIL if writing fails.
10152 */
10153 static int
10154ses_fname(fd, buf, flagp)
10155 FILE *fd;
10156 buf_T *buf;
10157 unsigned *flagp;
10158{
10159 char_u *name;
10160
10161 /* Use the short file name if the current directory is known at the time
10162 * the session file will be sourced. Don't do this for ":mkview", we
10163 * don't know the current directory. */
10164 if (buf->b_sfname != NULL
10165 && flagp == &ssop_flags
10166 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
10167 name = buf->b_sfname;
10168 else
10169 name = buf->b_ffname;
10170 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10171 return FAIL;
10172 return OK;
10173}
10174
10175/*
10176 * Write a file name to the session file.
10177 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10178 * characters.
10179 * Returns FAIL if writing fails.
10180 */
10181 static int
10182ses_put_fname(fd, name, flagp)
10183 FILE *fd;
10184 char_u *name;
10185 unsigned *flagp;
10186{
10187 char_u *sname;
10188 int retval = OK;
10189 int c;
10190
10191 sname = home_replace_save(NULL, name);
10192 if (sname != NULL)
10193 name = sname;
10194 while (*name != NUL)
10195 {
10196#ifdef FEAT_MBYTE
10197 {
10198 int l;
10199
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010200 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 {
10202 /* copy a multibyte char */
10203 while (--l >= 0)
10204 {
10205 if (putc(*name, fd) != *name)
10206 retval = FAIL;
10207 ++name;
10208 }
10209 continue;
10210 }
10211 }
10212#endif
10213 c = *name++;
10214 if (c == '\\' && (*flagp & SSOP_SLASH))
10215 /* change a backslash to a forward slash */
10216 c = '/';
10217 else if ((vim_strchr(escape_chars, c) != NULL
10218#ifdef BACKSLASH_IN_FILENAME
10219 && c != '\\'
10220#endif
10221 ) || c == '#' || c == '%')
10222 {
10223 /* escape a special character with a backslash */
10224 if (putc('\\', fd) != '\\')
10225 retval = FAIL;
10226 }
10227 if (putc(c, fd) != c)
10228 retval = FAIL;
10229 }
10230 vim_free(sname);
10231 return retval;
10232}
10233
10234/*
10235 * ":loadview [nr]"
10236 */
10237 static void
10238ex_loadview(eap)
10239 exarg_T *eap;
10240{
10241 char_u *fname;
10242
10243 fname = get_view_file(*eap->arg);
10244 if (fname != NULL)
10245 {
10246 do_source(fname, FALSE, FALSE);
10247 vim_free(fname);
10248 }
10249}
10250
10251/*
10252 * Get the name of the view file for the current buffer.
10253 */
10254 static char_u *
10255get_view_file(c)
10256 int c;
10257{
10258 int len = 0;
10259 char_u *p, *s;
10260 char_u *retval;
10261 char_u *sname;
10262
10263 if (curbuf->b_ffname == NULL)
10264 {
10265 EMSG(_(e_noname));
10266 return NULL;
10267 }
10268 sname = home_replace_save(NULL, curbuf->b_ffname);
10269 if (sname == NULL)
10270 return NULL;
10271
10272 /*
10273 * We want a file name without separators, because we're not going to make
10274 * a directory.
10275 * "normal" path separator -> "=+"
10276 * "=" -> "=="
10277 * ":" path separator -> "=-"
10278 */
10279 for (p = sname; *p; ++p)
10280 if (*p == '=' || vim_ispathsep(*p))
10281 ++len;
10282 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10283 if (retval != NULL)
10284 {
10285 STRCPY(retval, p_vdir);
10286 add_pathsep(retval);
10287 s = retval + STRLEN(retval);
10288 for (p = sname; *p; ++p)
10289 {
10290 if (*p == '=')
10291 {
10292 *s++ = '=';
10293 *s++ = '=';
10294 }
10295 else if (vim_ispathsep(*p))
10296 {
10297 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010298#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 || defined(VMS)
10300 if (*p == ':')
10301 *s++ = '-';
10302 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010304 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 }
10306 else
10307 *s++ = *p;
10308 }
10309 *s++ = '=';
10310 *s++ = c;
10311 STRCPY(s, ".vim");
10312 }
10313
10314 vim_free(sname);
10315 return retval;
10316}
10317
10318#endif /* FEAT_SESSION */
10319
10320/*
10321 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10322 * Return FAIL for a write error.
10323 */
10324 int
10325put_eol(fd)
10326 FILE *fd;
10327{
10328 if (
10329#ifdef USE_CRNL
10330 (
10331# ifdef MKSESSION_NL
10332 !mksession_nl &&
10333# endif
10334 (putc('\r', fd) < 0)) ||
10335#endif
10336 (putc('\n', fd) < 0))
10337 return FAIL;
10338 return OK;
10339}
10340
10341/*
10342 * Write a line to "fd".
10343 * Return FAIL for a write error.
10344 */
10345 int
10346put_line(fd, s)
10347 FILE *fd;
10348 char *s;
10349{
10350 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10351 return FAIL;
10352 return OK;
10353}
10354
10355#ifdef FEAT_VIMINFO
10356/*
10357 * ":rviminfo" and ":wviminfo".
10358 */
10359 static void
10360ex_viminfo(eap)
10361 exarg_T *eap;
10362{
10363 char_u *save_viminfo;
10364
10365 save_viminfo = p_viminfo;
10366 if (*p_viminfo == NUL)
10367 p_viminfo = (char_u *)"'100";
10368 if (eap->cmdidx == CMD_rviminfo)
10369 {
10370 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
10371 EMSG(_("E195: Cannot open viminfo file for reading"));
10372 }
10373 else
10374 write_viminfo(eap->arg, eap->forceit);
10375 p_viminfo = save_viminfo;
10376}
10377#endif
10378
10379#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010380/*
10381 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010382 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 void
10385dialog_msg(buff, format, fname)
10386 char_u *buff;
10387 char *format;
10388 char_u *fname;
10389{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 if (fname == NULL)
10391 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010392 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393}
10394#endif
10395
10396/*
10397 * ":behave {mswin,xterm}"
10398 */
10399 static void
10400ex_behave(eap)
10401 exarg_T *eap;
10402{
10403 if (STRCMP(eap->arg, "mswin") == 0)
10404 {
10405 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10406 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10407 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10408 set_option_value((char_u *)"keymodel", 0L,
10409 (char_u *)"startsel,stopsel", 0);
10410 }
10411 else if (STRCMP(eap->arg, "xterm") == 0)
10412 {
10413 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10414 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10415 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10416 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10417 }
10418 else
10419 EMSG2(_(e_invarg2), eap->arg);
10420}
10421
10422#ifdef FEAT_AUTOCMD
10423static int filetype_detect = FALSE;
10424static int filetype_plugin = FALSE;
10425static int filetype_indent = FALSE;
10426
10427/*
10428 * ":filetype [plugin] [indent] {on,off,detect}"
10429 * on: Load the filetype.vim file to install autocommands for file types.
10430 * off: Load the ftoff.vim file to remove all autocommands for file types.
10431 * plugin on: load filetype.vim and ftplugin.vim
10432 * plugin off: load ftplugof.vim
10433 * indent on: load filetype.vim and indent.vim
10434 * indent off: load indoff.vim
10435 */
10436 static void
10437ex_filetype(eap)
10438 exarg_T *eap;
10439{
10440 char_u *arg = eap->arg;
10441 int plugin = FALSE;
10442 int indent = FALSE;
10443
10444 if (*eap->arg == NUL)
10445 {
10446 /* Print current status. */
10447 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10448 filetype_detect ? "ON" : "OFF",
10449 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10450 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10451 return;
10452 }
10453
10454 /* Accept "plugin" and "indent" in any order. */
10455 for (;;)
10456 {
10457 if (STRNCMP(arg, "plugin", 6) == 0)
10458 {
10459 plugin = TRUE;
10460 arg = skipwhite(arg + 6);
10461 continue;
10462 }
10463 if (STRNCMP(arg, "indent", 6) == 0)
10464 {
10465 indent = TRUE;
10466 arg = skipwhite(arg + 6);
10467 continue;
10468 }
10469 break;
10470 }
10471 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10472 {
10473 if (*arg == 'o' || !filetype_detect)
10474 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010475 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 filetype_detect = TRUE;
10477 if (plugin)
10478 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010479 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 filetype_plugin = TRUE;
10481 }
10482 if (indent)
10483 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010484 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 filetype_indent = TRUE;
10486 }
10487 }
10488 if (*arg == 'd')
10489 {
10490 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +000010491 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 }
10493 }
10494 else if (STRCMP(arg, "off") == 0)
10495 {
10496 if (plugin || indent)
10497 {
10498 if (plugin)
10499 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010500 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 filetype_plugin = FALSE;
10502 }
10503 if (indent)
10504 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010505 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 filetype_indent = FALSE;
10507 }
10508 }
10509 else
10510 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010511 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 filetype_detect = FALSE;
10513 }
10514 }
10515 else
10516 EMSG2(_(e_invarg2), arg);
10517}
10518
10519/*
10520 * ":setfiletype {name}"
10521 */
10522 static void
10523ex_setfiletype(eap)
10524 exarg_T *eap;
10525{
10526 if (!did_filetype)
10527 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10528}
10529#endif
10530
10531/*ARGSUSED*/
10532 static void
10533ex_digraphs(eap)
10534 exarg_T *eap;
10535{
10536#ifdef FEAT_DIGRAPHS
10537 if (*eap->arg != NUL)
10538 putdigraph(eap->arg);
10539 else
10540 listdigraphs();
10541#else
10542 EMSG(_("E196: No digraphs in this version"));
10543#endif
10544}
10545
10546 static void
10547ex_set(eap)
10548 exarg_T *eap;
10549{
10550 int flags = 0;
10551
10552 if (eap->cmdidx == CMD_setlocal)
10553 flags = OPT_LOCAL;
10554 else if (eap->cmdidx == CMD_setglobal)
10555 flags = OPT_GLOBAL;
10556#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10557 if (cmdmod.browse && flags == 0)
10558 ex_options(eap);
10559 else
10560#endif
10561 (void)do_set(eap->arg, flags);
10562}
10563
10564#ifdef FEAT_SEARCH_EXTRA
10565/*
10566 * ":nohlsearch"
10567 */
10568/*ARGSUSED*/
10569 static void
10570ex_nohlsearch(eap)
10571 exarg_T *eap;
10572{
10573 no_hlsearch = TRUE;
10574 redraw_all_later(NOT_VALID);
10575}
10576
10577/*
10578 * ":match {group} {pattern}"
10579 * Sets nextcmd to the start of the next command, if any. Also called when
10580 * skipping commands to find the next command.
10581 */
10582 static void
10583ex_match(eap)
10584 exarg_T *eap;
10585{
10586 char_u *p;
10587 char_u *end;
10588 int c;
10589
10590 /* First clear any old pattern. */
10591 if (!eap->skip)
10592 {
10593 vim_free(curwin->w_match.regprog);
10594 curwin->w_match.regprog = NULL;
10595 redraw_later(NOT_VALID); /* always need a redraw */
10596 }
10597
10598 if (ends_excmd(*eap->arg))
10599 end = eap->arg;
10600 else if ((STRNICMP(eap->arg, "none", 4) == 0
10601 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10602 end = eap->arg + 4;
10603 else
10604 {
10605 p = skiptowhite(eap->arg);
10606 if (!eap->skip)
10607 {
10608 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg));
10609 if (curwin->w_match_id == 0)
10610 {
10611 EMSG2(_(e_nogroup), eap->arg);
10612 return;
10613 }
10614 }
10615 p = skipwhite(p);
10616 if (*p == NUL)
10617 {
10618 /* There must be two arguments. */
10619 EMSG2(_(e_invarg2), eap->arg);
10620 return;
10621 }
10622 end = skip_regexp(p + 1, *p, TRUE, NULL);
10623 if (!eap->skip)
10624 {
10625 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10626 {
10627 eap->errmsg = e_trailing;
10628 return;
10629 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010630 if (*end != *p)
10631 {
10632 EMSG2(_(e_invarg2), p);
10633 return;
10634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635
10636 c = *end;
10637 *end = NUL;
10638 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC);
10639 *end = c;
10640 if (curwin->w_match.regprog == NULL)
10641 {
10642 EMSG2(_(e_invarg2), p);
10643 return;
10644 }
10645 }
10646 }
10647 eap->nextcmd = find_nextcmd(end);
10648}
10649#endif
10650
10651#ifdef FEAT_CRYPT
10652/*
10653 * ":X": Get crypt key
10654 */
10655/*ARGSUSED*/
10656 static void
10657ex_X(eap)
10658 exarg_T *eap;
10659{
10660 (void)get_crypt_key(TRUE, TRUE);
10661}
10662#endif
10663
10664#ifdef FEAT_FOLDING
10665 static void
10666ex_fold(eap)
10667 exarg_T *eap;
10668{
10669 if (foldManualAllowed(TRUE))
10670 foldCreate(eap->line1, eap->line2);
10671}
10672
10673 static void
10674ex_foldopen(eap)
10675 exarg_T *eap;
10676{
10677 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10678 eap->forceit, FALSE);
10679}
10680
10681 static void
10682ex_folddo(eap)
10683 exarg_T *eap;
10684{
10685 linenr_T lnum;
10686
10687 /* First set the marks for all lines closed/open. */
10688 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10689 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10690 ml_setmarked(lnum);
10691
10692 /* Execute the command on the marked lines. */
10693 global_exe(eap->arg);
10694 ml_clearmarked(); /* clear rest of the marks */
10695}
10696#endif