blob: 72ffc18c47a8a03aceced7555ff0b1e213ddcba3 [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);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005120#ifdef FEAT_EVAL
5121 if (p_verbose > 0)
5122 last_set_msg(cmd->uc_scriptID);
5123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 out_flush();
5125 ui_breakcheck();
5126 if (got_int)
5127 break;
5128 }
5129 if (gap == &ucmds || i < gap->ga_len)
5130 break;
5131 gap = &ucmds;
5132 }
5133
5134 if (!found)
5135 MSG(_("No user-defined commands found"));
5136}
5137
5138 static char_u *
5139uc_fun_cmd()
5140{
5141 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5142 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5143 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5144 0xb9, 0x7f, 0};
5145 int i;
5146
5147 for (i = 0; fcmd[i]; ++i)
5148 IObuff[i] = fcmd[i] - 0x40;
5149 IObuff[i] = 0;
5150 return IObuff;
5151}
5152
5153 static int
5154uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5155 char_u *attr;
5156 size_t len;
5157 long *argt;
5158 long *def;
5159 int *flags;
5160 int *compl;
5161 char_u **compl_arg;
5162{
5163 char_u *p;
5164
5165 if (len == 0)
5166 {
5167 EMSG(_("E175: No attribute specified"));
5168 return FAIL;
5169 }
5170
5171 /* First, try the simple attributes (no arguments) */
5172 if (STRNICMP(attr, "bang", len) == 0)
5173 *argt |= BANG;
5174 else if (STRNICMP(attr, "buffer", len) == 0)
5175 *flags |= UC_BUFFER;
5176 else if (STRNICMP(attr, "register", len) == 0)
5177 *argt |= REGSTR;
5178 else if (STRNICMP(attr, "bar", len) == 0)
5179 *argt |= TRLBAR;
5180 else
5181 {
5182 int i;
5183 char_u *val = NULL;
5184 size_t vallen = 0;
5185 size_t attrlen = len;
5186
5187 /* Look for the attribute name - which is the part before any '=' */
5188 for (i = 0; i < (int)len; ++i)
5189 {
5190 if (attr[i] == '=')
5191 {
5192 val = &attr[i + 1];
5193 vallen = len - i - 1;
5194 attrlen = i;
5195 break;
5196 }
5197 }
5198
5199 if (STRNICMP(attr, "nargs", attrlen) == 0)
5200 {
5201 if (vallen == 1)
5202 {
5203 if (*val == '0')
5204 /* Do nothing - this is the default */;
5205 else if (*val == '1')
5206 *argt |= (EXTRA | NOSPC | NEEDARG);
5207 else if (*val == '*')
5208 *argt |= EXTRA;
5209 else if (*val == '?')
5210 *argt |= (EXTRA | NOSPC);
5211 else if (*val == '+')
5212 *argt |= (EXTRA | NEEDARG);
5213 else
5214 goto wrong_nargs;
5215 }
5216 else
5217 {
5218wrong_nargs:
5219 EMSG(_("E176: Invalid number of arguments"));
5220 return FAIL;
5221 }
5222 }
5223 else if (STRNICMP(attr, "range", attrlen) == 0)
5224 {
5225 *argt |= RANGE;
5226 if (vallen == 1 && *val == '%')
5227 *argt |= DFLALL;
5228 else if (val != NULL)
5229 {
5230 p = val;
5231 if (*def >= 0)
5232 {
5233two_count:
5234 EMSG(_("E177: Count cannot be specified twice"));
5235 return FAIL;
5236 }
5237
5238 *def = getdigits(&p);
5239 *argt |= (ZEROR | NOTADR);
5240
5241 if (p != val + vallen || vallen == 0)
5242 {
5243invalid_count:
5244 EMSG(_("E178: Invalid default value for count"));
5245 return FAIL;
5246 }
5247 }
5248 }
5249 else if (STRNICMP(attr, "count", attrlen) == 0)
5250 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005251 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
5253 if (val != NULL)
5254 {
5255 p = val;
5256 if (*def >= 0)
5257 goto two_count;
5258
5259 *def = getdigits(&p);
5260
5261 if (p != val + vallen)
5262 goto invalid_count;
5263 }
5264
5265 if (*def < 0)
5266 *def = 0;
5267 }
5268 else if (STRNICMP(attr, "complete", attrlen) == 0)
5269 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 if (val == NULL)
5271 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005272 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 return FAIL;
5274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005276 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5277 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
5280 else
5281 {
5282 char_u ch = attr[len];
5283 attr[len] = '\0';
5284 EMSG2(_("E181: Invalid attribute: %s"), attr);
5285 attr[len] = ch;
5286 return FAIL;
5287 }
5288 }
5289
5290 return OK;
5291}
5292
5293 static void
5294ex_command(eap)
5295 exarg_T *eap;
5296{
5297 char_u *name;
5298 char_u *end;
5299 char_u *p;
5300 long argt = 0;
5301 long def = -1;
5302 int flags = 0;
5303 int compl = EXPAND_NOTHING;
5304 char_u *compl_arg = NULL;
5305 int has_attr = (eap->arg[0] == '-');
5306
5307 p = eap->arg;
5308
5309 /* Check for attributes */
5310 while (*p == '-')
5311 {
5312 ++p;
5313 end = skiptowhite(p);
5314 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5315 == FAIL)
5316 return;
5317 p = skipwhite(end);
5318 }
5319
5320 /* Get the name (if any) and skip to the following argument */
5321 name = p;
5322 if (ASCII_ISALPHA(*p))
5323 while (ASCII_ISALNUM(*p))
5324 ++p;
5325 if (!ends_excmd(*p) && !vim_iswhite(*p))
5326 {
5327 EMSG(_("E182: Invalid command name"));
5328 return;
5329 }
5330 end = p;
5331
5332 /* If there is nothing after the name, and no attributes were specified,
5333 * we are listing commands
5334 */
5335 p = skipwhite(end);
5336 if (!has_attr && ends_excmd(*p))
5337 {
5338 uc_list(name, end - name);
5339 }
5340 else if (!ASCII_ISUPPER(*name))
5341 {
5342 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5343 return;
5344 }
5345 else
5346 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5347 eap->forceit);
5348}
5349
5350/*
5351 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 * Clear all user commands, global and for current buffer.
5353 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005354/*ARGSUSED*/
5355 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356ex_comclear(eap)
5357 exarg_T *eap;
5358{
5359 uc_clear(&ucmds);
5360 uc_clear(&curbuf->b_ucmds);
5361}
5362
5363/*
5364 * Clear all user commands for "gap".
5365 */
5366 void
5367uc_clear(gap)
5368 garray_T *gap;
5369{
5370 int i;
5371 ucmd_T *cmd;
5372
5373 for (i = 0; i < gap->ga_len; ++i)
5374 {
5375 cmd = USER_CMD_GA(gap, i);
5376 vim_free(cmd->uc_name);
5377 vim_free(cmd->uc_rep);
5378# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5379 vim_free(cmd->uc_compl_arg);
5380# endif
5381 }
5382 ga_clear(gap);
5383}
5384
5385 static void
5386ex_delcommand(eap)
5387 exarg_T *eap;
5388{
5389 int i = 0;
5390 ucmd_T *cmd = NULL;
5391 int cmp = -1;
5392 garray_T *gap;
5393
5394 gap = &curbuf->b_ucmds;
5395 for (;;)
5396 {
5397 for (i = 0; i < gap->ga_len; ++i)
5398 {
5399 cmd = USER_CMD_GA(gap, i);
5400 cmp = STRCMP(eap->arg, cmd->uc_name);
5401 if (cmp <= 0)
5402 break;
5403 }
5404 if (gap == &ucmds || cmp == 0)
5405 break;
5406 gap = &ucmds;
5407 }
5408
5409 if (cmp != 0)
5410 {
5411 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5412 return;
5413 }
5414
5415 vim_free(cmd->uc_name);
5416 vim_free(cmd->uc_rep);
5417# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5418 vim_free(cmd->uc_compl_arg);
5419# endif
5420
5421 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
5423 if (i < gap->ga_len)
5424 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5425}
5426
5427 static char_u *
5428uc_split_args(arg, lenp)
5429 char_u *arg;
5430 size_t *lenp;
5431{
5432 char_u *buf;
5433 char_u *p;
5434 char_u *q;
5435 int len;
5436
5437 /* Precalculate length */
5438 p = arg;
5439 len = 2; /* Initial and final quotes */
5440
5441 while (*p)
5442 {
5443 if (p[0] == '\\' && vim_iswhite(p[1]))
5444 {
5445 len += 1;
5446 p += 2;
5447 }
5448 else if (*p == '\\' || *p == '"')
5449 {
5450 len += 2;
5451 p += 1;
5452 }
5453 else if (vim_iswhite(*p))
5454 {
5455 p = skipwhite(p);
5456 if (*p == NUL)
5457 break;
5458 len += 3; /* "," */
5459 }
5460 else
5461 {
5462 ++len;
5463 ++p;
5464 }
5465 }
5466
5467 buf = alloc(len + 1);
5468 if (buf == NULL)
5469 {
5470 *lenp = 0;
5471 return buf;
5472 }
5473
5474 p = arg;
5475 q = buf;
5476 *q++ = '"';
5477 while (*p)
5478 {
5479 if (p[0] == '\\' && vim_iswhite(p[1]))
5480 {
5481 *q++ = p[1];
5482 p += 2;
5483 }
5484 else if (*p == '\\' || *p == '"')
5485 {
5486 *q++ = '\\';
5487 *q++ = *p++;
5488 }
5489 else if (vim_iswhite(*p))
5490 {
5491 p = skipwhite(p);
5492 if (*p == NUL)
5493 break;
5494 *q++ = '"';
5495 *q++ = ',';
5496 *q++ = '"';
5497 }
5498 else
5499 {
5500 *q++ = *p++;
5501 }
5502 }
5503 *q++ = '"';
5504 *q = 0;
5505
5506 *lenp = len;
5507 return buf;
5508}
5509
5510/*
5511 * Check for a <> code in a user command.
5512 * "code" points to the '<'. "len" the length of the <> (inclusive).
5513 * "buf" is where the result is to be added.
5514 * "split_buf" points to a buffer used for splitting, caller should free it.
5515 * "split_len" is the length of what "split_buf" contains.
5516 * Returns the length of the replacement, which has been added to "buf".
5517 * Returns -1 if there was no match, and only the "<" has been copied.
5518 */
5519 static size_t
5520uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5521 char_u *code;
5522 size_t len;
5523 char_u *buf;
5524 ucmd_T *cmd; /* the user command we're expanding */
5525 exarg_T *eap; /* ex arguments */
5526 char_u **split_buf;
5527 size_t *split_len;
5528{
5529 size_t result = 0;
5530 char_u *p = code + 1;
5531 size_t l = len - 2;
5532 int quote = 0;
5533 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5534 ct_LT, ct_NONE } type = ct_NONE;
5535
5536 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5537 {
5538 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5539 p += 2;
5540 l -= 2;
5541 }
5542
5543 if (l < 1)
5544 type = ct_NONE;
5545 else if (STRNICMP(p, "args", l) == 0)
5546 type = ct_ARGS;
5547 else if (STRNICMP(p, "bang", l) == 0)
5548 type = ct_BANG;
5549 else if (STRNICMP(p, "count", l) == 0)
5550 type = ct_COUNT;
5551 else if (STRNICMP(p, "line1", l) == 0)
5552 type = ct_LINE1;
5553 else if (STRNICMP(p, "line2", l) == 0)
5554 type = ct_LINE2;
5555 else if (STRNICMP(p, "lt", l) == 0)
5556 type = ct_LT;
5557 else if (STRNICMP(p, "register", l) == 0)
5558 type = ct_REGISTER;
5559
5560 switch (type)
5561 {
5562 case ct_ARGS:
5563 /* Simple case first */
5564 if (*eap->arg == NUL)
5565 {
5566 if (quote == 1)
5567 {
5568 result = 2;
5569 if (buf != NULL)
5570 STRCPY(buf, "''");
5571 }
5572 else
5573 result = 0;
5574 break;
5575 }
5576
5577 /* When specified there is a single argument don't split it.
5578 * Works for ":Cmd %" when % is "a b c". */
5579 if ((eap->argt & NOSPC) && quote == 2)
5580 quote = 1;
5581
5582 switch (quote)
5583 {
5584 case 0: /* No quoting, no splitting */
5585 result = STRLEN(eap->arg);
5586 if (buf != NULL)
5587 STRCPY(buf, eap->arg);
5588 break;
5589 case 1: /* Quote, but don't split */
5590 result = STRLEN(eap->arg) + 2;
5591 for (p = eap->arg; *p; ++p)
5592 {
5593 if (*p == '\\' || *p == '"')
5594 ++result;
5595 }
5596
5597 if (buf != NULL)
5598 {
5599 *buf++ = '"';
5600 for (p = eap->arg; *p; ++p)
5601 {
5602 if (*p == '\\' || *p == '"')
5603 *buf++ = '\\';
5604 *buf++ = *p;
5605 }
5606 *buf = '"';
5607 }
5608
5609 break;
5610 case 2: /* Quote and split */
5611 /* This is hard, so only do it once, and cache the result */
5612 if (*split_buf == NULL)
5613 *split_buf = uc_split_args(eap->arg, split_len);
5614
5615 result = *split_len;
5616 if (buf != NULL && result != 0)
5617 STRCPY(buf, *split_buf);
5618
5619 break;
5620 }
5621 break;
5622
5623 case ct_BANG:
5624 result = eap->forceit ? 1 : 0;
5625 if (quote)
5626 result += 2;
5627 if (buf != NULL)
5628 {
5629 if (quote)
5630 *buf++ = '"';
5631 if (eap->forceit)
5632 *buf++ = '!';
5633 if (quote)
5634 *buf = '"';
5635 }
5636 break;
5637
5638 case ct_LINE1:
5639 case ct_LINE2:
5640 case ct_COUNT:
5641 {
5642 char num_buf[20];
5643 long num = (type == ct_LINE1) ? eap->line1 :
5644 (type == ct_LINE2) ? eap->line2 :
5645 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5646 size_t num_len;
5647
5648 sprintf(num_buf, "%ld", num);
5649 num_len = STRLEN(num_buf);
5650 result = num_len;
5651
5652 if (quote)
5653 result += 2;
5654
5655 if (buf != NULL)
5656 {
5657 if (quote)
5658 *buf++ = '"';
5659 STRCPY(buf, num_buf);
5660 buf += num_len;
5661 if (quote)
5662 *buf = '"';
5663 }
5664
5665 break;
5666 }
5667
5668 case ct_REGISTER:
5669 result = eap->regname ? 1 : 0;
5670 if (quote)
5671 result += 2;
5672 if (buf != NULL)
5673 {
5674 if (quote)
5675 *buf++ = '\'';
5676 if (eap->regname)
5677 *buf++ = eap->regname;
5678 if (quote)
5679 *buf = '\'';
5680 }
5681 break;
5682
5683 case ct_LT:
5684 result = 1;
5685 if (buf != NULL)
5686 *buf = '<';
5687 break;
5688
5689 default:
5690 /* Not recognized: just copy the '<' and return -1. */
5691 result = (size_t)-1;
5692 if (buf != NULL)
5693 *buf = '<';
5694 break;
5695 }
5696
5697 return result;
5698}
5699
5700 static void
5701do_ucmd(eap)
5702 exarg_T *eap;
5703{
5704 char_u *buf;
5705 char_u *p;
5706 char_u *q;
5707
5708 char_u *start;
5709 char_u *end;
5710 size_t len, totlen;
5711
5712 size_t split_len = 0;
5713 char_u *split_buf = NULL;
5714 ucmd_T *cmd;
5715#ifdef FEAT_EVAL
5716 scid_T save_current_SID = current_SID;
5717#endif
5718
5719 if (eap->cmdidx == CMD_USER)
5720 cmd = USER_CMD(eap->useridx);
5721 else
5722 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5723
5724 /*
5725 * Replace <> in the command by the arguments.
5726 */
5727 buf = NULL;
5728 for (;;)
5729 {
5730 p = cmd->uc_rep;
5731 q = buf;
5732 totlen = 0;
5733 while ((start = vim_strchr(p, '<')) != NULL
5734 && (end = vim_strchr(start + 1, '>')) != NULL)
5735 {
5736 /* Include the '>' */
5737 ++end;
5738
5739 /* Take everything up to the '<' */
5740 len = start - p;
5741 if (buf == NULL)
5742 totlen += len;
5743 else
5744 {
5745 mch_memmove(q, p, len);
5746 q += len;
5747 }
5748
5749 len = uc_check_code(start, end - start, q, cmd, eap,
5750 &split_buf, &split_len);
5751 if (len == (size_t)-1)
5752 {
5753 /* no match, continue after '<' */
5754 p = start + 1;
5755 len = 1;
5756 }
5757 else
5758 p = end;
5759 if (buf == NULL)
5760 totlen += len;
5761 else
5762 q += len;
5763 }
5764 if (buf != NULL) /* second time here, finished */
5765 {
5766 STRCPY(q, p);
5767 break;
5768 }
5769
5770 totlen += STRLEN(p); /* Add on the trailing characters */
5771 buf = alloc((unsigned)(totlen + 1));
5772 if (buf == NULL)
5773 {
5774 vim_free(split_buf);
5775 return;
5776 }
5777 }
5778
5779#ifdef FEAT_EVAL
5780 current_SID = cmd->uc_scriptID;
5781#endif
5782 (void)do_cmdline(buf, eap->getline, eap->cookie,
5783 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5784#ifdef FEAT_EVAL
5785 current_SID = save_current_SID;
5786#endif
5787 vim_free(buf);
5788 vim_free(split_buf);
5789}
5790
5791# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5792 static char_u *
5793get_user_command_name(idx)
5794 int idx;
5795{
5796 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5797}
5798
5799/*
5800 * Function given to ExpandGeneric() to obtain the list of user command names.
5801 */
5802/*ARGSUSED*/
5803 char_u *
5804get_user_commands(xp, idx)
5805 expand_T *xp;
5806 int idx;
5807{
5808 if (idx < curbuf->b_ucmds.ga_len)
5809 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5810 idx -= curbuf->b_ucmds.ga_len;
5811 if (idx < ucmds.ga_len)
5812 return USER_CMD(idx)->uc_name;
5813 return NULL;
5814}
5815
5816/*
5817 * Function given to ExpandGeneric() to obtain the list of user command
5818 * attributes.
5819 */
5820/*ARGSUSED*/
5821 char_u *
5822get_user_cmd_flags(xp, idx)
5823 expand_T *xp;
5824 int idx;
5825{
5826 static char *user_cmd_flags[] =
5827 {"bang", "bar", "buffer", "complete", "count",
5828 "nargs", "range", "register"};
5829
5830 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5831 return NULL;
5832 return (char_u *)user_cmd_flags[idx];
5833}
5834
5835/*
5836 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5837 */
5838/*ARGSUSED*/
5839 char_u *
5840get_user_cmd_nargs(xp, idx)
5841 expand_T *xp;
5842 int idx;
5843{
5844 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5845
5846 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5847 return NULL;
5848 return (char_u *)user_cmd_nargs[idx];
5849}
5850
5851/*
5852 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5853 */
5854/*ARGSUSED*/
5855 char_u *
5856get_user_cmd_complete(xp, idx)
5857 expand_T *xp;
5858 int idx;
5859{
5860 return (char_u *)command_complete[idx].name;
5861}
5862# endif /* FEAT_CMDL_COMPL */
5863
5864#endif /* FEAT_USR_CMDS */
5865
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005866#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
5867/*
5868 * Parse a completion argument "value[vallen]".
5869 * The detected completion goes in "*complp", argument type in "*argt".
5870 * When there is an argument, for function and user defined completion, it's
5871 * copied to allocated memory and stored in "*compl_arg".
5872 * Returns FAIL if something is wrong.
5873 */
5874 int
5875parse_compl_arg(value, vallen, complp, argt, compl_arg)
5876 char_u *value;
5877 int vallen;
5878 int *complp;
5879 long *argt;
5880 char_u **compl_arg;
5881{
5882 char_u *arg = NULL;
5883 size_t arglen = 0;
5884 int i;
5885 int valend = vallen;
5886
5887 /* Look for any argument part - which is the part after any ',' */
5888 for (i = 0; i < vallen; ++i)
5889 {
5890 if (value[i] == ',')
5891 {
5892 arg = &value[i + 1];
5893 arglen = vallen - i - 1;
5894 valend = i;
5895 break;
5896 }
5897 }
5898
5899 for (i = 0; command_complete[i].expand != 0; ++i)
5900 {
5901 if (STRLEN(command_complete[i].name) == valend
5902 && STRNCMP(value, command_complete[i].name, valend) == 0)
5903 {
5904 *complp = command_complete[i].expand;
5905 if (command_complete[i].expand == EXPAND_BUFFERS)
5906 *argt |= BUFNAME;
5907 else if (command_complete[i].expand == EXPAND_DIRECTORIES
5908 || command_complete[i].expand == EXPAND_FILES)
5909 *argt |= XFILE;
5910 break;
5911 }
5912 }
5913
5914 if (command_complete[i].expand == 0)
5915 {
5916 EMSG2(_("E180: Invalid complete value: %s"), value);
5917 return FAIL;
5918 }
5919
5920# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5921 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
5922 && arg != NULL)
5923# else
5924 if (arg != NULL)
5925# endif
5926 {
5927 EMSG(_("E468: Completion argument only allowed for custom completion"));
5928 return FAIL;
5929 }
5930
5931# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5932 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
5933 && arg == NULL)
5934 {
5935 EMSG(_("E467: Custom completion requires a function argument"));
5936 return FAIL;
5937 }
5938
5939 if (arg != NULL)
5940 *compl_arg = vim_strnsave(arg, (int)arglen);
5941# endif
5942 return OK;
5943}
5944#endif
5945
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 static void
5947ex_colorscheme(eap)
5948 exarg_T *eap;
5949{
5950 if (load_colors(eap->arg) == FAIL)
5951 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
5952}
5953
5954 static void
5955ex_highlight(eap)
5956 exarg_T *eap;
5957{
5958 if (*eap->arg == NUL && eap->cmd[2] == '!')
5959 MSG(_("Greetings, Vim user!"));
5960 do_highlight(eap->arg, eap->forceit, FALSE);
5961}
5962
5963
5964/*
5965 * Call this function if we thought we were going to exit, but we won't
5966 * (because of an error). May need to restore the terminal mode.
5967 */
5968 void
5969not_exiting()
5970{
5971 exiting = FALSE;
5972 settmode(TMODE_RAW);
5973}
5974
5975/*
5976 * ":quit": quit current window, quit Vim if closed the last window.
5977 */
5978 static void
5979ex_quit(eap)
5980 exarg_T *eap;
5981{
5982#ifdef FEAT_CMDWIN
5983 if (cmdwin_type != 0)
5984 {
5985 cmdwin_result = Ctrl_C;
5986 return;
5987 }
5988#endif
5989
5990#ifdef FEAT_NETBEANS_INTG
5991 netbeansForcedQuit = eap->forceit;
5992#endif
5993
5994 /*
5995 * If there are more files or windows we won't exit.
5996 */
5997 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5998 exiting = TRUE;
5999 if ((!P_HID(curbuf)
6000 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6001 || check_more(TRUE, eap->forceit) == FAIL
6002 || (only_one_window() && check_changed_any(eap->forceit)))
6003 {
6004 not_exiting();
6005 }
6006 else
6007 {
6008#ifdef FEAT_WINDOWS
6009 if (only_one_window()) /* quit last window */
6010#endif
6011 getout(0);
6012#ifdef FEAT_WINDOWS
6013# ifdef FEAT_GUI
6014 need_mouse_correct = TRUE;
6015# endif
6016 /* close window; may free buffer */
6017 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6018#endif
6019 }
6020}
6021
6022/*
6023 * ":cquit".
6024 */
6025/*ARGSUSED*/
6026 static void
6027ex_cquit(eap)
6028 exarg_T *eap;
6029{
6030 getout(1); /* this does not always pass on the exit code to the Manx
6031 compiler. why? */
6032}
6033
6034/*
6035 * ":qall": try to quit all windows
6036 */
6037 static void
6038ex_quit_all(eap)
6039 exarg_T *eap;
6040{
6041# ifdef FEAT_CMDWIN
6042 if (cmdwin_type != 0)
6043 {
6044 if (eap->forceit)
6045 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6046 else
6047 cmdwin_result = K_XF2;
6048 return;
6049 }
6050# endif
6051 exiting = TRUE;
6052 if (eap->forceit || !check_changed_any(FALSE))
6053 getout(0);
6054 not_exiting();
6055}
6056
6057#ifdef FEAT_WINDOWS
6058/*
6059 * ":close": close current window, unless it is the last one
6060 */
6061 static void
6062ex_close(eap)
6063 exarg_T *eap;
6064{
6065# ifdef FEAT_CMDWIN
6066 if (cmdwin_type != 0)
6067 cmdwin_result = K_IGNORE;
6068 else
6069# endif
6070 ex_win_close(eap, curwin);
6071}
6072
6073 static void
6074ex_win_close(eap, win)
6075 exarg_T *eap;
6076 win_T *win;
6077{
6078 int need_hide;
6079 buf_T *buf = win->w_buffer;
6080
6081 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
6082 if (need_hide && !P_HID(buf) && !eap->forceit)
6083 {
6084#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6085 if ((p_confirm || cmdmod.confirm) && p_write)
6086 {
6087 dialog_changed(buf, FALSE);
6088 if (buf_valid(buf) && bufIsChanged(buf))
6089 return;
6090 need_hide = FALSE;
6091 }
6092 else
6093#endif
6094 {
6095 EMSG(_(e_nowrtmsg));
6096 return;
6097 }
6098 }
6099
6100#ifdef FEAT_GUI
6101 need_mouse_correct = TRUE;
6102#endif
6103 /* free buffer when not hiding it or when it's a scratch buffer */
6104 win_close(win, !need_hide && !P_HID(buf));
6105}
6106
6107#ifdef FEAT_QUICKFIX
6108/*
6109 * ":pclose": Close any preview window.
6110 */
6111 static void
6112ex_pclose(eap)
6113 exarg_T *eap;
6114{
6115 win_T *win;
6116
6117 for (win = firstwin; win != NULL; win = win->w_next)
6118 if (win->w_p_pvw)
6119 {
6120 ex_win_close(eap, win);
6121 break;
6122 }
6123}
6124#endif
6125
6126/*
6127 * ":only".
6128 */
6129 static void
6130ex_only(eap)
6131 exarg_T *eap;
6132{
6133# ifdef FEAT_GUI
6134 need_mouse_correct = TRUE;
6135# endif
6136 close_others(TRUE, eap->forceit);
6137}
6138
6139/*
6140 * ":all" and ":sall".
6141 */
6142 static void
6143ex_all(eap)
6144 exarg_T *eap;
6145{
6146 if (eap->addr_count == 0)
6147 eap->line2 = 9999;
6148 do_arg_all((int)eap->line2, eap->forceit);
6149}
6150#endif /* FEAT_WINDOWS */
6151
6152 static void
6153ex_hide(eap)
6154 exarg_T *eap;
6155{
6156 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6157 eap->errmsg = e_invarg;
6158 else
6159 {
6160 /* ":hide" or ":hide | cmd": hide current window */
6161 eap->nextcmd = check_nextcmd(eap->arg);
6162#ifdef FEAT_WINDOWS
6163 if (!eap->skip)
6164 {
6165# ifdef FEAT_GUI
6166 need_mouse_correct = TRUE;
6167# endif
6168 win_close(curwin, FALSE); /* don't free buffer */
6169 }
6170#endif
6171 }
6172}
6173
6174/*
6175 * ":stop" and ":suspend": Suspend Vim.
6176 */
6177 static void
6178ex_stop(eap)
6179 exarg_T *eap;
6180{
6181 /*
6182 * Disallow suspending for "rvim".
6183 */
6184 if (!check_restricted()
6185#ifdef WIN3264
6186 /*
6187 * Check if external commands are allowed now.
6188 */
6189 && can_end_termcap_mode(TRUE)
6190#endif
6191 )
6192 {
6193 if (!eap->forceit)
6194 autowrite_all();
6195 windgoto((int)Rows - 1, 0);
6196 out_char('\n');
6197 out_flush();
6198 stoptermcap();
6199 out_flush(); /* needed for SUN to restore xterm buffer */
6200#ifdef FEAT_TITLE
6201 mch_restore_title(3); /* restore window titles */
6202#endif
6203 ui_suspend(); /* call machine specific function */
6204#ifdef FEAT_TITLE
6205 maketitle();
6206 resettitle(); /* force updating the title */
6207#endif
6208 starttermcap();
6209 scroll_start(); /* scroll screen before redrawing */
6210 redraw_later_clear();
6211 shell_resized(); /* may have resized window */
6212 }
6213}
6214
6215/*
6216 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6217 */
6218 static void
6219ex_exit(eap)
6220 exarg_T *eap;
6221{
6222#ifdef FEAT_CMDWIN
6223 if (cmdwin_type != 0)
6224 {
6225 cmdwin_result = Ctrl_C;
6226 return;
6227 }
6228#endif
6229
6230 /*
6231 * if more files or windows we won't exit
6232 */
6233 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6234 exiting = TRUE;
6235 if ( ((eap->cmdidx == CMD_wq
6236 || curbufIsChanged())
6237 && do_write(eap) == FAIL)
6238 || check_more(TRUE, eap->forceit) == FAIL
6239 || (only_one_window() && check_changed_any(eap->forceit)))
6240 {
6241 not_exiting();
6242 }
6243 else
6244 {
6245#ifdef FEAT_WINDOWS
6246 if (only_one_window()) /* quit last window, exit Vim */
6247#endif
6248 getout(0);
6249#ifdef FEAT_WINDOWS
6250# ifdef FEAT_GUI
6251 need_mouse_correct = TRUE;
6252# endif
6253 /* quit current window, may free buffer */
6254 win_close(curwin, !P_HID(curwin->w_buffer));
6255#endif
6256 }
6257}
6258
6259/*
6260 * ":print", ":list", ":number".
6261 */
6262 static void
6263ex_print(eap)
6264 exarg_T *eap;
6265{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006266 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6267 EMSG(_(e_emptybuf));
6268 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006270 for ( ;!got_int; ui_breakcheck())
6271 {
6272 print_line(eap->line1,
6273 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6274 || (eap->flags & EXFLAG_NR)),
6275 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6276 if (++eap->line1 > eap->line2)
6277 break;
6278 out_flush(); /* show one line at a time */
6279 }
6280 setpcmark();
6281 /* put cursor at last line */
6282 curwin->w_cursor.lnum = eap->line2;
6283 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 }
6285
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287}
6288
6289#ifdef FEAT_BYTEOFF
6290 static void
6291ex_goto(eap)
6292 exarg_T *eap;
6293{
6294 goto_byte(eap->line2);
6295}
6296#endif
6297
6298/*
6299 * ":shell".
6300 */
6301/*ARGSUSED*/
6302 static void
6303ex_shell(eap)
6304 exarg_T *eap;
6305{
6306 do_shell(NULL, 0);
6307}
6308
6309#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6310 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
6311 || defined(PROTO)
6312
6313/*
6314 * Handle a file drop. The code is here because a drop is *nearly* like an
6315 * :args command, but not quite (we have a list of exact filenames, so we
6316 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6317 * code is very similar to :args and hence needs access to a lot of the static
6318 * functions in this file.
6319 *
6320 * The list should be allocated using alloc(), as should each item in the
6321 * list. This function takes over responsibility for freeing the list.
6322 *
6323 * XXX The list is made into the arggument list. This is freed using
6324 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6325 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6326 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6327 * file functionality is (currently) not in EMX this is not presently a
6328 * problem.
6329 */
6330 void
6331handle_drop(filec, filev, split)
6332 int filec; /* the number of files dropped */
6333 char_u **filev; /* the list of files dropped */
6334 int split; /* force splitting the window */
6335{
6336 exarg_T ea;
6337 int save_msg_scroll = msg_scroll;
6338
6339# ifdef FEAT_CMDWIN
6340 if (cmdwin_type != 0)
6341 return;
6342# endif
6343
6344 /* Check whether the current buffer is changed. If so, we will need
6345 * to split the current window or data could be lost.
6346 * We don't need to check if the 'hidden' option is set, as in this
6347 * case the buffer won't be lost.
6348 */
6349 if (!P_HID(curbuf) && !split)
6350 {
6351 ++emsg_off;
6352 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6353 --emsg_off;
6354 }
6355 if (split)
6356 {
6357# ifdef FEAT_WINDOWS
6358 if (win_split(0, 0) == FAIL)
6359 return;
6360# ifdef FEAT_SCROLLBIND
6361 curwin->w_p_scb = FALSE;
6362# endif
6363
6364 /* When splitting the window, create a new alist. Otherwise the
6365 * existing one is overwritten. */
6366 alist_unlink(curwin->w_alist);
6367 alist_new();
6368# else
6369 return; /* can't split, always fail */
6370# endif
6371 }
6372
6373 /*
6374 * Set up the new argument list.
6375 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006376 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
6378 /*
6379 * Move to the first file.
6380 */
6381 /* Fake up a minimal "next" command for do_argfile() */
6382 vim_memset(&ea, 0, sizeof(ea));
6383 ea.cmd = (char_u *)"next";
6384 do_argfile(&ea, 0);
6385
6386 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6387 * mode that is not needed here. */
6388 need_start_insertmode = FALSE;
6389
6390 /* Restore msg_scroll, otherwise a following command may cause scrolling
6391 * unexpectedly. The screen will be redrawn by the caller, thus
6392 * msg_scroll being set by displaying a message is irrelevant. */
6393 msg_scroll = save_msg_scroll;
6394}
6395#endif
6396
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397/*
6398 * Clear an argument list: free all file names and reset it to zero entries.
6399 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006400 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401alist_clear(al)
6402 alist_T *al;
6403{
6404 while (--al->al_ga.ga_len >= 0)
6405 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6406 ga_clear(&al->al_ga);
6407}
6408
6409/*
6410 * Init an argument list.
6411 */
6412 void
6413alist_init(al)
6414 alist_T *al;
6415{
6416 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6417}
6418
6419#if defined(FEAT_WINDOWS) || defined(PROTO)
6420
6421/*
6422 * Remove a reference from an argument list.
6423 * Ignored when the argument list is the global one.
6424 * If the argument list is no longer used by any window, free it.
6425 */
6426 void
6427alist_unlink(al)
6428 alist_T *al;
6429{
6430 if (al != &global_alist && --al->al_refcount <= 0)
6431 {
6432 alist_clear(al);
6433 vim_free(al);
6434 }
6435}
6436
6437# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6438/*
6439 * Create a new argument list and use it for the current window.
6440 */
6441 void
6442alist_new()
6443{
6444 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6445 if (curwin->w_alist == NULL)
6446 {
6447 curwin->w_alist = &global_alist;
6448 ++global_alist.al_refcount;
6449 }
6450 else
6451 {
6452 curwin->w_alist->al_refcount = 1;
6453 alist_init(curwin->w_alist);
6454 }
6455}
6456# endif
6457#endif
6458
6459#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6460/*
6461 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006462 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6463 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 */
6465 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006466alist_expand(fnum_list, fnum_len)
6467 int *fnum_list;
6468 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006471 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 char_u **new_arg_files;
6473 int new_arg_file_count;
6474 char_u *save_p_su = p_su;
6475 int i;
6476
6477 /* Don't use 'suffixes' here. This should work like the shell did the
6478 * expansion. Also, the vimrc file isn't read yet, thus the user
6479 * can't set the options. */
6480 p_su = empty_option;
6481 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6482 if (old_arg_files != NULL)
6483 {
6484 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006485 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6486 old_arg_count = GARGCOUNT;
6487 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 &new_arg_file_count, &new_arg_files,
6489 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6490 && new_arg_file_count > 0)
6491 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006492 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6493 TRUE, fnum_list, fnum_len);
6494 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 }
6497 p_su = save_p_su;
6498}
6499#endif
6500
6501/*
6502 * Set the argument list for the current window.
6503 * Takes over the allocated files[] and the allocated fnames in it.
6504 */
6505 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006506alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 alist_T *al;
6508 int count;
6509 char_u **files;
6510 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006511 int *fnum_list;
6512 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
6514 int i;
6515
6516 alist_clear(al);
6517 if (ga_grow(&al->al_ga, count) == OK)
6518 {
6519 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006520 {
6521 if (got_int)
6522 {
6523 /* When adding many buffers this can take a long time. Allow
6524 * interrupting here. */
6525 while (i < count)
6526 vim_free(files[i++]);
6527 break;
6528 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006529
6530 /* May set buffer name of a buffer previously used for the
6531 * argument list, so that it's re-used by alist_add. */
6532 if (fnum_list != NULL && i < fnum_len)
6533 buf_set_name(fnum_list[i], files[i]);
6534
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006536 ui_breakcheck();
6537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 vim_free(files);
6539 }
6540 else
6541 FreeWild(count, files);
6542#ifdef FEAT_WINDOWS
6543 if (al == &global_alist)
6544#endif
6545 arg_had_last = FALSE;
6546}
6547
6548/*
6549 * Add file "fname" to argument list "al".
6550 * "fname" must have been allocated and "al" must have been checked for room.
6551 */
6552 void
6553alist_add(al, fname, set_fnum)
6554 alist_T *al;
6555 char_u *fname;
6556 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6557{
6558 if (fname == NULL) /* don't add NULL file names */
6559 return;
6560#ifdef BACKSLASH_IN_FILENAME
6561 slash_adjust(fname);
6562#endif
6563 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6564 if (set_fnum > 0)
6565 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6566 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6567 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568}
6569
6570#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6571/*
6572 * Adjust slashes in file names. Called after 'shellslash' was set.
6573 */
6574 void
6575alist_slash_adjust()
6576{
6577 int i;
6578# ifdef FEAT_WINDOWS
6579 win_T *wp;
6580# endif
6581
6582 for (i = 0; i < GARGCOUNT; ++i)
6583 if (GARGLIST[i].ae_fname != NULL)
6584 slash_adjust(GARGLIST[i].ae_fname);
6585# ifdef FEAT_WINDOWS
6586 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6587 if (wp->w_alist != &global_alist)
6588 for (i = 0; i < WARGCOUNT(wp); ++i)
6589 if (WARGLIST(wp)[i].ae_fname != NULL)
6590 slash_adjust(WARGLIST(wp)[i].ae_fname);
6591# endif
6592}
6593#endif
6594
6595/*
6596 * ":preserve".
6597 */
6598/*ARGSUSED*/
6599 static void
6600ex_preserve(eap)
6601 exarg_T *eap;
6602{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006603 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 ml_preserve(curbuf, TRUE);
6605}
6606
6607/*
6608 * ":recover".
6609 */
6610 static void
6611ex_recover(eap)
6612 exarg_T *eap;
6613{
6614 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6615 recoverymode = TRUE;
6616 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6617 && (*eap->arg == NUL
6618 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6619 ml_recover();
6620 recoverymode = FALSE;
6621}
6622
6623/*
6624 * Command modifier used in a wrong way.
6625 */
6626 static void
6627ex_wrongmodifier(eap)
6628 exarg_T *eap;
6629{
6630 eap->errmsg = e_invcmd;
6631}
6632
6633#ifdef FEAT_WINDOWS
6634/*
6635 * :sview [+command] file split window with new file, read-only
6636 * :split [[+command] file] split window with current or new file
6637 * :vsplit [[+command] file] split window vertically with current or new file
6638 * :new [[+command] file] split window with no or new file
6639 * :vnew [[+command] file] split vertically window with no or new file
6640 * :sfind [+command] file split window with file in 'path'
6641 */
6642 void
6643ex_splitview(eap)
6644 exarg_T *eap;
6645{
6646 win_T *old_curwin;
6647#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6648 char_u *fname = NULL;
6649#endif
6650#ifdef FEAT_BROWSE
6651 int browse_flag = cmdmod.browse;
6652#endif
6653
6654#ifndef FEAT_VERTSPLIT
6655 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6656 {
6657 ex_ni(eap);
6658 return;
6659 }
6660#endif
6661
6662 old_curwin = curwin;
6663#ifdef FEAT_GUI
6664 need_mouse_correct = TRUE;
6665#endif
6666
6667#ifdef FEAT_QUICKFIX
6668 /* A ":split" in the quickfix window works like ":new". Don't want two
6669 * quickfix windows. */
6670 if (bt_quickfix(curbuf))
6671 {
6672 if (eap->cmdidx == CMD_split)
6673 eap->cmdidx = CMD_new;
6674# ifdef FEAT_VERTSPLIT
6675 if (eap->cmdidx == CMD_vsplit)
6676 eap->cmdidx = CMD_vnew;
6677# endif
6678 }
6679#endif
6680
6681#ifdef FEAT_SEARCHPATH
6682 if (eap->cmdidx == CMD_sfind)
6683 {
6684 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6685 FNAME_MESS, TRUE, curbuf->b_ffname);
6686 if (fname == NULL)
6687 goto theend;
6688 eap->arg = fname;
6689 }
6690# ifdef FEAT_BROWSE
6691 else
6692# endif
6693#endif
6694#ifdef FEAT_BROWSE
6695 if (cmdmod.browse
6696# ifdef FEAT_VERTSPLIT
6697 && eap->cmdidx != CMD_vnew
6698#endif
6699 && eap->cmdidx != CMD_new)
6700 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006701 if (
6702# ifdef FEAT_GUI
6703 !gui.in_use &&
6704# endif
6705 au_has_group((char_u *)"FileExplorer"))
6706 {
6707 /* No browsing supported but we do have the file explorer:
6708 * Edit the directory. */
6709 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6710 eap->arg = (char_u *)".";
6711 }
6712 else
6713 {
6714 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006716 if (fname == NULL)
6717 goto theend;
6718 eap->arg = fname;
6719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 }
6721 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
6722#endif
6723
6724 if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
6725 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6726 {
6727#ifdef FEAT_SCROLLBIND
6728 /* Reset 'scrollbind' when editing another file, but keep it when
6729 * doing ":split" without arguments. */
6730 if (*eap->arg != NUL
6731#ifdef FEAT_BROWSE
6732 || cmdmod.browse
6733#endif
6734 )
6735 curwin->w_p_scb = FALSE;
6736 else
6737 do_check_scrollbind(FALSE);
6738#endif
6739 do_exedit(eap, old_curwin);
6740 }
6741
6742#ifdef FEAT_BROWSE
6743 cmdmod.browse = browse_flag;
6744#endif
6745
6746#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6747theend:
6748 vim_free(fname);
6749#endif
6750}
6751#endif
6752
6753/*
6754 * ":mode": Set screen mode.
6755 * If no argument given, just get the screen size and redraw.
6756 */
6757 static void
6758ex_mode(eap)
6759 exarg_T *eap;
6760{
6761 if (*eap->arg == NUL)
6762 shell_resized();
6763 else
6764 mch_screenmode(eap->arg);
6765}
6766
6767#ifdef FEAT_WINDOWS
6768/*
6769 * ":resize".
6770 * set, increment or decrement current window height
6771 */
6772 static void
6773ex_resize(eap)
6774 exarg_T *eap;
6775{
6776 int n;
6777 win_T *wp = curwin;
6778
6779 if (eap->addr_count > 0)
6780 {
6781 n = eap->line2;
6782 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6783 ;
6784 }
6785
6786#ifdef FEAT_GUI
6787 need_mouse_correct = TRUE;
6788#endif
6789 n = atol((char *)eap->arg);
6790#ifdef FEAT_VERTSPLIT
6791 if (cmdmod.split & WSP_VERT)
6792 {
6793 if (*eap->arg == '-' || *eap->arg == '+')
6794 n += W_WIDTH(curwin);
6795 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6796 n = 9999;
6797 win_setwidth_win((int)n, wp);
6798 }
6799 else
6800#endif
6801 {
6802 if (*eap->arg == '-' || *eap->arg == '+')
6803 n += curwin->w_height;
6804 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6805 n = 9999;
6806 win_setheight_win((int)n, wp);
6807 }
6808}
6809#endif
6810
6811/*
6812 * ":find [+command] <file>" command.
6813 */
6814 static void
6815ex_find(eap)
6816 exarg_T *eap;
6817{
6818#ifdef FEAT_SEARCHPATH
6819 char_u *fname;
6820 int count;
6821
6822 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6823 TRUE, curbuf->b_ffname);
6824 if (eap->addr_count > 0)
6825 {
6826 /* Repeat finding the file "count" times. This matters when it
6827 * appears several times in the path. */
6828 count = eap->line2;
6829 while (fname != NULL && --count > 0)
6830 {
6831 vim_free(fname);
6832 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6833 FALSE, curbuf->b_ffname);
6834 }
6835 }
6836
6837 if (fname != NULL)
6838 {
6839 eap->arg = fname;
6840#endif
6841 do_exedit(eap, NULL);
6842#ifdef FEAT_SEARCHPATH
6843 vim_free(fname);
6844 }
6845#endif
6846}
6847
6848/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006849 * ":open" simulation: for now just work like ":visual".
6850 */
6851 static void
6852ex_open(eap)
6853 exarg_T *eap;
6854{
6855 regmatch_T regmatch;
6856 char_u *p;
6857
6858 curwin->w_cursor.lnum = eap->line2;
6859 beginline(BL_SOL | BL_FIX);
6860 if (*eap->arg == '/')
6861 {
6862 /* ":open /pattern/": put cursor in column found with pattern */
6863 ++eap->arg;
6864 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6865 *p = NUL;
6866 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6867 if (regmatch.regprog != NULL)
6868 {
6869 regmatch.rm_ic = p_ic;
6870 p = ml_get_curline();
6871 if (vim_regexec(&regmatch, p, (colnr_T)0))
6872 curwin->w_cursor.col = regmatch.startp[0] - p;
6873 else
6874 EMSG(_(e_nomatch));
6875 vim_free(regmatch.regprog);
6876 }
6877 /* Move to the NUL, ignore any other arguments. */
6878 eap->arg += STRLEN(eap->arg);
6879 }
6880 check_cursor();
6881
6882 eap->cmdidx = CMD_visual;
6883 do_exedit(eap, NULL);
6884}
6885
6886/*
6887 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 */
6889 static void
6890ex_edit(eap)
6891 exarg_T *eap;
6892{
6893 do_exedit(eap, NULL);
6894}
6895
6896/*
6897 * ":edit <file>" command and alikes.
6898 */
6899/*ARGSUSED*/
6900 void
6901do_exedit(eap, old_curwin)
6902 exarg_T *eap;
6903 win_T *old_curwin; /* curwin before doing a split or NULL */
6904{
6905 int n;
6906#ifdef FEAT_WINDOWS
6907 int need_hide;
6908#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006909 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910
6911 /*
6912 * ":vi" command ends Ex mode.
6913 */
6914 if (exmode_active && (eap->cmdidx == CMD_visual
6915 || eap->cmdidx == CMD_view))
6916 {
6917 exmode_active = FALSE;
6918 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006919 {
6920 /* Special case: ":global/pat/visual\NLvi-commands" */
6921 if (global_busy)
6922 {
6923 int rd = RedrawingDisabled;
6924 int nwr = no_wait_return;
6925 int ms = msg_scroll;
6926#ifdef FEAT_GUI
6927 int he = hold_gui_events;
6928#endif
6929
6930 if (eap->nextcmd != NULL)
6931 {
6932 stuffReadbuff(eap->nextcmd);
6933 eap->nextcmd = NULL;
6934 }
6935
6936 if (exmode_was != EXMODE_VIM)
6937 settmode(TMODE_RAW);
6938 RedrawingDisabled = 0;
6939 no_wait_return = 0;
6940 need_wait_return = FALSE;
6941 msg_scroll = 0;
6942#ifdef FEAT_GUI
6943 hold_gui_events = 0;
6944#endif
6945 must_redraw = CLEAR;
6946
6947 main_loop(FALSE, TRUE);
6948
6949 RedrawingDisabled = rd;
6950 no_wait_return = nwr;
6951 msg_scroll = ms;
6952#ifdef FEAT_GUI
6953 hold_gui_events = he;
6954#endif
6955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 }
6959
6960 if ((eap->cmdidx == CMD_new
6961#ifdef FEAT_VERTSPLIT
6962 || eap->cmdidx == CMD_vnew
6963#endif
6964 ) && *eap->arg == NUL)
6965 {
6966 /* ":new" without argument: edit an new empty buffer */
6967 setpcmark();
6968 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
6969 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
6970 }
6971 else if ((eap->cmdidx != CMD_split
6972#ifdef FEAT_VERTSPLIT
6973 && eap->cmdidx != CMD_vsplit
6974#endif
6975 )
6976 || *eap->arg != NUL
6977#ifdef FEAT_BROWSE
6978 || cmdmod.browse
6979#endif
6980 )
6981 {
6982 n = readonlymode;
6983 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6984 readonlymode = TRUE;
6985 else if (eap->cmdidx == CMD_enew)
6986 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
6987 empty buffer */
6988 setpcmark();
6989 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6990 NULL, eap,
6991 /* ":edit" goes to first line if Vi compatible */
6992 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6993 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6994 ? ECMD_ONE : eap->do_ecmd_lnum,
6995 (P_HID(curbuf) ? ECMD_HIDE : 0)
6996 + (eap->forceit ? ECMD_FORCEIT : 0)
6997#ifdef FEAT_LISTCMDS
6998 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
6999#endif
7000 ) == FAIL)
7001 {
7002 /* Editing the file failed. If the window was split, close it. */
7003#ifdef FEAT_WINDOWS
7004 if (old_curwin != NULL)
7005 {
7006 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7007 if (!need_hide || P_HID(curbuf))
7008 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007009# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7010 cleanup_T cs;
7011
7012 /* Reset the error/interrupt/exception state here so that
7013 * aborting() returns FALSE when closing a window. */
7014 enter_cleanup(&cs);
7015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016# ifdef FEAT_GUI
7017 need_mouse_correct = TRUE;
7018# endif
7019 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007020
7021# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7022 /* Restore the error/interrupt/exception state if not
7023 * discarded by a new aborting error, interrupt, or
7024 * uncaught exception. */
7025 leave_cleanup(&cs);
7026# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 }
7028 }
7029#endif
7030 }
7031 else if (readonlymode && curbuf->b_nwindows == 1)
7032 {
7033 /* When editing an already visited buffer, 'readonly' won't be set
7034 * but the previous value is kept. With ":view" and ":sview" we
7035 * want the file to be readonly, except when another window is
7036 * editing the same buffer. */
7037 curbuf->b_p_ro = TRUE;
7038 }
7039 readonlymode = n;
7040 }
7041 else
7042 {
7043 if (eap->do_ecmd_cmd != NULL)
7044 do_cmdline_cmd(eap->do_ecmd_cmd);
7045#ifdef FEAT_TITLE
7046 n = curwin->w_arg_idx_invalid;
7047#endif
7048 check_arg_idx(curwin);
7049#ifdef FEAT_TITLE
7050 if (n != curwin->w_arg_idx_invalid)
7051 maketitle();
7052#endif
7053 }
7054
7055#ifdef FEAT_WINDOWS
7056 /*
7057 * if ":split file" worked, set alternate file name in old window to new
7058 * file
7059 */
7060 if (old_curwin != NULL
7061 && *eap->arg != NUL
7062 && curwin != old_curwin
7063 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007064 && old_curwin->w_buffer != curbuf
7065 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 old_curwin->w_alt_fnum = curbuf->b_fnum;
7067#endif
7068
7069 ex_no_reprint = TRUE;
7070}
7071
7072#ifndef FEAT_GUI
7073/*
7074 * ":gui" and ":gvim" when there is no GUI.
7075 */
7076 static void
7077ex_nogui(eap)
7078 exarg_T *eap;
7079{
7080 eap->errmsg = e_nogvim;
7081}
7082#endif
7083
7084#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7085 static void
7086ex_tearoff(eap)
7087 exarg_T *eap;
7088{
7089 gui_make_tearoff(eap->arg);
7090}
7091#endif
7092
Bram Moolenaar843ee412004-06-30 16:16:41 +00007093#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_KDE) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 static void
7095ex_popup(eap)
7096 exarg_T *eap;
7097{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007098 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099}
7100#endif
7101
7102/*ARGSUSED*/
7103 static void
7104ex_swapname(eap)
7105 exarg_T *eap;
7106{
7107 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7108 MSG(_("No swap file"));
7109 else
7110 msg(curbuf->b_ml.ml_mfp->mf_fname);
7111}
7112
7113/*
7114 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7115 * offset.
7116 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7117 */
7118/*ARGSUSED*/
7119 static void
7120ex_syncbind(eap)
7121 exarg_T *eap;
7122{
7123#ifdef FEAT_SCROLLBIND
7124 win_T *wp;
7125 long topline;
7126 long y;
7127 linenr_T old_linenr = curwin->w_cursor.lnum;
7128
7129 setpcmark();
7130
7131 /*
7132 * determine max topline
7133 */
7134 if (curwin->w_p_scb)
7135 {
7136 topline = curwin->w_topline;
7137 for (wp = firstwin; wp; wp = wp->w_next)
7138 {
7139 if (wp->w_p_scb && wp->w_buffer)
7140 {
7141 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7142 if (topline > y)
7143 topline = y;
7144 }
7145 }
7146 if (topline < 1)
7147 topline = 1;
7148 }
7149 else
7150 {
7151 topline = 1;
7152 }
7153
7154
7155 /*
7156 * set all scrollbind windows to the same topline
7157 */
7158 wp = curwin;
7159 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7160 {
7161 if (curwin->w_p_scb)
7162 {
7163 y = topline - curwin->w_topline;
7164 if (y > 0)
7165 scrollup(y, TRUE);
7166 else
7167 scrolldown(-y, TRUE);
7168 curwin->w_scbind_pos = topline;
7169 redraw_later(VALID);
7170 cursor_correct();
7171#ifdef FEAT_WINDOWS
7172 curwin->w_redr_status = TRUE;
7173#endif
7174 }
7175 }
7176 curwin = wp;
7177 if (curwin->w_p_scb)
7178 {
7179 did_syncbind = TRUE;
7180 checkpcmark();
7181 if (old_linenr != curwin->w_cursor.lnum)
7182 {
7183 char_u ctrl_o[2];
7184
7185 ctrl_o[0] = Ctrl_O;
7186 ctrl_o[1] = 0;
7187 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7188 }
7189 }
7190#endif
7191}
7192
7193
7194 static void
7195ex_read(eap)
7196 exarg_T *eap;
7197{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007198 int i;
7199 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7200 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201
7202 if (eap->usefilter) /* :r!cmd */
7203 do_bang(1, eap, FALSE, FALSE, TRUE);
7204 else
7205 {
7206 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7207 return;
7208
7209#ifdef FEAT_BROWSE
7210 if (cmdmod.browse)
7211 {
7212 char_u *browseFile;
7213
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007214 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 NULL, NULL, NULL, curbuf);
7216 if (browseFile != NULL)
7217 {
7218 i = readfile(browseFile, NULL,
7219 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7220 vim_free(browseFile);
7221 }
7222 else
7223 i = OK;
7224 }
7225 else
7226#endif
7227 if (*eap->arg == NUL)
7228 {
7229 if (check_fname() == FAIL) /* check for no file name */
7230 return;
7231 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7232 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7233 }
7234 else
7235 {
7236 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7237 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7238 i = readfile(eap->arg, NULL,
7239 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7240
7241 }
7242 if (i == FAIL)
7243 {
7244#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7245 if (!aborting())
7246#endif
7247 EMSG2(_(e_notopen), eap->arg);
7248 }
7249 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007250 {
7251 if (empty && exmode_active)
7252 {
7253 /* Delete the empty line that remains. Historically ex does
7254 * this but vi doesn't. */
7255 if (eap->line2 == 0)
7256 lnum = curbuf->b_ml.ml_line_count;
7257 else
7258 lnum = 1;
7259 if (*ml_get(lnum) == NUL)
7260 {
7261 ml_delete(lnum, FALSE);
7262 deleted_lines_mark(lnum, 1L);
7263 if (curwin->w_cursor.lnum >= lnum)
7264 --curwin->w_cursor.lnum;
7265 }
7266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 }
7270}
7271
Bram Moolenaarea408852005-06-25 22:49:46 +00007272static char_u *prev_dir = NULL;
7273
7274#if defined(EXITFREE) || defined(PROTO)
7275 void
7276free_cd_dir()
7277{
7278 vim_free(prev_dir);
7279}
7280#endif
7281
7282
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283/*
7284 * ":cd", ":lcd", ":chdir" and ":lchdir".
7285 */
7286 static void
7287ex_cd(eap)
7288 exarg_T *eap;
7289{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 char_u *new_dir;
7291 char_u *tofree;
7292
7293 new_dir = eap->arg;
7294#if !defined(UNIX) && !defined(VMS)
7295 /* for non-UNIX ":cd" means: print current directory */
7296 if (*new_dir == NUL)
7297 ex_pwd(NULL);
7298 else
7299#endif
7300 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007301 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7302 && !eap->forceit)
7303 {
7304 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7305 return;
7306 }
7307
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 /* ":cd -": Change to previous directory */
7309 if (STRCMP(new_dir, "-") == 0)
7310 {
7311 if (prev_dir == NULL)
7312 {
7313 EMSG(_("E186: No previous directory"));
7314 return;
7315 }
7316 new_dir = prev_dir;
7317 }
7318
7319 /* Save current directory for next ":cd -" */
7320 tofree = prev_dir;
7321 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7322 prev_dir = vim_strsave(NameBuff);
7323 else
7324 prev_dir = NULL;
7325
7326#if defined(UNIX) || defined(VMS)
7327 /* for UNIX ":cd" means: go to home directory */
7328 if (*new_dir == NUL)
7329 {
7330 /* use NameBuff for home directory name */
7331# ifdef VMS
7332 char_u *p;
7333
7334 p = mch_getenv((char_u *)"SYS$LOGIN");
7335 if (p == NULL || *p == NUL) /* empty is the same as not set */
7336 NameBuff[0] = NUL;
7337 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007338 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339# else
7340 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7341# endif
7342 new_dir = NameBuff;
7343 }
7344#endif
7345 if (new_dir == NULL || vim_chdir(new_dir))
7346 EMSG(_(e_failed));
7347 else
7348 {
7349 vim_free(curwin->w_localdir);
7350 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7351 {
7352 /* If still in global directory, need to remember current
7353 * directory as global directory. */
7354 if (globaldir == NULL && prev_dir != NULL)
7355 globaldir = vim_strsave(prev_dir);
7356 /* Remember this local directory for the window. */
7357 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7358 curwin->w_localdir = vim_strsave(NameBuff);
7359 }
7360 else
7361 {
7362 /* We are now in the global directory, no need to remember its
7363 * name. */
7364 vim_free(globaldir);
7365 globaldir = NULL;
7366 curwin->w_localdir = NULL;
7367 }
7368
7369 shorten_fnames(TRUE);
7370
7371 /* Echo the new current directory if the command was typed. */
7372 if (KeyTyped)
7373 ex_pwd(eap);
7374 }
7375 vim_free(tofree);
7376 }
7377}
7378
7379/*
7380 * ":pwd".
7381 */
7382/*ARGSUSED*/
7383 static void
7384ex_pwd(eap)
7385 exarg_T *eap;
7386{
7387 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7388 {
7389#ifdef BACKSLASH_IN_FILENAME
7390 slash_adjust(NameBuff);
7391#endif
7392 msg(NameBuff);
7393 }
7394 else
7395 EMSG(_("E187: Unknown"));
7396}
7397
7398/*
7399 * ":=".
7400 */
7401 static void
7402ex_equal(eap)
7403 exarg_T *eap;
7404{
7405 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007406 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407}
7408
7409 static void
7410ex_sleep(eap)
7411 exarg_T *eap;
7412{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007413 int n;
7414 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415
7416 if (cursor_valid())
7417 {
7418 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7419 if (n >= 0)
7420 windgoto((int)n, curwin->w_wcol);
7421 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007422
7423 len = eap->line2;
7424 switch (*eap->arg)
7425 {
7426 case 'm': break;
7427 case NUL: len *= 1000L; break;
7428 default: EMSG2(_(e_invarg2), eap->arg); return;
7429 }
7430 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431}
7432
7433/*
7434 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7435 */
7436 void
7437do_sleep(msec)
7438 long msec;
7439{
7440 long done;
7441
7442 cursor_on();
7443 out_flush();
7444 for (done = 0; !got_int && done < msec; done += 1000L)
7445 {
7446 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7447 ui_breakcheck();
7448 }
7449}
7450
7451 static void
7452do_exmap(eap, isabbrev)
7453 exarg_T *eap;
7454 int isabbrev;
7455{
7456 int mode;
7457 char_u *cmdp;
7458
7459 cmdp = eap->cmd;
7460 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7461
7462 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7463 eap->arg, mode, isabbrev))
7464 {
7465 case 1: EMSG(_(e_invarg));
7466 break;
7467 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7468 break;
7469 }
7470}
7471
7472/*
7473 * ":winsize" command (obsolete).
7474 */
7475 static void
7476ex_winsize(eap)
7477 exarg_T *eap;
7478{
7479 int w, h;
7480 char_u *arg = eap->arg;
7481 char_u *p;
7482
7483 w = getdigits(&arg);
7484 arg = skipwhite(arg);
7485 p = arg;
7486 h = getdigits(&arg);
7487 if (*p != NUL && *arg == NUL)
7488 set_shellsize(w, h, TRUE);
7489 else
7490 EMSG(_("E465: :winsize requires two number arguments"));
7491}
7492
7493#ifdef FEAT_WINDOWS
7494 static void
7495ex_wincmd(eap)
7496 exarg_T *eap;
7497{
7498 int xchar = NUL;
7499 char_u *p;
7500
7501 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7502 {
7503 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7504 if (eap->arg[1] == NUL)
7505 {
7506 EMSG(_(e_invarg));
7507 return;
7508 }
7509 xchar = eap->arg[1];
7510 p = eap->arg + 2;
7511 }
7512 else
7513 p = eap->arg + 1;
7514
7515 eap->nextcmd = check_nextcmd(p);
7516 p = skipwhite(p);
7517 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7518 EMSG(_(e_invarg));
7519 else
7520 {
7521 /* Pass flags on for ":vertical wincmd ]". */
7522 postponed_split_flags = cmdmod.split;
7523 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7524 postponed_split_flags = 0;
7525 }
7526}
7527#endif
7528
Bram Moolenaar843ee412004-06-30 16:16:41 +00007529#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530/*
7531 * ":winpos".
7532 */
7533 static void
7534ex_winpos(eap)
7535 exarg_T *eap;
7536{
7537 int x, y;
7538 char_u *arg = eap->arg;
7539 char_u *p;
7540
7541 if (*arg == NUL)
7542 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007543# if defined(FEAT_GUI) || defined(MSWIN)
7544# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007546# else
7547 if (mch_get_winpos(&x, &y) != FAIL)
7548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 {
7550 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7551 msg(IObuff);
7552 }
7553 else
7554# endif
7555 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7556 }
7557 else
7558 {
7559 x = getdigits(&arg);
7560 arg = skipwhite(arg);
7561 p = arg;
7562 y = getdigits(&arg);
7563 if (*p == NUL || *arg != NUL)
7564 {
7565 EMSG(_("E466: :winpos requires two number arguments"));
7566 return;
7567 }
7568# ifdef FEAT_GUI
7569 if (gui.in_use)
7570 gui_mch_set_winpos(x, y);
7571 else if (gui.starting)
7572 {
7573 /* Remember the coordinates for when the window is opened. */
7574 gui_win_x = x;
7575 gui_win_y = y;
7576 }
7577# ifdef HAVE_TGETENT
7578 else
7579# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007580# else
7581# ifdef MSWIN
7582 mch_set_winpos(x, y);
7583# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584# endif
7585# ifdef HAVE_TGETENT
7586 if (*T_CWP)
7587 term_set_winpos(x, y);
7588# endif
7589 }
7590}
7591#endif
7592
7593/*
7594 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7595 */
7596 static void
7597ex_operators(eap)
7598 exarg_T *eap;
7599{
7600 oparg_T oa;
7601
7602 clear_oparg(&oa);
7603 oa.regname = eap->regname;
7604 oa.start.lnum = eap->line1;
7605 oa.end.lnum = eap->line2;
7606 oa.line_count = eap->line2 - eap->line1 + 1;
7607 oa.motion_type = MLINE;
7608#ifdef FEAT_VIRTUALEDIT
7609 virtual_op = FALSE;
7610#endif
7611 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7612 {
7613 setpcmark();
7614 curwin->w_cursor.lnum = eap->line1;
7615 beginline(BL_SOL | BL_FIX);
7616 }
7617
7618 switch (eap->cmdidx)
7619 {
7620 case CMD_delete:
7621 oa.op_type = OP_DELETE;
7622 op_delete(&oa);
7623 break;
7624
7625 case CMD_yank:
7626 oa.op_type = OP_YANK;
7627 (void)op_yank(&oa, FALSE, TRUE);
7628 break;
7629
7630 default: /* CMD_rshift or CMD_lshift */
7631 if ((eap->cmdidx == CMD_rshift)
7632#ifdef FEAT_RIGHTLEFT
7633 ^ curwin->w_p_rl
7634#endif
7635 )
7636 oa.op_type = OP_RSHIFT;
7637 else
7638 oa.op_type = OP_LSHIFT;
7639 op_shift(&oa, FALSE, eap->amount);
7640 break;
7641 }
7642#ifdef FEAT_VIRTUALEDIT
7643 virtual_op = MAYBE;
7644#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007645 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646}
7647
7648/*
7649 * ":put".
7650 */
7651 static void
7652ex_put(eap)
7653 exarg_T *eap;
7654{
7655 /* ":0put" works like ":1put!". */
7656 if (eap->line2 == 0)
7657 {
7658 eap->line2 = 1;
7659 eap->forceit = TRUE;
7660 }
7661 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007662 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7663 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664}
7665
7666/*
7667 * Handle ":copy" and ":move".
7668 */
7669 static void
7670ex_copymove(eap)
7671 exarg_T *eap;
7672{
7673 long n;
7674
7675 n = get_address(&eap->arg, FALSE, FALSE);
7676 if (eap->arg == NULL) /* error detected */
7677 {
7678 eap->nextcmd = NULL;
7679 return;
7680 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007681 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682
7683 /*
7684 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7685 */
7686 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7687 {
7688 EMSG(_(e_invaddr));
7689 return;
7690 }
7691
7692 if (eap->cmdidx == CMD_move)
7693 {
7694 if (do_move(eap->line1, eap->line2, n) == FAIL)
7695 return;
7696 }
7697 else
7698 ex_copy(eap->line1, eap->line2, n);
7699 u_clearline();
7700 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007701 ex_may_print(eap);
7702}
7703
7704/*
7705 * Print the current line if flags were given to the Ex command.
7706 */
7707 static void
7708ex_may_print(eap)
7709 exarg_T *eap;
7710{
7711 if (eap->flags != 0)
7712 {
7713 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7714 (eap->flags & EXFLAG_LIST));
7715 ex_no_reprint = TRUE;
7716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717}
7718
7719/*
7720 * ":smagic" and ":snomagic".
7721 */
7722 static void
7723ex_submagic(eap)
7724 exarg_T *eap;
7725{
7726 int magic_save = p_magic;
7727
7728 p_magic = (eap->cmdidx == CMD_smagic);
7729 do_sub(eap);
7730 p_magic = magic_save;
7731}
7732
7733/*
7734 * ":join".
7735 */
7736 static void
7737ex_join(eap)
7738 exarg_T *eap;
7739{
7740 curwin->w_cursor.lnum = eap->line1;
7741 if (eap->line1 == eap->line2)
7742 {
7743 if (eap->addr_count >= 2) /* :2,2join does nothing */
7744 return;
7745 if (eap->line2 == curbuf->b_ml.ml_line_count)
7746 {
7747 beep_flush();
7748 return;
7749 }
7750 ++eap->line2;
7751 }
7752 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
7753 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007754 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755}
7756
7757/*
7758 * ":[addr]@r" or ":[addr]*r": execute register
7759 */
7760 static void
7761ex_at(eap)
7762 exarg_T *eap;
7763{
7764 int c;
7765
7766 curwin->w_cursor.lnum = eap->line2;
7767
7768#ifdef USE_ON_FLY_SCROLL
7769 dont_scroll = TRUE; /* disallow scrolling here */
7770#endif
7771
7772 /* get the register name. No name means to use the previous one */
7773 c = *eap->arg;
7774 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7775 c = '@';
7776 /* put the register in mapbuf */
7777 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007778 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 else
7782 {
7783 int save_efr = exec_from_reg;
7784
7785 exec_from_reg = TRUE;
7786
7787 /*
7788 * Execute from the typeahead buffer.
7789 * Originally this didn't check for the typeahead buffer to be empty,
7790 * thus could read more Ex commands from stdin. It's not clear why,
7791 * it is certainly unexpected.
7792 */
7793 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
7794 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7795
7796 exec_from_reg = save_efr;
7797 }
7798}
7799
7800/*
7801 * ":!".
7802 */
7803 static void
7804ex_bang(eap)
7805 exarg_T *eap;
7806{
7807 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7808}
7809
7810/*
7811 * ":undo".
7812 */
7813/*ARGSUSED*/
7814 static void
7815ex_undo(eap)
7816 exarg_T *eap;
7817{
7818 u_undo(1);
7819}
7820
7821/*
7822 * ":redo".
7823 */
7824/*ARGSUSED*/
7825 static void
7826ex_redo(eap)
7827 exarg_T *eap;
7828{
7829 u_redo(1);
7830}
7831
7832/*
7833 * ":redir": start/stop redirection.
7834 */
7835 static void
7836ex_redir(eap)
7837 exarg_T *eap;
7838{
7839 char *mode;
7840 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007841 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842
7843 if (STRICMP(eap->arg, "END") == 0)
7844 close_redir();
7845 else
7846 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007847 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007849 ++arg;
7850 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007852 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 mode = "a";
7854 }
7855 else
7856 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007857 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858
7859 close_redir();
7860
7861 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00007862 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 if (fname == NULL)
7864 return;
7865#ifdef FEAT_BROWSE
7866 if (cmdmod.browse)
7867 {
7868 char_u *browseFile;
7869
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007870 browseFile = do_browse(BROWSE_SAVE,
7871 (char_u *)_("Save Redirection"),
7872 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 if (browseFile == NULL)
7874 return; /* operation cancelled */
7875 vim_free(fname);
7876 fname = browseFile;
7877 eap->forceit = TRUE; /* since dialog already asked */
7878 }
7879#endif
7880
7881 redir_fd = open_exfile(fname, eap->forceit, mode);
7882 vim_free(fname);
7883 }
7884#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007885 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
7887 /* redirect to a register a-z (resp. A-Z for appending) */
7888 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007889 ++arg;
7890 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007892 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007894 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007896 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00007897 if (*arg == '>' && arg[1] == '>')
7898 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007899 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
7900 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007902 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007904 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007906 if (*arg == '>')
7907 arg++;
7908
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 /* make register empty */
7910 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
7911 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007912 }
7913 if (*arg != NUL)
7914 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007915 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00007916 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007917 }
7918 }
7919 else if (*arg == '=' && arg[1] == '>')
7920 {
7921 int append;
7922
7923 /* redirect to a variable */
7924 close_redir();
7925 arg += 2;
7926
7927 if (*arg == '>')
7928 {
7929 ++arg;
7930 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 }
7932 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007933 append = FALSE;
7934
7935 if (var_redir_start(skipwhite(arg), append) == OK)
7936 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 }
7938#endif
7939
7940 /* TODO: redirect to a buffer */
7941
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 else
Bram Moolenaarca472992005-01-21 11:46:23 +00007943 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 }
7945}
7946
7947/*
7948 * ":redraw": force redraw
7949 */
7950 static void
7951ex_redraw(eap)
7952 exarg_T *eap;
7953{
7954 int r = RedrawingDisabled;
7955 int p = p_lz;
7956
7957 RedrawingDisabled = 0;
7958 p_lz = FALSE;
7959 update_topline();
7960 update_screen(eap->forceit ? CLEAR :
7961#ifdef FEAT_VISUAL
7962 VIsual_active ? INVERTED :
7963#endif
7964 0);
7965#ifdef FEAT_TITLE
7966 if (need_maketitle)
7967 maketitle();
7968#endif
7969 RedrawingDisabled = r;
7970 p_lz = p;
7971
7972 /* Reset msg_didout, so that a message that's there is overwritten. */
7973 msg_didout = FALSE;
7974 msg_col = 0;
7975
7976 out_flush();
7977}
7978
7979/*
7980 * ":redrawstatus": force redraw of status line(s)
7981 */
7982/*ARGSUSED*/
7983 static void
7984ex_redrawstatus(eap)
7985 exarg_T *eap;
7986{
7987#if defined(FEAT_WINDOWS)
7988 int r = RedrawingDisabled;
7989 int p = p_lz;
7990
7991 RedrawingDisabled = 0;
7992 p_lz = FALSE;
7993 if (eap->forceit)
7994 status_redraw_all();
7995 else
7996 status_redraw_curbuf();
7997 update_screen(
7998# ifdef FEAT_VISUAL
7999 VIsual_active ? INVERTED :
8000# endif
8001 0);
8002 RedrawingDisabled = r;
8003 p_lz = p;
8004 out_flush();
8005#endif
8006}
8007
8008 static void
8009close_redir()
8010{
8011 if (redir_fd != NULL)
8012 {
8013 fclose(redir_fd);
8014 redir_fd = NULL;
8015 }
8016#ifdef FEAT_EVAL
8017 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008018 if (redir_vname)
8019 {
8020 var_redir_stop();
8021 redir_vname = 0;
8022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023#endif
8024}
8025
8026#if defined(FEAT_SESSION) && defined(USE_CRNL)
8027# define MKSESSION_NL
8028static int mksession_nl = FALSE; /* use NL only in put_eol() */
8029#endif
8030
8031/*
8032 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8033 */
8034 static void
8035ex_mkrc(eap)
8036 exarg_T *eap;
8037{
8038 FILE *fd;
8039 int failed = FALSE;
8040 char_u *fname;
8041#ifdef FEAT_BROWSE
8042 char_u *browseFile = NULL;
8043#endif
8044#ifdef FEAT_SESSION
8045 int view_session = FALSE;
8046 int using_vdir = FALSE; /* using 'viewdir'? */
8047 char_u *viewFile = NULL;
8048 unsigned *flagp;
8049#endif
8050
8051 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8052 {
8053#ifdef FEAT_SESSION
8054 view_session = TRUE;
8055#else
8056 ex_ni(eap);
8057 return;
8058#endif
8059 }
8060
8061#ifdef FEAT_SESSION
8062 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8063 if (eap->cmdidx == CMD_mkview
8064 && (*eap->arg == NUL
8065 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8066 {
8067 eap->forceit = TRUE;
8068 fname = get_view_file(*eap->arg);
8069 if (fname == NULL)
8070 return;
8071 viewFile = fname;
8072 using_vdir = TRUE;
8073 }
8074 else
8075#endif
8076 if (*eap->arg != NUL)
8077 fname = eap->arg;
8078 else if (eap->cmdidx == CMD_mkvimrc)
8079 fname = (char_u *)VIMRC_FILE;
8080#ifdef FEAT_SESSION
8081 else if (eap->cmdidx == CMD_mksession)
8082 fname = (char_u *)SESSION_FILE;
8083#endif
8084 else
8085 fname = (char_u *)EXRC_FILE;
8086
8087#ifdef FEAT_BROWSE
8088 if (cmdmod.browse)
8089 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008090 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091# ifdef FEAT_SESSION
8092 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8093 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8094# endif
8095 (char_u *)_("Save Setup"),
8096 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8097 if (browseFile == NULL)
8098 goto theend;
8099 fname = browseFile;
8100 eap->forceit = TRUE; /* since dialog already asked */
8101 }
8102#endif
8103
8104#if defined(FEAT_SESSION) && defined(vim_mkdir)
8105 /* When using 'viewdir' may have to create the directory. */
8106 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008107 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108#endif
8109
8110 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8111 if (fd != NULL)
8112 {
8113#ifdef FEAT_SESSION
8114 if (eap->cmdidx == CMD_mkview)
8115 flagp = &vop_flags;
8116 else
8117 flagp = &ssop_flags;
8118#endif
8119
8120#ifdef MKSESSION_NL
8121 /* "unix" in 'sessionoptions': use NL line separator */
8122 if (view_session && (*flagp & SSOP_UNIX))
8123 mksession_nl = TRUE;
8124#endif
8125
8126 /* Write the version command for :mkvimrc */
8127 if (eap->cmdidx == CMD_mkvimrc)
8128 (void)put_line(fd, "version 6.0");
8129
8130#ifdef FEAT_SESSION
8131 if (eap->cmdidx != CMD_mkview)
8132#endif
8133 {
8134 /* Write setting 'compatible' first, because it has side effects.
8135 * For that same reason only do it when needed. */
8136 if (p_cp)
8137 (void)put_line(fd, "if !&cp | set cp | endif");
8138 else
8139 (void)put_line(fd, "if &cp | set nocp | endif");
8140 }
8141
8142#ifdef FEAT_SESSION
8143 if (!view_session
8144 || (eap->cmdidx == CMD_mksession
8145 && (*flagp & SSOP_OPTIONS)))
8146#endif
8147 failed |= (makemap(fd, NULL) == FAIL
8148 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8149
8150#ifdef FEAT_SESSION
8151 if (!failed && view_session)
8152 {
8153 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8154 failed = TRUE;
8155 if (eap->cmdidx == CMD_mksession)
8156 {
8157 char_u dirnow[MAXPATHL]; /* current directory */
8158
8159 /*
8160 * Change to session file's dir.
8161 */
8162 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8163 || mch_chdir((char *)dirnow) != 0)
8164 *dirnow = NUL;
8165 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8166 {
8167 if (vim_chdirfile(fname) == OK)
8168 shorten_fnames(TRUE);
8169 }
8170 else if (*dirnow != NUL
8171 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8172 {
8173 (void)mch_chdir((char *)globaldir);
8174 shorten_fnames(TRUE);
8175 }
8176
8177 failed |= (makeopens(fd, dirnow) == FAIL);
8178
8179 /* restore original dir */
8180 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8181 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8182 {
8183 if (mch_chdir((char *)dirnow) != 0)
8184 EMSG(_(e_prev_dir));
8185 shorten_fnames(TRUE);
8186 }
8187 }
8188 else
8189 {
8190 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8191 }
8192 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8193 == FAIL)
8194 failed = TRUE;
8195 }
8196#endif
8197 failed |= fclose(fd);
8198
8199 if (failed)
8200 EMSG(_(e_write));
8201#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8202 else if (eap->cmdidx == CMD_mksession)
8203 {
8204 /* successful session write - set this_session var */
8205 char_u tbuf[MAXPATHL];
8206
8207 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8208 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8209 }
8210#endif
8211#ifdef MKSESSION_NL
8212 mksession_nl = FALSE;
8213#endif
8214 }
8215
8216#ifdef FEAT_BROWSE
8217theend:
8218 vim_free(browseFile);
8219#endif
8220#ifdef FEAT_SESSION
8221 vim_free(viewFile);
8222#endif
8223}
8224
Bram Moolenaardf177f62005-02-22 08:39:57 +00008225#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8226 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008227/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008228 int
8229vim_mkdir_emsg(name, prot)
8230 char_u *name;
8231 int prot;
8232{
8233 if (vim_mkdir(name, prot) != 0)
8234 {
8235 EMSG2(_("E739: Cannot create directory: %s"), name);
8236 return FAIL;
8237 }
8238 return OK;
8239}
8240#endif
8241
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242/*
8243 * Open a file for writing for an Ex command, with some checks.
8244 * Return file descriptor, or NULL on failure.
8245 */
8246 FILE *
8247open_exfile(fname, forceit, mode)
8248 char_u *fname;
8249 int forceit;
8250 char *mode; /* "w" for create new file or "a" for append */
8251{
8252 FILE *fd;
8253
8254#ifdef UNIX
8255 /* with Unix it is possible to open a directory */
8256 if (mch_isdir(fname))
8257 {
8258 EMSG2(_(e_isadir2), fname);
8259 return NULL;
8260 }
8261#endif
8262 if (!forceit && *mode != 'a' && vim_fexists(fname))
8263 {
8264 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8265 return NULL;
8266 }
8267
8268 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8269 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8270
8271 return fd;
8272}
8273
8274/*
8275 * ":mark" and ":k".
8276 */
8277 static void
8278ex_mark(eap)
8279 exarg_T *eap;
8280{
8281 pos_T pos;
8282
8283 if (*eap->arg == NUL) /* No argument? */
8284 EMSG(_(e_argreq));
8285 else if (eap->arg[1] != NUL) /* more than one character? */
8286 EMSG(_(e_trailing));
8287 else
8288 {
8289 pos = curwin->w_cursor; /* save curwin->w_cursor */
8290 curwin->w_cursor.lnum = eap->line2;
8291 beginline(BL_WHITE | BL_FIX);
8292 if (setmark(*eap->arg) == FAIL) /* set mark */
8293 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8294 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8295 }
8296}
8297
8298/*
8299 * Update w_topline, w_leftcol and the cursor position.
8300 */
8301 void
8302update_topline_cursor()
8303{
8304 check_cursor(); /* put cursor on valid line */
8305 update_topline();
8306 if (!curwin->w_p_wrap)
8307 validate_cursor();
8308 update_curswant();
8309}
8310
8311#ifdef FEAT_EX_EXTRA
8312/*
8313 * ":normal[!] {commands}": Execute normal mode commands.
8314 */
8315 static void
8316ex_normal(eap)
8317 exarg_T *eap;
8318{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 int save_msg_scroll = msg_scroll;
8320 int save_restart_edit = restart_edit;
8321 int save_msg_didout = msg_didout;
8322 int save_State = State;
8323 tasave_T tabuf;
8324 int save_insertmode = p_im;
8325 int save_finish_op = finish_op;
8326#ifdef FEAT_MBYTE
8327 char_u *arg = NULL;
8328 int l;
8329 char_u *p;
8330#endif
8331
8332 if (ex_normal_busy >= p_mmd)
8333 {
8334 EMSG(_("E192: Recursive use of :normal too deep"));
8335 return;
8336 }
8337 ++ex_normal_busy;
8338
8339 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8340 restart_edit = 0; /* don't go to Insert mode */
8341 p_im = FALSE; /* don't use 'insertmode' */
8342
8343#ifdef FEAT_MBYTE
8344 /*
8345 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8346 * this for the K_SPECIAL leading byte, otherwise special keys will not
8347 * work.
8348 */
8349 if (has_mbyte)
8350 {
8351 int len = 0;
8352
8353 /* Count the number of characters to be escaped. */
8354 for (p = eap->arg; *p != NUL; ++p)
8355 {
8356# ifdef FEAT_GUI
8357 if (*p == CSI) /* leadbyte CSI */
8358 len += 2;
8359# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008360 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8362# ifdef FEAT_GUI
8363 || *p == CSI
8364# endif
8365 )
8366 len += 2;
8367 }
8368 if (len > 0)
8369 {
8370 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8371 if (arg != NULL)
8372 {
8373 len = 0;
8374 for (p = eap->arg; *p != NUL; ++p)
8375 {
8376 arg[len++] = *p;
8377# ifdef FEAT_GUI
8378 if (*p == CSI)
8379 {
8380 arg[len++] = KS_EXTRA;
8381 arg[len++] = (int)KE_CSI;
8382 }
8383# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008384 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {
8386 arg[len++] = *++p;
8387 if (*p == K_SPECIAL)
8388 {
8389 arg[len++] = KS_SPECIAL;
8390 arg[len++] = KE_FILLER;
8391 }
8392# ifdef FEAT_GUI
8393 else if (*p == CSI)
8394 {
8395 arg[len++] = KS_EXTRA;
8396 arg[len++] = (int)KE_CSI;
8397 }
8398# endif
8399 }
8400 arg[len] = NUL;
8401 }
8402 }
8403 }
8404 }
8405#endif
8406
8407 /*
8408 * Save the current typeahead. This is required to allow using ":normal"
8409 * from an event handler and makes sure we don't hang when the argument
8410 * ends with half a command.
8411 */
8412 save_typeahead(&tabuf);
8413 if (tabuf.typebuf_valid)
8414 {
8415 /*
8416 * Repeat the :normal command for each line in the range. When no
8417 * range given, execute it just once, without positioning the cursor
8418 * first.
8419 */
8420 do
8421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 if (eap->addr_count != 0)
8423 {
8424 curwin->w_cursor.lnum = eap->line1++;
8425 curwin->w_cursor.col = 0;
8426 }
8427
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008428 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429#ifdef FEAT_MBYTE
8430 arg != NULL ? arg :
8431#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008432 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 }
8434 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8435 }
8436
8437 /* Might not return to the main loop when in an event handler. */
8438 update_topline_cursor();
8439
8440 /* Restore the previous typeahead. */
8441 restore_typeahead(&tabuf);
8442
8443 --ex_normal_busy;
8444 msg_scroll = save_msg_scroll;
8445 restart_edit = save_restart_edit;
8446 p_im = save_insertmode;
8447 finish_op = save_finish_op;
8448 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8449
8450 /* Restore the state (needed when called from a function executed for
8451 * 'indentexpr'). */
8452 State = save_State;
8453#ifdef FEAT_MBYTE
8454 vim_free(arg);
8455#endif
8456}
8457
8458/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008459 * ":startinsert" and ":startreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 */
8461 static void
8462ex_startinsert(eap)
8463 exarg_T *eap;
8464{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008465 if (eap->forceit)
8466 {
8467 coladvance((colnr_T)MAXCOL);
8468 curwin->w_curswant = MAXCOL;
8469 curwin->w_set_curswant = FALSE;
8470 }
8471
Bram Moolenaara40c5002005-01-09 21:16:21 +00008472 /* Ignore the command when already in Insert mode. Inserting an
8473 * expression register that invokes a function can do this. */
8474 if (State & INSERT)
8475 return;
8476
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 if (eap->forceit)
8478 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008479 if (eap->cmdidx == CMD_startinsert)
8480 restart_edit = 'a';
8481 else
8482 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 }
8484 else
8485 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008486 if (eap->cmdidx == CMD_startinsert)
8487 restart_edit = 'i';
8488 else
8489 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 curwin->w_curswant = 0; /* avoid MAXCOL */
8491 }
8492}
8493
8494/*
8495 * ":stopinsert"
8496 */
8497/*ARGSUSED*/
8498 static void
8499ex_stopinsert(eap)
8500 exarg_T *eap;
8501{
8502 restart_edit = 0;
8503 stop_insert_mode = TRUE;
8504}
8505#endif
8506
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008507#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8508/*
8509 * Execute normal mode command "cmd".
8510 * "remap" can be REMAP_NONE or REMAP_YES.
8511 */
8512 void
8513exec_normal_cmd(cmd, remap, silent)
8514 char_u *cmd;
8515 int remap;
8516 int silent;
8517{
8518 oparg_T oa;
8519
8520 /*
8521 * Stuff the argument into the typeahead buffer.
8522 * Execute normal_cmd() until there is no typeahead left.
8523 */
8524 clear_oparg(&oa);
8525 finish_op = FALSE;
8526 ins_typebuf(cmd, remap, 0, TRUE, silent);
8527 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8528 && !got_int)
8529 {
8530 update_topline_cursor();
8531 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8532 }
8533}
8534#endif
8535
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536#ifdef FEAT_FIND_ID
8537 static void
8538ex_checkpath(eap)
8539 exarg_T *eap;
8540{
8541 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8542 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8543 (linenr_T)1, (linenr_T)MAXLNUM);
8544}
8545
8546#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8547/*
8548 * ":psearch"
8549 */
8550 static void
8551ex_psearch(eap)
8552 exarg_T *eap;
8553{
8554 g_do_tagpreview = p_pvh;
8555 ex_findpat(eap);
8556 g_do_tagpreview = 0;
8557}
8558#endif
8559
8560 static void
8561ex_findpat(eap)
8562 exarg_T *eap;
8563{
8564 int whole = TRUE;
8565 long n;
8566 char_u *p;
8567 int action;
8568
8569 switch (cmdnames[eap->cmdidx].cmd_name[2])
8570 {
8571 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8572 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8573 action = ACTION_GOTO;
8574 else
8575 action = ACTION_SHOW;
8576 break;
8577 case 'i': /* ":ilist" and ":dlist" */
8578 action = ACTION_SHOW_ALL;
8579 break;
8580 case 'u': /* ":ijump" and ":djump" */
8581 action = ACTION_GOTO;
8582 break;
8583 default: /* ":isplit" and ":dsplit" */
8584 action = ACTION_SPLIT;
8585 break;
8586 }
8587
8588 n = 1;
8589 if (vim_isdigit(*eap->arg)) /* get count */
8590 {
8591 n = getdigits(&eap->arg);
8592 eap->arg = skipwhite(eap->arg);
8593 }
8594 if (*eap->arg == '/') /* Match regexp, not just whole words */
8595 {
8596 whole = FALSE;
8597 ++eap->arg;
8598 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8599 if (*p)
8600 {
8601 *p++ = NUL;
8602 p = skipwhite(p);
8603
8604 /* Check for trailing illegal characters */
8605 if (!ends_excmd(*p))
8606 eap->errmsg = e_trailing;
8607 else
8608 eap->nextcmd = check_nextcmd(p);
8609 }
8610 }
8611 if (!eap->skip)
8612 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8613 whole, !eap->forceit,
8614 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8615 n, action, eap->line1, eap->line2);
8616}
8617#endif
8618
8619#ifdef FEAT_WINDOWS
8620
8621# ifdef FEAT_QUICKFIX
8622/*
8623 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8624 */
8625 static void
8626ex_ptag(eap)
8627 exarg_T *eap;
8628{
8629 g_do_tagpreview = p_pvh;
8630 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8631}
8632
8633/*
8634 * ":pedit"
8635 */
8636 static void
8637ex_pedit(eap)
8638 exarg_T *eap;
8639{
8640 win_T *curwin_save = curwin;
8641
8642 g_do_tagpreview = p_pvh;
8643 prepare_tagpreview();
8644 keep_help_flag = curwin_save->w_buffer->b_help;
8645 do_exedit(eap, NULL);
8646 keep_help_flag = FALSE;
8647 if (curwin != curwin_save && win_valid(curwin_save))
8648 {
8649 /* Return cursor to where we were */
8650 validate_cursor();
8651 redraw_later(VALID);
8652 win_enter(curwin_save, TRUE);
8653 }
8654 g_do_tagpreview = 0;
8655}
8656# endif
8657
8658/*
8659 * ":stag", ":stselect" and ":stjump".
8660 */
8661 static void
8662ex_stag(eap)
8663 exarg_T *eap;
8664{
8665 postponed_split = -1;
8666 postponed_split_flags = cmdmod.split;
8667 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8668 postponed_split_flags = 0;
8669}
8670#endif
8671
8672/*
8673 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8674 */
8675 static void
8676ex_tag(eap)
8677 exarg_T *eap;
8678{
8679 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8680}
8681
8682 static void
8683ex_tag_cmd(eap, name)
8684 exarg_T *eap;
8685 char_u *name;
8686{
8687 int cmd;
8688
8689 switch (name[1])
8690 {
8691 case 'j': cmd = DT_JUMP; /* ":tjump" */
8692 break;
8693 case 's': cmd = DT_SELECT; /* ":tselect" */
8694 break;
8695 case 'p': cmd = DT_PREV; /* ":tprevious" */
8696 break;
8697 case 'N': cmd = DT_PREV; /* ":tNext" */
8698 break;
8699 case 'n': cmd = DT_NEXT; /* ":tnext" */
8700 break;
8701 case 'o': cmd = DT_POP; /* ":pop" */
8702 break;
8703 case 'f': /* ":tfirst" */
8704 case 'r': cmd = DT_FIRST; /* ":trewind" */
8705 break;
8706 case 'l': cmd = DT_LAST; /* ":tlast" */
8707 break;
8708 default: /* ":tag" */
8709#ifdef FEAT_CSCOPE
8710 if (p_cst)
8711 {
8712 do_cstag(eap);
8713 return;
8714 }
8715#endif
8716 cmd = DT_TAG;
8717 break;
8718 }
8719
8720 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8721 eap->forceit, TRUE);
8722}
8723
8724/*
8725 * Evaluate cmdline variables.
8726 *
8727 * change '%' to curbuf->b_ffname
8728 * '#' to curwin->w_altfile
8729 * '<cword>' to word under the cursor
8730 * '<cWORD>' to WORD under the cursor
8731 * '<cfile>' to path name under the cursor
8732 * '<sfile>' to sourced file name
8733 * '<afile>' to file name for autocommand
8734 * '<abuf>' to buffer number for autocommand
8735 * '<amatch>' to matching name for autocommand
8736 *
8737 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8738 * "" for error without a message) and NULL is returned.
8739 * Returns an allocated string if a valid match was found.
8740 * Returns NULL if no match was found. "usedlen" then still contains the
8741 * number of characters to skip.
8742 */
8743 char_u *
8744eval_vars(src, usedlen, lnump, errormsg, srcstart)
8745 char_u *src; /* pointer into commandline */
8746 int *usedlen; /* characters after src that are used */
8747 linenr_T *lnump; /* line number for :e command, or NULL */
8748 char_u **errormsg; /* pointer to error message */
8749 char_u *srcstart; /* beginning of valid memory for src */
8750{
8751 int i;
8752 char_u *s;
8753 char_u *result;
8754 char_u *resultbuf = NULL;
8755 int resultlen;
8756 buf_T *buf;
8757 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
8758 int spec_idx;
8759#ifdef FEAT_MODIFY_FNAME
8760 int skip_mod = FALSE;
8761#endif
8762 static char *(spec_str[]) =
8763 {
8764 "%",
8765#define SPEC_PERC 0
8766 "#",
8767#define SPEC_HASH 1
8768 "<cword>", /* cursor word */
8769#define SPEC_CWORD 2
8770 "<cWORD>", /* cursor WORD */
8771#define SPEC_CCWORD 3
8772 "<cfile>", /* cursor path name */
8773#define SPEC_CFILE 4
8774 "<sfile>", /* ":so" file name */
8775#define SPEC_SFILE 5
8776#ifdef FEAT_AUTOCMD
8777 "<afile>", /* autocommand file name */
8778# define SPEC_AFILE 6
8779 "<abuf>", /* autocommand buffer number */
8780# define SPEC_ABUF 7
8781 "<amatch>", /* autocommand match name */
8782# define SPEC_AMATCH 8
8783#endif
8784#ifdef FEAT_CLIENTSERVER
8785 "<client>"
8786# define SPEC_CLIENT 9
8787#endif
8788 };
8789#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
8790
8791#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
8792 char_u strbuf[30];
8793#endif
8794
8795 *errormsg = NULL;
8796
8797 /*
8798 * Check if there is something to do.
8799 */
8800 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
8801 {
8802 *usedlen = (int)STRLEN(spec_str[spec_idx]);
8803 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
8804 break;
8805 }
8806 if (spec_idx == SPEC_COUNT) /* no match */
8807 {
8808 *usedlen = 1;
8809 return NULL;
8810 }
8811
8812 /*
8813 * Skip when preceded with a backslash "\%" and "\#".
8814 * Note: In "\\%" the % is also not recognized!
8815 */
8816 if (src > srcstart && src[-1] == '\\')
8817 {
8818 *usedlen = 0;
8819 STRCPY(src - 1, src); /* remove backslash */
8820 return NULL;
8821 }
8822
8823 /*
8824 * word or WORD under cursor
8825 */
8826 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
8827 {
8828 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
8829 (FIND_IDENT|FIND_STRING) : FIND_STRING);
8830 if (resultlen == 0)
8831 {
8832 *errormsg = (char_u *)"";
8833 return NULL;
8834 }
8835 }
8836
8837 /*
8838 * '#': Alternate file name
8839 * '%': Current file name
8840 * File name under the cursor
8841 * File name for autocommand
8842 * and following modifiers
8843 */
8844 else
8845 {
8846 switch (spec_idx)
8847 {
8848 case SPEC_PERC: /* '%': current file */
8849 if (curbuf->b_fname == NULL)
8850 {
8851 result = (char_u *)"";
8852 valid = 0; /* Must have ":p:h" to be valid */
8853 }
8854 else
8855#ifdef RISCOS
8856 /* Always use the full path for RISC OS if possible. */
8857 result = curbuf->b_ffname;
8858 if (result == NULL)
8859 result = curbuf->b_fname;
8860#else
8861 result = curbuf->b_fname;
8862#endif
8863 break;
8864
8865 case SPEC_HASH: /* '#' or "#99": alternate file */
8866 if (src[1] == '#') /* "##": the argument list */
8867 {
8868 result = arg_all();
8869 resultbuf = result;
8870 *usedlen = 2;
8871#ifdef FEAT_MODIFY_FNAME
8872 skip_mod = TRUE;
8873#endif
8874 break;
8875 }
8876 s = src + 1;
8877 i = (int)getdigits(&s);
8878 *usedlen = (int)(s - src); /* length of what we expand */
8879
8880 buf = buflist_findnr(i);
8881 if (buf == NULL)
8882 {
8883 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
8884 return NULL;
8885 }
8886 if (lnump != NULL)
8887 *lnump = ECMD_LAST;
8888 if (buf->b_fname == NULL)
8889 {
8890 result = (char_u *)"";
8891 valid = 0; /* Must have ":p:h" to be valid */
8892 }
8893 else
8894 result = buf->b_fname;
8895 break;
8896
8897#ifdef FEAT_SEARCHPATH
8898 case SPEC_CFILE: /* file name under cursor */
8899 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L);
8900 if (result == NULL)
8901 {
8902 *errormsg = (char_u *)"";
8903 return NULL;
8904 }
8905 resultbuf = result; /* remember allocated string */
8906 break;
8907#endif
8908
8909#ifdef FEAT_AUTOCMD
8910 case SPEC_AFILE: /* file name for autocommand */
8911 result = autocmd_fname;
8912 if (result == NULL)
8913 {
8914 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
8915 return NULL;
8916 }
8917 break;
8918
8919 case SPEC_ABUF: /* buffer number for autocommand */
8920 if (autocmd_bufnr <= 0)
8921 {
8922 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
8923 return NULL;
8924 }
8925 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8926 result = strbuf;
8927 break;
8928
8929 case SPEC_AMATCH: /* match name for autocommand */
8930 result = autocmd_match;
8931 if (result == NULL)
8932 {
8933 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
8934 return NULL;
8935 }
8936 break;
8937
8938#endif
8939 case SPEC_SFILE: /* file name for ":so" command */
8940 result = sourcing_name;
8941 if (result == NULL)
8942 {
8943 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
8944 return NULL;
8945 }
8946 break;
8947#if defined(FEAT_CLIENTSERVER)
8948 case SPEC_CLIENT: /* Source of last submitted input */
8949 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
8950 result = strbuf;
8951 break;
8952#endif
8953 }
8954
8955 resultlen = (int)STRLEN(result); /* length of new string */
8956 if (src[*usedlen] == '<') /* remove the file name extension */
8957 {
8958 ++*usedlen;
8959#ifdef RISCOS
8960 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
8961#else
8962 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
8963#endif
8964 resultlen = (int)(s - result);
8965 }
8966#ifdef FEAT_MODIFY_FNAME
8967 else if (!skip_mod)
8968 {
8969 valid |= modify_fname(src, usedlen, &result, &resultbuf,
8970 &resultlen);
8971 if (result == NULL)
8972 {
8973 *errormsg = (char_u *)"";
8974 return NULL;
8975 }
8976 }
8977#endif
8978 }
8979
8980 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8981 {
8982 if (valid != VALID_HEAD + VALID_PATH)
8983 /* xgettext:no-c-format */
8984 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
8985 else
8986 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
8987 result = NULL;
8988 }
8989 else
8990 result = vim_strnsave(result, resultlen);
8991 vim_free(resultbuf);
8992 return result;
8993}
8994
8995/*
8996 * Concatenate all files in the argument list, separated by spaces, and return
8997 * it in one allocated string.
8998 * Spaces and backslashes in the file names are escaped with a backslash.
8999 * Returns NULL when out of memory.
9000 */
9001 static char_u *
9002arg_all()
9003{
9004 int len;
9005 int idx;
9006 char_u *retval = NULL;
9007 char_u *p;
9008
9009 /*
9010 * Do this loop two times:
9011 * first time: compute the total length
9012 * second time: concatenate the names
9013 */
9014 for (;;)
9015 {
9016 len = 0;
9017 for (idx = 0; idx < ARGCOUNT; ++idx)
9018 {
9019 p = alist_name(&ARGLIST[idx]);
9020 if (p != NULL)
9021 {
9022 if (len > 0)
9023 {
9024 /* insert a space in between names */
9025 if (retval != NULL)
9026 retval[len] = ' ';
9027 ++len;
9028 }
9029 for ( ; *p != NUL; ++p)
9030 {
9031 if (*p == ' ' || *p == '\\')
9032 {
9033 /* insert a backslash */
9034 if (retval != NULL)
9035 retval[len] = '\\';
9036 ++len;
9037 }
9038 if (retval != NULL)
9039 retval[len] = *p;
9040 ++len;
9041 }
9042 }
9043 }
9044
9045 /* second time: break here */
9046 if (retval != NULL)
9047 {
9048 retval[len] = NUL;
9049 break;
9050 }
9051
9052 /* allocate memory */
9053 retval = alloc(len + 1);
9054 if (retval == NULL)
9055 break;
9056 }
9057
9058 return retval;
9059}
9060
9061#if defined(FEAT_AUTOCMD) || defined(PROTO)
9062/*
9063 * Expand the <sfile> string in "arg".
9064 *
9065 * Returns an allocated string, or NULL for any error.
9066 */
9067 char_u *
9068expand_sfile(arg)
9069 char_u *arg;
9070{
9071 char_u *errormsg;
9072 int len;
9073 char_u *result;
9074 char_u *newres;
9075 char_u *repl;
9076 int srclen;
9077 char_u *p;
9078
9079 result = vim_strsave(arg);
9080 if (result == NULL)
9081 return NULL;
9082
9083 for (p = result; *p; )
9084 {
9085 if (STRNCMP(p, "<sfile>", 7) != 0)
9086 ++p;
9087 else
9088 {
9089 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9090 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9091 if (errormsg != NULL)
9092 {
9093 if (*errormsg)
9094 emsg(errormsg);
9095 vim_free(result);
9096 return NULL;
9097 }
9098 if (repl == NULL) /* no match (cannot happen) */
9099 {
9100 p += srclen;
9101 continue;
9102 }
9103 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9104 newres = alloc(len);
9105 if (newres == NULL)
9106 {
9107 vim_free(repl);
9108 vim_free(result);
9109 return NULL;
9110 }
9111 mch_memmove(newres, result, (size_t)(p - result));
9112 STRCPY(newres + (p - result), repl);
9113 len = (int)STRLEN(newres);
9114 STRCAT(newres, p + srclen);
9115 vim_free(repl);
9116 vim_free(result);
9117 result = newres;
9118 p = newres + len; /* continue after the match */
9119 }
9120 }
9121
9122 return result;
9123}
9124#endif
9125
9126#ifdef FEAT_SESSION
9127static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9128static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9129static frame_T *ses_skipframe __ARGS((frame_T *fr));
9130static int ses_do_frame __ARGS((frame_T *fr));
9131static int ses_do_win __ARGS((win_T *wp));
9132static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9133static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9134static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9135
9136/*
9137 * Write openfile commands for the current buffers to an .exrc file.
9138 * Return FAIL on error, OK otherwise.
9139 */
9140 static int
9141makeopens(fd, dirnow)
9142 FILE *fd;
9143 char_u *dirnow; /* Current directory name */
9144{
9145 buf_T *buf;
9146 int only_save_windows = TRUE;
9147 int nr;
9148 int cnr = 1;
9149 int restore_size = TRUE;
9150 win_T *wp;
9151 char_u *sname;
9152 win_T *edited_win = NULL;
9153
9154 if (ssop_flags & SSOP_BUFFERS)
9155 only_save_windows = FALSE; /* Save ALL buffers */
9156
9157 /*
9158 * Begin by setting the this_session variable, and then other
9159 * sessionable variables.
9160 */
9161#ifdef FEAT_EVAL
9162 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9163 return FAIL;
9164 if (ssop_flags & SSOP_GLOBALS)
9165 if (store_session_globals(fd) == FAIL)
9166 return FAIL;
9167#endif
9168
9169 /*
9170 * Close all windows but one.
9171 */
9172 if (put_line(fd, "silent only") == FAIL)
9173 return FAIL;
9174
9175 /*
9176 * Now a :cd command to the session directory or the current directory
9177 */
9178 if (ssop_flags & SSOP_SESDIR)
9179 {
9180 if (put_line(fd, "exe \"cd \" . expand(\"<sfile>:p:h\")") == FAIL)
9181 return FAIL;
9182 }
9183 else if (ssop_flags & SSOP_CURDIR)
9184 {
9185 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9186 if (sname == NULL
9187 || fprintf(fd, "cd %s", sname) < 0 || put_eol(fd) == FAIL)
9188 return FAIL;
9189 vim_free(sname);
9190 }
9191
9192 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009193 * If there is an empty, unnamed buffer we will wipe it out later.
9194 * Remember the buffer number.
9195 */
9196 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9197 return FAIL;
9198 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9199 return FAIL;
9200 if (put_line(fd, "endif") == FAIL)
9201 return FAIL;
9202
9203 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 * Now save the current files, current buffer first.
9205 */
9206 if (put_line(fd, "set shortmess=aoO") == FAIL)
9207 return FAIL;
9208
9209 /* Now put the other buffers into the buffer list */
9210 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9211 {
9212 if (!(only_save_windows && buf->b_nwindows == 0)
9213 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9214 && buf->b_fname != NULL
9215 && buf->b_p_bl)
9216 {
9217 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9218 : buf->b_wininfo->wi_fpos.lnum) < 0
9219 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9220 return FAIL;
9221 }
9222 }
9223
9224 /* the global argument list */
9225 if (ses_arglist(fd, "args", &global_alist.al_ga,
9226 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9227 return FAIL;
9228
9229 if (ssop_flags & SSOP_RESIZE)
9230 {
9231 /* Note: after the restore we still check it worked!*/
9232 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9233 || put_eol(fd) == FAIL)
9234 return FAIL;
9235 }
9236
9237#ifdef FEAT_GUI
9238 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9239 {
9240 int x, y;
9241
9242 if (gui_mch_get_winpos(&x, &y) == OK)
9243 {
9244 /* Note: after the restore we still check it worked!*/
9245 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9246 return FAIL;
9247 }
9248 }
9249#endif
9250
9251 /*
9252 * Before creating the window layout, try loading one file. If this is
9253 * aborted we don't end up with a number of useless windows.
9254 * This may have side effects! (e.g., compressed or network file).
9255 */
9256 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9257 {
9258 if (ses_do_win(wp)
9259 && wp->w_buffer->b_ffname != NULL
9260 && !wp->w_buffer->b_help
9261#ifdef FEAT_QUICKFIX
9262 && !bt_nofile(wp->w_buffer)
9263#endif
9264 )
9265 {
9266 if (fputs("edit ", fd) < 0
9267 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9268 return FAIL;
9269 if (!wp->w_arg_idx_invalid)
9270 edited_win = wp;
9271 break;
9272 }
9273 }
9274
9275 /*
9276 * Save current window layout.
9277 */
9278 if (put_line(fd, "set splitbelow splitright") == FAIL)
9279 return FAIL;
9280 if (ses_win_rec(fd, topframe) == FAIL)
9281 return FAIL;
9282 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9283 return FAIL;
9284 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9285 return FAIL;
9286
9287 /*
9288 * Check if window sizes can be restored (no windows omitted).
9289 * Remember the window number of the current window after restoring.
9290 */
9291 nr = 0;
9292 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9293 {
9294 if (ses_do_win(wp))
9295 ++nr;
9296 else
9297 restore_size = FALSE;
9298 if (curwin == wp)
9299 cnr = nr;
9300 }
9301
9302 /* Go to the first window. */
9303 if (put_line(fd, "wincmd t") == FAIL)
9304 return FAIL;
9305
9306 /*
9307 * If more than one window, see if sizes can be restored.
9308 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9309 * resized when moving between windows.
9310 * Do this before restoring the view, so that the topline and the cursor
9311 * can be set. This is done again below.
9312 */
9313 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9314 return FAIL;
9315 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9316 return FAIL;
9317
9318 /*
9319 * Restore the view of the window (options, file, cursor, etc.).
9320 */
9321 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9322 {
9323 if (!ses_do_win(wp))
9324 continue;
9325 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9326 return FAIL;
9327 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9328 return FAIL;
9329 }
9330
9331 /*
9332 * Restore cursor to the current window if it's not the first one.
9333 */
9334 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9335 return FAIL;
9336
9337 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009338 * Wipe out an empty unnamed buffer we started in.
9339 */
9340 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9341 return FAIL;
9342 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9343 return FAIL;
9344 if (put_line(fd, "endif") == FAIL)
9345 return FAIL;
9346 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9347 return FAIL;
9348
9349 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 * Restore window sizes again after jumping around in windows, because the
9351 * current window has a minimum size while others may not.
9352 */
9353 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9354 return FAIL;
9355
9356 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9357 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9358 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9359 return FAIL;
9360
9361 /*
9362 * Lastly, execute the x.vim file if it exists.
9363 */
9364 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9365 || put_line(fd, "if file_readable(s:sx)") == FAIL
9366 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9367 || put_line(fd, "endif") == FAIL)
9368 return FAIL;
9369
9370 return OK;
9371}
9372
9373 static int
9374ses_winsizes(fd, restore_size)
9375 FILE *fd;
9376 int restore_size;
9377{
9378 int n = 0;
9379 win_T *wp;
9380
9381 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9382 {
9383 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9384 {
9385 if (!ses_do_win(wp))
9386 continue;
9387 ++n;
9388
9389 /* restore height when not full height */
9390 if (wp->w_height + wp->w_status_height < topframe->fr_height
9391 && (fprintf(fd,
9392 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9393 n, (long)wp->w_height, Rows / 2, Rows) < 0
9394 || put_eol(fd) == FAIL))
9395 return FAIL;
9396
9397 /* restore width when not full width */
9398 if (wp->w_width < Columns && (fprintf(fd,
9399 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9400 n, (long)wp->w_width, Columns / 2, Columns) < 0
9401 || put_eol(fd) == FAIL))
9402 return FAIL;
9403 }
9404 }
9405 else
9406 {
9407 /* Just equalise window sizes */
9408 if (put_line(fd, "wincmd =") == FAIL)
9409 return FAIL;
9410 }
9411 return OK;
9412}
9413
9414/*
9415 * Write commands to "fd" to recursively create windows for frame "fr",
9416 * horizontally and vertically split.
9417 * After the commands the last window in the frame is the current window.
9418 * Returns FAIL when writing the commands to "fd" fails.
9419 */
9420 static int
9421ses_win_rec(fd, fr)
9422 FILE *fd;
9423 frame_T *fr;
9424{
9425 frame_T *frc;
9426 int count = 0;
9427
9428 if (fr->fr_layout != FR_LEAF)
9429 {
9430 /* Find first frame that's not skipped and then create a window for
9431 * each following one (first frame is already there). */
9432 frc = ses_skipframe(fr->fr_child);
9433 if (frc != NULL)
9434 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9435 {
9436 /* Make window as big as possible so that we have lots of room
9437 * to split. */
9438 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9439 || put_line(fd, fr->fr_layout == FR_COL
9440 ? "split" : "vsplit") == FAIL)
9441 return FAIL;
9442 ++count;
9443 }
9444
9445 /* Go back to the first window. */
9446 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9447 ? "%dwincmd k" : "%dwincmd h", count) < 0
9448 || put_eol(fd) == FAIL))
9449 return FAIL;
9450
9451 /* Recursively create frames/windows in each window of this column or
9452 * row. */
9453 frc = ses_skipframe(fr->fr_child);
9454 while (frc != NULL)
9455 {
9456 ses_win_rec(fd, frc);
9457 frc = ses_skipframe(frc->fr_next);
9458 /* Go to next window. */
9459 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9460 return FAIL;
9461 }
9462 }
9463 return OK;
9464}
9465
9466/*
9467 * Skip frames that don't contain windows we want to save in the Session.
9468 * Returns NULL when there none.
9469 */
9470 static frame_T *
9471ses_skipframe(fr)
9472 frame_T *fr;
9473{
9474 frame_T *frc;
9475
9476 for (frc = fr; frc != NULL; frc = frc->fr_next)
9477 if (ses_do_frame(frc))
9478 break;
9479 return frc;
9480}
9481
9482/*
9483 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9484 * the Session.
9485 */
9486 static int
9487ses_do_frame(fr)
9488 frame_T *fr;
9489{
9490 frame_T *frc;
9491
9492 if (fr->fr_layout == FR_LEAF)
9493 return ses_do_win(fr->fr_win);
9494 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9495 if (ses_do_frame(frc))
9496 return TRUE;
9497 return FALSE;
9498}
9499
9500/*
9501 * Return non-zero if window "wp" is to be stored in the Session.
9502 */
9503 static int
9504ses_do_win(wp)
9505 win_T *wp;
9506{
9507 if (wp->w_buffer->b_fname == NULL
9508#ifdef FEAT_QUICKFIX
9509 /* When 'buftype' is "nofile" can't restore the window contents. */
9510 || bt_nofile(wp->w_buffer)
9511#endif
9512 )
9513 return (ssop_flags & SSOP_BLANK);
9514 if (wp->w_buffer->b_help)
9515 return (ssop_flags & SSOP_HELP);
9516 return TRUE;
9517}
9518
9519/*
9520 * Write commands to "fd" to restore the view of a window.
9521 * Caller must make sure 'scrolloff' is zero.
9522 */
9523 static int
9524put_view(fd, wp, add_edit, flagp)
9525 FILE *fd;
9526 win_T *wp;
9527 int add_edit; /* add ":edit" command to view */
9528 unsigned *flagp; /* vop_flags or ssop_flags */
9529{
9530 win_T *save_curwin;
9531 int f;
9532 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009533 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534
9535 /* Always restore cursor position for ":mksession". For ":mkview" only
9536 * when 'viewoptions' contains "cursor". */
9537 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9538
9539 /*
9540 * Local argument list.
9541 */
9542 if (wp->w_alist == &global_alist)
9543 {
9544 if (put_line(fd, "argglobal") == FAIL)
9545 return FAIL;
9546 }
9547 else
9548 {
9549 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9550 flagp == &vop_flags
9551 || !(*flagp & SSOP_CURDIR)
9552 || wp->w_localdir != NULL, flagp) == FAIL)
9553 return FAIL;
9554 }
9555
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009556 /* Only when part of a session: restore the argument index. Some
9557 * arguments may have been deleted, check if the index is valid. */
9558 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
9559 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 {
9561 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9562 || put_eol(fd) == FAIL)
9563 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009564 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 }
9566
9567 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009568 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 {
9570 /*
9571 * Load the file.
9572 */
9573 if (wp->w_buffer->b_ffname != NULL
9574#ifdef FEAT_QUICKFIX
9575 && !bt_nofile(wp->w_buffer)
9576#endif
9577 )
9578 {
9579 /*
9580 * Editing a file in this buffer: use ":edit file".
9581 * This may have side effects! (e.g., compressed or network file).
9582 */
9583 if (fputs("edit ", fd) < 0
9584 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9585 return FAIL;
9586 }
9587 else
9588 {
9589 /* No file in this buffer, just make it empty. */
9590 if (put_line(fd, "enew") == FAIL)
9591 return FAIL;
9592#ifdef FEAT_QUICKFIX
9593 if (wp->w_buffer->b_ffname != NULL)
9594 {
9595 /* The buffer does have a name, but it's not a file name. */
9596 if (fputs("file ", fd) < 0
9597 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9598 return FAIL;
9599 }
9600#endif
9601 do_cursor = FALSE;
9602 }
9603 }
9604
9605 /*
9606 * Local mappings and abbreviations.
9607 */
9608 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9609 && makemap(fd, wp->w_buffer) == FAIL)
9610 return FAIL;
9611
9612 /*
9613 * Local options. Need to go to the window temporarily.
9614 * Store only local values when using ":mkview" and when ":mksession" is
9615 * used and 'sessionoptions' doesn't include "options".
9616 * Some folding options are always stored when "folds" is included,
9617 * otherwise the folds would not be restored correctly.
9618 */
9619 save_curwin = curwin;
9620 curwin = wp;
9621 curbuf = curwin->w_buffer;
9622 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9623 f = makeset(fd, OPT_LOCAL,
9624 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
9625#ifdef FEAT_FOLDING
9626 else if (*flagp & SSOP_FOLDS)
9627 f = makefoldset(fd);
9628#endif
9629 else
9630 f = OK;
9631 curwin = save_curwin;
9632 curbuf = curwin->w_buffer;
9633 if (f == FAIL)
9634 return FAIL;
9635
9636#ifdef FEAT_FOLDING
9637 /*
9638 * Save Folds when 'buftype' is empty and for help files.
9639 */
9640 if ((*flagp & SSOP_FOLDS)
9641 && wp->w_buffer->b_ffname != NULL
9642 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help))
9643 {
9644 if (put_folds(fd, wp) == FAIL)
9645 return FAIL;
9646 }
9647#endif
9648
9649 /*
9650 * Set the cursor after creating folds, since that moves the cursor.
9651 */
9652 if (do_cursor)
9653 {
9654
9655 /* Restore the cursor line in the file and relatively in the
9656 * window. Don't use "G", it changes the jumplist. */
9657 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
9658 (long)wp->w_cursor.lnum,
9659 (long)(wp->w_cursor.lnum - wp->w_topline),
9660 (long)wp->w_height / 2, (long)wp->w_height) < 0
9661 || put_eol(fd) == FAIL
9662 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
9663 || put_line(fd, "exe s:l") == FAIL
9664 || put_line(fd, "normal! zt") == FAIL
9665 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
9666 || put_eol(fd) == FAIL)
9667 return FAIL;
9668 /* Restore the cursor column and left offset when not wrapping. */
9669 if (wp->w_cursor.col == 0)
9670 {
9671 if (put_line(fd, "normal! 0") == FAIL)
9672 return FAIL;
9673 }
9674 else
9675 {
9676 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
9677 {
9678 if (fprintf(fd,
9679 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
9680 (long)wp->w_cursor.col,
9681 (long)(wp->w_cursor.col - wp->w_leftcol),
9682 (long)wp->w_width / 2, (long)wp->w_width) < 0
9683 || put_eol(fd) == FAIL
9684 || put_line(fd, "if s:c > 0") == FAIL
9685 || fprintf(fd,
9686 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
9687 (long)wp->w_cursor.col) < 0
9688 || put_eol(fd) == FAIL
9689 || put_line(fd, "else") == FAIL
9690 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
9691 || put_eol(fd) == FAIL
9692 || put_line(fd, "endif") == FAIL)
9693 return FAIL;
9694 }
9695 else
9696 {
9697 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
9698 || put_eol(fd) == FAIL)
9699 return FAIL;
9700 }
9701 }
9702 }
9703
9704 /*
9705 * Local directory.
9706 */
9707 if (wp->w_localdir != NULL)
9708 {
9709 if (fputs("lcd ", fd) < 0
9710 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
9711 || put_eol(fd) == FAIL)
9712 return FAIL;
9713 }
9714
9715 return OK;
9716}
9717
9718/*
9719 * Write an argument list to the session file.
9720 * Returns FAIL if writing fails.
9721 */
9722 static int
9723ses_arglist(fd, cmd, gap, fullname, flagp)
9724 FILE *fd;
9725 char *cmd;
9726 garray_T *gap;
9727 int fullname; /* TRUE: use full path name */
9728 unsigned *flagp;
9729{
9730 int i;
9731 char_u buf[MAXPATHL];
9732 char_u *s;
9733
9734 if (gap->ga_len == 0)
9735 return put_line(fd, "silent! argdel *");
9736 if (fputs(cmd, fd) < 0)
9737 return FAIL;
9738 for (i = 0; i < gap->ga_len; ++i)
9739 {
9740 /* NULL file names are skipped (only happens when out of memory). */
9741 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
9742 if (s != NULL)
9743 {
9744 if (fullname)
9745 {
9746 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
9747 s = buf;
9748 }
9749 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
9750 return FAIL;
9751 }
9752 }
9753 return put_eol(fd);
9754}
9755
9756/*
9757 * Write a buffer name to the session file.
9758 * Also ends the line.
9759 * Returns FAIL if writing fails.
9760 */
9761 static int
9762ses_fname(fd, buf, flagp)
9763 FILE *fd;
9764 buf_T *buf;
9765 unsigned *flagp;
9766{
9767 char_u *name;
9768
9769 /* Use the short file name if the current directory is known at the time
9770 * the session file will be sourced. Don't do this for ":mkview", we
9771 * don't know the current directory. */
9772 if (buf->b_sfname != NULL
9773 && flagp == &ssop_flags
9774 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
9775 name = buf->b_sfname;
9776 else
9777 name = buf->b_ffname;
9778 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
9779 return FAIL;
9780 return OK;
9781}
9782
9783/*
9784 * Write a file name to the session file.
9785 * Takes care of the "slash" option in 'sessionoptions' and escapes special
9786 * characters.
9787 * Returns FAIL if writing fails.
9788 */
9789 static int
9790ses_put_fname(fd, name, flagp)
9791 FILE *fd;
9792 char_u *name;
9793 unsigned *flagp;
9794{
9795 char_u *sname;
9796 int retval = OK;
9797 int c;
9798
9799 sname = home_replace_save(NULL, name);
9800 if (sname != NULL)
9801 name = sname;
9802 while (*name != NUL)
9803 {
9804#ifdef FEAT_MBYTE
9805 {
9806 int l;
9807
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009808 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 {
9810 /* copy a multibyte char */
9811 while (--l >= 0)
9812 {
9813 if (putc(*name, fd) != *name)
9814 retval = FAIL;
9815 ++name;
9816 }
9817 continue;
9818 }
9819 }
9820#endif
9821 c = *name++;
9822 if (c == '\\' && (*flagp & SSOP_SLASH))
9823 /* change a backslash to a forward slash */
9824 c = '/';
9825 else if ((vim_strchr(escape_chars, c) != NULL
9826#ifdef BACKSLASH_IN_FILENAME
9827 && c != '\\'
9828#endif
9829 ) || c == '#' || c == '%')
9830 {
9831 /* escape a special character with a backslash */
9832 if (putc('\\', fd) != '\\')
9833 retval = FAIL;
9834 }
9835 if (putc(c, fd) != c)
9836 retval = FAIL;
9837 }
9838 vim_free(sname);
9839 return retval;
9840}
9841
9842/*
9843 * ":loadview [nr]"
9844 */
9845 static void
9846ex_loadview(eap)
9847 exarg_T *eap;
9848{
9849 char_u *fname;
9850
9851 fname = get_view_file(*eap->arg);
9852 if (fname != NULL)
9853 {
9854 do_source(fname, FALSE, FALSE);
9855 vim_free(fname);
9856 }
9857}
9858
9859/*
9860 * Get the name of the view file for the current buffer.
9861 */
9862 static char_u *
9863get_view_file(c)
9864 int c;
9865{
9866 int len = 0;
9867 char_u *p, *s;
9868 char_u *retval;
9869 char_u *sname;
9870
9871 if (curbuf->b_ffname == NULL)
9872 {
9873 EMSG(_(e_noname));
9874 return NULL;
9875 }
9876 sname = home_replace_save(NULL, curbuf->b_ffname);
9877 if (sname == NULL)
9878 return NULL;
9879
9880 /*
9881 * We want a file name without separators, because we're not going to make
9882 * a directory.
9883 * "normal" path separator -> "=+"
9884 * "=" -> "=="
9885 * ":" path separator -> "=-"
9886 */
9887 for (p = sname; *p; ++p)
9888 if (*p == '=' || vim_ispathsep(*p))
9889 ++len;
9890 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
9891 if (retval != NULL)
9892 {
9893 STRCPY(retval, p_vdir);
9894 add_pathsep(retval);
9895 s = retval + STRLEN(retval);
9896 for (p = sname; *p; ++p)
9897 {
9898 if (*p == '=')
9899 {
9900 *s++ = '=';
9901 *s++ = '=';
9902 }
9903 else if (vim_ispathsep(*p))
9904 {
9905 *s++ = '=';
9906#ifdef MACOS_CLASSIC /* TODO: Is it also needed for MACOS_X? (Dany) */
9907 *s++ = '+';
9908#else
9909# if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
9910 || defined(VMS)
9911 if (*p == ':')
9912 *s++ = '-';
9913 else
9914# endif
9915 *s++ = '+';
9916#endif
9917 }
9918 else
9919 *s++ = *p;
9920 }
9921 *s++ = '=';
9922 *s++ = c;
9923 STRCPY(s, ".vim");
9924 }
9925
9926 vim_free(sname);
9927 return retval;
9928}
9929
9930#endif /* FEAT_SESSION */
9931
9932/*
9933 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
9934 * Return FAIL for a write error.
9935 */
9936 int
9937put_eol(fd)
9938 FILE *fd;
9939{
9940 if (
9941#ifdef USE_CRNL
9942 (
9943# ifdef MKSESSION_NL
9944 !mksession_nl &&
9945# endif
9946 (putc('\r', fd) < 0)) ||
9947#endif
9948 (putc('\n', fd) < 0))
9949 return FAIL;
9950 return OK;
9951}
9952
9953/*
9954 * Write a line to "fd".
9955 * Return FAIL for a write error.
9956 */
9957 int
9958put_line(fd, s)
9959 FILE *fd;
9960 char *s;
9961{
9962 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
9963 return FAIL;
9964 return OK;
9965}
9966
9967#ifdef FEAT_VIMINFO
9968/*
9969 * ":rviminfo" and ":wviminfo".
9970 */
9971 static void
9972ex_viminfo(eap)
9973 exarg_T *eap;
9974{
9975 char_u *save_viminfo;
9976
9977 save_viminfo = p_viminfo;
9978 if (*p_viminfo == NUL)
9979 p_viminfo = (char_u *)"'100";
9980 if (eap->cmdidx == CMD_rviminfo)
9981 {
9982 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
9983 EMSG(_("E195: Cannot open viminfo file for reading"));
9984 }
9985 else
9986 write_viminfo(eap->arg, eap->forceit);
9987 p_viminfo = save_viminfo;
9988}
9989#endif
9990
9991#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009992/*
9993 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009994 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 void
9997dialog_msg(buff, format, fname)
9998 char_u *buff;
9999 char *format;
10000 char_u *fname;
10001{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 if (fname == NULL)
10003 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010004 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005}
10006#endif
10007
10008/*
10009 * ":behave {mswin,xterm}"
10010 */
10011 static void
10012ex_behave(eap)
10013 exarg_T *eap;
10014{
10015 if (STRCMP(eap->arg, "mswin") == 0)
10016 {
10017 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10018 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10019 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10020 set_option_value((char_u *)"keymodel", 0L,
10021 (char_u *)"startsel,stopsel", 0);
10022 }
10023 else if (STRCMP(eap->arg, "xterm") == 0)
10024 {
10025 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10026 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10027 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10028 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10029 }
10030 else
10031 EMSG2(_(e_invarg2), eap->arg);
10032}
10033
10034#ifdef FEAT_AUTOCMD
10035static int filetype_detect = FALSE;
10036static int filetype_plugin = FALSE;
10037static int filetype_indent = FALSE;
10038
10039/*
10040 * ":filetype [plugin] [indent] {on,off,detect}"
10041 * on: Load the filetype.vim file to install autocommands for file types.
10042 * off: Load the ftoff.vim file to remove all autocommands for file types.
10043 * plugin on: load filetype.vim and ftplugin.vim
10044 * plugin off: load ftplugof.vim
10045 * indent on: load filetype.vim and indent.vim
10046 * indent off: load indoff.vim
10047 */
10048 static void
10049ex_filetype(eap)
10050 exarg_T *eap;
10051{
10052 char_u *arg = eap->arg;
10053 int plugin = FALSE;
10054 int indent = FALSE;
10055
10056 if (*eap->arg == NUL)
10057 {
10058 /* Print current status. */
10059 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10060 filetype_detect ? "ON" : "OFF",
10061 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10062 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10063 return;
10064 }
10065
10066 /* Accept "plugin" and "indent" in any order. */
10067 for (;;)
10068 {
10069 if (STRNCMP(arg, "plugin", 6) == 0)
10070 {
10071 plugin = TRUE;
10072 arg = skipwhite(arg + 6);
10073 continue;
10074 }
10075 if (STRNCMP(arg, "indent", 6) == 0)
10076 {
10077 indent = TRUE;
10078 arg = skipwhite(arg + 6);
10079 continue;
10080 }
10081 break;
10082 }
10083 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10084 {
10085 if (*arg == 'o' || !filetype_detect)
10086 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010087 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 filetype_detect = TRUE;
10089 if (plugin)
10090 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010091 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 filetype_plugin = TRUE;
10093 }
10094 if (indent)
10095 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010096 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 filetype_indent = TRUE;
10098 }
10099 }
10100 if (*arg == 'd')
10101 {
10102 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +000010103 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 }
10105 }
10106 else if (STRCMP(arg, "off") == 0)
10107 {
10108 if (plugin || indent)
10109 {
10110 if (plugin)
10111 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010112 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 filetype_plugin = FALSE;
10114 }
10115 if (indent)
10116 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010117 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 filetype_indent = FALSE;
10119 }
10120 }
10121 else
10122 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010123 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 filetype_detect = FALSE;
10125 }
10126 }
10127 else
10128 EMSG2(_(e_invarg2), arg);
10129}
10130
10131/*
10132 * ":setfiletype {name}"
10133 */
10134 static void
10135ex_setfiletype(eap)
10136 exarg_T *eap;
10137{
10138 if (!did_filetype)
10139 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10140}
10141#endif
10142
10143/*ARGSUSED*/
10144 static void
10145ex_digraphs(eap)
10146 exarg_T *eap;
10147{
10148#ifdef FEAT_DIGRAPHS
10149 if (*eap->arg != NUL)
10150 putdigraph(eap->arg);
10151 else
10152 listdigraphs();
10153#else
10154 EMSG(_("E196: No digraphs in this version"));
10155#endif
10156}
10157
10158 static void
10159ex_set(eap)
10160 exarg_T *eap;
10161{
10162 int flags = 0;
10163
10164 if (eap->cmdidx == CMD_setlocal)
10165 flags = OPT_LOCAL;
10166 else if (eap->cmdidx == CMD_setglobal)
10167 flags = OPT_GLOBAL;
10168#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10169 if (cmdmod.browse && flags == 0)
10170 ex_options(eap);
10171 else
10172#endif
10173 (void)do_set(eap->arg, flags);
10174}
10175
10176#ifdef FEAT_SEARCH_EXTRA
10177/*
10178 * ":nohlsearch"
10179 */
10180/*ARGSUSED*/
10181 static void
10182ex_nohlsearch(eap)
10183 exarg_T *eap;
10184{
10185 no_hlsearch = TRUE;
10186 redraw_all_later(NOT_VALID);
10187}
10188
10189/*
10190 * ":match {group} {pattern}"
10191 * Sets nextcmd to the start of the next command, if any. Also called when
10192 * skipping commands to find the next command.
10193 */
10194 static void
10195ex_match(eap)
10196 exarg_T *eap;
10197{
10198 char_u *p;
10199 char_u *end;
10200 int c;
10201
10202 /* First clear any old pattern. */
10203 if (!eap->skip)
10204 {
10205 vim_free(curwin->w_match.regprog);
10206 curwin->w_match.regprog = NULL;
10207 redraw_later(NOT_VALID); /* always need a redraw */
10208 }
10209
10210 if (ends_excmd(*eap->arg))
10211 end = eap->arg;
10212 else if ((STRNICMP(eap->arg, "none", 4) == 0
10213 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10214 end = eap->arg + 4;
10215 else
10216 {
10217 p = skiptowhite(eap->arg);
10218 if (!eap->skip)
10219 {
10220 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg));
10221 if (curwin->w_match_id == 0)
10222 {
10223 EMSG2(_(e_nogroup), eap->arg);
10224 return;
10225 }
10226 }
10227 p = skipwhite(p);
10228 if (*p == NUL)
10229 {
10230 /* There must be two arguments. */
10231 EMSG2(_(e_invarg2), eap->arg);
10232 return;
10233 }
10234 end = skip_regexp(p + 1, *p, TRUE, NULL);
10235 if (!eap->skip)
10236 {
10237 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10238 {
10239 eap->errmsg = e_trailing;
10240 return;
10241 }
10242
10243 c = *end;
10244 *end = NUL;
10245 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC);
10246 *end = c;
10247 if (curwin->w_match.regprog == NULL)
10248 {
10249 EMSG2(_(e_invarg2), p);
10250 return;
10251 }
10252 }
10253 }
10254 eap->nextcmd = find_nextcmd(end);
10255}
10256#endif
10257
10258#ifdef FEAT_CRYPT
10259/*
10260 * ":X": Get crypt key
10261 */
10262/*ARGSUSED*/
10263 static void
10264ex_X(eap)
10265 exarg_T *eap;
10266{
10267 (void)get_crypt_key(TRUE, TRUE);
10268}
10269#endif
10270
10271#ifdef FEAT_FOLDING
10272 static void
10273ex_fold(eap)
10274 exarg_T *eap;
10275{
10276 if (foldManualAllowed(TRUE))
10277 foldCreate(eap->line1, eap->line2);
10278}
10279
10280 static void
10281ex_foldopen(eap)
10282 exarg_T *eap;
10283{
10284 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10285 eap->forceit, FALSE);
10286}
10287
10288 static void
10289ex_folddo(eap)
10290 exarg_T *eap;
10291{
10292 linenr_T lnum;
10293
10294 /* First set the marks for all lines closed/open. */
10295 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10296 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10297 ml_setmarked(lnum);
10298
10299 /* Execute the command on the marked lines. */
10300 global_exe(eap->arg);
10301 ml_clearmarked(); /* clear rest of the marks */
10302}
10303#endif