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