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

Already on GitHub? Sign in to your account

Center the screen on the tag after jumping. #339

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
2 participants

This also makes the behavior consistent with buffertag, changes, and
quickfix.

@jszakmeister jszakmeister Center the screen on the tag after jumping.
This also makes the behavior consistent with buffertag, changes, and
quickfix.
6be5bfe
Owner

kien commented Jan 1, 2013

Thanks! But tag.vim doesn't have it because the :tag, :tjump... commands don't always jump to the tag's position (because of an outdated tags file, for example). I suggest you create a mapping for zvzz, like ,z, which can be used elsewhere.

@kien kien closed this Jan 1, 2013

I still think it's nice to center, even if it doesn't land where it's supposed to. Centering at least gives you a fair chance to find the actual tag in either direction. Plus, it makes where to look for the cursor consistent. At the moment, you have no idea where you're going to be. And I'd like to avoid the extra keystrokes.

Owner

kien commented Jan 1, 2013

If :tag fails, running zvzz blindly will open the folds and change the view of the current buffer.

Would it be okay if I make this an option or is there someway to hook it to get the behavior I'd like? I've been testing this for a little while, and I really like it. It's a huge speed improvement to see the contents for a function right away, instead of having to center it myself. In the case that the tags are off, I haven't run into a case where I've been unhappy with the result. If the signature still matches, Vim still searches for the tag and finds it. If the signature is off, it does expand the fold, but then I could see the target nearby. And in most cases, Vim still found the right location via searching.

If you're really opposed, I'll happily run my patched version. I just think it's a nice feature for users. Thanks for all your help!

Owner

kien commented Jan 1, 2013

Vim has the 'foldopen' option, but it only applies when the command is typed directly in the command line. I'll see if using feedkeys() can get around this.

@kien kien added a commit that referenced this pull request Jan 9, 2013

@kien kien Run :tag as if typed
Refs #339
e71cb9f
Owner

kien commented Jan 9, 2013

Done. Let me know if you find any problem with this.

This does help with opening the folds. Can we now safely add the centering? Just calling sil! norm! zz didn't work, but wrapping it with feedkeys as well did:

diff --git a/autoload/ctrlp/tag.vim b/autoload/ctrlp/tag.vim
index bb78744..f10d6c0 100644
--- a/autoload/ctrlp/tag.vim
+++ b/autoload/ctrlp/tag.vim
@@ -111,6 +111,7 @@ fu! ctrlp#tag#accept(mode, str)
    el
        sil! cal feedkeys(":".cmd." ".tg."\r", 'nt')
    en
+   sil! cal feedkeys("zz", 'nt')
    cal ctrlp#setlcdir()
 endf
Owner

kien commented Jan 9, 2013

The reason for using feedkeys() is so that 'foldopen' will take effect automatically. There's nothing for zz. If :tag fails, running zz will still change the view of the current buffer regardless. So no, it's not safe. You'll just have to type zz manually.

Right, I understood why you used feedkeys(). My goal was to still center the tag. With the original way, wouldn't a failure in :tag cause an exception and stop execution? I'm not sure how it works with feedkeys(), but at least you would have been pretty sure not to change the view on error since you'd never get to the line that does the centering.

I appreciate your effort, but I'm not content to hit zz manually, so I'll maintain a forked version if that's what I need to do. Thanks for all your help!

Owner

kien commented Jan 9, 2013

Actually no, unless in a :try :catch, Vim will continue with the next command, in this case with zz whether it's in feedkeys() or not.

Bah. I always forget that VimScript operates differently than most languages. One last try here, and I'll give up trying to convince you. What about something like this then:

diff --git a/autoload/ctrlp/tag.vim b/autoload/ctrlp/tag.vim
index bb78744..fb6d9fe 100644
--- a/autoload/ctrlp/tag.vim
+++ b/autoload/ctrlp/tag.vim
@@ -106,10 +106,17 @@ fu! ctrlp#tag#accept(mode, str)
        en
        let save_cst = &cst
        set cst&
-       sil! cal feedkeys(":".fnd[1]."ta ".tg."\r", 'nt')
-       let &cst = save_cst
+       try
+           sil! cal feedkeys(":".fnd[1]."ta ".tg."\r", 'nt')
+           sil! cal feedkeys("zz", 'nt')
+       finally
+           let &cst = save_cst
+       endtry
    el
-       sil! cal feedkeys(":".cmd." ".tg."\r", 'nt')
+       try
+           sil! cal feedkeys(":".cmd." ".tg."\r", 'nt')
+           sil! cal feedkeys("zz", 'nt')
+       endtry
    en
    cal ctrlp#setlcdir()
 endf

That'll keep the zz from executing when :tag fails, but center the line if it does. I wish the fix were a little more elegant, but we've got the two paths here.

Owner

kien commented Jan 9, 2013

The problem with that is without :catch, the commands after :endtry won't be executed. I also don't like using :try with commands that involve opening of files because :try can stop an autocmd from firing if an error happens before it.

I spent last evening this morning looking over error and exception handling in VimScript. I see what you're talking about. There's really no clean way to make sure the :tag command was successful. Even checking v:errmsg seems wrong because an autocmd may have done a silent! .... It's a real shame that there's no good way of coping with an error here. :-(

Thank you very much for taking the time to explain your position. It was very instructive, and pushed me to dig in further to VimScript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment