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