blob: ebfac55fd7e2a6dda57393204e5bb97544f92b11 [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
127
128static int check_more __ARGS((int, int));
129static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000130static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000132 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133static void ex_script_ni __ARGS((exarg_T *eap));
134#endif
135static char_u *invalid_range __ARGS((exarg_T *eap));
136static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000137#ifdef FEAT_QUICKFIX
138static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
141static void ex_highlight __ARGS((exarg_T *eap));
142static void ex_colorscheme __ARGS((exarg_T *eap));
143static void ex_quit __ARGS((exarg_T *eap));
144static void ex_cquit __ARGS((exarg_T *eap));
145static void ex_quit_all __ARGS((exarg_T *eap));
146#ifdef FEAT_WINDOWS
147static void ex_close __ARGS((exarg_T *eap));
148static void ex_win_close __ARGS((exarg_T *eap, win_T *win));
149static void ex_only __ARGS((exarg_T *eap));
150static void ex_all __ARGS((exarg_T *eap));
151static void ex_resize __ARGS((exarg_T *eap));
152static void ex_stag __ARGS((exarg_T *eap));
153#else
154# define ex_close ex_ni
155# define ex_only ex_ni
156# define ex_all ex_ni
157# define ex_resize ex_ni
158# define ex_splitview ex_ni
159# define ex_stag ex_ni
160#endif
161#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
162static void ex_pclose __ARGS((exarg_T *eap));
163static void ex_ptag __ARGS((exarg_T *eap));
164static void ex_pedit __ARGS((exarg_T *eap));
165#else
166# define ex_pclose ex_ni
167# define ex_ptag ex_ni
168# define ex_pedit ex_ni
169#endif
170static void ex_hide __ARGS((exarg_T *eap));
171static void ex_stop __ARGS((exarg_T *eap));
172static void ex_exit __ARGS((exarg_T *eap));
173static void ex_print __ARGS((exarg_T *eap));
174#ifdef FEAT_BYTEOFF
175static void ex_goto __ARGS((exarg_T *eap));
176#else
177# define ex_goto ex_ni
178#endif
179static void ex_shell __ARGS((exarg_T *eap));
180static void ex_preserve __ARGS((exarg_T *eap));
181static void ex_recover __ARGS((exarg_T *eap));
182#ifndef FEAT_LISTCMDS
183# define ex_argedit ex_ni
184# define ex_argadd ex_ni
185# define ex_argdelete ex_ni
186# define ex_listdo ex_ni
187#endif
188static void ex_mode __ARGS((exarg_T *eap));
189static void ex_wrongmodifier __ARGS((exarg_T *eap));
190static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000191static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192static void ex_edit __ARGS((exarg_T *eap));
193#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
194# define ex_drop ex_ni
195#endif
196#ifndef FEAT_GUI
197# define ex_gui ex_nogui
198static void ex_nogui __ARGS((exarg_T *eap));
199#endif
200#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
201static void ex_tearoff __ARGS((exarg_T *eap));
202#else
203# define ex_tearoff ex_ni
204#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000205#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_KDE) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206static void ex_popup __ARGS((exarg_T *eap));
207#else
208# define ex_popup ex_ni
209#endif
210#ifndef FEAT_GUI_MSWIN
211# define ex_simalt ex_ni
212#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000213#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_KDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214# define gui_mch_find_dialog ex_ni
215# define gui_mch_replace_dialog ex_ni
216#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000217#if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_KDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218# define ex_helpfind ex_ni
219#endif
220#ifndef FEAT_CSCOPE
221# define do_cscope ex_ni
222# define do_scscope ex_ni
223# define do_cstag ex_ni
224#endif
225#ifndef FEAT_SYN_HL
226# define ex_syntax ex_ni
Bram Moolenaarb765d632005-06-07 21:00:02 +0000227# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000228# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000229# define ex_spelldump ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000230# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000231#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000232#ifndef FEAT_MZSCHEME
233# define ex_mzscheme ex_script_ni
234# define ex_mzfile ex_ni
235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236#ifndef FEAT_PERL
237# define ex_perl ex_script_ni
238# define ex_perldo ex_ni
239#endif
240#ifndef FEAT_PYTHON
241# define ex_python ex_script_ni
242# define ex_pyfile ex_ni
243#endif
244#ifndef FEAT_TCL
245# define ex_tcl ex_script_ni
246# define ex_tcldo ex_ni
247# define ex_tclfile ex_ni
248#endif
249#ifndef FEAT_RUBY
250# define ex_ruby ex_script_ni
251# define ex_rubydo ex_ni
252# define ex_rubyfile ex_ni
253#endif
254#ifndef FEAT_SNIFF
255# define ex_sniff ex_ni
256#endif
257#ifndef FEAT_KEYMAP
258# define ex_loadkeymap ex_ni
259#endif
260static void ex_swapname __ARGS((exarg_T *eap));
261static void ex_syncbind __ARGS((exarg_T *eap));
262static void ex_read __ARGS((exarg_T *eap));
263static void ex_cd __ARGS((exarg_T *eap));
264static void ex_pwd __ARGS((exarg_T *eap));
265static void ex_equal __ARGS((exarg_T *eap));
266static void ex_sleep __ARGS((exarg_T *eap));
267static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
268static void ex_winsize __ARGS((exarg_T *eap));
269#ifdef FEAT_WINDOWS
270static void ex_wincmd __ARGS((exarg_T *eap));
271#else
272# define ex_wincmd ex_ni
273#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000274#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275static void ex_winpos __ARGS((exarg_T *eap));
276#else
277# define ex_winpos ex_ni
278#endif
279static void ex_operators __ARGS((exarg_T *eap));
280static void ex_put __ARGS((exarg_T *eap));
281static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000282static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283static void ex_submagic __ARGS((exarg_T *eap));
284static void ex_join __ARGS((exarg_T *eap));
285static void ex_at __ARGS((exarg_T *eap));
286static void ex_bang __ARGS((exarg_T *eap));
287static void ex_undo __ARGS((exarg_T *eap));
288static void ex_redo __ARGS((exarg_T *eap));
289static void ex_redir __ARGS((exarg_T *eap));
290static void ex_redraw __ARGS((exarg_T *eap));
291static void ex_redrawstatus __ARGS((exarg_T *eap));
292static void close_redir __ARGS((void));
293static void ex_mkrc __ARGS((exarg_T *eap));
294static void ex_mark __ARGS((exarg_T *eap));
295#ifdef FEAT_USR_CMDS
296static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000297static 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 +0000298#endif
299#ifdef FEAT_EX_EXTRA
300static void ex_normal __ARGS((exarg_T *eap));
301static void ex_startinsert __ARGS((exarg_T *eap));
302static void ex_stopinsert __ARGS((exarg_T *eap));
303#else
304# define ex_normal ex_ni
305# define ex_align ex_ni
306# define ex_retab ex_ni
307# define ex_startinsert ex_ni
308# define ex_stopinsert ex_ni
309# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000310# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
312#ifdef FEAT_FIND_ID
313static void ex_checkpath __ARGS((exarg_T *eap));
314static void ex_findpat __ARGS((exarg_T *eap));
315#else
316# define ex_findpat ex_ni
317# define ex_checkpath ex_ni
318#endif
319#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
320static void ex_psearch __ARGS((exarg_T *eap));
321#else
322# define ex_psearch ex_ni
323#endif
324static void ex_tag __ARGS((exarg_T *eap));
325static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
326#ifndef FEAT_EVAL
327# define ex_scriptnames ex_ni
328# define ex_finish ex_ni
329# define ex_echo ex_ni
330# define ex_echohl ex_ni
331# define ex_execute ex_ni
332# define ex_call ex_ni
333# define ex_if ex_ni
334# define ex_endif ex_ni
335# define ex_else ex_ni
336# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000337# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338# define ex_continue ex_ni
339# define ex_break ex_ni
340# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000341# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342# define ex_throw ex_ni
343# define ex_try ex_ni
344# define ex_catch ex_ni
345# define ex_finally ex_ni
346# define ex_endtry ex_ni
347# define ex_endfunction ex_ni
348# define ex_let ex_ni
349# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000350# define ex_lockvar ex_ni
351# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352# define ex_function ex_ni
353# define ex_delfunction ex_ni
354# define ex_return ex_ni
355#endif
356static char_u *arg_all __ARGS((void));
357#ifdef FEAT_SESSION
358static int makeopens __ARGS((FILE *fd, char_u *dirnow));
359static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp));
360static void ex_loadview __ARGS((exarg_T *eap));
361static char_u *get_view_file __ARGS((int c));
362#else
363# define ex_loadview ex_ni
364#endif
365#ifndef FEAT_EVAL
366# define ex_compiler ex_ni
367#endif
368#ifdef FEAT_VIMINFO
369static void ex_viminfo __ARGS((exarg_T *eap));
370#else
371# define ex_viminfo ex_ni
372#endif
373static void ex_behave __ARGS((exarg_T *eap));
374#ifdef FEAT_AUTOCMD
375static void ex_filetype __ARGS((exarg_T *eap));
376static void ex_setfiletype __ARGS((exarg_T *eap));
377#else
378# define ex_filetype ex_ni
379# define ex_setfiletype ex_ni
380#endif
381#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000382# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383# define ex_diffpatch ex_ni
384# define ex_diffgetput ex_ni
385# define ex_diffsplit ex_ni
386# define ex_diffthis ex_ni
387# define ex_diffupdate ex_ni
388#endif
389static void ex_digraphs __ARGS((exarg_T *eap));
390static void ex_set __ARGS((exarg_T *eap));
391#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
392# define ex_options ex_ni
393#endif
394#ifdef FEAT_SEARCH_EXTRA
395static void ex_nohlsearch __ARGS((exarg_T *eap));
396static void ex_match __ARGS((exarg_T *eap));
397#else
398# define ex_nohlsearch ex_ni
399# define ex_match ex_ni
400#endif
401#ifdef FEAT_CRYPT
402static void ex_X __ARGS((exarg_T *eap));
403#else
404# define ex_X ex_ni
405#endif
406#ifdef FEAT_FOLDING
407static void ex_fold __ARGS((exarg_T *eap));
408static void ex_foldopen __ARGS((exarg_T *eap));
409static void ex_folddo __ARGS((exarg_T *eap));
410#else
411# define ex_fold ex_ni
412# define ex_foldopen ex_ni
413# define ex_folddo ex_ni
414#endif
415#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
416 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
417# define ex_language ex_ni
418#endif
419#ifndef FEAT_SIGNS
420# define ex_sign ex_ni
421#endif
422#ifndef FEAT_SUN_WORKSHOP
423# define ex_wsverb ex_ni
424#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000425#ifndef FEAT_NETBEANS_INTG
426# define ex_nbkey ex_ni
427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428
429#ifndef FEAT_EVAL
430# define ex_debug ex_ni
431# define ex_breakadd ex_ni
432# define ex_debuggreedy ex_ni
433# define ex_breakdel ex_ni
434# define ex_breaklist ex_ni
435#endif
436
437#ifndef FEAT_CMDHIST
438# define ex_history ex_ni
439#endif
440#ifndef FEAT_JUMPLIST
441# define ex_jumps ex_ni
442# define ex_changes ex_ni
443#endif
444
Bram Moolenaar05159a02005-02-26 23:04:13 +0000445#ifndef FEAT_PROFILE
446# define ex_profile ex_ni
447#endif
448
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449/*
450 * Declare cmdnames[].
451 */
452#define DO_DECLARE_EXCMD
453#include "ex_cmds.h"
454
455/*
456 * Table used to quickly search for a command, based on its first character.
457 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000458static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459{
460 CMD_append,
461 CMD_buffer,
462 CMD_change,
463 CMD_delete,
464 CMD_edit,
465 CMD_file,
466 CMD_global,
467 CMD_help,
468 CMD_insert,
469 CMD_join,
470 CMD_k,
471 CMD_list,
472 CMD_move,
473 CMD_next,
474 CMD_open,
475 CMD_print,
476 CMD_quit,
477 CMD_read,
478 CMD_substitute,
479 CMD_t,
480 CMD_undo,
481 CMD_vglobal,
482 CMD_write,
483 CMD_xit,
484 CMD_yank,
485 CMD_z,
486 CMD_bang
487};
488
489static char_u dollar_command[2] = {'$', 0};
490
491
492#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000493/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494typedef struct
495{
496 char_u *line; /* command line */
497 linenr_T lnum; /* sourcing_lnum of the line */
498} wcmd_T;
499
500/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000501 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000503 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000505struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506{
507 garray_T *lines_gap; /* growarray with line info */
508 int current_line; /* last read line from growarray */
509 int repeating; /* TRUE when looping a second time */
510 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
511 char_u *(*getline) __ARGS((int, void *, int));
512 void *cookie;
513};
514
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000515static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
516static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000518
519/* Struct to save a few things while debugging. Used in do_cmdline() only. */
520struct dbg_stuff
521{
522 int trylevel;
523 int force_abort;
524 except_T *caught_stack;
525 char_u *vv_exception;
526 char_u *vv_throwpoint;
527 int did_emsg;
528 int got_int;
529 int did_throw;
530 int need_rethrow;
531 int check_cstack;
532 except_T *current_exception;
533};
534
535static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
536static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
537
538 static void
539save_dbg_stuff(dsp)
540 struct dbg_stuff *dsp;
541{
542 dsp->trylevel = trylevel; trylevel = 0;
543 dsp->force_abort = force_abort; force_abort = FALSE;
544 dsp->caught_stack = caught_stack; caught_stack = NULL;
545 dsp->vv_exception = v_exception(NULL);
546 dsp->vv_throwpoint = v_throwpoint(NULL);
547
548 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
549 dsp->did_emsg = did_emsg; did_emsg = FALSE;
550 dsp->got_int = got_int; got_int = FALSE;
551 dsp->did_throw = did_throw; did_throw = FALSE;
552 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
553 dsp->check_cstack = check_cstack; check_cstack = FALSE;
554 dsp->current_exception = current_exception; current_exception = NULL;
555}
556
557 static void
558restore_dbg_stuff(dsp)
559 struct dbg_stuff *dsp;
560{
561 suppress_errthrow = FALSE;
562 trylevel = dsp->trylevel;
563 force_abort = dsp->force_abort;
564 caught_stack = dsp->caught_stack;
565 (void)v_exception(dsp->vv_exception);
566 (void)v_throwpoint(dsp->vv_throwpoint);
567 did_emsg = dsp->did_emsg;
568 got_int = dsp->got_int;
569 did_throw = dsp->did_throw;
570 need_rethrow = dsp->need_rethrow;
571 check_cstack = dsp->check_cstack;
572 current_exception = dsp->current_exception;
573}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574#endif
575
576
577/*
578 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
579 * command is given.
580 */
581 void
582do_exmode(improved)
583 int improved; /* TRUE for "improved Ex" mode */
584{
585 int save_msg_scroll;
586 int prev_msg_row;
587 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000588 int changedtick;
589
590 if (improved)
591 exmode_active = EXMODE_VIM;
592 else
593 exmode_active = EXMODE_NORMAL;
594 State = NORMAL;
595
596 /* When using ":global /pat/ visual" and then "Q" we return to continue
597 * the :global command. */
598 if (global_busy)
599 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600
601 save_msg_scroll = msg_scroll;
602 ++RedrawingDisabled; /* don't redisplay the window */
603 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604#ifdef FEAT_GUI
605 /* Ignore scrollbar and mouse events in Ex mode */
606 ++hold_gui_events;
607#endif
608#ifdef FEAT_SNIFF
609 want_sniff_request = 0; /* No K_SNIFF wanted */
610#endif
611
612 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
613 while (exmode_active)
614 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000615#ifdef FEAT_EX_EXTRA
616 /* Check for a ":normal" command and no more characters left. */
617 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
618 {
619 exmode_active = FALSE;
620 break;
621 }
622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 msg_scroll = TRUE;
624 need_wait_return = FALSE;
625 ex_pressedreturn = FALSE;
626 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000627 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 prev_msg_row = msg_row;
629 prev_line = curwin->w_cursor.lnum;
630#ifdef FEAT_SNIFF
631 ProcessSniffRequests();
632#endif
633 if (improved)
634 {
635 cmdline_row = msg_row;
636 do_cmdline(NULL, getexline, NULL, 0);
637 }
638 else
639 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
640 lines_left = Rows - 1;
641
Bram Moolenaardf177f62005-02-22 08:39:57 +0000642 if ((prev_line != curwin->w_cursor.lnum
643 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000645 if (curbuf->b_ml.ml_flags & ML_EMPTY)
646 EMSG(_(e_emptybuf));
647 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000649 if (ex_pressedreturn)
650 {
651 /* go up one line, to overwrite the ":<CR>" line, so the
652 * output doensn't contain empty lines. */
653 msg_row = prev_msg_row;
654 if (prev_msg_row == Rows - 1)
655 msg_row--;
656 }
657 msg_col = 0;
658 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
659 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000662 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
663 {
664 if (curbuf->b_ml.ml_flags & ML_EMPTY)
665 EMSG(_(e_emptybuf));
666 else
667 EMSG(_("E501: At end-of-file"));
668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 }
670
671#ifdef FEAT_GUI
672 --hold_gui_events;
673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 --RedrawingDisabled;
675 --no_wait_return;
676 update_screen(CLEAR);
677 need_wait_return = FALSE;
678 msg_scroll = save_msg_scroll;
679}
680
681/*
682 * Execute a simple command line. Used for translated commands like "*".
683 */
684 int
685do_cmdline_cmd(cmd)
686 char_u *cmd;
687{
688 return do_cmdline(cmd, NULL, NULL,
689 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
690}
691
692/*
693 * do_cmdline(): execute one Ex command line
694 *
695 * 1. Execute "cmdline" when it is not NULL.
696 * If "cmdline" is NULL, or more lines are needed, getline() is used.
697 * 2. Split up in parts separated with '|'.
698 *
699 * This function can be called recursively!
700 *
701 * flags:
702 * DOCMD_VERBOSE - The command will be included in the error message.
703 * DOCMD_NOWAIT - Don't call wait_return() and friends.
704 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
705 * DOCMD_KEYTYPED - Don't reset KeyTyped.
706 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
707 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
708 *
709 * return FAIL if cmdline could not be executed, OK otherwise
710 */
711 int
712do_cmdline(cmdline, getline, cookie, flags)
713 char_u *cmdline;
714 char_u *(*getline) __ARGS((int, void *, int));
715 void *cookie; /* argument for getline() */
716 int flags;
717{
718 char_u *next_cmdline; /* next cmd to execute */
719 char_u *cmdline_copy = NULL; /* copy of cmd line */
720 int used_getline = FALSE; /* used "getline" to obtain command */
721 static int recursive = 0; /* recursive depth */
722 int msg_didout_before_start = 0;
723 int count = 0; /* line number count */
724 int did_inc = FALSE; /* incremented RedrawingDisabled */
725 int retval = OK;
726#ifdef FEAT_EVAL
727 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000728 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 int current_line = 0; /* active line in lines_ga */
730 char_u *fname = NULL; /* function or script name */
731 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
732 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000733 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 int initial_trylevel;
735 struct msglist **saved_msg_list = NULL;
736 struct msglist *private_msg_list;
737
738 /* "getline" and "cookie" passed to do_one_cmd() */
739 char_u *(*cmd_getline) __ARGS((int, void *, int));
740 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000741 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000743 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744#else
745# define cmd_getline getline
746# define cmd_cookie cookie
747#endif
748 static int call_depth = 0; /* recursiveness */
749
750#ifdef FEAT_EVAL
751 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
752 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000753 * This ensures that the do_errthrow() call in do_one_cmd() does not
754 * combine the messages stored by an earlier invocation of do_one_cmd()
755 * with the command name of the later one. This would happen when
756 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 saved_msg_list = msg_list;
758 msg_list = &private_msg_list;
759 private_msg_list = NULL;
760#endif
761
762 /* It's possible to create an endless loop with ":execute", catch that
763 * here. The value of 200 allows nested function calls, ":source", etc. */
764 if (call_depth == 200)
765 {
766 EMSG(_("E169: Command too recursive"));
767#ifdef FEAT_EVAL
768 /* When converting to an exception, we do not include the command name
769 * since this is not an error of the specific command. */
770 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
771 msg_list = saved_msg_list;
772#endif
773 return FAIL;
774 }
775 ++call_depth;
776
777#ifdef FEAT_EVAL
778 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000779 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 cstack.cs_trylevel = 0;
781 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000782 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
784
785 real_cookie = getline_cookie(getline, cookie);
786
787 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000788 getline_is_func = getline_equal(getline, cookie, get_func_line);
789 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 ++ex_nesting_level;
791
792 /* Get the function or script name and the address where the next breakpoint
793 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000794 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 {
796 fname = func_name(real_cookie);
797 breakpoint = func_breakpoint(real_cookie);
798 dbg_tick = func_dbg_tick(real_cookie);
799 }
800 else if (getline_equal(getline, cookie, getsourceline))
801 {
802 fname = sourcing_name;
803 breakpoint = source_breakpoint(real_cookie);
804 dbg_tick = source_dbg_tick(real_cookie);
805 }
806
807 /*
808 * Initialize "force_abort" and "suppress_errthrow" at the top level.
809 */
810 if (!recursive)
811 {
812 force_abort = FALSE;
813 suppress_errthrow = FALSE;
814 }
815
816 /*
817 * If requested, store and reset the global values controlling the
818 * exception handling (used when debugging).
819 */
820 else if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000821 save_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
823 initial_trylevel = trylevel;
824
825 /*
826 * "did_throw" will be set to TRUE when an exception is being thrown.
827 */
828 did_throw = FALSE;
829#endif
830 /*
831 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000832 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 * If force_abort is set, we cancel everything.
834 */
835 did_emsg = FALSE;
836
837 /*
838 * KeyTyped is only set when calling vgetc(). Reset it here when not
839 * calling vgetc() (sourced command lines).
840 */
841 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
842 KeyTyped = FALSE;
843
844 /*
845 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000846 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 * - for multiple commands on one line, separated with '|'
848 * - when repeating until there are no more lines (for ":source")
849 */
850 next_cmdline = cmdline;
851 do
852 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000853#ifdef FEAT_EVAL
854 getline_is_func = getline_equal(getline, cookie, get_func_line);
855#endif
856
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000857 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 if (next_cmdline == NULL
859#ifdef FEAT_EVAL
860 && !force_abort
861 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863#endif
864 )
865 did_emsg = FALSE;
866
867 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000868 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 * 2. If no line given: Get an allocated line with getline().
870 * 3. If a line is given: Make a copy, so we can mess with it.
871 */
872
873#ifdef FEAT_EVAL
874 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000875 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 {
877 /* Each '|' separated command is stored separately in lines_ga, to
878 * be able to jump to it. Don't use next_cmdline now. */
879 vim_free(cmdline_copy);
880 cmdline_copy = NULL;
881
882 /* Check if a function has returned or, unless it has an unclosed
883 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000884 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000886# ifdef FEAT_PROFILE
887 if (do_profiling)
888 func_line_end(real_cookie);
889# endif
890 if (func_has_ended(real_cookie))
891 {
892 retval = FAIL;
893 break;
894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896#ifdef FEAT_PROFILE
897 else if (do_profiling
898 && getline_equal(getline, cookie, getsourceline))
899 script_line_end();
900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
902 /* Check if a sourced file hit a ":finish" command. */
903 if (source_finished(getline, cookie))
904 {
905 retval = FAIL;
906 break;
907 }
908
909 /* If breakpoints have been added/deleted need to check for it. */
910 if (breakpoint != NULL && dbg_tick != NULL
911 && *dbg_tick != debug_tick)
912 {
913 *breakpoint = dbg_find_breakpoint(
914 getline_equal(getline, cookie, getsourceline),
915 fname, sourcing_lnum);
916 *dbg_tick = debug_tick;
917 }
918
919 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
920 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
921
922 /* Did we encounter a breakpoint? */
923 if (breakpoint != NULL && *breakpoint != 0
924 && *breakpoint <= sourcing_lnum)
925 {
926 dbg_breakpoint(fname, sourcing_lnum);
927 /* Find next breakpoint. */
928 *breakpoint = dbg_find_breakpoint(
929 getline_equal(getline, cookie, getsourceline),
930 fname, sourcing_lnum);
931 *dbg_tick = debug_tick;
932 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000933# ifdef FEAT_PROFILE
934 if (do_profiling)
935 {
936 if (getline_is_func)
937 func_line_start(real_cookie);
938 else if (getline_equal(getline, cookie, getsourceline))
939 script_line_start();
940 }
941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
943
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000944 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000946 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 * again. Pass a different "getline" function to do_one_cmd()
948 * below, so that it stores lines in or reads them from
949 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000950 * while/for loop. */
951 cmd_getline = get_loop_line;
952 cmd_cookie = (void *)&cmd_loop_cookie;
953 cmd_loop_cookie.lines_gap = &lines_ga;
954 cmd_loop_cookie.current_line = current_line;
955 cmd_loop_cookie.getline = getline;
956 cmd_loop_cookie.cookie = cookie;
957 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959 else
960 {
961 cmd_getline = getline;
962 cmd_cookie = cookie;
963 }
964#endif
965
966 /* 2. If no line given, get an allocated line with getline(). */
967 if (next_cmdline == NULL)
968 {
969 /*
970 * Need to set msg_didout for the first line after an ":if",
971 * otherwise the ":if" will be overwritten.
972 */
973 if (count == 1 && getline_equal(getline, cookie, getexline))
974 msg_didout = TRUE;
975 if (getline == NULL || (next_cmdline = getline(':', cookie,
976#ifdef FEAT_EVAL
977 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
978#else
979 0
980#endif
981 )) == NULL)
982 {
983 /* Don't call wait_return for aborted command line. The NULL
984 * returned for the end of a sourced file or executed function
985 * doesn't do this. */
986 if (KeyTyped && !(flags & DOCMD_REPEAT))
987 need_wait_return = FALSE;
988 retval = FAIL;
989 break;
990 }
991 used_getline = TRUE;
992
993 /*
994 * Keep the first typed line. Clear it when more lines are typed.
995 */
996 if (flags & DOCMD_KEEPLINE)
997 {
998 vim_free(repeat_cmdline);
999 if (count == 0)
1000 repeat_cmdline = vim_strsave(next_cmdline);
1001 else
1002 repeat_cmdline = NULL;
1003 }
1004 }
1005
1006 /* 3. Make a copy of the command so we can mess with it. */
1007 else if (cmdline_copy == NULL)
1008 {
1009 next_cmdline = vim_strsave(next_cmdline);
1010 if (next_cmdline == NULL)
1011 {
1012 EMSG(_(e_outofmem));
1013 retval = FAIL;
1014 break;
1015 }
1016 }
1017 cmdline_copy = next_cmdline;
1018
1019#ifdef FEAT_EVAL
1020 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001021 * Save the current line when inside a ":while" or ":for", and when
1022 * the command looks like a ":while" or ":for", because we may need it
1023 * later. When there is a '|' and another command, it is stored
1024 * separately, because we need to be able to jump back to it from an
1025 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001027 if (current_line == lines_ga.ga_len
1028 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001030 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 {
1032 retval = FAIL;
1033 break;
1034 }
1035 }
1036 did_endif = FALSE;
1037#endif
1038
1039 if (count++ == 0)
1040 {
1041 /*
1042 * All output from the commands is put below each other, without
1043 * waiting for a return. Don't do this when executing commands
1044 * from a script or when being called recursive (e.g. for ":e
1045 * +command file").
1046 */
1047 if (!(flags & DOCMD_NOWAIT) && !recursive)
1048 {
1049 msg_didout_before_start = msg_didout;
1050 msg_didany = FALSE; /* no output yet */
1051 msg_start();
1052 msg_scroll = TRUE; /* put messages below each other */
1053 ++no_wait_return; /* dont wait for return until finished */
1054 ++RedrawingDisabled;
1055 did_inc = TRUE;
1056 }
1057 }
1058
1059 if (p_verbose >= 15 && sourcing_name != NULL)
1060 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001062 verbose_enter_scroll();
1063
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 smsg((char_u *)_("line %ld: %s"),
1065 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001066 if (msg_silent == 0)
1067 msg_puts((char_u *)"\n"); /* don't overwrite this */
1068
1069 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 --no_wait_return;
1071 }
1072
1073 /*
1074 * 2. Execute one '|' separated command.
1075 * do_one_cmd() will return NULL if there is no trailing '|'.
1076 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1077 */
1078 ++recursive;
1079 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1080#ifdef FEAT_EVAL
1081 &cstack,
1082#endif
1083 cmd_getline, cmd_cookie);
1084 --recursive;
1085
1086#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001087 if (cmd_cookie == (void *)&cmd_loop_cookie)
1088 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091#endif
1092
1093 if (next_cmdline == NULL)
1094 {
1095 vim_free(cmdline_copy);
1096 cmdline_copy = NULL;
1097#ifdef FEAT_CMDHIST
1098 /*
1099 * If the command was typed, remember it for the ':' register.
1100 * Do this AFTER executing the command to make :@: work.
1101 */
1102 if (getline_equal(getline, cookie, getexline)
1103 && new_last_cmdline != NULL)
1104 {
1105 vim_free(last_cmdline);
1106 last_cmdline = new_last_cmdline;
1107 new_last_cmdline = NULL;
1108 }
1109#endif
1110 }
1111 else
1112 {
1113 /* need to copy the command after the '|' to cmdline_copy, for the
1114 * next do_one_cmd() */
1115 mch_memmove(cmdline_copy, next_cmdline, STRLEN(next_cmdline) + 1);
1116 next_cmdline = cmdline_copy;
1117 }
1118
1119
1120#ifdef FEAT_EVAL
1121 /* reset did_emsg for a function that is not aborted by an error */
1122 if (did_emsg && !force_abort
1123 && getline_equal(getline, cookie, get_func_line)
1124 && !func_has_abort(real_cookie))
1125 did_emsg = FALSE;
1126
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001127 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
1129 ++current_line;
1130
1131 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 * An ":endwhile", ":endfor" and ":continue" is handled here.
1133 * If we were executing commands, jump back to the ":while" or
1134 * ":for".
1135 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001141 /* Jump back to the matching ":while" or ":for". Be careful
1142 * not to use a cs_line[] from an entry that isn't a ":while"
1143 * or ":for": It would make "current_line" invalid and can
1144 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 if (!did_emsg && !got_int && !did_throw
1146 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001147 && (cstack.cs_flags[cstack.cs_idx]
1148 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 && cstack.cs_line[cstack.cs_idx] >= 0
1150 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1151 {
1152 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001153 /* remember we jumped there */
1154 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 line_breakcheck(); /* check if CTRL-C typed */
1156
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 /* Check for the next breakpoint at or after the ":while"
1158 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (breakpoint != NULL)
1160 {
1161 *breakpoint = dbg_find_breakpoint(
1162 getline_equal(getline, cookie, getsourceline),
1163 fname,
1164 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1165 *dbg_tick = debug_tick;
1166 }
1167 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001172 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1173 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 }
1175 }
1176
1177 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1184 }
1185 }
1186
1187 /*
1188 * When not inside any ":while" loop, clear remembered lines.
1189 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
1192 if (lines_ga.ga_len > 0)
1193 {
1194 sourcing_lnum =
1195 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1196 free_cmdlines(&lines_ga);
1197 }
1198 current_line = 0;
1199 }
1200
1201 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001202 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1203 * being restored at the ":endtry". Reset them here and set the
1204 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1205 * This includes the case where a missing ":endif", ":endwhile" or
1206 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001208 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001210 cstack.cs_lflags &= ~CSL_HAD_FINA;
1211 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1212 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 did_throw ? (void *)current_exception : NULL);
1214 did_emsg = got_int = did_throw = FALSE;
1215 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1216 }
1217
1218 /* Update global "trylevel" for recursive calls to do_cmdline() from
1219 * within this loop. */
1220 trylevel = initial_trylevel + cstack.cs_trylevel;
1221
1222 /*
1223 * If the outermost try conditional (accross function calls and sourced
1224 * files) is aborted because of an error, an interrupt, or an uncaught
1225 * exception, cancel everything. If it is left normally, reset
1226 * force_abort to get the non-EH compatible abortion behavior for
1227 * the rest of the script.
1228 */
1229 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1230 force_abort = FALSE;
1231
1232 /* Convert an interrupt to an exception if appropriate. */
1233 (void)do_intthrow(&cstack);
1234#endif /* FEAT_EVAL */
1235
1236 }
1237 /*
1238 * Continue executing command lines when:
1239 * - no CTRL-C typed, no aborting error, no exception thrown or try
1240 * conditionals need to be checked for executing finally clauses or
1241 * catching an interrupt exception
1242 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001243 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 * looping for ":source" command or function call.
1245 */
1246 while (!((got_int
1247#ifdef FEAT_EVAL
1248 || (did_emsg && force_abort) || did_throw
1249#endif
1250 )
1251#ifdef FEAT_EVAL
1252 && cstack.cs_trylevel == 0
1253#endif
1254 )
1255 && !(did_emsg && used_getline
1256 && (getline_equal(getline, cookie, getexmodeline)
1257 || getline_equal(getline, cookie, getexline)))
1258 && (next_cmdline != NULL
1259#ifdef FEAT_EVAL
1260 || cstack.cs_idx >= 0
1261#endif
1262 || (flags & DOCMD_REPEAT)));
1263
1264 vim_free(cmdline_copy);
1265#ifdef FEAT_EVAL
1266 free_cmdlines(&lines_ga);
1267 ga_clear(&lines_ga);
1268
1269 if (cstack.cs_idx >= 0)
1270 {
1271 /*
1272 * If a sourced file or executed function ran to its end, report the
1273 * unclosed conditional.
1274 */
1275 if (!got_int && !did_throw
1276 && ((getline_equal(getline, cookie, getsourceline)
1277 && !source_finished(getline, cookie))
1278 || (getline_equal(getline, cookie, get_func_line)
1279 && !func_has_ended(real_cookie))))
1280 {
1281 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1282 EMSG(_(e_endtry));
1283 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1284 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001285 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1286 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 else
1288 EMSG(_(e_endif));
1289 }
1290
1291 /*
1292 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1293 * ":endtry" in a sourced file or executed function. If the try
1294 * conditional is in its finally clause, ignore anything pending.
1295 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001296 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 */
1298 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001299 {
1300 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1301
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001302 if (idx >= 0)
1303 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001304 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1305 &cstack.cs_looplevel);
1306 }
1307 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 trylevel = initial_trylevel;
1309 }
1310
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001311 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1312 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 * exception, do this now after rewinding the cstack. */
1314 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1315 ? (char_u *)"endfunction" : (char_u *)NULL);
1316
1317 if (trylevel == 0)
1318 {
1319 /*
1320 * When an exception is being thrown out of the outermost try
1321 * conditional, discard the uncaught exception, disable the conversion
1322 * of interrupts or errors to exceptions, and ensure that no more
1323 * commands are executed.
1324 */
1325 if (did_throw)
1326 {
1327 void *p = NULL;
1328 char_u *saved_sourcing_name;
1329 int saved_sourcing_lnum;
1330 struct msglist *messages = NULL, *next;
1331
1332 /*
1333 * If the uncaught exception is a user exception, report it as an
1334 * error. If it is an error exception, display the saved error
1335 * message now. For an interrupt exception, do nothing; the
1336 * interrupt message is given elsewhere.
1337 */
1338 switch (current_exception->type)
1339 {
1340 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001341 vim_snprintf((char *)IObuff, IOSIZE,
1342 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 current_exception->value);
1344 p = vim_strsave(IObuff);
1345 break;
1346 case ET_ERROR:
1347 messages = current_exception->messages;
1348 current_exception->messages = NULL;
1349 break;
1350 case ET_INTERRUPT:
1351 break;
1352 default:
1353 p = vim_strsave((char_u *)_(e_internal));
1354 }
1355
1356 saved_sourcing_name = sourcing_name;
1357 saved_sourcing_lnum = sourcing_lnum;
1358 sourcing_name = current_exception->throw_name;
1359 sourcing_lnum = current_exception->throw_lnum;
1360 current_exception->throw_name = NULL;
1361
1362 discard_current_exception(); /* uses IObuff if 'verbose' */
1363 suppress_errthrow = TRUE;
1364 force_abort = TRUE;
1365
1366 if (messages != NULL)
1367 {
1368 do
1369 {
1370 next = messages->next;
1371 emsg(messages->msg);
1372 vim_free(messages->msg);
1373 vim_free(messages);
1374 messages = next;
1375 }
1376 while (messages != NULL);
1377 }
1378 else if (p != NULL)
1379 {
1380 emsg(p);
1381 vim_free(p);
1382 }
1383 vim_free(sourcing_name);
1384 sourcing_name = saved_sourcing_name;
1385 sourcing_lnum = saved_sourcing_lnum;
1386 }
1387
1388 /*
1389 * On an interrupt or an aborting error not converted to an exception,
1390 * disable the conversion of errors to exceptions. (Interrupts are not
1391 * converted any more, here.) This enables also the interrupt message
1392 * when force_abort is set and did_emsg unset in case of an interrupt
1393 * from a finally clause after an error.
1394 */
1395 else if (got_int || (did_emsg && force_abort))
1396 suppress_errthrow = TRUE;
1397 }
1398
1399 /*
1400 * The current cstack will be freed when do_cmdline() returns. An uncaught
1401 * exception will have to be rethrown in the previous cstack. If a function
1402 * has just returned or a script file was just finished and the previous
1403 * cstack belongs to the same function or, respectively, script file, it
1404 * will have to be checked for finally clauses to be executed due to the
1405 * ":return" or ":finish". This is done in do_one_cmd().
1406 */
1407 if (did_throw)
1408 need_rethrow = TRUE;
1409 if ((getline_equal(getline, cookie, getsourceline)
1410 && ex_nesting_level > source_level(real_cookie))
1411 || (getline_equal(getline, cookie, get_func_line)
1412 && ex_nesting_level > func_level(real_cookie) + 1))
1413 {
1414 if (!did_throw)
1415 check_cstack = TRUE;
1416 }
1417 else
1418 {
1419 /* When leaving a function, reduce nesting level. */
1420 if (getline_equal(getline, cookie, get_func_line))
1421 --ex_nesting_level;
1422 /*
1423 * Go to debug mode when returning from a function in which we are
1424 * single-stepping.
1425 */
1426 if ((getline_equal(getline, cookie, getsourceline)
1427 || getline_equal(getline, cookie, get_func_line))
1428 && ex_nesting_level + 1 <= debug_break_level)
1429 do_debug(getline_equal(getline, cookie, getsourceline)
1430 ? (char_u *)_("End of sourced file")
1431 : (char_u *)_("End of function"));
1432 }
1433
1434 /*
1435 * Restore the exception environment (done after returning from the
1436 * debugger).
1437 */
1438 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001439 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
1441 msg_list = saved_msg_list;
1442#endif /* FEAT_EVAL */
1443
1444 /*
1445 * If there was too much output to fit on the command line, ask the user to
1446 * hit return before redrawing the screen. With the ":global" command we do
1447 * this only once after the command is finished.
1448 */
1449 if (did_inc)
1450 {
1451 --RedrawingDisabled;
1452 --no_wait_return;
1453 msg_scroll = FALSE;
1454
1455 /*
1456 * When just finished an ":if"-":else" which was typed, no need to
1457 * wait for hit-return. Also for an error situation.
1458 */
1459 if (retval == FAIL
1460#ifdef FEAT_EVAL
1461 || (did_endif && KeyTyped && !did_emsg)
1462#endif
1463 )
1464 {
1465 need_wait_return = FALSE;
1466 msg_didany = FALSE; /* don't wait when restarting edit */
1467 }
1468 else if (need_wait_return)
1469 {
1470 /*
1471 * The msg_start() above clears msg_didout. The wait_return we do
1472 * here should not overwrite the command that may be shown before
1473 * doing that.
1474 */
1475 msg_didout |= msg_didout_before_start;
1476 wait_return(FALSE);
1477 }
1478 }
1479
1480#ifndef FEAT_EVAL
1481 /*
1482 * Reset if_level, in case a sourced script file contains more ":if" than
1483 * ":endif" (could be ":if x | foo | endif").
1484 */
1485 if_level = 0;
1486#endif
1487
1488 --call_depth;
1489 return retval;
1490}
1491
1492#ifdef FEAT_EVAL
1493/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001494 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 */
1496 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001497get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 int c;
1499 void *cookie;
1500 int indent;
1501{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001502 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 wcmd_T *wp;
1504 char_u *line;
1505
1506 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1507 {
1508 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001509 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001511 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 if (cp->getline == NULL)
1513 line = getcmdline(c, 0L, indent);
1514 else
1515 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001516 if (store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 ++cp->current_line;
1518
1519 return line;
1520 }
1521
1522 KeyTyped = FALSE;
1523 ++cp->current_line;
1524 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1525 sourcing_lnum = wp->lnum;
1526 return vim_strsave(wp->line);
1527}
1528
1529/*
1530 * Store a line in "gap" so that a ":while" loop can execute it again.
1531 */
1532 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001533store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 garray_T *gap;
1535 char_u *line;
1536{
1537 if (ga_grow(gap, 1) == FAIL)
1538 return FAIL;
1539 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1540 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1541 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 return OK;
1543}
1544
1545/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001546 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 */
1548 static void
1549free_cmdlines(gap)
1550 garray_T *gap;
1551{
1552 while (gap->ga_len > 0)
1553 {
1554 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1555 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 }
1557}
1558#endif
1559
1560/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001561 * If "getline" is get_loop_line(), return TRUE if the getline it uses equals
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 * "func". * Otherwise return TRUE when "getline" equals "func".
1563 */
1564/*ARGSUSED*/
1565 int
1566getline_equal(getline, cookie, func)
1567 char_u *(*getline) __ARGS((int, void *, int));
1568 void *cookie; /* argument for getline() */
1569 char_u *(*func) __ARGS((int, void *, int));
1570{
1571#ifdef FEAT_EVAL
1572 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001573 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001575 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 * function that's orignally used to obtain the lines. This may be nested
1577 * several levels. */
1578 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001579 cp = (struct loop_cookie *)cookie;
1580 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 {
1582 gp = cp->getline;
1583 cp = cp->cookie;
1584 }
1585 return gp == func;
1586#else
1587 return getline == func;
1588#endif
1589}
1590
1591#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1592/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001593 * If "getline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 * getline function. Otherwise return "cookie".
1595 */
1596/*ARGSUSED*/
1597 void *
1598getline_cookie(getline, cookie)
1599 char_u *(*getline) __ARGS((int, void *, int));
1600 void *cookie; /* argument for getline() */
1601{
1602# ifdef FEAT_EVAL
1603 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001604 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001606 /* When "getline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 * cookie that's orignally used to obtain the lines. This may be nested
1608 * several levels. */
1609 gp = getline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001610 cp = (struct loop_cookie *)cookie;
1611 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
1613 gp = cp->getline;
1614 cp = cp->cookie;
1615 }
1616 return cp;
1617# else
1618 return cookie;
1619# endif
1620}
1621#endif
1622
1623/*
1624 * Execute one Ex command.
1625 *
1626 * If 'sourcing' is TRUE, the command will be included in the error message.
1627 *
1628 * 1. skip comment lines and leading space
1629 * 2. handle command modifiers
1630 * 3. parse range
1631 * 4. parse command
1632 * 5. parse arguments
1633 * 6. switch on command name
1634 *
1635 * Note: "getline" can be NULL.
1636 *
1637 * This function may be called recursively!
1638 */
1639#if (_MSC_VER == 1200)
1640/*
Bram Moolenaared203462004-06-16 11:19:22 +00001641 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001643 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644#endif
1645 static char_u *
1646do_one_cmd(cmdlinep, sourcing,
1647#ifdef FEAT_EVAL
1648 cstack,
1649#endif
1650 getline, cookie)
1651 char_u **cmdlinep;
1652 int sourcing;
1653#ifdef FEAT_EVAL
1654 struct condstack *cstack;
1655#endif
1656 char_u *(*getline) __ARGS((int, void *, int));
1657 void *cookie; /* argument for getline() */
1658{
1659 char_u *p;
1660 linenr_T lnum;
1661 long n;
1662 char_u *errormsg = NULL; /* error message */
1663 exarg_T ea; /* Ex command arguments */
1664 long verbose_save = -1;
1665 int save_msg_scroll = 0;
1666 int did_silent = 0;
1667 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001668#ifdef HAVE_SANDBOX
1669 int did_sandbox = FALSE;
1670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 cmdmod_T save_cmdmod;
1672 int ni; /* set when Not Implemented */
1673
1674 vim_memset(&ea, 0, sizeof(ea));
1675 ea.line1 = 1;
1676 ea.line2 = 1;
1677#ifdef FEAT_EVAL
1678 ++ex_nesting_level;
1679#endif
1680
1681 /* when not editing the last file :q has to be typed twice */
1682 if (quitmore
1683#ifdef FEAT_EVAL
1684 /* avoid that a function call in 'statusline' does this */
1685 && !getline_equal(getline, cookie, get_func_line)
1686#endif
1687 )
1688 --quitmore;
1689
1690 /*
1691 * Reset browse, confirm, etc.. They are restored when returning, for
1692 * recursive calls.
1693 */
1694 save_cmdmod = cmdmod;
1695 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1696
1697 /*
1698 * Repeat until no more command modifiers are found.
1699 */
1700 ea.cmd = *cmdlinep;
1701 for (;;)
1702 {
1703/*
1704 * 1. skip comment lines and leading white space and colons
1705 */
1706 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1707 ++ea.cmd;
1708
1709 /* in ex mode, an empty line works like :+ */
1710 if (*ea.cmd == NUL && exmode_active
1711 && (getline_equal(getline, cookie, getexmodeline)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001712 || getline_equal(getline, cookie, getexline))
1713 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 {
1715 ea.cmd = (char_u *)"+";
1716 ex_pressedreturn = TRUE;
1717 }
1718
1719 /* ignore comment and empty lines */
1720 if (*ea.cmd == '"' || *ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001721 {
1722 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
1726/*
1727 * 2. handle command modifiers.
1728 */
1729 p = ea.cmd;
1730 if (VIM_ISDIGIT(*ea.cmd))
1731 p = skipwhite(skipdigits(ea.cmd));
1732 switch (*p)
1733 {
1734 /* When adding an entry, also modify cmd_exists(). */
1735 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1736 break;
1737#ifdef FEAT_WINDOWS
1738 cmdmod.split |= WSP_ABOVE;
1739#endif
1740 continue;
1741
1742 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1743 {
1744#ifdef FEAT_WINDOWS
1745 cmdmod.split |= WSP_BELOW;
1746#endif
1747 continue;
1748 }
1749 if (checkforcmd(&ea.cmd, "browse", 3))
1750 {
1751#ifdef FEAT_BROWSE
1752 cmdmod.browse = TRUE;
1753#endif
1754 continue;
1755 }
1756 if (!checkforcmd(&ea.cmd, "botright", 2))
1757 break;
1758#ifdef FEAT_WINDOWS
1759 cmdmod.split |= WSP_BOT;
1760#endif
1761 continue;
1762
1763 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1764 break;
1765#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1766 cmdmod.confirm = TRUE;
1767#endif
1768 continue;
1769
1770 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1771 {
1772 cmdmod.keepmarks = TRUE;
1773 continue;
1774 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001775 if (checkforcmd(&ea.cmd, "keepalt", 5))
1776 {
1777 cmdmod.keepalt = TRUE;
1778 continue;
1779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1781 break;
1782 cmdmod.keepjumps = TRUE;
1783 continue;
1784
1785 /* ":hide" and ":hide | cmd" are not modifiers */
1786 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1787 || *p == NUL || ends_excmd(*p))
1788 break;
1789 ea.cmd = p;
1790 cmdmod.hide = TRUE;
1791 continue;
1792
1793 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1794 {
1795 cmdmod.lockmarks = TRUE;
1796 continue;
1797 }
1798
1799 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1800 break;
1801#ifdef FEAT_WINDOWS
1802 cmdmod.split |= WSP_ABOVE;
1803#endif
1804 continue;
1805
1806 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1807 break;
1808#ifdef FEAT_WINDOWS
1809 cmdmod.split |= WSP_BELOW;
1810#endif
1811 continue;
1812
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001813 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1814 {
1815#ifdef HAVE_SANDBOX
1816 if (!did_sandbox)
1817 ++sandbox;
1818 did_sandbox = TRUE;
1819#endif
1820 continue;
1821 }
1822 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 break;
1824 ++did_silent;
1825 ++msg_silent;
1826 save_msg_scroll = msg_scroll;
1827 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1828 {
1829 /* ":silent!", but not "silent !cmd" */
1830 ea.cmd = skipwhite(ea.cmd + 1);
1831 ++emsg_silent;
1832 ++did_esilent;
1833 }
1834 continue;
1835
1836 case 't': if (!checkforcmd(&ea.cmd, "topleft", 2))
1837 break;
1838#ifdef FEAT_WINDOWS
1839 cmdmod.split |= WSP_TOP;
1840#endif
1841 continue;
1842
1843 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1844 {
1845#ifdef FEAT_VERTSPLIT
1846 cmdmod.split |= WSP_VERT;
1847#endif
1848 continue;
1849 }
1850 if (!checkforcmd(&p, "verbose", 4))
1851 break;
1852 if (verbose_save < 0)
1853 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001854 if (vim_isdigit(*ea.cmd))
1855 p_verbose = atoi((char *)ea.cmd);
1856 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 p_verbose = 1;
1858 ea.cmd = p;
1859 continue;
1860 }
1861 break;
1862 }
1863
1864#ifdef FEAT_EVAL
1865 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1866 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1867#else
1868 ea.skip = (if_level > 0);
1869#endif
1870
1871#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001872# ifdef FEAT_PROFILE
1873 /* Count this line for profiling if ea.skip is FALSE. */
1874 if (do_profiling && !ea.skip)
1875 {
1876 if (getline_equal(getline, cookie, get_func_line))
1877 func_line_exec(getline_cookie(getline, cookie));
1878 else if (getline_equal(getline, cookie, getsourceline))
1879 script_line_exec();
1880 }
1881#endif
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 /* May go to debug mode. If this happens and the ">quit" debug command is
1884 * used, throw an interrupt exception and skip the next command. */
1885 dbg_check_breakpoint(&ea);
1886 if (!ea.skip && got_int)
1887 {
1888 ea.skip = TRUE;
1889 (void)do_intthrow(cstack);
1890 }
1891#endif
1892
1893/*
1894 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1895 *
1896 * where 'addr' is:
1897 *
1898 * % (entire file)
1899 * $ [+-NUM]
1900 * 'x [+-NUM] (where x denotes a currently defined mark)
1901 * . [+-NUM]
1902 * [+-NUM]..
1903 * NUM
1904 *
1905 * The ea.cmd pointer is updated to point to the first character following the
1906 * range spec. If an initial address is found, but no second, the upper bound
1907 * is equal to the lower.
1908 */
1909
1910 /* repeat for all ',' or ';' separated addresses */
1911 for (;;)
1912 {
1913 ea.line1 = ea.line2;
1914 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1915 ea.cmd = skipwhite(ea.cmd);
1916 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1917 if (ea.cmd == NULL) /* error detected */
1918 goto doend;
1919 if (lnum == MAXLNUM)
1920 {
1921 if (*ea.cmd == '%') /* '%' - all lines */
1922 {
1923 ++ea.cmd;
1924 ea.line1 = 1;
1925 ea.line2 = curbuf->b_ml.ml_line_count;
1926 ++ea.addr_count;
1927 }
1928 /* '*' - visual area */
1929 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1930 {
1931 pos_T *fp;
1932
1933 ++ea.cmd;
1934 if (!ea.skip)
1935 {
1936 fp = getmark('<', FALSE);
1937 if (check_mark(fp) == FAIL)
1938 goto doend;
1939 ea.line1 = fp->lnum;
1940 fp = getmark('>', FALSE);
1941 if (check_mark(fp) == FAIL)
1942 goto doend;
1943 ea.line2 = fp->lnum;
1944 ++ea.addr_count;
1945 }
1946 }
1947 }
1948 else
1949 ea.line2 = lnum;
1950 ea.addr_count++;
1951
1952 if (*ea.cmd == ';')
1953 {
1954 if (!ea.skip)
1955 curwin->w_cursor.lnum = ea.line2;
1956 }
1957 else if (*ea.cmd != ',')
1958 break;
1959 ++ea.cmd;
1960 }
1961
1962 /* One address given: set start and end lines */
1963 if (ea.addr_count == 1)
1964 {
1965 ea.line1 = ea.line2;
1966 /* ... but only implicit: really no address given */
1967 if (lnum == MAXLNUM)
1968 ea.addr_count = 0;
1969 }
1970
1971 /* Don't leave the cursor on an illegal line (caused by ';') */
1972 check_cursor_lnum();
1973
1974/*
1975 * 4. parse command
1976 */
1977
1978 /*
1979 * Skip ':' and any white space
1980 */
1981 ea.cmd = skipwhite(ea.cmd);
1982 while (*ea.cmd == ':')
1983 ea.cmd = skipwhite(ea.cmd + 1);
1984
1985 /*
1986 * If we got a line, but no command, then go to the line.
1987 * If we find a '|' or '\n' we set ea.nextcmd.
1988 */
1989 if (*ea.cmd == NUL || *ea.cmd == '"' ||
1990 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
1991 {
1992 /*
1993 * strange vi behaviour:
1994 * ":3" jumps to line 3
1995 * ":3|..." prints line 3
1996 * ":|" prints current line
1997 */
1998 if (ea.skip) /* skip this if inside :if */
1999 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002000 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 {
2002 ea.cmdidx = CMD_print;
2003 ea.argt = RANGE+COUNT+TRLBAR;
2004 if ((errormsg = invalid_range(&ea)) == NULL)
2005 {
2006 correct_range(&ea);
2007 ex_print(&ea);
2008 }
2009 }
2010 else if (ea.addr_count != 0)
2011 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002012 if (ea.line2 < 0 || ea.line2 > curbuf->b_ml.ml_line_count)
2013 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 else
2015 {
2016 if (ea.line2 == 0)
2017 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 else
2019 curwin->w_cursor.lnum = ea.line2;
2020 beginline(BL_SOL | BL_FIX);
2021 }
2022 }
2023 goto doend;
2024 }
2025
2026 /* Find the command and let "p" point to after it. */
2027 p = find_command(&ea, NULL);
2028
2029#ifdef FEAT_USR_CMDS
2030 if (p == NULL)
2031 {
2032 if (!ea.skip)
2033 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2034 goto doend;
2035 }
2036 /* Check for wrong commands. */
2037 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2038 {
2039 errormsg = uc_fun_cmd();
2040 goto doend;
2041 }
2042#endif
2043 if (ea.cmdidx == CMD_SIZE)
2044 {
2045 if (!ea.skip)
2046 {
2047 STRCPY(IObuff, _("E492: Not an editor command"));
2048 if (!sourcing)
2049 {
2050 STRCAT(IObuff, ": ");
2051 STRNCAT(IObuff, *cmdlinep, 40);
2052 }
2053 errormsg = IObuff;
2054 }
2055 goto doend;
2056 }
2057
2058 ni = (
2059#ifdef FEAT_USR_CMDS
2060 !USER_CMDIDX(ea.cmdidx) &&
2061#endif
2062 cmdnames[ea.cmdidx].cmd_func == ex_ni);
2063
2064#ifndef FEAT_EVAL
2065 /*
2066 * When the expression evaluation is disabled, recognize the ":if" and
2067 * ":endif" commands and ignore everything in between it.
2068 */
2069 if (ea.cmdidx == CMD_if)
2070 ++if_level;
2071 if (if_level)
2072 {
2073 if (ea.cmdidx == CMD_endif)
2074 --if_level;
2075 goto doend;
2076 }
2077
2078#endif
2079
2080 if (*p == '!' && ea.cmdidx != CMD_substitute) /* forced commands */
2081 {
2082 ++p;
2083 ea.forceit = TRUE;
2084 }
2085 else
2086 ea.forceit = FALSE;
2087
2088/*
2089 * 5. parse arguments
2090 */
2091#ifdef FEAT_USR_CMDS
2092 if (!USER_CMDIDX(ea.cmdidx))
2093#endif
2094 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
2095
2096 if (!ea.skip)
2097 {
2098#ifdef HAVE_SANDBOX
2099 if (sandbox != 0 && !(ea.argt & SBOXOK))
2100 {
2101 /* Command not allowed in sandbox. */
2102 errormsg = (char_u *)_(e_sandbox);
2103 goto doend;
2104 }
2105#endif
2106 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2107 {
2108 /* Command not allowed in non-'modifiable' buffer */
2109 errormsg = (char_u *)_(e_modifiable);
2110 goto doend;
2111 }
2112#ifdef FEAT_CMDWIN
2113 if (cmdwin_type != 0 && !(ea.argt & CMDWIN)
2114# ifdef FEAT_USR_CMDS
2115 && !USER_CMDIDX(ea.cmdidx)
2116# endif
2117 )
2118 {
2119 /* Command not allowed in cmdline window. */
2120 errormsg = (char_u *)_(e_cmdwin);
2121 goto doend;
2122 }
2123#endif
2124
2125 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2126 {
2127 /* no range allowed */
2128 errormsg = (char_u *)_(e_norange);
2129 goto doend;
2130 }
2131 }
2132
2133 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2134 {
2135 errormsg = (char_u *)_(e_nobang);
2136 goto doend;
2137 }
2138
2139 /*
2140 * Don't complain about the range if it is not used
2141 * (could happen if line_count is accidentally set to 0).
2142 */
2143 if (!ea.skip && !ni)
2144 {
2145 /*
2146 * If the range is backwards, ask for confirmation and, if given, swap
2147 * ea.line1 & ea.line2 so it's forwards again.
2148 * When global command is busy, don't ask, will fail below.
2149 */
2150 if (!global_busy && ea.line1 > ea.line2)
2151 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002152 if (sourcing || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
2154 errormsg = (char_u *)_("E493: Backwards range given");
2155 goto doend;
2156 }
2157 else
2158 {
2159 int msg_silent_save = msg_silent;
2160
2161 msg_silent = 0;
2162 if (ask_yesno((char_u *)
2163 _("Backwards range given, OK to swap"), FALSE) != 'y')
2164 goto doend;
2165 msg_silent = msg_silent_save;
2166 }
2167 lnum = ea.line1;
2168 ea.line1 = ea.line2;
2169 ea.line2 = lnum;
2170 }
2171 if ((errormsg = invalid_range(&ea)) != NULL)
2172 goto doend;
2173 }
2174
2175 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2176 ea.line2 = 1;
2177
2178 correct_range(&ea);
2179
2180#ifdef FEAT_FOLDING
2181 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2182 {
2183 /* Put the first line at the start of a closed fold, put the last line
2184 * at the end of a closed fold. */
2185 (void)hasFolding(ea.line1, &ea.line1, NULL);
2186 (void)hasFolding(ea.line2, NULL, &ea.line2);
2187 }
2188#endif
2189
2190#ifdef FEAT_QUICKFIX
2191 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002192 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 * option here, so things like % get expanded.
2194 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002195 p = replace_makeprg(&ea, p, cmdlinep);
2196 if (p == NULL)
2197 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#endif
2199
2200 /*
2201 * Skip to start of argument.
2202 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2203 */
2204 if (ea.cmdidx == CMD_bang)
2205 ea.arg = p;
2206 else
2207 ea.arg = skipwhite(p);
2208
2209 /*
2210 * Check for "++opt=val" argument.
2211 * Must be first, allow ":w ++enc=utf8 !cmd"
2212 */
2213 if (ea.argt & ARGOPT)
2214 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2215 if (getargopt(&ea) == FAIL && !ni)
2216 {
2217 errormsg = (char_u *)_(e_invarg);
2218 goto doend;
2219 }
2220
2221 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2222 {
2223 if (*ea.arg == '>') /* append */
2224 {
2225 if (*++ea.arg != '>') /* typed wrong */
2226 {
2227 errormsg = (char_u *)_("E494: Use w or w>>");
2228 goto doend;
2229 }
2230 ea.arg = skipwhite(ea.arg + 1);
2231 ea.append = TRUE;
2232 }
2233 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2234 {
2235 ++ea.arg;
2236 ea.usefilter = TRUE;
2237 }
2238 }
2239
2240 if (ea.cmdidx == CMD_read)
2241 {
2242 if (ea.forceit)
2243 {
2244 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2245 ea.forceit = FALSE;
2246 }
2247 else if (*ea.arg == '!') /* :r !filter */
2248 {
2249 ++ea.arg;
2250 ea.usefilter = TRUE;
2251 }
2252 }
2253
2254 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2255 {
2256 ea.amount = 1;
2257 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2258 {
2259 ++ea.arg;
2260 ++ea.amount;
2261 }
2262 ea.arg = skipwhite(ea.arg);
2263 }
2264
2265 /*
2266 * Check for "+command" argument, before checking for next command.
2267 * Don't do this for ":read !cmd" and ":write !cmd".
2268 */
2269 if ((ea.argt & EDITCMD) && !ea.usefilter)
2270 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2271
2272 /*
2273 * Check for '|' to separate commands and '"' to start comments.
2274 * Don't do this for ":read !cmd" and ":write !cmd".
2275 */
2276 if ((ea.argt & TRLBAR) && !ea.usefilter)
2277 separate_nextcmd(&ea);
2278
2279 /*
2280 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002281 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2282 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002284 else if (ea.cmdidx == CMD_bang
2285 || ea.cmdidx == CMD_global
2286 || ea.cmdidx == CMD_vglobal
2287 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
2289 for (p = ea.arg; *p; ++p)
2290 {
2291 /* Remove one backslash before a newline, so that it's possible to
2292 * pass a newline to the shell and also a newline that is preceded
2293 * with a backslash. This makes it impossible to end a shell
2294 * command in a backslash, but that doesn't appear useful.
2295 * Halving the number of backslashes is incompatible with previous
2296 * versions. */
2297 if (*p == '\\' && p[1] == '\n')
2298 mch_memmove(p, p + 1, STRLEN(p));
2299 else if (*p == '\n')
2300 {
2301 ea.nextcmd = p + 1;
2302 *p = NUL;
2303 break;
2304 }
2305 }
2306 }
2307
2308 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2309 {
2310 ea.line1 = 1;
2311 ea.line2 = curbuf->b_ml.ml_line_count;
2312 }
2313
2314 /* accept numbered register only when no count allowed (:put) */
2315 if ( (ea.argt & REGSTR)
2316 && *ea.arg != NUL
2317#ifdef FEAT_USR_CMDS
2318 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2319 && USER_CMDIDX(ea.cmdidx)))
2320 /* Do not allow register = for user commands */
2321 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2322#else
2323 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2324#endif
2325 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2326 {
2327 ea.regname = *ea.arg++;
2328#ifdef FEAT_EVAL
2329 /* for '=' register: accept the rest of the line as an expression */
2330 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2331 {
2332 set_expr_line(vim_strsave(ea.arg));
2333 ea.arg += STRLEN(ea.arg);
2334 }
2335#endif
2336 ea.arg = skipwhite(ea.arg);
2337 }
2338
2339 /*
2340 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2341 * count, it's a buffer name.
2342 */
2343 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2344 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2345 || vim_iswhite(*p)))
2346 {
2347 n = getdigits(&ea.arg);
2348 ea.arg = skipwhite(ea.arg);
2349 if (n <= 0 && !ni)
2350 {
2351 errormsg = (char_u *)_(e_zerocount);
2352 goto doend;
2353 }
2354 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2355 {
2356 ea.line2 = n;
2357 if (ea.addr_count == 0)
2358 ea.addr_count = 1;
2359 }
2360 else
2361 {
2362 ea.line1 = ea.line2;
2363 ea.line2 += n - 1;
2364 ++ea.addr_count;
2365 /*
2366 * Be vi compatible: no error message for out of range.
2367 */
2368 if (ea.line2 > curbuf->b_ml.ml_line_count)
2369 ea.line2 = curbuf->b_ml.ml_line_count;
2370 }
2371 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002372
2373 /*
2374 * Check for flags: 'l', 'p' and '#'.
2375 */
2376 if (ea.argt & EXFLAGS)
2377 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002379 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
2380 && vim_strchr((char_u *)"|\"", *ea.arg) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 {
2382 errormsg = (char_u *)_(e_trailing);
2383 goto doend;
2384 }
2385
2386 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2387 {
2388 errormsg = (char_u *)_(e_argreq);
2389 goto doend;
2390 }
2391
2392#ifdef FEAT_EVAL
2393 /*
2394 * Skip the command when it's not going to be executed.
2395 * The commands like :if, :endif, etc. always need to be executed.
2396 * Also make an exception for commands that handle a trailing command
2397 * themselves.
2398 */
2399 if (ea.skip)
2400 {
2401 switch (ea.cmdidx)
2402 {
2403 /* commands that need evaluation */
2404 case CMD_while:
2405 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002406 case CMD_for:
2407 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 case CMD_if:
2409 case CMD_elseif:
2410 case CMD_else:
2411 case CMD_endif:
2412 case CMD_try:
2413 case CMD_catch:
2414 case CMD_finally:
2415 case CMD_endtry:
2416 case CMD_function:
2417 break;
2418
2419 /* Commands that handle '|' themselves. Check: A command should
2420 * either have the TRLBAR flag, appear in this list or appear in
2421 * the list at ":help :bar". */
2422 case CMD_aboveleft:
2423 case CMD_and:
2424 case CMD_belowright:
2425 case CMD_botright:
2426 case CMD_browse:
2427 case CMD_call:
2428 case CMD_confirm:
2429 case CMD_delfunction:
2430 case CMD_djump:
2431 case CMD_dlist:
2432 case CMD_dsearch:
2433 case CMD_dsplit:
2434 case CMD_echo:
2435 case CMD_echoerr:
2436 case CMD_echomsg:
2437 case CMD_echon:
2438 case CMD_execute:
2439 case CMD_help:
2440 case CMD_hide:
2441 case CMD_ijump:
2442 case CMD_ilist:
2443 case CMD_isearch:
2444 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002445 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 case CMD_keepjumps:
2447 case CMD_keepmarks:
2448 case CMD_leftabove:
2449 case CMD_let:
2450 case CMD_lockmarks:
2451 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002452 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 case CMD_perl:
2454 case CMD_psearch:
2455 case CMD_python:
2456 case CMD_return:
2457 case CMD_rightbelow:
2458 case CMD_ruby:
2459 case CMD_silent:
2460 case CMD_smagic:
2461 case CMD_snomagic:
2462 case CMD_substitute:
2463 case CMD_syntax:
2464 case CMD_tcl:
2465 case CMD_throw:
2466 case CMD_tilde:
2467 case CMD_topleft:
2468 case CMD_unlet:
2469 case CMD_verbose:
2470 case CMD_vertical:
2471 break;
2472
2473 default: goto doend;
2474 }
2475 }
2476#endif
2477
2478 if (ea.argt & XFILE)
2479 {
2480 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2481 goto doend;
2482 }
2483
2484#ifdef FEAT_LISTCMDS
2485 /*
2486 * Accept buffer name. Cannot be used at the same time with a buffer
2487 * number. Don't do this for a user command.
2488 */
2489 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2490# ifdef FEAT_USR_CMDS
2491 && !USER_CMDIDX(ea.cmdidx)
2492# endif
2493 )
2494 {
2495 /*
2496 * :bdelete, :bwipeout and :bunload take several arguments, separated
2497 * by spaces: find next space (skipping over escaped characters).
2498 * The others take one argument: ignore trailing spaces.
2499 */
2500 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2501 || ea.cmdidx == CMD_bunload)
2502 p = skiptowhite_esc(ea.arg);
2503 else
2504 {
2505 p = ea.arg + STRLEN(ea.arg);
2506 while (p > ea.arg && vim_iswhite(p[-1]))
2507 --p;
2508 }
2509 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2510 if (ea.line2 < 0) /* failed */
2511 goto doend;
2512 ea.addr_count = 1;
2513 ea.arg = skipwhite(p);
2514 }
2515#endif
2516
2517/*
2518 * 6. switch on command name
2519 *
2520 * The "ea" structure holds the arguments that can be used.
2521 */
2522 ea.cmdlinep = cmdlinep;
2523 ea.getline = getline;
2524 ea.cookie = cookie;
2525#ifdef FEAT_EVAL
2526 ea.cstack = cstack;
2527#endif
2528
2529#ifdef FEAT_USR_CMDS
2530 if (USER_CMDIDX(ea.cmdidx))
2531 {
2532 /*
2533 * Execute a user-defined command.
2534 */
2535 do_ucmd(&ea);
2536 }
2537 else
2538#endif
2539 {
2540 /*
2541 * Call the function to execute the command.
2542 */
2543 ea.errmsg = NULL;
2544 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2545 if (ea.errmsg != NULL)
2546 errormsg = (char_u *)_(ea.errmsg);
2547 }
2548
2549#ifdef FEAT_EVAL
2550 /*
2551 * If the command just executed called do_cmdline(), any throw or ":return"
2552 * or ":finish" encountered there must also check the cstack of the still
2553 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2554 * exception, or reanimate a returned function or finished script file and
2555 * return or finish it again.
2556 */
2557 if (need_rethrow)
2558 do_throw(cstack);
2559 else if (check_cstack)
2560 {
2561 if (source_finished(getline, cookie))
2562 do_finish(&ea, TRUE);
2563 else if (getline_equal(getline, cookie, get_func_line)
2564 && current_func_returned())
2565 do_return(&ea, TRUE, FALSE, NULL);
2566 }
2567 need_rethrow = check_cstack = FALSE;
2568#endif
2569
2570doend:
2571 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2572 curwin->w_cursor.lnum = 1;
2573
2574 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2575 {
2576 if (sourcing)
2577 {
2578 if (errormsg != IObuff)
2579 {
2580 STRCPY(IObuff, errormsg);
2581 errormsg = IObuff;
2582 }
2583 STRCAT(errormsg, ": ");
2584 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff));
2585 }
2586 emsg(errormsg);
2587 }
2588#ifdef FEAT_EVAL
2589 do_errthrow(cstack,
2590 (ea.cmdidx != CMD_SIZE
2591# ifdef FEAT_USR_CMDS
2592 && !USER_CMDIDX(ea.cmdidx)
2593# endif
2594 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2595#endif
2596
2597 if (verbose_save >= 0)
2598 p_verbose = verbose_save;
2599
2600 cmdmod = save_cmdmod;
2601
2602 if (did_silent > 0)
2603 {
2604 /* messages could be enabled for a serious error, need to check if the
2605 * counters don't become negative */
2606 msg_silent -= did_silent;
2607 if (msg_silent < 0)
2608 msg_silent = 0;
2609 emsg_silent -= did_esilent;
2610 if (emsg_silent < 0)
2611 emsg_silent = 0;
2612 /* Restore msg_scroll, it's set by file I/O commands, even when no
2613 * message is actually displayed. */
2614 msg_scroll = save_msg_scroll;
2615 }
2616
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002617#ifdef HAVE_SANDBOX
2618 if (did_sandbox)
2619 --sandbox;
2620#endif
2621
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2623 ea.nextcmd = NULL;
2624
2625#ifdef FEAT_EVAL
2626 --ex_nesting_level;
2627#endif
2628
2629 return ea.nextcmd;
2630}
2631#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002632 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633#endif
2634
2635/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002636 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 * If there is a match advance "pp" to the argument and return TRUE.
2638 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002639 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002641 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 char *cmd; /* name of command */
2643 int len; /* required length */
2644{
2645 int i;
2646
2647 for (i = 0; cmd[i] != NUL; ++i)
2648 if (cmd[i] != (*pp)[i])
2649 break;
2650 if (i >= len && !isalpha((*pp)[i]))
2651 {
2652 *pp = skipwhite(*pp + i);
2653 return TRUE;
2654 }
2655 return FALSE;
2656}
2657
2658/*
2659 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002660 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002662 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 * Returns NULL for an ambiguous user command.
2664 */
2665/*ARGSUSED*/
2666 static char_u *
2667find_command(eap, full)
2668 exarg_T *eap;
2669 int *full;
2670{
2671 int len;
2672 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002673 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
2675 /*
2676 * Isolate the command and search for it in the command table.
2677 * Exeptions:
2678 * - the 'k' command can directly be followed by any character.
2679 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2680 * but :sre[wind] is another command, as are :scrip[tnames],
2681 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002682 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 */
2684 p = eap->cmd;
2685 if (*p == 'k')
2686 {
2687 eap->cmdidx = CMD_k;
2688 ++p;
2689 }
2690 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002691 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2692 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 || p[1] == 'g'
2694 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2695 || p[1] == 'I'
2696 || (p[1] == 'r' && p[2] != 'e')))
2697 {
2698 eap->cmdidx = CMD_substitute;
2699 ++p;
2700 }
2701 else
2702 {
2703 while (ASCII_ISALPHA(*p))
2704 ++p;
2705 /* check for non-alpha command */
2706 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2707 ++p;
2708 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002709 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2710 {
2711 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2712 * :delete with the 'l' flag. Same for 'p'. */
2713 for (i = 0; i < len; ++i)
2714 if (eap->cmd[i] != "delete"[i])
2715 break;
2716 if (i == len - 1)
2717 {
2718 --len;
2719 if (p[-1] == 'l')
2720 eap->flags |= EXFLAG_LIST;
2721 else
2722 eap->flags |= EXFLAG_PRINT;
2723 }
2724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
2726 if (ASCII_ISLOWER(*eap->cmd))
2727 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2728 else
2729 eap->cmdidx = cmdidxs[26];
2730
2731 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2732 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2733 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2734 (size_t)len) == 0)
2735 {
2736#ifdef FEAT_EVAL
2737 if (full != NULL
2738 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2739 *full = TRUE;
2740#endif
2741 break;
2742 }
2743
2744#ifdef FEAT_USR_CMDS
2745 /* Look for a user defined command as a last resort */
2746 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2747 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002748 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 while (ASCII_ISALNUM(*p))
2750 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002751 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 }
2753#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002754 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 eap->cmdidx = CMD_SIZE;
2756 }
2757
2758 return p;
2759}
2760
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002761#ifdef FEAT_USR_CMDS
2762/*
2763 * Search for a user command that matches "eap->cmd".
2764 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2765 * Return a pointer to just after the command.
2766 * Return NULL if there is no matching command.
2767 */
2768 static char_u *
2769find_ucmd(eap, p, full, xp, compl)
2770 exarg_T *eap;
2771 char_u *p; /* end of the command (possibly including count) */
2772 int *full; /* set to TRUE for a full match */
2773 expand_T *xp; /* used for completion, NULL otherwise */
2774 int *compl; /* completion flags or NULL */
2775{
2776 int len = (int)(p - eap->cmd);
2777 int j, k, matchlen = 0;
2778 ucmd_T *uc;
2779 int found = FALSE;
2780 int possible = FALSE;
2781 char_u *cp, *np; /* Point into typed cmd and test name */
2782 garray_T *gap;
2783 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2784 only full match global is accepted. */
2785
2786 /*
2787 * Look for buffer-local user commands first, then global ones.
2788 */
2789 gap = &curbuf->b_ucmds;
2790 for (;;)
2791 {
2792 for (j = 0; j < gap->ga_len; ++j)
2793 {
2794 uc = USER_CMD_GA(gap, j);
2795 cp = eap->cmd;
2796 np = uc->uc_name;
2797 k = 0;
2798 while (k < len && *np != NUL && *cp++ == *np++)
2799 k++;
2800 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2801 {
2802 /* If finding a second match, the command is ambiguous. But
2803 * not if a buffer-local command wasn't a full match and a
2804 * global command is a full match. */
2805 if (k == len && found && *np != NUL)
2806 {
2807 if (gap == &ucmds)
2808 {
2809 if (xp != NULL)
2810 xp->xp_context = EXPAND_UNSUCCESSFUL;
2811 return NULL;
2812 }
2813 amb_local = TRUE;
2814 }
2815
2816 if (!found || (k == len && *np == NUL))
2817 {
2818 /* If we matched up to a digit, then there could
2819 * be another command including the digit that we
2820 * should use instead.
2821 */
2822 if (k == len)
2823 found = TRUE;
2824 else
2825 possible = TRUE;
2826
2827 if (gap == &ucmds)
2828 eap->cmdidx = CMD_USER;
2829 else
2830 eap->cmdidx = CMD_USER_BUF;
2831 eap->argt = uc->uc_argt;
2832 eap->useridx = j;
2833
2834# ifdef FEAT_CMDL_COMPL
2835 if (compl != NULL)
2836 *compl = uc->uc_compl;
2837# ifdef FEAT_EVAL
2838 if (xp != NULL)
2839 {
2840 xp->xp_arg = uc->uc_compl_arg;
2841 xp->xp_scriptID = uc->uc_scriptID;
2842 }
2843# endif
2844# endif
2845 /* Do not search for further abbreviations
2846 * if this is an exact match. */
2847 matchlen = k;
2848 if (k == len && *np == NUL)
2849 {
2850 if (full != NULL)
2851 *full = TRUE;
2852 amb_local = FALSE;
2853 break;
2854 }
2855 }
2856 }
2857 }
2858
2859 /* Stop if we found a full match or searched all. */
2860 if (j < gap->ga_len || gap == &ucmds)
2861 break;
2862 gap = &ucmds;
2863 }
2864
2865 /* Only found ambiguous matches. */
2866 if (amb_local)
2867 {
2868 if (xp != NULL)
2869 xp->xp_context = EXPAND_UNSUCCESSFUL;
2870 return NULL;
2871 }
2872
2873 /* The match we found may be followed immediately by a number. Move "p"
2874 * back to point to it. */
2875 if (found || possible)
2876 return p + (matchlen - len);
2877 return p;
2878}
2879#endif
2880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881#if defined(FEAT_EVAL) || defined(PROTO)
2882/*
2883 * Return > 0 if an Ex command "name" exists.
2884 * Return 2 if there is an exact match.
2885 * Return 3 if there is an ambiguous match.
2886 */
2887 int
2888cmd_exists(name)
2889 char_u *name;
2890{
2891 exarg_T ea;
2892 int full = FALSE;
2893 int i;
2894 int j;
2895 static struct cmdmod
2896 {
2897 char *name;
2898 int minlen;
2899 } cmdmods[] = {
2900 {"aboveleft", 3},
2901 {"belowright", 3},
2902 {"botright", 2},
2903 {"browse", 3},
2904 {"confirm", 4},
2905 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002906 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {"keepjumps", 5},
2908 {"keepmarks", 3},
2909 {"leftabove", 5},
2910 {"lockmarks", 3},
2911 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002912 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 {"silent", 3},
2914 {"topleft", 2},
2915 {"verbose", 4},
2916 {"vertical", 4},
2917 };
2918
2919 /* Check command modifiers. */
2920 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2921 {
2922 for (j = 0; name[j] != NUL; ++j)
2923 if (name[j] != cmdmods[i].name[j])
2924 break;
2925 if (name[j] == NUL && j >= cmdmods[i].minlen)
2926 return (cmdmods[i].name[j] == NUL ? 2 : 1);
2927 }
2928
2929 /* Check built-in commands and user defined commands. */
2930 ea.cmd = name;
2931 ea.cmdidx = (cmdidx_T)0;
2932 if (find_command(&ea, &full) == NULL)
2933 return 3;
2934 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
2935}
2936#endif
2937
2938/*
2939 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2940 * we don't need/want deleted. Maybe this could be done better if we didn't
2941 * repeat all this stuff. The only problem is that they may not stay
2942 * perfectly compatible with each other, but then the command line syntax
2943 * probably won't change that much -- webb.
2944 */
2945 char_u *
2946set_one_cmd_context(xp, buff)
2947 expand_T *xp;
2948 char_u *buff; /* buffer for command string */
2949{
2950 char_u *p;
2951 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002952 int len = 0;
2953 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
2955 int compl = EXPAND_NOTHING;
2956#endif
2957#ifdef FEAT_CMDL_COMPL
2958 int delim;
2959#endif
2960 int forceit = FALSE;
2961 int usefilter = FALSE; /* filter instead of file name */
2962
2963 xp->xp_pattern = buff;
2964 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
2965 xp->xp_backslash = XP_BS_NONE;
2966#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
2967 xp->xp_arg = NULL;
2968#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002969 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
2971/*
2972 * 2. skip comment lines and leading space, colons or bars
2973 */
2974 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2975 ;
2976 xp->xp_pattern = cmd;
2977
2978 if (*cmd == NUL)
2979 return NULL;
2980 if (*cmd == '"') /* ignore comment lines */
2981 {
2982 xp->xp_context = EXPAND_NOTHING;
2983 return NULL;
2984 }
2985
2986/*
2987 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
2988 */
2989 cmd = skip_range(cmd, &xp->xp_context);
2990
2991/*
2992 * 4. parse command
2993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 xp->xp_pattern = cmd;
2995 if (*cmd == NUL)
2996 return NULL;
2997 if (*cmd == '"')
2998 {
2999 xp->xp_context = EXPAND_NOTHING;
3000 return NULL;
3001 }
3002
3003 if (*cmd == '|' || *cmd == '\n')
3004 return cmd + 1; /* There's another command */
3005
3006 /*
3007 * Isolate the command and search for it in the command table.
3008 * Exceptions:
3009 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003010 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3012 */
3013 if (*cmd == 'k' && cmd[1] != 'e')
3014 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003015 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 p = cmd + 1;
3017 }
3018 else
3019 {
3020 p = cmd;
3021 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3022 ++p;
3023 /* check for non-alpha command */
3024 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3025 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003026 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003028 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 {
3030 xp->xp_context = EXPAND_UNSUCCESSFUL;
3031 return NULL;
3032 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003033 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3034 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3035 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 break;
3037
3038#ifdef FEAT_USR_CMDS
3039 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3040 {
3041 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3042 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003043 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 }
3045#endif
3046 }
3047
3048 /*
3049 * If the cursor is touching the command, and it ends in an alpha-numeric
3050 * character, complete the command name.
3051 */
3052 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3053 return NULL;
3054
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003055 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 {
3057 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3058 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003059 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 p = cmd + 1;
3061 }
3062#ifdef FEAT_USR_CMDS
3063 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3064 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003065 ea.cmd = cmd;
3066 p = find_ucmd(&ea, p, NULL, xp,
3067# if defined(FEAT_CMDL_COMPL)
3068 &compl
3069# else
3070 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003072 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 }
3074#endif
3075 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003076 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 {
3078 /* Not still touching the command and it was an illegal one */
3079 xp->xp_context = EXPAND_UNSUCCESSFUL;
3080 return NULL;
3081 }
3082
3083 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3084
3085 if (*p == '!') /* forced commands */
3086 {
3087 forceit = TRUE;
3088 ++p;
3089 }
3090
3091/*
3092 * 5. parse arguments
3093 */
3094#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003095 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003097 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
3099 arg = skipwhite(p);
3100
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003101 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
3103 if (*arg == '>') /* append */
3104 {
3105 if (*++arg == '>')
3106 ++arg;
3107 arg = skipwhite(arg);
3108 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003109 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 {
3111 ++arg;
3112 usefilter = TRUE;
3113 }
3114 }
3115
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003116 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 {
3118 usefilter = forceit; /* :r! filter if forced */
3119 if (*arg == '!') /* :r !filter */
3120 {
3121 ++arg;
3122 usefilter = TRUE;
3123 }
3124 }
3125
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003126 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 {
3128 while (*arg == *cmd) /* allow any number of '>' or '<' */
3129 ++arg;
3130 arg = skipwhite(arg);
3131 }
3132
3133 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003134 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 {
3136 /* Check if we're in the +command */
3137 p = arg + 1;
3138 arg = skip_cmd_arg(arg, FALSE);
3139
3140 /* Still touching the command after '+'? */
3141 if (*arg == NUL)
3142 return p;
3143
3144 /* Skip space(s) after +command to get to the real argument */
3145 arg = skipwhite(arg);
3146 }
3147
3148 /*
3149 * Check for '|' to separate commands and '"' to start comments.
3150 * Don't do this for ":read !cmd" and ":write !cmd".
3151 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003152 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 {
3154 p = arg;
3155 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003156 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 p += 2;
3158 while (*p)
3159 {
3160 if (*p == Ctrl_V)
3161 {
3162 if (p[1] != NUL)
3163 ++p;
3164 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003165 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 || *p == '|' || *p == '\n')
3167 {
3168 if (*(p - 1) != '\\')
3169 {
3170 if (*p == '|' || *p == '\n')
3171 return p + 1;
3172 return NULL; /* It's a comment */
3173 }
3174 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003175 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 }
3177 }
3178
3179 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003180 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 vim_strchr((char_u *)"|\"", *arg) == NULL)
3182 return NULL;
3183
3184 /* Find start of last argument (argument just before cursor): */
3185 p = buff + STRLEN(buff);
3186 while (p != arg && *p != ' ' && *p != TAB)
3187 p--;
3188 if (*p == ' ' || *p == TAB)
3189 p++;
3190 xp->xp_pattern = p;
3191
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003192 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 {
3194 int in_quote = FALSE;
3195 char_u *bow = NULL; /* Beginning of word */
3196
3197 /*
3198 * Allow spaces within back-quotes to count as part of the argument
3199 * being expanded.
3200 */
3201 xp->xp_pattern = skipwhite(arg);
3202 for (p = xp->xp_pattern; *p; )
3203 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003204 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 ++p;
3206#ifdef SPACE_IN_FILENAME
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003207 else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208#else
3209 else if (vim_iswhite(*p))
3210#endif
3211 {
3212 p = skipwhite(p);
3213 if (in_quote)
3214 bow = p;
3215 else
3216 xp->xp_pattern = p;
3217 --p;
3218 }
3219 else if (*p == '`')
3220 {
3221 if (!in_quote)
3222 {
3223 xp->xp_pattern = p;
3224 bow = p + 1;
3225 }
3226 in_quote = !in_quote;
3227 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003228 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 }
3230
3231 /*
3232 * If we are still inside the quotes, and we passed a space, just
3233 * expand from there.
3234 */
3235 if (bow != NULL && in_quote)
3236 xp->xp_pattern = bow;
3237 xp->xp_context = EXPAND_FILES;
3238
3239 /* Check for environment variable */
3240 if (*xp->xp_pattern == '$'
3241#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3242 || *xp->xp_pattern == '%'
3243#endif
3244 )
3245 {
3246 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3247 if (!vim_isIDc(*p))
3248 break;
3249 if (*p == NUL)
3250 {
3251 xp->xp_context = EXPAND_ENV_VARS;
3252 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003253#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3254 /* Avoid that the assignment uses EXPAND_FILES again. */
3255 if (compl != EXPAND_USER_DEFINED)
3256 compl = EXPAND_ENV_VARS;
3257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 }
3259 }
3260 }
3261
3262/*
3263 * 6. switch on command name
3264 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003265 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 {
3267 case CMD_cd:
3268 case CMD_chdir:
3269 case CMD_lcd:
3270 case CMD_lchdir:
3271 if (xp->xp_context == EXPAND_FILES)
3272 xp->xp_context = EXPAND_DIRECTORIES;
3273 break;
3274 case CMD_help:
3275 xp->xp_context = EXPAND_HELP;
3276 xp->xp_pattern = arg;
3277 break;
3278
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003279 /* Command modifiers: return the argument.
3280 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003282 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 case CMD_belowright:
3284 case CMD_botright:
3285 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003286 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003288 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 case CMD_folddoclosed:
3290 case CMD_folddoopen:
3291 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003292 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 case CMD_keepjumps:
3294 case CMD_keepmarks:
3295 case CMD_leftabove:
3296 case CMD_lockmarks:
3297 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003298 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 case CMD_silent:
3300 case CMD_topleft:
3301 case CMD_verbose:
3302 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003303 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 return arg;
3305
3306#ifdef FEAT_SEARCH_EXTRA
3307 case CMD_match:
3308 if (*arg == NUL || !ends_excmd(*arg))
3309 {
3310 /* Dummy call to clear variables. */
3311 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3312 xp->xp_context = EXPAND_HIGHLIGHT;
3313 xp->xp_pattern = arg;
3314 arg = skipwhite(skiptowhite(arg));
3315 if (*arg != NUL)
3316 {
3317 xp->xp_context = EXPAND_NOTHING;
3318 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3319 }
3320 }
3321 return find_nextcmd(arg);
3322#endif
3323
3324#ifdef FEAT_CMDL_COMPL
3325/*
3326 * All completion for the +cmdline_compl feature goes here.
3327 */
3328
3329# ifdef FEAT_USR_CMDS
3330 case CMD_command:
3331 /* Check for attributes */
3332 while (*arg == '-')
3333 {
3334 arg++; /* Skip "-" */
3335 p = skiptowhite(arg);
3336 if (*p == NUL)
3337 {
3338 /* Cursor is still in the attribute */
3339 p = vim_strchr(arg, '=');
3340 if (p == NULL)
3341 {
3342 /* No "=", so complete attribute names */
3343 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3344 xp->xp_pattern = arg;
3345 return NULL;
3346 }
3347
3348 /* For the -complete and -nargs attributes, we complete
3349 * their arguments as well.
3350 */
3351 if (STRNICMP(arg, "complete", p - arg) == 0)
3352 {
3353 xp->xp_context = EXPAND_USER_COMPLETE;
3354 xp->xp_pattern = p + 1;
3355 return NULL;
3356 }
3357 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3358 {
3359 xp->xp_context = EXPAND_USER_NARGS;
3360 xp->xp_pattern = p + 1;
3361 return NULL;
3362 }
3363 return NULL;
3364 }
3365 arg = skipwhite(p);
3366 }
3367
3368 /* After the attributes comes the new command name */
3369 p = skiptowhite(arg);
3370 if (*p == NUL)
3371 {
3372 xp->xp_context = EXPAND_USER_COMMANDS;
3373 xp->xp_pattern = arg;
3374 break;
3375 }
3376
3377 /* And finally comes a normal command */
3378 return skipwhite(p);
3379
3380 case CMD_delcommand:
3381 xp->xp_context = EXPAND_USER_COMMANDS;
3382 xp->xp_pattern = arg;
3383 break;
3384# endif
3385
3386 case CMD_global:
3387 case CMD_vglobal:
3388 delim = *arg; /* get the delimiter */
3389 if (delim)
3390 ++arg; /* skip delimiter if there is one */
3391
3392 while (arg[0] != NUL && arg[0] != delim)
3393 {
3394 if (arg[0] == '\\' && arg[1] != NUL)
3395 ++arg;
3396 ++arg;
3397 }
3398 if (arg[0] != NUL)
3399 return arg + 1;
3400 break;
3401 case CMD_and:
3402 case CMD_substitute:
3403 delim = *arg;
3404 if (delim)
3405 {
3406 /* skip "from" part */
3407 ++arg;
3408 arg = skip_regexp(arg, delim, p_magic, NULL);
3409 }
3410 /* skip "to" part */
3411 while (arg[0] != NUL && arg[0] != delim)
3412 {
3413 if (arg[0] == '\\' && arg[1] != NUL)
3414 ++arg;
3415 ++arg;
3416 }
3417 if (arg[0] != NUL) /* skip delimiter */
3418 ++arg;
3419 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3420 ++arg;
3421 if (arg[0] != NUL)
3422 return arg;
3423 break;
3424 case CMD_isearch:
3425 case CMD_dsearch:
3426 case CMD_ilist:
3427 case CMD_dlist:
3428 case CMD_ijump:
3429 case CMD_psearch:
3430 case CMD_djump:
3431 case CMD_isplit:
3432 case CMD_dsplit:
3433 arg = skipwhite(skipdigits(arg)); /* skip count */
3434 if (*arg == '/') /* Match regexp, not just whole words */
3435 {
3436 for (++arg; *arg && *arg != '/'; arg++)
3437 if (*arg == '\\' && arg[1] != NUL)
3438 arg++;
3439 if (*arg)
3440 {
3441 arg = skipwhite(arg + 1);
3442
3443 /* Check for trailing illegal characters */
3444 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3445 xp->xp_context = EXPAND_NOTHING;
3446 else
3447 return arg;
3448 }
3449 }
3450 break;
3451#ifdef FEAT_AUTOCMD
3452 case CMD_autocmd:
3453 return set_context_in_autocmd(xp, arg, FALSE);
3454
3455 case CMD_doautocmd:
3456 return set_context_in_autocmd(xp, arg, TRUE);
3457#endif
3458 case CMD_set:
3459 set_context_in_set_cmd(xp, arg, 0);
3460 break;
3461 case CMD_setglobal:
3462 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3463 break;
3464 case CMD_setlocal:
3465 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3466 break;
3467 case CMD_tag:
3468 case CMD_stag:
3469 case CMD_ptag:
3470 case CMD_tselect:
3471 case CMD_stselect:
3472 case CMD_ptselect:
3473 case CMD_tjump:
3474 case CMD_stjump:
3475 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003476 if (*p_wop != NUL)
3477 xp->xp_context = EXPAND_TAGS_LISTFILES;
3478 else
3479 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 xp->xp_pattern = arg;
3481 break;
3482 case CMD_augroup:
3483 xp->xp_context = EXPAND_AUGROUP;
3484 xp->xp_pattern = arg;
3485 break;
3486#ifdef FEAT_SYN_HL
3487 case CMD_syntax:
3488 set_context_in_syntax_cmd(xp, arg);
3489 break;
3490#endif
3491#ifdef FEAT_EVAL
3492 case CMD_let:
3493 case CMD_if:
3494 case CMD_elseif:
3495 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003496 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 case CMD_echo:
3498 case CMD_echon:
3499 case CMD_execute:
3500 case CMD_echomsg:
3501 case CMD_echoerr:
3502 case CMD_call:
3503 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003504 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 break;
3506
3507 case CMD_unlet:
3508 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3509 arg = xp->xp_pattern + 1;
3510 xp->xp_context = EXPAND_USER_VARS;
3511 xp->xp_pattern = arg;
3512 break;
3513
3514 case CMD_function:
3515 case CMD_delfunction:
3516 xp->xp_context = EXPAND_USER_FUNC;
3517 xp->xp_pattern = arg;
3518 break;
3519
3520 case CMD_echohl:
3521 xp->xp_context = EXPAND_HIGHLIGHT;
3522 xp->xp_pattern = arg;
3523 break;
3524#endif
3525 case CMD_highlight:
3526 set_context_in_highlight_cmd(xp, arg);
3527 break;
3528#ifdef FEAT_LISTCMDS
3529 case CMD_bdelete:
3530 case CMD_bwipeout:
3531 case CMD_bunload:
3532 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3533 arg = xp->xp_pattern + 1;
3534 /*FALLTHROUGH*/
3535 case CMD_buffer:
3536 case CMD_sbuffer:
3537 case CMD_checktime:
3538 xp->xp_context = EXPAND_BUFFERS;
3539 xp->xp_pattern = arg;
3540 break;
3541#endif
3542#ifdef FEAT_USR_CMDS
3543 case CMD_USER:
3544 case CMD_USER_BUF:
3545 if (compl != EXPAND_NOTHING)
3546 {
3547 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003548 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
3550# ifdef FEAT_MENU
3551 if (compl == EXPAND_MENUS)
3552 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3553# endif
3554 if (compl == EXPAND_COMMANDS)
3555 return arg;
3556 if (compl == EXPAND_MAPPINGS)
3557 return set_context_in_map_cmd(xp, (char_u *)"map",
3558 arg, forceit, FALSE, FALSE, CMD_map);
3559 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3560 arg = xp->xp_pattern + 1;
3561 xp->xp_pattern = arg;
3562 }
3563 xp->xp_context = compl;
3564 }
3565 break;
3566#endif
3567 case CMD_map: case CMD_noremap:
3568 case CMD_nmap: case CMD_nnoremap:
3569 case CMD_vmap: case CMD_vnoremap:
3570 case CMD_omap: case CMD_onoremap:
3571 case CMD_imap: case CMD_inoremap:
3572 case CMD_cmap: case CMD_cnoremap:
3573 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003574 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 case CMD_unmap:
3576 case CMD_nunmap:
3577 case CMD_vunmap:
3578 case CMD_ounmap:
3579 case CMD_iunmap:
3580 case CMD_cunmap:
3581 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003582 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 case CMD_abbreviate: case CMD_noreabbrev:
3584 case CMD_cabbrev: case CMD_cnoreabbrev:
3585 case CMD_iabbrev: case CMD_inoreabbrev:
3586 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003587 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 case CMD_unabbreviate:
3589 case CMD_cunabbrev:
3590 case CMD_iunabbrev:
3591 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003592 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593#ifdef FEAT_MENU
3594 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3595 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3596 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3597 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3598 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3599 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3600 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3601 case CMD_tmenu: case CMD_tunmenu:
3602 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3603 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3604#endif
3605
3606 case CMD_colorscheme:
3607 xp->xp_context = EXPAND_COLORS;
3608 xp->xp_pattern = arg;
3609 break;
3610
3611 case CMD_compiler:
3612 xp->xp_context = EXPAND_COMPILER;
3613 xp->xp_pattern = arg;
3614 break;
3615
3616#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3617 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3618 case CMD_language:
3619 if (*skiptowhite(arg) == NUL)
3620 {
3621 xp->xp_context = EXPAND_LANGUAGE;
3622 xp->xp_pattern = arg;
3623 }
3624 else
3625 xp->xp_context = EXPAND_NOTHING;
3626 break;
3627#endif
3628
3629#endif /* FEAT_CMDL_COMPL */
3630
3631 default:
3632 break;
3633 }
3634 return NULL;
3635}
3636
3637/*
3638 * skip a range specifier of the form: addr [,addr] [;addr] ..
3639 *
3640 * Backslashed delimiters after / or ? will be skipped, and commands will
3641 * not be expanded between /'s and ?'s or after "'".
3642 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003643 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 * Returns the "cmd" pointer advanced to beyond the range.
3645 */
3646 char_u *
3647skip_range(cmd, ctx)
3648 char_u *cmd;
3649 int *ctx; /* pointer to xp_context or NULL */
3650{
3651 int delim;
3652
Bram Moolenaardf177f62005-02-22 08:39:57 +00003653 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
3655 if (*cmd == '\'')
3656 {
3657 if (*++cmd == NUL && ctx != NULL)
3658 *ctx = EXPAND_NOTHING;
3659 }
3660 else if (*cmd == '/' || *cmd == '?')
3661 {
3662 delim = *cmd++;
3663 while (*cmd != NUL && *cmd != delim)
3664 if (*cmd++ == '\\' && *cmd != NUL)
3665 ++cmd;
3666 if (*cmd == NUL && ctx != NULL)
3667 *ctx = EXPAND_NOTHING;
3668 }
3669 if (*cmd != NUL)
3670 ++cmd;
3671 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003672
3673 /* Skip ":" and white space. */
3674 while (*cmd == ':')
3675 cmd = skipwhite(cmd + 1);
3676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 return cmd;
3678}
3679
3680/*
3681 * get a single EX address
3682 *
3683 * Set ptr to the next character after the part that was interpreted.
3684 * Set ptr to NULL when an error is encountered.
3685 *
3686 * Return MAXLNUM when no Ex address was found.
3687 */
3688 static linenr_T
3689get_address(ptr, skip, to_other_file)
3690 char_u **ptr;
3691 int skip; /* only skip the address, don't use it */
3692 int to_other_file; /* flag: may jump to other file */
3693{
3694 int c;
3695 int i;
3696 long n;
3697 char_u *cmd;
3698 pos_T pos;
3699 pos_T *fp;
3700 linenr_T lnum;
3701
3702 cmd = skipwhite(*ptr);
3703 lnum = MAXLNUM;
3704 do
3705 {
3706 switch (*cmd)
3707 {
3708 case '.': /* '.' - Cursor position */
3709 ++cmd;
3710 lnum = curwin->w_cursor.lnum;
3711 break;
3712
3713 case '$': /* '$' - last line */
3714 ++cmd;
3715 lnum = curbuf->b_ml.ml_line_count;
3716 break;
3717
3718 case '\'': /* ''' - mark */
3719 if (*++cmd == NUL)
3720 {
3721 cmd = NULL;
3722 goto error;
3723 }
3724 if (skip)
3725 ++cmd;
3726 else
3727 {
3728 /* Only accept a mark in another file when it is
3729 * used by itself: ":'M". */
3730 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3731 ++cmd;
3732 if (fp == (pos_T *)-1)
3733 /* Jumped to another file. */
3734 lnum = curwin->w_cursor.lnum;
3735 else
3736 {
3737 if (check_mark(fp) == FAIL)
3738 {
3739 cmd = NULL;
3740 goto error;
3741 }
3742 lnum = fp->lnum;
3743 }
3744 }
3745 break;
3746
3747 case '/':
3748 case '?': /* '/' or '?' - search */
3749 c = *cmd++;
3750 if (skip) /* skip "/pat/" */
3751 {
3752 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3753 if (*cmd == c)
3754 ++cmd;
3755 }
3756 else
3757 {
3758 pos = curwin->w_cursor; /* save curwin->w_cursor */
3759 /*
3760 * When '/' or '?' follows another address, start
3761 * from there.
3762 */
3763 if (lnum != MAXLNUM)
3764 curwin->w_cursor.lnum = lnum;
3765 /*
3766 * Start a forward search at the end of the line.
3767 * Start a backward search at the start of the line.
3768 * This makes sure we never match in the current
3769 * line, and can match anywhere in the
3770 * next/previous line.
3771 */
3772 if (c == '/')
3773 curwin->w_cursor.col = MAXCOL;
3774 else
3775 curwin->w_cursor.col = 0;
3776 searchcmdlen = 0;
3777 if (!do_search(NULL, c, cmd, 1L,
3778 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3779 {
3780 curwin->w_cursor = pos;
3781 cmd = NULL;
3782 goto error;
3783 }
3784 lnum = curwin->w_cursor.lnum;
3785 curwin->w_cursor = pos;
3786 /* adjust command string pointer */
3787 cmd += searchcmdlen;
3788 }
3789 break;
3790
3791 case '\\': /* "\?", "\/" or "\&", repeat search */
3792 ++cmd;
3793 if (*cmd == '&')
3794 i = RE_SUBST;
3795 else if (*cmd == '?' || *cmd == '/')
3796 i = RE_SEARCH;
3797 else
3798 {
3799 EMSG(_(e_backslash));
3800 cmd = NULL;
3801 goto error;
3802 }
3803
3804 if (!skip)
3805 {
3806 /*
3807 * When search follows another address, start from
3808 * there.
3809 */
3810 if (lnum != MAXLNUM)
3811 pos.lnum = lnum;
3812 else
3813 pos.lnum = curwin->w_cursor.lnum;
3814
3815 /*
3816 * Start the search just like for the above
3817 * do_search().
3818 */
3819 if (*cmd != '?')
3820 pos.col = MAXCOL;
3821 else
3822 pos.col = 0;
3823 if (searchit(curwin, curbuf, &pos,
3824 *cmd == '?' ? BACKWARD : FORWARD,
3825 (char_u *)"", 1L,
3826 SEARCH_MSG + SEARCH_START, i) != FAIL)
3827 lnum = pos.lnum;
3828 else
3829 {
3830 cmd = NULL;
3831 goto error;
3832 }
3833 }
3834 ++cmd;
3835 break;
3836
3837 default:
3838 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3839 lnum = getdigits(&cmd);
3840 }
3841
3842 for (;;)
3843 {
3844 cmd = skipwhite(cmd);
3845 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3846 break;
3847
3848 if (lnum == MAXLNUM)
3849 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3850 if (VIM_ISDIGIT(*cmd))
3851 i = '+'; /* "number" is same as "+number" */
3852 else
3853 i = *cmd++;
3854 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3855 n = 1;
3856 else
3857 n = getdigits(&cmd);
3858 if (i == '-')
3859 lnum -= n;
3860 else
3861 lnum += n;
3862 }
3863 } while (*cmd == '/' || *cmd == '?');
3864
3865error:
3866 *ptr = cmd;
3867 return lnum;
3868}
3869
3870/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003871 * Get flags from an Ex command argument.
3872 */
3873 static void
3874get_flags(eap)
3875 exarg_T *eap;
3876{
3877 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3878 {
3879 if (*eap->arg == 'l')
3880 eap->flags |= EXFLAG_LIST;
3881 else if (*eap->arg == 'p')
3882 eap->flags |= EXFLAG_PRINT;
3883 else
3884 eap->flags |= EXFLAG_NR;
3885 eap->arg = skipwhite(eap->arg + 1);
3886 }
3887}
3888
3889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 * Function called for command which is Not Implemented. NI!
3891 */
3892 void
3893ex_ni(eap)
3894 exarg_T *eap;
3895{
3896 if (!eap->skip)
3897 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3898}
3899
3900#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003901 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902/*
3903 * Function called for script command which is Not Implemented. NI!
3904 * Skips over ":perl <<EOF" constructs.
3905 */
3906 static void
3907ex_script_ni(eap)
3908 exarg_T *eap;
3909{
3910 if (!eap->skip)
3911 ex_ni(eap);
3912 else
3913 vim_free(script_get(eap, eap->arg));
3914}
3915#endif
3916
3917/*
3918 * Check range in Ex command for validity.
3919 * Return NULL when valid, error message when invalid.
3920 */
3921 static char_u *
3922invalid_range(eap)
3923 exarg_T *eap;
3924{
3925 if ( eap->line1 < 0
3926 || eap->line2 < 0
3927 || eap->line1 > eap->line2
3928 || ((eap->argt & RANGE)
3929 && !(eap->argt & NOTADR)
3930 && eap->line2 > curbuf->b_ml.ml_line_count
3931#ifdef FEAT_DIFF
3932 + (eap->cmdidx == CMD_diffget)
3933#endif
3934 ))
3935 return (char_u *)_(e_invrange);
3936 return NULL;
3937}
3938
3939/*
3940 * Correct the range for zero line number, if required.
3941 */
3942 static void
3943correct_range(eap)
3944 exarg_T *eap;
3945{
3946 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
3947 {
3948 if (eap->line1 == 0)
3949 eap->line1 = 1;
3950 if (eap->line2 == 0)
3951 eap->line2 = 1;
3952 }
3953}
3954
Bram Moolenaar748bf032005-02-02 23:04:36 +00003955#ifdef FEAT_QUICKFIX
3956static char_u *skip_grep_pat __ARGS((exarg_T *eap));
3957
3958/*
3959 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
3960 * pattern. Otherwise return eap->arg.
3961 */
3962 static char_u *
3963skip_grep_pat(eap)
3964 exarg_T *eap;
3965{
3966 char_u *p = eap->arg;
3967
3968 if (*p != NUL && (eap->cmdidx == CMD_vimgrep
3969 || eap->cmdidx == CMD_vimgrepadd || grep_internal(eap->cmdidx)))
3970 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003971 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003972 if (p == NULL)
3973 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003974 }
3975 return p;
3976}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003977
3978/*
3979 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
3980 * in the command line, so that things like % get expanded.
3981 */
3982 static char_u *
3983replace_makeprg(eap, p, cmdlinep)
3984 exarg_T *eap;
3985 char_u *p;
3986 char_u **cmdlinep;
3987{
3988 char_u *new_cmdline;
3989 char_u *program;
3990 char_u *pos;
3991 char_u *ptr;
3992 int len;
3993 int i;
3994
3995 /*
3996 * Don't do it when ":vimgrep" is used for ":grep".
3997 */
3998 if ((eap->cmdidx == CMD_make
3999 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_grepadd)
4000 && !grep_internal(eap->cmdidx))
4001 {
4002 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_grepadd)
4003 {
4004 if (*curbuf->b_p_gp == NUL)
4005 program = p_gp;
4006 else
4007 program = curbuf->b_p_gp;
4008 }
4009 else
4010 {
4011 if (*curbuf->b_p_mp == NUL)
4012 program = p_mp;
4013 else
4014 program = curbuf->b_p_mp;
4015 }
4016
4017 p = skipwhite(p);
4018
4019 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4020 {
4021 /* replace $* by given arguments */
4022 i = 1;
4023 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4024 ++i;
4025 len = (int)STRLEN(p);
4026 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4027 if (new_cmdline == NULL)
4028 return NULL; /* out of memory */
4029 ptr = new_cmdline;
4030 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4031 {
4032 i = (int)(pos - program);
4033 STRNCPY(ptr, program, i);
4034 STRCPY(ptr += i, p);
4035 ptr += len;
4036 program = pos + 2;
4037 }
4038 STRCPY(ptr, program);
4039 }
4040 else
4041 {
4042 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4043 if (new_cmdline == NULL)
4044 return NULL; /* out of memory */
4045 STRCPY(new_cmdline, program);
4046 STRCAT(new_cmdline, " ");
4047 STRCAT(new_cmdline, p);
4048 }
4049 msg_make(p);
4050
4051 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4052 vim_free(*cmdlinep);
4053 *cmdlinep = new_cmdline;
4054 p = new_cmdline;
4055 }
4056 return p;
4057}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004058#endif
4059
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060/*
4061 * Expand file name in Ex command argument.
4062 * Return FAIL for failure, OK otherwise.
4063 */
4064 int
4065expand_filename(eap, cmdlinep, errormsgp)
4066 exarg_T *eap;
4067 char_u **cmdlinep;
4068 char_u **errormsgp;
4069{
4070 int has_wildcards; /* need to expand wildcards */
4071 char_u *repl;
4072 int srclen;
4073 char_u *p;
4074 int n;
4075
Bram Moolenaar748bf032005-02-02 23:04:36 +00004076#ifdef FEAT_QUICKFIX
4077 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4078 p = skip_grep_pat(eap);
4079#else
4080 p = eap->arg;
4081#endif
4082
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 /*
4084 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4085 * the file name contains a wildcard it should not cause expanding.
4086 * (it will be expanded anyway if there is a wildcard before replacing).
4087 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004088 has_wildcards = mch_has_wildcard(p);
4089 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004091#ifdef FEAT_EVAL
4092 /* Skip over `=expr`, wildcards in it are not expanded. */
4093 if (p[0] == '`' && p[1] == '=')
4094 {
4095 p += 2;
4096 (void)skip_expr(&p);
4097 if (*p == '`')
4098 ++p;
4099 continue;
4100 }
4101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 /*
4103 * Quick check if this cannot be the start of a special string.
4104 * Also removes backslash before '%', '#' and '<'.
4105 */
4106 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4107 {
4108 ++p;
4109 continue;
4110 }
4111
4112 /*
4113 * Try to find a match at this position.
4114 */
4115 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4116 if (*errormsgp != NULL) /* error detected */
4117 return FAIL;
4118 if (repl == NULL) /* no match found */
4119 {
4120 p += srclen;
4121 continue;
4122 }
4123
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004124 /* Wildcards won't be expanded below, the replacement is taken
4125 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4126 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4127 {
4128 char_u *l = repl;
4129
4130 repl = expand_env_save(repl);
4131 vim_free(l);
4132 }
4133
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 /* Need to escape white space et al. with a backslash. Don't do this
4135 * for shell commands (may have to use quotes instead). Don't do this
4136 * for non-unix systems when there is a single argument (spaces don't
4137 * separate arguments then). */
4138 if (!eap->usefilter
4139 && eap->cmdidx != CMD_bang
4140 && eap->cmdidx != CMD_make
4141 && eap->cmdidx != CMD_grep
4142 && eap->cmdidx != CMD_grepadd
4143#ifndef UNIX
4144 && !(eap->argt & NOSPC)
4145#endif
4146 )
4147 {
4148 char_u *l;
4149#ifdef BACKSLASH_IN_FILENAME
4150 /* Don't escape a backslash here, because rem_backslash() doesn't
4151 * remove it later. */
4152 static char_u *nobslash = (char_u *)" \t\"|";
4153# define ESCAPE_CHARS nobslash
4154#else
4155# define ESCAPE_CHARS escape_chars
4156#endif
4157
4158 for (l = repl; *l; ++l)
4159 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4160 {
4161 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4162 if (l != NULL)
4163 {
4164 vim_free(repl);
4165 repl = l;
4166 }
4167 break;
4168 }
4169 }
4170
4171 /* For a shell command a '!' must be escaped. */
4172 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaarea424162005-06-16 21:51:00 +00004173 && vim_strpbrk(repl, (char_u *)"!&;()") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {
4175 char_u *l;
4176
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004177 l = vim_strsave_escaped(repl, (char_u *)"!&;()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 if (l != NULL)
4179 {
4180 vim_free(repl);
4181 repl = l;
4182 /* For a sh-like shell escape it another time. */
4183 if (strstr((char *)p_sh, "sh") != NULL)
4184 {
4185 l = vim_strsave_escaped(repl, (char_u *)"!");
4186 if (l != NULL)
4187 {
4188 vim_free(repl);
4189 repl = l;
4190 }
4191 }
4192 }
4193 }
4194
4195 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4196 vim_free(repl);
4197 if (p == NULL)
4198 return FAIL;
4199 }
4200
4201 /*
4202 * One file argument: Expand wildcards.
4203 * Don't do this with ":r !command" or ":w !command".
4204 */
4205 if ((eap->argt & NOSPC) && !eap->usefilter)
4206 {
4207 /*
4208 * May do this twice:
4209 * 1. Replace environment variables.
4210 * 2. Replace any other wildcards, remove backslashes.
4211 */
4212 for (n = 1; n <= 2; ++n)
4213 {
4214 if (n == 2)
4215 {
4216#ifdef UNIX
4217 /*
4218 * Only for Unix we check for more than one file name.
4219 * For other systems spaces are considered to be part
4220 * of the file name.
4221 * Only check here if there is no wildcard, otherwise
4222 * ExpandOne() will check for errors. This allows
4223 * ":e `ls ve*.c`" on Unix.
4224 */
4225 if (!has_wildcards)
4226 for (p = eap->arg; *p; ++p)
4227 {
4228 /* skip escaped characters */
4229 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4230 ++p;
4231 else if (vim_iswhite(*p))
4232 {
4233 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4234 return FAIL;
4235 }
4236 }
4237#endif
4238
4239 /*
4240 * Halve the number of backslashes (this is Vi compatible).
4241 * For Unix and OS/2, when wildcards are expanded, this is
4242 * done by ExpandOne() below.
4243 */
4244#if defined(UNIX) || defined(OS2)
4245 if (!has_wildcards)
4246#endif
4247 backslash_halve(eap->arg);
4248#ifdef MACOS_CLASSIC
4249 /*
4250 * translate unix-like path components
4251 */
4252 slash_n_colon_adjust(eap->arg);
4253#endif
4254 }
4255
4256 if (has_wildcards)
4257 {
4258 if (n == 1)
4259 {
4260 /*
4261 * First loop: May expand environment variables. This
4262 * can be done much faster with expand_env() than with
4263 * something else (e.g., calling a shell).
4264 * After expanding environment variables, check again
4265 * if there are still wildcards present.
4266 */
4267 if (vim_strchr(eap->arg, '$') != NULL
4268 || vim_strchr(eap->arg, '~') != NULL)
4269 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004270 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4271 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 has_wildcards = mch_has_wildcard(NameBuff);
4273 p = NameBuff;
4274 }
4275 else
4276 p = NULL;
4277 }
4278 else /* n == 2 */
4279 {
4280 expand_T xpc;
4281
4282 ExpandInit(&xpc);
4283 xpc.xp_context = EXPAND_FILES;
4284 p = ExpandOne(&xpc, eap->arg, NULL,
4285 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4286 WILD_EXPAND_FREE);
4287 ExpandCleanup(&xpc);
4288 if (p == NULL)
4289 return FAIL;
4290 }
4291 if (p != NULL)
4292 {
4293 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4294 p, cmdlinep);
4295 if (n == 2) /* p came from ExpandOne() */
4296 vim_free(p);
4297 }
4298 }
4299 }
4300 }
4301 return OK;
4302}
4303
4304/*
4305 * Replace part of the command line, keeping eap->cmd, eap->arg and
4306 * eap->nextcmd correct.
4307 * "src" points to the part that is to be replaced, of length "srclen".
4308 * "repl" is the replacement string.
4309 * Returns a pointer to the character after the replaced string.
4310 * Returns NULL for failure.
4311 */
4312 static char_u *
4313repl_cmdline(eap, src, srclen, repl, cmdlinep)
4314 exarg_T *eap;
4315 char_u *src;
4316 int srclen;
4317 char_u *repl;
4318 char_u **cmdlinep;
4319{
4320 int len;
4321 int i;
4322 char_u *new_cmdline;
4323
4324 /*
4325 * The new command line is build in new_cmdline[].
4326 * First allocate it.
4327 * Careful: a "+cmd" argument may have been NUL terminated.
4328 */
4329 len = (int)STRLEN(repl);
4330 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
4331 if (eap->nextcmd)
4332 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4333 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4334 return NULL; /* out of memory! */
4335
4336 /*
4337 * Copy the stuff before the expanded part.
4338 * Copy the expanded stuff.
4339 * Copy what came after the expanded part.
4340 * Copy the next commands, if there are any.
4341 */
4342 i = (int)(src - *cmdlinep); /* length of part before match */
4343 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
4344 mch_memmove(new_cmdline + i, repl, (size_t)len);
4345 i += len; /* remember the end of the string */
4346 STRCPY(new_cmdline + i, src + srclen);
4347 src = new_cmdline + i; /* remember where to continue */
4348
4349 if (eap->nextcmd) /* append next command */
4350 {
4351 i = (int)STRLEN(new_cmdline) + 1;
4352 STRCPY(new_cmdline + i, eap->nextcmd);
4353 eap->nextcmd = new_cmdline + i;
4354 }
4355 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4356 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4357 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4358 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4359 vim_free(*cmdlinep);
4360 *cmdlinep = new_cmdline;
4361
4362 return src;
4363}
4364
4365/*
4366 * Check for '|' to separate commands and '"' to start comments.
4367 */
4368 void
4369separate_nextcmd(eap)
4370 exarg_T *eap;
4371{
4372 char_u *p;
4373
Bram Moolenaar86b68352004-12-27 21:59:20 +00004374#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004375 p = skip_grep_pat(eap);
4376#else
4377 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004378#endif
4379
4380 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 {
4382 if (*p == Ctrl_V)
4383 {
4384 if (eap->argt & (USECTRLV | XFILE))
4385 ++p; /* skip CTRL-V and next char */
4386 else
4387 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4388 if (*p == NUL) /* stop at NUL after CTRL-V */
4389 break;
4390 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004391
4392#ifdef FEAT_EVAL
4393 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004394 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004395 {
4396 p += 2;
4397 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004398 }
4399#endif
4400
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 /* Check for '"': start of comment or '|': next command */
4402 /* :@" and :*" do not start a comment!
4403 * :redir @" doesn't either. */
4404 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4405 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4406 || p != eap->arg)
4407 && (eap->cmdidx != CMD_redir
4408 || p != eap->arg + 1 || p[-1] != '@'))
4409 || *p == '|' || *p == '\n')
4410 {
4411 /*
4412 * We remove the '\' before the '|', unless USECTRLV is used
4413 * AND 'b' is present in 'cpoptions'.
4414 */
4415 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4416 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4417 {
4418 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4419 --p;
4420 }
4421 else
4422 {
4423 eap->nextcmd = check_nextcmd(p);
4424 *p = NUL;
4425 break;
4426 }
4427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004429
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4431 del_trailing_spaces(eap->arg);
4432}
4433
4434/*
4435 * get + command from ex argument
4436 */
4437 static char_u *
4438getargcmd(argp)
4439 char_u **argp;
4440{
4441 char_u *arg = *argp;
4442 char_u *command = NULL;
4443
4444 if (*arg == '+') /* +[command] */
4445 {
4446 ++arg;
4447 if (vim_isspace(*arg))
4448 command = dollar_command;
4449 else
4450 {
4451 command = arg;
4452 arg = skip_cmd_arg(command, TRUE);
4453 if (*arg != NUL)
4454 *arg++ = NUL; /* terminate command with NUL */
4455 }
4456
4457 arg = skipwhite(arg); /* skip over spaces */
4458 *argp = arg;
4459 }
4460 return command;
4461}
4462
4463/*
4464 * Find end of "+command" argument. Skip over "\ " and "\\".
4465 */
4466 static char_u *
4467skip_cmd_arg(p, rembs)
4468 char_u *p;
4469 int rembs; /* TRUE to halve the number of backslashes */
4470{
4471 while (*p && !vim_isspace(*p))
4472 {
4473 if (*p == '\\' && p[1] != NUL)
4474 {
4475 if (rembs)
4476 mch_memmove(p, p + 1, STRLEN(p));
4477 else
4478 ++p;
4479 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004480 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 }
4482 return p;
4483}
4484
4485/*
4486 * Get "++opt=arg" argument.
4487 * Return FAIL or OK.
4488 */
4489 static int
4490getargopt(eap)
4491 exarg_T *eap;
4492{
4493 char_u *arg = eap->arg + 2;
4494 int *pp = NULL;
4495#ifdef FEAT_MBYTE
4496 char_u *p;
4497#endif
4498
4499 /* ":edit ++[no]bin[ary] file" */
4500 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4501 {
4502 if (*arg == 'n')
4503 {
4504 arg += 2;
4505 eap->force_bin = FORCE_NOBIN;
4506 }
4507 else
4508 eap->force_bin = FORCE_BIN;
4509 if (!checkforcmd(&arg, "binary", 3))
4510 return FAIL;
4511 eap->arg = skipwhite(arg);
4512 return OK;
4513 }
4514
4515 if (STRNCMP(arg, "ff", 2) == 0)
4516 {
4517 arg += 2;
4518 pp = &eap->force_ff;
4519 }
4520 else if (STRNCMP(arg, "fileformat", 10) == 0)
4521 {
4522 arg += 10;
4523 pp = &eap->force_ff;
4524 }
4525#ifdef FEAT_MBYTE
4526 else if (STRNCMP(arg, "enc", 3) == 0)
4527 {
4528 arg += 3;
4529 pp = &eap->force_enc;
4530 }
4531 else if (STRNCMP(arg, "encoding", 8) == 0)
4532 {
4533 arg += 8;
4534 pp = &eap->force_enc;
4535 }
4536#endif
4537
4538 if (pp == NULL || *arg != '=')
4539 return FAIL;
4540
4541 ++arg;
4542 *pp = (int)(arg - eap->cmd);
4543 arg = skip_cmd_arg(arg, FALSE);
4544 eap->arg = skipwhite(arg);
4545 *arg = NUL;
4546
4547#ifdef FEAT_MBYTE
4548 if (pp == &eap->force_ff)
4549 {
4550#endif
4551 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4552 return FAIL;
4553#ifdef FEAT_MBYTE
4554 }
4555 else
4556 {
4557 /* Make 'fileencoding' lower case. */
4558 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4559 *p = TOLOWER_ASC(*p);
4560 }
4561#endif
4562
4563 return OK;
4564}
4565
4566/*
4567 * ":abbreviate" and friends.
4568 */
4569 static void
4570ex_abbreviate(eap)
4571 exarg_T *eap;
4572{
4573 do_exmap(eap, TRUE); /* almost the same as mapping */
4574}
4575
4576/*
4577 * ":map" and friends.
4578 */
4579 static void
4580ex_map(eap)
4581 exarg_T *eap;
4582{
4583 /*
4584 * If we are sourcing .exrc or .vimrc in current directory we
4585 * print the mappings for security reasons.
4586 */
4587 if (secure)
4588 {
4589 secure = 2;
4590 msg_outtrans(eap->cmd);
4591 msg_putchar('\n');
4592 }
4593 do_exmap(eap, FALSE);
4594}
4595
4596/*
4597 * ":unmap" and friends.
4598 */
4599 static void
4600ex_unmap(eap)
4601 exarg_T *eap;
4602{
4603 do_exmap(eap, FALSE);
4604}
4605
4606/*
4607 * ":mapclear" and friends.
4608 */
4609 static void
4610ex_mapclear(eap)
4611 exarg_T *eap;
4612{
4613 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4614}
4615
4616/*
4617 * ":abclear" and friends.
4618 */
4619 static void
4620ex_abclear(eap)
4621 exarg_T *eap;
4622{
4623 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4624}
4625
4626#ifdef FEAT_AUTOCMD
4627 static void
4628ex_autocmd(eap)
4629 exarg_T *eap;
4630{
4631 /*
4632 * Disallow auto commands from .exrc and .vimrc in current
4633 * directory for security reasons.
4634 */
4635 if (secure)
4636 {
4637 secure = 2;
4638 eap->errmsg = e_curdir;
4639 }
4640 else if (eap->cmdidx == CMD_autocmd)
4641 do_autocmd(eap->arg, eap->forceit);
4642 else
4643 do_augroup(eap->arg, eap->forceit);
4644}
4645
4646/*
4647 * ":doautocmd": Apply the automatic commands to the current buffer.
4648 */
4649 static void
4650ex_doautocmd(eap)
4651 exarg_T *eap;
4652{
4653 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004654 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655}
4656#endif
4657
4658#ifdef FEAT_LISTCMDS
4659/*
4660 * :[N]bunload[!] [N] [bufname] unload buffer
4661 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4662 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4663 */
4664 static void
4665ex_bunload(eap)
4666 exarg_T *eap;
4667{
4668 eap->errmsg = do_bufdel(
4669 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4670 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4671 : DOBUF_UNLOAD, eap->arg,
4672 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4673}
4674
4675/*
4676 * :[N]buffer [N] to buffer N
4677 * :[N]sbuffer [N] to buffer N
4678 */
4679 static void
4680ex_buffer(eap)
4681 exarg_T *eap;
4682{
4683 if (*eap->arg)
4684 eap->errmsg = e_trailing;
4685 else
4686 {
4687 if (eap->addr_count == 0) /* default is current buffer */
4688 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4689 else
4690 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4691 }
4692}
4693
4694/*
4695 * :[N]bmodified [N] to next mod. buffer
4696 * :[N]sbmodified [N] to next mod. buffer
4697 */
4698 static void
4699ex_bmodified(eap)
4700 exarg_T *eap;
4701{
4702 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4703}
4704
4705/*
4706 * :[N]bnext [N] to next buffer
4707 * :[N]sbnext [N] split and to next buffer
4708 */
4709 static void
4710ex_bnext(eap)
4711 exarg_T *eap;
4712{
4713 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4714}
4715
4716/*
4717 * :[N]bNext [N] to previous buffer
4718 * :[N]bprevious [N] to previous buffer
4719 * :[N]sbNext [N] split and to previous buffer
4720 * :[N]sbprevious [N] split and to previous buffer
4721 */
4722 static void
4723ex_bprevious(eap)
4724 exarg_T *eap;
4725{
4726 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4727}
4728
4729/*
4730 * :brewind to first buffer
4731 * :bfirst to first buffer
4732 * :sbrewind split and to first buffer
4733 * :sbfirst split and to first buffer
4734 */
4735 static void
4736ex_brewind(eap)
4737 exarg_T *eap;
4738{
4739 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4740}
4741
4742/*
4743 * :blast to last buffer
4744 * :sblast split and to last buffer
4745 */
4746 static void
4747ex_blast(eap)
4748 exarg_T *eap;
4749{
4750 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4751}
4752#endif
4753
4754 int
4755ends_excmd(c)
4756 int c;
4757{
4758 return (c == NUL || c == '|' || c == '"' || c == '\n');
4759}
4760
4761#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4762 || defined(PROTO)
4763/*
4764 * Return the next command, after the first '|' or '\n'.
4765 * Return NULL if not found.
4766 */
4767 char_u *
4768find_nextcmd(p)
4769 char_u *p;
4770{
4771 while (*p != '|' && *p != '\n')
4772 {
4773 if (*p == NUL)
4774 return NULL;
4775 ++p;
4776 }
4777 return (p + 1);
4778}
4779#endif
4780
4781/*
4782 * Check if *p is a separator between Ex commands.
4783 * Return NULL if it isn't, (p + 1) if it is.
4784 */
4785 char_u *
4786check_nextcmd(p)
4787 char_u *p;
4788{
4789 p = skipwhite(p);
4790 if (*p == '|' || *p == '\n')
4791 return (p + 1);
4792 else
4793 return NULL;
4794}
4795
4796/*
4797 * - if there are more files to edit
4798 * - and this is the last window
4799 * - and forceit not used
4800 * - and not repeated twice on a row
4801 * return FAIL and give error message if 'message' TRUE
4802 * return OK otherwise
4803 */
4804 static int
4805check_more(message, forceit)
4806 int message; /* when FALSE check only, no messages */
4807 int forceit;
4808{
4809 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4810
4811 if (!forceit && only_one_window() && ARGCOUNT > 1 && !arg_had_last
4812 && n >= 0 && quitmore == 0)
4813 {
4814 if (message)
4815 {
4816#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4817 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4818 {
4819 char_u buff[IOSIZE];
4820
4821 if (n == 1)
4822 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4823 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004824 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 _("%d more files to edit. Quit anyway?"), n);
4826 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4827 return OK;
4828 return FAIL;
4829 }
4830#endif
4831 if (n == 1)
4832 EMSG(_("E173: 1 more file to edit"));
4833 else
4834 EMSGN(_("E173: %ld more files to edit"), n);
4835 quitmore = 2; /* next try to quit is allowed */
4836 }
4837 return FAIL;
4838 }
4839 return OK;
4840}
4841
4842#ifdef FEAT_CMDL_COMPL
4843/*
4844 * Function given to ExpandGeneric() to obtain the list of command names.
4845 */
4846/*ARGSUSED*/
4847 char_u *
4848get_command_name(xp, idx)
4849 expand_T *xp;
4850 int idx;
4851{
4852 if (idx >= (int)CMD_SIZE)
4853# ifdef FEAT_USR_CMDS
4854 return get_user_command_name(idx);
4855# else
4856 return NULL;
4857# endif
4858 return cmdnames[idx].cmd_name;
4859}
4860#endif
4861
4862#if defined(FEAT_USR_CMDS) || defined(PROTO)
4863static 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));
4864static void uc_list __ARGS((char_u *name, size_t name_len));
4865static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4866static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4867static 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));
4868
4869 static int
4870uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4871 char_u *name;
4872 size_t name_len;
4873 char_u *rep;
4874 long argt;
4875 long def;
4876 int flags;
4877 int compl;
4878 char_u *compl_arg;
4879 int force;
4880{
4881 ucmd_T *cmd = NULL;
4882 char_u *p;
4883 int i;
4884 int cmp = 1;
4885 char_u *rep_buf = NULL;
4886 garray_T *gap;
4887
4888 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4889 if (rep_buf == NULL)
4890 {
4891 /* Can't replace termcodes - try using the string as is */
4892 rep_buf = vim_strsave(rep);
4893
4894 /* Give up if out of memory */
4895 if (rep_buf == NULL)
4896 return FAIL;
4897 }
4898
4899 /* get address of growarray: global or in curbuf */
4900 if (flags & UC_BUFFER)
4901 {
4902 gap = &curbuf->b_ucmds;
4903 if (gap->ga_itemsize == 0)
4904 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4905 }
4906 else
4907 gap = &ucmds;
4908
4909 /* Search for the command in the already defined commands. */
4910 for (i = 0; i < gap->ga_len; ++i)
4911 {
4912 size_t len;
4913
4914 cmd = USER_CMD_GA(gap, i);
4915 len = STRLEN(cmd->uc_name);
4916 cmp = STRNCMP(name, cmd->uc_name, name_len);
4917 if (cmp == 0)
4918 {
4919 if (name_len < len)
4920 cmp = -1;
4921 else if (name_len > len)
4922 cmp = 1;
4923 }
4924
4925 if (cmp == 0)
4926 {
4927 if (!force)
4928 {
4929 EMSG(_("E174: Command already exists: add ! to replace it"));
4930 goto fail;
4931 }
4932
4933 vim_free(cmd->uc_rep);
4934 cmd->uc_rep = 0;
4935 break;
4936 }
4937
4938 /* Stop as soon as we pass the name to add */
4939 if (cmp < 0)
4940 break;
4941 }
4942
4943 /* Extend the array unless we're replacing an existing command */
4944 if (cmp != 0)
4945 {
4946 if (ga_grow(gap, 1) != OK)
4947 goto fail;
4948 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
4949 goto fail;
4950
4951 cmd = USER_CMD_GA(gap, i);
4952 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
4953
4954 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955
4956 cmd->uc_name = p;
4957 }
4958
4959 cmd->uc_rep = rep_buf;
4960 cmd->uc_argt = argt;
4961 cmd->uc_def = def;
4962 cmd->uc_compl = compl;
4963#ifdef FEAT_EVAL
4964 cmd->uc_scriptID = current_SID;
4965# ifdef FEAT_CMDL_COMPL
4966 cmd->uc_compl_arg = compl_arg;
4967# endif
4968#endif
4969
4970 return OK;
4971
4972fail:
4973 vim_free(rep_buf);
4974#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4975 vim_free(compl_arg);
4976#endif
4977 return FAIL;
4978}
4979
4980/*
4981 * List of names for completion for ":command" with the EXPAND_ flag.
4982 * Must be alphabetical for completion.
4983 */
4984static struct
4985{
4986 int expand;
4987 char *name;
4988} command_complete[] =
4989{
4990 {EXPAND_AUGROUP, "augroup"},
4991 {EXPAND_BUFFERS, "buffer"},
4992 {EXPAND_COMMANDS, "command"},
4993#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4994 {EXPAND_USER_DEFINED, "custom"},
4995#endif
4996 {EXPAND_DIRECTORIES, "dir"},
4997 {EXPAND_ENV_VARS, "environment"},
4998 {EXPAND_EVENTS, "event"},
4999 {EXPAND_EXPRESSION, "expression"},
5000 {EXPAND_FILES, "file"},
5001 {EXPAND_FUNCTIONS, "function"},
5002 {EXPAND_HELP, "help"},
5003 {EXPAND_HIGHLIGHT, "highlight"},
5004 {EXPAND_MAPPINGS, "mapping"},
5005 {EXPAND_MENUS, "menu"},
5006 {EXPAND_SETTINGS, "option"},
5007 {EXPAND_TAGS, "tag"},
5008 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5009 {EXPAND_USER_VARS, "var"},
5010 {0, NULL}
5011};
5012
5013 static void
5014uc_list(name, name_len)
5015 char_u *name;
5016 size_t name_len;
5017{
5018 int i, j;
5019 int found = FALSE;
5020 ucmd_T *cmd;
5021 int len;
5022 long a;
5023 garray_T *gap;
5024
5025 gap = &curbuf->b_ucmds;
5026 for (;;)
5027 {
5028 for (i = 0; i < gap->ga_len; ++i)
5029 {
5030 cmd = USER_CMD_GA(gap, i);
5031 a = cmd->uc_argt;
5032
5033 /* Skip commands which don't match the requested prefix */
5034 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5035 continue;
5036
5037 /* Put out the title first time */
5038 if (!found)
5039 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5040 found = TRUE;
5041 msg_putchar('\n');
5042 if (got_int)
5043 break;
5044
5045 /* Special cases */
5046 msg_putchar(a & BANG ? '!' : ' ');
5047 msg_putchar(a & REGSTR ? '"' : ' ');
5048 msg_putchar(gap != &ucmds ? 'b' : ' ');
5049 msg_putchar(' ');
5050
5051 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5052 len = (int)STRLEN(cmd->uc_name) + 4;
5053
5054 do {
5055 msg_putchar(' ');
5056 ++len;
5057 } while (len < 16);
5058
5059 len = 0;
5060
5061 /* Arguments */
5062 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5063 {
5064 case 0: IObuff[len++] = '0'; break;
5065 case (EXTRA): IObuff[len++] = '*'; break;
5066 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5067 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5068 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5069 }
5070
5071 do {
5072 IObuff[len++] = ' ';
5073 } while (len < 5);
5074
5075 /* Range */
5076 if (a & (RANGE|COUNT))
5077 {
5078 if (a & COUNT)
5079 {
5080 /* -count=N */
5081 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5082 len += (int)STRLEN(IObuff + len);
5083 }
5084 else if (a & DFLALL)
5085 IObuff[len++] = '%';
5086 else if (cmd->uc_def >= 0)
5087 {
5088 /* -range=N */
5089 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5090 len += (int)STRLEN(IObuff + len);
5091 }
5092 else
5093 IObuff[len++] = '.';
5094 }
5095
5096 do {
5097 IObuff[len++] = ' ';
5098 } while (len < 11);
5099
5100 /* Completion */
5101 for (j = 0; command_complete[j].expand != 0; ++j)
5102 if (command_complete[j].expand == cmd->uc_compl)
5103 {
5104 STRCPY(IObuff + len, command_complete[j].name);
5105 len += (int)STRLEN(IObuff + len);
5106 break;
5107 }
5108
5109 do {
5110 IObuff[len++] = ' ';
5111 } while (len < 21);
5112
5113 IObuff[len] = '\0';
5114 msg_outtrans(IObuff);
5115
5116 msg_outtrans_special(cmd->uc_rep, FALSE);
5117 out_flush();
5118 ui_breakcheck();
5119 if (got_int)
5120 break;
5121 }
5122 if (gap == &ucmds || i < gap->ga_len)
5123 break;
5124 gap = &ucmds;
5125 }
5126
5127 if (!found)
5128 MSG(_("No user-defined commands found"));
5129}
5130
5131 static char_u *
5132uc_fun_cmd()
5133{
5134 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5135 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5136 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5137 0xb9, 0x7f, 0};
5138 int i;
5139
5140 for (i = 0; fcmd[i]; ++i)
5141 IObuff[i] = fcmd[i] - 0x40;
5142 IObuff[i] = 0;
5143 return IObuff;
5144}
5145
5146 static int
5147uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5148 char_u *attr;
5149 size_t len;
5150 long *argt;
5151 long *def;
5152 int *flags;
5153 int *compl;
5154 char_u **compl_arg;
5155{
5156 char_u *p;
5157
5158 if (len == 0)
5159 {
5160 EMSG(_("E175: No attribute specified"));
5161 return FAIL;
5162 }
5163
5164 /* First, try the simple attributes (no arguments) */
5165 if (STRNICMP(attr, "bang", len) == 0)
5166 *argt |= BANG;
5167 else if (STRNICMP(attr, "buffer", len) == 0)
5168 *flags |= UC_BUFFER;
5169 else if (STRNICMP(attr, "register", len) == 0)
5170 *argt |= REGSTR;
5171 else if (STRNICMP(attr, "bar", len) == 0)
5172 *argt |= TRLBAR;
5173 else
5174 {
5175 int i;
5176 char_u *val = NULL;
5177 size_t vallen = 0;
5178 size_t attrlen = len;
5179
5180 /* Look for the attribute name - which is the part before any '=' */
5181 for (i = 0; i < (int)len; ++i)
5182 {
5183 if (attr[i] == '=')
5184 {
5185 val = &attr[i + 1];
5186 vallen = len - i - 1;
5187 attrlen = i;
5188 break;
5189 }
5190 }
5191
5192 if (STRNICMP(attr, "nargs", attrlen) == 0)
5193 {
5194 if (vallen == 1)
5195 {
5196 if (*val == '0')
5197 /* Do nothing - this is the default */;
5198 else if (*val == '1')
5199 *argt |= (EXTRA | NOSPC | NEEDARG);
5200 else if (*val == '*')
5201 *argt |= EXTRA;
5202 else if (*val == '?')
5203 *argt |= (EXTRA | NOSPC);
5204 else if (*val == '+')
5205 *argt |= (EXTRA | NEEDARG);
5206 else
5207 goto wrong_nargs;
5208 }
5209 else
5210 {
5211wrong_nargs:
5212 EMSG(_("E176: Invalid number of arguments"));
5213 return FAIL;
5214 }
5215 }
5216 else if (STRNICMP(attr, "range", attrlen) == 0)
5217 {
5218 *argt |= RANGE;
5219 if (vallen == 1 && *val == '%')
5220 *argt |= DFLALL;
5221 else if (val != NULL)
5222 {
5223 p = val;
5224 if (*def >= 0)
5225 {
5226two_count:
5227 EMSG(_("E177: Count cannot be specified twice"));
5228 return FAIL;
5229 }
5230
5231 *def = getdigits(&p);
5232 *argt |= (ZEROR | NOTADR);
5233
5234 if (p != val + vallen || vallen == 0)
5235 {
5236invalid_count:
5237 EMSG(_("E178: Invalid default value for count"));
5238 return FAIL;
5239 }
5240 }
5241 }
5242 else if (STRNICMP(attr, "count", attrlen) == 0)
5243 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005244 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
5246 if (val != NULL)
5247 {
5248 p = val;
5249 if (*def >= 0)
5250 goto two_count;
5251
5252 *def = getdigits(&p);
5253
5254 if (p != val + vallen)
5255 goto invalid_count;
5256 }
5257
5258 if (*def < 0)
5259 *def = 0;
5260 }
5261 else if (STRNICMP(attr, "complete", attrlen) == 0)
5262 {
5263 char_u *arg = NULL;
5264 size_t arglen = 0;
5265
5266 if (val == NULL)
5267 {
5268 EMSG(_("E179: argument required for complete"));
5269 return FAIL;
5270 }
5271 /* Look for any argument part - which is the part after any ',' */
5272 for (i = 0; i < (int)vallen; ++i)
5273 {
5274 if (val[i] == ',')
5275 {
5276 arg = &val[i + 1];
5277 arglen = vallen - i - 1;
5278 vallen = i;
5279 break;
5280 }
5281 }
5282
5283 for (i = 0; command_complete[i].expand != 0; ++i)
5284 {
5285 if (STRLEN(command_complete[i].name) == vallen
5286 && STRNCMP(val, command_complete[i].name, vallen) == 0)
5287 {
5288 *compl = command_complete[i].expand;
5289 if (command_complete[i].expand == EXPAND_BUFFERS)
5290 *argt |= BUFNAME;
5291 else if (command_complete[i].expand == EXPAND_DIRECTORIES
5292 || command_complete[i].expand == EXPAND_FILES)
5293 *argt |= XFILE;
5294 break;
5295 }
5296 }
5297
5298 if (command_complete[i].expand == 0)
5299 {
5300 EMSG2(_("E180: Invalid complete value: %s"), val);
5301 return FAIL;
5302 }
5303#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5304 if (*compl != EXPAND_USER_DEFINED && arg != NULL)
5305#else
5306 if (arg != NULL)
5307#endif
5308 {
5309 EMSG(_("E468: Completion argument only allowed for custom completion"));
5310 return FAIL;
5311 }
5312#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5313 if (*compl == EXPAND_USER_DEFINED && arg == NULL)
5314 {
5315 EMSG(_("E467: Custom completion requires a function argument"));
5316 return FAIL;
5317 }
5318 if (arg != NULL)
5319 *compl_arg = vim_strnsave(arg, (int)arglen);
5320#endif
5321 }
5322 else
5323 {
5324 char_u ch = attr[len];
5325 attr[len] = '\0';
5326 EMSG2(_("E181: Invalid attribute: %s"), attr);
5327 attr[len] = ch;
5328 return FAIL;
5329 }
5330 }
5331
5332 return OK;
5333}
5334
5335 static void
5336ex_command(eap)
5337 exarg_T *eap;
5338{
5339 char_u *name;
5340 char_u *end;
5341 char_u *p;
5342 long argt = 0;
5343 long def = -1;
5344 int flags = 0;
5345 int compl = EXPAND_NOTHING;
5346 char_u *compl_arg = NULL;
5347 int has_attr = (eap->arg[0] == '-');
5348
5349 p = eap->arg;
5350
5351 /* Check for attributes */
5352 while (*p == '-')
5353 {
5354 ++p;
5355 end = skiptowhite(p);
5356 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5357 == FAIL)
5358 return;
5359 p = skipwhite(end);
5360 }
5361
5362 /* Get the name (if any) and skip to the following argument */
5363 name = p;
5364 if (ASCII_ISALPHA(*p))
5365 while (ASCII_ISALNUM(*p))
5366 ++p;
5367 if (!ends_excmd(*p) && !vim_iswhite(*p))
5368 {
5369 EMSG(_("E182: Invalid command name"));
5370 return;
5371 }
5372 end = p;
5373
5374 /* If there is nothing after the name, and no attributes were specified,
5375 * we are listing commands
5376 */
5377 p = skipwhite(end);
5378 if (!has_attr && ends_excmd(*p))
5379 {
5380 uc_list(name, end - name);
5381 }
5382 else if (!ASCII_ISUPPER(*name))
5383 {
5384 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5385 return;
5386 }
5387 else
5388 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5389 eap->forceit);
5390}
5391
5392/*
5393 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 * Clear all user commands, global and for current buffer.
5395 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005396/*ARGSUSED*/
5397 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398ex_comclear(eap)
5399 exarg_T *eap;
5400{
5401 uc_clear(&ucmds);
5402 uc_clear(&curbuf->b_ucmds);
5403}
5404
5405/*
5406 * Clear all user commands for "gap".
5407 */
5408 void
5409uc_clear(gap)
5410 garray_T *gap;
5411{
5412 int i;
5413 ucmd_T *cmd;
5414
5415 for (i = 0; i < gap->ga_len; ++i)
5416 {
5417 cmd = USER_CMD_GA(gap, i);
5418 vim_free(cmd->uc_name);
5419 vim_free(cmd->uc_rep);
5420# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5421 vim_free(cmd->uc_compl_arg);
5422# endif
5423 }
5424 ga_clear(gap);
5425}
5426
5427 static void
5428ex_delcommand(eap)
5429 exarg_T *eap;
5430{
5431 int i = 0;
5432 ucmd_T *cmd = NULL;
5433 int cmp = -1;
5434 garray_T *gap;
5435
5436 gap = &curbuf->b_ucmds;
5437 for (;;)
5438 {
5439 for (i = 0; i < gap->ga_len; ++i)
5440 {
5441 cmd = USER_CMD_GA(gap, i);
5442 cmp = STRCMP(eap->arg, cmd->uc_name);
5443 if (cmp <= 0)
5444 break;
5445 }
5446 if (gap == &ucmds || cmp == 0)
5447 break;
5448 gap = &ucmds;
5449 }
5450
5451 if (cmp != 0)
5452 {
5453 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5454 return;
5455 }
5456
5457 vim_free(cmd->uc_name);
5458 vim_free(cmd->uc_rep);
5459# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5460 vim_free(cmd->uc_compl_arg);
5461# endif
5462
5463 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
5465 if (i < gap->ga_len)
5466 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5467}
5468
5469 static char_u *
5470uc_split_args(arg, lenp)
5471 char_u *arg;
5472 size_t *lenp;
5473{
5474 char_u *buf;
5475 char_u *p;
5476 char_u *q;
5477 int len;
5478
5479 /* Precalculate length */
5480 p = arg;
5481 len = 2; /* Initial and final quotes */
5482
5483 while (*p)
5484 {
5485 if (p[0] == '\\' && vim_iswhite(p[1]))
5486 {
5487 len += 1;
5488 p += 2;
5489 }
5490 else if (*p == '\\' || *p == '"')
5491 {
5492 len += 2;
5493 p += 1;
5494 }
5495 else if (vim_iswhite(*p))
5496 {
5497 p = skipwhite(p);
5498 if (*p == NUL)
5499 break;
5500 len += 3; /* "," */
5501 }
5502 else
5503 {
5504 ++len;
5505 ++p;
5506 }
5507 }
5508
5509 buf = alloc(len + 1);
5510 if (buf == NULL)
5511 {
5512 *lenp = 0;
5513 return buf;
5514 }
5515
5516 p = arg;
5517 q = buf;
5518 *q++ = '"';
5519 while (*p)
5520 {
5521 if (p[0] == '\\' && vim_iswhite(p[1]))
5522 {
5523 *q++ = p[1];
5524 p += 2;
5525 }
5526 else if (*p == '\\' || *p == '"')
5527 {
5528 *q++ = '\\';
5529 *q++ = *p++;
5530 }
5531 else if (vim_iswhite(*p))
5532 {
5533 p = skipwhite(p);
5534 if (*p == NUL)
5535 break;
5536 *q++ = '"';
5537 *q++ = ',';
5538 *q++ = '"';
5539 }
5540 else
5541 {
5542 *q++ = *p++;
5543 }
5544 }
5545 *q++ = '"';
5546 *q = 0;
5547
5548 *lenp = len;
5549 return buf;
5550}
5551
5552/*
5553 * Check for a <> code in a user command.
5554 * "code" points to the '<'. "len" the length of the <> (inclusive).
5555 * "buf" is where the result is to be added.
5556 * "split_buf" points to a buffer used for splitting, caller should free it.
5557 * "split_len" is the length of what "split_buf" contains.
5558 * Returns the length of the replacement, which has been added to "buf".
5559 * Returns -1 if there was no match, and only the "<" has been copied.
5560 */
5561 static size_t
5562uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5563 char_u *code;
5564 size_t len;
5565 char_u *buf;
5566 ucmd_T *cmd; /* the user command we're expanding */
5567 exarg_T *eap; /* ex arguments */
5568 char_u **split_buf;
5569 size_t *split_len;
5570{
5571 size_t result = 0;
5572 char_u *p = code + 1;
5573 size_t l = len - 2;
5574 int quote = 0;
5575 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5576 ct_LT, ct_NONE } type = ct_NONE;
5577
5578 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5579 {
5580 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5581 p += 2;
5582 l -= 2;
5583 }
5584
5585 if (l < 1)
5586 type = ct_NONE;
5587 else if (STRNICMP(p, "args", l) == 0)
5588 type = ct_ARGS;
5589 else if (STRNICMP(p, "bang", l) == 0)
5590 type = ct_BANG;
5591 else if (STRNICMP(p, "count", l) == 0)
5592 type = ct_COUNT;
5593 else if (STRNICMP(p, "line1", l) == 0)
5594 type = ct_LINE1;
5595 else if (STRNICMP(p, "line2", l) == 0)
5596 type = ct_LINE2;
5597 else if (STRNICMP(p, "lt", l) == 0)
5598 type = ct_LT;
5599 else if (STRNICMP(p, "register", l) == 0)
5600 type = ct_REGISTER;
5601
5602 switch (type)
5603 {
5604 case ct_ARGS:
5605 /* Simple case first */
5606 if (*eap->arg == NUL)
5607 {
5608 if (quote == 1)
5609 {
5610 result = 2;
5611 if (buf != NULL)
5612 STRCPY(buf, "''");
5613 }
5614 else
5615 result = 0;
5616 break;
5617 }
5618
5619 /* When specified there is a single argument don't split it.
5620 * Works for ":Cmd %" when % is "a b c". */
5621 if ((eap->argt & NOSPC) && quote == 2)
5622 quote = 1;
5623
5624 switch (quote)
5625 {
5626 case 0: /* No quoting, no splitting */
5627 result = STRLEN(eap->arg);
5628 if (buf != NULL)
5629 STRCPY(buf, eap->arg);
5630 break;
5631 case 1: /* Quote, but don't split */
5632 result = STRLEN(eap->arg) + 2;
5633 for (p = eap->arg; *p; ++p)
5634 {
5635 if (*p == '\\' || *p == '"')
5636 ++result;
5637 }
5638
5639 if (buf != NULL)
5640 {
5641 *buf++ = '"';
5642 for (p = eap->arg; *p; ++p)
5643 {
5644 if (*p == '\\' || *p == '"')
5645 *buf++ = '\\';
5646 *buf++ = *p;
5647 }
5648 *buf = '"';
5649 }
5650
5651 break;
5652 case 2: /* Quote and split */
5653 /* This is hard, so only do it once, and cache the result */
5654 if (*split_buf == NULL)
5655 *split_buf = uc_split_args(eap->arg, split_len);
5656
5657 result = *split_len;
5658 if (buf != NULL && result != 0)
5659 STRCPY(buf, *split_buf);
5660
5661 break;
5662 }
5663 break;
5664
5665 case ct_BANG:
5666 result = eap->forceit ? 1 : 0;
5667 if (quote)
5668 result += 2;
5669 if (buf != NULL)
5670 {
5671 if (quote)
5672 *buf++ = '"';
5673 if (eap->forceit)
5674 *buf++ = '!';
5675 if (quote)
5676 *buf = '"';
5677 }
5678 break;
5679
5680 case ct_LINE1:
5681 case ct_LINE2:
5682 case ct_COUNT:
5683 {
5684 char num_buf[20];
5685 long num = (type == ct_LINE1) ? eap->line1 :
5686 (type == ct_LINE2) ? eap->line2 :
5687 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5688 size_t num_len;
5689
5690 sprintf(num_buf, "%ld", num);
5691 num_len = STRLEN(num_buf);
5692 result = num_len;
5693
5694 if (quote)
5695 result += 2;
5696
5697 if (buf != NULL)
5698 {
5699 if (quote)
5700 *buf++ = '"';
5701 STRCPY(buf, num_buf);
5702 buf += num_len;
5703 if (quote)
5704 *buf = '"';
5705 }
5706
5707 break;
5708 }
5709
5710 case ct_REGISTER:
5711 result = eap->regname ? 1 : 0;
5712 if (quote)
5713 result += 2;
5714 if (buf != NULL)
5715 {
5716 if (quote)
5717 *buf++ = '\'';
5718 if (eap->regname)
5719 *buf++ = eap->regname;
5720 if (quote)
5721 *buf = '\'';
5722 }
5723 break;
5724
5725 case ct_LT:
5726 result = 1;
5727 if (buf != NULL)
5728 *buf = '<';
5729 break;
5730
5731 default:
5732 /* Not recognized: just copy the '<' and return -1. */
5733 result = (size_t)-1;
5734 if (buf != NULL)
5735 *buf = '<';
5736 break;
5737 }
5738
5739 return result;
5740}
5741
5742 static void
5743do_ucmd(eap)
5744 exarg_T *eap;
5745{
5746 char_u *buf;
5747 char_u *p;
5748 char_u *q;
5749
5750 char_u *start;
5751 char_u *end;
5752 size_t len, totlen;
5753
5754 size_t split_len = 0;
5755 char_u *split_buf = NULL;
5756 ucmd_T *cmd;
5757#ifdef FEAT_EVAL
5758 scid_T save_current_SID = current_SID;
5759#endif
5760
5761 if (eap->cmdidx == CMD_USER)
5762 cmd = USER_CMD(eap->useridx);
5763 else
5764 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5765
5766 /*
5767 * Replace <> in the command by the arguments.
5768 */
5769 buf = NULL;
5770 for (;;)
5771 {
5772 p = cmd->uc_rep;
5773 q = buf;
5774 totlen = 0;
5775 while ((start = vim_strchr(p, '<')) != NULL
5776 && (end = vim_strchr(start + 1, '>')) != NULL)
5777 {
5778 /* Include the '>' */
5779 ++end;
5780
5781 /* Take everything up to the '<' */
5782 len = start - p;
5783 if (buf == NULL)
5784 totlen += len;
5785 else
5786 {
5787 mch_memmove(q, p, len);
5788 q += len;
5789 }
5790
5791 len = uc_check_code(start, end - start, q, cmd, eap,
5792 &split_buf, &split_len);
5793 if (len == (size_t)-1)
5794 {
5795 /* no match, continue after '<' */
5796 p = start + 1;
5797 len = 1;
5798 }
5799 else
5800 p = end;
5801 if (buf == NULL)
5802 totlen += len;
5803 else
5804 q += len;
5805 }
5806 if (buf != NULL) /* second time here, finished */
5807 {
5808 STRCPY(q, p);
5809 break;
5810 }
5811
5812 totlen += STRLEN(p); /* Add on the trailing characters */
5813 buf = alloc((unsigned)(totlen + 1));
5814 if (buf == NULL)
5815 {
5816 vim_free(split_buf);
5817 return;
5818 }
5819 }
5820
5821#ifdef FEAT_EVAL
5822 current_SID = cmd->uc_scriptID;
5823#endif
5824 (void)do_cmdline(buf, eap->getline, eap->cookie,
5825 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5826#ifdef FEAT_EVAL
5827 current_SID = save_current_SID;
5828#endif
5829 vim_free(buf);
5830 vim_free(split_buf);
5831}
5832
5833# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5834 static char_u *
5835get_user_command_name(idx)
5836 int idx;
5837{
5838 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5839}
5840
5841/*
5842 * Function given to ExpandGeneric() to obtain the list of user command names.
5843 */
5844/*ARGSUSED*/
5845 char_u *
5846get_user_commands(xp, idx)
5847 expand_T *xp;
5848 int idx;
5849{
5850 if (idx < curbuf->b_ucmds.ga_len)
5851 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5852 idx -= curbuf->b_ucmds.ga_len;
5853 if (idx < ucmds.ga_len)
5854 return USER_CMD(idx)->uc_name;
5855 return NULL;
5856}
5857
5858/*
5859 * Function given to ExpandGeneric() to obtain the list of user command
5860 * attributes.
5861 */
5862/*ARGSUSED*/
5863 char_u *
5864get_user_cmd_flags(xp, idx)
5865 expand_T *xp;
5866 int idx;
5867{
5868 static char *user_cmd_flags[] =
5869 {"bang", "bar", "buffer", "complete", "count",
5870 "nargs", "range", "register"};
5871
5872 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5873 return NULL;
5874 return (char_u *)user_cmd_flags[idx];
5875}
5876
5877/*
5878 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5879 */
5880/*ARGSUSED*/
5881 char_u *
5882get_user_cmd_nargs(xp, idx)
5883 expand_T *xp;
5884 int idx;
5885{
5886 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5887
5888 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5889 return NULL;
5890 return (char_u *)user_cmd_nargs[idx];
5891}
5892
5893/*
5894 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5895 */
5896/*ARGSUSED*/
5897 char_u *
5898get_user_cmd_complete(xp, idx)
5899 expand_T *xp;
5900 int idx;
5901{
5902 return (char_u *)command_complete[idx].name;
5903}
5904# endif /* FEAT_CMDL_COMPL */
5905
5906#endif /* FEAT_USR_CMDS */
5907
5908 static void
5909ex_colorscheme(eap)
5910 exarg_T *eap;
5911{
5912 if (load_colors(eap->arg) == FAIL)
5913 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
5914}
5915
5916 static void
5917ex_highlight(eap)
5918 exarg_T *eap;
5919{
5920 if (*eap->arg == NUL && eap->cmd[2] == '!')
5921 MSG(_("Greetings, Vim user!"));
5922 do_highlight(eap->arg, eap->forceit, FALSE);
5923}
5924
5925
5926/*
5927 * Call this function if we thought we were going to exit, but we won't
5928 * (because of an error). May need to restore the terminal mode.
5929 */
5930 void
5931not_exiting()
5932{
5933 exiting = FALSE;
5934 settmode(TMODE_RAW);
5935}
5936
5937/*
5938 * ":quit": quit current window, quit Vim if closed the last window.
5939 */
5940 static void
5941ex_quit(eap)
5942 exarg_T *eap;
5943{
5944#ifdef FEAT_CMDWIN
5945 if (cmdwin_type != 0)
5946 {
5947 cmdwin_result = Ctrl_C;
5948 return;
5949 }
5950#endif
5951
5952#ifdef FEAT_NETBEANS_INTG
5953 netbeansForcedQuit = eap->forceit;
5954#endif
5955
5956 /*
5957 * If there are more files or windows we won't exit.
5958 */
5959 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5960 exiting = TRUE;
5961 if ((!P_HID(curbuf)
5962 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
5963 || check_more(TRUE, eap->forceit) == FAIL
5964 || (only_one_window() && check_changed_any(eap->forceit)))
5965 {
5966 not_exiting();
5967 }
5968 else
5969 {
5970#ifdef FEAT_WINDOWS
5971 if (only_one_window()) /* quit last window */
5972#endif
5973 getout(0);
5974#ifdef FEAT_WINDOWS
5975# ifdef FEAT_GUI
5976 need_mouse_correct = TRUE;
5977# endif
5978 /* close window; may free buffer */
5979 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
5980#endif
5981 }
5982}
5983
5984/*
5985 * ":cquit".
5986 */
5987/*ARGSUSED*/
5988 static void
5989ex_cquit(eap)
5990 exarg_T *eap;
5991{
5992 getout(1); /* this does not always pass on the exit code to the Manx
5993 compiler. why? */
5994}
5995
5996/*
5997 * ":qall": try to quit all windows
5998 */
5999 static void
6000ex_quit_all(eap)
6001 exarg_T *eap;
6002{
6003# ifdef FEAT_CMDWIN
6004 if (cmdwin_type != 0)
6005 {
6006 if (eap->forceit)
6007 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6008 else
6009 cmdwin_result = K_XF2;
6010 return;
6011 }
6012# endif
6013 exiting = TRUE;
6014 if (eap->forceit || !check_changed_any(FALSE))
6015 getout(0);
6016 not_exiting();
6017}
6018
6019#ifdef FEAT_WINDOWS
6020/*
6021 * ":close": close current window, unless it is the last one
6022 */
6023 static void
6024ex_close(eap)
6025 exarg_T *eap;
6026{
6027# ifdef FEAT_CMDWIN
6028 if (cmdwin_type != 0)
6029 cmdwin_result = K_IGNORE;
6030 else
6031# endif
6032 ex_win_close(eap, curwin);
6033}
6034
6035 static void
6036ex_win_close(eap, win)
6037 exarg_T *eap;
6038 win_T *win;
6039{
6040 int need_hide;
6041 buf_T *buf = win->w_buffer;
6042
6043 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
6044 if (need_hide && !P_HID(buf) && !eap->forceit)
6045 {
6046#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6047 if ((p_confirm || cmdmod.confirm) && p_write)
6048 {
6049 dialog_changed(buf, FALSE);
6050 if (buf_valid(buf) && bufIsChanged(buf))
6051 return;
6052 need_hide = FALSE;
6053 }
6054 else
6055#endif
6056 {
6057 EMSG(_(e_nowrtmsg));
6058 return;
6059 }
6060 }
6061
6062#ifdef FEAT_GUI
6063 need_mouse_correct = TRUE;
6064#endif
6065 /* free buffer when not hiding it or when it's a scratch buffer */
6066 win_close(win, !need_hide && !P_HID(buf));
6067}
6068
6069#ifdef FEAT_QUICKFIX
6070/*
6071 * ":pclose": Close any preview window.
6072 */
6073 static void
6074ex_pclose(eap)
6075 exarg_T *eap;
6076{
6077 win_T *win;
6078
6079 for (win = firstwin; win != NULL; win = win->w_next)
6080 if (win->w_p_pvw)
6081 {
6082 ex_win_close(eap, win);
6083 break;
6084 }
6085}
6086#endif
6087
6088/*
6089 * ":only".
6090 */
6091 static void
6092ex_only(eap)
6093 exarg_T *eap;
6094{
6095# ifdef FEAT_GUI
6096 need_mouse_correct = TRUE;
6097# endif
6098 close_others(TRUE, eap->forceit);
6099}
6100
6101/*
6102 * ":all" and ":sall".
6103 */
6104 static void
6105ex_all(eap)
6106 exarg_T *eap;
6107{
6108 if (eap->addr_count == 0)
6109 eap->line2 = 9999;
6110 do_arg_all((int)eap->line2, eap->forceit);
6111}
6112#endif /* FEAT_WINDOWS */
6113
6114 static void
6115ex_hide(eap)
6116 exarg_T *eap;
6117{
6118 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6119 eap->errmsg = e_invarg;
6120 else
6121 {
6122 /* ":hide" or ":hide | cmd": hide current window */
6123 eap->nextcmd = check_nextcmd(eap->arg);
6124#ifdef FEAT_WINDOWS
6125 if (!eap->skip)
6126 {
6127# ifdef FEAT_GUI
6128 need_mouse_correct = TRUE;
6129# endif
6130 win_close(curwin, FALSE); /* don't free buffer */
6131 }
6132#endif
6133 }
6134}
6135
6136/*
6137 * ":stop" and ":suspend": Suspend Vim.
6138 */
6139 static void
6140ex_stop(eap)
6141 exarg_T *eap;
6142{
6143 /*
6144 * Disallow suspending for "rvim".
6145 */
6146 if (!check_restricted()
6147#ifdef WIN3264
6148 /*
6149 * Check if external commands are allowed now.
6150 */
6151 && can_end_termcap_mode(TRUE)
6152#endif
6153 )
6154 {
6155 if (!eap->forceit)
6156 autowrite_all();
6157 windgoto((int)Rows - 1, 0);
6158 out_char('\n');
6159 out_flush();
6160 stoptermcap();
6161 out_flush(); /* needed for SUN to restore xterm buffer */
6162#ifdef FEAT_TITLE
6163 mch_restore_title(3); /* restore window titles */
6164#endif
6165 ui_suspend(); /* call machine specific function */
6166#ifdef FEAT_TITLE
6167 maketitle();
6168 resettitle(); /* force updating the title */
6169#endif
6170 starttermcap();
6171 scroll_start(); /* scroll screen before redrawing */
6172 redraw_later_clear();
6173 shell_resized(); /* may have resized window */
6174 }
6175}
6176
6177/*
6178 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6179 */
6180 static void
6181ex_exit(eap)
6182 exarg_T *eap;
6183{
6184#ifdef FEAT_CMDWIN
6185 if (cmdwin_type != 0)
6186 {
6187 cmdwin_result = Ctrl_C;
6188 return;
6189 }
6190#endif
6191
6192 /*
6193 * if more files or windows we won't exit
6194 */
6195 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6196 exiting = TRUE;
6197 if ( ((eap->cmdidx == CMD_wq
6198 || curbufIsChanged())
6199 && do_write(eap) == FAIL)
6200 || check_more(TRUE, eap->forceit) == FAIL
6201 || (only_one_window() && check_changed_any(eap->forceit)))
6202 {
6203 not_exiting();
6204 }
6205 else
6206 {
6207#ifdef FEAT_WINDOWS
6208 if (only_one_window()) /* quit last window, exit Vim */
6209#endif
6210 getout(0);
6211#ifdef FEAT_WINDOWS
6212# ifdef FEAT_GUI
6213 need_mouse_correct = TRUE;
6214# endif
6215 /* quit current window, may free buffer */
6216 win_close(curwin, !P_HID(curwin->w_buffer));
6217#endif
6218 }
6219}
6220
6221/*
6222 * ":print", ":list", ":number".
6223 */
6224 static void
6225ex_print(eap)
6226 exarg_T *eap;
6227{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006228 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6229 EMSG(_(e_emptybuf));
6230 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006232 for ( ;!got_int; ui_breakcheck())
6233 {
6234 print_line(eap->line1,
6235 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6236 || (eap->flags & EXFLAG_NR)),
6237 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6238 if (++eap->line1 > eap->line2)
6239 break;
6240 out_flush(); /* show one line at a time */
6241 }
6242 setpcmark();
6243 /* put cursor at last line */
6244 curwin->w_cursor.lnum = eap->line2;
6245 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 }
6247
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249}
6250
6251#ifdef FEAT_BYTEOFF
6252 static void
6253ex_goto(eap)
6254 exarg_T *eap;
6255{
6256 goto_byte(eap->line2);
6257}
6258#endif
6259
6260/*
6261 * ":shell".
6262 */
6263/*ARGSUSED*/
6264 static void
6265ex_shell(eap)
6266 exarg_T *eap;
6267{
6268 do_shell(NULL, 0);
6269}
6270
6271#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6272 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
6273 || defined(PROTO)
6274
6275/*
6276 * Handle a file drop. The code is here because a drop is *nearly* like an
6277 * :args command, but not quite (we have a list of exact filenames, so we
6278 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6279 * code is very similar to :args and hence needs access to a lot of the static
6280 * functions in this file.
6281 *
6282 * The list should be allocated using alloc(), as should each item in the
6283 * list. This function takes over responsibility for freeing the list.
6284 *
6285 * XXX The list is made into the arggument list. This is freed using
6286 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6287 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6288 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6289 * file functionality is (currently) not in EMX this is not presently a
6290 * problem.
6291 */
6292 void
6293handle_drop(filec, filev, split)
6294 int filec; /* the number of files dropped */
6295 char_u **filev; /* the list of files dropped */
6296 int split; /* force splitting the window */
6297{
6298 exarg_T ea;
6299 int save_msg_scroll = msg_scroll;
6300
6301# ifdef FEAT_CMDWIN
6302 if (cmdwin_type != 0)
6303 return;
6304# endif
6305
6306 /* Check whether the current buffer is changed. If so, we will need
6307 * to split the current window or data could be lost.
6308 * We don't need to check if the 'hidden' option is set, as in this
6309 * case the buffer won't be lost.
6310 */
6311 if (!P_HID(curbuf) && !split)
6312 {
6313 ++emsg_off;
6314 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6315 --emsg_off;
6316 }
6317 if (split)
6318 {
6319# ifdef FEAT_WINDOWS
6320 if (win_split(0, 0) == FAIL)
6321 return;
6322# ifdef FEAT_SCROLLBIND
6323 curwin->w_p_scb = FALSE;
6324# endif
6325
6326 /* When splitting the window, create a new alist. Otherwise the
6327 * existing one is overwritten. */
6328 alist_unlink(curwin->w_alist);
6329 alist_new();
6330# else
6331 return; /* can't split, always fail */
6332# endif
6333 }
6334
6335 /*
6336 * Set up the new argument list.
6337 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006338 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339
6340 /*
6341 * Move to the first file.
6342 */
6343 /* Fake up a minimal "next" command for do_argfile() */
6344 vim_memset(&ea, 0, sizeof(ea));
6345 ea.cmd = (char_u *)"next";
6346 do_argfile(&ea, 0);
6347
6348 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6349 * mode that is not needed here. */
6350 need_start_insertmode = FALSE;
6351
6352 /* Restore msg_scroll, otherwise a following command may cause scrolling
6353 * unexpectedly. The screen will be redrawn by the caller, thus
6354 * msg_scroll being set by displaying a message is irrelevant. */
6355 msg_scroll = save_msg_scroll;
6356}
6357#endif
6358
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359/*
6360 * Clear an argument list: free all file names and reset it to zero entries.
6361 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006362 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363alist_clear(al)
6364 alist_T *al;
6365{
6366 while (--al->al_ga.ga_len >= 0)
6367 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6368 ga_clear(&al->al_ga);
6369}
6370
6371/*
6372 * Init an argument list.
6373 */
6374 void
6375alist_init(al)
6376 alist_T *al;
6377{
6378 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6379}
6380
6381#if defined(FEAT_WINDOWS) || defined(PROTO)
6382
6383/*
6384 * Remove a reference from an argument list.
6385 * Ignored when the argument list is the global one.
6386 * If the argument list is no longer used by any window, free it.
6387 */
6388 void
6389alist_unlink(al)
6390 alist_T *al;
6391{
6392 if (al != &global_alist && --al->al_refcount <= 0)
6393 {
6394 alist_clear(al);
6395 vim_free(al);
6396 }
6397}
6398
6399# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6400/*
6401 * Create a new argument list and use it for the current window.
6402 */
6403 void
6404alist_new()
6405{
6406 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6407 if (curwin->w_alist == NULL)
6408 {
6409 curwin->w_alist = &global_alist;
6410 ++global_alist.al_refcount;
6411 }
6412 else
6413 {
6414 curwin->w_alist->al_refcount = 1;
6415 alist_init(curwin->w_alist);
6416 }
6417}
6418# endif
6419#endif
6420
6421#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6422/*
6423 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006424 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6425 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 */
6427 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006428alist_expand(fnum_list, fnum_len)
6429 int *fnum_list;
6430 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431{
6432 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006433 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 char_u **new_arg_files;
6435 int new_arg_file_count;
6436 char_u *save_p_su = p_su;
6437 int i;
6438
6439 /* Don't use 'suffixes' here. This should work like the shell did the
6440 * expansion. Also, the vimrc file isn't read yet, thus the user
6441 * can't set the options. */
6442 p_su = empty_option;
6443 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6444 if (old_arg_files != NULL)
6445 {
6446 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006447 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6448 old_arg_count = GARGCOUNT;
6449 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 &new_arg_file_count, &new_arg_files,
6451 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6452 && new_arg_file_count > 0)
6453 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006454 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6455 TRUE, fnum_list, fnum_len);
6456 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 }
6459 p_su = save_p_su;
6460}
6461#endif
6462
6463/*
6464 * Set the argument list for the current window.
6465 * Takes over the allocated files[] and the allocated fnames in it.
6466 */
6467 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006468alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 alist_T *al;
6470 int count;
6471 char_u **files;
6472 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006473 int *fnum_list;
6474 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475{
6476 int i;
6477
6478 alist_clear(al);
6479 if (ga_grow(&al->al_ga, count) == OK)
6480 {
6481 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006482 {
6483 if (got_int)
6484 {
6485 /* When adding many buffers this can take a long time. Allow
6486 * interrupting here. */
6487 while (i < count)
6488 vim_free(files[i++]);
6489 break;
6490 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006491
6492 /* May set buffer name of a buffer previously used for the
6493 * argument list, so that it's re-used by alist_add. */
6494 if (fnum_list != NULL && i < fnum_len)
6495 buf_set_name(fnum_list[i], files[i]);
6496
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006498 ui_breakcheck();
6499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 vim_free(files);
6501 }
6502 else
6503 FreeWild(count, files);
6504#ifdef FEAT_WINDOWS
6505 if (al == &global_alist)
6506#endif
6507 arg_had_last = FALSE;
6508}
6509
6510/*
6511 * Add file "fname" to argument list "al".
6512 * "fname" must have been allocated and "al" must have been checked for room.
6513 */
6514 void
6515alist_add(al, fname, set_fnum)
6516 alist_T *al;
6517 char_u *fname;
6518 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6519{
6520 if (fname == NULL) /* don't add NULL file names */
6521 return;
6522#ifdef BACKSLASH_IN_FILENAME
6523 slash_adjust(fname);
6524#endif
6525 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6526 if (set_fnum > 0)
6527 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6528 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6529 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530}
6531
6532#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6533/*
6534 * Adjust slashes in file names. Called after 'shellslash' was set.
6535 */
6536 void
6537alist_slash_adjust()
6538{
6539 int i;
6540# ifdef FEAT_WINDOWS
6541 win_T *wp;
6542# endif
6543
6544 for (i = 0; i < GARGCOUNT; ++i)
6545 if (GARGLIST[i].ae_fname != NULL)
6546 slash_adjust(GARGLIST[i].ae_fname);
6547# ifdef FEAT_WINDOWS
6548 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6549 if (wp->w_alist != &global_alist)
6550 for (i = 0; i < WARGCOUNT(wp); ++i)
6551 if (WARGLIST(wp)[i].ae_fname != NULL)
6552 slash_adjust(WARGLIST(wp)[i].ae_fname);
6553# endif
6554}
6555#endif
6556
6557/*
6558 * ":preserve".
6559 */
6560/*ARGSUSED*/
6561 static void
6562ex_preserve(eap)
6563 exarg_T *eap;
6564{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006565 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 ml_preserve(curbuf, TRUE);
6567}
6568
6569/*
6570 * ":recover".
6571 */
6572 static void
6573ex_recover(eap)
6574 exarg_T *eap;
6575{
6576 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6577 recoverymode = TRUE;
6578 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6579 && (*eap->arg == NUL
6580 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6581 ml_recover();
6582 recoverymode = FALSE;
6583}
6584
6585/*
6586 * Command modifier used in a wrong way.
6587 */
6588 static void
6589ex_wrongmodifier(eap)
6590 exarg_T *eap;
6591{
6592 eap->errmsg = e_invcmd;
6593}
6594
6595#ifdef FEAT_WINDOWS
6596/*
6597 * :sview [+command] file split window with new file, read-only
6598 * :split [[+command] file] split window with current or new file
6599 * :vsplit [[+command] file] split window vertically with current or new file
6600 * :new [[+command] file] split window with no or new file
6601 * :vnew [[+command] file] split vertically window with no or new file
6602 * :sfind [+command] file split window with file in 'path'
6603 */
6604 void
6605ex_splitview(eap)
6606 exarg_T *eap;
6607{
6608 win_T *old_curwin;
6609#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6610 char_u *fname = NULL;
6611#endif
6612#ifdef FEAT_BROWSE
6613 int browse_flag = cmdmod.browse;
6614#endif
6615
6616#ifndef FEAT_VERTSPLIT
6617 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6618 {
6619 ex_ni(eap);
6620 return;
6621 }
6622#endif
6623
6624 old_curwin = curwin;
6625#ifdef FEAT_GUI
6626 need_mouse_correct = TRUE;
6627#endif
6628
6629#ifdef FEAT_QUICKFIX
6630 /* A ":split" in the quickfix window works like ":new". Don't want two
6631 * quickfix windows. */
6632 if (bt_quickfix(curbuf))
6633 {
6634 if (eap->cmdidx == CMD_split)
6635 eap->cmdidx = CMD_new;
6636# ifdef FEAT_VERTSPLIT
6637 if (eap->cmdidx == CMD_vsplit)
6638 eap->cmdidx = CMD_vnew;
6639# endif
6640 }
6641#endif
6642
6643#ifdef FEAT_SEARCHPATH
6644 if (eap->cmdidx == CMD_sfind)
6645 {
6646 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6647 FNAME_MESS, TRUE, curbuf->b_ffname);
6648 if (fname == NULL)
6649 goto theend;
6650 eap->arg = fname;
6651 }
6652# ifdef FEAT_BROWSE
6653 else
6654# endif
6655#endif
6656#ifdef FEAT_BROWSE
6657 if (cmdmod.browse
6658# ifdef FEAT_VERTSPLIT
6659 && eap->cmdidx != CMD_vnew
6660#endif
6661 && eap->cmdidx != CMD_new)
6662 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006663 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 eap->arg, NULL, NULL, NULL, curbuf);
6665 if (fname == NULL)
6666 goto theend;
6667 eap->arg = fname;
6668 }
6669 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
6670#endif
6671
6672 if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
6673 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6674 {
6675#ifdef FEAT_SCROLLBIND
6676 /* Reset 'scrollbind' when editing another file, but keep it when
6677 * doing ":split" without arguments. */
6678 if (*eap->arg != NUL
6679#ifdef FEAT_BROWSE
6680 || cmdmod.browse
6681#endif
6682 )
6683 curwin->w_p_scb = FALSE;
6684 else
6685 do_check_scrollbind(FALSE);
6686#endif
6687 do_exedit(eap, old_curwin);
6688 }
6689
6690#ifdef FEAT_BROWSE
6691 cmdmod.browse = browse_flag;
6692#endif
6693
6694#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6695theend:
6696 vim_free(fname);
6697#endif
6698}
6699#endif
6700
6701/*
6702 * ":mode": Set screen mode.
6703 * If no argument given, just get the screen size and redraw.
6704 */
6705 static void
6706ex_mode(eap)
6707 exarg_T *eap;
6708{
6709 if (*eap->arg == NUL)
6710 shell_resized();
6711 else
6712 mch_screenmode(eap->arg);
6713}
6714
6715#ifdef FEAT_WINDOWS
6716/*
6717 * ":resize".
6718 * set, increment or decrement current window height
6719 */
6720 static void
6721ex_resize(eap)
6722 exarg_T *eap;
6723{
6724 int n;
6725 win_T *wp = curwin;
6726
6727 if (eap->addr_count > 0)
6728 {
6729 n = eap->line2;
6730 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6731 ;
6732 }
6733
6734#ifdef FEAT_GUI
6735 need_mouse_correct = TRUE;
6736#endif
6737 n = atol((char *)eap->arg);
6738#ifdef FEAT_VERTSPLIT
6739 if (cmdmod.split & WSP_VERT)
6740 {
6741 if (*eap->arg == '-' || *eap->arg == '+')
6742 n += W_WIDTH(curwin);
6743 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6744 n = 9999;
6745 win_setwidth_win((int)n, wp);
6746 }
6747 else
6748#endif
6749 {
6750 if (*eap->arg == '-' || *eap->arg == '+')
6751 n += curwin->w_height;
6752 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6753 n = 9999;
6754 win_setheight_win((int)n, wp);
6755 }
6756}
6757#endif
6758
6759/*
6760 * ":find [+command] <file>" command.
6761 */
6762 static void
6763ex_find(eap)
6764 exarg_T *eap;
6765{
6766#ifdef FEAT_SEARCHPATH
6767 char_u *fname;
6768 int count;
6769
6770 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6771 TRUE, curbuf->b_ffname);
6772 if (eap->addr_count > 0)
6773 {
6774 /* Repeat finding the file "count" times. This matters when it
6775 * appears several times in the path. */
6776 count = eap->line2;
6777 while (fname != NULL && --count > 0)
6778 {
6779 vim_free(fname);
6780 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6781 FALSE, curbuf->b_ffname);
6782 }
6783 }
6784
6785 if (fname != NULL)
6786 {
6787 eap->arg = fname;
6788#endif
6789 do_exedit(eap, NULL);
6790#ifdef FEAT_SEARCHPATH
6791 vim_free(fname);
6792 }
6793#endif
6794}
6795
6796/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006797 * ":open" simulation: for now just work like ":visual".
6798 */
6799 static void
6800ex_open(eap)
6801 exarg_T *eap;
6802{
6803 regmatch_T regmatch;
6804 char_u *p;
6805
6806 curwin->w_cursor.lnum = eap->line2;
6807 beginline(BL_SOL | BL_FIX);
6808 if (*eap->arg == '/')
6809 {
6810 /* ":open /pattern/": put cursor in column found with pattern */
6811 ++eap->arg;
6812 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6813 *p = NUL;
6814 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6815 if (regmatch.regprog != NULL)
6816 {
6817 regmatch.rm_ic = p_ic;
6818 p = ml_get_curline();
6819 if (vim_regexec(&regmatch, p, (colnr_T)0))
6820 curwin->w_cursor.col = regmatch.startp[0] - p;
6821 else
6822 EMSG(_(e_nomatch));
6823 vim_free(regmatch.regprog);
6824 }
6825 /* Move to the NUL, ignore any other arguments. */
6826 eap->arg += STRLEN(eap->arg);
6827 }
6828 check_cursor();
6829
6830 eap->cmdidx = CMD_visual;
6831 do_exedit(eap, NULL);
6832}
6833
6834/*
6835 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 */
6837 static void
6838ex_edit(eap)
6839 exarg_T *eap;
6840{
6841 do_exedit(eap, NULL);
6842}
6843
6844/*
6845 * ":edit <file>" command and alikes.
6846 */
6847/*ARGSUSED*/
6848 void
6849do_exedit(eap, old_curwin)
6850 exarg_T *eap;
6851 win_T *old_curwin; /* curwin before doing a split or NULL */
6852{
6853 int n;
6854#ifdef FEAT_WINDOWS
6855 int need_hide;
6856#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006857 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858
6859 /*
6860 * ":vi" command ends Ex mode.
6861 */
6862 if (exmode_active && (eap->cmdidx == CMD_visual
6863 || eap->cmdidx == CMD_view))
6864 {
6865 exmode_active = FALSE;
6866 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006867 {
6868 /* Special case: ":global/pat/visual\NLvi-commands" */
6869 if (global_busy)
6870 {
6871 int rd = RedrawingDisabled;
6872 int nwr = no_wait_return;
6873 int ms = msg_scroll;
6874#ifdef FEAT_GUI
6875 int he = hold_gui_events;
6876#endif
6877
6878 if (eap->nextcmd != NULL)
6879 {
6880 stuffReadbuff(eap->nextcmd);
6881 eap->nextcmd = NULL;
6882 }
6883
6884 if (exmode_was != EXMODE_VIM)
6885 settmode(TMODE_RAW);
6886 RedrawingDisabled = 0;
6887 no_wait_return = 0;
6888 need_wait_return = FALSE;
6889 msg_scroll = 0;
6890#ifdef FEAT_GUI
6891 hold_gui_events = 0;
6892#endif
6893 must_redraw = CLEAR;
6894
6895 main_loop(FALSE, TRUE);
6896
6897 RedrawingDisabled = rd;
6898 no_wait_return = nwr;
6899 msg_scroll = ms;
6900#ifdef FEAT_GUI
6901 hold_gui_events = he;
6902#endif
6903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 }
6907
6908 if ((eap->cmdidx == CMD_new
6909#ifdef FEAT_VERTSPLIT
6910 || eap->cmdidx == CMD_vnew
6911#endif
6912 ) && *eap->arg == NUL)
6913 {
6914 /* ":new" without argument: edit an new empty buffer */
6915 setpcmark();
6916 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
6917 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
6918 }
6919 else if ((eap->cmdidx != CMD_split
6920#ifdef FEAT_VERTSPLIT
6921 && eap->cmdidx != CMD_vsplit
6922#endif
6923 )
6924 || *eap->arg != NUL
6925#ifdef FEAT_BROWSE
6926 || cmdmod.browse
6927#endif
6928 )
6929 {
6930 n = readonlymode;
6931 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6932 readonlymode = TRUE;
6933 else if (eap->cmdidx == CMD_enew)
6934 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
6935 empty buffer */
6936 setpcmark();
6937 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6938 NULL, eap,
6939 /* ":edit" goes to first line if Vi compatible */
6940 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6941 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6942 ? ECMD_ONE : eap->do_ecmd_lnum,
6943 (P_HID(curbuf) ? ECMD_HIDE : 0)
6944 + (eap->forceit ? ECMD_FORCEIT : 0)
6945#ifdef FEAT_LISTCMDS
6946 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
6947#endif
6948 ) == FAIL)
6949 {
6950 /* Editing the file failed. If the window was split, close it. */
6951#ifdef FEAT_WINDOWS
6952 if (old_curwin != NULL)
6953 {
6954 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
6955 if (!need_hide || P_HID(curbuf))
6956 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006957# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6958 cleanup_T cs;
6959
6960 /* Reset the error/interrupt/exception state here so that
6961 * aborting() returns FALSE when closing a window. */
6962 enter_cleanup(&cs);
6963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964# ifdef FEAT_GUI
6965 need_mouse_correct = TRUE;
6966# endif
6967 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006968
6969# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6970 /* Restore the error/interrupt/exception state if not
6971 * discarded by a new aborting error, interrupt, or
6972 * uncaught exception. */
6973 leave_cleanup(&cs);
6974# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 }
6976 }
6977#endif
6978 }
6979 else if (readonlymode && curbuf->b_nwindows == 1)
6980 {
6981 /* When editing an already visited buffer, 'readonly' won't be set
6982 * but the previous value is kept. With ":view" and ":sview" we
6983 * want the file to be readonly, except when another window is
6984 * editing the same buffer. */
6985 curbuf->b_p_ro = TRUE;
6986 }
6987 readonlymode = n;
6988 }
6989 else
6990 {
6991 if (eap->do_ecmd_cmd != NULL)
6992 do_cmdline_cmd(eap->do_ecmd_cmd);
6993#ifdef FEAT_TITLE
6994 n = curwin->w_arg_idx_invalid;
6995#endif
6996 check_arg_idx(curwin);
6997#ifdef FEAT_TITLE
6998 if (n != curwin->w_arg_idx_invalid)
6999 maketitle();
7000#endif
7001 }
7002
7003#ifdef FEAT_WINDOWS
7004 /*
7005 * if ":split file" worked, set alternate file name in old window to new
7006 * file
7007 */
7008 if (old_curwin != NULL
7009 && *eap->arg != NUL
7010 && curwin != old_curwin
7011 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007012 && old_curwin->w_buffer != curbuf
7013 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 old_curwin->w_alt_fnum = curbuf->b_fnum;
7015#endif
7016
7017 ex_no_reprint = TRUE;
7018}
7019
7020#ifndef FEAT_GUI
7021/*
7022 * ":gui" and ":gvim" when there is no GUI.
7023 */
7024 static void
7025ex_nogui(eap)
7026 exarg_T *eap;
7027{
7028 eap->errmsg = e_nogvim;
7029}
7030#endif
7031
7032#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7033 static void
7034ex_tearoff(eap)
7035 exarg_T *eap;
7036{
7037 gui_make_tearoff(eap->arg);
7038}
7039#endif
7040
Bram Moolenaar843ee412004-06-30 16:16:41 +00007041#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_KDE) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 static void
7043ex_popup(eap)
7044 exarg_T *eap;
7045{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007046 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047}
7048#endif
7049
7050/*ARGSUSED*/
7051 static void
7052ex_swapname(eap)
7053 exarg_T *eap;
7054{
7055 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7056 MSG(_("No swap file"));
7057 else
7058 msg(curbuf->b_ml.ml_mfp->mf_fname);
7059}
7060
7061/*
7062 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7063 * offset.
7064 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7065 */
7066/*ARGSUSED*/
7067 static void
7068ex_syncbind(eap)
7069 exarg_T *eap;
7070{
7071#ifdef FEAT_SCROLLBIND
7072 win_T *wp;
7073 long topline;
7074 long y;
7075 linenr_T old_linenr = curwin->w_cursor.lnum;
7076
7077 setpcmark();
7078
7079 /*
7080 * determine max topline
7081 */
7082 if (curwin->w_p_scb)
7083 {
7084 topline = curwin->w_topline;
7085 for (wp = firstwin; wp; wp = wp->w_next)
7086 {
7087 if (wp->w_p_scb && wp->w_buffer)
7088 {
7089 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7090 if (topline > y)
7091 topline = y;
7092 }
7093 }
7094 if (topline < 1)
7095 topline = 1;
7096 }
7097 else
7098 {
7099 topline = 1;
7100 }
7101
7102
7103 /*
7104 * set all scrollbind windows to the same topline
7105 */
7106 wp = curwin;
7107 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7108 {
7109 if (curwin->w_p_scb)
7110 {
7111 y = topline - curwin->w_topline;
7112 if (y > 0)
7113 scrollup(y, TRUE);
7114 else
7115 scrolldown(-y, TRUE);
7116 curwin->w_scbind_pos = topline;
7117 redraw_later(VALID);
7118 cursor_correct();
7119#ifdef FEAT_WINDOWS
7120 curwin->w_redr_status = TRUE;
7121#endif
7122 }
7123 }
7124 curwin = wp;
7125 if (curwin->w_p_scb)
7126 {
7127 did_syncbind = TRUE;
7128 checkpcmark();
7129 if (old_linenr != curwin->w_cursor.lnum)
7130 {
7131 char_u ctrl_o[2];
7132
7133 ctrl_o[0] = Ctrl_O;
7134 ctrl_o[1] = 0;
7135 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7136 }
7137 }
7138#endif
7139}
7140
7141
7142 static void
7143ex_read(eap)
7144 exarg_T *eap;
7145{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007146 int i;
7147 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7148 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
7150 if (eap->usefilter) /* :r!cmd */
7151 do_bang(1, eap, FALSE, FALSE, TRUE);
7152 else
7153 {
7154 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7155 return;
7156
7157#ifdef FEAT_BROWSE
7158 if (cmdmod.browse)
7159 {
7160 char_u *browseFile;
7161
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007162 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 NULL, NULL, NULL, curbuf);
7164 if (browseFile != NULL)
7165 {
7166 i = readfile(browseFile, NULL,
7167 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7168 vim_free(browseFile);
7169 }
7170 else
7171 i = OK;
7172 }
7173 else
7174#endif
7175 if (*eap->arg == NUL)
7176 {
7177 if (check_fname() == FAIL) /* check for no file name */
7178 return;
7179 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7180 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7181 }
7182 else
7183 {
7184 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7185 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7186 i = readfile(eap->arg, NULL,
7187 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7188
7189 }
7190 if (i == FAIL)
7191 {
7192#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7193 if (!aborting())
7194#endif
7195 EMSG2(_(e_notopen), eap->arg);
7196 }
7197 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007198 {
7199 if (empty && exmode_active)
7200 {
7201 /* Delete the empty line that remains. Historically ex does
7202 * this but vi doesn't. */
7203 if (eap->line2 == 0)
7204 lnum = curbuf->b_ml.ml_line_count;
7205 else
7206 lnum = 1;
7207 if (*ml_get(lnum) == NUL)
7208 {
7209 ml_delete(lnum, FALSE);
7210 deleted_lines_mark(lnum, 1L);
7211 if (curwin->w_cursor.lnum >= lnum)
7212 --curwin->w_cursor.lnum;
7213 }
7214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 }
7218}
7219
Bram Moolenaarea408852005-06-25 22:49:46 +00007220static char_u *prev_dir = NULL;
7221
7222#if defined(EXITFREE) || defined(PROTO)
7223 void
7224free_cd_dir()
7225{
7226 vim_free(prev_dir);
7227}
7228#endif
7229
7230
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231/*
7232 * ":cd", ":lcd", ":chdir" and ":lchdir".
7233 */
7234 static void
7235ex_cd(eap)
7236 exarg_T *eap;
7237{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 char_u *new_dir;
7239 char_u *tofree;
7240
7241 new_dir = eap->arg;
7242#if !defined(UNIX) && !defined(VMS)
7243 /* for non-UNIX ":cd" means: print current directory */
7244 if (*new_dir == NUL)
7245 ex_pwd(NULL);
7246 else
7247#endif
7248 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007249 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7250 && !eap->forceit)
7251 {
7252 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7253 return;
7254 }
7255
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 /* ":cd -": Change to previous directory */
7257 if (STRCMP(new_dir, "-") == 0)
7258 {
7259 if (prev_dir == NULL)
7260 {
7261 EMSG(_("E186: No previous directory"));
7262 return;
7263 }
7264 new_dir = prev_dir;
7265 }
7266
7267 /* Save current directory for next ":cd -" */
7268 tofree = prev_dir;
7269 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7270 prev_dir = vim_strsave(NameBuff);
7271 else
7272 prev_dir = NULL;
7273
7274#if defined(UNIX) || defined(VMS)
7275 /* for UNIX ":cd" means: go to home directory */
7276 if (*new_dir == NUL)
7277 {
7278 /* use NameBuff for home directory name */
7279# ifdef VMS
7280 char_u *p;
7281
7282 p = mch_getenv((char_u *)"SYS$LOGIN");
7283 if (p == NULL || *p == NUL) /* empty is the same as not set */
7284 NameBuff[0] = NUL;
7285 else
7286 STRNCPY(NameBuff, p, MAXPATHL);
7287# else
7288 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7289# endif
7290 new_dir = NameBuff;
7291 }
7292#endif
7293 if (new_dir == NULL || vim_chdir(new_dir))
7294 EMSG(_(e_failed));
7295 else
7296 {
7297 vim_free(curwin->w_localdir);
7298 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7299 {
7300 /* If still in global directory, need to remember current
7301 * directory as global directory. */
7302 if (globaldir == NULL && prev_dir != NULL)
7303 globaldir = vim_strsave(prev_dir);
7304 /* Remember this local directory for the window. */
7305 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7306 curwin->w_localdir = vim_strsave(NameBuff);
7307 }
7308 else
7309 {
7310 /* We are now in the global directory, no need to remember its
7311 * name. */
7312 vim_free(globaldir);
7313 globaldir = NULL;
7314 curwin->w_localdir = NULL;
7315 }
7316
7317 shorten_fnames(TRUE);
7318
7319 /* Echo the new current directory if the command was typed. */
7320 if (KeyTyped)
7321 ex_pwd(eap);
7322 }
7323 vim_free(tofree);
7324 }
7325}
7326
7327/*
7328 * ":pwd".
7329 */
7330/*ARGSUSED*/
7331 static void
7332ex_pwd(eap)
7333 exarg_T *eap;
7334{
7335 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7336 {
7337#ifdef BACKSLASH_IN_FILENAME
7338 slash_adjust(NameBuff);
7339#endif
7340 msg(NameBuff);
7341 }
7342 else
7343 EMSG(_("E187: Unknown"));
7344}
7345
7346/*
7347 * ":=".
7348 */
7349 static void
7350ex_equal(eap)
7351 exarg_T *eap;
7352{
7353 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007354 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355}
7356
7357 static void
7358ex_sleep(eap)
7359 exarg_T *eap;
7360{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007361 int n;
7362 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363
7364 if (cursor_valid())
7365 {
7366 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7367 if (n >= 0)
7368 windgoto((int)n, curwin->w_wcol);
7369 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007370
7371 len = eap->line2;
7372 switch (*eap->arg)
7373 {
7374 case 'm': break;
7375 case NUL: len *= 1000L; break;
7376 default: EMSG2(_(e_invarg2), eap->arg); return;
7377 }
7378 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379}
7380
7381/*
7382 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7383 */
7384 void
7385do_sleep(msec)
7386 long msec;
7387{
7388 long done;
7389
7390 cursor_on();
7391 out_flush();
7392 for (done = 0; !got_int && done < msec; done += 1000L)
7393 {
7394 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7395 ui_breakcheck();
7396 }
7397}
7398
7399 static void
7400do_exmap(eap, isabbrev)
7401 exarg_T *eap;
7402 int isabbrev;
7403{
7404 int mode;
7405 char_u *cmdp;
7406
7407 cmdp = eap->cmd;
7408 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7409
7410 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7411 eap->arg, mode, isabbrev))
7412 {
7413 case 1: EMSG(_(e_invarg));
7414 break;
7415 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7416 break;
7417 }
7418}
7419
7420/*
7421 * ":winsize" command (obsolete).
7422 */
7423 static void
7424ex_winsize(eap)
7425 exarg_T *eap;
7426{
7427 int w, h;
7428 char_u *arg = eap->arg;
7429 char_u *p;
7430
7431 w = getdigits(&arg);
7432 arg = skipwhite(arg);
7433 p = arg;
7434 h = getdigits(&arg);
7435 if (*p != NUL && *arg == NUL)
7436 set_shellsize(w, h, TRUE);
7437 else
7438 EMSG(_("E465: :winsize requires two number arguments"));
7439}
7440
7441#ifdef FEAT_WINDOWS
7442 static void
7443ex_wincmd(eap)
7444 exarg_T *eap;
7445{
7446 int xchar = NUL;
7447 char_u *p;
7448
7449 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7450 {
7451 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7452 if (eap->arg[1] == NUL)
7453 {
7454 EMSG(_(e_invarg));
7455 return;
7456 }
7457 xchar = eap->arg[1];
7458 p = eap->arg + 2;
7459 }
7460 else
7461 p = eap->arg + 1;
7462
7463 eap->nextcmd = check_nextcmd(p);
7464 p = skipwhite(p);
7465 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7466 EMSG(_(e_invarg));
7467 else
7468 {
7469 /* Pass flags on for ":vertical wincmd ]". */
7470 postponed_split_flags = cmdmod.split;
7471 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7472 postponed_split_flags = 0;
7473 }
7474}
7475#endif
7476
Bram Moolenaar843ee412004-06-30 16:16:41 +00007477#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478/*
7479 * ":winpos".
7480 */
7481 static void
7482ex_winpos(eap)
7483 exarg_T *eap;
7484{
7485 int x, y;
7486 char_u *arg = eap->arg;
7487 char_u *p;
7488
7489 if (*arg == NUL)
7490 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007491# if defined(FEAT_GUI) || defined(MSWIN)
7492# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007494# else
7495 if (mch_get_winpos(&x, &y) != FAIL)
7496# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 {
7498 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7499 msg(IObuff);
7500 }
7501 else
7502# endif
7503 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7504 }
7505 else
7506 {
7507 x = getdigits(&arg);
7508 arg = skipwhite(arg);
7509 p = arg;
7510 y = getdigits(&arg);
7511 if (*p == NUL || *arg != NUL)
7512 {
7513 EMSG(_("E466: :winpos requires two number arguments"));
7514 return;
7515 }
7516# ifdef FEAT_GUI
7517 if (gui.in_use)
7518 gui_mch_set_winpos(x, y);
7519 else if (gui.starting)
7520 {
7521 /* Remember the coordinates for when the window is opened. */
7522 gui_win_x = x;
7523 gui_win_y = y;
7524 }
7525# ifdef HAVE_TGETENT
7526 else
7527# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007528# else
7529# ifdef MSWIN
7530 mch_set_winpos(x, y);
7531# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532# endif
7533# ifdef HAVE_TGETENT
7534 if (*T_CWP)
7535 term_set_winpos(x, y);
7536# endif
7537 }
7538}
7539#endif
7540
7541/*
7542 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7543 */
7544 static void
7545ex_operators(eap)
7546 exarg_T *eap;
7547{
7548 oparg_T oa;
7549
7550 clear_oparg(&oa);
7551 oa.regname = eap->regname;
7552 oa.start.lnum = eap->line1;
7553 oa.end.lnum = eap->line2;
7554 oa.line_count = eap->line2 - eap->line1 + 1;
7555 oa.motion_type = MLINE;
7556#ifdef FEAT_VIRTUALEDIT
7557 virtual_op = FALSE;
7558#endif
7559 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7560 {
7561 setpcmark();
7562 curwin->w_cursor.lnum = eap->line1;
7563 beginline(BL_SOL | BL_FIX);
7564 }
7565
7566 switch (eap->cmdidx)
7567 {
7568 case CMD_delete:
7569 oa.op_type = OP_DELETE;
7570 op_delete(&oa);
7571 break;
7572
7573 case CMD_yank:
7574 oa.op_type = OP_YANK;
7575 (void)op_yank(&oa, FALSE, TRUE);
7576 break;
7577
7578 default: /* CMD_rshift or CMD_lshift */
7579 if ((eap->cmdidx == CMD_rshift)
7580#ifdef FEAT_RIGHTLEFT
7581 ^ curwin->w_p_rl
7582#endif
7583 )
7584 oa.op_type = OP_RSHIFT;
7585 else
7586 oa.op_type = OP_LSHIFT;
7587 op_shift(&oa, FALSE, eap->amount);
7588 break;
7589 }
7590#ifdef FEAT_VIRTUALEDIT
7591 virtual_op = MAYBE;
7592#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007593 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594}
7595
7596/*
7597 * ":put".
7598 */
7599 static void
7600ex_put(eap)
7601 exarg_T *eap;
7602{
7603 /* ":0put" works like ":1put!". */
7604 if (eap->line2 == 0)
7605 {
7606 eap->line2 = 1;
7607 eap->forceit = TRUE;
7608 }
7609 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007610 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7611 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612}
7613
7614/*
7615 * Handle ":copy" and ":move".
7616 */
7617 static void
7618ex_copymove(eap)
7619 exarg_T *eap;
7620{
7621 long n;
7622
7623 n = get_address(&eap->arg, FALSE, FALSE);
7624 if (eap->arg == NULL) /* error detected */
7625 {
7626 eap->nextcmd = NULL;
7627 return;
7628 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007629 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630
7631 /*
7632 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7633 */
7634 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7635 {
7636 EMSG(_(e_invaddr));
7637 return;
7638 }
7639
7640 if (eap->cmdidx == CMD_move)
7641 {
7642 if (do_move(eap->line1, eap->line2, n) == FAIL)
7643 return;
7644 }
7645 else
7646 ex_copy(eap->line1, eap->line2, n);
7647 u_clearline();
7648 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007649 ex_may_print(eap);
7650}
7651
7652/*
7653 * Print the current line if flags were given to the Ex command.
7654 */
7655 static void
7656ex_may_print(eap)
7657 exarg_T *eap;
7658{
7659 if (eap->flags != 0)
7660 {
7661 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7662 (eap->flags & EXFLAG_LIST));
7663 ex_no_reprint = TRUE;
7664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665}
7666
7667/*
7668 * ":smagic" and ":snomagic".
7669 */
7670 static void
7671ex_submagic(eap)
7672 exarg_T *eap;
7673{
7674 int magic_save = p_magic;
7675
7676 p_magic = (eap->cmdidx == CMD_smagic);
7677 do_sub(eap);
7678 p_magic = magic_save;
7679}
7680
7681/*
7682 * ":join".
7683 */
7684 static void
7685ex_join(eap)
7686 exarg_T *eap;
7687{
7688 curwin->w_cursor.lnum = eap->line1;
7689 if (eap->line1 == eap->line2)
7690 {
7691 if (eap->addr_count >= 2) /* :2,2join does nothing */
7692 return;
7693 if (eap->line2 == curbuf->b_ml.ml_line_count)
7694 {
7695 beep_flush();
7696 return;
7697 }
7698 ++eap->line2;
7699 }
7700 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
7701 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007702 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703}
7704
7705/*
7706 * ":[addr]@r" or ":[addr]*r": execute register
7707 */
7708 static void
7709ex_at(eap)
7710 exarg_T *eap;
7711{
7712 int c;
7713
7714 curwin->w_cursor.lnum = eap->line2;
7715
7716#ifdef USE_ON_FLY_SCROLL
7717 dont_scroll = TRUE; /* disallow scrolling here */
7718#endif
7719
7720 /* get the register name. No name means to use the previous one */
7721 c = *eap->arg;
7722 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7723 c = '@';
7724 /* put the register in mapbuf */
7725 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007726 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 else
7730 {
7731 int save_efr = exec_from_reg;
7732
7733 exec_from_reg = TRUE;
7734
7735 /*
7736 * Execute from the typeahead buffer.
7737 * Originally this didn't check for the typeahead buffer to be empty,
7738 * thus could read more Ex commands from stdin. It's not clear why,
7739 * it is certainly unexpected.
7740 */
7741 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
7742 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7743
7744 exec_from_reg = save_efr;
7745 }
7746}
7747
7748/*
7749 * ":!".
7750 */
7751 static void
7752ex_bang(eap)
7753 exarg_T *eap;
7754{
7755 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7756}
7757
7758/*
7759 * ":undo".
7760 */
7761/*ARGSUSED*/
7762 static void
7763ex_undo(eap)
7764 exarg_T *eap;
7765{
7766 u_undo(1);
7767}
7768
7769/*
7770 * ":redo".
7771 */
7772/*ARGSUSED*/
7773 static void
7774ex_redo(eap)
7775 exarg_T *eap;
7776{
7777 u_redo(1);
7778}
7779
7780/*
7781 * ":redir": start/stop redirection.
7782 */
7783 static void
7784ex_redir(eap)
7785 exarg_T *eap;
7786{
7787 char *mode;
7788 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007789 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790
7791 if (STRICMP(eap->arg, "END") == 0)
7792 close_redir();
7793 else
7794 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007795 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007797 ++arg;
7798 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007800 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 mode = "a";
7802 }
7803 else
7804 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007805 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806
7807 close_redir();
7808
7809 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00007810 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 if (fname == NULL)
7812 return;
7813#ifdef FEAT_BROWSE
7814 if (cmdmod.browse)
7815 {
7816 char_u *browseFile;
7817
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007818 browseFile = do_browse(BROWSE_SAVE,
7819 (char_u *)_("Save Redirection"),
7820 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 if (browseFile == NULL)
7822 return; /* operation cancelled */
7823 vim_free(fname);
7824 fname = browseFile;
7825 eap->forceit = TRUE; /* since dialog already asked */
7826 }
7827#endif
7828
7829 redir_fd = open_exfile(fname, eap->forceit, mode);
7830 vim_free(fname);
7831 }
7832#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007833 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 {
7835 /* redirect to a register a-z (resp. A-Z for appending) */
7836 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007837 ++arg;
7838 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007840 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007842 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007844 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00007845 if (*arg == '>' && arg[1] == '>')
7846 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007847 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
7848 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007850 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007852 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007854 if (*arg == '>')
7855 arg++;
7856
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 /* make register empty */
7858 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
7859 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007860 }
7861 if (*arg != NUL)
7862 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007863 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00007864 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007865 }
7866 }
7867 else if (*arg == '=' && arg[1] == '>')
7868 {
7869 int append;
7870
7871 /* redirect to a variable */
7872 close_redir();
7873 arg += 2;
7874
7875 if (*arg == '>')
7876 {
7877 ++arg;
7878 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 }
7880 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007881 append = FALSE;
7882
7883 if (var_redir_start(skipwhite(arg), append) == OK)
7884 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 }
7886#endif
7887
7888 /* TODO: redirect to a buffer */
7889
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 else
Bram Moolenaarca472992005-01-21 11:46:23 +00007891 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 }
7893}
7894
7895/*
7896 * ":redraw": force redraw
7897 */
7898 static void
7899ex_redraw(eap)
7900 exarg_T *eap;
7901{
7902 int r = RedrawingDisabled;
7903 int p = p_lz;
7904
7905 RedrawingDisabled = 0;
7906 p_lz = FALSE;
7907 update_topline();
7908 update_screen(eap->forceit ? CLEAR :
7909#ifdef FEAT_VISUAL
7910 VIsual_active ? INVERTED :
7911#endif
7912 0);
7913#ifdef FEAT_TITLE
7914 if (need_maketitle)
7915 maketitle();
7916#endif
7917 RedrawingDisabled = r;
7918 p_lz = p;
7919
7920 /* Reset msg_didout, so that a message that's there is overwritten. */
7921 msg_didout = FALSE;
7922 msg_col = 0;
7923
7924 out_flush();
7925}
7926
7927/*
7928 * ":redrawstatus": force redraw of status line(s)
7929 */
7930/*ARGSUSED*/
7931 static void
7932ex_redrawstatus(eap)
7933 exarg_T *eap;
7934{
7935#if defined(FEAT_WINDOWS)
7936 int r = RedrawingDisabled;
7937 int p = p_lz;
7938
7939 RedrawingDisabled = 0;
7940 p_lz = FALSE;
7941 if (eap->forceit)
7942 status_redraw_all();
7943 else
7944 status_redraw_curbuf();
7945 update_screen(
7946# ifdef FEAT_VISUAL
7947 VIsual_active ? INVERTED :
7948# endif
7949 0);
7950 RedrawingDisabled = r;
7951 p_lz = p;
7952 out_flush();
7953#endif
7954}
7955
7956 static void
7957close_redir()
7958{
7959 if (redir_fd != NULL)
7960 {
7961 fclose(redir_fd);
7962 redir_fd = NULL;
7963 }
7964#ifdef FEAT_EVAL
7965 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007966 if (redir_vname)
7967 {
7968 var_redir_stop();
7969 redir_vname = 0;
7970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971#endif
7972}
7973
7974#if defined(FEAT_SESSION) && defined(USE_CRNL)
7975# define MKSESSION_NL
7976static int mksession_nl = FALSE; /* use NL only in put_eol() */
7977#endif
7978
7979/*
7980 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
7981 */
7982 static void
7983ex_mkrc(eap)
7984 exarg_T *eap;
7985{
7986 FILE *fd;
7987 int failed = FALSE;
7988 char_u *fname;
7989#ifdef FEAT_BROWSE
7990 char_u *browseFile = NULL;
7991#endif
7992#ifdef FEAT_SESSION
7993 int view_session = FALSE;
7994 int using_vdir = FALSE; /* using 'viewdir'? */
7995 char_u *viewFile = NULL;
7996 unsigned *flagp;
7997#endif
7998
7999 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8000 {
8001#ifdef FEAT_SESSION
8002 view_session = TRUE;
8003#else
8004 ex_ni(eap);
8005 return;
8006#endif
8007 }
8008
8009#ifdef FEAT_SESSION
8010 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8011 if (eap->cmdidx == CMD_mkview
8012 && (*eap->arg == NUL
8013 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8014 {
8015 eap->forceit = TRUE;
8016 fname = get_view_file(*eap->arg);
8017 if (fname == NULL)
8018 return;
8019 viewFile = fname;
8020 using_vdir = TRUE;
8021 }
8022 else
8023#endif
8024 if (*eap->arg != NUL)
8025 fname = eap->arg;
8026 else if (eap->cmdidx == CMD_mkvimrc)
8027 fname = (char_u *)VIMRC_FILE;
8028#ifdef FEAT_SESSION
8029 else if (eap->cmdidx == CMD_mksession)
8030 fname = (char_u *)SESSION_FILE;
8031#endif
8032 else
8033 fname = (char_u *)EXRC_FILE;
8034
8035#ifdef FEAT_BROWSE
8036 if (cmdmod.browse)
8037 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008038 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039# ifdef FEAT_SESSION
8040 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8041 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8042# endif
8043 (char_u *)_("Save Setup"),
8044 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8045 if (browseFile == NULL)
8046 goto theend;
8047 fname = browseFile;
8048 eap->forceit = TRUE; /* since dialog already asked */
8049 }
8050#endif
8051
8052#if defined(FEAT_SESSION) && defined(vim_mkdir)
8053 /* When using 'viewdir' may have to create the directory. */
8054 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008055 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056#endif
8057
8058 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8059 if (fd != NULL)
8060 {
8061#ifdef FEAT_SESSION
8062 if (eap->cmdidx == CMD_mkview)
8063 flagp = &vop_flags;
8064 else
8065 flagp = &ssop_flags;
8066#endif
8067
8068#ifdef MKSESSION_NL
8069 /* "unix" in 'sessionoptions': use NL line separator */
8070 if (view_session && (*flagp & SSOP_UNIX))
8071 mksession_nl = TRUE;
8072#endif
8073
8074 /* Write the version command for :mkvimrc */
8075 if (eap->cmdidx == CMD_mkvimrc)
8076 (void)put_line(fd, "version 6.0");
8077
8078#ifdef FEAT_SESSION
8079 if (eap->cmdidx != CMD_mkview)
8080#endif
8081 {
8082 /* Write setting 'compatible' first, because it has side effects.
8083 * For that same reason only do it when needed. */
8084 if (p_cp)
8085 (void)put_line(fd, "if !&cp | set cp | endif");
8086 else
8087 (void)put_line(fd, "if &cp | set nocp | endif");
8088 }
8089
8090#ifdef FEAT_SESSION
8091 if (!view_session
8092 || (eap->cmdidx == CMD_mksession
8093 && (*flagp & SSOP_OPTIONS)))
8094#endif
8095 failed |= (makemap(fd, NULL) == FAIL
8096 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8097
8098#ifdef FEAT_SESSION
8099 if (!failed && view_session)
8100 {
8101 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8102 failed = TRUE;
8103 if (eap->cmdidx == CMD_mksession)
8104 {
8105 char_u dirnow[MAXPATHL]; /* current directory */
8106
8107 /*
8108 * Change to session file's dir.
8109 */
8110 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8111 || mch_chdir((char *)dirnow) != 0)
8112 *dirnow = NUL;
8113 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8114 {
8115 if (vim_chdirfile(fname) == OK)
8116 shorten_fnames(TRUE);
8117 }
8118 else if (*dirnow != NUL
8119 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8120 {
8121 (void)mch_chdir((char *)globaldir);
8122 shorten_fnames(TRUE);
8123 }
8124
8125 failed |= (makeopens(fd, dirnow) == FAIL);
8126
8127 /* restore original dir */
8128 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8129 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8130 {
8131 if (mch_chdir((char *)dirnow) != 0)
8132 EMSG(_(e_prev_dir));
8133 shorten_fnames(TRUE);
8134 }
8135 }
8136 else
8137 {
8138 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8139 }
8140 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8141 == FAIL)
8142 failed = TRUE;
8143 }
8144#endif
8145 failed |= fclose(fd);
8146
8147 if (failed)
8148 EMSG(_(e_write));
8149#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8150 else if (eap->cmdidx == CMD_mksession)
8151 {
8152 /* successful session write - set this_session var */
8153 char_u tbuf[MAXPATHL];
8154
8155 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8156 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8157 }
8158#endif
8159#ifdef MKSESSION_NL
8160 mksession_nl = FALSE;
8161#endif
8162 }
8163
8164#ifdef FEAT_BROWSE
8165theend:
8166 vim_free(browseFile);
8167#endif
8168#ifdef FEAT_SESSION
8169 vim_free(viewFile);
8170#endif
8171}
8172
Bram Moolenaardf177f62005-02-22 08:39:57 +00008173#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8174 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008175/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008176 int
8177vim_mkdir_emsg(name, prot)
8178 char_u *name;
8179 int prot;
8180{
8181 if (vim_mkdir(name, prot) != 0)
8182 {
8183 EMSG2(_("E739: Cannot create directory: %s"), name);
8184 return FAIL;
8185 }
8186 return OK;
8187}
8188#endif
8189
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190/*
8191 * Open a file for writing for an Ex command, with some checks.
8192 * Return file descriptor, or NULL on failure.
8193 */
8194 FILE *
8195open_exfile(fname, forceit, mode)
8196 char_u *fname;
8197 int forceit;
8198 char *mode; /* "w" for create new file or "a" for append */
8199{
8200 FILE *fd;
8201
8202#ifdef UNIX
8203 /* with Unix it is possible to open a directory */
8204 if (mch_isdir(fname))
8205 {
8206 EMSG2(_(e_isadir2), fname);
8207 return NULL;
8208 }
8209#endif
8210 if (!forceit && *mode != 'a' && vim_fexists(fname))
8211 {
8212 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8213 return NULL;
8214 }
8215
8216 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8217 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8218
8219 return fd;
8220}
8221
8222/*
8223 * ":mark" and ":k".
8224 */
8225 static void
8226ex_mark(eap)
8227 exarg_T *eap;
8228{
8229 pos_T pos;
8230
8231 if (*eap->arg == NUL) /* No argument? */
8232 EMSG(_(e_argreq));
8233 else if (eap->arg[1] != NUL) /* more than one character? */
8234 EMSG(_(e_trailing));
8235 else
8236 {
8237 pos = curwin->w_cursor; /* save curwin->w_cursor */
8238 curwin->w_cursor.lnum = eap->line2;
8239 beginline(BL_WHITE | BL_FIX);
8240 if (setmark(*eap->arg) == FAIL) /* set mark */
8241 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8242 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8243 }
8244}
8245
8246/*
8247 * Update w_topline, w_leftcol and the cursor position.
8248 */
8249 void
8250update_topline_cursor()
8251{
8252 check_cursor(); /* put cursor on valid line */
8253 update_topline();
8254 if (!curwin->w_p_wrap)
8255 validate_cursor();
8256 update_curswant();
8257}
8258
8259#ifdef FEAT_EX_EXTRA
8260/*
8261 * ":normal[!] {commands}": Execute normal mode commands.
8262 */
8263 static void
8264ex_normal(eap)
8265 exarg_T *eap;
8266{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 int save_msg_scroll = msg_scroll;
8268 int save_restart_edit = restart_edit;
8269 int save_msg_didout = msg_didout;
8270 int save_State = State;
8271 tasave_T tabuf;
8272 int save_insertmode = p_im;
8273 int save_finish_op = finish_op;
8274#ifdef FEAT_MBYTE
8275 char_u *arg = NULL;
8276 int l;
8277 char_u *p;
8278#endif
8279
8280 if (ex_normal_busy >= p_mmd)
8281 {
8282 EMSG(_("E192: Recursive use of :normal too deep"));
8283 return;
8284 }
8285 ++ex_normal_busy;
8286
8287 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8288 restart_edit = 0; /* don't go to Insert mode */
8289 p_im = FALSE; /* don't use 'insertmode' */
8290
8291#ifdef FEAT_MBYTE
8292 /*
8293 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8294 * this for the K_SPECIAL leading byte, otherwise special keys will not
8295 * work.
8296 */
8297 if (has_mbyte)
8298 {
8299 int len = 0;
8300
8301 /* Count the number of characters to be escaped. */
8302 for (p = eap->arg; *p != NUL; ++p)
8303 {
8304# ifdef FEAT_GUI
8305 if (*p == CSI) /* leadbyte CSI */
8306 len += 2;
8307# endif
8308 for (l = (*mb_ptr2len_check)(p) - 1; l > 0; --l)
8309 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8310# ifdef FEAT_GUI
8311 || *p == CSI
8312# endif
8313 )
8314 len += 2;
8315 }
8316 if (len > 0)
8317 {
8318 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8319 if (arg != NULL)
8320 {
8321 len = 0;
8322 for (p = eap->arg; *p != NUL; ++p)
8323 {
8324 arg[len++] = *p;
8325# ifdef FEAT_GUI
8326 if (*p == CSI)
8327 {
8328 arg[len++] = KS_EXTRA;
8329 arg[len++] = (int)KE_CSI;
8330 }
8331# endif
8332 for (l = (*mb_ptr2len_check)(p) - 1; l > 0; --l)
8333 {
8334 arg[len++] = *++p;
8335 if (*p == K_SPECIAL)
8336 {
8337 arg[len++] = KS_SPECIAL;
8338 arg[len++] = KE_FILLER;
8339 }
8340# ifdef FEAT_GUI
8341 else if (*p == CSI)
8342 {
8343 arg[len++] = KS_EXTRA;
8344 arg[len++] = (int)KE_CSI;
8345 }
8346# endif
8347 }
8348 arg[len] = NUL;
8349 }
8350 }
8351 }
8352 }
8353#endif
8354
8355 /*
8356 * Save the current typeahead. This is required to allow using ":normal"
8357 * from an event handler and makes sure we don't hang when the argument
8358 * ends with half a command.
8359 */
8360 save_typeahead(&tabuf);
8361 if (tabuf.typebuf_valid)
8362 {
8363 /*
8364 * Repeat the :normal command for each line in the range. When no
8365 * range given, execute it just once, without positioning the cursor
8366 * first.
8367 */
8368 do
8369 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 if (eap->addr_count != 0)
8371 {
8372 curwin->w_cursor.lnum = eap->line1++;
8373 curwin->w_cursor.col = 0;
8374 }
8375
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008376 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377#ifdef FEAT_MBYTE
8378 arg != NULL ? arg :
8379#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008380 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 }
8382 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8383 }
8384
8385 /* Might not return to the main loop when in an event handler. */
8386 update_topline_cursor();
8387
8388 /* Restore the previous typeahead. */
8389 restore_typeahead(&tabuf);
8390
8391 --ex_normal_busy;
8392 msg_scroll = save_msg_scroll;
8393 restart_edit = save_restart_edit;
8394 p_im = save_insertmode;
8395 finish_op = save_finish_op;
8396 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8397
8398 /* Restore the state (needed when called from a function executed for
8399 * 'indentexpr'). */
8400 State = save_State;
8401#ifdef FEAT_MBYTE
8402 vim_free(arg);
8403#endif
8404}
8405
8406/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008407 * ":startinsert" and ":startreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 */
8409 static void
8410ex_startinsert(eap)
8411 exarg_T *eap;
8412{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008413 if (eap->forceit)
8414 {
8415 coladvance((colnr_T)MAXCOL);
8416 curwin->w_curswant = MAXCOL;
8417 curwin->w_set_curswant = FALSE;
8418 }
8419
Bram Moolenaara40c5002005-01-09 21:16:21 +00008420 /* Ignore the command when already in Insert mode. Inserting an
8421 * expression register that invokes a function can do this. */
8422 if (State & INSERT)
8423 return;
8424
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 if (eap->forceit)
8426 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008427 if (eap->cmdidx == CMD_startinsert)
8428 restart_edit = 'a';
8429 else
8430 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 }
8432 else
8433 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008434 if (eap->cmdidx == CMD_startinsert)
8435 restart_edit = 'i';
8436 else
8437 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 curwin->w_curswant = 0; /* avoid MAXCOL */
8439 }
8440}
8441
8442/*
8443 * ":stopinsert"
8444 */
8445/*ARGSUSED*/
8446 static void
8447ex_stopinsert(eap)
8448 exarg_T *eap;
8449{
8450 restart_edit = 0;
8451 stop_insert_mode = TRUE;
8452}
8453#endif
8454
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008455#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8456/*
8457 * Execute normal mode command "cmd".
8458 * "remap" can be REMAP_NONE or REMAP_YES.
8459 */
8460 void
8461exec_normal_cmd(cmd, remap, silent)
8462 char_u *cmd;
8463 int remap;
8464 int silent;
8465{
8466 oparg_T oa;
8467
8468 /*
8469 * Stuff the argument into the typeahead buffer.
8470 * Execute normal_cmd() until there is no typeahead left.
8471 */
8472 clear_oparg(&oa);
8473 finish_op = FALSE;
8474 ins_typebuf(cmd, remap, 0, TRUE, silent);
8475 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8476 && !got_int)
8477 {
8478 update_topline_cursor();
8479 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8480 }
8481}
8482#endif
8483
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484#ifdef FEAT_FIND_ID
8485 static void
8486ex_checkpath(eap)
8487 exarg_T *eap;
8488{
8489 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8490 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8491 (linenr_T)1, (linenr_T)MAXLNUM);
8492}
8493
8494#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8495/*
8496 * ":psearch"
8497 */
8498 static void
8499ex_psearch(eap)
8500 exarg_T *eap;
8501{
8502 g_do_tagpreview = p_pvh;
8503 ex_findpat(eap);
8504 g_do_tagpreview = 0;
8505}
8506#endif
8507
8508 static void
8509ex_findpat(eap)
8510 exarg_T *eap;
8511{
8512 int whole = TRUE;
8513 long n;
8514 char_u *p;
8515 int action;
8516
8517 switch (cmdnames[eap->cmdidx].cmd_name[2])
8518 {
8519 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8520 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8521 action = ACTION_GOTO;
8522 else
8523 action = ACTION_SHOW;
8524 break;
8525 case 'i': /* ":ilist" and ":dlist" */
8526 action = ACTION_SHOW_ALL;
8527 break;
8528 case 'u': /* ":ijump" and ":djump" */
8529 action = ACTION_GOTO;
8530 break;
8531 default: /* ":isplit" and ":dsplit" */
8532 action = ACTION_SPLIT;
8533 break;
8534 }
8535
8536 n = 1;
8537 if (vim_isdigit(*eap->arg)) /* get count */
8538 {
8539 n = getdigits(&eap->arg);
8540 eap->arg = skipwhite(eap->arg);
8541 }
8542 if (*eap->arg == '/') /* Match regexp, not just whole words */
8543 {
8544 whole = FALSE;
8545 ++eap->arg;
8546 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8547 if (*p)
8548 {
8549 *p++ = NUL;
8550 p = skipwhite(p);
8551
8552 /* Check for trailing illegal characters */
8553 if (!ends_excmd(*p))
8554 eap->errmsg = e_trailing;
8555 else
8556 eap->nextcmd = check_nextcmd(p);
8557 }
8558 }
8559 if (!eap->skip)
8560 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8561 whole, !eap->forceit,
8562 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8563 n, action, eap->line1, eap->line2);
8564}
8565#endif
8566
8567#ifdef FEAT_WINDOWS
8568
8569# ifdef FEAT_QUICKFIX
8570/*
8571 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8572 */
8573 static void
8574ex_ptag(eap)
8575 exarg_T *eap;
8576{
8577 g_do_tagpreview = p_pvh;
8578 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8579}
8580
8581/*
8582 * ":pedit"
8583 */
8584 static void
8585ex_pedit(eap)
8586 exarg_T *eap;
8587{
8588 win_T *curwin_save = curwin;
8589
8590 g_do_tagpreview = p_pvh;
8591 prepare_tagpreview();
8592 keep_help_flag = curwin_save->w_buffer->b_help;
8593 do_exedit(eap, NULL);
8594 keep_help_flag = FALSE;
8595 if (curwin != curwin_save && win_valid(curwin_save))
8596 {
8597 /* Return cursor to where we were */
8598 validate_cursor();
8599 redraw_later(VALID);
8600 win_enter(curwin_save, TRUE);
8601 }
8602 g_do_tagpreview = 0;
8603}
8604# endif
8605
8606/*
8607 * ":stag", ":stselect" and ":stjump".
8608 */
8609 static void
8610ex_stag(eap)
8611 exarg_T *eap;
8612{
8613 postponed_split = -1;
8614 postponed_split_flags = cmdmod.split;
8615 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8616 postponed_split_flags = 0;
8617}
8618#endif
8619
8620/*
8621 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8622 */
8623 static void
8624ex_tag(eap)
8625 exarg_T *eap;
8626{
8627 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8628}
8629
8630 static void
8631ex_tag_cmd(eap, name)
8632 exarg_T *eap;
8633 char_u *name;
8634{
8635 int cmd;
8636
8637 switch (name[1])
8638 {
8639 case 'j': cmd = DT_JUMP; /* ":tjump" */
8640 break;
8641 case 's': cmd = DT_SELECT; /* ":tselect" */
8642 break;
8643 case 'p': cmd = DT_PREV; /* ":tprevious" */
8644 break;
8645 case 'N': cmd = DT_PREV; /* ":tNext" */
8646 break;
8647 case 'n': cmd = DT_NEXT; /* ":tnext" */
8648 break;
8649 case 'o': cmd = DT_POP; /* ":pop" */
8650 break;
8651 case 'f': /* ":tfirst" */
8652 case 'r': cmd = DT_FIRST; /* ":trewind" */
8653 break;
8654 case 'l': cmd = DT_LAST; /* ":tlast" */
8655 break;
8656 default: /* ":tag" */
8657#ifdef FEAT_CSCOPE
8658 if (p_cst)
8659 {
8660 do_cstag(eap);
8661 return;
8662 }
8663#endif
8664 cmd = DT_TAG;
8665 break;
8666 }
8667
8668 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8669 eap->forceit, TRUE);
8670}
8671
8672/*
8673 * Evaluate cmdline variables.
8674 *
8675 * change '%' to curbuf->b_ffname
8676 * '#' to curwin->w_altfile
8677 * '<cword>' to word under the cursor
8678 * '<cWORD>' to WORD under the cursor
8679 * '<cfile>' to path name under the cursor
8680 * '<sfile>' to sourced file name
8681 * '<afile>' to file name for autocommand
8682 * '<abuf>' to buffer number for autocommand
8683 * '<amatch>' to matching name for autocommand
8684 *
8685 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8686 * "" for error without a message) and NULL is returned.
8687 * Returns an allocated string if a valid match was found.
8688 * Returns NULL if no match was found. "usedlen" then still contains the
8689 * number of characters to skip.
8690 */
8691 char_u *
8692eval_vars(src, usedlen, lnump, errormsg, srcstart)
8693 char_u *src; /* pointer into commandline */
8694 int *usedlen; /* characters after src that are used */
8695 linenr_T *lnump; /* line number for :e command, or NULL */
8696 char_u **errormsg; /* pointer to error message */
8697 char_u *srcstart; /* beginning of valid memory for src */
8698{
8699 int i;
8700 char_u *s;
8701 char_u *result;
8702 char_u *resultbuf = NULL;
8703 int resultlen;
8704 buf_T *buf;
8705 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
8706 int spec_idx;
8707#ifdef FEAT_MODIFY_FNAME
8708 int skip_mod = FALSE;
8709#endif
8710 static char *(spec_str[]) =
8711 {
8712 "%",
8713#define SPEC_PERC 0
8714 "#",
8715#define SPEC_HASH 1
8716 "<cword>", /* cursor word */
8717#define SPEC_CWORD 2
8718 "<cWORD>", /* cursor WORD */
8719#define SPEC_CCWORD 3
8720 "<cfile>", /* cursor path name */
8721#define SPEC_CFILE 4
8722 "<sfile>", /* ":so" file name */
8723#define SPEC_SFILE 5
8724#ifdef FEAT_AUTOCMD
8725 "<afile>", /* autocommand file name */
8726# define SPEC_AFILE 6
8727 "<abuf>", /* autocommand buffer number */
8728# define SPEC_ABUF 7
8729 "<amatch>", /* autocommand match name */
8730# define SPEC_AMATCH 8
8731#endif
8732#ifdef FEAT_CLIENTSERVER
8733 "<client>"
8734# define SPEC_CLIENT 9
8735#endif
8736 };
8737#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
8738
8739#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
8740 char_u strbuf[30];
8741#endif
8742
8743 *errormsg = NULL;
8744
8745 /*
8746 * Check if there is something to do.
8747 */
8748 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
8749 {
8750 *usedlen = (int)STRLEN(spec_str[spec_idx]);
8751 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
8752 break;
8753 }
8754 if (spec_idx == SPEC_COUNT) /* no match */
8755 {
8756 *usedlen = 1;
8757 return NULL;
8758 }
8759
8760 /*
8761 * Skip when preceded with a backslash "\%" and "\#".
8762 * Note: In "\\%" the % is also not recognized!
8763 */
8764 if (src > srcstart && src[-1] == '\\')
8765 {
8766 *usedlen = 0;
8767 STRCPY(src - 1, src); /* remove backslash */
8768 return NULL;
8769 }
8770
8771 /*
8772 * word or WORD under cursor
8773 */
8774 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
8775 {
8776 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
8777 (FIND_IDENT|FIND_STRING) : FIND_STRING);
8778 if (resultlen == 0)
8779 {
8780 *errormsg = (char_u *)"";
8781 return NULL;
8782 }
8783 }
8784
8785 /*
8786 * '#': Alternate file name
8787 * '%': Current file name
8788 * File name under the cursor
8789 * File name for autocommand
8790 * and following modifiers
8791 */
8792 else
8793 {
8794 switch (spec_idx)
8795 {
8796 case SPEC_PERC: /* '%': current file */
8797 if (curbuf->b_fname == NULL)
8798 {
8799 result = (char_u *)"";
8800 valid = 0; /* Must have ":p:h" to be valid */
8801 }
8802 else
8803#ifdef RISCOS
8804 /* Always use the full path for RISC OS if possible. */
8805 result = curbuf->b_ffname;
8806 if (result == NULL)
8807 result = curbuf->b_fname;
8808#else
8809 result = curbuf->b_fname;
8810#endif
8811 break;
8812
8813 case SPEC_HASH: /* '#' or "#99": alternate file */
8814 if (src[1] == '#') /* "##": the argument list */
8815 {
8816 result = arg_all();
8817 resultbuf = result;
8818 *usedlen = 2;
8819#ifdef FEAT_MODIFY_FNAME
8820 skip_mod = TRUE;
8821#endif
8822 break;
8823 }
8824 s = src + 1;
8825 i = (int)getdigits(&s);
8826 *usedlen = (int)(s - src); /* length of what we expand */
8827
8828 buf = buflist_findnr(i);
8829 if (buf == NULL)
8830 {
8831 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
8832 return NULL;
8833 }
8834 if (lnump != NULL)
8835 *lnump = ECMD_LAST;
8836 if (buf->b_fname == NULL)
8837 {
8838 result = (char_u *)"";
8839 valid = 0; /* Must have ":p:h" to be valid */
8840 }
8841 else
8842 result = buf->b_fname;
8843 break;
8844
8845#ifdef FEAT_SEARCHPATH
8846 case SPEC_CFILE: /* file name under cursor */
8847 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L);
8848 if (result == NULL)
8849 {
8850 *errormsg = (char_u *)"";
8851 return NULL;
8852 }
8853 resultbuf = result; /* remember allocated string */
8854 break;
8855#endif
8856
8857#ifdef FEAT_AUTOCMD
8858 case SPEC_AFILE: /* file name for autocommand */
8859 result = autocmd_fname;
8860 if (result == NULL)
8861 {
8862 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
8863 return NULL;
8864 }
8865 break;
8866
8867 case SPEC_ABUF: /* buffer number for autocommand */
8868 if (autocmd_bufnr <= 0)
8869 {
8870 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
8871 return NULL;
8872 }
8873 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8874 result = strbuf;
8875 break;
8876
8877 case SPEC_AMATCH: /* match name for autocommand */
8878 result = autocmd_match;
8879 if (result == NULL)
8880 {
8881 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
8882 return NULL;
8883 }
8884 break;
8885
8886#endif
8887 case SPEC_SFILE: /* file name for ":so" command */
8888 result = sourcing_name;
8889 if (result == NULL)
8890 {
8891 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
8892 return NULL;
8893 }
8894 break;
8895#if defined(FEAT_CLIENTSERVER)
8896 case SPEC_CLIENT: /* Source of last submitted input */
8897 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
8898 result = strbuf;
8899 break;
8900#endif
8901 }
8902
8903 resultlen = (int)STRLEN(result); /* length of new string */
8904 if (src[*usedlen] == '<') /* remove the file name extension */
8905 {
8906 ++*usedlen;
8907#ifdef RISCOS
8908 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
8909#else
8910 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
8911#endif
8912 resultlen = (int)(s - result);
8913 }
8914#ifdef FEAT_MODIFY_FNAME
8915 else if (!skip_mod)
8916 {
8917 valid |= modify_fname(src, usedlen, &result, &resultbuf,
8918 &resultlen);
8919 if (result == NULL)
8920 {
8921 *errormsg = (char_u *)"";
8922 return NULL;
8923 }
8924 }
8925#endif
8926 }
8927
8928 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8929 {
8930 if (valid != VALID_HEAD + VALID_PATH)
8931 /* xgettext:no-c-format */
8932 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
8933 else
8934 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
8935 result = NULL;
8936 }
8937 else
8938 result = vim_strnsave(result, resultlen);
8939 vim_free(resultbuf);
8940 return result;
8941}
8942
8943/*
8944 * Concatenate all files in the argument list, separated by spaces, and return
8945 * it in one allocated string.
8946 * Spaces and backslashes in the file names are escaped with a backslash.
8947 * Returns NULL when out of memory.
8948 */
8949 static char_u *
8950arg_all()
8951{
8952 int len;
8953 int idx;
8954 char_u *retval = NULL;
8955 char_u *p;
8956
8957 /*
8958 * Do this loop two times:
8959 * first time: compute the total length
8960 * second time: concatenate the names
8961 */
8962 for (;;)
8963 {
8964 len = 0;
8965 for (idx = 0; idx < ARGCOUNT; ++idx)
8966 {
8967 p = alist_name(&ARGLIST[idx]);
8968 if (p != NULL)
8969 {
8970 if (len > 0)
8971 {
8972 /* insert a space in between names */
8973 if (retval != NULL)
8974 retval[len] = ' ';
8975 ++len;
8976 }
8977 for ( ; *p != NUL; ++p)
8978 {
8979 if (*p == ' ' || *p == '\\')
8980 {
8981 /* insert a backslash */
8982 if (retval != NULL)
8983 retval[len] = '\\';
8984 ++len;
8985 }
8986 if (retval != NULL)
8987 retval[len] = *p;
8988 ++len;
8989 }
8990 }
8991 }
8992
8993 /* second time: break here */
8994 if (retval != NULL)
8995 {
8996 retval[len] = NUL;
8997 break;
8998 }
8999
9000 /* allocate memory */
9001 retval = alloc(len + 1);
9002 if (retval == NULL)
9003 break;
9004 }
9005
9006 return retval;
9007}
9008
9009#if defined(FEAT_AUTOCMD) || defined(PROTO)
9010/*
9011 * Expand the <sfile> string in "arg".
9012 *
9013 * Returns an allocated string, or NULL for any error.
9014 */
9015 char_u *
9016expand_sfile(arg)
9017 char_u *arg;
9018{
9019 char_u *errormsg;
9020 int len;
9021 char_u *result;
9022 char_u *newres;
9023 char_u *repl;
9024 int srclen;
9025 char_u *p;
9026
9027 result = vim_strsave(arg);
9028 if (result == NULL)
9029 return NULL;
9030
9031 for (p = result; *p; )
9032 {
9033 if (STRNCMP(p, "<sfile>", 7) != 0)
9034 ++p;
9035 else
9036 {
9037 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9038 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9039 if (errormsg != NULL)
9040 {
9041 if (*errormsg)
9042 emsg(errormsg);
9043 vim_free(result);
9044 return NULL;
9045 }
9046 if (repl == NULL) /* no match (cannot happen) */
9047 {
9048 p += srclen;
9049 continue;
9050 }
9051 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9052 newres = alloc(len);
9053 if (newres == NULL)
9054 {
9055 vim_free(repl);
9056 vim_free(result);
9057 return NULL;
9058 }
9059 mch_memmove(newres, result, (size_t)(p - result));
9060 STRCPY(newres + (p - result), repl);
9061 len = (int)STRLEN(newres);
9062 STRCAT(newres, p + srclen);
9063 vim_free(repl);
9064 vim_free(result);
9065 result = newres;
9066 p = newres + len; /* continue after the match */
9067 }
9068 }
9069
9070 return result;
9071}
9072#endif
9073
9074#ifdef FEAT_SESSION
9075static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9076static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9077static frame_T *ses_skipframe __ARGS((frame_T *fr));
9078static int ses_do_frame __ARGS((frame_T *fr));
9079static int ses_do_win __ARGS((win_T *wp));
9080static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9081static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9082static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9083
9084/*
9085 * Write openfile commands for the current buffers to an .exrc file.
9086 * Return FAIL on error, OK otherwise.
9087 */
9088 static int
9089makeopens(fd, dirnow)
9090 FILE *fd;
9091 char_u *dirnow; /* Current directory name */
9092{
9093 buf_T *buf;
9094 int only_save_windows = TRUE;
9095 int nr;
9096 int cnr = 1;
9097 int restore_size = TRUE;
9098 win_T *wp;
9099 char_u *sname;
9100 win_T *edited_win = NULL;
9101
9102 if (ssop_flags & SSOP_BUFFERS)
9103 only_save_windows = FALSE; /* Save ALL buffers */
9104
9105 /*
9106 * Begin by setting the this_session variable, and then other
9107 * sessionable variables.
9108 */
9109#ifdef FEAT_EVAL
9110 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9111 return FAIL;
9112 if (ssop_flags & SSOP_GLOBALS)
9113 if (store_session_globals(fd) == FAIL)
9114 return FAIL;
9115#endif
9116
9117 /*
9118 * Close all windows but one.
9119 */
9120 if (put_line(fd, "silent only") == FAIL)
9121 return FAIL;
9122
9123 /*
9124 * Now a :cd command to the session directory or the current directory
9125 */
9126 if (ssop_flags & SSOP_SESDIR)
9127 {
9128 if (put_line(fd, "exe \"cd \" . expand(\"<sfile>:p:h\")") == FAIL)
9129 return FAIL;
9130 }
9131 else if (ssop_flags & SSOP_CURDIR)
9132 {
9133 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9134 if (sname == NULL
9135 || fprintf(fd, "cd %s", sname) < 0 || put_eol(fd) == FAIL)
9136 return FAIL;
9137 vim_free(sname);
9138 }
9139
9140 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009141 * If there is an empty, unnamed buffer we will wipe it out later.
9142 * Remember the buffer number.
9143 */
9144 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9145 return FAIL;
9146 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9147 return FAIL;
9148 if (put_line(fd, "endif") == FAIL)
9149 return FAIL;
9150
9151 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 * Now save the current files, current buffer first.
9153 */
9154 if (put_line(fd, "set shortmess=aoO") == FAIL)
9155 return FAIL;
9156
9157 /* Now put the other buffers into the buffer list */
9158 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9159 {
9160 if (!(only_save_windows && buf->b_nwindows == 0)
9161 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9162 && buf->b_fname != NULL
9163 && buf->b_p_bl)
9164 {
9165 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9166 : buf->b_wininfo->wi_fpos.lnum) < 0
9167 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9168 return FAIL;
9169 }
9170 }
9171
9172 /* the global argument list */
9173 if (ses_arglist(fd, "args", &global_alist.al_ga,
9174 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9175 return FAIL;
9176
9177 if (ssop_flags & SSOP_RESIZE)
9178 {
9179 /* Note: after the restore we still check it worked!*/
9180 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9181 || put_eol(fd) == FAIL)
9182 return FAIL;
9183 }
9184
9185#ifdef FEAT_GUI
9186 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9187 {
9188 int x, y;
9189
9190 if (gui_mch_get_winpos(&x, &y) == OK)
9191 {
9192 /* Note: after the restore we still check it worked!*/
9193 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9194 return FAIL;
9195 }
9196 }
9197#endif
9198
9199 /*
9200 * Before creating the window layout, try loading one file. If this is
9201 * aborted we don't end up with a number of useless windows.
9202 * This may have side effects! (e.g., compressed or network file).
9203 */
9204 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9205 {
9206 if (ses_do_win(wp)
9207 && wp->w_buffer->b_ffname != NULL
9208 && !wp->w_buffer->b_help
9209#ifdef FEAT_QUICKFIX
9210 && !bt_nofile(wp->w_buffer)
9211#endif
9212 )
9213 {
9214 if (fputs("edit ", fd) < 0
9215 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9216 return FAIL;
9217 if (!wp->w_arg_idx_invalid)
9218 edited_win = wp;
9219 break;
9220 }
9221 }
9222
9223 /*
9224 * Save current window layout.
9225 */
9226 if (put_line(fd, "set splitbelow splitright") == FAIL)
9227 return FAIL;
9228 if (ses_win_rec(fd, topframe) == FAIL)
9229 return FAIL;
9230 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9231 return FAIL;
9232 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9233 return FAIL;
9234
9235 /*
9236 * Check if window sizes can be restored (no windows omitted).
9237 * Remember the window number of the current window after restoring.
9238 */
9239 nr = 0;
9240 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9241 {
9242 if (ses_do_win(wp))
9243 ++nr;
9244 else
9245 restore_size = FALSE;
9246 if (curwin == wp)
9247 cnr = nr;
9248 }
9249
9250 /* Go to the first window. */
9251 if (put_line(fd, "wincmd t") == FAIL)
9252 return FAIL;
9253
9254 /*
9255 * If more than one window, see if sizes can be restored.
9256 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9257 * resized when moving between windows.
9258 * Do this before restoring the view, so that the topline and the cursor
9259 * can be set. This is done again below.
9260 */
9261 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9262 return FAIL;
9263 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9264 return FAIL;
9265
9266 /*
9267 * Restore the view of the window (options, file, cursor, etc.).
9268 */
9269 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9270 {
9271 if (!ses_do_win(wp))
9272 continue;
9273 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9274 return FAIL;
9275 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9276 return FAIL;
9277 }
9278
9279 /*
9280 * Restore cursor to the current window if it's not the first one.
9281 */
9282 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9283 return FAIL;
9284
9285 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009286 * Wipe out an empty unnamed buffer we started in.
9287 */
9288 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9289 return FAIL;
9290 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9291 return FAIL;
9292 if (put_line(fd, "endif") == FAIL)
9293 return FAIL;
9294 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9295 return FAIL;
9296
9297 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 * Restore window sizes again after jumping around in windows, because the
9299 * current window has a minimum size while others may not.
9300 */
9301 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9302 return FAIL;
9303
9304 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9305 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9306 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9307 return FAIL;
9308
9309 /*
9310 * Lastly, execute the x.vim file if it exists.
9311 */
9312 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9313 || put_line(fd, "if file_readable(s:sx)") == FAIL
9314 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9315 || put_line(fd, "endif") == FAIL)
9316 return FAIL;
9317
9318 return OK;
9319}
9320
9321 static int
9322ses_winsizes(fd, restore_size)
9323 FILE *fd;
9324 int restore_size;
9325{
9326 int n = 0;
9327 win_T *wp;
9328
9329 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9330 {
9331 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9332 {
9333 if (!ses_do_win(wp))
9334 continue;
9335 ++n;
9336
9337 /* restore height when not full height */
9338 if (wp->w_height + wp->w_status_height < topframe->fr_height
9339 && (fprintf(fd,
9340 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9341 n, (long)wp->w_height, Rows / 2, Rows) < 0
9342 || put_eol(fd) == FAIL))
9343 return FAIL;
9344
9345 /* restore width when not full width */
9346 if (wp->w_width < Columns && (fprintf(fd,
9347 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9348 n, (long)wp->w_width, Columns / 2, Columns) < 0
9349 || put_eol(fd) == FAIL))
9350 return FAIL;
9351 }
9352 }
9353 else
9354 {
9355 /* Just equalise window sizes */
9356 if (put_line(fd, "wincmd =") == FAIL)
9357 return FAIL;
9358 }
9359 return OK;
9360}
9361
9362/*
9363 * Write commands to "fd" to recursively create windows for frame "fr",
9364 * horizontally and vertically split.
9365 * After the commands the last window in the frame is the current window.
9366 * Returns FAIL when writing the commands to "fd" fails.
9367 */
9368 static int
9369ses_win_rec(fd, fr)
9370 FILE *fd;
9371 frame_T *fr;
9372{
9373 frame_T *frc;
9374 int count = 0;
9375
9376 if (fr->fr_layout != FR_LEAF)
9377 {
9378 /* Find first frame that's not skipped and then create a window for
9379 * each following one (first frame is already there). */
9380 frc = ses_skipframe(fr->fr_child);
9381 if (frc != NULL)
9382 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9383 {
9384 /* Make window as big as possible so that we have lots of room
9385 * to split. */
9386 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9387 || put_line(fd, fr->fr_layout == FR_COL
9388 ? "split" : "vsplit") == FAIL)
9389 return FAIL;
9390 ++count;
9391 }
9392
9393 /* Go back to the first window. */
9394 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9395 ? "%dwincmd k" : "%dwincmd h", count) < 0
9396 || put_eol(fd) == FAIL))
9397 return FAIL;
9398
9399 /* Recursively create frames/windows in each window of this column or
9400 * row. */
9401 frc = ses_skipframe(fr->fr_child);
9402 while (frc != NULL)
9403 {
9404 ses_win_rec(fd, frc);
9405 frc = ses_skipframe(frc->fr_next);
9406 /* Go to next window. */
9407 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9408 return FAIL;
9409 }
9410 }
9411 return OK;
9412}
9413
9414/*
9415 * Skip frames that don't contain windows we want to save in the Session.
9416 * Returns NULL when there none.
9417 */
9418 static frame_T *
9419ses_skipframe(fr)
9420 frame_T *fr;
9421{
9422 frame_T *frc;
9423
9424 for (frc = fr; frc != NULL; frc = frc->fr_next)
9425 if (ses_do_frame(frc))
9426 break;
9427 return frc;
9428}
9429
9430/*
9431 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9432 * the Session.
9433 */
9434 static int
9435ses_do_frame(fr)
9436 frame_T *fr;
9437{
9438 frame_T *frc;
9439
9440 if (fr->fr_layout == FR_LEAF)
9441 return ses_do_win(fr->fr_win);
9442 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9443 if (ses_do_frame(frc))
9444 return TRUE;
9445 return FALSE;
9446}
9447
9448/*
9449 * Return non-zero if window "wp" is to be stored in the Session.
9450 */
9451 static int
9452ses_do_win(wp)
9453 win_T *wp;
9454{
9455 if (wp->w_buffer->b_fname == NULL
9456#ifdef FEAT_QUICKFIX
9457 /* When 'buftype' is "nofile" can't restore the window contents. */
9458 || bt_nofile(wp->w_buffer)
9459#endif
9460 )
9461 return (ssop_flags & SSOP_BLANK);
9462 if (wp->w_buffer->b_help)
9463 return (ssop_flags & SSOP_HELP);
9464 return TRUE;
9465}
9466
9467/*
9468 * Write commands to "fd" to restore the view of a window.
9469 * Caller must make sure 'scrolloff' is zero.
9470 */
9471 static int
9472put_view(fd, wp, add_edit, flagp)
9473 FILE *fd;
9474 win_T *wp;
9475 int add_edit; /* add ":edit" command to view */
9476 unsigned *flagp; /* vop_flags or ssop_flags */
9477{
9478 win_T *save_curwin;
9479 int f;
9480 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009481 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482
9483 /* Always restore cursor position for ":mksession". For ":mkview" only
9484 * when 'viewoptions' contains "cursor". */
9485 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9486
9487 /*
9488 * Local argument list.
9489 */
9490 if (wp->w_alist == &global_alist)
9491 {
9492 if (put_line(fd, "argglobal") == FAIL)
9493 return FAIL;
9494 }
9495 else
9496 {
9497 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9498 flagp == &vop_flags
9499 || !(*flagp & SSOP_CURDIR)
9500 || wp->w_localdir != NULL, flagp) == FAIL)
9501 return FAIL;
9502 }
9503
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009504 /* Only when part of a session: restore the argument index. Some
9505 * arguments may have been deleted, check if the index is valid. */
9506 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
9507 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 {
9509 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9510 || put_eol(fd) == FAIL)
9511 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009512 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 }
9514
9515 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009516 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 {
9518 /*
9519 * Load the file.
9520 */
9521 if (wp->w_buffer->b_ffname != NULL
9522#ifdef FEAT_QUICKFIX
9523 && !bt_nofile(wp->w_buffer)
9524#endif
9525 )
9526 {
9527 /*
9528 * Editing a file in this buffer: use ":edit file".
9529 * This may have side effects! (e.g., compressed or network file).
9530 */
9531 if (fputs("edit ", fd) < 0
9532 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9533 return FAIL;
9534 }
9535 else
9536 {
9537 /* No file in this buffer, just make it empty. */
9538 if (put_line(fd, "enew") == FAIL)
9539 return FAIL;
9540#ifdef FEAT_QUICKFIX
9541 if (wp->w_buffer->b_ffname != NULL)
9542 {
9543 /* The buffer does have a name, but it's not a file name. */
9544 if (fputs("file ", fd) < 0
9545 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9546 return FAIL;
9547 }
9548#endif
9549 do_cursor = FALSE;
9550 }
9551 }
9552
9553 /*
9554 * Local mappings and abbreviations.
9555 */
9556 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9557 && makemap(fd, wp->w_buffer) == FAIL)
9558 return FAIL;
9559
9560 /*
9561 * Local options. Need to go to the window temporarily.
9562 * Store only local values when using ":mkview" and when ":mksession" is
9563 * used and 'sessionoptions' doesn't include "options".
9564 * Some folding options are always stored when "folds" is included,
9565 * otherwise the folds would not be restored correctly.
9566 */
9567 save_curwin = curwin;
9568 curwin = wp;
9569 curbuf = curwin->w_buffer;
9570 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9571 f = makeset(fd, OPT_LOCAL,
9572 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
9573#ifdef FEAT_FOLDING
9574 else if (*flagp & SSOP_FOLDS)
9575 f = makefoldset(fd);
9576#endif
9577 else
9578 f = OK;
9579 curwin = save_curwin;
9580 curbuf = curwin->w_buffer;
9581 if (f == FAIL)
9582 return FAIL;
9583
9584#ifdef FEAT_FOLDING
9585 /*
9586 * Save Folds when 'buftype' is empty and for help files.
9587 */
9588 if ((*flagp & SSOP_FOLDS)
9589 && wp->w_buffer->b_ffname != NULL
9590 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help))
9591 {
9592 if (put_folds(fd, wp) == FAIL)
9593 return FAIL;
9594 }
9595#endif
9596
9597 /*
9598 * Set the cursor after creating folds, since that moves the cursor.
9599 */
9600 if (do_cursor)
9601 {
9602
9603 /* Restore the cursor line in the file and relatively in the
9604 * window. Don't use "G", it changes the jumplist. */
9605 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
9606 (long)wp->w_cursor.lnum,
9607 (long)(wp->w_cursor.lnum - wp->w_topline),
9608 (long)wp->w_height / 2, (long)wp->w_height) < 0
9609 || put_eol(fd) == FAIL
9610 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
9611 || put_line(fd, "exe s:l") == FAIL
9612 || put_line(fd, "normal! zt") == FAIL
9613 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
9614 || put_eol(fd) == FAIL)
9615 return FAIL;
9616 /* Restore the cursor column and left offset when not wrapping. */
9617 if (wp->w_cursor.col == 0)
9618 {
9619 if (put_line(fd, "normal! 0") == FAIL)
9620 return FAIL;
9621 }
9622 else
9623 {
9624 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
9625 {
9626 if (fprintf(fd,
9627 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
9628 (long)wp->w_cursor.col,
9629 (long)(wp->w_cursor.col - wp->w_leftcol),
9630 (long)wp->w_width / 2, (long)wp->w_width) < 0
9631 || put_eol(fd) == FAIL
9632 || put_line(fd, "if s:c > 0") == FAIL
9633 || fprintf(fd,
9634 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
9635 (long)wp->w_cursor.col) < 0
9636 || put_eol(fd) == FAIL
9637 || put_line(fd, "else") == FAIL
9638 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
9639 || put_eol(fd) == FAIL
9640 || put_line(fd, "endif") == FAIL)
9641 return FAIL;
9642 }
9643 else
9644 {
9645 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
9646 || put_eol(fd) == FAIL)
9647 return FAIL;
9648 }
9649 }
9650 }
9651
9652 /*
9653 * Local directory.
9654 */
9655 if (wp->w_localdir != NULL)
9656 {
9657 if (fputs("lcd ", fd) < 0
9658 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
9659 || put_eol(fd) == FAIL)
9660 return FAIL;
9661 }
9662
9663 return OK;
9664}
9665
9666/*
9667 * Write an argument list to the session file.
9668 * Returns FAIL if writing fails.
9669 */
9670 static int
9671ses_arglist(fd, cmd, gap, fullname, flagp)
9672 FILE *fd;
9673 char *cmd;
9674 garray_T *gap;
9675 int fullname; /* TRUE: use full path name */
9676 unsigned *flagp;
9677{
9678 int i;
9679 char_u buf[MAXPATHL];
9680 char_u *s;
9681
9682 if (gap->ga_len == 0)
9683 return put_line(fd, "silent! argdel *");
9684 if (fputs(cmd, fd) < 0)
9685 return FAIL;
9686 for (i = 0; i < gap->ga_len; ++i)
9687 {
9688 /* NULL file names are skipped (only happens when out of memory). */
9689 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
9690 if (s != NULL)
9691 {
9692 if (fullname)
9693 {
9694 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
9695 s = buf;
9696 }
9697 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
9698 return FAIL;
9699 }
9700 }
9701 return put_eol(fd);
9702}
9703
9704/*
9705 * Write a buffer name to the session file.
9706 * Also ends the line.
9707 * Returns FAIL if writing fails.
9708 */
9709 static int
9710ses_fname(fd, buf, flagp)
9711 FILE *fd;
9712 buf_T *buf;
9713 unsigned *flagp;
9714{
9715 char_u *name;
9716
9717 /* Use the short file name if the current directory is known at the time
9718 * the session file will be sourced. Don't do this for ":mkview", we
9719 * don't know the current directory. */
9720 if (buf->b_sfname != NULL
9721 && flagp == &ssop_flags
9722 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
9723 name = buf->b_sfname;
9724 else
9725 name = buf->b_ffname;
9726 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
9727 return FAIL;
9728 return OK;
9729}
9730
9731/*
9732 * Write a file name to the session file.
9733 * Takes care of the "slash" option in 'sessionoptions' and escapes special
9734 * characters.
9735 * Returns FAIL if writing fails.
9736 */
9737 static int
9738ses_put_fname(fd, name, flagp)
9739 FILE *fd;
9740 char_u *name;
9741 unsigned *flagp;
9742{
9743 char_u *sname;
9744 int retval = OK;
9745 int c;
9746
9747 sname = home_replace_save(NULL, name);
9748 if (sname != NULL)
9749 name = sname;
9750 while (*name != NUL)
9751 {
9752#ifdef FEAT_MBYTE
9753 {
9754 int l;
9755
9756 if (has_mbyte && (l = (*mb_ptr2len_check)(name)) > 1)
9757 {
9758 /* copy a multibyte char */
9759 while (--l >= 0)
9760 {
9761 if (putc(*name, fd) != *name)
9762 retval = FAIL;
9763 ++name;
9764 }
9765 continue;
9766 }
9767 }
9768#endif
9769 c = *name++;
9770 if (c == '\\' && (*flagp & SSOP_SLASH))
9771 /* change a backslash to a forward slash */
9772 c = '/';
9773 else if ((vim_strchr(escape_chars, c) != NULL
9774#ifdef BACKSLASH_IN_FILENAME
9775 && c != '\\'
9776#endif
9777 ) || c == '#' || c == '%')
9778 {
9779 /* escape a special character with a backslash */
9780 if (putc('\\', fd) != '\\')
9781 retval = FAIL;
9782 }
9783 if (putc(c, fd) != c)
9784 retval = FAIL;
9785 }
9786 vim_free(sname);
9787 return retval;
9788}
9789
9790/*
9791 * ":loadview [nr]"
9792 */
9793 static void
9794ex_loadview(eap)
9795 exarg_T *eap;
9796{
9797 char_u *fname;
9798
9799 fname = get_view_file(*eap->arg);
9800 if (fname != NULL)
9801 {
9802 do_source(fname, FALSE, FALSE);
9803 vim_free(fname);
9804 }
9805}
9806
9807/*
9808 * Get the name of the view file for the current buffer.
9809 */
9810 static char_u *
9811get_view_file(c)
9812 int c;
9813{
9814 int len = 0;
9815 char_u *p, *s;
9816 char_u *retval;
9817 char_u *sname;
9818
9819 if (curbuf->b_ffname == NULL)
9820 {
9821 EMSG(_(e_noname));
9822 return NULL;
9823 }
9824 sname = home_replace_save(NULL, curbuf->b_ffname);
9825 if (sname == NULL)
9826 return NULL;
9827
9828 /*
9829 * We want a file name without separators, because we're not going to make
9830 * a directory.
9831 * "normal" path separator -> "=+"
9832 * "=" -> "=="
9833 * ":" path separator -> "=-"
9834 */
9835 for (p = sname; *p; ++p)
9836 if (*p == '=' || vim_ispathsep(*p))
9837 ++len;
9838 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
9839 if (retval != NULL)
9840 {
9841 STRCPY(retval, p_vdir);
9842 add_pathsep(retval);
9843 s = retval + STRLEN(retval);
9844 for (p = sname; *p; ++p)
9845 {
9846 if (*p == '=')
9847 {
9848 *s++ = '=';
9849 *s++ = '=';
9850 }
9851 else if (vim_ispathsep(*p))
9852 {
9853 *s++ = '=';
9854#ifdef MACOS_CLASSIC /* TODO: Is it also needed for MACOS_X? (Dany) */
9855 *s++ = '+';
9856#else
9857# if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
9858 || defined(VMS)
9859 if (*p == ':')
9860 *s++ = '-';
9861 else
9862# endif
9863 *s++ = '+';
9864#endif
9865 }
9866 else
9867 *s++ = *p;
9868 }
9869 *s++ = '=';
9870 *s++ = c;
9871 STRCPY(s, ".vim");
9872 }
9873
9874 vim_free(sname);
9875 return retval;
9876}
9877
9878#endif /* FEAT_SESSION */
9879
9880/*
9881 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
9882 * Return FAIL for a write error.
9883 */
9884 int
9885put_eol(fd)
9886 FILE *fd;
9887{
9888 if (
9889#ifdef USE_CRNL
9890 (
9891# ifdef MKSESSION_NL
9892 !mksession_nl &&
9893# endif
9894 (putc('\r', fd) < 0)) ||
9895#endif
9896 (putc('\n', fd) < 0))
9897 return FAIL;
9898 return OK;
9899}
9900
9901/*
9902 * Write a line to "fd".
9903 * Return FAIL for a write error.
9904 */
9905 int
9906put_line(fd, s)
9907 FILE *fd;
9908 char *s;
9909{
9910 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
9911 return FAIL;
9912 return OK;
9913}
9914
9915#ifdef FEAT_VIMINFO
9916/*
9917 * ":rviminfo" and ":wviminfo".
9918 */
9919 static void
9920ex_viminfo(eap)
9921 exarg_T *eap;
9922{
9923 char_u *save_viminfo;
9924
9925 save_viminfo = p_viminfo;
9926 if (*p_viminfo == NUL)
9927 p_viminfo = (char_u *)"'100";
9928 if (eap->cmdidx == CMD_rviminfo)
9929 {
9930 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
9931 EMSG(_("E195: Cannot open viminfo file for reading"));
9932 }
9933 else
9934 write_viminfo(eap->arg, eap->forceit);
9935 p_viminfo = save_viminfo;
9936}
9937#endif
9938
9939#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009940/*
9941 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009942 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 void
9945dialog_msg(buff, format, fname)
9946 char_u *buff;
9947 char *format;
9948 char_u *fname;
9949{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 if (fname == NULL)
9951 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009952 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953}
9954#endif
9955
9956/*
9957 * ":behave {mswin,xterm}"
9958 */
9959 static void
9960ex_behave(eap)
9961 exarg_T *eap;
9962{
9963 if (STRCMP(eap->arg, "mswin") == 0)
9964 {
9965 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9966 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9967 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9968 set_option_value((char_u *)"keymodel", 0L,
9969 (char_u *)"startsel,stopsel", 0);
9970 }
9971 else if (STRCMP(eap->arg, "xterm") == 0)
9972 {
9973 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9974 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9975 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9976 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9977 }
9978 else
9979 EMSG2(_(e_invarg2), eap->arg);
9980}
9981
9982#ifdef FEAT_AUTOCMD
9983static int filetype_detect = FALSE;
9984static int filetype_plugin = FALSE;
9985static int filetype_indent = FALSE;
9986
9987/*
9988 * ":filetype [plugin] [indent] {on,off,detect}"
9989 * on: Load the filetype.vim file to install autocommands for file types.
9990 * off: Load the ftoff.vim file to remove all autocommands for file types.
9991 * plugin on: load filetype.vim and ftplugin.vim
9992 * plugin off: load ftplugof.vim
9993 * indent on: load filetype.vim and indent.vim
9994 * indent off: load indoff.vim
9995 */
9996 static void
9997ex_filetype(eap)
9998 exarg_T *eap;
9999{
10000 char_u *arg = eap->arg;
10001 int plugin = FALSE;
10002 int indent = FALSE;
10003
10004 if (*eap->arg == NUL)
10005 {
10006 /* Print current status. */
10007 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10008 filetype_detect ? "ON" : "OFF",
10009 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10010 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10011 return;
10012 }
10013
10014 /* Accept "plugin" and "indent" in any order. */
10015 for (;;)
10016 {
10017 if (STRNCMP(arg, "plugin", 6) == 0)
10018 {
10019 plugin = TRUE;
10020 arg = skipwhite(arg + 6);
10021 continue;
10022 }
10023 if (STRNCMP(arg, "indent", 6) == 0)
10024 {
10025 indent = TRUE;
10026 arg = skipwhite(arg + 6);
10027 continue;
10028 }
10029 break;
10030 }
10031 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10032 {
10033 if (*arg == 'o' || !filetype_detect)
10034 {
10035 cmd_runtime((char_u *)FILETYPE_FILE, TRUE);
10036 filetype_detect = TRUE;
10037 if (plugin)
10038 {
10039 cmd_runtime((char_u *)FTPLUGIN_FILE, TRUE);
10040 filetype_plugin = TRUE;
10041 }
10042 if (indent)
10043 {
10044 cmd_runtime((char_u *)INDENT_FILE, TRUE);
10045 filetype_indent = TRUE;
10046 }
10047 }
10048 if (*arg == 'd')
10049 {
10050 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +000010051 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 }
10053 }
10054 else if (STRCMP(arg, "off") == 0)
10055 {
10056 if (plugin || indent)
10057 {
10058 if (plugin)
10059 {
10060 cmd_runtime((char_u *)FTPLUGOF_FILE, TRUE);
10061 filetype_plugin = FALSE;
10062 }
10063 if (indent)
10064 {
10065 cmd_runtime((char_u *)INDOFF_FILE, TRUE);
10066 filetype_indent = FALSE;
10067 }
10068 }
10069 else
10070 {
10071 cmd_runtime((char_u *)FTOFF_FILE, TRUE);
10072 filetype_detect = FALSE;
10073 }
10074 }
10075 else
10076 EMSG2(_(e_invarg2), arg);
10077}
10078
10079/*
10080 * ":setfiletype {name}"
10081 */
10082 static void
10083ex_setfiletype(eap)
10084 exarg_T *eap;
10085{
10086 if (!did_filetype)
10087 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10088}
10089#endif
10090
10091/*ARGSUSED*/
10092 static void
10093ex_digraphs(eap)
10094 exarg_T *eap;
10095{
10096#ifdef FEAT_DIGRAPHS
10097 if (*eap->arg != NUL)
10098 putdigraph(eap->arg);
10099 else
10100 listdigraphs();
10101#else
10102 EMSG(_("E196: No digraphs in this version"));
10103#endif
10104}
10105
10106 static void
10107ex_set(eap)
10108 exarg_T *eap;
10109{
10110 int flags = 0;
10111
10112 if (eap->cmdidx == CMD_setlocal)
10113 flags = OPT_LOCAL;
10114 else if (eap->cmdidx == CMD_setglobal)
10115 flags = OPT_GLOBAL;
10116#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10117 if (cmdmod.browse && flags == 0)
10118 ex_options(eap);
10119 else
10120#endif
10121 (void)do_set(eap->arg, flags);
10122}
10123
10124#ifdef FEAT_SEARCH_EXTRA
10125/*
10126 * ":nohlsearch"
10127 */
10128/*ARGSUSED*/
10129 static void
10130ex_nohlsearch(eap)
10131 exarg_T *eap;
10132{
10133 no_hlsearch = TRUE;
10134 redraw_all_later(NOT_VALID);
10135}
10136
10137/*
10138 * ":match {group} {pattern}"
10139 * Sets nextcmd to the start of the next command, if any. Also called when
10140 * skipping commands to find the next command.
10141 */
10142 static void
10143ex_match(eap)
10144 exarg_T *eap;
10145{
10146 char_u *p;
10147 char_u *end;
10148 int c;
10149
10150 /* First clear any old pattern. */
10151 if (!eap->skip)
10152 {
10153 vim_free(curwin->w_match.regprog);
10154 curwin->w_match.regprog = NULL;
10155 redraw_later(NOT_VALID); /* always need a redraw */
10156 }
10157
10158 if (ends_excmd(*eap->arg))
10159 end = eap->arg;
10160 else if ((STRNICMP(eap->arg, "none", 4) == 0
10161 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10162 end = eap->arg + 4;
10163 else
10164 {
10165 p = skiptowhite(eap->arg);
10166 if (!eap->skip)
10167 {
10168 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg));
10169 if (curwin->w_match_id == 0)
10170 {
10171 EMSG2(_(e_nogroup), eap->arg);
10172 return;
10173 }
10174 }
10175 p = skipwhite(p);
10176 if (*p == NUL)
10177 {
10178 /* There must be two arguments. */
10179 EMSG2(_(e_invarg2), eap->arg);
10180 return;
10181 }
10182 end = skip_regexp(p + 1, *p, TRUE, NULL);
10183 if (!eap->skip)
10184 {
10185 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10186 {
10187 eap->errmsg = e_trailing;
10188 return;
10189 }
10190
10191 c = *end;
10192 *end = NUL;
10193 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC);
10194 *end = c;
10195 if (curwin->w_match.regprog == NULL)
10196 {
10197 EMSG2(_(e_invarg2), p);
10198 return;
10199 }
10200 }
10201 }
10202 eap->nextcmd = find_nextcmd(end);
10203}
10204#endif
10205
10206#ifdef FEAT_CRYPT
10207/*
10208 * ":X": Get crypt key
10209 */
10210/*ARGSUSED*/
10211 static void
10212ex_X(eap)
10213 exarg_T *eap;
10214{
10215 (void)get_crypt_key(TRUE, TRUE);
10216}
10217#endif
10218
10219#ifdef FEAT_FOLDING
10220 static void
10221ex_fold(eap)
10222 exarg_T *eap;
10223{
10224 if (foldManualAllowed(TRUE))
10225 foldCreate(eap->line1, eap->line2);
10226}
10227
10228 static void
10229ex_foldopen(eap)
10230 exarg_T *eap;
10231{
10232 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10233 eap->forceit, FALSE);
10234}
10235
10236 static void
10237ex_folddo(eap)
10238 exarg_T *eap;
10239{
10240 linenr_T lnum;
10241
10242 /* First set the marks for all lines closed/open. */
10243 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10244 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10245 ml_setmarked(lnum);
10246
10247 /* Execute the command on the marked lines. */
10248 global_exe(eap->arg);
10249 ml_clearmarked(); /* clear rest of the marks */
10250}
10251#endif