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