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