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