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