Skip to content

autocmd: introduce "++once" flag #4100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

Conversation

justinmk
Copy link
Contributor

@justinmk justinmk commented Mar 12, 2019

Adds a new feature to :autocmd which sets the handler to be executed once, then removed.

Before:

augroup FooGroup
  autocmd!
  autocmd FileType foo call Foo() | autocmd! FooGroup FileType foo
augroup END

After:

autocmd FileType foo ++once call Foo()
  • commit 2 renames the once flag to -once and also adds support for -nested. This is to avoid yet another "flags" pattern not seen anywhere else in Vim, and instead be consistent with e.g. :command flags pattern.
  • commit 3 renames again, to ++once and ++nested

Adds a new feature to :autocmd which sets the handler to be executed at
most one times.

Before:

    augroup FooGroup
      autocmd!
      autocmd FileType foo call Foo() | autocmd! FooGroup * <buffer>
    augroup END

After:

    autocmd FileType foo once call Foo()
@codecov-io
Copy link

codecov-io commented Mar 12, 2019

Codecov Report

Merging #4100 into master will decrease coverage by <.01%.
The diff coverage is 66.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4100      +/-   ##
==========================================
- Coverage   79.24%   79.24%   -0.01%     
==========================================
  Files         105      105              
  Lines      141163   141177      +14     
==========================================
+ Hits       111860   111871      +11     
- Misses      29303    29306       +3
Impacted Files Coverage Δ
src/autocmd.c 86.37% <66.66%> (-0.54%) ⬇️
src/screen.c 80.34% <0%> (-0.03%) ⬇️
src/quickfix.c 93.44% <0%> (-0.01%) ⬇️
src/version.c 86.28% <0%> (ø) ⬆️
src/window.c 83.6% <0%> (+0.06%) ⬆️
src/gui.c 58.2% <0%> (+0.15%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 55d81cd...7040ec6. Read the comment docs.

@Houl
Copy link
Contributor

Houl commented Mar 12, 2019

That's weird syntax. I'd think :au <once> MyGroup BufRead * :DoSomething would be more familiar, similar to :h <nomodeline>. If :-once was a valid command, it would be hidden by your syntax.

@justinmk
Copy link
Contributor Author

justinmk commented Mar 12, 2019

If any of MyGroup BufRead * were valid commands then they also would be "hidden" as :autocmd struggles to decide which is an augroup and which is a valid command. It's all weird because :autocmd is weird. Hyphenated flags are slightly less weird and as mentioned consistent with :command itself.

So let's not get mired in hypotheticals, the goal is only to be slightly less inconsistent with Vim's predominant idioms.

@brammool
Copy link
Contributor

brammool commented Mar 13, 2019 via email

justinmk added a commit to justinmk/neovim that referenced this pull request Mar 13, 2019
Based on feedback from upstream:
vim/vim#4100
@justinmk justinmk changed the title autocmd: introduce "-once" flag autocmd: introduce "++once" flag Mar 13, 2019
@justinmk
Copy link
Contributor Author

Renamed to ++once and ++nested.

justinmk added a commit to justinmk/neovim that referenced this pull request Mar 13, 2019
Based on feedback from upstream:
vim/vim#4100
justinmk added a commit to justinmk/neovim that referenced this pull request Mar 13, 2019
Based on feedback from upstream:
vim/vim#4100
justinmk added a commit to justinmk/neovim that referenced this pull request Mar 14, 2019
Based on feedback from upstream:
vim/vim#4100
@Houl
Copy link
Contributor

Houl commented Mar 14, 2019

:doautocmd accepts the <nomodeline> argument (I'm repeating myself here). For the sake of a bit of consistency, this should become ++nomodeline then (?). Alternatively: change ++once to <once> and ++nested to <nested>. I think while :command is quite unrelated, :autocmd and :doautocmd are quite strongly related to each other. Maybe this doesn't matter at all, just wanted to mention this.

@justinmk
Copy link
Contributor Author

justinmk commented Mar 14, 2019

Alternatively: change ++once to <once> and ++nested to <nested>.

Angle-bracket variants are unpleasant to type as already mentioned.

@ilAYAli
Copy link

ilAYAli commented Mar 14, 2019

Renamed to ++once and ++nested.

hmm.. that looks like an obscure way to pre-increment the option to “twice”

@Houl
Copy link
Contributor

Houl commented Mar 15, 2019

Angle-bracket variants are unpleasant to type as already mentioned.
Mentioned by someone who introduced this notation for :doau. I'm fine with whatever choice, but this argument is silly.

Problem: If autocmd pattern only contained `++once` handlers, and
         all of them completed, then there would be an empty group
         displayed by `:autocmd Foo`.
Solution: Delete the pattern if all of its commands were deleted.
@brammool brammool closed this in eb93f3f Apr 4, 2019
@justinmk
Copy link
Contributor Author

justinmk commented Apr 4, 2019

Thanks, Bram!

@justinmk justinmk deleted the aucmd-once branch April 4, 2019 13:17
justinmk added a commit to neovim/neovim that referenced this pull request Apr 6, 2019
Problem:    Making an autocommand trigger once is not so easy.
Solution:   Add the ++once argument.  Also add ++nested as an alias for
            "nested". (Justin M. Keyes, closes vim/vim#4100)
vim/vim@eb93f3f
lucc added a commit to lucc/nvimpager that referenced this pull request Sep 25, 2019
The main cat mode function is started in an autocomand but normal
autocomands don't nest.  They are made nesting in order to detect and
highlight the filetype on buffer switches in highlight() (see :h
autocmd-nested).

The "++nested" syntax in the docs was introduced with neovim v0.4 (and
vim/vim#4100).  For compatibility with neovim <
0.4 we use the old "nested" which still seems to work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants