reuse of buffer number in :vimgrep

75 views
Skip to first unread message

leit...@gmail.com

unread,
Feb 4, 2021, 5:02:19 PM2/4/21
to vim_dev
Hi all,

We recently received an issue about using :vimgrep in the Go plugin govim.

In govim we handle `BufRead` autocommands for .go files, and send notifications to the Language Server that a new file has been opened.

As :vimgrep search through files, we do get autocommands for each and every file, but sending notifications for all .go files searched will put a lot of load on the language server as it thinks that the user actually opened (and closed) all files searched within just a second.

Is there a way to differentiate BufRead autocommands triggered by :vimgrep from others? It would be great if we could identify those and filter them, or even better if they didn't trigger autocommands in the first place.

I also noticed that vim will keep the same buffer numbers as :vimgrep search through files without a match. If it do find a match the file & buffer is kept and the buffer number is increased. For those we obviously do want the autocommand as they are indeed opened.

I can't find any information about if this is expected, except for the "Vim Buffer FAQ" over at https://vim.fandom.com/wiki/Vim_buffer_FAQ. According to that FAQ a buffer number should never be reused.

Are there cases where we should expect that a buffer number is reused within the same vim session?

Regards, Pontus

Yegappan Lakshmanan

unread,
Feb 4, 2021, 5:38:57 PM2/4/21
to vim_dev
Hi,

On Thu, Feb 4, 2021 at 2:02 PM leit...@gmail.com <leit...@gmail.com> wrote:
Hi all,

We recently received an issue about using :vimgrep in the Go plugin govim.

In govim we handle `BufRead` autocommands for .go files, and send notifications to the Language Server that a new file has been opened.

As :vimgrep search through files, we do get autocommands for each and every file, but sending notifications for all .go files searched will put a lot of load on the language server as it thinks that the user actually opened (and closed) all files searched within just a second.

Is there a way to differentiate BufRead autocommands triggered by :vimgrep from others? It would be great if we could identify those and filter them, or even better if they didn't trigger autocommands in the first place.


The MRU (https://github.com/yegappan/mru) plugin uses the QuickFixCmdPre and
QuickFixCmdPost autocmds to stop adding the buffers to the MRU list when a
vimgrep command is executed. You can see whether that will help.


Regards,
Yegappan
 
I also noticed that vim will keep the same buffer numbers as :vimgrep search through files without a match. If it do find a match the file & buffer is kept and the buffer number is increased. For those we obviously do want the autocommand as they are indeed opened.

I can't find any information about if this is expected, except for the "Vim Buffer FAQ" over at https://vim.fandom.com/wiki/Vim_buffer_FAQ. According to that FAQ a buffer number should never be reused.

Are there cases where we should expect that a buffer number is reused within the same vim session?

Regards, Pontus

leit...@gmail.com

unread,
Feb 8, 2021, 3:03:50 PM2/8/21
to vim_dev
First of all, thank you for the quick response!

My new approach is to keep track of all BufRead autocmds that happen between QuickFixCmdPre and QuickFixCmdPost, and explicitly call bufexists() on each one of them afterwards to get all buffers that was actually added. The reason being that I do actually want to handle those.
Do that sound like a good plan? It seems to work well so far.

Also, is it intended behaviour that vim do reuse buffer numbers, or do you want me to write an issue about it?
If it is intended, are there other cases where it could happen? The current design in govim assumes that buffer numbers are unique and never reused.

Regards, Pontus

Bram Moolenaar

unread,
Feb 8, 2021, 3:54:14 PM2/8/21
to vim...@googlegroups.com, leit...@gmail.com

> Also, is it intended behaviour that vim do reuse buffer numbers, or do you
> want me to write an issue about it?
> If it is intended, are there other cases where it could happen? The current
> design in govim assumes that buffer numbers are unique and never reused.

In general buffer numbers are not reused. However, for the scratch
buffer that grep uses an exception was made to avoid it eating up a lot
of buffer numbers.

--
You know you use Vim too much when you have this alias in your
~/.bashrc file: alias :e=/bin/vim (Eljay Love-Jensen)

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

leit...@gmail.com

unread,
Feb 8, 2021, 4:21:49 PM2/8/21
to vim_dev
I see, then we'll just handle that case for now.

Thank you Bram
Reply all
Reply to author
Forward
0 new messages