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