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