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