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