[patch] Add TagNotFound autocmd event

124 views
Skip to first unread message

Anton Lindqvist

unread,
Feb 3, 2016, 8:04:46 AM2/3/16
to vim...@vim.org
Hi,
This patch add a autocmd event called TagNotFound triggered when the requested
tag was not found. The motivation for this event is as follows: I usually add
the following to my vimrc in order to ensure my tags file is up-to-date:

au BufWritePost * if filereadable('tags') | silent exe '!ctags -a %' | endif

However this seems wasteful since the majority of my writes don't include new
identifiers to be indexed. With this patch the command above could be replaced
with:

au TagNotFound * if filereadable('tags') | silent exe '!ctags' | endif

Thus indexing would only be performed when a missing tag is requested.

Another use-case for this new event would be to trigger a psearch when the tag
was not found:

au TagNotFound * exe 'psearch /' . expand('<amatch>') . '/'

BTW: what's the recommended cinoptions for the Vim codebase?
add-tagnotfound-autocmd-event.patch

Anton Lindqvist

unread,
Feb 3, 2016, 9:06:47 AM2/3/16
to vim_dev, vim...@vim.org

A unintentional change to one of the Makefile was present in the patch. See the attached revised patch.

add-tagnotfound-autocmd-event.patch

Bram Moolenaar

unread,
Feb 4, 2016, 3:22:45 PM2/4/16
to Anton Lindqvist, vim...@vim.org
Thanks, it looks useful. It still doesn't catch the case that a tag is
found but it's now in a different place. I wonder if we can do
something more clever, only rebuild the tags file when one of the files
change AND using a tag command. Might not be so easy.

> BTW: what's the recommended cinoptions for the Vim codebase?

Empty, that's my preference :-)

--
hundred-and-one symptoms of being an internet addict:
137. You decide to stay in college for an additional year or two,
just so you can have the free Internet access.

/// 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 ///

Christian Brabandt

unread,
Feb 4, 2016, 3:36:31 PM2/4/16
to vim...@vim.org
Hi Anton!
While I agree that this is useful, I have been thinking, if a more
general approach would not be more useful. Something like an Error
autocommand, that triggers on the EXXX numbers?

Best,
Christian
--
Neue Viren:
Reich-Ranicki Virus: Liest alle Ihre Textdateien ein, sendet sie ins
Internet und bewertet sie dort hinsichtlich ihrer literarischen Qualität.

Anton Lindqvist

unread,
Feb 4, 2016, 4:01:26 PM2/4/16
to vim...@vim.org, Christian Brabandt
Christian,

On Thu, Feb 04, 2016 at 09:36:21PM +0100, Christian Brabandt wrote:
> While I agree that this is useful, I have been thinking, if a more
> general approach would not be more useful. Something like an Error
> autocommand, that triggers on the EXXX numbers?

I really like this idea, especially if <amatch> would be the actual error
message since it would allow people to get creative. Having limited experience
with the Vim codebase: would it be feasible to trigger the autocmd event
somewhere along the call stack for the emsg functions? At first glance it looks
like that solution would require less changes. Compared to adding a explicit
call to apply_autocmds prior calling any of the emsg functions.

--
:wq

Christian Brabandt

unread,
Feb 4, 2016, 4:04:53 PM2/4/16
to vim...@vim.org
That's what my half working patch did. It is here:
https://github.com/chrisbra/vim-mq-patches/blob/master/error_aucmd
you might want to look into it. Never got around finishing it.

Best,
Christian

Anton Lindqvist

unread,
Feb 5, 2016, 5:55:04 AM2/5/16
to Christian Brabandt, vim...@vim.org
On Thu, Feb 04, 2016 at 10:04:44PM +0100, Christian Brabandt wrote:
> That's what my half working patch did. It is here:
> https://github.com/chrisbra/vim-mq-patches/blob/master/error_aucmd
> you might want to look into it. Never got around finishing it.

Thanks, I will try to finish your patch.

--
:wq

Anton Lindqvist

unread,
Feb 6, 2016, 5:08:07 AM2/6/16
to vim...@vim.org, Christian Brabandt
On Thu, Feb 04, 2016 at 10:04:44PM +0100, Christian Brabandt wrote:
Christian,
I am able to apply your patch and it works fine. Here's an example of a
potential use-case of mine:

" Run ctags if the tag file is missing (E433) or the tag was not found (E426)
augroup ctags
au! Error E4\(26\|33\)* silent exe '!ctags'
augroup END

Are you aware of any bugs with your patch or other potential gotchas that needs
to be resolved?

--
:wq

Christian Brabandt

unread,
Feb 6, 2016, 8:43:34 AM2/6/16
to vim...@vim.org
Hi Anton!

On Sa, 06 Feb 2016, Anton Lindqvist wrote:

> I am able to apply your patch and it works fine. Here's an example of a
> potential use-case of mine:
>
> " Run ctags if the tag file is missing (E433) or the tag was not found (E426)
> augroup ctags
> au! Error E4\(26\|33\)* silent exe '!ctags'
> augroup END

Great!

> Are you aware of any bugs with your patch or other potential gotchas
> that needs to be resolved?

I think it was about some error codes not being caught, but I don't
remember the details. However, if you got it working, please submit it.
We might need to add some tests however.

Best,
Christian
--
Tu erst das Notwendige, dann das Mögliche, und plötzlich schaffst Du
das Unmögliche.
-- Franz von Assisi

Bram Moolenaar

unread,
Feb 6, 2016, 9:28:51 AM2/6/16
to Anton Lindqvist, vim...@vim.org, Christian Brabandt
Although this sounds like a nice general solution, it will require the
code that gives the error message to be prepared for an autocommand
kicking in. Otherwise, whatever the autocommand does may completely
mess up what the code was doing. We have had many autocommands cause
trouble and still fixing more.

So the code would explicitly check for an autocommand that handles the
error. And since we need to do that, we might as well use a nicer name
than the error number. That also helps for when there can be multiple
errors. E.g. "tagnotfound" is much nicer than matching a list of error
codes. And more error codes could be added later.

An alternative would be to match the error code, but not trigger the
autocommand yet. In the main loop we can then check for the matches
and execute the autocommand. That is a lot safer and simpler.

Nevertheless, you probably want to do something more clever, since after
rebuilding the tags file you would want to search for a match again.

--
hundred-and-one symptoms of being an internet addict:
151. You find yourself engaged to someone you've never actually met,
except through e-mail.

Anton Lindqvist

unread,
Feb 7, 2016, 5:35:15 AM2/7/16
to vim...@vim.org, Bram Moolenaar
Bram,
Thanks for your input. I'm working on a new patch which will extract the logic
to the main loop.

> Nevertheless, you probably want to do something more clever, since after
> rebuilding the tags file you would want to search for a match again.

I've been thinking on a way to detect the last tag related normal mode mapping
or command without any progress. However being forced to invoke the tag search
normal mode mapping or execute the latest command line entry again isn't to
frustrating to me.

--
:wq

Anton Lindqvist

unread,
Feb 8, 2016, 2:31:58 PM2/8/16
to vim...@vim.org, Bram Moolenaar, Christian Brabandt
Hi,
See the new patch[1].

[1] https://groups.google.com/d/msg/vim_dev/3_Mn38Jk3N0/W3TQ0g1WDQAJ

On Sat, Feb 06, 2016 at 03:28:37PM +0100, Bram Moolenaar wrote:
>
--
:wq
Reply all
Reply to author
Forward
0 new messages