Mercurial > vim
comparison src/autocmd.c @ 25463:05f9e8f2016c v8.2.3268
patch 8.2.3268: cannot use a block with :autocmd like with :command
Commit: https://github.com/vim/vim/commit/73b8b0ae3acac220e823076f8ca1c14524ed96dd
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Aug 1 14:52:32 2021 +0200
patch 8.2.3268: cannot use a block with :autocmd like with :command
Problem: Cannot use a block with :autocmd like with :command.
Solution: Add support for a {} block after :autocmd. (closes https://github.com/vim/vim/issues/8620)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 01 Aug 2021 15:00:05 +0200 |
parents | f8619a303e9d |
children | ddc223a7f5f5 |
comparison
equal
deleted
inserted
replaced
25462:66d8f0116a76 | 25463:05f9e8f2016c |
---|---|
256 | 256 |
257 static int au_need_clean = FALSE; // need to delete marked patterns | 257 static int au_need_clean = FALSE; // need to delete marked patterns |
258 | 258 |
259 static char_u *event_nr2name(event_T event); | 259 static char_u *event_nr2name(event_T event); |
260 static int au_get_grouparg(char_u **argp); | 260 static int au_get_grouparg(char_u **argp); |
261 static int do_autocmd_event(event_T event, char_u *pat, int once, int nested, char_u *cmd, int forceit, int group); | 261 static int do_autocmd_event(event_T event, char_u *pat, int once, int nested, char_u *cmd, int forceit, int group, int flags); |
262 static int apply_autocmds_group(event_T event, char_u *fname, char_u *fname_io, int force, int group, buf_T *buf, exarg_T *eap); | 262 static int apply_autocmds_group(event_T event, char_u *fname, char_u *fname_io, int force, int group, buf_T *buf, exarg_T *eap); |
263 static void auto_next_pat(AutoPatCmd *apc, int stop_at_last); | 263 static void auto_next_pat(AutoPatCmd *apc, int stop_at_last); |
264 static int au_find_group(char_u *name); | 264 static int au_find_group(char_u *name); |
265 | 265 |
266 static event_T last_event; | 266 static event_T last_event; |
613 int i; | 613 int i; |
614 char_u *s; | 614 char_u *s; |
615 | 615 |
616 for (current_augroup = -1; current_augroup < augroups.ga_len; | 616 for (current_augroup = -1; current_augroup < augroups.ga_len; |
617 ++current_augroup) | 617 ++current_augroup) |
618 do_autocmd((char_u *)"", TRUE); | 618 do_autocmd(NULL, (char_u *)"", TRUE); |
619 | 619 |
620 for (i = 0; i < augroups.ga_len; ++i) | 620 for (i = 0; i < augroups.ga_len; ++i) |
621 { | 621 { |
622 s = ((char_u **)(augroups.ga_data))[i]; | 622 s = ((char_u **)(augroups.ga_data))[i]; |
623 if (s != get_deleted_augroup()) | 623 if (s != get_deleted_augroup()) |
821 * :autocmd bufleave * set tw=79 nosmartindent ic infercase | 821 * :autocmd bufleave * set tw=79 nosmartindent ic infercase |
822 * | 822 * |
823 * :autocmd * *.c show all autocommands for *.c files. | 823 * :autocmd * *.c show all autocommands for *.c files. |
824 * | 824 * |
825 * Mostly a {group} argument can optionally appear before <event>. | 825 * Mostly a {group} argument can optionally appear before <event>. |
826 * "eap" can be NULL. | |
826 */ | 827 */ |
827 void | 828 void |
828 do_autocmd(char_u *arg_in, int forceit) | 829 do_autocmd(exarg_T *eap, char_u *arg_in, int forceit) |
829 { | 830 { |
830 char_u *arg = arg_in; | 831 char_u *arg = arg_in; |
831 char_u *pat; | 832 char_u *pat; |
832 char_u *envpat = NULL; | 833 char_u *envpat = NULL; |
833 char_u *cmd; | 834 char_u *cmd; |
835 int cmd_need_free = FALSE; | |
834 event_T event; | 836 event_T event; |
835 int need_free = FALSE; | 837 char_u *tofree = NULL; |
836 int nested = FALSE; | 838 int nested = FALSE; |
837 int once = FALSE; | 839 int once = FALSE; |
838 int group; | 840 int group; |
839 int i; | 841 int i; |
842 int flags = 0; | |
840 | 843 |
841 if (*arg == '|') | 844 if (*arg == '|') |
842 { | 845 { |
843 arg = (char_u *)""; | 846 arg = (char_u *)""; |
844 group = AUGROUP_ALL; // no argument, use all groups | 847 group = AUGROUP_ALL; // no argument, use all groups |
933 * Find the start of the commands. | 936 * Find the start of the commands. |
934 * Expand <sfile> in it. | 937 * Expand <sfile> in it. |
935 */ | 938 */ |
936 if (*cmd != NUL) | 939 if (*cmd != NUL) |
937 { | 940 { |
941 if (eap != NULL) | |
942 // Read a {} block if it follows. | |
943 cmd = may_get_cmd_block(eap, cmd, &tofree, &flags); | |
944 | |
938 cmd = expand_sfile(cmd); | 945 cmd = expand_sfile(cmd); |
939 if (cmd == NULL) // some error | 946 if (cmd == NULL) // some error |
940 return; | 947 return; |
941 need_free = TRUE; | 948 cmd_need_free = TRUE; |
942 } | 949 } |
943 } | 950 } |
944 | 951 |
945 /* | 952 /* |
946 * Print header when showing autocommands. | 953 * Print header when showing autocommands. |
960 emsg(_(e_cannot_define_autocommands_for_all_events)); | 967 emsg(_(e_cannot_define_autocommands_for_all_events)); |
961 else | 968 else |
962 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; | 969 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
963 event = (event_T)((int)event + 1)) | 970 event = (event_T)((int)event + 1)) |
964 if (do_autocmd_event(event, pat, | 971 if (do_autocmd_event(event, pat, |
965 once, nested, cmd, forceit, group) == FAIL) | 972 once, nested, cmd, forceit, group, flags) == FAIL) |
966 break; | 973 break; |
967 } | 974 } |
968 else | 975 else |
969 { | 976 { |
970 while (*arg && *arg != '|' && !VIM_ISWHITE(*arg)) | 977 while (*arg && *arg != '|' && !VIM_ISWHITE(*arg)) |
971 if (do_autocmd_event(event_name2nr(arg, &arg), pat, | 978 if (do_autocmd_event(event_name2nr(arg, &arg), pat, |
972 once, nested, cmd, forceit, group) == FAIL) | 979 once, nested, cmd, forceit, group, flags) == FAIL) |
973 break; | 980 break; |
974 } | 981 } |
975 | 982 |
976 if (need_free) | 983 if (cmd_need_free) |
977 vim_free(cmd); | 984 vim_free(cmd); |
985 vim_free(tofree); | |
978 vim_free(envpat); | 986 vim_free(envpat); |
979 } | 987 } |
980 | 988 |
981 /* | 989 /* |
982 * Find the group ID in a ":autocmd" or ":doautocmd" argument. | 990 * Find the group ID in a ":autocmd" or ":doautocmd" argument. |
1022 char_u *pat, | 1030 char_u *pat, |
1023 int once, | 1031 int once, |
1024 int nested, | 1032 int nested, |
1025 char_u *cmd, | 1033 char_u *cmd, |
1026 int forceit, | 1034 int forceit, |
1027 int group) | 1035 int group, |
1036 int flags) | |
1028 { | 1037 { |
1029 AutoPat *ap; | 1038 AutoPat *ap; |
1030 AutoPat **prev_ap; | 1039 AutoPat **prev_ap; |
1031 AutoCmd *ac; | 1040 AutoCmd *ac; |
1032 AutoCmd **prev_ac; | 1041 AutoCmd **prev_ac; |
1249 ac = ALLOC_ONE(AutoCmd); | 1258 ac = ALLOC_ONE(AutoCmd); |
1250 if (ac == NULL) | 1259 if (ac == NULL) |
1251 return FAIL; | 1260 return FAIL; |
1252 ac->cmd = vim_strsave(cmd); | 1261 ac->cmd = vim_strsave(cmd); |
1253 ac->script_ctx = current_sctx; | 1262 ac->script_ctx = current_sctx; |
1263 if (flags & UC_VIM9) | |
1264 ac->script_ctx.sc_version = SCRIPT_VERSION_VIM9; | |
1254 #ifdef FEAT_EVAL | 1265 #ifdef FEAT_EVAL |
1255 ac->script_ctx.sc_lnum += SOURCING_LNUM; | 1266 ac->script_ctx.sc_lnum += SOURCING_LNUM; |
1256 #endif | 1267 #endif |
1257 if (ac->cmd == NULL) | 1268 if (ac->cmd == NULL) |
1258 { | 1269 { |