forked from vim/vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_docmd.c
9838 lines (9054 loc) · 230 KB
/
ex_docmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* ex_docmd.c: functions for executing an Ex command line.
*/
#include "vim.h"
static int quitmore = 0;
static int ex_pressedreturn = FALSE;
#ifndef FEAT_PRINTER
# define ex_hardcopy ex_ni
#endif
#ifdef FEAT_EVAL
static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
#else
static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
static int if_level = 0; // depth in :if
#endif
static void append_command(char_u *cmd);
#ifndef FEAT_MENU
# define ex_emenu ex_ni
# define ex_menu ex_ni
# define ex_menutranslate ex_ni
#endif
static void ex_autocmd(exarg_T *eap);
static void ex_doautocmd(exarg_T *eap);
static void ex_bunload(exarg_T *eap);
static void ex_buffer(exarg_T *eap);
static void ex_bmodified(exarg_T *eap);
static void ex_bnext(exarg_T *eap);
static void ex_bprevious(exarg_T *eap);
static void ex_brewind(exarg_T *eap);
static void ex_blast(exarg_T *eap);
static char_u *getargcmd(char_u **);
static int getargopt(exarg_T *eap);
#ifndef FEAT_QUICKFIX
# define ex_make ex_ni
# define ex_cbuffer ex_ni
# define ex_cc ex_ni
# define ex_cnext ex_ni
# define ex_cbelow ex_ni
# define ex_cfile ex_ni
# define qf_list ex_ni
# define qf_age ex_ni
# define qf_history ex_ni
# define ex_helpgrep ex_ni
# define ex_vimgrep ex_ni
# define ex_cclose ex_ni
# define ex_copen ex_ni
# define ex_cwindow ex_ni
# define ex_cbottom ex_ni
#endif
#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
# define ex_cexpr ex_ni
#endif
static linenr_T default_address(exarg_T *eap);
static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
static void address_default_all(exarg_T *eap);
static void get_flags(exarg_T *eap);
#if !defined(FEAT_PERL) \
|| !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
|| !defined(FEAT_TCL) \
|| !defined(FEAT_RUBY) \
|| !defined(FEAT_LUA) \
|| !defined(FEAT_MZSCHEME)
# define HAVE_EX_SCRIPT_NI
static void ex_script_ni(exarg_T *eap);
#endif
static char *invalid_range(exarg_T *eap);
static void correct_range(exarg_T *eap);
#ifdef FEAT_QUICKFIX
static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
#endif
static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
static void ex_highlight(exarg_T *eap);
static void ex_colorscheme(exarg_T *eap);
static void ex_cquit(exarg_T *eap);
static void ex_quit_all(exarg_T *eap);
static void ex_close(exarg_T *eap);
static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
static void ex_only(exarg_T *eap);
static void ex_resize(exarg_T *eap);
static void ex_stag(exarg_T *eap);
static void ex_tabclose(exarg_T *eap);
static void ex_tabonly(exarg_T *eap);
static void ex_tabnext(exarg_T *eap);
static void ex_tabmove(exarg_T *eap);
static void ex_tabs(exarg_T *eap);
#if defined(FEAT_QUICKFIX)
static void ex_pclose(exarg_T *eap);
static void ex_ptag(exarg_T *eap);
static void ex_pedit(exarg_T *eap);
#else
# define ex_pclose ex_ni
# define ex_ptag ex_ni
# define ex_pedit ex_ni
#endif
static void ex_hide(exarg_T *eap);
static void ex_exit(exarg_T *eap);
static void ex_print(exarg_T *eap);
#ifdef FEAT_BYTEOFF
static void ex_goto(exarg_T *eap);
#else
# define ex_goto ex_ni
#endif
static void ex_shell(exarg_T *eap);
static void ex_preserve(exarg_T *eap);
static void ex_recover(exarg_T *eap);
static void ex_mode(exarg_T *eap);
static void ex_wrongmodifier(exarg_T *eap);
static void ex_find(exarg_T *eap);
static void ex_open(exarg_T *eap);
static void ex_edit(exarg_T *eap);
#ifndef FEAT_GUI
# define ex_gui ex_nogui
static void ex_nogui(exarg_T *eap);
#endif
#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
static void ex_tearoff(exarg_T *eap);
#else
# define ex_tearoff ex_ni
#endif
#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
|| defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
static void ex_popup(exarg_T *eap);
#else
# define ex_popup ex_ni
#endif
#ifndef FEAT_GUI_MSWIN
# define ex_simalt ex_ni
#endif
#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
# define gui_mch_find_dialog ex_ni
# define gui_mch_replace_dialog ex_ni
#endif
#if !defined(FEAT_GUI_GTK)
# define ex_helpfind ex_ni
#endif
#ifndef FEAT_CSCOPE
# define ex_cscope ex_ni
# define ex_scscope ex_ni
# define ex_cstag ex_ni
#endif
#ifndef FEAT_SYN_HL
# define ex_syntax ex_ni
# define ex_ownsyntax ex_ni
#endif
#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
# define ex_syntime ex_ni
#endif
#ifndef FEAT_SPELL
# define ex_spell ex_ni
# define ex_mkspell ex_ni
# define ex_spelldump ex_ni
# define ex_spellinfo ex_ni
# define ex_spellrepall ex_ni
#endif
#ifndef FEAT_PERSISTENT_UNDO
# define ex_rundo ex_ni
# define ex_wundo ex_ni
#endif
#ifndef FEAT_LUA
# define ex_lua ex_script_ni
# define ex_luado ex_ni
# define ex_luafile ex_ni
#endif
#ifndef FEAT_MZSCHEME
# define ex_mzscheme ex_script_ni
# define ex_mzfile ex_ni
#endif
#ifndef FEAT_PERL
# define ex_perl ex_script_ni
# define ex_perldo ex_ni
#endif
#ifndef FEAT_PYTHON
# define ex_python ex_script_ni
# define ex_pydo ex_ni
# define ex_pyfile ex_ni
#endif
#ifndef FEAT_PYTHON3
# define ex_py3 ex_script_ni
# define ex_py3do ex_ni
# define ex_py3file ex_ni
#endif
#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
# define ex_pyx ex_script_ni
# define ex_pyxdo ex_ni
# define ex_pyxfile ex_ni
#endif
#ifndef FEAT_TCL
# define ex_tcl ex_script_ni
# define ex_tcldo ex_ni
# define ex_tclfile ex_ni
#endif
#ifndef FEAT_RUBY
# define ex_ruby ex_script_ni
# define ex_rubydo ex_ni
# define ex_rubyfile ex_ni
#endif
#ifndef FEAT_KEYMAP
# define ex_loadkeymap ex_ni
#endif
static void ex_swapname(exarg_T *eap);
static void ex_syncbind(exarg_T *eap);
static void ex_read(exarg_T *eap);
static void ex_pwd(exarg_T *eap);
static void ex_equal(exarg_T *eap);
static void ex_sleep(exarg_T *eap);
static void ex_winsize(exarg_T *eap);
static void ex_wincmd(exarg_T *eap);
#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
static void ex_winpos(exarg_T *eap);
#else
# define ex_winpos ex_ni
#endif
static void ex_operators(exarg_T *eap);
static void ex_put(exarg_T *eap);
static void ex_copymove(exarg_T *eap);
static void ex_submagic(exarg_T *eap);
static void ex_join(exarg_T *eap);
static void ex_at(exarg_T *eap);
static void ex_bang(exarg_T *eap);
static void ex_undo(exarg_T *eap);
#ifdef FEAT_PERSISTENT_UNDO
static void ex_wundo(exarg_T *eap);
static void ex_rundo(exarg_T *eap);
#endif
static void ex_redo(exarg_T *eap);
static void ex_later(exarg_T *eap);
static void ex_redir(exarg_T *eap);
static void ex_redrawstatus(exarg_T *eap);
static void ex_redrawtabline(exarg_T *eap);
static void close_redir(void);
static void ex_mark(exarg_T *eap);
static void ex_startinsert(exarg_T *eap);
static void ex_stopinsert(exarg_T *eap);
#ifdef FEAT_FIND_ID
static void ex_checkpath(exarg_T *eap);
static void ex_findpat(exarg_T *eap);
#else
# define ex_findpat ex_ni
# define ex_checkpath ex_ni
#endif
#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
static void ex_psearch(exarg_T *eap);
#else
# define ex_psearch ex_ni
#endif
static void ex_tag(exarg_T *eap);
static void ex_tag_cmd(exarg_T *eap, char_u *name);
#ifndef FEAT_EVAL
# define ex_block ex_ni
# define ex_break ex_ni
# define ex_breakadd ex_ni
# define ex_breakdel ex_ni
# define ex_breaklist ex_ni
# define ex_call ex_ni
# define ex_catch ex_ni
# define ex_class ex_ni
# define ex_compiler ex_ni
# define ex_continue ex_ni
# define ex_debug ex_ni
# define ex_debuggreedy ex_ni
# define ex_defcompile ex_ni
# define ex_delfunction ex_ni
# define ex_disassemble ex_ni
# define ex_echo ex_ni
# define ex_echohl ex_ni
# define ex_else ex_ni
# define ex_endblock ex_ni
# define ex_endfunction ex_ni
# define ex_endif ex_ni
# define ex_endtry ex_ni
# define ex_endwhile ex_ni
# define ex_enum ex_ni
# define ex_eval ex_ni
# define ex_execute ex_ni
# define ex_finally ex_ni
# define ex_incdec ex_ni
# define ex_finish ex_ni
# define ex_function ex_ni
# define ex_if ex_ni
# define ex_let ex_ni
# define ex_var ex_ni
# define ex_lockvar ex_ni
# define ex_oldfiles ex_ni
# define ex_options ex_ni
# define ex_packadd ex_ni
# define ex_packloadall ex_ni
# define ex_return ex_ni
# define ex_scriptnames ex_ni
# define ex_throw ex_ni
# define ex_try ex_ni
# define ex_type ex_ni
# define ex_unlet ex_ni
# define ex_while ex_ni
# define ex_import ex_ni
# define ex_export ex_ni
#endif
#ifndef FEAT_SESSION
# define ex_loadview ex_ni
#endif
#ifndef FEAT_VIMINFO
# define ex_viminfo ex_ni
#endif
static void ex_behave(exarg_T *eap);
static void ex_filetype(exarg_T *eap);
static void ex_setfiletype(exarg_T *eap);
#ifndef FEAT_DIFF
# define ex_diffoff ex_ni
# define ex_diffpatch ex_ni
# define ex_diffgetput ex_ni
# define ex_diffsplit ex_ni
# define ex_diffthis ex_ni
# define ex_diffupdate ex_ni
#endif
static void ex_digraphs(exarg_T *eap);
#ifdef FEAT_SEARCH_EXTRA
static void ex_nohlsearch(exarg_T *eap);
#else
# define ex_nohlsearch ex_ni
# define ex_match ex_ni
#endif
#ifdef FEAT_CRYPT
static void ex_X(exarg_T *eap);
#else
# define ex_X ex_ni
#endif
#ifdef FEAT_FOLDING
static void ex_fold(exarg_T *eap);
static void ex_foldopen(exarg_T *eap);
static void ex_folddo(exarg_T *eap);
#else
# define ex_fold ex_ni
# define ex_foldopen ex_ni
# define ex_folddo ex_ni
#endif
#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
# define ex_language ex_ni
#endif
#ifndef FEAT_SIGNS
# define ex_sign ex_ni
#endif
#ifndef FEAT_NETBEANS_INTG
# define ex_nbclose ex_ni
# define ex_nbkey ex_ni
# define ex_nbstart ex_ni
#endif
#ifndef FEAT_PROFILE
# define ex_profile ex_ni
#endif
#ifndef FEAT_TERMINAL
# define ex_terminal ex_ni
#endif
#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
# define ex_xrestore ex_ni
#endif
#if !defined(FEAT_PROP_POPUP)
# define ex_popupclear ex_ni
#endif
/*
* Declare cmdnames[].
*/
#define DO_DECLARE_EXCMD
#include "ex_cmds.h"
#include "ex_cmdidxs.h"
static char_u dollar_command[2] = {'$', 0};
#ifdef FEAT_EVAL
// Struct for storing a line inside a while/for loop
typedef struct
{
char_u *line; // command line
linenr_T lnum; // sourcing_lnum of the line
} wcmd_T;
/*
* Structure used to store info for line position in a while or for loop.
* This is required, because do_one_cmd() may invoke ex_function(), which
* reads more lines that may come from the while/for loop.
*/
struct loop_cookie
{
garray_T *lines_gap; // growarray with line info
int current_line; // last read line from growarray
int repeating; // TRUE when looping a second time
// When "repeating" is FALSE use "getline" and "cookie" to get lines
char_u *(*getline)(int, void *, int, getline_opt_T);
void *cookie;
};
static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
static int store_loop_line(garray_T *gap, char_u *line);
static void free_cmdlines(garray_T *gap);
// Struct to save a few things while debugging. Used in do_cmdline() only.
struct dbg_stuff
{
int trylevel;
int force_abort;
except_T *caught_stack;
char_u *vv_exception;
char_u *vv_throwpoint;
int did_emsg;
int got_int;
int did_throw;
int need_rethrow;
int check_cstack;
except_T *current_exception;
};
static void
save_dbg_stuff(struct dbg_stuff *dsp)
{
dsp->trylevel = trylevel; trylevel = 0;
dsp->force_abort = force_abort; force_abort = FALSE;
dsp->caught_stack = caught_stack; caught_stack = NULL;
dsp->vv_exception = v_exception(NULL);
dsp->vv_throwpoint = v_throwpoint(NULL);
// Necessary for debugging an inactive ":catch", ":finally", ":endtry"
dsp->did_emsg = did_emsg; did_emsg = FALSE;
dsp->got_int = got_int; got_int = FALSE;
dsp->did_throw = did_throw; did_throw = FALSE;
dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
dsp->check_cstack = check_cstack; check_cstack = FALSE;
dsp->current_exception = current_exception; current_exception = NULL;
}
static void
restore_dbg_stuff(struct dbg_stuff *dsp)
{
suppress_errthrow = FALSE;
trylevel = dsp->trylevel;
force_abort = dsp->force_abort;
caught_stack = dsp->caught_stack;
(void)v_exception(dsp->vv_exception);
(void)v_throwpoint(dsp->vv_throwpoint);
did_emsg = dsp->did_emsg;
got_int = dsp->got_int;
did_throw = dsp->did_throw;
need_rethrow = dsp->need_rethrow;
check_cstack = dsp->check_cstack;
current_exception = dsp->current_exception;
}
#endif
/*
* do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
* command is given.
*/
void
do_exmode(
int improved) // TRUE for "improved Ex" mode
{
int save_msg_scroll;
int prev_msg_row;
linenr_T prev_line;
varnumber_T changedtick;
if (improved)
exmode_active = EXMODE_VIM;
else
exmode_active = EXMODE_NORMAL;
State = MODE_NORMAL;
may_trigger_modechanged();
// When using ":global /pat/ visual" and then "Q" we return to continue
// the :global command.
if (global_busy)
return;
save_msg_scroll = msg_scroll;
++RedrawingDisabled; // don't redisplay the window
++no_wait_return; // don't wait for return
#ifdef FEAT_GUI
// Ignore scrollbar and mouse events in Ex mode
++hold_gui_events;
#endif
msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
while (exmode_active)
{
// Check for a ":normal" command and no more characters left.
if (ex_normal_busy > 0 && typebuf.tb_len == 0)
{
exmode_active = FALSE;
break;
}
msg_scroll = TRUE;
need_wait_return = FALSE;
ex_pressedreturn = FALSE;
ex_no_reprint = FALSE;
changedtick = CHANGEDTICK(curbuf);
prev_msg_row = msg_row;
prev_line = curwin->w_cursor.lnum;
if (improved)
{
cmdline_row = msg_row;
do_cmdline(NULL, getexline, NULL, 0);
}
else
do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
lines_left = Rows - 1;
if ((prev_line != curwin->w_cursor.lnum
|| changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
{
if (curbuf->b_ml.ml_flags & ML_EMPTY)
emsg(_(e_empty_buffer));
else
{
if (ex_pressedreturn)
{
// go up one line, to overwrite the ":<CR>" line, so the
// output doesn't contain empty lines.
msg_row = prev_msg_row;
if (prev_msg_row == Rows - 1)
msg_row--;
}
msg_col = 0;
print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
msg_clr_eos();
}
}
else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
{
if (curbuf->b_ml.ml_flags & ML_EMPTY)
emsg(_(e_empty_buffer));
else
emsg(_(e_at_end_of_file));
}
}
#ifdef FEAT_GUI
--hold_gui_events;
#endif
--RedrawingDisabled;
--no_wait_return;
update_screen(UPD_CLEAR);
need_wait_return = FALSE;
msg_scroll = save_msg_scroll;
}
/*
* Print the executed command for when 'verbose' is set.
* When "lnum" is 0 only print the command.
*/
static void
msg_verbose_cmd(linenr_T lnum, char_u *cmd)
{
++no_wait_return;
verbose_enter_scroll();
if (lnum == 0)
smsg(_("Executing: %s"), cmd);
else
smsg(_("line %ld: %s"), (long)lnum, cmd);
if (msg_silent == 0)
msg_puts("\n"); // don't overwrite this
verbose_leave_scroll();
--no_wait_return;
}
/*
* Execute a simple command line. Used for translated commands like "*".
*/
int
do_cmdline_cmd(char_u *cmd)
{
return do_cmdline(cmd, NULL, NULL,
DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
}
/*
* Execute the "+cmd" argument of "edit +cmd fname" and the like.
* This allows for using a range without ":" in Vim9 script.
*/
static int
do_cmd_argument(char_u *cmd)
{
return do_cmdline(cmd, NULL, NULL,
DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
}
/*
* do_cmdline(): execute one Ex command line
*
* 1. Execute "cmdline" when it is not NULL.
* If "cmdline" is NULL, or more lines are needed, fgetline() is used.
* 2. Split up in parts separated with '|'.
*
* This function can be called recursively!
*
* flags:
* DOCMD_VERBOSE - The command will be included in the error message.
* DOCMD_NOWAIT - Don't call wait_return() and friends.
* DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
* DOCMD_KEYTYPED - Don't reset KeyTyped.
* DOCMD_EXCRESET - Reset the exception environment (used for debugging).
* DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
*
* return FAIL if cmdline could not be executed, OK otherwise
*/
int
do_cmdline(
char_u *cmdline,
char_u *(*fgetline)(int, void *, int, getline_opt_T),
void *cookie, // argument for fgetline()
int flags)
{
char_u *next_cmdline; // next cmd to execute
char_u *cmdline_copy = NULL; // copy of cmd line
int used_getline = FALSE; // used "fgetline" to obtain command
static int recursive = 0; // recursive depth
int msg_didout_before_start = 0;
int count = 0; // line number count
int did_inc = FALSE; // incremented RedrawingDisabled
int retval = OK;
#ifdef FEAT_EVAL
cstack_T cstack; // conditional stack
garray_T lines_ga; // keep lines for ":while"/":for"
int current_line = 0; // active line in lines_ga
int current_line_before = 0;
char_u *fname = NULL; // function or script name
linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
struct dbg_stuff debug_saved; // saved things for debug mode
int initial_trylevel;
msglist_T **saved_msg_list = NULL;
msglist_T *private_msg_list = NULL;
// "fgetline" and "cookie" passed to do_one_cmd()
char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
void *cmd_cookie;
struct loop_cookie cmd_loop_cookie;
void *real_cookie;
int getline_is_func;
#else
# define cmd_getline fgetline
# define cmd_cookie cookie
#endif
static int call_depth = 0; // recursiveness
#ifdef FEAT_EVAL
// For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
// location for storing error messages to be converted to an exception.
// This ensures that the do_errthrow() call in do_one_cmd() does not
// combine the messages stored by an earlier invocation of do_one_cmd()
// with the command name of the later one. This would happen when
// BufWritePost autocommands are executed after a write error.
saved_msg_list = msg_list;
msg_list = &private_msg_list;
#endif
// It's possible to create an endless loop with ":execute", catch that
// here. The value of 200 allows nested function calls, ":source", etc.
// Allow 200 or 'maxfuncdepth', whatever is larger.
if (call_depth >= 200
#ifdef FEAT_EVAL
&& call_depth >= p_mfd
#endif
)
{
emsg(_(e_command_too_recursive));
#ifdef FEAT_EVAL
// When converting to an exception, we do not include the command name
// since this is not an error of the specific command.
do_errthrow((cstack_T *)NULL, (char_u *)NULL);
msg_list = saved_msg_list;
#endif
return FAIL;
}
++call_depth;
#ifdef FEAT_EVAL
CLEAR_FIELD(cstack);
cstack.cs_idx = -1;
ga_init2(&lines_ga, sizeof(wcmd_T), 10);
real_cookie = getline_cookie(fgetline, cookie);
// Inside a function use a higher nesting level.
getline_is_func = getline_equal(fgetline, cookie, get_func_line);
if (getline_is_func && ex_nesting_level == func_level(real_cookie))
++ex_nesting_level;
// Get the function or script name and the address where the next breakpoint
// line and the debug tick for a function or script are stored.
if (getline_is_func)
{
fname = func_name(real_cookie);
breakpoint = func_breakpoint(real_cookie);
dbg_tick = func_dbg_tick(real_cookie);
}
else if (getline_equal(fgetline, cookie, getsourceline))
{
fname = SOURCING_NAME;
breakpoint = source_breakpoint(real_cookie);
dbg_tick = source_dbg_tick(real_cookie);
}
/*
* Initialize "force_abort" and "suppress_errthrow" at the top level.
*/
if (!recursive)
{
force_abort = FALSE;
suppress_errthrow = FALSE;
}
/*
* If requested, store and reset the global values controlling the
* exception handling (used when debugging). Otherwise clear it to avoid
* a bogus compiler warning when the optimizer uses inline functions...
*/
if (flags & DOCMD_EXCRESET)
save_dbg_stuff(&debug_saved);
else
CLEAR_FIELD(debug_saved);
initial_trylevel = trylevel;
/*
* "did_throw" will be set to TRUE when an exception is being thrown.
*/
did_throw = FALSE;
#endif
/*
* "did_emsg" will be set to TRUE when emsg() is used, in which case we
* cancel the whole command line, and any if/endif or loop.
* If force_abort is set, we cancel everything.
*/
#ifdef FEAT_EVAL
did_emsg_cumul += did_emsg;
#endif
did_emsg = FALSE;
/*
* KeyTyped is only set when calling vgetc(). Reset it here when not
* calling vgetc() (sourced command lines).
*/
if (!(flags & DOCMD_KEYTYPED)
&& !getline_equal(fgetline, cookie, getexline))
KeyTyped = FALSE;
/*
* Continue executing command lines:
* - when inside an ":if", ":while" or ":for"
* - for multiple commands on one line, separated with '|'
* - when repeating until there are no more lines (for ":source")
*/
next_cmdline = cmdline;
do
{
#ifdef FEAT_EVAL
getline_is_func = getline_equal(fgetline, cookie, get_func_line);
#endif
// stop skipping cmds for an error msg after all endif/while/for
if (next_cmdline == NULL
#ifdef FEAT_EVAL
&& !force_abort
&& cstack.cs_idx < 0
&& !(getline_is_func && func_has_abort(real_cookie))
#endif
)
{
#ifdef FEAT_EVAL
did_emsg_cumul += did_emsg;
#endif
did_emsg = FALSE;
}
/*
* 1. If repeating a line in a loop, get a line from lines_ga.
* 2. If no line given: Get an allocated line with fgetline().
* 3. If a line is given: Make a copy, so we can mess with it.
*/
#ifdef FEAT_EVAL
// 1. If repeating, get a previous line from lines_ga.
if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
{
// Each '|' separated command is stored separately in lines_ga, to
// be able to jump to it. Don't use next_cmdline now.
VIM_CLEAR(cmdline_copy);
// Check if a function has returned or, unless it has an unclosed
// try conditional, aborted.
if (getline_is_func)
{
# ifdef FEAT_PROFILE
if (do_profiling == PROF_YES)
func_line_end(real_cookie);
# endif
if (func_has_ended(real_cookie))
{
retval = FAIL;
break;
}
}
#ifdef FEAT_PROFILE
else if (do_profiling == PROF_YES
&& getline_equal(fgetline, cookie, getsourceline))
script_line_end();
#endif
// Check if a sourced file hit a ":finish" command.
if (source_finished(fgetline, cookie))
{
retval = FAIL;
break;
}
// If breakpoints have been added/deleted need to check for it.
if (breakpoint != NULL && dbg_tick != NULL
&& *dbg_tick != debug_tick)
{
*breakpoint = dbg_find_breakpoint(
getline_equal(fgetline, cookie, getsourceline),
fname, SOURCING_LNUM);
*dbg_tick = debug_tick;
}
next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
// Did we encounter a breakpoint?
if (breakpoint != NULL && *breakpoint != 0
&& *breakpoint <= SOURCING_LNUM)
{
dbg_breakpoint(fname, SOURCING_LNUM);
// Find next breakpoint.
*breakpoint = dbg_find_breakpoint(
getline_equal(fgetline, cookie, getsourceline),
fname, SOURCING_LNUM);
*dbg_tick = debug_tick;
}
# ifdef FEAT_PROFILE
if (do_profiling == PROF_YES)
{
if (getline_is_func)
func_line_start(real_cookie, SOURCING_LNUM);
else if (getline_equal(fgetline, cookie, getsourceline))
script_line_start();
}
# endif
}
#endif
// 2. If no line given, get an allocated line with fgetline().
if (next_cmdline == NULL)
{
/*
* Need to set msg_didout for the first line after an ":if",
* otherwise the ":if" will be overwritten.
*/
if (count == 1 && getline_equal(fgetline, cookie, getexline))
msg_didout = TRUE;
if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
#ifdef FEAT_EVAL
cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
#else
0
#endif
, in_vim9script() ? GETLINE_CONCAT_CONTBAR
: GETLINE_CONCAT_CONT)) == NULL)
{
// Don't call wait_return() for aborted command line. The NULL
// returned for the end of a sourced file or executed function
// doesn't do this.
if (KeyTyped && !(flags & DOCMD_REPEAT))
need_wait_return = FALSE;
retval = FAIL;
break;
}
used_getline = TRUE;
/*
* Keep the first typed line. Clear it when more lines are typed.
*/
if (flags & DOCMD_KEEPLINE)
{
vim_free(repeat_cmdline);
if (count == 0)
repeat_cmdline = vim_strsave(next_cmdline);
else
repeat_cmdline = NULL;
}
}
// 3. Make a copy of the command so we can mess with it.
else if (cmdline_copy == NULL)
{
next_cmdline = vim_strsave(next_cmdline);
if (next_cmdline == NULL)
{
emsg(_(e_out_of_memory));
retval = FAIL;
break;
}
}
cmdline_copy = next_cmdline;
#ifdef FEAT_EVAL
/*
* Inside a while/for loop, and when the command looks like a ":while"
* or ":for", the line is stored, because we may need it later when
* looping.
*
* When there is a '|' and another command, it is stored separately,
* because we need to be able to jump back to it from an
* :endwhile/:endfor.
*
* Pass a different "fgetline" function to do_one_cmd() below,
* that it stores lines in or reads them from "lines_ga". Makes it
* possible to define a function inside a while/for loop and handles
* line continuation.
*/
if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
{
cmd_getline = get_loop_line;
cmd_cookie = (void *)&cmd_loop_cookie;
cmd_loop_cookie.lines_gap = &lines_ga;
cmd_loop_cookie.current_line = current_line;
cmd_loop_cookie.getline = fgetline;
cmd_loop_cookie.cookie = cookie;
cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
// Save the current line when encountering it the first time.
if (current_line == lines_ga.ga_len
&& store_loop_line(&lines_ga, next_cmdline) == FAIL)
{
retval = FAIL;
break;
}
current_line_before = current_line;
}
else
{
cmd_getline = fgetline;
cmd_cookie = cookie;
}
did_endif = FALSE;
#endif
if (count++ == 0)
{
/*
* All output from the commands is put below each other, without
* waiting for a return. Don't do this when executing commands
* from a script or when being called recursive (e.g. for ":e
* +command file").
*/
if (!(flags & DOCMD_NOWAIT) && !recursive)
{
msg_didout_before_start = msg_didout;
msg_didany = FALSE; // no output yet
msg_start();
msg_scroll = TRUE; // put messages below each other
++no_wait_return; // don't wait for return until finished
++RedrawingDisabled;
did_inc = TRUE;
}
}
if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
/*
* 2. Execute one '|' separated command.
* do_one_cmd() will return NULL if there is no trailing '|'.
* "cmdline_copy" can change, e.g. for '%' and '#' expansion.
*/
++recursive;
next_cmdline = do_one_cmd(&cmdline_copy, flags,
#ifdef FEAT_EVAL
&cstack,
#endif
cmd_getline, cmd_cookie);
--recursive;
#ifdef FEAT_EVAL