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