To: vim_dev@googlegroups.com Subject: Patch 8.2.5011 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.5011 Problem: Replacing an autocommand requires several lines. Solution: Add the "replace" flag to autocmd_add(). (Yegappan Lakshmanan, closes #10473) Files: runtime/doc/autocmd.txt, runtime/doc/builtin.txt, src/autocmd.c, src/testdir/test_autocmd.vim *** ../vim-8.2.5010/runtime/doc/autocmd.txt 2022-05-22 14:48:26.319247292 +0100 --- runtime/doc/autocmd.txt 2022-05-24 11:39:08.838745186 +0100 *************** *** 47,52 **** --- 47,74 ---- It's a good idea to use the same autocommands for the File* and Buf* events when possible. + Recommended use: + - Always use a group, so that it's easy to delete the autocommand. + - Keep the command itself short, call a function to do more work. + - Make it so that the script it is defined it can be sourced several times + without the autocommand being repeated. + + Example in Vim9 script: > + autocmd_add({replace: true, + group: 'DemoGroup', + event: 'BufEnter', + pattern: '*.txt', + cmd: 'call DemoBufEnter()' + }) + + In legacy script: > + call autocmd_add(#{replace: v:true, + \ group: 'DemoGroup', + \ event: 'BufEnter', + \ pattern: '*.txt', + \ cmd: 'call DemoBufEnter()' + \ }) + ============================================================================== 2. Defining autocommands *autocmd-define* *************** *** 83,89 **** } The |autocmd_add()| function can be used to add a list of autocmds and autocmd ! groups from a Vim script. Note: The ":autocmd" command can only be followed by another command when the '|' appears where the pattern is expected. This works: > --- 105,112 ---- } The |autocmd_add()| function can be used to add a list of autocmds and autocmd ! groups from a Vim script. It is preferred if you have anything that would ! require using `:execute` with `:autocmd`. Note: The ":autocmd" command can only be followed by another command when the '|' appears where the pattern is expected. This works: > *** ../vim-8.2.5010/runtime/doc/builtin.txt 2022-05-22 14:48:26.323247294 +0100 --- runtime/doc/builtin.txt 2022-05-24 11:23:40.967498279 +0100 *************** *** 940,952 **** If this group doesn't exist then it is created. If not specified or empty, then the default group is used. ! nested set to v:true to add a nested autocmd. ! Refer to |autocmd-nested|. ! once set to v:true to add a autocmd which executes ! only once. Refer to |autocmd-once|. pattern autocmd pattern string. Refer to |autocmd-patterns|. If "bufnr" item is present, then this item is ignored. Returns v:true on success and v:false on failure. Examples: > --- 940,958 ---- If this group doesn't exist then it is created. If not specified or empty, then the default group is used. ! nested boolean flag, set to v:true to add a nested ! autocmd. Refer to |autocmd-nested|. ! once boolean flag, set to v:true to add a autocmd ! which executes only once. Refer to ! |autocmd-once|. pattern autocmd pattern string. Refer to |autocmd-patterns|. If "bufnr" item is present, then this item is ignored. + replace boolean flag, set to v:true to remove all the + commands associated with the specified autocmd + event and group and add the {cmd}. This is + useful to avoid adding the same command + multiple times for a autocmd event in a group. Returns v:true on success and v:false on failure. Examples: > *************** *** 1037,1046 **** cmd Command executed for this autocmd. event Autocmd event name. group Autocmd group name. ! nested Set to v:true for a nested autocmd. See ! |autocmd-nested|. ! once Set to v:true, if the autocmd will be executed ! only once. See |autocmd-once|. pattern Autocmd pattern. For a buffer-local autocmd, this will be of the form "". If there are multiple commands for an autocmd event in a --- 1043,1052 ---- cmd Command executed for this autocmd. event Autocmd event name. group Autocmd group name. ! nested Boolean flag, set to v:true for a nested ! autocmd. See |autocmd-nested|. ! once Boolean flag, set to v:true, if the autocmd ! will be executed only once. See |autocmd-once|. pattern Autocmd pattern. For a buffer-local autocmd, this will be of the form "". If there are multiple commands for an autocmd event in a *** ../vim-8.2.5010/src/autocmd.c 2022-05-19 10:31:06.965630508 +0100 --- src/autocmd.c 2022-05-24 11:23:40.971498274 +0100 *************** *** 2766,2771 **** --- 2766,2772 ---- char_u *end; int once; int nested; + int replace; // replace the cmd for a group/event int retval = VVAL_TRUE; int save_augroup = current_augroup; *************** *** 2877,2882 **** --- 2878,2886 ---- once = dict_get_bool(event_dict, (char_u *)"once", FALSE); nested = dict_get_bool(event_dict, (char_u *)"nested", FALSE); + // if 'replace' is true, then remove all the commands associated with + // this autocmd event/group and add the new command. + replace = dict_get_bool(event_dict, (char_u *)"replace", FALSE); cmd = dict_get_string(event_dict, (char_u *)"cmd", TRUE); if (cmd == NULL) *************** *** 2903,2910 **** } else { ! if (do_autocmd_event(event, pat, once, nested, cmd, delete, group, ! 0) == FAIL) { retval = VVAL_FALSE; break; --- 2907,2914 ---- } else { ! if (do_autocmd_event(event, pat, once, nested, cmd, ! delete | replace, group, 0) == FAIL) { retval = VVAL_FALSE; break; *** ../vim-8.2.5010/src/testdir/test_autocmd.vim 2022-05-19 10:31:06.969630503 +0100 --- src/testdir/test_autocmd.vim 2022-05-24 11:23:40.975498274 +0100 *************** *** 3413,3418 **** --- 3413,3430 ---- \ nested: v:false, once: v:false, event: 'BufHidden'}], \ autocmd_get(#{group: 'TestAcSet'})) + " Test for replacing a cmd for an event in a group + call autocmd_delete([#{group: 'TestAcSet'}]) + call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter', + \ pattern: '*.py', cmd: 'echo "bufenter"'}]) + call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter', + \ pattern: '*.py', cmd: 'echo "bufenter"'}]) + call assert_equal([ + \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py', + \ nested: v:false, once: v:false, event: 'BufEnter'}], + \ autocmd_get(#{group: 'TestAcSet'})) + + " Test for adding a command for an unsupported autocmd event let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh', \ cmd: 'echo "bufadd"'}] call assert_fails('call autocmd_add(l)', 'E216:') *** ../vim-8.2.5010/src/version.c 2022-05-23 21:49:04.235459528 +0100 --- src/version.c 2022-05-24 11:25:48.227415334 +0100 *************** *** 736,737 **** --- 736,739 ---- { /* Add new patch number below this line */ + /**/ + 5011, /**/ -- Facepalm reply #9: "Speed up, you can drive 80 here" "Why, the cars behind us are also driving 60" /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///