blob: f88fbf815b2818db622fb314e265e5315638d1fe [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 Moolenaardf177f62005-02-22 08:39:57 +00002155 if (sourcing || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
2157 errormsg = (char_u *)_("E493: Backwards range given");
2158 goto doend;
2159 }
2160 else
2161 {
2162 int msg_silent_save = msg_silent;
2163
2164 msg_silent = 0;
2165 if (ask_yesno((char_u *)
2166 _("Backwards range given, OK to swap"), FALSE) != 'y')
2167 goto doend;
2168 msg_silent = msg_silent_save;
2169 }
2170 lnum = ea.line1;
2171 ea.line1 = ea.line2;
2172 ea.line2 = lnum;
2173 }
2174 if ((errormsg = invalid_range(&ea)) != NULL)
2175 goto doend;
2176 }
2177
2178 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2179 ea.line2 = 1;
2180
2181 correct_range(&ea);
2182
2183#ifdef FEAT_FOLDING
2184 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2185 {
2186 /* Put the first line at the start of a closed fold, put the last line
2187 * at the end of a closed fold. */
2188 (void)hasFolding(ea.line1, &ea.line1, NULL);
2189 (void)hasFolding(ea.line2, NULL, &ea.line2);
2190 }
2191#endif
2192
2193#ifdef FEAT_QUICKFIX
2194 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002195 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 * option here, so things like % get expanded.
2197 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002198 p = replace_makeprg(&ea, p, cmdlinep);
2199 if (p == NULL)
2200 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201#endif
2202
2203 /*
2204 * Skip to start of argument.
2205 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2206 */
2207 if (ea.cmdidx == CMD_bang)
2208 ea.arg = p;
2209 else
2210 ea.arg = skipwhite(p);
2211
2212 /*
2213 * Check for "++opt=val" argument.
2214 * Must be first, allow ":w ++enc=utf8 !cmd"
2215 */
2216 if (ea.argt & ARGOPT)
2217 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2218 if (getargopt(&ea) == FAIL && !ni)
2219 {
2220 errormsg = (char_u *)_(e_invarg);
2221 goto doend;
2222 }
2223
2224 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2225 {
2226 if (*ea.arg == '>') /* append */
2227 {
2228 if (*++ea.arg != '>') /* typed wrong */
2229 {
2230 errormsg = (char_u *)_("E494: Use w or w>>");
2231 goto doend;
2232 }
2233 ea.arg = skipwhite(ea.arg + 1);
2234 ea.append = TRUE;
2235 }
2236 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2237 {
2238 ++ea.arg;
2239 ea.usefilter = TRUE;
2240 }
2241 }
2242
2243 if (ea.cmdidx == CMD_read)
2244 {
2245 if (ea.forceit)
2246 {
2247 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2248 ea.forceit = FALSE;
2249 }
2250 else if (*ea.arg == '!') /* :r !filter */
2251 {
2252 ++ea.arg;
2253 ea.usefilter = TRUE;
2254 }
2255 }
2256
2257 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2258 {
2259 ea.amount = 1;
2260 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2261 {
2262 ++ea.arg;
2263 ++ea.amount;
2264 }
2265 ea.arg = skipwhite(ea.arg);
2266 }
2267
2268 /*
2269 * Check for "+command" argument, before checking for next command.
2270 * Don't do this for ":read !cmd" and ":write !cmd".
2271 */
2272 if ((ea.argt & EDITCMD) && !ea.usefilter)
2273 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2274
2275 /*
2276 * Check for '|' to separate commands and '"' to start comments.
2277 * Don't do this for ":read !cmd" and ":write !cmd".
2278 */
2279 if ((ea.argt & TRLBAR) && !ea.usefilter)
2280 separate_nextcmd(&ea);
2281
2282 /*
2283 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002284 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2285 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002287 else if (ea.cmdidx == CMD_bang
2288 || ea.cmdidx == CMD_global
2289 || ea.cmdidx == CMD_vglobal
2290 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 for (p = ea.arg; *p; ++p)
2293 {
2294 /* Remove one backslash before a newline, so that it's possible to
2295 * pass a newline to the shell and also a newline that is preceded
2296 * with a backslash. This makes it impossible to end a shell
2297 * command in a backslash, but that doesn't appear useful.
2298 * Halving the number of backslashes is incompatible with previous
2299 * versions. */
2300 if (*p == '\\' && p[1] == '\n')
2301 mch_memmove(p, p + 1, STRLEN(p));
2302 else if (*p == '\n')
2303 {
2304 ea.nextcmd = p + 1;
2305 *p = NUL;
2306 break;
2307 }
2308 }
2309 }
2310
2311 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2312 {
2313 ea.line1 = 1;
2314 ea.line2 = curbuf->b_ml.ml_line_count;
2315 }
2316
2317 /* accept numbered register only when no count allowed (:put) */
2318 if ( (ea.argt & REGSTR)
2319 && *ea.arg != NUL
2320#ifdef FEAT_USR_CMDS
2321 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2322 && USER_CMDIDX(ea.cmdidx)))
2323 /* Do not allow register = for user commands */
2324 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2325#else
2326 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2327#endif
2328 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2329 {
2330 ea.regname = *ea.arg++;
2331#ifdef FEAT_EVAL
2332 /* for '=' register: accept the rest of the line as an expression */
2333 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2334 {
2335 set_expr_line(vim_strsave(ea.arg));
2336 ea.arg += STRLEN(ea.arg);
2337 }
2338#endif
2339 ea.arg = skipwhite(ea.arg);
2340 }
2341
2342 /*
2343 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2344 * count, it's a buffer name.
2345 */
2346 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2347 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2348 || vim_iswhite(*p)))
2349 {
2350 n = getdigits(&ea.arg);
2351 ea.arg = skipwhite(ea.arg);
2352 if (n <= 0 && !ni)
2353 {
2354 errormsg = (char_u *)_(e_zerocount);
2355 goto doend;
2356 }
2357 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2358 {
2359 ea.line2 = n;
2360 if (ea.addr_count == 0)
2361 ea.addr_count = 1;
2362 }
2363 else
2364 {
2365 ea.line1 = ea.line2;
2366 ea.line2 += n - 1;
2367 ++ea.addr_count;
2368 /*
2369 * Be vi compatible: no error message for out of range.
2370 */
2371 if (ea.line2 > curbuf->b_ml.ml_line_count)
2372 ea.line2 = curbuf->b_ml.ml_line_count;
2373 }
2374 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002375
2376 /*
2377 * Check for flags: 'l', 'p' and '#'.
2378 */
2379 if (ea.argt & EXFLAGS)
2380 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002382 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
2383 && vim_strchr((char_u *)"|\"", *ea.arg) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 {
2385 errormsg = (char_u *)_(e_trailing);
2386 goto doend;
2387 }
2388
2389 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2390 {
2391 errormsg = (char_u *)_(e_argreq);
2392 goto doend;
2393 }
2394
2395#ifdef FEAT_EVAL
2396 /*
2397 * Skip the command when it's not going to be executed.
2398 * The commands like :if, :endif, etc. always need to be executed.
2399 * Also make an exception for commands that handle a trailing command
2400 * themselves.
2401 */
2402 if (ea.skip)
2403 {
2404 switch (ea.cmdidx)
2405 {
2406 /* commands that need evaluation */
2407 case CMD_while:
2408 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002409 case CMD_for:
2410 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 case CMD_if:
2412 case CMD_elseif:
2413 case CMD_else:
2414 case CMD_endif:
2415 case CMD_try:
2416 case CMD_catch:
2417 case CMD_finally:
2418 case CMD_endtry:
2419 case CMD_function:
2420 break;
2421
2422 /* Commands that handle '|' themselves. Check: A command should
2423 * either have the TRLBAR flag, appear in this list or appear in
2424 * the list at ":help :bar". */
2425 case CMD_aboveleft:
2426 case CMD_and:
2427 case CMD_belowright:
2428 case CMD_botright:
2429 case CMD_browse:
2430 case CMD_call:
2431 case CMD_confirm:
2432 case CMD_delfunction:
2433 case CMD_djump:
2434 case CMD_dlist:
2435 case CMD_dsearch:
2436 case CMD_dsplit:
2437 case CMD_echo:
2438 case CMD_echoerr:
2439 case CMD_echomsg:
2440 case CMD_echon:
2441 case CMD_execute:
2442 case CMD_help:
2443 case CMD_hide:
2444 case CMD_ijump:
2445 case CMD_ilist:
2446 case CMD_isearch:
2447 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002448 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 case CMD_keepjumps:
2450 case CMD_keepmarks:
2451 case CMD_leftabove:
2452 case CMD_let:
2453 case CMD_lockmarks:
2454 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002455 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 case CMD_perl:
2457 case CMD_psearch:
2458 case CMD_python:
2459 case CMD_return:
2460 case CMD_rightbelow:
2461 case CMD_ruby:
2462 case CMD_silent:
2463 case CMD_smagic:
2464 case CMD_snomagic:
2465 case CMD_substitute:
2466 case CMD_syntax:
2467 case CMD_tcl:
2468 case CMD_throw:
2469 case CMD_tilde:
2470 case CMD_topleft:
2471 case CMD_unlet:
2472 case CMD_verbose:
2473 case CMD_vertical:
2474 break;
2475
2476 default: goto doend;
2477 }
2478 }
2479#endif
2480
2481 if (ea.argt & XFILE)
2482 {
2483 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2484 goto doend;
2485 }
2486
2487#ifdef FEAT_LISTCMDS
2488 /*
2489 * Accept buffer name. Cannot be used at the same time with a buffer
2490 * number. Don't do this for a user command.
2491 */
2492 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2493# ifdef FEAT_USR_CMDS
2494 && !USER_CMDIDX(ea.cmdidx)
2495# endif
2496 )
2497 {
2498 /*
2499 * :bdelete, :bwipeout and :bunload take several arguments, separated
2500 * by spaces: find next space (skipping over escaped characters).
2501 * The others take one argument: ignore trailing spaces.
2502 */
2503 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2504 || ea.cmdidx == CMD_bunload)
2505 p = skiptowhite_esc(ea.arg);
2506 else
2507 {
2508 p = ea.arg + STRLEN(ea.arg);
2509 while (p > ea.arg && vim_iswhite(p[-1]))
2510 --p;
2511 }
2512 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2513 if (ea.line2 < 0) /* failed */
2514 goto doend;
2515 ea.addr_count = 1;
2516 ea.arg = skipwhite(p);
2517 }
2518#endif
2519
2520/*
2521 * 6. switch on command name
2522 *
2523 * The "ea" structure holds the arguments that can be used.
2524 */
2525 ea.cmdlinep = cmdlinep;
2526 ea.getline = getline;
2527 ea.cookie = cookie;
2528#ifdef FEAT_EVAL
2529 ea.cstack = cstack;
2530#endif
2531
2532#ifdef FEAT_USR_CMDS
2533 if (USER_CMDIDX(ea.cmdidx))
2534 {
2535 /*
2536 * Execute a user-defined command.
2537 */
2538 do_ucmd(&ea);
2539 }
2540 else
2541#endif
2542 {
2543 /*
2544 * Call the function to execute the command.
2545 */
2546 ea.errmsg = NULL;
2547 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2548 if (ea.errmsg != NULL)
2549 errormsg = (char_u *)_(ea.errmsg);
2550 }
2551
2552#ifdef FEAT_EVAL
2553 /*
2554 * If the command just executed called do_cmdline(), any throw or ":return"
2555 * or ":finish" encountered there must also check the cstack of the still
2556 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2557 * exception, or reanimate a returned function or finished script file and
2558 * return or finish it again.
2559 */
2560 if (need_rethrow)
2561 do_throw(cstack);
2562 else if (check_cstack)
2563 {
2564 if (source_finished(getline, cookie))
2565 do_finish(&ea, TRUE);
2566 else if (getline_equal(getline, cookie, get_func_line)
2567 && current_func_returned())
2568 do_return(&ea, TRUE, FALSE, NULL);
2569 }
2570 need_rethrow = check_cstack = FALSE;
2571#endif
2572
2573doend:
2574 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2575 curwin->w_cursor.lnum = 1;
2576
2577 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2578 {
2579 if (sourcing)
2580 {
2581 if (errormsg != IObuff)
2582 {
2583 STRCPY(IObuff, errormsg);
2584 errormsg = IObuff;
2585 }
2586 STRCAT(errormsg, ": ");
2587 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff));
2588 }
2589 emsg(errormsg);
2590 }
2591#ifdef FEAT_EVAL
2592 do_errthrow(cstack,
2593 (ea.cmdidx != CMD_SIZE
2594# ifdef FEAT_USR_CMDS
2595 && !USER_CMDIDX(ea.cmdidx)
2596# endif
2597 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2598#endif
2599
2600 if (verbose_save >= 0)
2601 p_verbose = verbose_save;
2602
2603 cmdmod = save_cmdmod;
2604
2605 if (did_silent > 0)
2606 {
2607 /* messages could be enabled for a serious error, need to check if the
2608 * counters don't become negative */
2609 msg_silent -= did_silent;
2610 if (msg_silent < 0)
2611 msg_silent = 0;
2612 emsg_silent -= did_esilent;
2613 if (emsg_silent < 0)
2614 emsg_silent = 0;
2615 /* Restore msg_scroll, it's set by file I/O commands, even when no
2616 * message is actually displayed. */
2617 msg_scroll = save_msg_scroll;
2618 }
2619
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002620#ifdef HAVE_SANDBOX
2621 if (did_sandbox)
2622 --sandbox;
2623#endif
2624
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2626 ea.nextcmd = NULL;
2627
2628#ifdef FEAT_EVAL
2629 --ex_nesting_level;
2630#endif
2631
2632 return ea.nextcmd;
2633}
2634#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002635 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636#endif
2637
2638/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002639 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 * If there is a match advance "pp" to the argument and return TRUE.
2641 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002642 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002644 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 char *cmd; /* name of command */
2646 int len; /* required length */
2647{
2648 int i;
2649
2650 for (i = 0; cmd[i] != NUL; ++i)
2651 if (cmd[i] != (*pp)[i])
2652 break;
2653 if (i >= len && !isalpha((*pp)[i]))
2654 {
2655 *pp = skipwhite(*pp + i);
2656 return TRUE;
2657 }
2658 return FALSE;
2659}
2660
2661/*
2662 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002663 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002665 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 * Returns NULL for an ambiguous user command.
2667 */
2668/*ARGSUSED*/
2669 static char_u *
2670find_command(eap, full)
2671 exarg_T *eap;
2672 int *full;
2673{
2674 int len;
2675 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002676 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677
2678 /*
2679 * Isolate the command and search for it in the command table.
2680 * Exeptions:
2681 * - the 'k' command can directly be followed by any character.
2682 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2683 * but :sre[wind] is another command, as are :scrip[tnames],
2684 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002685 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 */
2687 p = eap->cmd;
2688 if (*p == 'k')
2689 {
2690 eap->cmdidx = CMD_k;
2691 ++p;
2692 }
2693 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002694 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2695 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 || p[1] == 'g'
2697 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2698 || p[1] == 'I'
2699 || (p[1] == 'r' && p[2] != 'e')))
2700 {
2701 eap->cmdidx = CMD_substitute;
2702 ++p;
2703 }
2704 else
2705 {
2706 while (ASCII_ISALPHA(*p))
2707 ++p;
2708 /* check for non-alpha command */
2709 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2710 ++p;
2711 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002712 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2713 {
2714 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2715 * :delete with the 'l' flag. Same for 'p'. */
2716 for (i = 0; i < len; ++i)
2717 if (eap->cmd[i] != "delete"[i])
2718 break;
2719 if (i == len - 1)
2720 {
2721 --len;
2722 if (p[-1] == 'l')
2723 eap->flags |= EXFLAG_LIST;
2724 else
2725 eap->flags |= EXFLAG_PRINT;
2726 }
2727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 if (ASCII_ISLOWER(*eap->cmd))
2730 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2731 else
2732 eap->cmdidx = cmdidxs[26];
2733
2734 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2735 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2736 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2737 (size_t)len) == 0)
2738 {
2739#ifdef FEAT_EVAL
2740 if (full != NULL
2741 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2742 *full = TRUE;
2743#endif
2744 break;
2745 }
2746
2747#ifdef FEAT_USR_CMDS
2748 /* Look for a user defined command as a last resort */
2749 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2750 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002751 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 while (ASCII_ISALNUM(*p))
2753 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002754 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 }
2756#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002757 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 eap->cmdidx = CMD_SIZE;
2759 }
2760
2761 return p;
2762}
2763
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002764#ifdef FEAT_USR_CMDS
2765/*
2766 * Search for a user command that matches "eap->cmd".
2767 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2768 * Return a pointer to just after the command.
2769 * Return NULL if there is no matching command.
2770 */
2771 static char_u *
2772find_ucmd(eap, p, full, xp, compl)
2773 exarg_T *eap;
2774 char_u *p; /* end of the command (possibly including count) */
2775 int *full; /* set to TRUE for a full match */
2776 expand_T *xp; /* used for completion, NULL otherwise */
2777 int *compl; /* completion flags or NULL */
2778{
2779 int len = (int)(p - eap->cmd);
2780 int j, k, matchlen = 0;
2781 ucmd_T *uc;
2782 int found = FALSE;
2783 int possible = FALSE;
2784 char_u *cp, *np; /* Point into typed cmd and test name */
2785 garray_T *gap;
2786 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2787 only full match global is accepted. */
2788
2789 /*
2790 * Look for buffer-local user commands first, then global ones.
2791 */
2792 gap = &curbuf->b_ucmds;
2793 for (;;)
2794 {
2795 for (j = 0; j < gap->ga_len; ++j)
2796 {
2797 uc = USER_CMD_GA(gap, j);
2798 cp = eap->cmd;
2799 np = uc->uc_name;
2800 k = 0;
2801 while (k < len && *np != NUL && *cp++ == *np++)
2802 k++;
2803 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2804 {
2805 /* If finding a second match, the command is ambiguous. But
2806 * not if a buffer-local command wasn't a full match and a
2807 * global command is a full match. */
2808 if (k == len && found && *np != NUL)
2809 {
2810 if (gap == &ucmds)
2811 {
2812 if (xp != NULL)
2813 xp->xp_context = EXPAND_UNSUCCESSFUL;
2814 return NULL;
2815 }
2816 amb_local = TRUE;
2817 }
2818
2819 if (!found || (k == len && *np == NUL))
2820 {
2821 /* If we matched up to a digit, then there could
2822 * be another command including the digit that we
2823 * should use instead.
2824 */
2825 if (k == len)
2826 found = TRUE;
2827 else
2828 possible = TRUE;
2829
2830 if (gap == &ucmds)
2831 eap->cmdidx = CMD_USER;
2832 else
2833 eap->cmdidx = CMD_USER_BUF;
2834 eap->argt = uc->uc_argt;
2835 eap->useridx = j;
2836
2837# ifdef FEAT_CMDL_COMPL
2838 if (compl != NULL)
2839 *compl = uc->uc_compl;
2840# ifdef FEAT_EVAL
2841 if (xp != NULL)
2842 {
2843 xp->xp_arg = uc->uc_compl_arg;
2844 xp->xp_scriptID = uc->uc_scriptID;
2845 }
2846# endif
2847# endif
2848 /* Do not search for further abbreviations
2849 * if this is an exact match. */
2850 matchlen = k;
2851 if (k == len && *np == NUL)
2852 {
2853 if (full != NULL)
2854 *full = TRUE;
2855 amb_local = FALSE;
2856 break;
2857 }
2858 }
2859 }
2860 }
2861
2862 /* Stop if we found a full match or searched all. */
2863 if (j < gap->ga_len || gap == &ucmds)
2864 break;
2865 gap = &ucmds;
2866 }
2867
2868 /* Only found ambiguous matches. */
2869 if (amb_local)
2870 {
2871 if (xp != NULL)
2872 xp->xp_context = EXPAND_UNSUCCESSFUL;
2873 return NULL;
2874 }
2875
2876 /* The match we found may be followed immediately by a number. Move "p"
2877 * back to point to it. */
2878 if (found || possible)
2879 return p + (matchlen - len);
2880 return p;
2881}
2882#endif
2883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884#if defined(FEAT_EVAL) || defined(PROTO)
2885/*
2886 * Return > 0 if an Ex command "name" exists.
2887 * Return 2 if there is an exact match.
2888 * Return 3 if there is an ambiguous match.
2889 */
2890 int
2891cmd_exists(name)
2892 char_u *name;
2893{
2894 exarg_T ea;
2895 int full = FALSE;
2896 int i;
2897 int j;
2898 static struct cmdmod
2899 {
2900 char *name;
2901 int minlen;
2902 } cmdmods[] = {
2903 {"aboveleft", 3},
2904 {"belowright", 3},
2905 {"botright", 2},
2906 {"browse", 3},
2907 {"confirm", 4},
2908 {"hide", 3},
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002909 {"keepalt", 5},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {"keepjumps", 5},
2911 {"keepmarks", 3},
2912 {"leftabove", 5},
2913 {"lockmarks", 3},
2914 {"rightbelow", 6},
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002915 {"sandbox", 3},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 {"silent", 3},
2917 {"topleft", 2},
2918 {"verbose", 4},
2919 {"vertical", 4},
2920 };
2921
2922 /* Check command modifiers. */
2923 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i)
2924 {
2925 for (j = 0; name[j] != NUL; ++j)
2926 if (name[j] != cmdmods[i].name[j])
2927 break;
2928 if (name[j] == NUL && j >= cmdmods[i].minlen)
2929 return (cmdmods[i].name[j] == NUL ? 2 : 1);
2930 }
2931
2932 /* Check built-in commands and user defined commands. */
2933 ea.cmd = name;
2934 ea.cmdidx = (cmdidx_T)0;
2935 if (find_command(&ea, &full) == NULL)
2936 return 3;
2937 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
2938}
2939#endif
2940
2941/*
2942 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2943 * we don't need/want deleted. Maybe this could be done better if we didn't
2944 * repeat all this stuff. The only problem is that they may not stay
2945 * perfectly compatible with each other, but then the command line syntax
2946 * probably won't change that much -- webb.
2947 */
2948 char_u *
2949set_one_cmd_context(xp, buff)
2950 expand_T *xp;
2951 char_u *buff; /* buffer for command string */
2952{
2953 char_u *p;
2954 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002955 int len = 0;
2956 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
2958 int compl = EXPAND_NOTHING;
2959#endif
2960#ifdef FEAT_CMDL_COMPL
2961 int delim;
2962#endif
2963 int forceit = FALSE;
2964 int usefilter = FALSE; /* filter instead of file name */
2965
2966 xp->xp_pattern = buff;
2967 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
2968 xp->xp_backslash = XP_BS_NONE;
2969#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
2970 xp->xp_arg = NULL;
2971#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002972 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974/*
2975 * 2. skip comment lines and leading space, colons or bars
2976 */
2977 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2978 ;
2979 xp->xp_pattern = cmd;
2980
2981 if (*cmd == NUL)
2982 return NULL;
2983 if (*cmd == '"') /* ignore comment lines */
2984 {
2985 xp->xp_context = EXPAND_NOTHING;
2986 return NULL;
2987 }
2988
2989/*
2990 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
2991 */
2992 cmd = skip_range(cmd, &xp->xp_context);
2993
2994/*
2995 * 4. parse command
2996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 xp->xp_pattern = cmd;
2998 if (*cmd == NUL)
2999 return NULL;
3000 if (*cmd == '"')
3001 {
3002 xp->xp_context = EXPAND_NOTHING;
3003 return NULL;
3004 }
3005
3006 if (*cmd == '|' || *cmd == '\n')
3007 return cmd + 1; /* There's another command */
3008
3009 /*
3010 * Isolate the command and search for it in the command table.
3011 * Exceptions:
3012 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003013 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3015 */
3016 if (*cmd == 'k' && cmd[1] != 'e')
3017 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003018 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 p = cmd + 1;
3020 }
3021 else
3022 {
3023 p = cmd;
3024 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3025 ++p;
3026 /* check for non-alpha command */
3027 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3028 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003029 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003031 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 {
3033 xp->xp_context = EXPAND_UNSUCCESSFUL;
3034 return NULL;
3035 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003036 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
3037 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3038 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 break;
3040
3041#ifdef FEAT_USR_CMDS
3042 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3043 {
3044 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3045 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003046 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 }
3048#endif
3049 }
3050
3051 /*
3052 * If the cursor is touching the command, and it ends in an alpha-numeric
3053 * character, complete the command name.
3054 */
3055 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3056 return NULL;
3057
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003058 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 {
3060 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3061 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003062 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 p = cmd + 1;
3064 }
3065#ifdef FEAT_USR_CMDS
3066 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3067 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003068 ea.cmd = cmd;
3069 p = find_ucmd(&ea, p, NULL, xp,
3070# if defined(FEAT_CMDL_COMPL)
3071 &compl
3072# else
3073 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003075 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 }
3077#endif
3078 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003079 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
3081 /* Not still touching the command and it was an illegal one */
3082 xp->xp_context = EXPAND_UNSUCCESSFUL;
3083 return NULL;
3084 }
3085
3086 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3087
3088 if (*p == '!') /* forced commands */
3089 {
3090 forceit = TRUE;
3091 ++p;
3092 }
3093
3094/*
3095 * 5. parse arguments
3096 */
3097#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003098 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003100 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101
3102 arg = skipwhite(p);
3103
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003104 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 {
3106 if (*arg == '>') /* append */
3107 {
3108 if (*++arg == '>')
3109 ++arg;
3110 arg = skipwhite(arg);
3111 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003112 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 {
3114 ++arg;
3115 usefilter = TRUE;
3116 }
3117 }
3118
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003119 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 {
3121 usefilter = forceit; /* :r! filter if forced */
3122 if (*arg == '!') /* :r !filter */
3123 {
3124 ++arg;
3125 usefilter = TRUE;
3126 }
3127 }
3128
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003129 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 {
3131 while (*arg == *cmd) /* allow any number of '>' or '<' */
3132 ++arg;
3133 arg = skipwhite(arg);
3134 }
3135
3136 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003137 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
3139 /* Check if we're in the +command */
3140 p = arg + 1;
3141 arg = skip_cmd_arg(arg, FALSE);
3142
3143 /* Still touching the command after '+'? */
3144 if (*arg == NUL)
3145 return p;
3146
3147 /* Skip space(s) after +command to get to the real argument */
3148 arg = skipwhite(arg);
3149 }
3150
3151 /*
3152 * Check for '|' to separate commands and '"' to start comments.
3153 * Don't do this for ":read !cmd" and ":write !cmd".
3154 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003155 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 {
3157 p = arg;
3158 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003159 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 p += 2;
3161 while (*p)
3162 {
3163 if (*p == Ctrl_V)
3164 {
3165 if (p[1] != NUL)
3166 ++p;
3167 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003168 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 || *p == '|' || *p == '\n')
3170 {
3171 if (*(p - 1) != '\\')
3172 {
3173 if (*p == '|' || *p == '\n')
3174 return p + 1;
3175 return NULL; /* It's a comment */
3176 }
3177 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003178 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 }
3180 }
3181
3182 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003183 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 vim_strchr((char_u *)"|\"", *arg) == NULL)
3185 return NULL;
3186
3187 /* Find start of last argument (argument just before cursor): */
3188 p = buff + STRLEN(buff);
3189 while (p != arg && *p != ' ' && *p != TAB)
3190 p--;
3191 if (*p == ' ' || *p == TAB)
3192 p++;
3193 xp->xp_pattern = p;
3194
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003195 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 {
3197 int in_quote = FALSE;
3198 char_u *bow = NULL; /* Beginning of word */
3199
3200 /*
3201 * Allow spaces within back-quotes to count as part of the argument
3202 * being expanded.
3203 */
3204 xp->xp_pattern = skipwhite(arg);
3205 for (p = xp->xp_pattern; *p; )
3206 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003207 if (*p == '\\' && p[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 ++p;
3209#ifdef SPACE_IN_FILENAME
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003210 else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211#else
3212 else if (vim_iswhite(*p))
3213#endif
3214 {
3215 p = skipwhite(p);
3216 if (in_quote)
3217 bow = p;
3218 else
3219 xp->xp_pattern = p;
3220 --p;
3221 }
3222 else if (*p == '`')
3223 {
3224 if (!in_quote)
3225 {
3226 xp->xp_pattern = p;
3227 bow = p + 1;
3228 }
3229 in_quote = !in_quote;
3230 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003231 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 }
3233
3234 /*
3235 * If we are still inside the quotes, and we passed a space, just
3236 * expand from there.
3237 */
3238 if (bow != NULL && in_quote)
3239 xp->xp_pattern = bow;
3240 xp->xp_context = EXPAND_FILES;
3241
3242 /* Check for environment variable */
3243 if (*xp->xp_pattern == '$'
3244#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3245 || *xp->xp_pattern == '%'
3246#endif
3247 )
3248 {
3249 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3250 if (!vim_isIDc(*p))
3251 break;
3252 if (*p == NUL)
3253 {
3254 xp->xp_context = EXPAND_ENV_VARS;
3255 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003256#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3257 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003258 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003259 compl = EXPAND_ENV_VARS;
3260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 }
3262 }
3263 }
3264
3265/*
3266 * 6. switch on command name
3267 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003268 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 {
3270 case CMD_cd:
3271 case CMD_chdir:
3272 case CMD_lcd:
3273 case CMD_lchdir:
3274 if (xp->xp_context == EXPAND_FILES)
3275 xp->xp_context = EXPAND_DIRECTORIES;
3276 break;
3277 case CMD_help:
3278 xp->xp_context = EXPAND_HELP;
3279 xp->xp_pattern = arg;
3280 break;
3281
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003282 /* Command modifiers: return the argument.
3283 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003285 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 case CMD_belowright:
3287 case CMD_botright:
3288 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003289 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003291 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 case CMD_folddoclosed:
3293 case CMD_folddoopen:
3294 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003295 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 case CMD_keepjumps:
3297 case CMD_keepmarks:
3298 case CMD_leftabove:
3299 case CMD_lockmarks:
3300 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003301 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 case CMD_silent:
3303 case CMD_topleft:
3304 case CMD_verbose:
3305 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003306 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 return arg;
3308
3309#ifdef FEAT_SEARCH_EXTRA
3310 case CMD_match:
3311 if (*arg == NUL || !ends_excmd(*arg))
3312 {
3313 /* Dummy call to clear variables. */
3314 set_context_in_highlight_cmd(xp, (char_u *)"link n");
3315 xp->xp_context = EXPAND_HIGHLIGHT;
3316 xp->xp_pattern = arg;
3317 arg = skipwhite(skiptowhite(arg));
3318 if (*arg != NUL)
3319 {
3320 xp->xp_context = EXPAND_NOTHING;
3321 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3322 }
3323 }
3324 return find_nextcmd(arg);
3325#endif
3326
3327#ifdef FEAT_CMDL_COMPL
3328/*
3329 * All completion for the +cmdline_compl feature goes here.
3330 */
3331
3332# ifdef FEAT_USR_CMDS
3333 case CMD_command:
3334 /* Check for attributes */
3335 while (*arg == '-')
3336 {
3337 arg++; /* Skip "-" */
3338 p = skiptowhite(arg);
3339 if (*p == NUL)
3340 {
3341 /* Cursor is still in the attribute */
3342 p = vim_strchr(arg, '=');
3343 if (p == NULL)
3344 {
3345 /* No "=", so complete attribute names */
3346 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3347 xp->xp_pattern = arg;
3348 return NULL;
3349 }
3350
3351 /* For the -complete and -nargs attributes, we complete
3352 * their arguments as well.
3353 */
3354 if (STRNICMP(arg, "complete", p - arg) == 0)
3355 {
3356 xp->xp_context = EXPAND_USER_COMPLETE;
3357 xp->xp_pattern = p + 1;
3358 return NULL;
3359 }
3360 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3361 {
3362 xp->xp_context = EXPAND_USER_NARGS;
3363 xp->xp_pattern = p + 1;
3364 return NULL;
3365 }
3366 return NULL;
3367 }
3368 arg = skipwhite(p);
3369 }
3370
3371 /* After the attributes comes the new command name */
3372 p = skiptowhite(arg);
3373 if (*p == NUL)
3374 {
3375 xp->xp_context = EXPAND_USER_COMMANDS;
3376 xp->xp_pattern = arg;
3377 break;
3378 }
3379
3380 /* And finally comes a normal command */
3381 return skipwhite(p);
3382
3383 case CMD_delcommand:
3384 xp->xp_context = EXPAND_USER_COMMANDS;
3385 xp->xp_pattern = arg;
3386 break;
3387# endif
3388
3389 case CMD_global:
3390 case CMD_vglobal:
3391 delim = *arg; /* get the delimiter */
3392 if (delim)
3393 ++arg; /* skip delimiter if there is one */
3394
3395 while (arg[0] != NUL && arg[0] != delim)
3396 {
3397 if (arg[0] == '\\' && arg[1] != NUL)
3398 ++arg;
3399 ++arg;
3400 }
3401 if (arg[0] != NUL)
3402 return arg + 1;
3403 break;
3404 case CMD_and:
3405 case CMD_substitute:
3406 delim = *arg;
3407 if (delim)
3408 {
3409 /* skip "from" part */
3410 ++arg;
3411 arg = skip_regexp(arg, delim, p_magic, NULL);
3412 }
3413 /* skip "to" part */
3414 while (arg[0] != NUL && arg[0] != delim)
3415 {
3416 if (arg[0] == '\\' && arg[1] != NUL)
3417 ++arg;
3418 ++arg;
3419 }
3420 if (arg[0] != NUL) /* skip delimiter */
3421 ++arg;
3422 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3423 ++arg;
3424 if (arg[0] != NUL)
3425 return arg;
3426 break;
3427 case CMD_isearch:
3428 case CMD_dsearch:
3429 case CMD_ilist:
3430 case CMD_dlist:
3431 case CMD_ijump:
3432 case CMD_psearch:
3433 case CMD_djump:
3434 case CMD_isplit:
3435 case CMD_dsplit:
3436 arg = skipwhite(skipdigits(arg)); /* skip count */
3437 if (*arg == '/') /* Match regexp, not just whole words */
3438 {
3439 for (++arg; *arg && *arg != '/'; arg++)
3440 if (*arg == '\\' && arg[1] != NUL)
3441 arg++;
3442 if (*arg)
3443 {
3444 arg = skipwhite(arg + 1);
3445
3446 /* Check for trailing illegal characters */
3447 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3448 xp->xp_context = EXPAND_NOTHING;
3449 else
3450 return arg;
3451 }
3452 }
3453 break;
3454#ifdef FEAT_AUTOCMD
3455 case CMD_autocmd:
3456 return set_context_in_autocmd(xp, arg, FALSE);
3457
3458 case CMD_doautocmd:
3459 return set_context_in_autocmd(xp, arg, TRUE);
3460#endif
3461 case CMD_set:
3462 set_context_in_set_cmd(xp, arg, 0);
3463 break;
3464 case CMD_setglobal:
3465 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3466 break;
3467 case CMD_setlocal:
3468 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3469 break;
3470 case CMD_tag:
3471 case CMD_stag:
3472 case CMD_ptag:
3473 case CMD_tselect:
3474 case CMD_stselect:
3475 case CMD_ptselect:
3476 case CMD_tjump:
3477 case CMD_stjump:
3478 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003479 if (*p_wop != NUL)
3480 xp->xp_context = EXPAND_TAGS_LISTFILES;
3481 else
3482 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 xp->xp_pattern = arg;
3484 break;
3485 case CMD_augroup:
3486 xp->xp_context = EXPAND_AUGROUP;
3487 xp->xp_pattern = arg;
3488 break;
3489#ifdef FEAT_SYN_HL
3490 case CMD_syntax:
3491 set_context_in_syntax_cmd(xp, arg);
3492 break;
3493#endif
3494#ifdef FEAT_EVAL
3495 case CMD_let:
3496 case CMD_if:
3497 case CMD_elseif:
3498 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003499 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 case CMD_echo:
3501 case CMD_echon:
3502 case CMD_execute:
3503 case CMD_echomsg:
3504 case CMD_echoerr:
3505 case CMD_call:
3506 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003507 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 break;
3509
3510 case CMD_unlet:
3511 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3512 arg = xp->xp_pattern + 1;
3513 xp->xp_context = EXPAND_USER_VARS;
3514 xp->xp_pattern = arg;
3515 break;
3516
3517 case CMD_function:
3518 case CMD_delfunction:
3519 xp->xp_context = EXPAND_USER_FUNC;
3520 xp->xp_pattern = arg;
3521 break;
3522
3523 case CMD_echohl:
3524 xp->xp_context = EXPAND_HIGHLIGHT;
3525 xp->xp_pattern = arg;
3526 break;
3527#endif
3528 case CMD_highlight:
3529 set_context_in_highlight_cmd(xp, arg);
3530 break;
3531#ifdef FEAT_LISTCMDS
3532 case CMD_bdelete:
3533 case CMD_bwipeout:
3534 case CMD_bunload:
3535 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3536 arg = xp->xp_pattern + 1;
3537 /*FALLTHROUGH*/
3538 case CMD_buffer:
3539 case CMD_sbuffer:
3540 case CMD_checktime:
3541 xp->xp_context = EXPAND_BUFFERS;
3542 xp->xp_pattern = arg;
3543 break;
3544#endif
3545#ifdef FEAT_USR_CMDS
3546 case CMD_USER:
3547 case CMD_USER_BUF:
3548 if (compl != EXPAND_NOTHING)
3549 {
3550 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003551 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 {
3553# ifdef FEAT_MENU
3554 if (compl == EXPAND_MENUS)
3555 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3556# endif
3557 if (compl == EXPAND_COMMANDS)
3558 return arg;
3559 if (compl == EXPAND_MAPPINGS)
3560 return set_context_in_map_cmd(xp, (char_u *)"map",
3561 arg, forceit, FALSE, FALSE, CMD_map);
3562 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3563 arg = xp->xp_pattern + 1;
3564 xp->xp_pattern = arg;
3565 }
3566 xp->xp_context = compl;
3567 }
3568 break;
3569#endif
3570 case CMD_map: case CMD_noremap:
3571 case CMD_nmap: case CMD_nnoremap:
3572 case CMD_vmap: case CMD_vnoremap:
3573 case CMD_omap: case CMD_onoremap:
3574 case CMD_imap: case CMD_inoremap:
3575 case CMD_cmap: case CMD_cnoremap:
3576 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003577 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 case CMD_unmap:
3579 case CMD_nunmap:
3580 case CMD_vunmap:
3581 case CMD_ounmap:
3582 case CMD_iunmap:
3583 case CMD_cunmap:
3584 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003585 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 case CMD_abbreviate: case CMD_noreabbrev:
3587 case CMD_cabbrev: case CMD_cnoreabbrev:
3588 case CMD_iabbrev: case CMD_inoreabbrev:
3589 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003590 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 case CMD_unabbreviate:
3592 case CMD_cunabbrev:
3593 case CMD_iunabbrev:
3594 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003595 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596#ifdef FEAT_MENU
3597 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3598 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3599 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3600 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3601 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3602 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3603 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3604 case CMD_tmenu: case CMD_tunmenu:
3605 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3606 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3607#endif
3608
3609 case CMD_colorscheme:
3610 xp->xp_context = EXPAND_COLORS;
3611 xp->xp_pattern = arg;
3612 break;
3613
3614 case CMD_compiler:
3615 xp->xp_context = EXPAND_COMPILER;
3616 xp->xp_pattern = arg;
3617 break;
3618
3619#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3620 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3621 case CMD_language:
3622 if (*skiptowhite(arg) == NUL)
3623 {
3624 xp->xp_context = EXPAND_LANGUAGE;
3625 xp->xp_pattern = arg;
3626 }
3627 else
3628 xp->xp_context = EXPAND_NOTHING;
3629 break;
3630#endif
3631
3632#endif /* FEAT_CMDL_COMPL */
3633
3634 default:
3635 break;
3636 }
3637 return NULL;
3638}
3639
3640/*
3641 * skip a range specifier of the form: addr [,addr] [;addr] ..
3642 *
3643 * Backslashed delimiters after / or ? will be skipped, and commands will
3644 * not be expanded between /'s and ?'s or after "'".
3645 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003646 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 * Returns the "cmd" pointer advanced to beyond the range.
3648 */
3649 char_u *
3650skip_range(cmd, ctx)
3651 char_u *cmd;
3652 int *ctx; /* pointer to xp_context or NULL */
3653{
3654 int delim;
3655
Bram Moolenaardf177f62005-02-22 08:39:57 +00003656 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
3658 if (*cmd == '\'')
3659 {
3660 if (*++cmd == NUL && ctx != NULL)
3661 *ctx = EXPAND_NOTHING;
3662 }
3663 else if (*cmd == '/' || *cmd == '?')
3664 {
3665 delim = *cmd++;
3666 while (*cmd != NUL && *cmd != delim)
3667 if (*cmd++ == '\\' && *cmd != NUL)
3668 ++cmd;
3669 if (*cmd == NUL && ctx != NULL)
3670 *ctx = EXPAND_NOTHING;
3671 }
3672 if (*cmd != NUL)
3673 ++cmd;
3674 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003675
3676 /* Skip ":" and white space. */
3677 while (*cmd == ':')
3678 cmd = skipwhite(cmd + 1);
3679
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 return cmd;
3681}
3682
3683/*
3684 * get a single EX address
3685 *
3686 * Set ptr to the next character after the part that was interpreted.
3687 * Set ptr to NULL when an error is encountered.
3688 *
3689 * Return MAXLNUM when no Ex address was found.
3690 */
3691 static linenr_T
3692get_address(ptr, skip, to_other_file)
3693 char_u **ptr;
3694 int skip; /* only skip the address, don't use it */
3695 int to_other_file; /* flag: may jump to other file */
3696{
3697 int c;
3698 int i;
3699 long n;
3700 char_u *cmd;
3701 pos_T pos;
3702 pos_T *fp;
3703 linenr_T lnum;
3704
3705 cmd = skipwhite(*ptr);
3706 lnum = MAXLNUM;
3707 do
3708 {
3709 switch (*cmd)
3710 {
3711 case '.': /* '.' - Cursor position */
3712 ++cmd;
3713 lnum = curwin->w_cursor.lnum;
3714 break;
3715
3716 case '$': /* '$' - last line */
3717 ++cmd;
3718 lnum = curbuf->b_ml.ml_line_count;
3719 break;
3720
3721 case '\'': /* ''' - mark */
3722 if (*++cmd == NUL)
3723 {
3724 cmd = NULL;
3725 goto error;
3726 }
3727 if (skip)
3728 ++cmd;
3729 else
3730 {
3731 /* Only accept a mark in another file when it is
3732 * used by itself: ":'M". */
3733 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3734 ++cmd;
3735 if (fp == (pos_T *)-1)
3736 /* Jumped to another file. */
3737 lnum = curwin->w_cursor.lnum;
3738 else
3739 {
3740 if (check_mark(fp) == FAIL)
3741 {
3742 cmd = NULL;
3743 goto error;
3744 }
3745 lnum = fp->lnum;
3746 }
3747 }
3748 break;
3749
3750 case '/':
3751 case '?': /* '/' or '?' - search */
3752 c = *cmd++;
3753 if (skip) /* skip "/pat/" */
3754 {
3755 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3756 if (*cmd == c)
3757 ++cmd;
3758 }
3759 else
3760 {
3761 pos = curwin->w_cursor; /* save curwin->w_cursor */
3762 /*
3763 * When '/' or '?' follows another address, start
3764 * from there.
3765 */
3766 if (lnum != MAXLNUM)
3767 curwin->w_cursor.lnum = lnum;
3768 /*
3769 * Start a forward search at the end of the line.
3770 * Start a backward search at the start of the line.
3771 * This makes sure we never match in the current
3772 * line, and can match anywhere in the
3773 * next/previous line.
3774 */
3775 if (c == '/')
3776 curwin->w_cursor.col = MAXCOL;
3777 else
3778 curwin->w_cursor.col = 0;
3779 searchcmdlen = 0;
3780 if (!do_search(NULL, c, cmd, 1L,
3781 SEARCH_HIS + SEARCH_MSG + SEARCH_START))
3782 {
3783 curwin->w_cursor = pos;
3784 cmd = NULL;
3785 goto error;
3786 }
3787 lnum = curwin->w_cursor.lnum;
3788 curwin->w_cursor = pos;
3789 /* adjust command string pointer */
3790 cmd += searchcmdlen;
3791 }
3792 break;
3793
3794 case '\\': /* "\?", "\/" or "\&", repeat search */
3795 ++cmd;
3796 if (*cmd == '&')
3797 i = RE_SUBST;
3798 else if (*cmd == '?' || *cmd == '/')
3799 i = RE_SEARCH;
3800 else
3801 {
3802 EMSG(_(e_backslash));
3803 cmd = NULL;
3804 goto error;
3805 }
3806
3807 if (!skip)
3808 {
3809 /*
3810 * When search follows another address, start from
3811 * there.
3812 */
3813 if (lnum != MAXLNUM)
3814 pos.lnum = lnum;
3815 else
3816 pos.lnum = curwin->w_cursor.lnum;
3817
3818 /*
3819 * Start the search just like for the above
3820 * do_search().
3821 */
3822 if (*cmd != '?')
3823 pos.col = MAXCOL;
3824 else
3825 pos.col = 0;
3826 if (searchit(curwin, curbuf, &pos,
3827 *cmd == '?' ? BACKWARD : FORWARD,
3828 (char_u *)"", 1L,
3829 SEARCH_MSG + SEARCH_START, i) != FAIL)
3830 lnum = pos.lnum;
3831 else
3832 {
3833 cmd = NULL;
3834 goto error;
3835 }
3836 }
3837 ++cmd;
3838 break;
3839
3840 default:
3841 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3842 lnum = getdigits(&cmd);
3843 }
3844
3845 for (;;)
3846 {
3847 cmd = skipwhite(cmd);
3848 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3849 break;
3850
3851 if (lnum == MAXLNUM)
3852 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
3853 if (VIM_ISDIGIT(*cmd))
3854 i = '+'; /* "number" is same as "+number" */
3855 else
3856 i = *cmd++;
3857 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3858 n = 1;
3859 else
3860 n = getdigits(&cmd);
3861 if (i == '-')
3862 lnum -= n;
3863 else
3864 lnum += n;
3865 }
3866 } while (*cmd == '/' || *cmd == '?');
3867
3868error:
3869 *ptr = cmd;
3870 return lnum;
3871}
3872
3873/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003874 * Get flags from an Ex command argument.
3875 */
3876 static void
3877get_flags(eap)
3878 exarg_T *eap;
3879{
3880 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3881 {
3882 if (*eap->arg == 'l')
3883 eap->flags |= EXFLAG_LIST;
3884 else if (*eap->arg == 'p')
3885 eap->flags |= EXFLAG_PRINT;
3886 else
3887 eap->flags |= EXFLAG_NR;
3888 eap->arg = skipwhite(eap->arg + 1);
3889 }
3890}
3891
3892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 * Function called for command which is Not Implemented. NI!
3894 */
3895 void
3896ex_ni(eap)
3897 exarg_T *eap;
3898{
3899 if (!eap->skip)
3900 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
3901}
3902
3903#if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003904 || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905/*
3906 * Function called for script command which is Not Implemented. NI!
3907 * Skips over ":perl <<EOF" constructs.
3908 */
3909 static void
3910ex_script_ni(eap)
3911 exarg_T *eap;
3912{
3913 if (!eap->skip)
3914 ex_ni(eap);
3915 else
3916 vim_free(script_get(eap, eap->arg));
3917}
3918#endif
3919
3920/*
3921 * Check range in Ex command for validity.
3922 * Return NULL when valid, error message when invalid.
3923 */
3924 static char_u *
3925invalid_range(eap)
3926 exarg_T *eap;
3927{
3928 if ( eap->line1 < 0
3929 || eap->line2 < 0
3930 || eap->line1 > eap->line2
3931 || ((eap->argt & RANGE)
3932 && !(eap->argt & NOTADR)
3933 && eap->line2 > curbuf->b_ml.ml_line_count
3934#ifdef FEAT_DIFF
3935 + (eap->cmdidx == CMD_diffget)
3936#endif
3937 ))
3938 return (char_u *)_(e_invrange);
3939 return NULL;
3940}
3941
3942/*
3943 * Correct the range for zero line number, if required.
3944 */
3945 static void
3946correct_range(eap)
3947 exarg_T *eap;
3948{
3949 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
3950 {
3951 if (eap->line1 == 0)
3952 eap->line1 = 1;
3953 if (eap->line2 == 0)
3954 eap->line2 = 1;
3955 }
3956}
3957
Bram Moolenaar748bf032005-02-02 23:04:36 +00003958#ifdef FEAT_QUICKFIX
3959static char_u *skip_grep_pat __ARGS((exarg_T *eap));
3960
3961/*
3962 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
3963 * pattern. Otherwise return eap->arg.
3964 */
3965 static char_u *
3966skip_grep_pat(eap)
3967 exarg_T *eap;
3968{
3969 char_u *p = eap->arg;
3970
3971 if (*p != NUL && (eap->cmdidx == CMD_vimgrep
3972 || eap->cmdidx == CMD_vimgrepadd || grep_internal(eap->cmdidx)))
3973 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003974 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003975 if (p == NULL)
3976 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003977 }
3978 return p;
3979}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003980
3981/*
3982 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
3983 * in the command line, so that things like % get expanded.
3984 */
3985 static char_u *
3986replace_makeprg(eap, p, cmdlinep)
3987 exarg_T *eap;
3988 char_u *p;
3989 char_u **cmdlinep;
3990{
3991 char_u *new_cmdline;
3992 char_u *program;
3993 char_u *pos;
3994 char_u *ptr;
3995 int len;
3996 int i;
3997
3998 /*
3999 * Don't do it when ":vimgrep" is used for ":grep".
4000 */
4001 if ((eap->cmdidx == CMD_make
4002 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_grepadd)
4003 && !grep_internal(eap->cmdidx))
4004 {
4005 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_grepadd)
4006 {
4007 if (*curbuf->b_p_gp == NUL)
4008 program = p_gp;
4009 else
4010 program = curbuf->b_p_gp;
4011 }
4012 else
4013 {
4014 if (*curbuf->b_p_mp == NUL)
4015 program = p_mp;
4016 else
4017 program = curbuf->b_p_mp;
4018 }
4019
4020 p = skipwhite(p);
4021
4022 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4023 {
4024 /* replace $* by given arguments */
4025 i = 1;
4026 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4027 ++i;
4028 len = (int)STRLEN(p);
4029 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4030 if (new_cmdline == NULL)
4031 return NULL; /* out of memory */
4032 ptr = new_cmdline;
4033 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4034 {
4035 i = (int)(pos - program);
4036 STRNCPY(ptr, program, i);
4037 STRCPY(ptr += i, p);
4038 ptr += len;
4039 program = pos + 2;
4040 }
4041 STRCPY(ptr, program);
4042 }
4043 else
4044 {
4045 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4046 if (new_cmdline == NULL)
4047 return NULL; /* out of memory */
4048 STRCPY(new_cmdline, program);
4049 STRCAT(new_cmdline, " ");
4050 STRCAT(new_cmdline, p);
4051 }
4052 msg_make(p);
4053
4054 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4055 vim_free(*cmdlinep);
4056 *cmdlinep = new_cmdline;
4057 p = new_cmdline;
4058 }
4059 return p;
4060}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004061#endif
4062
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063/*
4064 * Expand file name in Ex command argument.
4065 * Return FAIL for failure, OK otherwise.
4066 */
4067 int
4068expand_filename(eap, cmdlinep, errormsgp)
4069 exarg_T *eap;
4070 char_u **cmdlinep;
4071 char_u **errormsgp;
4072{
4073 int has_wildcards; /* need to expand wildcards */
4074 char_u *repl;
4075 int srclen;
4076 char_u *p;
4077 int n;
4078
Bram Moolenaar748bf032005-02-02 23:04:36 +00004079#ifdef FEAT_QUICKFIX
4080 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4081 p = skip_grep_pat(eap);
4082#else
4083 p = eap->arg;
4084#endif
4085
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 /*
4087 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4088 * the file name contains a wildcard it should not cause expanding.
4089 * (it will be expanded anyway if there is a wildcard before replacing).
4090 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004091 has_wildcards = mch_has_wildcard(p);
4092 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004094#ifdef FEAT_EVAL
4095 /* Skip over `=expr`, wildcards in it are not expanded. */
4096 if (p[0] == '`' && p[1] == '=')
4097 {
4098 p += 2;
4099 (void)skip_expr(&p);
4100 if (*p == '`')
4101 ++p;
4102 continue;
4103 }
4104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 /*
4106 * Quick check if this cannot be the start of a special string.
4107 * Also removes backslash before '%', '#' and '<'.
4108 */
4109 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4110 {
4111 ++p;
4112 continue;
4113 }
4114
4115 /*
4116 * Try to find a match at this position.
4117 */
4118 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg);
4119 if (*errormsgp != NULL) /* error detected */
4120 return FAIL;
4121 if (repl == NULL) /* no match found */
4122 {
4123 p += srclen;
4124 continue;
4125 }
4126
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004127 /* Wildcards won't be expanded below, the replacement is taken
4128 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4129 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4130 {
4131 char_u *l = repl;
4132
4133 repl = expand_env_save(repl);
4134 vim_free(l);
4135 }
4136
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 /* Need to escape white space et al. with a backslash. Don't do this
4138 * for shell commands (may have to use quotes instead). Don't do this
4139 * for non-unix systems when there is a single argument (spaces don't
4140 * separate arguments then). */
4141 if (!eap->usefilter
4142 && eap->cmdidx != CMD_bang
4143 && eap->cmdidx != CMD_make
4144 && eap->cmdidx != CMD_grep
4145 && eap->cmdidx != CMD_grepadd
4146#ifndef UNIX
4147 && !(eap->argt & NOSPC)
4148#endif
4149 )
4150 {
4151 char_u *l;
4152#ifdef BACKSLASH_IN_FILENAME
4153 /* Don't escape a backslash here, because rem_backslash() doesn't
4154 * remove it later. */
4155 static char_u *nobslash = (char_u *)" \t\"|";
4156# define ESCAPE_CHARS nobslash
4157#else
4158# define ESCAPE_CHARS escape_chars
4159#endif
4160
4161 for (l = repl; *l; ++l)
4162 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4163 {
4164 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4165 if (l != NULL)
4166 {
4167 vim_free(repl);
4168 repl = l;
4169 }
4170 break;
4171 }
4172 }
4173
4174 /* For a shell command a '!' must be escaped. */
4175 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaarea424162005-06-16 21:51:00 +00004176 && vim_strpbrk(repl, (char_u *)"!&;()") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
4178 char_u *l;
4179
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004180 l = vim_strsave_escaped(repl, (char_u *)"!&;()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 if (l != NULL)
4182 {
4183 vim_free(repl);
4184 repl = l;
4185 /* For a sh-like shell escape it another time. */
4186 if (strstr((char *)p_sh, "sh") != NULL)
4187 {
4188 l = vim_strsave_escaped(repl, (char_u *)"!");
4189 if (l != NULL)
4190 {
4191 vim_free(repl);
4192 repl = l;
4193 }
4194 }
4195 }
4196 }
4197
4198 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4199 vim_free(repl);
4200 if (p == NULL)
4201 return FAIL;
4202 }
4203
4204 /*
4205 * One file argument: Expand wildcards.
4206 * Don't do this with ":r !command" or ":w !command".
4207 */
4208 if ((eap->argt & NOSPC) && !eap->usefilter)
4209 {
4210 /*
4211 * May do this twice:
4212 * 1. Replace environment variables.
4213 * 2. Replace any other wildcards, remove backslashes.
4214 */
4215 for (n = 1; n <= 2; ++n)
4216 {
4217 if (n == 2)
4218 {
4219#ifdef UNIX
4220 /*
4221 * Only for Unix we check for more than one file name.
4222 * For other systems spaces are considered to be part
4223 * of the file name.
4224 * Only check here if there is no wildcard, otherwise
4225 * ExpandOne() will check for errors. This allows
4226 * ":e `ls ve*.c`" on Unix.
4227 */
4228 if (!has_wildcards)
4229 for (p = eap->arg; *p; ++p)
4230 {
4231 /* skip escaped characters */
4232 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4233 ++p;
4234 else if (vim_iswhite(*p))
4235 {
4236 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4237 return FAIL;
4238 }
4239 }
4240#endif
4241
4242 /*
4243 * Halve the number of backslashes (this is Vi compatible).
4244 * For Unix and OS/2, when wildcards are expanded, this is
4245 * done by ExpandOne() below.
4246 */
4247#if defined(UNIX) || defined(OS2)
4248 if (!has_wildcards)
4249#endif
4250 backslash_halve(eap->arg);
4251#ifdef MACOS_CLASSIC
4252 /*
4253 * translate unix-like path components
4254 */
4255 slash_n_colon_adjust(eap->arg);
4256#endif
4257 }
4258
4259 if (has_wildcards)
4260 {
4261 if (n == 1)
4262 {
4263 /*
4264 * First loop: May expand environment variables. This
4265 * can be done much faster with expand_env() than with
4266 * something else (e.g., calling a shell).
4267 * After expanding environment variables, check again
4268 * if there are still wildcards present.
4269 */
4270 if (vim_strchr(eap->arg, '$') != NULL
4271 || vim_strchr(eap->arg, '~') != NULL)
4272 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004273 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
4274 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 has_wildcards = mch_has_wildcard(NameBuff);
4276 p = NameBuff;
4277 }
4278 else
4279 p = NULL;
4280 }
4281 else /* n == 2 */
4282 {
4283 expand_T xpc;
4284
4285 ExpandInit(&xpc);
4286 xpc.xp_context = EXPAND_FILES;
4287 p = ExpandOne(&xpc, eap->arg, NULL,
4288 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4289 WILD_EXPAND_FREE);
4290 ExpandCleanup(&xpc);
4291 if (p == NULL)
4292 return FAIL;
4293 }
4294 if (p != NULL)
4295 {
4296 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4297 p, cmdlinep);
4298 if (n == 2) /* p came from ExpandOne() */
4299 vim_free(p);
4300 }
4301 }
4302 }
4303 }
4304 return OK;
4305}
4306
4307/*
4308 * Replace part of the command line, keeping eap->cmd, eap->arg and
4309 * eap->nextcmd correct.
4310 * "src" points to the part that is to be replaced, of length "srclen".
4311 * "repl" is the replacement string.
4312 * Returns a pointer to the character after the replaced string.
4313 * Returns NULL for failure.
4314 */
4315 static char_u *
4316repl_cmdline(eap, src, srclen, repl, cmdlinep)
4317 exarg_T *eap;
4318 char_u *src;
4319 int srclen;
4320 char_u *repl;
4321 char_u **cmdlinep;
4322{
4323 int len;
4324 int i;
4325 char_u *new_cmdline;
4326
4327 /*
4328 * The new command line is build in new_cmdline[].
4329 * First allocate it.
4330 * Careful: a "+cmd" argument may have been NUL terminated.
4331 */
4332 len = (int)STRLEN(repl);
4333 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
4334 if (eap->nextcmd)
4335 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4336 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4337 return NULL; /* out of memory! */
4338
4339 /*
4340 * Copy the stuff before the expanded part.
4341 * Copy the expanded stuff.
4342 * Copy what came after the expanded part.
4343 * Copy the next commands, if there are any.
4344 */
4345 i = (int)(src - *cmdlinep); /* length of part before match */
4346 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004347
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 mch_memmove(new_cmdline + i, repl, (size_t)len);
4349 i += len; /* remember the end of the string */
4350 STRCPY(new_cmdline + i, src + srclen);
4351 src = new_cmdline + i; /* remember where to continue */
4352
4353 if (eap->nextcmd) /* append next command */
4354 {
4355 i = (int)STRLEN(new_cmdline) + 1;
4356 STRCPY(new_cmdline + i, eap->nextcmd);
4357 eap->nextcmd = new_cmdline + i;
4358 }
4359 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4360 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4361 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4362 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4363 vim_free(*cmdlinep);
4364 *cmdlinep = new_cmdline;
4365
4366 return src;
4367}
4368
4369/*
4370 * Check for '|' to separate commands and '"' to start comments.
4371 */
4372 void
4373separate_nextcmd(eap)
4374 exarg_T *eap;
4375{
4376 char_u *p;
4377
Bram Moolenaar86b68352004-12-27 21:59:20 +00004378#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004379 p = skip_grep_pat(eap);
4380#else
4381 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004382#endif
4383
4384 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
4386 if (*p == Ctrl_V)
4387 {
4388 if (eap->argt & (USECTRLV | XFILE))
4389 ++p; /* skip CTRL-V and next char */
4390 else
4391 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */
4392 if (*p == NUL) /* stop at NUL after CTRL-V */
4393 break;
4394 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004395
4396#ifdef FEAT_EVAL
4397 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004398 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004399 {
4400 p += 2;
4401 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004402 }
4403#endif
4404
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 /* Check for '"': start of comment or '|': next command */
4406 /* :@" and :*" do not start a comment!
4407 * :redir @" doesn't either. */
4408 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4409 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4410 || p != eap->arg)
4411 && (eap->cmdidx != CMD_redir
4412 || p != eap->arg + 1 || p[-1] != '@'))
4413 || *p == '|' || *p == '\n')
4414 {
4415 /*
4416 * We remove the '\' before the '|', unless USECTRLV is used
4417 * AND 'b' is present in 'cpoptions'.
4418 */
4419 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4420 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4421 {
4422 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */
4423 --p;
4424 }
4425 else
4426 {
4427 eap->nextcmd = check_nextcmd(p);
4428 *p = NUL;
4429 break;
4430 }
4431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004433
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4435 del_trailing_spaces(eap->arg);
4436}
4437
4438/*
4439 * get + command from ex argument
4440 */
4441 static char_u *
4442getargcmd(argp)
4443 char_u **argp;
4444{
4445 char_u *arg = *argp;
4446 char_u *command = NULL;
4447
4448 if (*arg == '+') /* +[command] */
4449 {
4450 ++arg;
4451 if (vim_isspace(*arg))
4452 command = dollar_command;
4453 else
4454 {
4455 command = arg;
4456 arg = skip_cmd_arg(command, TRUE);
4457 if (*arg != NUL)
4458 *arg++ = NUL; /* terminate command with NUL */
4459 }
4460
4461 arg = skipwhite(arg); /* skip over spaces */
4462 *argp = arg;
4463 }
4464 return command;
4465}
4466
4467/*
4468 * Find end of "+command" argument. Skip over "\ " and "\\".
4469 */
4470 static char_u *
4471skip_cmd_arg(p, rembs)
4472 char_u *p;
4473 int rembs; /* TRUE to halve the number of backslashes */
4474{
4475 while (*p && !vim_isspace(*p))
4476 {
4477 if (*p == '\\' && p[1] != NUL)
4478 {
4479 if (rembs)
4480 mch_memmove(p, p + 1, STRLEN(p));
4481 else
4482 ++p;
4483 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004484 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486 return p;
4487}
4488
4489/*
4490 * Get "++opt=arg" argument.
4491 * Return FAIL or OK.
4492 */
4493 static int
4494getargopt(eap)
4495 exarg_T *eap;
4496{
4497 char_u *arg = eap->arg + 2;
4498 int *pp = NULL;
4499#ifdef FEAT_MBYTE
4500 char_u *p;
4501#endif
4502
4503 /* ":edit ++[no]bin[ary] file" */
4504 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4505 {
4506 if (*arg == 'n')
4507 {
4508 arg += 2;
4509 eap->force_bin = FORCE_NOBIN;
4510 }
4511 else
4512 eap->force_bin = FORCE_BIN;
4513 if (!checkforcmd(&arg, "binary", 3))
4514 return FAIL;
4515 eap->arg = skipwhite(arg);
4516 return OK;
4517 }
4518
4519 if (STRNCMP(arg, "ff", 2) == 0)
4520 {
4521 arg += 2;
4522 pp = &eap->force_ff;
4523 }
4524 else if (STRNCMP(arg, "fileformat", 10) == 0)
4525 {
4526 arg += 10;
4527 pp = &eap->force_ff;
4528 }
4529#ifdef FEAT_MBYTE
4530 else if (STRNCMP(arg, "enc", 3) == 0)
4531 {
4532 arg += 3;
4533 pp = &eap->force_enc;
4534 }
4535 else if (STRNCMP(arg, "encoding", 8) == 0)
4536 {
4537 arg += 8;
4538 pp = &eap->force_enc;
4539 }
4540#endif
4541
4542 if (pp == NULL || *arg != '=')
4543 return FAIL;
4544
4545 ++arg;
4546 *pp = (int)(arg - eap->cmd);
4547 arg = skip_cmd_arg(arg, FALSE);
4548 eap->arg = skipwhite(arg);
4549 *arg = NUL;
4550
4551#ifdef FEAT_MBYTE
4552 if (pp == &eap->force_ff)
4553 {
4554#endif
4555 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4556 return FAIL;
4557#ifdef FEAT_MBYTE
4558 }
4559 else
4560 {
4561 /* Make 'fileencoding' lower case. */
4562 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4563 *p = TOLOWER_ASC(*p);
4564 }
4565#endif
4566
4567 return OK;
4568}
4569
4570/*
4571 * ":abbreviate" and friends.
4572 */
4573 static void
4574ex_abbreviate(eap)
4575 exarg_T *eap;
4576{
4577 do_exmap(eap, TRUE); /* almost the same as mapping */
4578}
4579
4580/*
4581 * ":map" and friends.
4582 */
4583 static void
4584ex_map(eap)
4585 exarg_T *eap;
4586{
4587 /*
4588 * If we are sourcing .exrc or .vimrc in current directory we
4589 * print the mappings for security reasons.
4590 */
4591 if (secure)
4592 {
4593 secure = 2;
4594 msg_outtrans(eap->cmd);
4595 msg_putchar('\n');
4596 }
4597 do_exmap(eap, FALSE);
4598}
4599
4600/*
4601 * ":unmap" and friends.
4602 */
4603 static void
4604ex_unmap(eap)
4605 exarg_T *eap;
4606{
4607 do_exmap(eap, FALSE);
4608}
4609
4610/*
4611 * ":mapclear" and friends.
4612 */
4613 static void
4614ex_mapclear(eap)
4615 exarg_T *eap;
4616{
4617 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4618}
4619
4620/*
4621 * ":abclear" and friends.
4622 */
4623 static void
4624ex_abclear(eap)
4625 exarg_T *eap;
4626{
4627 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4628}
4629
4630#ifdef FEAT_AUTOCMD
4631 static void
4632ex_autocmd(eap)
4633 exarg_T *eap;
4634{
4635 /*
4636 * Disallow auto commands from .exrc and .vimrc in current
4637 * directory for security reasons.
4638 */
4639 if (secure)
4640 {
4641 secure = 2;
4642 eap->errmsg = e_curdir;
4643 }
4644 else if (eap->cmdidx == CMD_autocmd)
4645 do_autocmd(eap->arg, eap->forceit);
4646 else
4647 do_augroup(eap->arg, eap->forceit);
4648}
4649
4650/*
4651 * ":doautocmd": Apply the automatic commands to the current buffer.
4652 */
4653 static void
4654ex_doautocmd(eap)
4655 exarg_T *eap;
4656{
4657 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004658 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659}
4660#endif
4661
4662#ifdef FEAT_LISTCMDS
4663/*
4664 * :[N]bunload[!] [N] [bufname] unload buffer
4665 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4666 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4667 */
4668 static void
4669ex_bunload(eap)
4670 exarg_T *eap;
4671{
4672 eap->errmsg = do_bufdel(
4673 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4674 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4675 : DOBUF_UNLOAD, eap->arg,
4676 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4677}
4678
4679/*
4680 * :[N]buffer [N] to buffer N
4681 * :[N]sbuffer [N] to buffer N
4682 */
4683 static void
4684ex_buffer(eap)
4685 exarg_T *eap;
4686{
4687 if (*eap->arg)
4688 eap->errmsg = e_trailing;
4689 else
4690 {
4691 if (eap->addr_count == 0) /* default is current buffer */
4692 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4693 else
4694 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4695 }
4696}
4697
4698/*
4699 * :[N]bmodified [N] to next mod. buffer
4700 * :[N]sbmodified [N] to next mod. buffer
4701 */
4702 static void
4703ex_bmodified(eap)
4704 exarg_T *eap;
4705{
4706 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4707}
4708
4709/*
4710 * :[N]bnext [N] to next buffer
4711 * :[N]sbnext [N] split and to next buffer
4712 */
4713 static void
4714ex_bnext(eap)
4715 exarg_T *eap;
4716{
4717 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4718}
4719
4720/*
4721 * :[N]bNext [N] to previous buffer
4722 * :[N]bprevious [N] to previous buffer
4723 * :[N]sbNext [N] split and to previous buffer
4724 * :[N]sbprevious [N] split and to previous buffer
4725 */
4726 static void
4727ex_bprevious(eap)
4728 exarg_T *eap;
4729{
4730 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4731}
4732
4733/*
4734 * :brewind to first buffer
4735 * :bfirst to first buffer
4736 * :sbrewind split and to first buffer
4737 * :sbfirst split and to first buffer
4738 */
4739 static void
4740ex_brewind(eap)
4741 exarg_T *eap;
4742{
4743 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
4744}
4745
4746/*
4747 * :blast to last buffer
4748 * :sblast split and to last buffer
4749 */
4750 static void
4751ex_blast(eap)
4752 exarg_T *eap;
4753{
4754 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
4755}
4756#endif
4757
4758 int
4759ends_excmd(c)
4760 int c;
4761{
4762 return (c == NUL || c == '|' || c == '"' || c == '\n');
4763}
4764
4765#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4766 || defined(PROTO)
4767/*
4768 * Return the next command, after the first '|' or '\n'.
4769 * Return NULL if not found.
4770 */
4771 char_u *
4772find_nextcmd(p)
4773 char_u *p;
4774{
4775 while (*p != '|' && *p != '\n')
4776 {
4777 if (*p == NUL)
4778 return NULL;
4779 ++p;
4780 }
4781 return (p + 1);
4782}
4783#endif
4784
4785/*
4786 * Check if *p is a separator between Ex commands.
4787 * Return NULL if it isn't, (p + 1) if it is.
4788 */
4789 char_u *
4790check_nextcmd(p)
4791 char_u *p;
4792{
4793 p = skipwhite(p);
4794 if (*p == '|' || *p == '\n')
4795 return (p + 1);
4796 else
4797 return NULL;
4798}
4799
4800/*
4801 * - if there are more files to edit
4802 * - and this is the last window
4803 * - and forceit not used
4804 * - and not repeated twice on a row
4805 * return FAIL and give error message if 'message' TRUE
4806 * return OK otherwise
4807 */
4808 static int
4809check_more(message, forceit)
4810 int message; /* when FALSE check only, no messages */
4811 int forceit;
4812{
4813 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4814
4815 if (!forceit && only_one_window() && ARGCOUNT > 1 && !arg_had_last
4816 && n >= 0 && quitmore == 0)
4817 {
4818 if (message)
4819 {
4820#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4821 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4822 {
4823 char_u buff[IOSIZE];
4824
4825 if (n == 1)
4826 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
4827 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004828 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 _("%d more files to edit. Quit anyway?"), n);
4830 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4831 return OK;
4832 return FAIL;
4833 }
4834#endif
4835 if (n == 1)
4836 EMSG(_("E173: 1 more file to edit"));
4837 else
4838 EMSGN(_("E173: %ld more files to edit"), n);
4839 quitmore = 2; /* next try to quit is allowed */
4840 }
4841 return FAIL;
4842 }
4843 return OK;
4844}
4845
4846#ifdef FEAT_CMDL_COMPL
4847/*
4848 * Function given to ExpandGeneric() to obtain the list of command names.
4849 */
4850/*ARGSUSED*/
4851 char_u *
4852get_command_name(xp, idx)
4853 expand_T *xp;
4854 int idx;
4855{
4856 if (idx >= (int)CMD_SIZE)
4857# ifdef FEAT_USR_CMDS
4858 return get_user_command_name(idx);
4859# else
4860 return NULL;
4861# endif
4862 return cmdnames[idx].cmd_name;
4863}
4864#endif
4865
4866#if defined(FEAT_USR_CMDS) || defined(PROTO)
4867static 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));
4868static void uc_list __ARGS((char_u *name, size_t name_len));
4869static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
4870static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
4871static 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));
4872
4873 static int
4874uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
4875 char_u *name;
4876 size_t name_len;
4877 char_u *rep;
4878 long argt;
4879 long def;
4880 int flags;
4881 int compl;
4882 char_u *compl_arg;
4883 int force;
4884{
4885 ucmd_T *cmd = NULL;
4886 char_u *p;
4887 int i;
4888 int cmp = 1;
4889 char_u *rep_buf = NULL;
4890 garray_T *gap;
4891
4892 replace_termcodes(rep, &rep_buf, FALSE, FALSE);
4893 if (rep_buf == NULL)
4894 {
4895 /* Can't replace termcodes - try using the string as is */
4896 rep_buf = vim_strsave(rep);
4897
4898 /* Give up if out of memory */
4899 if (rep_buf == NULL)
4900 return FAIL;
4901 }
4902
4903 /* get address of growarray: global or in curbuf */
4904 if (flags & UC_BUFFER)
4905 {
4906 gap = &curbuf->b_ucmds;
4907 if (gap->ga_itemsize == 0)
4908 ga_init2(gap, (int)sizeof(ucmd_T), 4);
4909 }
4910 else
4911 gap = &ucmds;
4912
4913 /* Search for the command in the already defined commands. */
4914 for (i = 0; i < gap->ga_len; ++i)
4915 {
4916 size_t len;
4917
4918 cmd = USER_CMD_GA(gap, i);
4919 len = STRLEN(cmd->uc_name);
4920 cmp = STRNCMP(name, cmd->uc_name, name_len);
4921 if (cmp == 0)
4922 {
4923 if (name_len < len)
4924 cmp = -1;
4925 else if (name_len > len)
4926 cmp = 1;
4927 }
4928
4929 if (cmp == 0)
4930 {
4931 if (!force)
4932 {
4933 EMSG(_("E174: Command already exists: add ! to replace it"));
4934 goto fail;
4935 }
4936
4937 vim_free(cmd->uc_rep);
4938 cmd->uc_rep = 0;
4939 break;
4940 }
4941
4942 /* Stop as soon as we pass the name to add */
4943 if (cmp < 0)
4944 break;
4945 }
4946
4947 /* Extend the array unless we're replacing an existing command */
4948 if (cmp != 0)
4949 {
4950 if (ga_grow(gap, 1) != OK)
4951 goto fail;
4952 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
4953 goto fail;
4954
4955 cmd = USER_CMD_GA(gap, i);
4956 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
4957
4958 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959
4960 cmd->uc_name = p;
4961 }
4962
4963 cmd->uc_rep = rep_buf;
4964 cmd->uc_argt = argt;
4965 cmd->uc_def = def;
4966 cmd->uc_compl = compl;
4967#ifdef FEAT_EVAL
4968 cmd->uc_scriptID = current_SID;
4969# ifdef FEAT_CMDL_COMPL
4970 cmd->uc_compl_arg = compl_arg;
4971# endif
4972#endif
4973
4974 return OK;
4975
4976fail:
4977 vim_free(rep_buf);
4978#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4979 vim_free(compl_arg);
4980#endif
4981 return FAIL;
4982}
4983
4984/*
4985 * List of names for completion for ":command" with the EXPAND_ flag.
4986 * Must be alphabetical for completion.
4987 */
4988static struct
4989{
4990 int expand;
4991 char *name;
4992} command_complete[] =
4993{
4994 {EXPAND_AUGROUP, "augroup"},
4995 {EXPAND_BUFFERS, "buffer"},
4996 {EXPAND_COMMANDS, "command"},
4997#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4998 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00004999 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000#endif
5001 {EXPAND_DIRECTORIES, "dir"},
5002 {EXPAND_ENV_VARS, "environment"},
5003 {EXPAND_EVENTS, "event"},
5004 {EXPAND_EXPRESSION, "expression"},
5005 {EXPAND_FILES, "file"},
5006 {EXPAND_FUNCTIONS, "function"},
5007 {EXPAND_HELP, "help"},
5008 {EXPAND_HIGHLIGHT, "highlight"},
5009 {EXPAND_MAPPINGS, "mapping"},
5010 {EXPAND_MENUS, "menu"},
5011 {EXPAND_SETTINGS, "option"},
5012 {EXPAND_TAGS, "tag"},
5013 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5014 {EXPAND_USER_VARS, "var"},
5015 {0, NULL}
5016};
5017
5018 static void
5019uc_list(name, name_len)
5020 char_u *name;
5021 size_t name_len;
5022{
5023 int i, j;
5024 int found = FALSE;
5025 ucmd_T *cmd;
5026 int len;
5027 long a;
5028 garray_T *gap;
5029
5030 gap = &curbuf->b_ucmds;
5031 for (;;)
5032 {
5033 for (i = 0; i < gap->ga_len; ++i)
5034 {
5035 cmd = USER_CMD_GA(gap, i);
5036 a = cmd->uc_argt;
5037
5038 /* Skip commands which don't match the requested prefix */
5039 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5040 continue;
5041
5042 /* Put out the title first time */
5043 if (!found)
5044 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5045 found = TRUE;
5046 msg_putchar('\n');
5047 if (got_int)
5048 break;
5049
5050 /* Special cases */
5051 msg_putchar(a & BANG ? '!' : ' ');
5052 msg_putchar(a & REGSTR ? '"' : ' ');
5053 msg_putchar(gap != &ucmds ? 'b' : ' ');
5054 msg_putchar(' ');
5055
5056 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5057 len = (int)STRLEN(cmd->uc_name) + 4;
5058
5059 do {
5060 msg_putchar(' ');
5061 ++len;
5062 } while (len < 16);
5063
5064 len = 0;
5065
5066 /* Arguments */
5067 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5068 {
5069 case 0: IObuff[len++] = '0'; break;
5070 case (EXTRA): IObuff[len++] = '*'; break;
5071 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5072 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5073 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5074 }
5075
5076 do {
5077 IObuff[len++] = ' ';
5078 } while (len < 5);
5079
5080 /* Range */
5081 if (a & (RANGE|COUNT))
5082 {
5083 if (a & COUNT)
5084 {
5085 /* -count=N */
5086 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5087 len += (int)STRLEN(IObuff + len);
5088 }
5089 else if (a & DFLALL)
5090 IObuff[len++] = '%';
5091 else if (cmd->uc_def >= 0)
5092 {
5093 /* -range=N */
5094 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5095 len += (int)STRLEN(IObuff + len);
5096 }
5097 else
5098 IObuff[len++] = '.';
5099 }
5100
5101 do {
5102 IObuff[len++] = ' ';
5103 } while (len < 11);
5104
5105 /* Completion */
5106 for (j = 0; command_complete[j].expand != 0; ++j)
5107 if (command_complete[j].expand == cmd->uc_compl)
5108 {
5109 STRCPY(IObuff + len, command_complete[j].name);
5110 len += (int)STRLEN(IObuff + len);
5111 break;
5112 }
5113
5114 do {
5115 IObuff[len++] = ' ';
5116 } while (len < 21);
5117
5118 IObuff[len] = '\0';
5119 msg_outtrans(IObuff);
5120
5121 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005122#ifdef FEAT_EVAL
5123 if (p_verbose > 0)
5124 last_set_msg(cmd->uc_scriptID);
5125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 out_flush();
5127 ui_breakcheck();
5128 if (got_int)
5129 break;
5130 }
5131 if (gap == &ucmds || i < gap->ga_len)
5132 break;
5133 gap = &ucmds;
5134 }
5135
5136 if (!found)
5137 MSG(_("No user-defined commands found"));
5138}
5139
5140 static char_u *
5141uc_fun_cmd()
5142{
5143 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5144 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5145 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5146 0xb9, 0x7f, 0};
5147 int i;
5148
5149 for (i = 0; fcmd[i]; ++i)
5150 IObuff[i] = fcmd[i] - 0x40;
5151 IObuff[i] = 0;
5152 return IObuff;
5153}
5154
5155 static int
5156uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5157 char_u *attr;
5158 size_t len;
5159 long *argt;
5160 long *def;
5161 int *flags;
5162 int *compl;
5163 char_u **compl_arg;
5164{
5165 char_u *p;
5166
5167 if (len == 0)
5168 {
5169 EMSG(_("E175: No attribute specified"));
5170 return FAIL;
5171 }
5172
5173 /* First, try the simple attributes (no arguments) */
5174 if (STRNICMP(attr, "bang", len) == 0)
5175 *argt |= BANG;
5176 else if (STRNICMP(attr, "buffer", len) == 0)
5177 *flags |= UC_BUFFER;
5178 else if (STRNICMP(attr, "register", len) == 0)
5179 *argt |= REGSTR;
5180 else if (STRNICMP(attr, "bar", len) == 0)
5181 *argt |= TRLBAR;
5182 else
5183 {
5184 int i;
5185 char_u *val = NULL;
5186 size_t vallen = 0;
5187 size_t attrlen = len;
5188
5189 /* Look for the attribute name - which is the part before any '=' */
5190 for (i = 0; i < (int)len; ++i)
5191 {
5192 if (attr[i] == '=')
5193 {
5194 val = &attr[i + 1];
5195 vallen = len - i - 1;
5196 attrlen = i;
5197 break;
5198 }
5199 }
5200
5201 if (STRNICMP(attr, "nargs", attrlen) == 0)
5202 {
5203 if (vallen == 1)
5204 {
5205 if (*val == '0')
5206 /* Do nothing - this is the default */;
5207 else if (*val == '1')
5208 *argt |= (EXTRA | NOSPC | NEEDARG);
5209 else if (*val == '*')
5210 *argt |= EXTRA;
5211 else if (*val == '?')
5212 *argt |= (EXTRA | NOSPC);
5213 else if (*val == '+')
5214 *argt |= (EXTRA | NEEDARG);
5215 else
5216 goto wrong_nargs;
5217 }
5218 else
5219 {
5220wrong_nargs:
5221 EMSG(_("E176: Invalid number of arguments"));
5222 return FAIL;
5223 }
5224 }
5225 else if (STRNICMP(attr, "range", attrlen) == 0)
5226 {
5227 *argt |= RANGE;
5228 if (vallen == 1 && *val == '%')
5229 *argt |= DFLALL;
5230 else if (val != NULL)
5231 {
5232 p = val;
5233 if (*def >= 0)
5234 {
5235two_count:
5236 EMSG(_("E177: Count cannot be specified twice"));
5237 return FAIL;
5238 }
5239
5240 *def = getdigits(&p);
5241 *argt |= (ZEROR | NOTADR);
5242
5243 if (p != val + vallen || vallen == 0)
5244 {
5245invalid_count:
5246 EMSG(_("E178: Invalid default value for count"));
5247 return FAIL;
5248 }
5249 }
5250 }
5251 else if (STRNICMP(attr, "count", attrlen) == 0)
5252 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005253 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254
5255 if (val != NULL)
5256 {
5257 p = val;
5258 if (*def >= 0)
5259 goto two_count;
5260
5261 *def = getdigits(&p);
5262
5263 if (p != val + vallen)
5264 goto invalid_count;
5265 }
5266
5267 if (*def < 0)
5268 *def = 0;
5269 }
5270 else if (STRNICMP(attr, "complete", attrlen) == 0)
5271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 if (val == NULL)
5273 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005274 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 return FAIL;
5276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005278 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5279 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
5282 else
5283 {
5284 char_u ch = attr[len];
5285 attr[len] = '\0';
5286 EMSG2(_("E181: Invalid attribute: %s"), attr);
5287 attr[len] = ch;
5288 return FAIL;
5289 }
5290 }
5291
5292 return OK;
5293}
5294
5295 static void
5296ex_command(eap)
5297 exarg_T *eap;
5298{
5299 char_u *name;
5300 char_u *end;
5301 char_u *p;
5302 long argt = 0;
5303 long def = -1;
5304 int flags = 0;
5305 int compl = EXPAND_NOTHING;
5306 char_u *compl_arg = NULL;
5307 int has_attr = (eap->arg[0] == '-');
5308
5309 p = eap->arg;
5310
5311 /* Check for attributes */
5312 while (*p == '-')
5313 {
5314 ++p;
5315 end = skiptowhite(p);
5316 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5317 == FAIL)
5318 return;
5319 p = skipwhite(end);
5320 }
5321
5322 /* Get the name (if any) and skip to the following argument */
5323 name = p;
5324 if (ASCII_ISALPHA(*p))
5325 while (ASCII_ISALNUM(*p))
5326 ++p;
5327 if (!ends_excmd(*p) && !vim_iswhite(*p))
5328 {
5329 EMSG(_("E182: Invalid command name"));
5330 return;
5331 }
5332 end = p;
5333
5334 /* If there is nothing after the name, and no attributes were specified,
5335 * we are listing commands
5336 */
5337 p = skipwhite(end);
5338 if (!has_attr && ends_excmd(*p))
5339 {
5340 uc_list(name, end - name);
5341 }
5342 else if (!ASCII_ISUPPER(*name))
5343 {
5344 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5345 return;
5346 }
5347 else
5348 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5349 eap->forceit);
5350}
5351
5352/*
5353 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 * Clear all user commands, global and for current buffer.
5355 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005356/*ARGSUSED*/
5357 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358ex_comclear(eap)
5359 exarg_T *eap;
5360{
5361 uc_clear(&ucmds);
5362 uc_clear(&curbuf->b_ucmds);
5363}
5364
5365/*
5366 * Clear all user commands for "gap".
5367 */
5368 void
5369uc_clear(gap)
5370 garray_T *gap;
5371{
5372 int i;
5373 ucmd_T *cmd;
5374
5375 for (i = 0; i < gap->ga_len; ++i)
5376 {
5377 cmd = USER_CMD_GA(gap, i);
5378 vim_free(cmd->uc_name);
5379 vim_free(cmd->uc_rep);
5380# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5381 vim_free(cmd->uc_compl_arg);
5382# endif
5383 }
5384 ga_clear(gap);
5385}
5386
5387 static void
5388ex_delcommand(eap)
5389 exarg_T *eap;
5390{
5391 int i = 0;
5392 ucmd_T *cmd = NULL;
5393 int cmp = -1;
5394 garray_T *gap;
5395
5396 gap = &curbuf->b_ucmds;
5397 for (;;)
5398 {
5399 for (i = 0; i < gap->ga_len; ++i)
5400 {
5401 cmd = USER_CMD_GA(gap, i);
5402 cmp = STRCMP(eap->arg, cmd->uc_name);
5403 if (cmp <= 0)
5404 break;
5405 }
5406 if (gap == &ucmds || cmp == 0)
5407 break;
5408 gap = &ucmds;
5409 }
5410
5411 if (cmp != 0)
5412 {
5413 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5414 return;
5415 }
5416
5417 vim_free(cmd->uc_name);
5418 vim_free(cmd->uc_rep);
5419# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5420 vim_free(cmd->uc_compl_arg);
5421# endif
5422
5423 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
5425 if (i < gap->ga_len)
5426 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5427}
5428
5429 static char_u *
5430uc_split_args(arg, lenp)
5431 char_u *arg;
5432 size_t *lenp;
5433{
5434 char_u *buf;
5435 char_u *p;
5436 char_u *q;
5437 int len;
5438
5439 /* Precalculate length */
5440 p = arg;
5441 len = 2; /* Initial and final quotes */
5442
5443 while (*p)
5444 {
5445 if (p[0] == '\\' && vim_iswhite(p[1]))
5446 {
5447 len += 1;
5448 p += 2;
5449 }
5450 else if (*p == '\\' || *p == '"')
5451 {
5452 len += 2;
5453 p += 1;
5454 }
5455 else if (vim_iswhite(*p))
5456 {
5457 p = skipwhite(p);
5458 if (*p == NUL)
5459 break;
5460 len += 3; /* "," */
5461 }
5462 else
5463 {
5464 ++len;
5465 ++p;
5466 }
5467 }
5468
5469 buf = alloc(len + 1);
5470 if (buf == NULL)
5471 {
5472 *lenp = 0;
5473 return buf;
5474 }
5475
5476 p = arg;
5477 q = buf;
5478 *q++ = '"';
5479 while (*p)
5480 {
5481 if (p[0] == '\\' && vim_iswhite(p[1]))
5482 {
5483 *q++ = p[1];
5484 p += 2;
5485 }
5486 else if (*p == '\\' || *p == '"')
5487 {
5488 *q++ = '\\';
5489 *q++ = *p++;
5490 }
5491 else if (vim_iswhite(*p))
5492 {
5493 p = skipwhite(p);
5494 if (*p == NUL)
5495 break;
5496 *q++ = '"';
5497 *q++ = ',';
5498 *q++ = '"';
5499 }
5500 else
5501 {
5502 *q++ = *p++;
5503 }
5504 }
5505 *q++ = '"';
5506 *q = 0;
5507
5508 *lenp = len;
5509 return buf;
5510}
5511
5512/*
5513 * Check for a <> code in a user command.
5514 * "code" points to the '<'. "len" the length of the <> (inclusive).
5515 * "buf" is where the result is to be added.
5516 * "split_buf" points to a buffer used for splitting, caller should free it.
5517 * "split_len" is the length of what "split_buf" contains.
5518 * Returns the length of the replacement, which has been added to "buf".
5519 * Returns -1 if there was no match, and only the "<" has been copied.
5520 */
5521 static size_t
5522uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5523 char_u *code;
5524 size_t len;
5525 char_u *buf;
5526 ucmd_T *cmd; /* the user command we're expanding */
5527 exarg_T *eap; /* ex arguments */
5528 char_u **split_buf;
5529 size_t *split_len;
5530{
5531 size_t result = 0;
5532 char_u *p = code + 1;
5533 size_t l = len - 2;
5534 int quote = 0;
5535 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5536 ct_LT, ct_NONE } type = ct_NONE;
5537
5538 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5539 {
5540 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5541 p += 2;
5542 l -= 2;
5543 }
5544
5545 if (l < 1)
5546 type = ct_NONE;
5547 else if (STRNICMP(p, "args", l) == 0)
5548 type = ct_ARGS;
5549 else if (STRNICMP(p, "bang", l) == 0)
5550 type = ct_BANG;
5551 else if (STRNICMP(p, "count", l) == 0)
5552 type = ct_COUNT;
5553 else if (STRNICMP(p, "line1", l) == 0)
5554 type = ct_LINE1;
5555 else if (STRNICMP(p, "line2", l) == 0)
5556 type = ct_LINE2;
5557 else if (STRNICMP(p, "lt", l) == 0)
5558 type = ct_LT;
5559 else if (STRNICMP(p, "register", l) == 0)
5560 type = ct_REGISTER;
5561
5562 switch (type)
5563 {
5564 case ct_ARGS:
5565 /* Simple case first */
5566 if (*eap->arg == NUL)
5567 {
5568 if (quote == 1)
5569 {
5570 result = 2;
5571 if (buf != NULL)
5572 STRCPY(buf, "''");
5573 }
5574 else
5575 result = 0;
5576 break;
5577 }
5578
5579 /* When specified there is a single argument don't split it.
5580 * Works for ":Cmd %" when % is "a b c". */
5581 if ((eap->argt & NOSPC) && quote == 2)
5582 quote = 1;
5583
5584 switch (quote)
5585 {
5586 case 0: /* No quoting, no splitting */
5587 result = STRLEN(eap->arg);
5588 if (buf != NULL)
5589 STRCPY(buf, eap->arg);
5590 break;
5591 case 1: /* Quote, but don't split */
5592 result = STRLEN(eap->arg) + 2;
5593 for (p = eap->arg; *p; ++p)
5594 {
5595 if (*p == '\\' || *p == '"')
5596 ++result;
5597 }
5598
5599 if (buf != NULL)
5600 {
5601 *buf++ = '"';
5602 for (p = eap->arg; *p; ++p)
5603 {
5604 if (*p == '\\' || *p == '"')
5605 *buf++ = '\\';
5606 *buf++ = *p;
5607 }
5608 *buf = '"';
5609 }
5610
5611 break;
5612 case 2: /* Quote and split */
5613 /* This is hard, so only do it once, and cache the result */
5614 if (*split_buf == NULL)
5615 *split_buf = uc_split_args(eap->arg, split_len);
5616
5617 result = *split_len;
5618 if (buf != NULL && result != 0)
5619 STRCPY(buf, *split_buf);
5620
5621 break;
5622 }
5623 break;
5624
5625 case ct_BANG:
5626 result = eap->forceit ? 1 : 0;
5627 if (quote)
5628 result += 2;
5629 if (buf != NULL)
5630 {
5631 if (quote)
5632 *buf++ = '"';
5633 if (eap->forceit)
5634 *buf++ = '!';
5635 if (quote)
5636 *buf = '"';
5637 }
5638 break;
5639
5640 case ct_LINE1:
5641 case ct_LINE2:
5642 case ct_COUNT:
5643 {
5644 char num_buf[20];
5645 long num = (type == ct_LINE1) ? eap->line1 :
5646 (type == ct_LINE2) ? eap->line2 :
5647 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5648 size_t num_len;
5649
5650 sprintf(num_buf, "%ld", num);
5651 num_len = STRLEN(num_buf);
5652 result = num_len;
5653
5654 if (quote)
5655 result += 2;
5656
5657 if (buf != NULL)
5658 {
5659 if (quote)
5660 *buf++ = '"';
5661 STRCPY(buf, num_buf);
5662 buf += num_len;
5663 if (quote)
5664 *buf = '"';
5665 }
5666
5667 break;
5668 }
5669
5670 case ct_REGISTER:
5671 result = eap->regname ? 1 : 0;
5672 if (quote)
5673 result += 2;
5674 if (buf != NULL)
5675 {
5676 if (quote)
5677 *buf++ = '\'';
5678 if (eap->regname)
5679 *buf++ = eap->regname;
5680 if (quote)
5681 *buf = '\'';
5682 }
5683 break;
5684
5685 case ct_LT:
5686 result = 1;
5687 if (buf != NULL)
5688 *buf = '<';
5689 break;
5690
5691 default:
5692 /* Not recognized: just copy the '<' and return -1. */
5693 result = (size_t)-1;
5694 if (buf != NULL)
5695 *buf = '<';
5696 break;
5697 }
5698
5699 return result;
5700}
5701
5702 static void
5703do_ucmd(eap)
5704 exarg_T *eap;
5705{
5706 char_u *buf;
5707 char_u *p;
5708 char_u *q;
5709
5710 char_u *start;
5711 char_u *end;
5712 size_t len, totlen;
5713
5714 size_t split_len = 0;
5715 char_u *split_buf = NULL;
5716 ucmd_T *cmd;
5717#ifdef FEAT_EVAL
5718 scid_T save_current_SID = current_SID;
5719#endif
5720
5721 if (eap->cmdidx == CMD_USER)
5722 cmd = USER_CMD(eap->useridx);
5723 else
5724 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
5725
5726 /*
5727 * Replace <> in the command by the arguments.
5728 */
5729 buf = NULL;
5730 for (;;)
5731 {
5732 p = cmd->uc_rep;
5733 q = buf;
5734 totlen = 0;
5735 while ((start = vim_strchr(p, '<')) != NULL
5736 && (end = vim_strchr(start + 1, '>')) != NULL)
5737 {
5738 /* Include the '>' */
5739 ++end;
5740
5741 /* Take everything up to the '<' */
5742 len = start - p;
5743 if (buf == NULL)
5744 totlen += len;
5745 else
5746 {
5747 mch_memmove(q, p, len);
5748 q += len;
5749 }
5750
5751 len = uc_check_code(start, end - start, q, cmd, eap,
5752 &split_buf, &split_len);
5753 if (len == (size_t)-1)
5754 {
5755 /* no match, continue after '<' */
5756 p = start + 1;
5757 len = 1;
5758 }
5759 else
5760 p = end;
5761 if (buf == NULL)
5762 totlen += len;
5763 else
5764 q += len;
5765 }
5766 if (buf != NULL) /* second time here, finished */
5767 {
5768 STRCPY(q, p);
5769 break;
5770 }
5771
5772 totlen += STRLEN(p); /* Add on the trailing characters */
5773 buf = alloc((unsigned)(totlen + 1));
5774 if (buf == NULL)
5775 {
5776 vim_free(split_buf);
5777 return;
5778 }
5779 }
5780
5781#ifdef FEAT_EVAL
5782 current_SID = cmd->uc_scriptID;
5783#endif
5784 (void)do_cmdline(buf, eap->getline, eap->cookie,
5785 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
5786#ifdef FEAT_EVAL
5787 current_SID = save_current_SID;
5788#endif
5789 vim_free(buf);
5790 vim_free(split_buf);
5791}
5792
5793# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5794 static char_u *
5795get_user_command_name(idx)
5796 int idx;
5797{
5798 return get_user_commands(NULL, idx - (int)CMD_SIZE);
5799}
5800
5801/*
5802 * Function given to ExpandGeneric() to obtain the list of user command names.
5803 */
5804/*ARGSUSED*/
5805 char_u *
5806get_user_commands(xp, idx)
5807 expand_T *xp;
5808 int idx;
5809{
5810 if (idx < curbuf->b_ucmds.ga_len)
5811 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
5812 idx -= curbuf->b_ucmds.ga_len;
5813 if (idx < ucmds.ga_len)
5814 return USER_CMD(idx)->uc_name;
5815 return NULL;
5816}
5817
5818/*
5819 * Function given to ExpandGeneric() to obtain the list of user command
5820 * attributes.
5821 */
5822/*ARGSUSED*/
5823 char_u *
5824get_user_cmd_flags(xp, idx)
5825 expand_T *xp;
5826 int idx;
5827{
5828 static char *user_cmd_flags[] =
5829 {"bang", "bar", "buffer", "complete", "count",
5830 "nargs", "range", "register"};
5831
5832 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))
5833 return NULL;
5834 return (char_u *)user_cmd_flags[idx];
5835}
5836
5837/*
5838 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
5839 */
5840/*ARGSUSED*/
5841 char_u *
5842get_user_cmd_nargs(xp, idx)
5843 expand_T *xp;
5844 int idx;
5845{
5846 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
5847
5848 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))
5849 return NULL;
5850 return (char_u *)user_cmd_nargs[idx];
5851}
5852
5853/*
5854 * Function given to ExpandGeneric() to obtain the list of values for -complete.
5855 */
5856/*ARGSUSED*/
5857 char_u *
5858get_user_cmd_complete(xp, idx)
5859 expand_T *xp;
5860 int idx;
5861{
5862 return (char_u *)command_complete[idx].name;
5863}
5864# endif /* FEAT_CMDL_COMPL */
5865
5866#endif /* FEAT_USR_CMDS */
5867
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005868#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
5869/*
5870 * Parse a completion argument "value[vallen]".
5871 * The detected completion goes in "*complp", argument type in "*argt".
5872 * When there is an argument, for function and user defined completion, it's
5873 * copied to allocated memory and stored in "*compl_arg".
5874 * Returns FAIL if something is wrong.
5875 */
5876 int
5877parse_compl_arg(value, vallen, complp, argt, compl_arg)
5878 char_u *value;
5879 int vallen;
5880 int *complp;
5881 long *argt;
5882 char_u **compl_arg;
5883{
5884 char_u *arg = NULL;
5885 size_t arglen = 0;
5886 int i;
5887 int valend = vallen;
5888
5889 /* Look for any argument part - which is the part after any ',' */
5890 for (i = 0; i < vallen; ++i)
5891 {
5892 if (value[i] == ',')
5893 {
5894 arg = &value[i + 1];
5895 arglen = vallen - i - 1;
5896 valend = i;
5897 break;
5898 }
5899 }
5900
5901 for (i = 0; command_complete[i].expand != 0; ++i)
5902 {
5903 if (STRLEN(command_complete[i].name) == valend
5904 && STRNCMP(value, command_complete[i].name, valend) == 0)
5905 {
5906 *complp = command_complete[i].expand;
5907 if (command_complete[i].expand == EXPAND_BUFFERS)
5908 *argt |= BUFNAME;
5909 else if (command_complete[i].expand == EXPAND_DIRECTORIES
5910 || command_complete[i].expand == EXPAND_FILES)
5911 *argt |= XFILE;
5912 break;
5913 }
5914 }
5915
5916 if (command_complete[i].expand == 0)
5917 {
5918 EMSG2(_("E180: Invalid complete value: %s"), value);
5919 return FAIL;
5920 }
5921
5922# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5923 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
5924 && arg != NULL)
5925# else
5926 if (arg != NULL)
5927# endif
5928 {
5929 EMSG(_("E468: Completion argument only allowed for custom completion"));
5930 return FAIL;
5931 }
5932
5933# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5934 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
5935 && arg == NULL)
5936 {
5937 EMSG(_("E467: Custom completion requires a function argument"));
5938 return FAIL;
5939 }
5940
5941 if (arg != NULL)
5942 *compl_arg = vim_strnsave(arg, (int)arglen);
5943# endif
5944 return OK;
5945}
5946#endif
5947
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 static void
5949ex_colorscheme(eap)
5950 exarg_T *eap;
5951{
5952 if (load_colors(eap->arg) == FAIL)
5953 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
5954}
5955
5956 static void
5957ex_highlight(eap)
5958 exarg_T *eap;
5959{
5960 if (*eap->arg == NUL && eap->cmd[2] == '!')
5961 MSG(_("Greetings, Vim user!"));
5962 do_highlight(eap->arg, eap->forceit, FALSE);
5963}
5964
5965
5966/*
5967 * Call this function if we thought we were going to exit, but we won't
5968 * (because of an error). May need to restore the terminal mode.
5969 */
5970 void
5971not_exiting()
5972{
5973 exiting = FALSE;
5974 settmode(TMODE_RAW);
5975}
5976
5977/*
5978 * ":quit": quit current window, quit Vim if closed the last window.
5979 */
5980 static void
5981ex_quit(eap)
5982 exarg_T *eap;
5983{
5984#ifdef FEAT_CMDWIN
5985 if (cmdwin_type != 0)
5986 {
5987 cmdwin_result = Ctrl_C;
5988 return;
5989 }
5990#endif
5991
5992#ifdef FEAT_NETBEANS_INTG
5993 netbeansForcedQuit = eap->forceit;
5994#endif
5995
5996 /*
5997 * If there are more files or windows we won't exit.
5998 */
5999 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6000 exiting = TRUE;
6001 if ((!P_HID(curbuf)
6002 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6003 || check_more(TRUE, eap->forceit) == FAIL
6004 || (only_one_window() && check_changed_any(eap->forceit)))
6005 {
6006 not_exiting();
6007 }
6008 else
6009 {
6010#ifdef FEAT_WINDOWS
6011 if (only_one_window()) /* quit last window */
6012#endif
6013 getout(0);
6014#ifdef FEAT_WINDOWS
6015# ifdef FEAT_GUI
6016 need_mouse_correct = TRUE;
6017# endif
6018 /* close window; may free buffer */
6019 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6020#endif
6021 }
6022}
6023
6024/*
6025 * ":cquit".
6026 */
6027/*ARGSUSED*/
6028 static void
6029ex_cquit(eap)
6030 exarg_T *eap;
6031{
6032 getout(1); /* this does not always pass on the exit code to the Manx
6033 compiler. why? */
6034}
6035
6036/*
6037 * ":qall": try to quit all windows
6038 */
6039 static void
6040ex_quit_all(eap)
6041 exarg_T *eap;
6042{
6043# ifdef FEAT_CMDWIN
6044 if (cmdwin_type != 0)
6045 {
6046 if (eap->forceit)
6047 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6048 else
6049 cmdwin_result = K_XF2;
6050 return;
6051 }
6052# endif
6053 exiting = TRUE;
6054 if (eap->forceit || !check_changed_any(FALSE))
6055 getout(0);
6056 not_exiting();
6057}
6058
6059#ifdef FEAT_WINDOWS
6060/*
6061 * ":close": close current window, unless it is the last one
6062 */
6063 static void
6064ex_close(eap)
6065 exarg_T *eap;
6066{
6067# ifdef FEAT_CMDWIN
6068 if (cmdwin_type != 0)
6069 cmdwin_result = K_IGNORE;
6070 else
6071# endif
6072 ex_win_close(eap, curwin);
6073}
6074
6075 static void
6076ex_win_close(eap, win)
6077 exarg_T *eap;
6078 win_T *win;
6079{
6080 int need_hide;
6081 buf_T *buf = win->w_buffer;
6082
6083 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
6084 if (need_hide && !P_HID(buf) && !eap->forceit)
6085 {
6086#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6087 if ((p_confirm || cmdmod.confirm) && p_write)
6088 {
6089 dialog_changed(buf, FALSE);
6090 if (buf_valid(buf) && bufIsChanged(buf))
6091 return;
6092 need_hide = FALSE;
6093 }
6094 else
6095#endif
6096 {
6097 EMSG(_(e_nowrtmsg));
6098 return;
6099 }
6100 }
6101
6102#ifdef FEAT_GUI
6103 need_mouse_correct = TRUE;
6104#endif
6105 /* free buffer when not hiding it or when it's a scratch buffer */
6106 win_close(win, !need_hide && !P_HID(buf));
6107}
6108
6109#ifdef FEAT_QUICKFIX
6110/*
6111 * ":pclose": Close any preview window.
6112 */
6113 static void
6114ex_pclose(eap)
6115 exarg_T *eap;
6116{
6117 win_T *win;
6118
6119 for (win = firstwin; win != NULL; win = win->w_next)
6120 if (win->w_p_pvw)
6121 {
6122 ex_win_close(eap, win);
6123 break;
6124 }
6125}
6126#endif
6127
6128/*
6129 * ":only".
6130 */
6131 static void
6132ex_only(eap)
6133 exarg_T *eap;
6134{
6135# ifdef FEAT_GUI
6136 need_mouse_correct = TRUE;
6137# endif
6138 close_others(TRUE, eap->forceit);
6139}
6140
6141/*
6142 * ":all" and ":sall".
6143 */
6144 static void
6145ex_all(eap)
6146 exarg_T *eap;
6147{
6148 if (eap->addr_count == 0)
6149 eap->line2 = 9999;
6150 do_arg_all((int)eap->line2, eap->forceit);
6151}
6152#endif /* FEAT_WINDOWS */
6153
6154 static void
6155ex_hide(eap)
6156 exarg_T *eap;
6157{
6158 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6159 eap->errmsg = e_invarg;
6160 else
6161 {
6162 /* ":hide" or ":hide | cmd": hide current window */
6163 eap->nextcmd = check_nextcmd(eap->arg);
6164#ifdef FEAT_WINDOWS
6165 if (!eap->skip)
6166 {
6167# ifdef FEAT_GUI
6168 need_mouse_correct = TRUE;
6169# endif
6170 win_close(curwin, FALSE); /* don't free buffer */
6171 }
6172#endif
6173 }
6174}
6175
6176/*
6177 * ":stop" and ":suspend": Suspend Vim.
6178 */
6179 static void
6180ex_stop(eap)
6181 exarg_T *eap;
6182{
6183 /*
6184 * Disallow suspending for "rvim".
6185 */
6186 if (!check_restricted()
6187#ifdef WIN3264
6188 /*
6189 * Check if external commands are allowed now.
6190 */
6191 && can_end_termcap_mode(TRUE)
6192#endif
6193 )
6194 {
6195 if (!eap->forceit)
6196 autowrite_all();
6197 windgoto((int)Rows - 1, 0);
6198 out_char('\n');
6199 out_flush();
6200 stoptermcap();
6201 out_flush(); /* needed for SUN to restore xterm buffer */
6202#ifdef FEAT_TITLE
6203 mch_restore_title(3); /* restore window titles */
6204#endif
6205 ui_suspend(); /* call machine specific function */
6206#ifdef FEAT_TITLE
6207 maketitle();
6208 resettitle(); /* force updating the title */
6209#endif
6210 starttermcap();
6211 scroll_start(); /* scroll screen before redrawing */
6212 redraw_later_clear();
6213 shell_resized(); /* may have resized window */
6214 }
6215}
6216
6217/*
6218 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6219 */
6220 static void
6221ex_exit(eap)
6222 exarg_T *eap;
6223{
6224#ifdef FEAT_CMDWIN
6225 if (cmdwin_type != 0)
6226 {
6227 cmdwin_result = Ctrl_C;
6228 return;
6229 }
6230#endif
6231
6232 /*
6233 * if more files or windows we won't exit
6234 */
6235 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6236 exiting = TRUE;
6237 if ( ((eap->cmdidx == CMD_wq
6238 || curbufIsChanged())
6239 && do_write(eap) == FAIL)
6240 || check_more(TRUE, eap->forceit) == FAIL
6241 || (only_one_window() && check_changed_any(eap->forceit)))
6242 {
6243 not_exiting();
6244 }
6245 else
6246 {
6247#ifdef FEAT_WINDOWS
6248 if (only_one_window()) /* quit last window, exit Vim */
6249#endif
6250 getout(0);
6251#ifdef FEAT_WINDOWS
6252# ifdef FEAT_GUI
6253 need_mouse_correct = TRUE;
6254# endif
6255 /* quit current window, may free buffer */
6256 win_close(curwin, !P_HID(curwin->w_buffer));
6257#endif
6258 }
6259}
6260
6261/*
6262 * ":print", ":list", ":number".
6263 */
6264 static void
6265ex_print(eap)
6266 exarg_T *eap;
6267{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006268 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6269 EMSG(_(e_emptybuf));
6270 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006272 for ( ;!got_int; ui_breakcheck())
6273 {
6274 print_line(eap->line1,
6275 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6276 || (eap->flags & EXFLAG_NR)),
6277 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6278 if (++eap->line1 > eap->line2)
6279 break;
6280 out_flush(); /* show one line at a time */
6281 }
6282 setpcmark();
6283 /* put cursor at last line */
6284 curwin->w_cursor.lnum = eap->line2;
6285 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 }
6287
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289}
6290
6291#ifdef FEAT_BYTEOFF
6292 static void
6293ex_goto(eap)
6294 exarg_T *eap;
6295{
6296 goto_byte(eap->line2);
6297}
6298#endif
6299
6300/*
6301 * ":shell".
6302 */
6303/*ARGSUSED*/
6304 static void
6305ex_shell(eap)
6306 exarg_T *eap;
6307{
6308 do_shell(NULL, 0);
6309}
6310
6311#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6312 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
6313 || defined(PROTO)
6314
6315/*
6316 * Handle a file drop. The code is here because a drop is *nearly* like an
6317 * :args command, but not quite (we have a list of exact filenames, so we
6318 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6319 * code is very similar to :args and hence needs access to a lot of the static
6320 * functions in this file.
6321 *
6322 * The list should be allocated using alloc(), as should each item in the
6323 * list. This function takes over responsibility for freeing the list.
6324 *
6325 * XXX The list is made into the arggument list. This is freed using
6326 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6327 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6328 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6329 * file functionality is (currently) not in EMX this is not presently a
6330 * problem.
6331 */
6332 void
6333handle_drop(filec, filev, split)
6334 int filec; /* the number of files dropped */
6335 char_u **filev; /* the list of files dropped */
6336 int split; /* force splitting the window */
6337{
6338 exarg_T ea;
6339 int save_msg_scroll = msg_scroll;
6340
6341# ifdef FEAT_CMDWIN
6342 if (cmdwin_type != 0)
6343 return;
6344# endif
6345
6346 /* Check whether the current buffer is changed. If so, we will need
6347 * to split the current window or data could be lost.
6348 * We don't need to check if the 'hidden' option is set, as in this
6349 * case the buffer won't be lost.
6350 */
6351 if (!P_HID(curbuf) && !split)
6352 {
6353 ++emsg_off;
6354 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6355 --emsg_off;
6356 }
6357 if (split)
6358 {
6359# ifdef FEAT_WINDOWS
6360 if (win_split(0, 0) == FAIL)
6361 return;
6362# ifdef FEAT_SCROLLBIND
6363 curwin->w_p_scb = FALSE;
6364# endif
6365
6366 /* When splitting the window, create a new alist. Otherwise the
6367 * existing one is overwritten. */
6368 alist_unlink(curwin->w_alist);
6369 alist_new();
6370# else
6371 return; /* can't split, always fail */
6372# endif
6373 }
6374
6375 /*
6376 * Set up the new argument list.
6377 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006378 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
6380 /*
6381 * Move to the first file.
6382 */
6383 /* Fake up a minimal "next" command for do_argfile() */
6384 vim_memset(&ea, 0, sizeof(ea));
6385 ea.cmd = (char_u *)"next";
6386 do_argfile(&ea, 0);
6387
6388 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6389 * mode that is not needed here. */
6390 need_start_insertmode = FALSE;
6391
6392 /* Restore msg_scroll, otherwise a following command may cause scrolling
6393 * unexpectedly. The screen will be redrawn by the caller, thus
6394 * msg_scroll being set by displaying a message is irrelevant. */
6395 msg_scroll = save_msg_scroll;
6396}
6397#endif
6398
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399/*
6400 * Clear an argument list: free all file names and reset it to zero entries.
6401 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006402 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403alist_clear(al)
6404 alist_T *al;
6405{
6406 while (--al->al_ga.ga_len >= 0)
6407 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6408 ga_clear(&al->al_ga);
6409}
6410
6411/*
6412 * Init an argument list.
6413 */
6414 void
6415alist_init(al)
6416 alist_T *al;
6417{
6418 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6419}
6420
6421#if defined(FEAT_WINDOWS) || defined(PROTO)
6422
6423/*
6424 * Remove a reference from an argument list.
6425 * Ignored when the argument list is the global one.
6426 * If the argument list is no longer used by any window, free it.
6427 */
6428 void
6429alist_unlink(al)
6430 alist_T *al;
6431{
6432 if (al != &global_alist && --al->al_refcount <= 0)
6433 {
6434 alist_clear(al);
6435 vim_free(al);
6436 }
6437}
6438
6439# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6440/*
6441 * Create a new argument list and use it for the current window.
6442 */
6443 void
6444alist_new()
6445{
6446 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6447 if (curwin->w_alist == NULL)
6448 {
6449 curwin->w_alist = &global_alist;
6450 ++global_alist.al_refcount;
6451 }
6452 else
6453 {
6454 curwin->w_alist->al_refcount = 1;
6455 alist_init(curwin->w_alist);
6456 }
6457}
6458# endif
6459#endif
6460
6461#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6462/*
6463 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006464 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6465 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 */
6467 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006468alist_expand(fnum_list, fnum_len)
6469 int *fnum_list;
6470 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006473 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 char_u **new_arg_files;
6475 int new_arg_file_count;
6476 char_u *save_p_su = p_su;
6477 int i;
6478
6479 /* Don't use 'suffixes' here. This should work like the shell did the
6480 * expansion. Also, the vimrc file isn't read yet, thus the user
6481 * can't set the options. */
6482 p_su = empty_option;
6483 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6484 if (old_arg_files != NULL)
6485 {
6486 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006487 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6488 old_arg_count = GARGCOUNT;
6489 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 &new_arg_file_count, &new_arg_files,
6491 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
6492 && new_arg_file_count > 0)
6493 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006494 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6495 TRUE, fnum_list, fnum_len);
6496 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 }
6499 p_su = save_p_su;
6500}
6501#endif
6502
6503/*
6504 * Set the argument list for the current window.
6505 * Takes over the allocated files[] and the allocated fnames in it.
6506 */
6507 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006508alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 alist_T *al;
6510 int count;
6511 char_u **files;
6512 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006513 int *fnum_list;
6514 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515{
6516 int i;
6517
6518 alist_clear(al);
6519 if (ga_grow(&al->al_ga, count) == OK)
6520 {
6521 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006522 {
6523 if (got_int)
6524 {
6525 /* When adding many buffers this can take a long time. Allow
6526 * interrupting here. */
6527 while (i < count)
6528 vim_free(files[i++]);
6529 break;
6530 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006531
6532 /* May set buffer name of a buffer previously used for the
6533 * argument list, so that it's re-used by alist_add. */
6534 if (fnum_list != NULL && i < fnum_len)
6535 buf_set_name(fnum_list[i], files[i]);
6536
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006538 ui_breakcheck();
6539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 vim_free(files);
6541 }
6542 else
6543 FreeWild(count, files);
6544#ifdef FEAT_WINDOWS
6545 if (al == &global_alist)
6546#endif
6547 arg_had_last = FALSE;
6548}
6549
6550/*
6551 * Add file "fname" to argument list "al".
6552 * "fname" must have been allocated and "al" must have been checked for room.
6553 */
6554 void
6555alist_add(al, fname, set_fnum)
6556 alist_T *al;
6557 char_u *fname;
6558 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
6559{
6560 if (fname == NULL) /* don't add NULL file names */
6561 return;
6562#ifdef BACKSLASH_IN_FILENAME
6563 slash_adjust(fname);
6564#endif
6565 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6566 if (set_fnum > 0)
6567 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6568 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6569 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570}
6571
6572#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6573/*
6574 * Adjust slashes in file names. Called after 'shellslash' was set.
6575 */
6576 void
6577alist_slash_adjust()
6578{
6579 int i;
6580# ifdef FEAT_WINDOWS
6581 win_T *wp;
6582# endif
6583
6584 for (i = 0; i < GARGCOUNT; ++i)
6585 if (GARGLIST[i].ae_fname != NULL)
6586 slash_adjust(GARGLIST[i].ae_fname);
6587# ifdef FEAT_WINDOWS
6588 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6589 if (wp->w_alist != &global_alist)
6590 for (i = 0; i < WARGCOUNT(wp); ++i)
6591 if (WARGLIST(wp)[i].ae_fname != NULL)
6592 slash_adjust(WARGLIST(wp)[i].ae_fname);
6593# endif
6594}
6595#endif
6596
6597/*
6598 * ":preserve".
6599 */
6600/*ARGSUSED*/
6601 static void
6602ex_preserve(eap)
6603 exarg_T *eap;
6604{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006605 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 ml_preserve(curbuf, TRUE);
6607}
6608
6609/*
6610 * ":recover".
6611 */
6612 static void
6613ex_recover(eap)
6614 exarg_T *eap;
6615{
6616 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6617 recoverymode = TRUE;
6618 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
6619 && (*eap->arg == NUL
6620 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6621 ml_recover();
6622 recoverymode = FALSE;
6623}
6624
6625/*
6626 * Command modifier used in a wrong way.
6627 */
6628 static void
6629ex_wrongmodifier(eap)
6630 exarg_T *eap;
6631{
6632 eap->errmsg = e_invcmd;
6633}
6634
6635#ifdef FEAT_WINDOWS
6636/*
6637 * :sview [+command] file split window with new file, read-only
6638 * :split [[+command] file] split window with current or new file
6639 * :vsplit [[+command] file] split window vertically with current or new file
6640 * :new [[+command] file] split window with no or new file
6641 * :vnew [[+command] file] split vertically window with no or new file
6642 * :sfind [+command] file split window with file in 'path'
6643 */
6644 void
6645ex_splitview(eap)
6646 exarg_T *eap;
6647{
6648 win_T *old_curwin;
6649#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6650 char_u *fname = NULL;
6651#endif
6652#ifdef FEAT_BROWSE
6653 int browse_flag = cmdmod.browse;
6654#endif
6655
6656#ifndef FEAT_VERTSPLIT
6657 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
6658 {
6659 ex_ni(eap);
6660 return;
6661 }
6662#endif
6663
6664 old_curwin = curwin;
6665#ifdef FEAT_GUI
6666 need_mouse_correct = TRUE;
6667#endif
6668
6669#ifdef FEAT_QUICKFIX
6670 /* A ":split" in the quickfix window works like ":new". Don't want two
6671 * quickfix windows. */
6672 if (bt_quickfix(curbuf))
6673 {
6674 if (eap->cmdidx == CMD_split)
6675 eap->cmdidx = CMD_new;
6676# ifdef FEAT_VERTSPLIT
6677 if (eap->cmdidx == CMD_vsplit)
6678 eap->cmdidx = CMD_vnew;
6679# endif
6680 }
6681#endif
6682
6683#ifdef FEAT_SEARCHPATH
6684 if (eap->cmdidx == CMD_sfind)
6685 {
6686 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6687 FNAME_MESS, TRUE, curbuf->b_ffname);
6688 if (fname == NULL)
6689 goto theend;
6690 eap->arg = fname;
6691 }
6692# ifdef FEAT_BROWSE
6693 else
6694# endif
6695#endif
6696#ifdef FEAT_BROWSE
6697 if (cmdmod.browse
6698# ifdef FEAT_VERTSPLIT
6699 && eap->cmdidx != CMD_vnew
6700#endif
6701 && eap->cmdidx != CMD_new)
6702 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006703 if (
6704# ifdef FEAT_GUI
6705 !gui.in_use &&
6706# endif
6707 au_has_group((char_u *)"FileExplorer"))
6708 {
6709 /* No browsing supported but we do have the file explorer:
6710 * Edit the directory. */
6711 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6712 eap->arg = (char_u *)".";
6713 }
6714 else
6715 {
6716 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006718 if (fname == NULL)
6719 goto theend;
6720 eap->arg = fname;
6721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 }
6723 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
6724#endif
6725
6726 if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
6727 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6728 {
6729#ifdef FEAT_SCROLLBIND
6730 /* Reset 'scrollbind' when editing another file, but keep it when
6731 * doing ":split" without arguments. */
6732 if (*eap->arg != NUL
6733#ifdef FEAT_BROWSE
6734 || cmdmod.browse
6735#endif
6736 )
6737 curwin->w_p_scb = FALSE;
6738 else
6739 do_check_scrollbind(FALSE);
6740#endif
6741 do_exedit(eap, old_curwin);
6742 }
6743
6744#ifdef FEAT_BROWSE
6745 cmdmod.browse = browse_flag;
6746#endif
6747
6748#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
6749theend:
6750 vim_free(fname);
6751#endif
6752}
6753#endif
6754
6755/*
6756 * ":mode": Set screen mode.
6757 * If no argument given, just get the screen size and redraw.
6758 */
6759 static void
6760ex_mode(eap)
6761 exarg_T *eap;
6762{
6763 if (*eap->arg == NUL)
6764 shell_resized();
6765 else
6766 mch_screenmode(eap->arg);
6767}
6768
6769#ifdef FEAT_WINDOWS
6770/*
6771 * ":resize".
6772 * set, increment or decrement current window height
6773 */
6774 static void
6775ex_resize(eap)
6776 exarg_T *eap;
6777{
6778 int n;
6779 win_T *wp = curwin;
6780
6781 if (eap->addr_count > 0)
6782 {
6783 n = eap->line2;
6784 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6785 ;
6786 }
6787
6788#ifdef FEAT_GUI
6789 need_mouse_correct = TRUE;
6790#endif
6791 n = atol((char *)eap->arg);
6792#ifdef FEAT_VERTSPLIT
6793 if (cmdmod.split & WSP_VERT)
6794 {
6795 if (*eap->arg == '-' || *eap->arg == '+')
6796 n += W_WIDTH(curwin);
6797 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6798 n = 9999;
6799 win_setwidth_win((int)n, wp);
6800 }
6801 else
6802#endif
6803 {
6804 if (*eap->arg == '-' || *eap->arg == '+')
6805 n += curwin->w_height;
6806 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6807 n = 9999;
6808 win_setheight_win((int)n, wp);
6809 }
6810}
6811#endif
6812
6813/*
6814 * ":find [+command] <file>" command.
6815 */
6816 static void
6817ex_find(eap)
6818 exarg_T *eap;
6819{
6820#ifdef FEAT_SEARCHPATH
6821 char_u *fname;
6822 int count;
6823
6824 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6825 TRUE, curbuf->b_ffname);
6826 if (eap->addr_count > 0)
6827 {
6828 /* Repeat finding the file "count" times. This matters when it
6829 * appears several times in the path. */
6830 count = eap->line2;
6831 while (fname != NULL && --count > 0)
6832 {
6833 vim_free(fname);
6834 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6835 FALSE, curbuf->b_ffname);
6836 }
6837 }
6838
6839 if (fname != NULL)
6840 {
6841 eap->arg = fname;
6842#endif
6843 do_exedit(eap, NULL);
6844#ifdef FEAT_SEARCHPATH
6845 vim_free(fname);
6846 }
6847#endif
6848}
6849
6850/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006851 * ":open" simulation: for now just work like ":visual".
6852 */
6853 static void
6854ex_open(eap)
6855 exarg_T *eap;
6856{
6857 regmatch_T regmatch;
6858 char_u *p;
6859
6860 curwin->w_cursor.lnum = eap->line2;
6861 beginline(BL_SOL | BL_FIX);
6862 if (*eap->arg == '/')
6863 {
6864 /* ":open /pattern/": put cursor in column found with pattern */
6865 ++eap->arg;
6866 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6867 *p = NUL;
6868 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6869 if (regmatch.regprog != NULL)
6870 {
6871 regmatch.rm_ic = p_ic;
6872 p = ml_get_curline();
6873 if (vim_regexec(&regmatch, p, (colnr_T)0))
6874 curwin->w_cursor.col = regmatch.startp[0] - p;
6875 else
6876 EMSG(_(e_nomatch));
6877 vim_free(regmatch.regprog);
6878 }
6879 /* Move to the NUL, ignore any other arguments. */
6880 eap->arg += STRLEN(eap->arg);
6881 }
6882 check_cursor();
6883
6884 eap->cmdidx = CMD_visual;
6885 do_exedit(eap, NULL);
6886}
6887
6888/*
6889 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 */
6891 static void
6892ex_edit(eap)
6893 exarg_T *eap;
6894{
6895 do_exedit(eap, NULL);
6896}
6897
6898/*
6899 * ":edit <file>" command and alikes.
6900 */
6901/*ARGSUSED*/
6902 void
6903do_exedit(eap, old_curwin)
6904 exarg_T *eap;
6905 win_T *old_curwin; /* curwin before doing a split or NULL */
6906{
6907 int n;
6908#ifdef FEAT_WINDOWS
6909 int need_hide;
6910#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006911 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912
6913 /*
6914 * ":vi" command ends Ex mode.
6915 */
6916 if (exmode_active && (eap->cmdidx == CMD_visual
6917 || eap->cmdidx == CMD_view))
6918 {
6919 exmode_active = FALSE;
6920 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006921 {
6922 /* Special case: ":global/pat/visual\NLvi-commands" */
6923 if (global_busy)
6924 {
6925 int rd = RedrawingDisabled;
6926 int nwr = no_wait_return;
6927 int ms = msg_scroll;
6928#ifdef FEAT_GUI
6929 int he = hold_gui_events;
6930#endif
6931
6932 if (eap->nextcmd != NULL)
6933 {
6934 stuffReadbuff(eap->nextcmd);
6935 eap->nextcmd = NULL;
6936 }
6937
6938 if (exmode_was != EXMODE_VIM)
6939 settmode(TMODE_RAW);
6940 RedrawingDisabled = 0;
6941 no_wait_return = 0;
6942 need_wait_return = FALSE;
6943 msg_scroll = 0;
6944#ifdef FEAT_GUI
6945 hold_gui_events = 0;
6946#endif
6947 must_redraw = CLEAR;
6948
6949 main_loop(FALSE, TRUE);
6950
6951 RedrawingDisabled = rd;
6952 no_wait_return = nwr;
6953 msg_scroll = ms;
6954#ifdef FEAT_GUI
6955 hold_gui_events = he;
6956#endif
6957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 }
6961
6962 if ((eap->cmdidx == CMD_new
6963#ifdef FEAT_VERTSPLIT
6964 || eap->cmdidx == CMD_vnew
6965#endif
6966 ) && *eap->arg == NUL)
6967 {
6968 /* ":new" without argument: edit an new empty buffer */
6969 setpcmark();
6970 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
6971 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0));
6972 }
6973 else if ((eap->cmdidx != CMD_split
6974#ifdef FEAT_VERTSPLIT
6975 && eap->cmdidx != CMD_vsplit
6976#endif
6977 )
6978 || *eap->arg != NUL
6979#ifdef FEAT_BROWSE
6980 || cmdmod.browse
6981#endif
6982 )
6983 {
6984 n = readonlymode;
6985 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6986 readonlymode = TRUE;
6987 else if (eap->cmdidx == CMD_enew)
6988 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
6989 empty buffer */
6990 setpcmark();
6991 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6992 NULL, eap,
6993 /* ":edit" goes to first line if Vi compatible */
6994 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6995 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6996 ? ECMD_ONE : eap->do_ecmd_lnum,
6997 (P_HID(curbuf) ? ECMD_HIDE : 0)
6998 + (eap->forceit ? ECMD_FORCEIT : 0)
6999#ifdef FEAT_LISTCMDS
7000 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7001#endif
7002 ) == FAIL)
7003 {
7004 /* Editing the file failed. If the window was split, close it. */
7005#ifdef FEAT_WINDOWS
7006 if (old_curwin != NULL)
7007 {
7008 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7009 if (!need_hide || P_HID(curbuf))
7010 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007011# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7012 cleanup_T cs;
7013
7014 /* Reset the error/interrupt/exception state here so that
7015 * aborting() returns FALSE when closing a window. */
7016 enter_cleanup(&cs);
7017# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018# ifdef FEAT_GUI
7019 need_mouse_correct = TRUE;
7020# endif
7021 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007022
7023# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7024 /* Restore the error/interrupt/exception state if not
7025 * discarded by a new aborting error, interrupt, or
7026 * uncaught exception. */
7027 leave_cleanup(&cs);
7028# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 }
7030 }
7031#endif
7032 }
7033 else if (readonlymode && curbuf->b_nwindows == 1)
7034 {
7035 /* When editing an already visited buffer, 'readonly' won't be set
7036 * but the previous value is kept. With ":view" and ":sview" we
7037 * want the file to be readonly, except when another window is
7038 * editing the same buffer. */
7039 curbuf->b_p_ro = TRUE;
7040 }
7041 readonlymode = n;
7042 }
7043 else
7044 {
7045 if (eap->do_ecmd_cmd != NULL)
7046 do_cmdline_cmd(eap->do_ecmd_cmd);
7047#ifdef FEAT_TITLE
7048 n = curwin->w_arg_idx_invalid;
7049#endif
7050 check_arg_idx(curwin);
7051#ifdef FEAT_TITLE
7052 if (n != curwin->w_arg_idx_invalid)
7053 maketitle();
7054#endif
7055 }
7056
7057#ifdef FEAT_WINDOWS
7058 /*
7059 * if ":split file" worked, set alternate file name in old window to new
7060 * file
7061 */
7062 if (old_curwin != NULL
7063 && *eap->arg != NUL
7064 && curwin != old_curwin
7065 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007066 && old_curwin->w_buffer != curbuf
7067 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 old_curwin->w_alt_fnum = curbuf->b_fnum;
7069#endif
7070
7071 ex_no_reprint = TRUE;
7072}
7073
7074#ifndef FEAT_GUI
7075/*
7076 * ":gui" and ":gvim" when there is no GUI.
7077 */
7078 static void
7079ex_nogui(eap)
7080 exarg_T *eap;
7081{
7082 eap->errmsg = e_nogvim;
7083}
7084#endif
7085
7086#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7087 static void
7088ex_tearoff(eap)
7089 exarg_T *eap;
7090{
7091 gui_make_tearoff(eap->arg);
7092}
7093#endif
7094
Bram Moolenaar843ee412004-06-30 16:16:41 +00007095#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_KDE) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 static void
7097ex_popup(eap)
7098 exarg_T *eap;
7099{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007100 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101}
7102#endif
7103
7104/*ARGSUSED*/
7105 static void
7106ex_swapname(eap)
7107 exarg_T *eap;
7108{
7109 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7110 MSG(_("No swap file"));
7111 else
7112 msg(curbuf->b_ml.ml_mfp->mf_fname);
7113}
7114
7115/*
7116 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7117 * offset.
7118 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7119 */
7120/*ARGSUSED*/
7121 static void
7122ex_syncbind(eap)
7123 exarg_T *eap;
7124{
7125#ifdef FEAT_SCROLLBIND
7126 win_T *wp;
7127 long topline;
7128 long y;
7129 linenr_T old_linenr = curwin->w_cursor.lnum;
7130
7131 setpcmark();
7132
7133 /*
7134 * determine max topline
7135 */
7136 if (curwin->w_p_scb)
7137 {
7138 topline = curwin->w_topline;
7139 for (wp = firstwin; wp; wp = wp->w_next)
7140 {
7141 if (wp->w_p_scb && wp->w_buffer)
7142 {
7143 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7144 if (topline > y)
7145 topline = y;
7146 }
7147 }
7148 if (topline < 1)
7149 topline = 1;
7150 }
7151 else
7152 {
7153 topline = 1;
7154 }
7155
7156
7157 /*
7158 * set all scrollbind windows to the same topline
7159 */
7160 wp = curwin;
7161 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7162 {
7163 if (curwin->w_p_scb)
7164 {
7165 y = topline - curwin->w_topline;
7166 if (y > 0)
7167 scrollup(y, TRUE);
7168 else
7169 scrolldown(-y, TRUE);
7170 curwin->w_scbind_pos = topline;
7171 redraw_later(VALID);
7172 cursor_correct();
7173#ifdef FEAT_WINDOWS
7174 curwin->w_redr_status = TRUE;
7175#endif
7176 }
7177 }
7178 curwin = wp;
7179 if (curwin->w_p_scb)
7180 {
7181 did_syncbind = TRUE;
7182 checkpcmark();
7183 if (old_linenr != curwin->w_cursor.lnum)
7184 {
7185 char_u ctrl_o[2];
7186
7187 ctrl_o[0] = Ctrl_O;
7188 ctrl_o[1] = 0;
7189 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7190 }
7191 }
7192#endif
7193}
7194
7195
7196 static void
7197ex_read(eap)
7198 exarg_T *eap;
7199{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007200 int i;
7201 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7202 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203
7204 if (eap->usefilter) /* :r!cmd */
7205 do_bang(1, eap, FALSE, FALSE, TRUE);
7206 else
7207 {
7208 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7209 return;
7210
7211#ifdef FEAT_BROWSE
7212 if (cmdmod.browse)
7213 {
7214 char_u *browseFile;
7215
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007216 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 NULL, NULL, NULL, curbuf);
7218 if (browseFile != NULL)
7219 {
7220 i = readfile(browseFile, NULL,
7221 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7222 vim_free(browseFile);
7223 }
7224 else
7225 i = OK;
7226 }
7227 else
7228#endif
7229 if (*eap->arg == NUL)
7230 {
7231 if (check_fname() == FAIL) /* check for no file name */
7232 return;
7233 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7234 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7235 }
7236 else
7237 {
7238 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7239 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7240 i = readfile(eap->arg, NULL,
7241 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7242
7243 }
7244 if (i == FAIL)
7245 {
7246#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7247 if (!aborting())
7248#endif
7249 EMSG2(_(e_notopen), eap->arg);
7250 }
7251 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007252 {
7253 if (empty && exmode_active)
7254 {
7255 /* Delete the empty line that remains. Historically ex does
7256 * this but vi doesn't. */
7257 if (eap->line2 == 0)
7258 lnum = curbuf->b_ml.ml_line_count;
7259 else
7260 lnum = 1;
7261 if (*ml_get(lnum) == NUL)
7262 {
7263 ml_delete(lnum, FALSE);
7264 deleted_lines_mark(lnum, 1L);
7265 if (curwin->w_cursor.lnum >= lnum)
7266 --curwin->w_cursor.lnum;
7267 }
7268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 }
7272}
7273
Bram Moolenaarea408852005-06-25 22:49:46 +00007274static char_u *prev_dir = NULL;
7275
7276#if defined(EXITFREE) || defined(PROTO)
7277 void
7278free_cd_dir()
7279{
7280 vim_free(prev_dir);
7281}
7282#endif
7283
7284
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285/*
7286 * ":cd", ":lcd", ":chdir" and ":lchdir".
7287 */
7288 static void
7289ex_cd(eap)
7290 exarg_T *eap;
7291{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 char_u *new_dir;
7293 char_u *tofree;
7294
7295 new_dir = eap->arg;
7296#if !defined(UNIX) && !defined(VMS)
7297 /* for non-UNIX ":cd" means: print current directory */
7298 if (*new_dir == NUL)
7299 ex_pwd(NULL);
7300 else
7301#endif
7302 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007303 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7304 && !eap->forceit)
7305 {
7306 EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)"));
7307 return;
7308 }
7309
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 /* ":cd -": Change to previous directory */
7311 if (STRCMP(new_dir, "-") == 0)
7312 {
7313 if (prev_dir == NULL)
7314 {
7315 EMSG(_("E186: No previous directory"));
7316 return;
7317 }
7318 new_dir = prev_dir;
7319 }
7320
7321 /* Save current directory for next ":cd -" */
7322 tofree = prev_dir;
7323 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7324 prev_dir = vim_strsave(NameBuff);
7325 else
7326 prev_dir = NULL;
7327
7328#if defined(UNIX) || defined(VMS)
7329 /* for UNIX ":cd" means: go to home directory */
7330 if (*new_dir == NUL)
7331 {
7332 /* use NameBuff for home directory name */
7333# ifdef VMS
7334 char_u *p;
7335
7336 p = mch_getenv((char_u *)"SYS$LOGIN");
7337 if (p == NULL || *p == NUL) /* empty is the same as not set */
7338 NameBuff[0] = NUL;
7339 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007340 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341# else
7342 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7343# endif
7344 new_dir = NameBuff;
7345 }
7346#endif
7347 if (new_dir == NULL || vim_chdir(new_dir))
7348 EMSG(_(e_failed));
7349 else
7350 {
7351 vim_free(curwin->w_localdir);
7352 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7353 {
7354 /* If still in global directory, need to remember current
7355 * directory as global directory. */
7356 if (globaldir == NULL && prev_dir != NULL)
7357 globaldir = vim_strsave(prev_dir);
7358 /* Remember this local directory for the window. */
7359 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7360 curwin->w_localdir = vim_strsave(NameBuff);
7361 }
7362 else
7363 {
7364 /* We are now in the global directory, no need to remember its
7365 * name. */
7366 vim_free(globaldir);
7367 globaldir = NULL;
7368 curwin->w_localdir = NULL;
7369 }
7370
7371 shorten_fnames(TRUE);
7372
7373 /* Echo the new current directory if the command was typed. */
7374 if (KeyTyped)
7375 ex_pwd(eap);
7376 }
7377 vim_free(tofree);
7378 }
7379}
7380
7381/*
7382 * ":pwd".
7383 */
7384/*ARGSUSED*/
7385 static void
7386ex_pwd(eap)
7387 exarg_T *eap;
7388{
7389 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7390 {
7391#ifdef BACKSLASH_IN_FILENAME
7392 slash_adjust(NameBuff);
7393#endif
7394 msg(NameBuff);
7395 }
7396 else
7397 EMSG(_("E187: Unknown"));
7398}
7399
7400/*
7401 * ":=".
7402 */
7403 static void
7404ex_equal(eap)
7405 exarg_T *eap;
7406{
7407 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007408 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409}
7410
7411 static void
7412ex_sleep(eap)
7413 exarg_T *eap;
7414{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007415 int n;
7416 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417
7418 if (cursor_valid())
7419 {
7420 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7421 if (n >= 0)
7422 windgoto((int)n, curwin->w_wcol);
7423 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007424
7425 len = eap->line2;
7426 switch (*eap->arg)
7427 {
7428 case 'm': break;
7429 case NUL: len *= 1000L; break;
7430 default: EMSG2(_(e_invarg2), eap->arg); return;
7431 }
7432 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433}
7434
7435/*
7436 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7437 */
7438 void
7439do_sleep(msec)
7440 long msec;
7441{
7442 long done;
7443
7444 cursor_on();
7445 out_flush();
7446 for (done = 0; !got_int && done < msec; done += 1000L)
7447 {
7448 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
7449 ui_breakcheck();
7450 }
7451}
7452
7453 static void
7454do_exmap(eap, isabbrev)
7455 exarg_T *eap;
7456 int isabbrev;
7457{
7458 int mode;
7459 char_u *cmdp;
7460
7461 cmdp = eap->cmd;
7462 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7463
7464 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7465 eap->arg, mode, isabbrev))
7466 {
7467 case 1: EMSG(_(e_invarg));
7468 break;
7469 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
7470 break;
7471 }
7472}
7473
7474/*
7475 * ":winsize" command (obsolete).
7476 */
7477 static void
7478ex_winsize(eap)
7479 exarg_T *eap;
7480{
7481 int w, h;
7482 char_u *arg = eap->arg;
7483 char_u *p;
7484
7485 w = getdigits(&arg);
7486 arg = skipwhite(arg);
7487 p = arg;
7488 h = getdigits(&arg);
7489 if (*p != NUL && *arg == NUL)
7490 set_shellsize(w, h, TRUE);
7491 else
7492 EMSG(_("E465: :winsize requires two number arguments"));
7493}
7494
7495#ifdef FEAT_WINDOWS
7496 static void
7497ex_wincmd(eap)
7498 exarg_T *eap;
7499{
7500 int xchar = NUL;
7501 char_u *p;
7502
7503 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7504 {
7505 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7506 if (eap->arg[1] == NUL)
7507 {
7508 EMSG(_(e_invarg));
7509 return;
7510 }
7511 xchar = eap->arg[1];
7512 p = eap->arg + 2;
7513 }
7514 else
7515 p = eap->arg + 1;
7516
7517 eap->nextcmd = check_nextcmd(p);
7518 p = skipwhite(p);
7519 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
7520 EMSG(_(e_invarg));
7521 else
7522 {
7523 /* Pass flags on for ":vertical wincmd ]". */
7524 postponed_split_flags = cmdmod.split;
7525 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7526 postponed_split_flags = 0;
7527 }
7528}
7529#endif
7530
Bram Moolenaar843ee412004-06-30 16:16:41 +00007531#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532/*
7533 * ":winpos".
7534 */
7535 static void
7536ex_winpos(eap)
7537 exarg_T *eap;
7538{
7539 int x, y;
7540 char_u *arg = eap->arg;
7541 char_u *p;
7542
7543 if (*arg == NUL)
7544 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007545# if defined(FEAT_GUI) || defined(MSWIN)
7546# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007548# else
7549 if (mch_get_winpos(&x, &y) != FAIL)
7550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 {
7552 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
7553 msg(IObuff);
7554 }
7555 else
7556# endif
7557 EMSG(_("E188: Obtaining window position not implemented for this platform"));
7558 }
7559 else
7560 {
7561 x = getdigits(&arg);
7562 arg = skipwhite(arg);
7563 p = arg;
7564 y = getdigits(&arg);
7565 if (*p == NUL || *arg != NUL)
7566 {
7567 EMSG(_("E466: :winpos requires two number arguments"));
7568 return;
7569 }
7570# ifdef FEAT_GUI
7571 if (gui.in_use)
7572 gui_mch_set_winpos(x, y);
7573 else if (gui.starting)
7574 {
7575 /* Remember the coordinates for when the window is opened. */
7576 gui_win_x = x;
7577 gui_win_y = y;
7578 }
7579# ifdef HAVE_TGETENT
7580 else
7581# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007582# else
7583# ifdef MSWIN
7584 mch_set_winpos(x, y);
7585# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586# endif
7587# ifdef HAVE_TGETENT
7588 if (*T_CWP)
7589 term_set_winpos(x, y);
7590# endif
7591 }
7592}
7593#endif
7594
7595/*
7596 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7597 */
7598 static void
7599ex_operators(eap)
7600 exarg_T *eap;
7601{
7602 oparg_T oa;
7603
7604 clear_oparg(&oa);
7605 oa.regname = eap->regname;
7606 oa.start.lnum = eap->line1;
7607 oa.end.lnum = eap->line2;
7608 oa.line_count = eap->line2 - eap->line1 + 1;
7609 oa.motion_type = MLINE;
7610#ifdef FEAT_VIRTUALEDIT
7611 virtual_op = FALSE;
7612#endif
7613 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7614 {
7615 setpcmark();
7616 curwin->w_cursor.lnum = eap->line1;
7617 beginline(BL_SOL | BL_FIX);
7618 }
7619
7620 switch (eap->cmdidx)
7621 {
7622 case CMD_delete:
7623 oa.op_type = OP_DELETE;
7624 op_delete(&oa);
7625 break;
7626
7627 case CMD_yank:
7628 oa.op_type = OP_YANK;
7629 (void)op_yank(&oa, FALSE, TRUE);
7630 break;
7631
7632 default: /* CMD_rshift or CMD_lshift */
7633 if ((eap->cmdidx == CMD_rshift)
7634#ifdef FEAT_RIGHTLEFT
7635 ^ curwin->w_p_rl
7636#endif
7637 )
7638 oa.op_type = OP_RSHIFT;
7639 else
7640 oa.op_type = OP_LSHIFT;
7641 op_shift(&oa, FALSE, eap->amount);
7642 break;
7643 }
7644#ifdef FEAT_VIRTUALEDIT
7645 virtual_op = MAYBE;
7646#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007647 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648}
7649
7650/*
7651 * ":put".
7652 */
7653 static void
7654ex_put(eap)
7655 exarg_T *eap;
7656{
7657 /* ":0put" works like ":1put!". */
7658 if (eap->line2 == 0)
7659 {
7660 eap->line2 = 1;
7661 eap->forceit = TRUE;
7662 }
7663 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007664 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7665 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666}
7667
7668/*
7669 * Handle ":copy" and ":move".
7670 */
7671 static void
7672ex_copymove(eap)
7673 exarg_T *eap;
7674{
7675 long n;
7676
7677 n = get_address(&eap->arg, FALSE, FALSE);
7678 if (eap->arg == NULL) /* error detected */
7679 {
7680 eap->nextcmd = NULL;
7681 return;
7682 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007683 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684
7685 /*
7686 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7687 */
7688 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7689 {
7690 EMSG(_(e_invaddr));
7691 return;
7692 }
7693
7694 if (eap->cmdidx == CMD_move)
7695 {
7696 if (do_move(eap->line1, eap->line2, n) == FAIL)
7697 return;
7698 }
7699 else
7700 ex_copy(eap->line1, eap->line2, n);
7701 u_clearline();
7702 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007703 ex_may_print(eap);
7704}
7705
7706/*
7707 * Print the current line if flags were given to the Ex command.
7708 */
7709 static void
7710ex_may_print(eap)
7711 exarg_T *eap;
7712{
7713 if (eap->flags != 0)
7714 {
7715 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7716 (eap->flags & EXFLAG_LIST));
7717 ex_no_reprint = TRUE;
7718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719}
7720
7721/*
7722 * ":smagic" and ":snomagic".
7723 */
7724 static void
7725ex_submagic(eap)
7726 exarg_T *eap;
7727{
7728 int magic_save = p_magic;
7729
7730 p_magic = (eap->cmdidx == CMD_smagic);
7731 do_sub(eap);
7732 p_magic = magic_save;
7733}
7734
7735/*
7736 * ":join".
7737 */
7738 static void
7739ex_join(eap)
7740 exarg_T *eap;
7741{
7742 curwin->w_cursor.lnum = eap->line1;
7743 if (eap->line1 == eap->line2)
7744 {
7745 if (eap->addr_count >= 2) /* :2,2join does nothing */
7746 return;
7747 if (eap->line2 == curbuf->b_ml.ml_line_count)
7748 {
7749 beep_flush();
7750 return;
7751 }
7752 ++eap->line2;
7753 }
7754 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit);
7755 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007756 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757}
7758
7759/*
7760 * ":[addr]@r" or ":[addr]*r": execute register
7761 */
7762 static void
7763ex_at(eap)
7764 exarg_T *eap;
7765{
7766 int c;
7767
7768 curwin->w_cursor.lnum = eap->line2;
7769
7770#ifdef USE_ON_FLY_SCROLL
7771 dont_scroll = TRUE; /* disallow scrolling here */
7772#endif
7773
7774 /* get the register name. No name means to use the previous one */
7775 c = *eap->arg;
7776 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7777 c = '@';
7778 /* put the register in mapbuf */
7779 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 else
7784 {
7785 int save_efr = exec_from_reg;
7786
7787 exec_from_reg = TRUE;
7788
7789 /*
7790 * Execute from the typeahead buffer.
7791 * Originally this didn't check for the typeahead buffer to be empty,
7792 * thus could read more Ex commands from stdin. It's not clear why,
7793 * it is certainly unexpected.
7794 */
7795 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':')
7796 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7797
7798 exec_from_reg = save_efr;
7799 }
7800}
7801
7802/*
7803 * ":!".
7804 */
7805 static void
7806ex_bang(eap)
7807 exarg_T *eap;
7808{
7809 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7810}
7811
7812/*
7813 * ":undo".
7814 */
7815/*ARGSUSED*/
7816 static void
7817ex_undo(eap)
7818 exarg_T *eap;
7819{
7820 u_undo(1);
7821}
7822
7823/*
7824 * ":redo".
7825 */
7826/*ARGSUSED*/
7827 static void
7828ex_redo(eap)
7829 exarg_T *eap;
7830{
7831 u_redo(1);
7832}
7833
7834/*
7835 * ":redir": start/stop redirection.
7836 */
7837 static void
7838ex_redir(eap)
7839 exarg_T *eap;
7840{
7841 char *mode;
7842 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007843 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844
7845 if (STRICMP(eap->arg, "END") == 0)
7846 close_redir();
7847 else
7848 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007849 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007851 ++arg;
7852 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007854 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 mode = "a";
7856 }
7857 else
7858 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007859 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860
7861 close_redir();
7862
7863 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00007864 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 if (fname == NULL)
7866 return;
7867#ifdef FEAT_BROWSE
7868 if (cmdmod.browse)
7869 {
7870 char_u *browseFile;
7871
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007872 browseFile = do_browse(BROWSE_SAVE,
7873 (char_u *)_("Save Redirection"),
7874 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 if (browseFile == NULL)
7876 return; /* operation cancelled */
7877 vim_free(fname);
7878 fname = browseFile;
7879 eap->forceit = TRUE; /* since dialog already asked */
7880 }
7881#endif
7882
7883 redir_fd = open_exfile(fname, eap->forceit, mode);
7884 vim_free(fname);
7885 }
7886#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007887 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {
7889 /* redirect to a register a-z (resp. A-Z for appending) */
7890 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007891 ++arg;
7892 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007894 || *arg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007896 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007898 redir_reg = *arg++;
Bram Moolenaard9d30582005-05-18 22:10:28 +00007899 if (*arg == '>' && arg[1] == '>')
7900 arg += 2;
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007901 else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) &&
7902 (islower(redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007904 || redir_reg == '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007906 || redir_reg == '"'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 {
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007908 if (*arg == '>')
7909 arg++;
7910
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 /* make register empty */
7912 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
7913 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007914 }
7915 if (*arg != NUL)
7916 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007917 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00007918 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007919 }
7920 }
7921 else if (*arg == '=' && arg[1] == '>')
7922 {
7923 int append;
7924
7925 /* redirect to a variable */
7926 close_redir();
7927 arg += 2;
7928
7929 if (*arg == '>')
7930 {
7931 ++arg;
7932 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 }
7934 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007935 append = FALSE;
7936
7937 if (var_redir_start(skipwhite(arg), append) == OK)
7938 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 }
7940#endif
7941
7942 /* TODO: redirect to a buffer */
7943
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 else
Bram Moolenaarca472992005-01-21 11:46:23 +00007945 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 }
7947}
7948
7949/*
7950 * ":redraw": force redraw
7951 */
7952 static void
7953ex_redraw(eap)
7954 exarg_T *eap;
7955{
7956 int r = RedrawingDisabled;
7957 int p = p_lz;
7958
7959 RedrawingDisabled = 0;
7960 p_lz = FALSE;
7961 update_topline();
7962 update_screen(eap->forceit ? CLEAR :
7963#ifdef FEAT_VISUAL
7964 VIsual_active ? INVERTED :
7965#endif
7966 0);
7967#ifdef FEAT_TITLE
7968 if (need_maketitle)
7969 maketitle();
7970#endif
7971 RedrawingDisabled = r;
7972 p_lz = p;
7973
7974 /* Reset msg_didout, so that a message that's there is overwritten. */
7975 msg_didout = FALSE;
7976 msg_col = 0;
7977
7978 out_flush();
7979}
7980
7981/*
7982 * ":redrawstatus": force redraw of status line(s)
7983 */
7984/*ARGSUSED*/
7985 static void
7986ex_redrawstatus(eap)
7987 exarg_T *eap;
7988{
7989#if defined(FEAT_WINDOWS)
7990 int r = RedrawingDisabled;
7991 int p = p_lz;
7992
7993 RedrawingDisabled = 0;
7994 p_lz = FALSE;
7995 if (eap->forceit)
7996 status_redraw_all();
7997 else
7998 status_redraw_curbuf();
7999 update_screen(
8000# ifdef FEAT_VISUAL
8001 VIsual_active ? INVERTED :
8002# endif
8003 0);
8004 RedrawingDisabled = r;
8005 p_lz = p;
8006 out_flush();
8007#endif
8008}
8009
8010 static void
8011close_redir()
8012{
8013 if (redir_fd != NULL)
8014 {
8015 fclose(redir_fd);
8016 redir_fd = NULL;
8017 }
8018#ifdef FEAT_EVAL
8019 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008020 if (redir_vname)
8021 {
8022 var_redir_stop();
8023 redir_vname = 0;
8024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025#endif
8026}
8027
8028#if defined(FEAT_SESSION) && defined(USE_CRNL)
8029# define MKSESSION_NL
8030static int mksession_nl = FALSE; /* use NL only in put_eol() */
8031#endif
8032
8033/*
8034 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8035 */
8036 static void
8037ex_mkrc(eap)
8038 exarg_T *eap;
8039{
8040 FILE *fd;
8041 int failed = FALSE;
8042 char_u *fname;
8043#ifdef FEAT_BROWSE
8044 char_u *browseFile = NULL;
8045#endif
8046#ifdef FEAT_SESSION
8047 int view_session = FALSE;
8048 int using_vdir = FALSE; /* using 'viewdir'? */
8049 char_u *viewFile = NULL;
8050 unsigned *flagp;
8051#endif
8052
8053 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8054 {
8055#ifdef FEAT_SESSION
8056 view_session = TRUE;
8057#else
8058 ex_ni(eap);
8059 return;
8060#endif
8061 }
8062
8063#ifdef FEAT_SESSION
8064 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8065 if (eap->cmdidx == CMD_mkview
8066 && (*eap->arg == NUL
8067 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8068 {
8069 eap->forceit = TRUE;
8070 fname = get_view_file(*eap->arg);
8071 if (fname == NULL)
8072 return;
8073 viewFile = fname;
8074 using_vdir = TRUE;
8075 }
8076 else
8077#endif
8078 if (*eap->arg != NUL)
8079 fname = eap->arg;
8080 else if (eap->cmdidx == CMD_mkvimrc)
8081 fname = (char_u *)VIMRC_FILE;
8082#ifdef FEAT_SESSION
8083 else if (eap->cmdidx == CMD_mksession)
8084 fname = (char_u *)SESSION_FILE;
8085#endif
8086 else
8087 fname = (char_u *)EXRC_FILE;
8088
8089#ifdef FEAT_BROWSE
8090 if (cmdmod.browse)
8091 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008092 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093# ifdef FEAT_SESSION
8094 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8095 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8096# endif
8097 (char_u *)_("Save Setup"),
8098 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8099 if (browseFile == NULL)
8100 goto theend;
8101 fname = browseFile;
8102 eap->forceit = TRUE; /* since dialog already asked */
8103 }
8104#endif
8105
8106#if defined(FEAT_SESSION) && defined(vim_mkdir)
8107 /* When using 'viewdir' may have to create the directory. */
8108 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008109 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110#endif
8111
8112 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8113 if (fd != NULL)
8114 {
8115#ifdef FEAT_SESSION
8116 if (eap->cmdidx == CMD_mkview)
8117 flagp = &vop_flags;
8118 else
8119 flagp = &ssop_flags;
8120#endif
8121
8122#ifdef MKSESSION_NL
8123 /* "unix" in 'sessionoptions': use NL line separator */
8124 if (view_session && (*flagp & SSOP_UNIX))
8125 mksession_nl = TRUE;
8126#endif
8127
8128 /* Write the version command for :mkvimrc */
8129 if (eap->cmdidx == CMD_mkvimrc)
8130 (void)put_line(fd, "version 6.0");
8131
8132#ifdef FEAT_SESSION
8133 if (eap->cmdidx != CMD_mkview)
8134#endif
8135 {
8136 /* Write setting 'compatible' first, because it has side effects.
8137 * For that same reason only do it when needed. */
8138 if (p_cp)
8139 (void)put_line(fd, "if !&cp | set cp | endif");
8140 else
8141 (void)put_line(fd, "if &cp | set nocp | endif");
8142 }
8143
8144#ifdef FEAT_SESSION
8145 if (!view_session
8146 || (eap->cmdidx == CMD_mksession
8147 && (*flagp & SSOP_OPTIONS)))
8148#endif
8149 failed |= (makemap(fd, NULL) == FAIL
8150 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8151
8152#ifdef FEAT_SESSION
8153 if (!failed && view_session)
8154 {
8155 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8156 failed = TRUE;
8157 if (eap->cmdidx == CMD_mksession)
8158 {
8159 char_u dirnow[MAXPATHL]; /* current directory */
8160
8161 /*
8162 * Change to session file's dir.
8163 */
8164 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8165 || mch_chdir((char *)dirnow) != 0)
8166 *dirnow = NUL;
8167 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8168 {
8169 if (vim_chdirfile(fname) == OK)
8170 shorten_fnames(TRUE);
8171 }
8172 else if (*dirnow != NUL
8173 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8174 {
8175 (void)mch_chdir((char *)globaldir);
8176 shorten_fnames(TRUE);
8177 }
8178
8179 failed |= (makeopens(fd, dirnow) == FAIL);
8180
8181 /* restore original dir */
8182 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8183 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8184 {
8185 if (mch_chdir((char *)dirnow) != 0)
8186 EMSG(_(e_prev_dir));
8187 shorten_fnames(TRUE);
8188 }
8189 }
8190 else
8191 {
8192 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL);
8193 }
8194 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8195 == FAIL)
8196 failed = TRUE;
8197 }
8198#endif
8199 failed |= fclose(fd);
8200
8201 if (failed)
8202 EMSG(_(e_write));
8203#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8204 else if (eap->cmdidx == CMD_mksession)
8205 {
8206 /* successful session write - set this_session var */
8207 char_u tbuf[MAXPATHL];
8208
8209 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8210 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8211 }
8212#endif
8213#ifdef MKSESSION_NL
8214 mksession_nl = FALSE;
8215#endif
8216 }
8217
8218#ifdef FEAT_BROWSE
8219theend:
8220 vim_free(browseFile);
8221#endif
8222#ifdef FEAT_SESSION
8223 vim_free(viewFile);
8224#endif
8225}
8226
Bram Moolenaardf177f62005-02-22 08:39:57 +00008227#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8228 || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008229/*ARGSUSED*/
Bram Moolenaardf177f62005-02-22 08:39:57 +00008230 int
8231vim_mkdir_emsg(name, prot)
8232 char_u *name;
8233 int prot;
8234{
8235 if (vim_mkdir(name, prot) != 0)
8236 {
8237 EMSG2(_("E739: Cannot create directory: %s"), name);
8238 return FAIL;
8239 }
8240 return OK;
8241}
8242#endif
8243
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244/*
8245 * Open a file for writing for an Ex command, with some checks.
8246 * Return file descriptor, or NULL on failure.
8247 */
8248 FILE *
8249open_exfile(fname, forceit, mode)
8250 char_u *fname;
8251 int forceit;
8252 char *mode; /* "w" for create new file or "a" for append */
8253{
8254 FILE *fd;
8255
8256#ifdef UNIX
8257 /* with Unix it is possible to open a directory */
8258 if (mch_isdir(fname))
8259 {
8260 EMSG2(_(e_isadir2), fname);
8261 return NULL;
8262 }
8263#endif
8264 if (!forceit && *mode != 'a' && vim_fexists(fname))
8265 {
8266 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
8267 return NULL;
8268 }
8269
8270 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
8271 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
8272
8273 return fd;
8274}
8275
8276/*
8277 * ":mark" and ":k".
8278 */
8279 static void
8280ex_mark(eap)
8281 exarg_T *eap;
8282{
8283 pos_T pos;
8284
8285 if (*eap->arg == NUL) /* No argument? */
8286 EMSG(_(e_argreq));
8287 else if (eap->arg[1] != NUL) /* more than one character? */
8288 EMSG(_(e_trailing));
8289 else
8290 {
8291 pos = curwin->w_cursor; /* save curwin->w_cursor */
8292 curwin->w_cursor.lnum = eap->line2;
8293 beginline(BL_WHITE | BL_FIX);
8294 if (setmark(*eap->arg) == FAIL) /* set mark */
8295 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
8296 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8297 }
8298}
8299
8300/*
8301 * Update w_topline, w_leftcol and the cursor position.
8302 */
8303 void
8304update_topline_cursor()
8305{
8306 check_cursor(); /* put cursor on valid line */
8307 update_topline();
8308 if (!curwin->w_p_wrap)
8309 validate_cursor();
8310 update_curswant();
8311}
8312
8313#ifdef FEAT_EX_EXTRA
8314/*
8315 * ":normal[!] {commands}": Execute normal mode commands.
8316 */
8317 static void
8318ex_normal(eap)
8319 exarg_T *eap;
8320{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 int save_msg_scroll = msg_scroll;
8322 int save_restart_edit = restart_edit;
8323 int save_msg_didout = msg_didout;
8324 int save_State = State;
8325 tasave_T tabuf;
8326 int save_insertmode = p_im;
8327 int save_finish_op = finish_op;
8328#ifdef FEAT_MBYTE
8329 char_u *arg = NULL;
8330 int l;
8331 char_u *p;
8332#endif
8333
8334 if (ex_normal_busy >= p_mmd)
8335 {
8336 EMSG(_("E192: Recursive use of :normal too deep"));
8337 return;
8338 }
8339 ++ex_normal_busy;
8340
8341 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8342 restart_edit = 0; /* don't go to Insert mode */
8343 p_im = FALSE; /* don't use 'insertmode' */
8344
8345#ifdef FEAT_MBYTE
8346 /*
8347 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8348 * this for the K_SPECIAL leading byte, otherwise special keys will not
8349 * work.
8350 */
8351 if (has_mbyte)
8352 {
8353 int len = 0;
8354
8355 /* Count the number of characters to be escaped. */
8356 for (p = eap->arg; *p != NUL; ++p)
8357 {
8358# ifdef FEAT_GUI
8359 if (*p == CSI) /* leadbyte CSI */
8360 len += 2;
8361# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008362 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
8364# ifdef FEAT_GUI
8365 || *p == CSI
8366# endif
8367 )
8368 len += 2;
8369 }
8370 if (len > 0)
8371 {
8372 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8373 if (arg != NULL)
8374 {
8375 len = 0;
8376 for (p = eap->arg; *p != NUL; ++p)
8377 {
8378 arg[len++] = *p;
8379# ifdef FEAT_GUI
8380 if (*p == CSI)
8381 {
8382 arg[len++] = KS_EXTRA;
8383 arg[len++] = (int)KE_CSI;
8384 }
8385# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008386 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 {
8388 arg[len++] = *++p;
8389 if (*p == K_SPECIAL)
8390 {
8391 arg[len++] = KS_SPECIAL;
8392 arg[len++] = KE_FILLER;
8393 }
8394# ifdef FEAT_GUI
8395 else if (*p == CSI)
8396 {
8397 arg[len++] = KS_EXTRA;
8398 arg[len++] = (int)KE_CSI;
8399 }
8400# endif
8401 }
8402 arg[len] = NUL;
8403 }
8404 }
8405 }
8406 }
8407#endif
8408
8409 /*
8410 * Save the current typeahead. This is required to allow using ":normal"
8411 * from an event handler and makes sure we don't hang when the argument
8412 * ends with half a command.
8413 */
8414 save_typeahead(&tabuf);
8415 if (tabuf.typebuf_valid)
8416 {
8417 /*
8418 * Repeat the :normal command for each line in the range. When no
8419 * range given, execute it just once, without positioning the cursor
8420 * first.
8421 */
8422 do
8423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 if (eap->addr_count != 0)
8425 {
8426 curwin->w_cursor.lnum = eap->line1++;
8427 curwin->w_cursor.col = 0;
8428 }
8429
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008430 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431#ifdef FEAT_MBYTE
8432 arg != NULL ? arg :
8433#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008434 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 }
8436 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8437 }
8438
8439 /* Might not return to the main loop when in an event handler. */
8440 update_topline_cursor();
8441
8442 /* Restore the previous typeahead. */
8443 restore_typeahead(&tabuf);
8444
8445 --ex_normal_busy;
8446 msg_scroll = save_msg_scroll;
8447 restart_edit = save_restart_edit;
8448 p_im = save_insertmode;
8449 finish_op = save_finish_op;
8450 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
8451
8452 /* Restore the state (needed when called from a function executed for
8453 * 'indentexpr'). */
8454 State = save_State;
8455#ifdef FEAT_MBYTE
8456 vim_free(arg);
8457#endif
8458}
8459
8460/*
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008461 * ":startinsert" and ":startreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 */
8463 static void
8464ex_startinsert(eap)
8465 exarg_T *eap;
8466{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008467 if (eap->forceit)
8468 {
8469 coladvance((colnr_T)MAXCOL);
8470 curwin->w_curswant = MAXCOL;
8471 curwin->w_set_curswant = FALSE;
8472 }
8473
Bram Moolenaara40c5002005-01-09 21:16:21 +00008474 /* Ignore the command when already in Insert mode. Inserting an
8475 * expression register that invokes a function can do this. */
8476 if (State & INSERT)
8477 return;
8478
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 if (eap->forceit)
8480 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008481 if (eap->cmdidx == CMD_startinsert)
8482 restart_edit = 'a';
8483 else
8484 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 }
8486 else
8487 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008488 if (eap->cmdidx == CMD_startinsert)
8489 restart_edit = 'i';
8490 else
8491 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 curwin->w_curswant = 0; /* avoid MAXCOL */
8493 }
8494}
8495
8496/*
8497 * ":stopinsert"
8498 */
8499/*ARGSUSED*/
8500 static void
8501ex_stopinsert(eap)
8502 exarg_T *eap;
8503{
8504 restart_edit = 0;
8505 stop_insert_mode = TRUE;
8506}
8507#endif
8508
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008509#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
8510/*
8511 * Execute normal mode command "cmd".
8512 * "remap" can be REMAP_NONE or REMAP_YES.
8513 */
8514 void
8515exec_normal_cmd(cmd, remap, silent)
8516 char_u *cmd;
8517 int remap;
8518 int silent;
8519{
8520 oparg_T oa;
8521
8522 /*
8523 * Stuff the argument into the typeahead buffer.
8524 * Execute normal_cmd() until there is no typeahead left.
8525 */
8526 clear_oparg(&oa);
8527 finish_op = FALSE;
8528 ins_typebuf(cmd, remap, 0, TRUE, silent);
8529 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
8530 && !got_int)
8531 {
8532 update_topline_cursor();
8533 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
8534 }
8535}
8536#endif
8537
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538#ifdef FEAT_FIND_ID
8539 static void
8540ex_checkpath(eap)
8541 exarg_T *eap;
8542{
8543 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8544 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8545 (linenr_T)1, (linenr_T)MAXLNUM);
8546}
8547
8548#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
8549/*
8550 * ":psearch"
8551 */
8552 static void
8553ex_psearch(eap)
8554 exarg_T *eap;
8555{
8556 g_do_tagpreview = p_pvh;
8557 ex_findpat(eap);
8558 g_do_tagpreview = 0;
8559}
8560#endif
8561
8562 static void
8563ex_findpat(eap)
8564 exarg_T *eap;
8565{
8566 int whole = TRUE;
8567 long n;
8568 char_u *p;
8569 int action;
8570
8571 switch (cmdnames[eap->cmdidx].cmd_name[2])
8572 {
8573 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8574 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8575 action = ACTION_GOTO;
8576 else
8577 action = ACTION_SHOW;
8578 break;
8579 case 'i': /* ":ilist" and ":dlist" */
8580 action = ACTION_SHOW_ALL;
8581 break;
8582 case 'u': /* ":ijump" and ":djump" */
8583 action = ACTION_GOTO;
8584 break;
8585 default: /* ":isplit" and ":dsplit" */
8586 action = ACTION_SPLIT;
8587 break;
8588 }
8589
8590 n = 1;
8591 if (vim_isdigit(*eap->arg)) /* get count */
8592 {
8593 n = getdigits(&eap->arg);
8594 eap->arg = skipwhite(eap->arg);
8595 }
8596 if (*eap->arg == '/') /* Match regexp, not just whole words */
8597 {
8598 whole = FALSE;
8599 ++eap->arg;
8600 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8601 if (*p)
8602 {
8603 *p++ = NUL;
8604 p = skipwhite(p);
8605
8606 /* Check for trailing illegal characters */
8607 if (!ends_excmd(*p))
8608 eap->errmsg = e_trailing;
8609 else
8610 eap->nextcmd = check_nextcmd(p);
8611 }
8612 }
8613 if (!eap->skip)
8614 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8615 whole, !eap->forceit,
8616 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8617 n, action, eap->line1, eap->line2);
8618}
8619#endif
8620
8621#ifdef FEAT_WINDOWS
8622
8623# ifdef FEAT_QUICKFIX
8624/*
8625 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8626 */
8627 static void
8628ex_ptag(eap)
8629 exarg_T *eap;
8630{
8631 g_do_tagpreview = p_pvh;
8632 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8633}
8634
8635/*
8636 * ":pedit"
8637 */
8638 static void
8639ex_pedit(eap)
8640 exarg_T *eap;
8641{
8642 win_T *curwin_save = curwin;
8643
8644 g_do_tagpreview = p_pvh;
8645 prepare_tagpreview();
8646 keep_help_flag = curwin_save->w_buffer->b_help;
8647 do_exedit(eap, NULL);
8648 keep_help_flag = FALSE;
8649 if (curwin != curwin_save && win_valid(curwin_save))
8650 {
8651 /* Return cursor to where we were */
8652 validate_cursor();
8653 redraw_later(VALID);
8654 win_enter(curwin_save, TRUE);
8655 }
8656 g_do_tagpreview = 0;
8657}
8658# endif
8659
8660/*
8661 * ":stag", ":stselect" and ":stjump".
8662 */
8663 static void
8664ex_stag(eap)
8665 exarg_T *eap;
8666{
8667 postponed_split = -1;
8668 postponed_split_flags = cmdmod.split;
8669 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8670 postponed_split_flags = 0;
8671}
8672#endif
8673
8674/*
8675 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8676 */
8677 static void
8678ex_tag(eap)
8679 exarg_T *eap;
8680{
8681 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8682}
8683
8684 static void
8685ex_tag_cmd(eap, name)
8686 exarg_T *eap;
8687 char_u *name;
8688{
8689 int cmd;
8690
8691 switch (name[1])
8692 {
8693 case 'j': cmd = DT_JUMP; /* ":tjump" */
8694 break;
8695 case 's': cmd = DT_SELECT; /* ":tselect" */
8696 break;
8697 case 'p': cmd = DT_PREV; /* ":tprevious" */
8698 break;
8699 case 'N': cmd = DT_PREV; /* ":tNext" */
8700 break;
8701 case 'n': cmd = DT_NEXT; /* ":tnext" */
8702 break;
8703 case 'o': cmd = DT_POP; /* ":pop" */
8704 break;
8705 case 'f': /* ":tfirst" */
8706 case 'r': cmd = DT_FIRST; /* ":trewind" */
8707 break;
8708 case 'l': cmd = DT_LAST; /* ":tlast" */
8709 break;
8710 default: /* ":tag" */
8711#ifdef FEAT_CSCOPE
8712 if (p_cst)
8713 {
8714 do_cstag(eap);
8715 return;
8716 }
8717#endif
8718 cmd = DT_TAG;
8719 break;
8720 }
8721
8722 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8723 eap->forceit, TRUE);
8724}
8725
8726/*
8727 * Evaluate cmdline variables.
8728 *
8729 * change '%' to curbuf->b_ffname
8730 * '#' to curwin->w_altfile
8731 * '<cword>' to word under the cursor
8732 * '<cWORD>' to WORD under the cursor
8733 * '<cfile>' to path name under the cursor
8734 * '<sfile>' to sourced file name
8735 * '<afile>' to file name for autocommand
8736 * '<abuf>' to buffer number for autocommand
8737 * '<amatch>' to matching name for autocommand
8738 *
8739 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8740 * "" for error without a message) and NULL is returned.
8741 * Returns an allocated string if a valid match was found.
8742 * Returns NULL if no match was found. "usedlen" then still contains the
8743 * number of characters to skip.
8744 */
8745 char_u *
8746eval_vars(src, usedlen, lnump, errormsg, srcstart)
8747 char_u *src; /* pointer into commandline */
8748 int *usedlen; /* characters after src that are used */
8749 linenr_T *lnump; /* line number for :e command, or NULL */
8750 char_u **errormsg; /* pointer to error message */
8751 char_u *srcstart; /* beginning of valid memory for src */
8752{
8753 int i;
8754 char_u *s;
8755 char_u *result;
8756 char_u *resultbuf = NULL;
8757 int resultlen;
8758 buf_T *buf;
8759 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
8760 int spec_idx;
8761#ifdef FEAT_MODIFY_FNAME
8762 int skip_mod = FALSE;
8763#endif
8764 static char *(spec_str[]) =
8765 {
8766 "%",
8767#define SPEC_PERC 0
8768 "#",
8769#define SPEC_HASH 1
8770 "<cword>", /* cursor word */
8771#define SPEC_CWORD 2
8772 "<cWORD>", /* cursor WORD */
8773#define SPEC_CCWORD 3
8774 "<cfile>", /* cursor path name */
8775#define SPEC_CFILE 4
8776 "<sfile>", /* ":so" file name */
8777#define SPEC_SFILE 5
8778#ifdef FEAT_AUTOCMD
8779 "<afile>", /* autocommand file name */
8780# define SPEC_AFILE 6
8781 "<abuf>", /* autocommand buffer number */
8782# define SPEC_ABUF 7
8783 "<amatch>", /* autocommand match name */
8784# define SPEC_AMATCH 8
8785#endif
8786#ifdef FEAT_CLIENTSERVER
8787 "<client>"
8788# define SPEC_CLIENT 9
8789#endif
8790 };
8791#define SPEC_COUNT (sizeof(spec_str) / sizeof(char *))
8792
8793#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
8794 char_u strbuf[30];
8795#endif
8796
8797 *errormsg = NULL;
8798
8799 /*
8800 * Check if there is something to do.
8801 */
8802 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
8803 {
8804 *usedlen = (int)STRLEN(spec_str[spec_idx]);
8805 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0)
8806 break;
8807 }
8808 if (spec_idx == SPEC_COUNT) /* no match */
8809 {
8810 *usedlen = 1;
8811 return NULL;
8812 }
8813
8814 /*
8815 * Skip when preceded with a backslash "\%" and "\#".
8816 * Note: In "\\%" the % is also not recognized!
8817 */
8818 if (src > srcstart && src[-1] == '\\')
8819 {
8820 *usedlen = 0;
8821 STRCPY(src - 1, src); /* remove backslash */
8822 return NULL;
8823 }
8824
8825 /*
8826 * word or WORD under cursor
8827 */
8828 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
8829 {
8830 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
8831 (FIND_IDENT|FIND_STRING) : FIND_STRING);
8832 if (resultlen == 0)
8833 {
8834 *errormsg = (char_u *)"";
8835 return NULL;
8836 }
8837 }
8838
8839 /*
8840 * '#': Alternate file name
8841 * '%': Current file name
8842 * File name under the cursor
8843 * File name for autocommand
8844 * and following modifiers
8845 */
8846 else
8847 {
8848 switch (spec_idx)
8849 {
8850 case SPEC_PERC: /* '%': current file */
8851 if (curbuf->b_fname == NULL)
8852 {
8853 result = (char_u *)"";
8854 valid = 0; /* Must have ":p:h" to be valid */
8855 }
8856 else
8857#ifdef RISCOS
8858 /* Always use the full path for RISC OS if possible. */
8859 result = curbuf->b_ffname;
8860 if (result == NULL)
8861 result = curbuf->b_fname;
8862#else
8863 result = curbuf->b_fname;
8864#endif
8865 break;
8866
8867 case SPEC_HASH: /* '#' or "#99": alternate file */
8868 if (src[1] == '#') /* "##": the argument list */
8869 {
8870 result = arg_all();
8871 resultbuf = result;
8872 *usedlen = 2;
8873#ifdef FEAT_MODIFY_FNAME
8874 skip_mod = TRUE;
8875#endif
8876 break;
8877 }
8878 s = src + 1;
8879 i = (int)getdigits(&s);
8880 *usedlen = (int)(s - src); /* length of what we expand */
8881
8882 buf = buflist_findnr(i);
8883 if (buf == NULL)
8884 {
8885 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
8886 return NULL;
8887 }
8888 if (lnump != NULL)
8889 *lnump = ECMD_LAST;
8890 if (buf->b_fname == NULL)
8891 {
8892 result = (char_u *)"";
8893 valid = 0; /* Must have ":p:h" to be valid */
8894 }
8895 else
8896 result = buf->b_fname;
8897 break;
8898
8899#ifdef FEAT_SEARCHPATH
8900 case SPEC_CFILE: /* file name under cursor */
8901 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L);
8902 if (result == NULL)
8903 {
8904 *errormsg = (char_u *)"";
8905 return NULL;
8906 }
8907 resultbuf = result; /* remember allocated string */
8908 break;
8909#endif
8910
8911#ifdef FEAT_AUTOCMD
8912 case SPEC_AFILE: /* file name for autocommand */
8913 result = autocmd_fname;
8914 if (result == NULL)
8915 {
8916 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
8917 return NULL;
8918 }
8919 break;
8920
8921 case SPEC_ABUF: /* buffer number for autocommand */
8922 if (autocmd_bufnr <= 0)
8923 {
8924 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
8925 return NULL;
8926 }
8927 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8928 result = strbuf;
8929 break;
8930
8931 case SPEC_AMATCH: /* match name for autocommand */
8932 result = autocmd_match;
8933 if (result == NULL)
8934 {
8935 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
8936 return NULL;
8937 }
8938 break;
8939
8940#endif
8941 case SPEC_SFILE: /* file name for ":so" command */
8942 result = sourcing_name;
8943 if (result == NULL)
8944 {
8945 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
8946 return NULL;
8947 }
8948 break;
8949#if defined(FEAT_CLIENTSERVER)
8950 case SPEC_CLIENT: /* Source of last submitted input */
8951 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow);
8952 result = strbuf;
8953 break;
8954#endif
8955 }
8956
8957 resultlen = (int)STRLEN(result); /* length of new string */
8958 if (src[*usedlen] == '<') /* remove the file name extension */
8959 {
8960 ++*usedlen;
8961#ifdef RISCOS
8962 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
8963#else
8964 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
8965#endif
8966 resultlen = (int)(s - result);
8967 }
8968#ifdef FEAT_MODIFY_FNAME
8969 else if (!skip_mod)
8970 {
8971 valid |= modify_fname(src, usedlen, &result, &resultbuf,
8972 &resultlen);
8973 if (result == NULL)
8974 {
8975 *errormsg = (char_u *)"";
8976 return NULL;
8977 }
8978 }
8979#endif
8980 }
8981
8982 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8983 {
8984 if (valid != VALID_HEAD + VALID_PATH)
8985 /* xgettext:no-c-format */
8986 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
8987 else
8988 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
8989 result = NULL;
8990 }
8991 else
8992 result = vim_strnsave(result, resultlen);
8993 vim_free(resultbuf);
8994 return result;
8995}
8996
8997/*
8998 * Concatenate all files in the argument list, separated by spaces, and return
8999 * it in one allocated string.
9000 * Spaces and backslashes in the file names are escaped with a backslash.
9001 * Returns NULL when out of memory.
9002 */
9003 static char_u *
9004arg_all()
9005{
9006 int len;
9007 int idx;
9008 char_u *retval = NULL;
9009 char_u *p;
9010
9011 /*
9012 * Do this loop two times:
9013 * first time: compute the total length
9014 * second time: concatenate the names
9015 */
9016 for (;;)
9017 {
9018 len = 0;
9019 for (idx = 0; idx < ARGCOUNT; ++idx)
9020 {
9021 p = alist_name(&ARGLIST[idx]);
9022 if (p != NULL)
9023 {
9024 if (len > 0)
9025 {
9026 /* insert a space in between names */
9027 if (retval != NULL)
9028 retval[len] = ' ';
9029 ++len;
9030 }
9031 for ( ; *p != NUL; ++p)
9032 {
9033 if (*p == ' ' || *p == '\\')
9034 {
9035 /* insert a backslash */
9036 if (retval != NULL)
9037 retval[len] = '\\';
9038 ++len;
9039 }
9040 if (retval != NULL)
9041 retval[len] = *p;
9042 ++len;
9043 }
9044 }
9045 }
9046
9047 /* second time: break here */
9048 if (retval != NULL)
9049 {
9050 retval[len] = NUL;
9051 break;
9052 }
9053
9054 /* allocate memory */
9055 retval = alloc(len + 1);
9056 if (retval == NULL)
9057 break;
9058 }
9059
9060 return retval;
9061}
9062
9063#if defined(FEAT_AUTOCMD) || defined(PROTO)
9064/*
9065 * Expand the <sfile> string in "arg".
9066 *
9067 * Returns an allocated string, or NULL for any error.
9068 */
9069 char_u *
9070expand_sfile(arg)
9071 char_u *arg;
9072{
9073 char_u *errormsg;
9074 int len;
9075 char_u *result;
9076 char_u *newres;
9077 char_u *repl;
9078 int srclen;
9079 char_u *p;
9080
9081 result = vim_strsave(arg);
9082 if (result == NULL)
9083 return NULL;
9084
9085 for (p = result; *p; )
9086 {
9087 if (STRNCMP(p, "<sfile>", 7) != 0)
9088 ++p;
9089 else
9090 {
9091 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
9092 repl = eval_vars(p, &srclen, NULL, &errormsg, result);
9093 if (errormsg != NULL)
9094 {
9095 if (*errormsg)
9096 emsg(errormsg);
9097 vim_free(result);
9098 return NULL;
9099 }
9100 if (repl == NULL) /* no match (cannot happen) */
9101 {
9102 p += srclen;
9103 continue;
9104 }
9105 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9106 newres = alloc(len);
9107 if (newres == NULL)
9108 {
9109 vim_free(repl);
9110 vim_free(result);
9111 return NULL;
9112 }
9113 mch_memmove(newres, result, (size_t)(p - result));
9114 STRCPY(newres + (p - result), repl);
9115 len = (int)STRLEN(newres);
9116 STRCAT(newres, p + srclen);
9117 vim_free(repl);
9118 vim_free(result);
9119 result = newres;
9120 p = newres + len; /* continue after the match */
9121 }
9122 }
9123
9124 return result;
9125}
9126#endif
9127
9128#ifdef FEAT_SESSION
9129static int ses_winsizes __ARGS((FILE *fd, int restore_size));
9130static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9131static frame_T *ses_skipframe __ARGS((frame_T *fr));
9132static int ses_do_frame __ARGS((frame_T *fr));
9133static int ses_do_win __ARGS((win_T *wp));
9134static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9135static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9136static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9137
9138/*
9139 * Write openfile commands for the current buffers to an .exrc file.
9140 * Return FAIL on error, OK otherwise.
9141 */
9142 static int
9143makeopens(fd, dirnow)
9144 FILE *fd;
9145 char_u *dirnow; /* Current directory name */
9146{
9147 buf_T *buf;
9148 int only_save_windows = TRUE;
9149 int nr;
9150 int cnr = 1;
9151 int restore_size = TRUE;
9152 win_T *wp;
9153 char_u *sname;
9154 win_T *edited_win = NULL;
9155
9156 if (ssop_flags & SSOP_BUFFERS)
9157 only_save_windows = FALSE; /* Save ALL buffers */
9158
9159 /*
9160 * Begin by setting the this_session variable, and then other
9161 * sessionable variables.
9162 */
9163#ifdef FEAT_EVAL
9164 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9165 return FAIL;
9166 if (ssop_flags & SSOP_GLOBALS)
9167 if (store_session_globals(fd) == FAIL)
9168 return FAIL;
9169#endif
9170
9171 /*
9172 * Close all windows but one.
9173 */
9174 if (put_line(fd, "silent only") == FAIL)
9175 return FAIL;
9176
9177 /*
9178 * Now a :cd command to the session directory or the current directory
9179 */
9180 if (ssop_flags & SSOP_SESDIR)
9181 {
9182 if (put_line(fd, "exe \"cd \" . expand(\"<sfile>:p:h\")") == FAIL)
9183 return FAIL;
9184 }
9185 else if (ssop_flags & SSOP_CURDIR)
9186 {
9187 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9188 if (sname == NULL
9189 || fprintf(fd, "cd %s", sname) < 0 || put_eol(fd) == FAIL)
9190 return FAIL;
9191 vim_free(sname);
9192 }
9193
9194 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009195 * If there is an empty, unnamed buffer we will wipe it out later.
9196 * Remember the buffer number.
9197 */
9198 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9199 return FAIL;
9200 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9201 return FAIL;
9202 if (put_line(fd, "endif") == FAIL)
9203 return FAIL;
9204
9205 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 * Now save the current files, current buffer first.
9207 */
9208 if (put_line(fd, "set shortmess=aoO") == FAIL)
9209 return FAIL;
9210
9211 /* Now put the other buffers into the buffer list */
9212 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9213 {
9214 if (!(only_save_windows && buf->b_nwindows == 0)
9215 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9216 && buf->b_fname != NULL
9217 && buf->b_p_bl)
9218 {
9219 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9220 : buf->b_wininfo->wi_fpos.lnum) < 0
9221 || ses_fname(fd, buf, &ssop_flags) == FAIL)
9222 return FAIL;
9223 }
9224 }
9225
9226 /* the global argument list */
9227 if (ses_arglist(fd, "args", &global_alist.al_ga,
9228 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9229 return FAIL;
9230
9231 if (ssop_flags & SSOP_RESIZE)
9232 {
9233 /* Note: after the restore we still check it worked!*/
9234 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9235 || put_eol(fd) == FAIL)
9236 return FAIL;
9237 }
9238
9239#ifdef FEAT_GUI
9240 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9241 {
9242 int x, y;
9243
9244 if (gui_mch_get_winpos(&x, &y) == OK)
9245 {
9246 /* Note: after the restore we still check it worked!*/
9247 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9248 return FAIL;
9249 }
9250 }
9251#endif
9252
9253 /*
9254 * Before creating the window layout, try loading one file. If this is
9255 * aborted we don't end up with a number of useless windows.
9256 * This may have side effects! (e.g., compressed or network file).
9257 */
9258 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9259 {
9260 if (ses_do_win(wp)
9261 && wp->w_buffer->b_ffname != NULL
9262 && !wp->w_buffer->b_help
9263#ifdef FEAT_QUICKFIX
9264 && !bt_nofile(wp->w_buffer)
9265#endif
9266 )
9267 {
9268 if (fputs("edit ", fd) < 0
9269 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
9270 return FAIL;
9271 if (!wp->w_arg_idx_invalid)
9272 edited_win = wp;
9273 break;
9274 }
9275 }
9276
9277 /*
9278 * Save current window layout.
9279 */
9280 if (put_line(fd, "set splitbelow splitright") == FAIL)
9281 return FAIL;
9282 if (ses_win_rec(fd, topframe) == FAIL)
9283 return FAIL;
9284 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9285 return FAIL;
9286 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9287 return FAIL;
9288
9289 /*
9290 * Check if window sizes can be restored (no windows omitted).
9291 * Remember the window number of the current window after restoring.
9292 */
9293 nr = 0;
9294 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
9295 {
9296 if (ses_do_win(wp))
9297 ++nr;
9298 else
9299 restore_size = FALSE;
9300 if (curwin == wp)
9301 cnr = nr;
9302 }
9303
9304 /* Go to the first window. */
9305 if (put_line(fd, "wincmd t") == FAIL)
9306 return FAIL;
9307
9308 /*
9309 * If more than one window, see if sizes can be restored.
9310 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9311 * resized when moving between windows.
9312 * Do this before restoring the view, so that the topline and the cursor
9313 * can be set. This is done again below.
9314 */
9315 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
9316 return FAIL;
9317 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9318 return FAIL;
9319
9320 /*
9321 * Restore the view of the window (options, file, cursor, etc.).
9322 */
9323 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9324 {
9325 if (!ses_do_win(wp))
9326 continue;
9327 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL)
9328 return FAIL;
9329 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9330 return FAIL;
9331 }
9332
9333 /*
9334 * Restore cursor to the current window if it's not the first one.
9335 */
9336 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL))
9337 return FAIL;
9338
9339 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009340 * Wipe out an empty unnamed buffer we started in.
9341 */
9342 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
9343 return FAIL;
9344 if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL)
9345 return FAIL;
9346 if (put_line(fd, "endif") == FAIL)
9347 return FAIL;
9348 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9349 return FAIL;
9350
9351 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 * Restore window sizes again after jumping around in windows, because the
9353 * current window has a minimum size while others may not.
9354 */
9355 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL)
9356 return FAIL;
9357
9358 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9359 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9360 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9361 return FAIL;
9362
9363 /*
9364 * Lastly, execute the x.vim file if it exists.
9365 */
9366 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9367 || put_line(fd, "if file_readable(s:sx)") == FAIL
9368 || put_line(fd, " exe \"source \" . s:sx") == FAIL
9369 || put_line(fd, "endif") == FAIL)
9370 return FAIL;
9371
9372 return OK;
9373}
9374
9375 static int
9376ses_winsizes(fd, restore_size)
9377 FILE *fd;
9378 int restore_size;
9379{
9380 int n = 0;
9381 win_T *wp;
9382
9383 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9384 {
9385 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9386 {
9387 if (!ses_do_win(wp))
9388 continue;
9389 ++n;
9390
9391 /* restore height when not full height */
9392 if (wp->w_height + wp->w_status_height < topframe->fr_height
9393 && (fprintf(fd,
9394 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9395 n, (long)wp->w_height, Rows / 2, Rows) < 0
9396 || put_eol(fd) == FAIL))
9397 return FAIL;
9398
9399 /* restore width when not full width */
9400 if (wp->w_width < Columns && (fprintf(fd,
9401 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9402 n, (long)wp->w_width, Columns / 2, Columns) < 0
9403 || put_eol(fd) == FAIL))
9404 return FAIL;
9405 }
9406 }
9407 else
9408 {
9409 /* Just equalise window sizes */
9410 if (put_line(fd, "wincmd =") == FAIL)
9411 return FAIL;
9412 }
9413 return OK;
9414}
9415
9416/*
9417 * Write commands to "fd" to recursively create windows for frame "fr",
9418 * horizontally and vertically split.
9419 * After the commands the last window in the frame is the current window.
9420 * Returns FAIL when writing the commands to "fd" fails.
9421 */
9422 static int
9423ses_win_rec(fd, fr)
9424 FILE *fd;
9425 frame_T *fr;
9426{
9427 frame_T *frc;
9428 int count = 0;
9429
9430 if (fr->fr_layout != FR_LEAF)
9431 {
9432 /* Find first frame that's not skipped and then create a window for
9433 * each following one (first frame is already there). */
9434 frc = ses_skipframe(fr->fr_child);
9435 if (frc != NULL)
9436 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
9437 {
9438 /* Make window as big as possible so that we have lots of room
9439 * to split. */
9440 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
9441 || put_line(fd, fr->fr_layout == FR_COL
9442 ? "split" : "vsplit") == FAIL)
9443 return FAIL;
9444 ++count;
9445 }
9446
9447 /* Go back to the first window. */
9448 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
9449 ? "%dwincmd k" : "%dwincmd h", count) < 0
9450 || put_eol(fd) == FAIL))
9451 return FAIL;
9452
9453 /* Recursively create frames/windows in each window of this column or
9454 * row. */
9455 frc = ses_skipframe(fr->fr_child);
9456 while (frc != NULL)
9457 {
9458 ses_win_rec(fd, frc);
9459 frc = ses_skipframe(frc->fr_next);
9460 /* Go to next window. */
9461 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
9462 return FAIL;
9463 }
9464 }
9465 return OK;
9466}
9467
9468/*
9469 * Skip frames that don't contain windows we want to save in the Session.
9470 * Returns NULL when there none.
9471 */
9472 static frame_T *
9473ses_skipframe(fr)
9474 frame_T *fr;
9475{
9476 frame_T *frc;
9477
9478 for (frc = fr; frc != NULL; frc = frc->fr_next)
9479 if (ses_do_frame(frc))
9480 break;
9481 return frc;
9482}
9483
9484/*
9485 * Return TRUE if frame "fr" has a window somewhere that we want to save in
9486 * the Session.
9487 */
9488 static int
9489ses_do_frame(fr)
9490 frame_T *fr;
9491{
9492 frame_T *frc;
9493
9494 if (fr->fr_layout == FR_LEAF)
9495 return ses_do_win(fr->fr_win);
9496 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
9497 if (ses_do_frame(frc))
9498 return TRUE;
9499 return FALSE;
9500}
9501
9502/*
9503 * Return non-zero if window "wp" is to be stored in the Session.
9504 */
9505 static int
9506ses_do_win(wp)
9507 win_T *wp;
9508{
9509 if (wp->w_buffer->b_fname == NULL
9510#ifdef FEAT_QUICKFIX
9511 /* When 'buftype' is "nofile" can't restore the window contents. */
9512 || bt_nofile(wp->w_buffer)
9513#endif
9514 )
9515 return (ssop_flags & SSOP_BLANK);
9516 if (wp->w_buffer->b_help)
9517 return (ssop_flags & SSOP_HELP);
9518 return TRUE;
9519}
9520
9521/*
9522 * Write commands to "fd" to restore the view of a window.
9523 * Caller must make sure 'scrolloff' is zero.
9524 */
9525 static int
9526put_view(fd, wp, add_edit, flagp)
9527 FILE *fd;
9528 win_T *wp;
9529 int add_edit; /* add ":edit" command to view */
9530 unsigned *flagp; /* vop_flags or ssop_flags */
9531{
9532 win_T *save_curwin;
9533 int f;
9534 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009535 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536
9537 /* Always restore cursor position for ":mksession". For ":mkview" only
9538 * when 'viewoptions' contains "cursor". */
9539 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
9540
9541 /*
9542 * Local argument list.
9543 */
9544 if (wp->w_alist == &global_alist)
9545 {
9546 if (put_line(fd, "argglobal") == FAIL)
9547 return FAIL;
9548 }
9549 else
9550 {
9551 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
9552 flagp == &vop_flags
9553 || !(*flagp & SSOP_CURDIR)
9554 || wp->w_localdir != NULL, flagp) == FAIL)
9555 return FAIL;
9556 }
9557
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009558 /* Only when part of a session: restore the argument index. Some
9559 * arguments may have been deleted, check if the index is valid. */
9560 if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp)
9561 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 {
9563 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0
9564 || put_eol(fd) == FAIL)
9565 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009566 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 }
9568
9569 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00009570 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 {
9572 /*
9573 * Load the file.
9574 */
9575 if (wp->w_buffer->b_ffname != NULL
9576#ifdef FEAT_QUICKFIX
9577 && !bt_nofile(wp->w_buffer)
9578#endif
9579 )
9580 {
9581 /*
9582 * Editing a file in this buffer: use ":edit file".
9583 * This may have side effects! (e.g., compressed or network file).
9584 */
9585 if (fputs("edit ", fd) < 0
9586 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9587 return FAIL;
9588 }
9589 else
9590 {
9591 /* No file in this buffer, just make it empty. */
9592 if (put_line(fd, "enew") == FAIL)
9593 return FAIL;
9594#ifdef FEAT_QUICKFIX
9595 if (wp->w_buffer->b_ffname != NULL)
9596 {
9597 /* The buffer does have a name, but it's not a file name. */
9598 if (fputs("file ", fd) < 0
9599 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
9600 return FAIL;
9601 }
9602#endif
9603 do_cursor = FALSE;
9604 }
9605 }
9606
9607 /*
9608 * Local mappings and abbreviations.
9609 */
9610 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9611 && makemap(fd, wp->w_buffer) == FAIL)
9612 return FAIL;
9613
9614 /*
9615 * Local options. Need to go to the window temporarily.
9616 * Store only local values when using ":mkview" and when ":mksession" is
9617 * used and 'sessionoptions' doesn't include "options".
9618 * Some folding options are always stored when "folds" is included,
9619 * otherwise the folds would not be restored correctly.
9620 */
9621 save_curwin = curwin;
9622 curwin = wp;
9623 curbuf = curwin->w_buffer;
9624 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
9625 f = makeset(fd, OPT_LOCAL,
9626 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
9627#ifdef FEAT_FOLDING
9628 else if (*flagp & SSOP_FOLDS)
9629 f = makefoldset(fd);
9630#endif
9631 else
9632 f = OK;
9633 curwin = save_curwin;
9634 curbuf = curwin->w_buffer;
9635 if (f == FAIL)
9636 return FAIL;
9637
9638#ifdef FEAT_FOLDING
9639 /*
9640 * Save Folds when 'buftype' is empty and for help files.
9641 */
9642 if ((*flagp & SSOP_FOLDS)
9643 && wp->w_buffer->b_ffname != NULL
9644 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help))
9645 {
9646 if (put_folds(fd, wp) == FAIL)
9647 return FAIL;
9648 }
9649#endif
9650
9651 /*
9652 * Set the cursor after creating folds, since that moves the cursor.
9653 */
9654 if (do_cursor)
9655 {
9656
9657 /* Restore the cursor line in the file and relatively in the
9658 * window. Don't use "G", it changes the jumplist. */
9659 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
9660 (long)wp->w_cursor.lnum,
9661 (long)(wp->w_cursor.lnum - wp->w_topline),
9662 (long)wp->w_height / 2, (long)wp->w_height) < 0
9663 || put_eol(fd) == FAIL
9664 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
9665 || put_line(fd, "exe s:l") == FAIL
9666 || put_line(fd, "normal! zt") == FAIL
9667 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
9668 || put_eol(fd) == FAIL)
9669 return FAIL;
9670 /* Restore the cursor column and left offset when not wrapping. */
9671 if (wp->w_cursor.col == 0)
9672 {
9673 if (put_line(fd, "normal! 0") == FAIL)
9674 return FAIL;
9675 }
9676 else
9677 {
9678 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
9679 {
9680 if (fprintf(fd,
9681 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
9682 (long)wp->w_cursor.col,
9683 (long)(wp->w_cursor.col - wp->w_leftcol),
9684 (long)wp->w_width / 2, (long)wp->w_width) < 0
9685 || put_eol(fd) == FAIL
9686 || put_line(fd, "if s:c > 0") == FAIL
9687 || fprintf(fd,
9688 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
9689 (long)wp->w_cursor.col) < 0
9690 || put_eol(fd) == FAIL
9691 || put_line(fd, "else") == FAIL
9692 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
9693 || put_eol(fd) == FAIL
9694 || put_line(fd, "endif") == FAIL)
9695 return FAIL;
9696 }
9697 else
9698 {
9699 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
9700 || put_eol(fd) == FAIL)
9701 return FAIL;
9702 }
9703 }
9704 }
9705
9706 /*
9707 * Local directory.
9708 */
9709 if (wp->w_localdir != NULL)
9710 {
9711 if (fputs("lcd ", fd) < 0
9712 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
9713 || put_eol(fd) == FAIL)
9714 return FAIL;
9715 }
9716
9717 return OK;
9718}
9719
9720/*
9721 * Write an argument list to the session file.
9722 * Returns FAIL if writing fails.
9723 */
9724 static int
9725ses_arglist(fd, cmd, gap, fullname, flagp)
9726 FILE *fd;
9727 char *cmd;
9728 garray_T *gap;
9729 int fullname; /* TRUE: use full path name */
9730 unsigned *flagp;
9731{
9732 int i;
9733 char_u buf[MAXPATHL];
9734 char_u *s;
9735
9736 if (gap->ga_len == 0)
9737 return put_line(fd, "silent! argdel *");
9738 if (fputs(cmd, fd) < 0)
9739 return FAIL;
9740 for (i = 0; i < gap->ga_len; ++i)
9741 {
9742 /* NULL file names are skipped (only happens when out of memory). */
9743 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
9744 if (s != NULL)
9745 {
9746 if (fullname)
9747 {
9748 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
9749 s = buf;
9750 }
9751 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
9752 return FAIL;
9753 }
9754 }
9755 return put_eol(fd);
9756}
9757
9758/*
9759 * Write a buffer name to the session file.
9760 * Also ends the line.
9761 * Returns FAIL if writing fails.
9762 */
9763 static int
9764ses_fname(fd, buf, flagp)
9765 FILE *fd;
9766 buf_T *buf;
9767 unsigned *flagp;
9768{
9769 char_u *name;
9770
9771 /* Use the short file name if the current directory is known at the time
9772 * the session file will be sourced. Don't do this for ":mkview", we
9773 * don't know the current directory. */
9774 if (buf->b_sfname != NULL
9775 && flagp == &ssop_flags
9776 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)))
9777 name = buf->b_sfname;
9778 else
9779 name = buf->b_ffname;
9780 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
9781 return FAIL;
9782 return OK;
9783}
9784
9785/*
9786 * Write a file name to the session file.
9787 * Takes care of the "slash" option in 'sessionoptions' and escapes special
9788 * characters.
9789 * Returns FAIL if writing fails.
9790 */
9791 static int
9792ses_put_fname(fd, name, flagp)
9793 FILE *fd;
9794 char_u *name;
9795 unsigned *flagp;
9796{
9797 char_u *sname;
9798 int retval = OK;
9799 int c;
9800
9801 sname = home_replace_save(NULL, name);
9802 if (sname != NULL)
9803 name = sname;
9804 while (*name != NUL)
9805 {
9806#ifdef FEAT_MBYTE
9807 {
9808 int l;
9809
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009810 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 {
9812 /* copy a multibyte char */
9813 while (--l >= 0)
9814 {
9815 if (putc(*name, fd) != *name)
9816 retval = FAIL;
9817 ++name;
9818 }
9819 continue;
9820 }
9821 }
9822#endif
9823 c = *name++;
9824 if (c == '\\' && (*flagp & SSOP_SLASH))
9825 /* change a backslash to a forward slash */
9826 c = '/';
9827 else if ((vim_strchr(escape_chars, c) != NULL
9828#ifdef BACKSLASH_IN_FILENAME
9829 && c != '\\'
9830#endif
9831 ) || c == '#' || c == '%')
9832 {
9833 /* escape a special character with a backslash */
9834 if (putc('\\', fd) != '\\')
9835 retval = FAIL;
9836 }
9837 if (putc(c, fd) != c)
9838 retval = FAIL;
9839 }
9840 vim_free(sname);
9841 return retval;
9842}
9843
9844/*
9845 * ":loadview [nr]"
9846 */
9847 static void
9848ex_loadview(eap)
9849 exarg_T *eap;
9850{
9851 char_u *fname;
9852
9853 fname = get_view_file(*eap->arg);
9854 if (fname != NULL)
9855 {
9856 do_source(fname, FALSE, FALSE);
9857 vim_free(fname);
9858 }
9859}
9860
9861/*
9862 * Get the name of the view file for the current buffer.
9863 */
9864 static char_u *
9865get_view_file(c)
9866 int c;
9867{
9868 int len = 0;
9869 char_u *p, *s;
9870 char_u *retval;
9871 char_u *sname;
9872
9873 if (curbuf->b_ffname == NULL)
9874 {
9875 EMSG(_(e_noname));
9876 return NULL;
9877 }
9878 sname = home_replace_save(NULL, curbuf->b_ffname);
9879 if (sname == NULL)
9880 return NULL;
9881
9882 /*
9883 * We want a file name without separators, because we're not going to make
9884 * a directory.
9885 * "normal" path separator -> "=+"
9886 * "=" -> "=="
9887 * ":" path separator -> "=-"
9888 */
9889 for (p = sname; *p; ++p)
9890 if (*p == '=' || vim_ispathsep(*p))
9891 ++len;
9892 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
9893 if (retval != NULL)
9894 {
9895 STRCPY(retval, p_vdir);
9896 add_pathsep(retval);
9897 s = retval + STRLEN(retval);
9898 for (p = sname; *p; ++p)
9899 {
9900 if (*p == '=')
9901 {
9902 *s++ = '=';
9903 *s++ = '=';
9904 }
9905 else if (vim_ispathsep(*p))
9906 {
9907 *s++ = '=';
9908#ifdef MACOS_CLASSIC /* TODO: Is it also needed for MACOS_X? (Dany) */
9909 *s++ = '+';
9910#else
9911# if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
9912 || defined(VMS)
9913 if (*p == ':')
9914 *s++ = '-';
9915 else
9916# endif
9917 *s++ = '+';
9918#endif
9919 }
9920 else
9921 *s++ = *p;
9922 }
9923 *s++ = '=';
9924 *s++ = c;
9925 STRCPY(s, ".vim");
9926 }
9927
9928 vim_free(sname);
9929 return retval;
9930}
9931
9932#endif /* FEAT_SESSION */
9933
9934/*
9935 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
9936 * Return FAIL for a write error.
9937 */
9938 int
9939put_eol(fd)
9940 FILE *fd;
9941{
9942 if (
9943#ifdef USE_CRNL
9944 (
9945# ifdef MKSESSION_NL
9946 !mksession_nl &&
9947# endif
9948 (putc('\r', fd) < 0)) ||
9949#endif
9950 (putc('\n', fd) < 0))
9951 return FAIL;
9952 return OK;
9953}
9954
9955/*
9956 * Write a line to "fd".
9957 * Return FAIL for a write error.
9958 */
9959 int
9960put_line(fd, s)
9961 FILE *fd;
9962 char *s;
9963{
9964 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
9965 return FAIL;
9966 return OK;
9967}
9968
9969#ifdef FEAT_VIMINFO
9970/*
9971 * ":rviminfo" and ":wviminfo".
9972 */
9973 static void
9974ex_viminfo(eap)
9975 exarg_T *eap;
9976{
9977 char_u *save_viminfo;
9978
9979 save_viminfo = p_viminfo;
9980 if (*p_viminfo == NUL)
9981 p_viminfo = (char_u *)"'100";
9982 if (eap->cmdidx == CMD_rviminfo)
9983 {
9984 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL)
9985 EMSG(_("E195: Cannot open viminfo file for reading"));
9986 }
9987 else
9988 write_viminfo(eap->arg, eap->forceit);
9989 p_viminfo = save_viminfo;
9990}
9991#endif
9992
9993#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009994/*
9995 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009996 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 void
9999dialog_msg(buff, format, fname)
10000 char_u *buff;
10001 char *format;
10002 char_u *fname;
10003{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 if (fname == NULL)
10005 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010006 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007}
10008#endif
10009
10010/*
10011 * ":behave {mswin,xterm}"
10012 */
10013 static void
10014ex_behave(eap)
10015 exarg_T *eap;
10016{
10017 if (STRCMP(eap->arg, "mswin") == 0)
10018 {
10019 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10020 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10021 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10022 set_option_value((char_u *)"keymodel", 0L,
10023 (char_u *)"startsel,stopsel", 0);
10024 }
10025 else if (STRCMP(eap->arg, "xterm") == 0)
10026 {
10027 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10028 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10029 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10030 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10031 }
10032 else
10033 EMSG2(_(e_invarg2), eap->arg);
10034}
10035
10036#ifdef FEAT_AUTOCMD
10037static int filetype_detect = FALSE;
10038static int filetype_plugin = FALSE;
10039static int filetype_indent = FALSE;
10040
10041/*
10042 * ":filetype [plugin] [indent] {on,off,detect}"
10043 * on: Load the filetype.vim file to install autocommands for file types.
10044 * off: Load the ftoff.vim file to remove all autocommands for file types.
10045 * plugin on: load filetype.vim and ftplugin.vim
10046 * plugin off: load ftplugof.vim
10047 * indent on: load filetype.vim and indent.vim
10048 * indent off: load indoff.vim
10049 */
10050 static void
10051ex_filetype(eap)
10052 exarg_T *eap;
10053{
10054 char_u *arg = eap->arg;
10055 int plugin = FALSE;
10056 int indent = FALSE;
10057
10058 if (*eap->arg == NUL)
10059 {
10060 /* Print current status. */
10061 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10062 filetype_detect ? "ON" : "OFF",
10063 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10064 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10065 return;
10066 }
10067
10068 /* Accept "plugin" and "indent" in any order. */
10069 for (;;)
10070 {
10071 if (STRNCMP(arg, "plugin", 6) == 0)
10072 {
10073 plugin = TRUE;
10074 arg = skipwhite(arg + 6);
10075 continue;
10076 }
10077 if (STRNCMP(arg, "indent", 6) == 0)
10078 {
10079 indent = TRUE;
10080 arg = skipwhite(arg + 6);
10081 continue;
10082 }
10083 break;
10084 }
10085 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10086 {
10087 if (*arg == 'o' || !filetype_detect)
10088 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010089 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 filetype_detect = TRUE;
10091 if (plugin)
10092 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010093 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 filetype_plugin = TRUE;
10095 }
10096 if (indent)
10097 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010098 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 filetype_indent = TRUE;
10100 }
10101 }
10102 if (*arg == 'd')
10103 {
10104 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +000010105 do_modelines(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 }
10107 }
10108 else if (STRCMP(arg, "off") == 0)
10109 {
10110 if (plugin || indent)
10111 {
10112 if (plugin)
10113 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010114 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 filetype_plugin = FALSE;
10116 }
10117 if (indent)
10118 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010119 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 filetype_indent = FALSE;
10121 }
10122 }
10123 else
10124 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000010125 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 filetype_detect = FALSE;
10127 }
10128 }
10129 else
10130 EMSG2(_(e_invarg2), arg);
10131}
10132
10133/*
10134 * ":setfiletype {name}"
10135 */
10136 static void
10137ex_setfiletype(eap)
10138 exarg_T *eap;
10139{
10140 if (!did_filetype)
10141 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
10142}
10143#endif
10144
10145/*ARGSUSED*/
10146 static void
10147ex_digraphs(eap)
10148 exarg_T *eap;
10149{
10150#ifdef FEAT_DIGRAPHS
10151 if (*eap->arg != NUL)
10152 putdigraph(eap->arg);
10153 else
10154 listdigraphs();
10155#else
10156 EMSG(_("E196: No digraphs in this version"));
10157#endif
10158}
10159
10160 static void
10161ex_set(eap)
10162 exarg_T *eap;
10163{
10164 int flags = 0;
10165
10166 if (eap->cmdidx == CMD_setlocal)
10167 flags = OPT_LOCAL;
10168 else if (eap->cmdidx == CMD_setglobal)
10169 flags = OPT_GLOBAL;
10170#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
10171 if (cmdmod.browse && flags == 0)
10172 ex_options(eap);
10173 else
10174#endif
10175 (void)do_set(eap->arg, flags);
10176}
10177
10178#ifdef FEAT_SEARCH_EXTRA
10179/*
10180 * ":nohlsearch"
10181 */
10182/*ARGSUSED*/
10183 static void
10184ex_nohlsearch(eap)
10185 exarg_T *eap;
10186{
10187 no_hlsearch = TRUE;
10188 redraw_all_later(NOT_VALID);
10189}
10190
10191/*
10192 * ":match {group} {pattern}"
10193 * Sets nextcmd to the start of the next command, if any. Also called when
10194 * skipping commands to find the next command.
10195 */
10196 static void
10197ex_match(eap)
10198 exarg_T *eap;
10199{
10200 char_u *p;
10201 char_u *end;
10202 int c;
10203
10204 /* First clear any old pattern. */
10205 if (!eap->skip)
10206 {
10207 vim_free(curwin->w_match.regprog);
10208 curwin->w_match.regprog = NULL;
10209 redraw_later(NOT_VALID); /* always need a redraw */
10210 }
10211
10212 if (ends_excmd(*eap->arg))
10213 end = eap->arg;
10214 else if ((STRNICMP(eap->arg, "none", 4) == 0
10215 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
10216 end = eap->arg + 4;
10217 else
10218 {
10219 p = skiptowhite(eap->arg);
10220 if (!eap->skip)
10221 {
10222 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg));
10223 if (curwin->w_match_id == 0)
10224 {
10225 EMSG2(_(e_nogroup), eap->arg);
10226 return;
10227 }
10228 }
10229 p = skipwhite(p);
10230 if (*p == NUL)
10231 {
10232 /* There must be two arguments. */
10233 EMSG2(_(e_invarg2), eap->arg);
10234 return;
10235 }
10236 end = skip_regexp(p + 1, *p, TRUE, NULL);
10237 if (!eap->skip)
10238 {
10239 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10240 {
10241 eap->errmsg = e_trailing;
10242 return;
10243 }
10244
10245 c = *end;
10246 *end = NUL;
10247 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC);
10248 *end = c;
10249 if (curwin->w_match.regprog == NULL)
10250 {
10251 EMSG2(_(e_invarg2), p);
10252 return;
10253 }
10254 }
10255 }
10256 eap->nextcmd = find_nextcmd(end);
10257}
10258#endif
10259
10260#ifdef FEAT_CRYPT
10261/*
10262 * ":X": Get crypt key
10263 */
10264/*ARGSUSED*/
10265 static void
10266ex_X(eap)
10267 exarg_T *eap;
10268{
10269 (void)get_crypt_key(TRUE, TRUE);
10270}
10271#endif
10272
10273#ifdef FEAT_FOLDING
10274 static void
10275ex_fold(eap)
10276 exarg_T *eap;
10277{
10278 if (foldManualAllowed(TRUE))
10279 foldCreate(eap->line1, eap->line2);
10280}
10281
10282 static void
10283ex_foldopen(eap)
10284 exarg_T *eap;
10285{
10286 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10287 eap->forceit, FALSE);
10288}
10289
10290 static void
10291ex_folddo(eap)
10292 exarg_T *eap;
10293{
10294 linenr_T lnum;
10295
10296 /* First set the marks for all lines closed/open. */
10297 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10298 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10299 ml_setmarked(lnum);
10300
10301 /* Execute the command on the marked lines. */
10302 global_exe(eap->arg);
10303 ml_clearmarked(); /* clear rest of the marks */
10304}
10305#endif